blob: 304e3cb6845c05e34add520780d7c283a9039700 [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 Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010021#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
23#include <sys/time.h>
Willy Tarreau077edcb2016-08-10 18:30:56 +020024#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020027#include <haproxy/applet.h>
Emeric Brun12941c82020-07-07 14:19:42 +020028#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020029#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020031#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020033#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020034#include <haproxy/http_ana.h>
Emeric Brun12941c82020-07-07 14:19:42 +020035#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036#include <haproxy/log.h>
Emeric Brun12941c82020-07-07 14:19:42 +020037#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020039#include <haproxy/sc_strm.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/sink.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020041#include <haproxy/ssl_sock.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020042#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020043#include <haproxy/stream.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
Willy Tarreau211ea252022-03-17 19:47:33 +0100736/* frees log server <logsrv> after freeing all of its allocated fields. The
737 * server must not belong to a list anymore. Logsrv may be NULL, which is
738 * silently ignored.
739 */
740void free_logsrv(struct logsrv *logsrv)
741{
742 if (!logsrv)
743 return;
744
745 BUG_ON(LIST_INLIST(&logsrv->list));
746 ha_free(&logsrv->conf.file);
747 ha_free(&logsrv->ring_name);
748 free(logsrv);
749}
750
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200751/*
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200752 * Parse "log" keyword and update <logsrvs> list accordingly.
753 *
754 * When <do_del> is set, it means the "no log" line was parsed, so all log
755 * servers in <logsrvs> are released.
756 *
757 * Otherwise, we try to parse the "log" line. First of all, when the list is not
758 * the global one, we look for the parameter "global". If we find it,
759 * global.logsrvs is copied. Else we parse each arguments.
760 *
761 * The function returns 1 in success case, otherwise, it returns 0 and err is
762 * filled.
763 */
Emeric Brun9533a702021-04-02 10:13:43 +0200764int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err)
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200765{
Willy Tarreauae32ac72020-10-27 09:51:37 +0100766 struct smp_log_range *smp_rgs = NULL;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200767 struct sockaddr_storage *sk;
Emeric Brun94aab062021-04-02 10:41:36 +0200768 struct protocol *proto;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200769 struct logsrv *logsrv = NULL;
770 int port1, port2;
771 int cur_arg;
Willy Tarreau89599262020-09-15 14:03:26 +0200772 int fd;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200773
774 /*
775 * "no log": delete previous herited or defined syslog
776 * servers.
777 */
778 if (do_del) {
779 struct logsrv *back;
780
781 if (*(args[1]) != 0) {
782 memprintf(err, "'no log' does not expect arguments");
783 goto error;
784 }
785
786 list_for_each_entry_safe(logsrv, back, logsrvs, list) {
Willy Tarreau211ea252022-03-17 19:47:33 +0100787 LIST_DEL_INIT(&logsrv->list);
788 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200789 }
790 return 1;
791 }
792
793 /*
794 * "log global": copy global.logrsvs linked list to the end of logsrvs
795 * list. But first, we check (logsrvs != global.logsrvs).
796 */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100797 if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200798 if (logsrvs == &global.logsrvs) {
799 memprintf(err, "'global' is not supported for a global syslog server");
800 goto error;
801 }
802 list_for_each_entry(logsrv, &global.logsrvs, list) {
Christopher Faulet28ac0992018-03-26 16:09:19 +0200803 struct logsrv *node;
804
805 list_for_each_entry(node, logsrvs, list) {
806 if (node->ref == logsrv)
807 goto skip_logsrv;
808 }
809
810 node = malloc(sizeof(*node));
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200811 memcpy(node, logsrv, sizeof(struct logsrv));
Christopher Faulet28ac0992018-03-26 16:09:19 +0200812 node->ref = logsrv;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200813 LIST_INIT(&node->list);
Willy Tarreau2b718102021-04-21 07:32:39 +0200814 LIST_APPEND(logsrvs, &node->list);
Willy Tarreau211ea252022-03-17 19:47:33 +0100815 node->ring_name = logsrv->ring_name ? strdup(logsrv->ring_name) : NULL;
Emeric Brun9533a702021-04-02 10:13:43 +0200816 node->conf.file = strdup(file);
817 node->conf.line = linenum;
Christopher Faulet28ac0992018-03-26 16:09:19 +0200818
819 skip_logsrv:
820 continue;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200821 }
822 return 1;
823 }
824
825 /*
826 * "log <address> ...: parse a syslog server line
827 */
828 if (*(args[1]) == 0 || *(args[2]) == 0) {
829 memprintf(err, "expects <address> and <facility> %s as arguments",
830 ((logsrvs == &global.logsrvs) ? "" : "or global"));
831 goto error;
832 }
833
Willy Tarreau5a32ecc2018-11-12 07:34:59 +0100834 /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */
835 if (strcmp(args[1], "stdout") == 0)
836 args[1] = "fd@1";
837 else if (strcmp(args[1], "stderr") == 0)
838 args[1] = "fd@2";
839
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200840 logsrv = calloc(1, sizeof(*logsrv));
841 if (!logsrv) {
842 memprintf(err, "out of memory");
843 goto error;
844 }
Christopher Fauletb4f96ed2022-03-29 14:17:09 +0200845 LIST_INIT(&logsrv->list);
Emeric Brun9533a702021-04-02 10:13:43 +0200846 logsrv->conf.file = strdup(file);
847 logsrv->conf.line = linenum;
848
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200849 /* skip address for now, it will be parsed at the end */
850 cur_arg = 2;
851
852 /* just after the address, a length may be specified */
853 logsrv->maxlen = MAX_SYSLOG_LEN;
854 if (strcmp(args[cur_arg], "len") == 0) {
855 int len = atoi(args[cur_arg+1]);
856 if (len < 80 || len > 65535) {
857 memprintf(err, "invalid log length '%s', must be between 80 and 65535",
858 args[cur_arg+1]);
859 goto error;
860 }
861 logsrv->maxlen = len;
862 cur_arg += 2;
863 }
864 if (logsrv->maxlen > global.max_syslog_len)
865 global.max_syslog_len = logsrv->maxlen;
866
867 /* after the length, a format may be specified */
868 if (strcmp(args[cur_arg], "format") == 0) {
869 logsrv->format = get_log_format(args[cur_arg+1]);
Emeric Brun54648852020-07-06 15:54:06 +0200870 if (logsrv->format == LOG_FORMAT_UNSPEC) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200871 memprintf(err, "unknown log format '%s'", args[cur_arg+1]);
872 goto error;
873 }
874 cur_arg += 2;
875 }
876
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200877 if (strcmp(args[cur_arg], "sample") == 0) {
878 unsigned low, high;
879 char *p, *beg, *end, *smp_sz_str;
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200880 size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz;
881
882 p = args[cur_arg+1];
883 smp_sz_str = strchr(p, ':');
884 if (!smp_sz_str) {
885 memprintf(err, "Missing sample size");
886 goto error;
887 }
888
889 *smp_sz_str++ = '\0';
890
891 end = p + strlen(p);
892
893 while (p != end) {
894 if (!get_logsrv_smp_range(&low, &high, &p, err))
895 goto error;
896
897 if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err))
898 goto error;
899
900 smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs);
901 if (!smp_rgs) {
902 memprintf(err, "out of memory error");
903 goto error;
904 }
905
906 smp_rgs[smp_rgs_sz].low = low;
907 smp_rgs[smp_rgs_sz].high = high;
908 smp_rgs[smp_rgs_sz].sz = high - low + 1;
909 smp_rgs[smp_rgs_sz].curr_idx = 0;
910 if (smp_rgs[smp_rgs_sz].high > smp_sz)
911 smp_sz = smp_rgs[smp_rgs_sz].high;
912 smp_rgs_sz++;
913 }
914
Tim Duesterhus21648002019-06-23 22:10:10 +0200915 if (smp_rgs == NULL) {
916 memprintf(err, "no sampling ranges given");
917 goto error;
918 }
919
Frédéric Lécailled95ea282019-04-24 16:14:33 +0200920 beg = smp_sz_str;
921 end = beg + strlen(beg);
922 new_smp_sz = read_uint((const char **)&beg, end);
923 if (!new_smp_sz || beg != end) {
924 memprintf(err, "wrong sample size '%s' for sample range '%s'",
925 smp_sz_str, args[cur_arg+1]);
926 goto error;
927 }
928
929 if (new_smp_sz < smp_sz) {
930 memprintf(err, "sample size %zu should be greater or equal to "
931 "%zu the maximum of the high ranges limits",
932 new_smp_sz, smp_sz);
933 goto error;
934 }
935 smp_sz = new_smp_sz;
936
937 /* Let's order <smp_rgs> array. */
938 qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp);
939
940 logsrv->lb.smp_rgs = smp_rgs;
941 logsrv->lb.smp_rgs_sz = smp_rgs_sz;
942 logsrv->lb.smp_sz = smp_sz;
943
944 cur_arg += 2;
945 }
Frédéric Lécailled803e472019-04-25 07:42:09 +0200946 HA_SPIN_INIT(&logsrv->lock);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +0200947 /* parse the facility */
948 logsrv->facility = get_log_facility(args[cur_arg]);
949 if (logsrv->facility < 0) {
950 memprintf(err, "unknown log facility '%s'", args[cur_arg]);
951 goto error;
952 }
953 cur_arg++;
954
955 /* parse the max syslog level (default: debug) */
956 logsrv->level = 7;
957 if (*(args[cur_arg])) {
958 logsrv->level = get_log_level(args[cur_arg]);
959 if (logsrv->level < 0) {
960 memprintf(err, "unknown optional log level '%s'", args[cur_arg]);
961 goto error;
962 }
963 cur_arg++;
964 }
965
966 /* parse the limit syslog level (default: emerg) */
967 logsrv->minlvl = 0;
968 if (*(args[cur_arg])) {
969 logsrv->minlvl = get_log_level(args[cur_arg]);
970 if (logsrv->minlvl < 0) {
971 memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]);
972 goto error;
973 }
974 cur_arg++;
975 }
976
977 /* Too many args */
978 if (*(args[cur_arg])) {
979 memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]);
980 goto error;
981 }
982
983 /* now, back to the address */
Willy Tarreauf3dc30f2019-08-30 14:18:44 +0200984 logsrv->type = LOG_TARGET_DGRAM;
Willy Tarreauc046d162019-08-30 15:24:59 +0200985 if (strncmp(args[1], "ring@", 5) == 0) {
Willy Tarreauc046d162019-08-30 15:24:59 +0200986 logsrv->addr.ss_family = AF_UNSPEC;
987 logsrv->type = LOG_TARGET_BUFFER;
Emeric Brun99c453d2020-05-25 15:01:04 +0200988 logsrv->sink = NULL;
989 logsrv->ring_name = strdup(args[1] + 5);
Willy Tarreauc046d162019-08-30 15:24:59 +0200990 goto done;
991 }
992
Emeric Brun94aab062021-04-02 10:41:36 +0200993 sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto,
994 err, NULL, NULL,
995 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 +0200996 if (!sk)
997 goto error;
Willy Tarreau89599262020-09-15 14:03:26 +0200998
999 if (fd != -1)
1000 logsrv->type = LOG_TARGET_FD;
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001001 logsrv->addr = *sk;
1002
1003 if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) {
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001004 if (!port1)
1005 set_host_port(&logsrv->addr, SYSLOG_PORT);
1006 }
Emeric Brun9533a702021-04-02 10:13:43 +02001007
Willy Tarreau91b47262022-05-20 16:36:46 +02001008 if (proto && proto->xprt_type == PROTO_TYPE_STREAM) {
Emeric Brun94aab062021-04-02 10:41:36 +02001009 static unsigned long ring_ids;
1010
1011 /* Implicit sink buffer will be
1012 * initialized in post_check
1013 */
1014 logsrv->type = LOG_TARGET_BUFFER;
1015 logsrv->sink = NULL;
1016 /* compute uniq name for the ring */
1017 memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids);
1018 }
1019
Willy Tarreauc046d162019-08-30 15:24:59 +02001020 done:
Willy Tarreau2b718102021-04-21 07:32:39 +02001021 LIST_APPEND(logsrvs, &logsrv->list);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001022 return 1;
1023
1024 error:
Willy Tarreauae32ac72020-10-27 09:51:37 +01001025 free(smp_rgs);
Willy Tarreau211ea252022-03-17 19:47:33 +01001026 free_logsrv(logsrv);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +02001027 return 0;
1028}
1029
Amaury Denoyelle7b01a8d2021-03-29 10:29:07 +02001030
1031/*
Emeric Brun54648852020-07-06 15:54:06 +02001032 * returns log format, LOG_FORMAT_UNSPEC is return if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +02001033 */
Emeric Brun54648852020-07-06 15:54:06 +02001034enum log_fmt get_log_format(const char *fmt)
Dragan Dosen1322d092015-09-22 16:05:32 +02001035{
Emeric Brun54648852020-07-06 15:54:06 +02001036 enum log_fmt format;
Dragan Dosen1322d092015-09-22 16:05:32 +02001037
1038 format = LOG_FORMATS - 1;
Emeric Brun54648852020-07-06 15:54:06 +02001039 while (format > 0 && log_formats[format].name
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001040 && strcmp(log_formats[format].name, fmt) != 0)
Dragan Dosen1322d092015-09-22 16:05:32 +02001041 format--;
1042
Emeric Brun54648852020-07-06 15:54:06 +02001043 /* Note: 0 is LOG_FORMAT_UNSPEC */
Dragan Dosen1322d092015-09-22 16:05:32 +02001044 return format;
1045}
1046
1047/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02001048 * returns log level for <lev> or -1 if not found.
1049 */
1050int get_log_level(const char *lev)
1051{
1052 int level;
1053
1054 level = NB_LOG_LEVELS - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001055 while (level >= 0 && strcmp(log_levels[level], lev) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001056 level--;
1057
1058 return level;
1059}
1060
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061/*
1062 * returns log facility for <fac> or -1 if not found.
1063 */
1064int get_log_facility(const char *fac)
1065{
1066 int facility;
1067
1068 facility = NB_LOG_FACILITIES - 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001069 while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001071
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 return facility;
1073}
1074
William Lallemanda1cc3812012-02-08 16:38:44 +01001075/*
Dragan Dosen835b9212016-02-12 13:23:03 +01001076 * Encode the string.
1077 *
1078 * When using the +E log format option, it will try to escape '"\]'
1079 * characters with '\' as prefix. The same prefix should not be used as
1080 * <escape>.
1081 */
1082static char *lf_encode_string(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001083 const char escape, const long *map,
Dragan Dosen835b9212016-02-12 13:23:03 +01001084 const char *string,
1085 struct logformat_node *node)
1086{
1087 if (node->options & LOG_OPT_ESC) {
1088 if (start < stop) {
1089 stop--; /* reserve one byte for the final '\0' */
1090 while (start < stop && *string != '\0') {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001091 if (!ha_bit_test((unsigned char)(*string), map)) {
1092 if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001093 *start++ = *string;
1094 else {
1095 if (start + 2 >= stop)
1096 break;
1097 *start++ = '\\';
1098 *start++ = *string;
1099 }
1100 }
1101 else {
1102 if (start + 3 >= stop)
1103 break;
1104 *start++ = escape;
1105 *start++ = hextab[(*string >> 4) & 15];
1106 *start++ = hextab[*string & 15];
1107 }
1108 string++;
1109 }
1110 *start = '\0';
1111 }
1112 }
1113 else {
1114 return encode_string(start, stop, escape, map, string);
1115 }
1116
1117 return start;
1118}
1119
1120/*
1121 * Encode the chunk.
1122 *
1123 * When using the +E log format option, it will try to escape '"\]'
1124 * characters with '\' as prefix. The same prefix should not be used as
1125 * <escape>.
1126 */
1127static char *lf_encode_chunk(char *start, char *stop,
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001128 const char escape, const long *map,
Willy Tarreau83061a82018-07-13 11:56:34 +02001129 const struct buffer *chunk,
Dragan Dosen835b9212016-02-12 13:23:03 +01001130 struct logformat_node *node)
1131{
1132 char *str, *end;
1133
1134 if (node->options & LOG_OPT_ESC) {
1135 if (start < stop) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001136 str = chunk->area;
1137 end = chunk->area + chunk->data;
Dragan Dosen835b9212016-02-12 13:23:03 +01001138
1139 stop--; /* reserve one byte for the final '\0' */
1140 while (start < stop && str < end) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001141 if (!ha_bit_test((unsigned char)(*str), map)) {
1142 if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
Dragan Dosen835b9212016-02-12 13:23:03 +01001143 *start++ = *str;
1144 else {
1145 if (start + 2 >= stop)
1146 break;
1147 *start++ = '\\';
1148 *start++ = *str;
1149 }
1150 }
1151 else {
1152 if (start + 3 >= stop)
1153 break;
1154 *start++ = escape;
1155 *start++ = hextab[(*str >> 4) & 15];
1156 *start++ = hextab[*str & 15];
1157 }
1158 str++;
1159 }
1160 *start = '\0';
1161 }
1162 }
1163 else {
1164 return encode_chunk(start, stop, escape, map, chunk);
1165 }
1166
1167 return start;
1168}
1169
1170/*
William Lallemanda1cc3812012-02-08 16:38:44 +01001171 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +01001172 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +01001173 *
Joseph Herlant85b40592018-11-15 12:10:04 -08001174 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +01001175 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001176char *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 +01001177{
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001178 if (size < 2)
1179 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +01001180
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001181 if (node->options & LOG_OPT_QUOTE) {
1182 *(dst++) = '"';
1183 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001185
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001186 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001187 if (++len > size)
1188 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +01001189 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001190 char *ret;
1191
Dragan Dosendb1b6f92016-07-25 11:35:02 +02001192 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +01001193 if (ret == NULL || *ret != '\0')
1194 return NULL;
1195 len = ret - dst;
1196 }
1197 else {
Dragan Dosen835b9212016-02-12 13:23:03 +01001198 len = strlcpy2(dst, src, len);
1199 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001200
1201 size -= len;
1202 dst += len;
1203 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +01001204 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
1205 if (size < 2)
1206 return NULL;
1207 *(dst++) = '-';
1208 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001209
1210 if (node->options & LOG_OPT_QUOTE) {
1211 if (size < 2)
1212 return NULL;
1213 *(dst++) = '"';
1214 }
1215
1216 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001217 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +01001218}
1219
Willy Tarreau26ffa852018-09-05 15:23:10 +02001220static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node)
Willy Tarreau2b0108a2012-12-21 19:23:44 +01001221{
1222 return lf_text_len(dst, src, size, size, node);
1223}
1224
William Lallemand5f232402012-04-05 18:02:55 +02001225/*
Joseph Herlant85b40592018-11-15 12:10:04 -08001226 * Write a IP address to the log string
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001227 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001228 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001229char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001230{
1231 char *ret = dst;
1232 int iret;
1233 char pn[INET6_ADDRSTRLEN];
1234
1235 if (node->options & LOG_OPT_HEXA) {
Radek Zajic594c4562019-03-22 10:21:54 +00001236 unsigned char *addr = NULL;
1237 switch (sockaddr->sa_family) {
1238 case AF_INET:
1239 addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
1240 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
1241 break;
1242 case AF_INET6:
1243 addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr;
1244 iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
1245 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7],
1246 addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]);
1247 break;
1248 default:
1249 return NULL;
1250 }
William Lallemand5f232402012-04-05 18:02:55 +02001251 if (iret < 0 || iret > size)
1252 return NULL;
1253 ret += iret;
1254 } else {
1255 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
1256 ret = lf_text(dst, pn, size, node);
1257 if (ret == NULL)
1258 return NULL;
1259 }
1260 return ret;
1261}
1262
1263/*
1264 * Write a port to the log
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001265 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +02001266 */
Willy Tarreau26ffa852018-09-05 15:23:10 +02001267char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node)
William Lallemand5f232402012-04-05 18:02:55 +02001268{
1269 char *ret = dst;
1270 int iret;
1271
1272 if (node->options & LOG_OPT_HEXA) {
1273 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
1274 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
1275 if (iret < 0 || iret > size)
1276 return NULL;
1277 ret += iret;
1278 } else {
1279 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
1280 if (ret == NULL)
1281 return NULL;
1282 }
1283 return ret;
1284}
1285
Emeric Brun54648852020-07-06 15:54:06 +02001286
1287/*
1288 * This function sends the syslog message using a printf format string. It
1289 * expects an LF-terminated message.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001290 */
Emeric Brun54648852020-07-06 15:54:06 +02001291void send_log(struct proxy *p, int level, const char *format, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292{
Emeric Brun54648852020-07-06 15:54:06 +02001293 va_list argp;
1294 int data_len;
1295
1296 if (level < 0 || format == NULL || logline == NULL)
1297 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298
Emeric Brun54648852020-07-06 15:54:06 +02001299 va_start(argp, format);
1300 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
1301 if (data_len < 0 || data_len > global.max_syslog_len)
1302 data_len = global.max_syslog_len;
1303 va_end(argp);
Willy Tarreaufe944602007-10-25 10:34:16 +02001304
Emeric Brun54648852020-07-06 15:54:06 +02001305 __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level,
1306 logline, data_len, default_rfc5424_sd_log_format, 2);
1307}
1308/*
1309 * This function builds a log header of given format using given
1310 * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries
1311 * to determine format based on given metadas. It is useful
1312 * for log-forwarding to be able to forward any format without
1313 * settings.
1314 * This function returns a struct ist array of elements of the header
1315 * nbelem is set to the number of available elements.
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001316 * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS
Emeric Brun54648852020-07-06 15:54:06 +02001317 * elements.
1318 */
1319struct ist *build_log_header(enum log_fmt format, int level, int facility,
1320 struct ist *metadata, size_t *nbelem)
1321{
1322 static THREAD_LOCAL struct {
1323 struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS];
1324 char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1];
1325 time_t cur_legacy_time;
1326 char priority_buffer[6];
1327 } hdr_ctx = { .priority_buffer = "<<<<>" };
Willy Tarreaubaaee002006-06-26 02:48:02 +02001328
Emeric Brun54648852020-07-06 15:54:06 +02001329 struct tm logtime;
1330 int len;
1331 int fac_level = 0;
1332 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001333
Emeric Brun54648852020-07-06 15:54:06 +02001334 *nbelem = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001335
Emeric Brun54648852020-07-06 15:54:06 +02001336
1337 if (format == LOG_FORMAT_UNSPEC) {
1338 format = LOG_FORMAT_RAW;
1339 if (metadata) {
1340 /* If a hostname is set, it appears we want to perform syslog
1341 * because only rfc5427 or rfc3164 support an hostname.
1342 */
1343 if (metadata[LOG_META_HOST].len) {
1344 /* If a rfc5424 compliant timestamp is used we consider
1345 * that output format is rfc5424, else legacy format
1346 * is used as specified default for local logs
1347 * in documentation.
1348 */
1349 if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-')
1350 || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN))
1351 format = LOG_FORMAT_RFC5424;
1352 else
1353 format = LOG_FORMAT_RFC3164;
1354 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001355 else if (metadata[LOG_META_TAG].len) {
1356 /* Tag is present but no hostname, we should
Ilya Shipitsinf38a0182020-12-21 01:16:17 +05001357 * consider we try to emit a local log
Emeric Brun0237c4e2020-11-27 16:24:34 +01001358 * in legacy format (analog to RFC3164 but
1359 * with stripped hostname).
1360 */
1361 format = LOG_FORMAT_LOCAL;
1362 }
Emeric Brun54648852020-07-06 15:54:06 +02001363 else if (metadata[LOG_META_PRIO].len) {
1364 /* the source seems a parsed message
1365 * offering a valid level/prio prefix
1366 * so we consider this format.
1367 */
1368 format = LOG_FORMAT_PRIO;
1369 }
1370 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001371 }
1372
Emeric Brun54648852020-07-06 15:54:06 +02001373 /* prepare priority, stored into 1 single elem */
1374 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001375 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001376 case LOG_FORMAT_RFC3164:
1377 case LOG_FORMAT_RFC5424:
1378 case LOG_FORMAT_PRIO:
1379 fac_level = facility << 3;
1380 /* further format ignore the facility */
1381 /* fall through */
1382 case LOG_FORMAT_TIMED:
1383 case LOG_FORMAT_SHORT:
1384 fac_level += level;
1385 hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */
1386 do {
1387 *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10;
1388 fac_level /= 10;
1389 hdr_ctx.ist_vector[*nbelem].ptr--;
1390 } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]);
1391 *hdr_ctx.ist_vector[*nbelem].ptr = '<';
1392 hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr;
1393 break;
1394 case LOG_FORMAT_ISO:
1395 case LOG_FORMAT_RAW:
1396 break;
1397 case LOG_FORMAT_UNSPEC:
1398 case LOG_FORMATS:
1399 ABORT_NOW();
1400 }
Willy Tarreau094af4e2015-01-07 15:03:42 +01001401
William Lallemand2a4a44f2012-02-06 16:00:33 +01001402
Emeric Brun54648852020-07-06 15:54:06 +02001403 /* prepare timestamp, stored into a max of 4 elems */
1404 switch (format) {
Emeric Brun0237c4e2020-11-27 16:24:34 +01001405 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001406 case LOG_FORMAT_RFC3164:
1407 /* rfc3164 ex: 'Jan 1 00:00:00 ' */
1408 if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1409 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1410 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001411 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001412 break;
1413 }
1414 else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1415 int month;
1416 char *timestamp = metadata[LOG_META_TIME].ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +02001417
Emeric Brun54648852020-07-06 15:54:06 +02001418 /* iso time always begins like this: '1970-01-01T00:00:00' */
Dragan Dosen1322d092015-09-22 16:05:32 +02001419
Emeric Brun54648852020-07-06 15:54:06 +02001420 /* compute month */
1421 month = 10*(timestamp[5] - '0') + (timestamp[6] - '0');
1422 if (month)
1423 month--;
1424 if (month <= 11) {
1425 /* builds log prefix ex: 'Jan 1 ' */
1426 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1427 "%s %c%c ", monthname[month],
1428 timestamp[8] != '0' ? timestamp[8] : ' ',
1429 timestamp[9]);
1430 /* we reused the timestamp_buffer, signal that it does not
1431 * contain local time anymore
1432 */
1433 hdr_ctx.cur_legacy_time = 0;
1434 if (len == 7) {
1435 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1436 /* adds 'HH:MM:SS' from iso time */
1437 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[11], 8);
1438 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1439 /* we successfully reuse iso time, we can break */
1440 break;
1441 }
1442 }
1443 /* Failed to reuse isotime time, fallback to local legacy time */
1444 }
Dragan Dosen1322d092015-09-22 16:05:32 +02001445
Emeric Brun54648852020-07-06 15:54:06 +02001446 if (unlikely(time != hdr_ctx.cur_legacy_time)) {
1447 /* re-builds timestamp from the current local time */
1448 get_localtime(time, &logtime);
1449
1450 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1451 "%s %2d %02d:%02d:%02d ",
1452 monthname[logtime.tm_mon],
1453 logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec);
1454 if (len != LOG_LEGACYTIME_LEN+1)
1455 hdr_ctx.cur_legacy_time = 0;
1456 else
1457 hdr_ctx.cur_legacy_time = time;
1458 }
1459 if (likely(hdr_ctx.cur_legacy_time))
1460 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1);
1461 else
1462 hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1);
1463 break;
1464 case LOG_FORMAT_RFC5424:
1465 /* adds rfc5425 version prefix */
1466 hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2);
1467 if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001468 /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */
Emeric Brun54648852020-07-06 15:54:06 +02001469 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1470 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1471 break;
1472 }
1473 /* let continue as 'timed' and 'iso' format for usual timestamp */
1474 /* fall through */
1475 case LOG_FORMAT_TIMED:
1476 case LOG_FORMAT_ISO:
1477 /* ISO format ex: '1900:01:01T12:00:00.123456Z'
1478 * '1900:01:01T14:00:00+02:00'
1479 * '1900:01:01T10:00:00.123456-02:00'
1480 */
1481 if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) {
1482 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME];
1483 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001484 /* time is set, break immediately */
Emeric Brun54648852020-07-06 15:54:06 +02001485 break;
1486 }
1487 else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) {
1488 int month;
1489 char *timestamp = metadata[LOG_META_TIME].ptr;
1490
1491 for (month = 0; month < 12; month++)
1492 if (!memcmp(monthname[month], timestamp, 3))
1493 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001494
Emeric Brun54648852020-07-06 15:54:06 +02001495 if (month < 12) {
1496
1497 /* get local time to retrieve year */
1498 get_localtime(time, &logtime);
1499
1500 /* year seems changed since log */
1501 if (logtime.tm_mon < month)
1502 logtime.tm_year--;
1503
1504 /* builds rfc5424 prefix ex: '1900-01-01T' */
1505 len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer),
1506 "%4d-%02d-%c%cT",
1507 logtime.tm_year+1900, month+1,
1508 timestamp[4] != ' ' ? timestamp[4] : '0',
1509 timestamp[5]);
1510
1511 /* we reused the timestamp_buffer, signal that it does not
1512 * contain local time anymore
1513 */
1514 hdr_ctx.cur_legacy_time = 0;
1515 if (len == 11) {
1516 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len);
1517 /* adds HH:MM:SS from legacy timestamp */
1518 hdr_ctx.ist_vector[(*nbelem)++] = ist2(&timestamp[7], 8);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001519 /* skip secfraq because it is optional */
Emeric Brun54648852020-07-06 15:54:06 +02001520 /* according to rfc: -00:00 means we don't know the timezone */
1521 hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7);
1522 /* we successfully reuse legacy time, we can break */
1523 break;
1524 }
1525 }
1526 /* Failed to reuse legacy time, fallback to local iso time */
1527 }
1528 hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1);
1529 break;
1530 case LOG_FORMAT_PRIO:
1531 case LOG_FORMAT_SHORT:
1532 case LOG_FORMAT_RAW:
1533 break;
1534 case LOG_FORMAT_UNSPEC:
1535 case LOG_FORMATS:
1536 ABORT_NOW();
Dragan Dosen1322d092015-09-22 16:05:32 +02001537 }
1538
Emeric Brun54648852020-07-06 15:54:06 +02001539 /* prepare other meta data, stored into a max of 10 elems */
1540 switch (format) {
1541 case LOG_FORMAT_RFC3164:
1542 if (metadata && metadata[LOG_META_HOST].len) {
1543 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1544 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1545 }
Emeric Brun0237c4e2020-11-27 16:24:34 +01001546 else /* the caller MUST fill the hostname, this field is mandatory */
Emeric Brun54648852020-07-06 15:54:06 +02001547 hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10);
Emeric Brun0237c4e2020-11-27 16:24:34 +01001548 /* fall through */
1549 case LOG_FORMAT_LOCAL:
Emeric Brun54648852020-07-06 15:54:06 +02001550 if (!metadata || !metadata[LOG_META_TAG].len)
1551 break;
Dragan Dosen1322d092015-09-22 16:05:32 +02001552
Emeric Brun54648852020-07-06 15:54:06 +02001553 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1554 if (metadata[LOG_META_PID].len) {
1555 hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1);
1556 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1557 hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1);
1558 }
1559 hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2);
1560 break;
1561 case LOG_FORMAT_RFC5424:
1562 if (metadata && metadata[LOG_META_HOST].len) {
1563 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST];
1564 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1565 }
1566 else
1567 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
Dragan Dosen1322d092015-09-22 16:05:32 +02001568
Emeric Brun54648852020-07-06 15:54:06 +02001569 if (metadata && metadata[LOG_META_TAG].len) {
1570 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG];
1571 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1572 }
1573 else
1574 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001575
Emeric Brun54648852020-07-06 15:54:06 +02001576 if (metadata && metadata[LOG_META_PID].len) {
1577 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID];
1578 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1579 }
1580 else
1581 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001582
Emeric Brun54648852020-07-06 15:54:06 +02001583 if (metadata && metadata[LOG_META_MSGID].len) {
1584 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID];
1585 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1586 }
1587 else
1588 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001589
Emeric Brun54648852020-07-06 15:54:06 +02001590 if (metadata && metadata[LOG_META_STDATA].len) {
1591 hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA];
1592 hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1);
1593 }
1594 else
1595 hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2);
1596 break;
1597 case LOG_FORMAT_PRIO:
1598 case LOG_FORMAT_SHORT:
1599 case LOG_FORMAT_TIMED:
1600 case LOG_FORMAT_ISO:
1601 case LOG_FORMAT_RAW:
1602 break;
1603 case LOG_FORMAT_UNSPEC:
1604 case LOG_FORMATS:
1605 ABORT_NOW();
1606 }
1607
1608 return hdr_ctx.ist_vector;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001609}
1610
1611/*
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001612 * This function sends a syslog message to <logsrv>.
Emeric Brun54648852020-07-06 15:54:06 +02001613 * The argument <metadata> MUST be an array of size
1614 * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001615 * It overrides the last byte of the message vector with an LF character.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001616 * Does not return any error,
William Lallemand2a4a44f2012-02-06 16:00:33 +01001617 */
Emeric Brun54648852020-07-06 15:54:06 +02001618static 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 +01001619{
Emeric Brun54648852020-07-06 15:54:06 +02001620 static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */
Christopher Fauletf8188c62017-06-02 16:20:16 +02001621 static THREAD_LOCAL struct msghdr msghdr = {
1622 //.msg_iov = iovec,
Emeric Brun54648852020-07-06 15:54:06 +02001623 .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001624 };
Christopher Fauletf8188c62017-06-02 16:20:16 +02001625 static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */
1626 static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001627 int *plogfd;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001628 int sent;
Emeric Brun54648852020-07-06 15:54:06 +02001629 size_t nbelem;
1630 struct ist *msg_header = NULL;
Christopher Fauletf8188c62017-06-02 16:20:16 +02001631
1632 msghdr.msg_iov = iovec;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001633
Emeric Brunfa9d7802020-05-28 14:21:33 +02001634 /* historically some messages used to already contain the trailing LF
1635 * or Zero. Let's remove all trailing LF or Zero
1636 */
Emeric Brun54648852020-07-06 15:54:06 +02001637 while (size && (message[size-1] == '\n' || (message[size-1] == 0)))
Emeric Brun9e8ea0a2020-05-12 19:33:15 +02001638 size--;
1639
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001640 if (logsrv->type == LOG_TARGET_BUFFER) {
Willy Tarreauc046d162019-08-30 15:24:59 +02001641 plogfd = NULL;
Emeric Brune709e1e2020-05-06 17:23:59 +02001642 goto send;
Willy Tarreauc046d162019-08-30 15:24:59 +02001643 }
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001644 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1645 /* the socket's address is a file descriptor */
1646 plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
1647 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001648 else if (logsrv->addr.ss_family == AF_UNIX)
1649 plogfd = &logfdunix;
1650 else
1651 plogfd = &logfdinet;
Robert Tsai81ae1952007-12-05 10:47:29 +01001652
Willy Tarreauc046d162019-08-30 15:24:59 +02001653 if (plogfd && unlikely(*plogfd < 0)) {
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001654 /* socket not successfully initialized yet */
1655 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
1656 (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) {
1657 static char once;
William Lallemand0f99e342011-10-12 17:50:54 +02001658
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001659 if (!once) {
1660 once = 1; /* note: no need for atomic ops here */
1661 ha_alert("socket() failed in logger #%d: %s (errno=%d)\n",
1662 nblogger, strerror(errno), errno);
1663 }
1664 return;
1665 } else {
1666 /* we don't want to receive anything on this socket */
1667 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1668 /* does nothing under Linux, maybe needed for others */
1669 shutdown(*plogfd, SHUT_RD);
Willy Tarreau38247432022-04-26 10:24:14 +02001670 fd_set_cloexec(*plogfd);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001671 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001672 }
1673
Emeric Brun54648852020-07-06 15:54:06 +02001674 msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem);
1675 send:
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001676 if (logsrv->type == LOG_TARGET_BUFFER) {
Emeric Brun54648852020-07-06 15:54:06 +02001677 struct ist msg;
1678
1679 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001680 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001681
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001682 sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata);
1683 }
1684 else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) {
1685 struct ist msg;
1686
1687 msg = ist2(message, size);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01001688 msg = isttrim(msg, logsrv->maxlen);
Willy Tarreaua5b325f2020-09-04 16:44:20 +02001689
1690 sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001691 }
1692 else {
Emeric Brun54648852020-07-06 15:54:06 +02001693 int i = 0;
1694 int totlen = logsrv->maxlen;
1695
1696 for (i = 0 ; i < nbelem ; i++ ) {
1697 iovec[i].iov_base = msg_header[i].ptr;
1698 iovec[i].iov_len = msg_header[i].len;
1699 if (totlen <= iovec[i].iov_len) {
1700 iovec[i].iov_len = totlen;
1701 totlen = 0;
1702 break;
1703 }
1704 totlen -= iovec[i].iov_len;
1705 }
1706 if (totlen) {
1707 iovec[i].iov_base = message;
1708 iovec[i].iov_len = size;
1709 if (totlen <= iovec[i].iov_len)
1710 iovec[i].iov_len = totlen;
1711 i++;
1712 }
1713 iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */
1714 iovec[i].iov_len = 1;
1715 i++;
Willy Tarreaud52a7f82019-08-30 14:05:35 +02001716
Emeric Brun54648852020-07-06 15:54:06 +02001717 msghdr.msg_iovlen = i;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001718 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1719 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
Dragan Dosen43885c72015-10-01 13:18:13 +02001720
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001721 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
1722 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001723
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001724 if (sent < 0) {
1725 static char once;
Dragan Dosen43885c72015-10-01 13:18:13 +02001726
Willy Tarreauacef5e22022-04-25 20:32:15 +02001727 if (errno == EAGAIN || errno == EWOULDBLOCK)
Willy Tarreau4781b152021-04-06 13:53:36 +02001728 _HA_ATOMIC_INC(&dropped_logs);
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001729 else if (!once) {
1730 once = 1; /* note: no need for atomic ops here */
1731 ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n",
1732 nblogger, strerror(errno), errno);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001733 }
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001734 }
1735}
Dragan Dosen59cee972015-09-19 22:09:02 +02001736
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001737/*
1738 * This function sends a syslog message.
1739 * It doesn't care about errors nor does it report them.
Emeric Brun54648852020-07-06 15:54:06 +02001740 * The argument <metadata> MUST be an array of size
1741 * LOG_META_FIELDS*sizeof(struct ist) containing
1742 * data to build the header.
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001743 */
Emeric Brun54648852020-07-06 15:54:06 +02001744void process_send_log(struct list *logsrvs, int level, int facility,
1745 struct ist *metadata, char *message, size_t size)
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001746{
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001747 struct logsrv *logsrv;
1748 int nblogger;
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001749
1750 /* Send log messages to syslog server. */
1751 nblogger = 0;
1752 list_for_each_entry(logsrv, logsrvs, list) {
Emeric Brun2f4cc282020-07-10 15:47:11 +02001753 int in_range = 1;
Frédéric Lécailled803e472019-04-25 07:42:09 +02001754
Frédéric Lécaille0bad8402019-04-10 08:22:17 +02001755 /* we can filter the level of the messages that are sent to each logger */
1756 if (level > logsrv->level)
1757 continue;
1758
Frédéric Lécailled803e472019-04-25 07:42:09 +02001759 if (logsrv->lb.smp_rgs) {
1760 struct smp_log_range *curr_rg;
1761
1762 HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
1763 curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
1764 in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx);
1765 if (in_range) {
1766 /* Let's consume this range. */
1767 curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz;
1768 if (!curr_rg->curr_idx) {
1769 /* If consumed, let's select the next range. */
1770 logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
1771 }
1772 }
1773 logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz;
1774 HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
1775 }
1776 if (in_range)
Emeric Brun54648852020-07-06 15:54:06 +02001777 __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl),
1778 (facility == -1) ? logsrv->facility : facility,
1779 metadata, message, size);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001780 }
1781}
1782
Emeric Brun54648852020-07-06 15:54:06 +02001783/*
1784 * This function sends a syslog message.
1785 * It doesn't care about errors nor does it report them.
1786 * The arguments <sd> and <sd_size> are used for the structured-data part
1787 * in RFC5424 formatted syslog messages.
1788 */
1789void __send_log(struct list *logsrvs, struct buffer *tagb, int level,
1790 char *message, size_t size, char *sd, size_t sd_size)
1791{
1792 static THREAD_LOCAL pid_t curr_pid;
1793 static THREAD_LOCAL char pidstr[16];
1794 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
1795
1796 if (logsrvs == NULL) {
1797 if (!LIST_ISEMPTY(&global.logsrvs)) {
1798 logsrvs = &global.logsrvs;
1799 }
1800 }
1801 if (!logsrvs || LIST_ISEMPTY(logsrvs))
1802 return;
1803
1804 if (!metadata[LOG_META_HOST].len) {
1805 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +01001806 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +02001807 }
1808
1809 if (!tagb || !tagb->area)
1810 tagb = &global.log_tag;
1811
1812 if (tagb)
1813 metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data);
1814
1815 if (unlikely(curr_pid != getpid()))
1816 metadata[LOG_META_PID].len = 0;
1817
1818 if (!metadata[LOG_META_PID].len) {
1819 curr_pid = getpid();
1820 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1821 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
1822 }
1823
1824 metadata[LOG_META_STDATA] = ist2(sd, sd_size);
1825
1826 /* Remove trailing space of structured data */
1827 while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ')
1828 metadata[LOG_META_STDATA].len--;
1829
1830 return process_send_log(logsrvs, level, -1, metadata, message, size);
1831}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001832
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001833const 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 +01001834const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1835 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1836 Set-cookie Updated, unknown, unknown */
1837
William Lallemand1d705562012-03-12 12:46:41 +01001838/*
1839 * try to write a character if there is enough space, or goto out
1840 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001841#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001842 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001843 *(tmplog++) = (x); \
1844 } else { \
1845 goto out; \
1846 } \
1847 } while(0)
1848
Dragan Dosen835b9212016-02-12 13:23:03 +01001849
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001850/* Initializes some log data at boot */
1851static void init_log()
Dragan Dosen835b9212016-02-12 13:23:03 +01001852{
1853 char *tmp;
Willy Tarreaue10cd482018-09-10 18:16:53 +02001854 int i;
Dragan Dosen835b9212016-02-12 13:23:03 +01001855
1856 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1857 * inside PARAM-VALUE should be escaped with '\' as prefix.
1858 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1859 * details.
1860 */
1861 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1862
1863 tmp = "\"\\]";
1864 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001865 ha_bit_set(*tmp, rfc5424_escape_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001866 tmp++;
1867 }
Willy Tarreaue10cd482018-09-10 18:16:53 +02001868
1869 /* initialize the log header encoding map : '{|}"#' should be encoded with
1870 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
1871 * URL encoding only requires '"', '#' to be encoded as well as non-
1872 * printable characters above.
1873 */
1874 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
1875 memset(url_encode_map, 0, sizeof(url_encode_map));
1876 for (i = 0; i < 32; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001877 ha_bit_set(i, hdr_encode_map);
1878 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001879 }
1880 for (i = 127; i < 256; i++) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001881 ha_bit_set(i, hdr_encode_map);
1882 ha_bit_set(i, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001883 }
1884
1885 tmp = "\"#{|}";
1886 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001887 ha_bit_set(*tmp, hdr_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001888 tmp++;
1889 }
1890
1891 tmp = "\"#";
1892 while (*tmp) {
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001893 ha_bit_set(*tmp, url_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001894 tmp++;
1895 }
1896
1897 /* initialize the http header encoding map. The draft httpbis define the
1898 * header content as:
1899 *
1900 * HTTP-message = start-line
1901 * *( header-field CRLF )
1902 * CRLF
1903 * [ message-body ]
1904 * header-field = field-name ":" OWS field-value OWS
1905 * field-value = *( field-content / obs-fold )
1906 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
1907 * obs-fold = CRLF 1*( SP / HTAB )
1908 * field-vchar = VCHAR / obs-text
1909 * VCHAR = %x21-7E
1910 * obs-text = %x80-FF
1911 *
1912 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
1913 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
Joseph Herlant85b40592018-11-15 12:10:04 -08001914 * "obs-fold" is voluntarily forgotten because haproxy remove this.
Willy Tarreaue10cd482018-09-10 18:16:53 +02001915 */
1916 memset(http_encode_map, 0, sizeof(http_encode_map));
1917 for (i = 0x00; i <= 0x08; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001918 ha_bit_set(i, http_encode_map);
Willy Tarreaue10cd482018-09-10 18:16:53 +02001919 for (i = 0x0a; i <= 0x1f; i++)
Willy Tarreau1bfd6022019-06-07 11:10:07 +02001920 ha_bit_set(i, http_encode_map);
1921 ha_bit_set(0x7f, http_encode_map);
Dragan Dosen835b9212016-02-12 13:23:03 +01001922}
William Lallemand1d705562012-03-12 12:46:41 +01001923
Willy Tarreaub6b3df32018-11-26 16:31:20 +01001924INITCALL0(STG_PREPARE, init_log);
1925
Christopher Faulet0132d062017-07-26 15:33:35 +02001926/* Initialize log buffers used for syslog messages */
1927int init_log_buffers()
1928{
Christopher Faulet0132d062017-07-26 15:33:35 +02001929 logline = my_realloc2(logline, global.max_syslog_len + 1);
1930 logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1);
Emeric Brun54648852020-07-06 15:54:06 +02001931 if (!logline || !logline_rfc5424)
Christopher Faulet0132d062017-07-26 15:33:35 +02001932 return 0;
1933 return 1;
1934}
1935
1936/* Deinitialize log buffers used for syslog messages */
1937void deinit_log_buffers()
1938{
Christopher Faulet0132d062017-07-26 15:33:35 +02001939 free(logline);
1940 free(logline_rfc5424);
Christopher Faulet0132d062017-07-26 15:33:35 +02001941 logline = NULL;
1942 logline_rfc5424 = NULL;
1943}
1944
Willy Tarreaudf974472012-12-28 02:44:01 +01001945/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1946 * <maxsize> characters. Returns the size of the output string in characters,
1947 * not counting the trailing zero which is always added if the resulting size
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001948 * is not zero. It requires a valid session and optionally a stream. If the
1949 * stream is NULL, default values will be assumed for the stream part.
Willy Tarreaudf974472012-12-28 02:44:01 +01001950 */
Willy Tarreau43c538e2018-09-05 14:58:15 +02001951int 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 +02001952{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001953 struct proxy *fe = sess->fe;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001954 struct proxy *be;
1955 struct http_txn *txn;
1956 const struct strm_logs *logs;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001957 struct connection *fe_conn, *be_conn;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001958 unsigned int s_flags;
1959 unsigned int uniq_id;
Willy Tarreau83061a82018-07-13 11:56:34 +02001960 struct buffer chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001961 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001962 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001963 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001964 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001965 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001966 int t_request;
1967 int hdr;
1968 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001969 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001970 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001971 char *ret;
1972 int iret;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001973 int status;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001974 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001975 struct timeval tv;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001976 struct strm_logs tmp_strm_log;
Maciej Zdebfcdfd852020-11-30 18:27:47 +00001977 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001978 struct http_uri_parser parser;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001979
William Lallemandbddd4fd2012-02-27 11:23:10 +01001980 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001981
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001982 if (likely(s)) {
1983 be = s->be;
1984 txn = s->txn;
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001985 be_conn = sc_conn(s->scb);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02001986 status = (txn ? txn->status : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001987 s_flags = s->flags;
1988 uniq_id = s->uniq_id;
1989 logs = &s->logs;
1990 } else {
1991 /* we have no stream so we first need to initialize a few
1992 * things that are needed later. We do increment the request
1993 * ID so that it's uniquely assigned to this request just as
1994 * if the request had reached the point of being processed.
1995 * A request error is reported as it's the only element we have
1996 * here and which justifies emitting such a log.
1997 */
Christopher Fauletfd818482021-04-14 14:01:41 +02001998 be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02001999 txn = NULL;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002000 fe_conn = objt_conn(sess->origin);
Willy Tarreaubde14ad2022-05-27 10:04:04 +02002001 be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? sc_conn(__objt_check(sess->origin)->sc) : NULL);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002002 status = 0;
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002003 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
Willy Tarreau18515722021-04-06 11:57:41 +02002004 uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002005
2006 /* prepare a valid log structure */
2007 tmp_strm_log.tv_accept = sess->tv_accept;
2008 tmp_strm_log.accept_date = sess->accept_date;
2009 tmp_strm_log.t_handshake = sess->t_handshake;
Christopher Fauletdd789212020-09-30 15:10:07 +02002010 tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0);
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002011 tv_zero(&tmp_strm_log.tv_request);
2012 tmp_strm_log.t_queue = -1;
2013 tmp_strm_log.t_connect = -1;
2014 tmp_strm_log.t_data = -1;
2015 tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now);
2016 tmp_strm_log.bytes_in = 0;
2017 tmp_strm_log.bytes_out = 0;
2018 tmp_strm_log.prx_queue_pos = 0;
2019 tmp_strm_log.srv_queue_pos = 0;
2020
2021 logs = &tmp_strm_log;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002022
2023 if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) {
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002024 enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, &status);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002025
2026 switch (es) {
2027 case MUX_ES_SUCCESS:
2028 break;
2029 case MUX_ES_INVALID_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002030 status = (status ? status : 400);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002031 if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn))
2032 s_flags = SF_ERR_CLICL | SF_FINST_R;
2033 else
2034 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2035 break;
2036 case MUX_ES_TOUT_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002037 status = (status ? status : 408);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002038 s_flags = SF_ERR_CLITO | SF_FINST_R;
2039 break;
Christopher Faulet142dd332020-12-07 11:24:37 +01002040 case MUX_ES_NOTIMPL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002041 status = (status ? status : 501);
Christopher Faulet142dd332020-12-07 11:24:37 +01002042 s_flags = SF_ERR_PRXCOND | SF_FINST_R;
2043 break;
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002044 case MUX_ES_INTERNAL_ERR:
Christopher Fauleta015b3e2021-09-28 11:36:28 +02002045 status = (status ? status : 500);
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002046 s_flags = SF_ERR_INTERNAL | SF_FINST_R;
2047 break;
2048 default:
2049 break;
2050 }
2051 }
Willy Tarreau09bb27c2018-09-05 16:55:15 +02002052 }
2053
William Lallemandbddd4fd2012-02-27 11:23:10 +01002054 t_request = -1;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002055 if (tv_isge(&logs->tv_request, &logs->tv_accept))
2056 t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request);
William Lallemandbddd4fd2012-02-27 11:23:10 +01002057
William Lallemand1d705562012-03-12 12:46:41 +01002058 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02002059
William Lallemandbddd4fd2012-02-27 11:23:10 +01002060 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01002061 if (LIST_ISEMPTY(list_format))
2062 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002063
William Lallemand1d705562012-03-12 12:46:41 +01002064 list_for_each_entry(tmp, list_format, list) {
Christopher Faulet52b28d22021-10-27 11:58:05 +02002065#ifdef USE_OPENSSL
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002066 struct connection *conn;
Christopher Faulet52b28d22021-10-27 11:58:05 +02002067#endif
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002068 const struct sockaddr_storage *addr;
Willy Tarreau4f653562012-10-12 19:48:16 +02002069 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01002070 struct sample *key;
Willy Tarreau83061a82018-07-13 11:56:34 +02002071 const struct buffer empty = { };
William Lallemandbddd4fd2012-02-27 11:23:10 +01002072
Willy Tarreauc8368452012-12-21 00:09:23 +01002073 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01002074 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01002075 if (!last_isspace) {
2076 LOGCHAR(' ');
2077 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002078 }
2079 break;
2080
William Lallemand1d705562012-03-12 12:46:41 +01002081 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01002082 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02002083 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002084 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002085 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002086 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002087 last_isspace = 0;
2088 break;
2089
Willy Tarreauc8368452012-12-21 00:09:23 +01002090 case LOG_FMT_EXPR: // sample expression, may be request or response
2091 key = NULL;
Christopher Faulet5f940702020-04-06 10:40:02 +02002092 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02002093 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 +02002094
Christopher Faulet5f940702020-04-06 10:40:02 +02002095 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02002096 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 +02002097
2098 if (!key && !(tmp->options & (LOG_OPT_REQ_CAP|LOG_OPT_RES_CAP))) // cfg, cli
2099 key = sample_fetch_as_type(be, sess, s, SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
2100
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002101 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01002102 ret = lf_encode_chunk(tmplog, dst + maxsize,
2103 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01002104 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002105 ret = lf_text_len(tmplog,
2106 key ? key->data.u.str.area : NULL,
2107 key ? key->data.u.str.data : 0,
2108 dst + maxsize - tmplog,
2109 tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01002110 if (ret == 0)
2111 goto out;
2112 tmplog = ret;
2113 last_isspace = 0;
2114 break;
2115
Willy Tarreau2beef582012-12-20 17:22:52 +01002116 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaud68ff012022-05-27 08:57:21 +02002117 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002118 if (addr)
2119 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002120 else
2121 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002122
William Lallemand1d705562012-03-12 12:46:41 +01002123 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002124 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002125 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002126 last_isspace = 0;
2127 break;
2128
Willy Tarreau2beef582012-12-20 17:22:52 +01002129 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002130 addr = (s ? sc_src(s->scf) : sess_src(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002131 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002132 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002133 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002134 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002135 else
2136 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002137 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002138 else
2139 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2140
William Lallemand5f232402012-04-05 18:02:55 +02002141 if (ret == NULL)
2142 goto out;
2143 tmplog = ret;
2144 last_isspace = 0;
2145 break;
2146
Willy Tarreau2beef582012-12-20 17:22:52 +01002147 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaud68ff012022-05-27 08:57:21 +02002148 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002149 if (addr)
2150 ret = lf_ip(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002151 else
2152 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2153
William Lallemand1d705562012-03-12 12:46:41 +01002154 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002155 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002156 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002157 last_isspace = 0;
2158 break;
2159
Willy Tarreau2beef582012-12-20 17:22:52 +01002160 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaud68ff012022-05-27 08:57:21 +02002161 addr = (s ? sc_dst(s->scf) : sess_dst(sess));
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002162 if (addr) {
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002163 /* sess->listener is always defined when the session's owner is an inbound connections */
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002164 if (addr->ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002165 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002166 else
Christopher Fauletf9c4d8d2021-10-22 17:43:22 +02002167 ret = lf_port(tmplog, (struct sockaddr *)addr, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02002168 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002169 else
2170 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2171
William Lallemand5f232402012-04-05 18:02:55 +02002172 if (ret == NULL)
2173 goto out;
2174 tmplog = ret;
2175 last_isspace = 0;
2176 break;
2177
Willy Tarreau2beef582012-12-20 17:22:52 +01002178 case LOG_FMT_BACKENDIP: // %bi
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_ip(tmplog, (const 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 Lallemand1d705562012-03-12 12:46:41 +01002184 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002185 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002186 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002187 last_isspace = 0;
2188 break;
2189
Willy Tarreau2beef582012-12-20 17:22:52 +01002190 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002191 if (be_conn && conn_get_src(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002192 ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, 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_SERVERIP: // %si
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_ip(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 Lallemand5f232402012-04-05 18:02:55 +02002208 if (ret == NULL)
2209 goto out;
2210 tmplog = ret;
2211 last_isspace = 0;
2212 break;
2213
Willy Tarreau2beef582012-12-20 17:22:52 +01002214 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau8fa99842019-07-17 11:47:11 +02002215 if (be_conn && conn_get_dst(be_conn))
Willy Tarreau6c6365f2019-07-17 16:48:18 +02002216 ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002217 else
2218 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
2219
William Lallemand1d705562012-03-12 12:46:41 +01002220 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01002221 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002222 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01002223 last_isspace = 0;
2224 break;
2225
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002226 case LOG_FMT_DATE: // %t = accept date
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002227 get_localtime(logs->accept_date.tv_sec, &tm);
2228 ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002229 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002230 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002231 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002232 last_isspace = 0;
2233 break;
2234
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002235 case LOG_FMT_tr: // %tr = start of request date
2236 /* Note that the timers are valid if we get here */
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002237 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 +02002238 get_localtime(tv.tv_sec, &tm);
2239 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
2240 if (ret == NULL)
2241 goto out;
2242 tmplog = ret;
2243 last_isspace = 0;
2244 break;
2245
2246 case LOG_FMT_DATEGMT: // %T = accept date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002247 get_gmtime(logs->accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02002248 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002249 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002250 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002251 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002252 last_isspace = 0;
2253 break;
2254
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002255 case LOG_FMT_trg: // %trg = start of request date, GMT
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002256 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 +02002257 get_gmtime(tv.tv_sec, &tm);
2258 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
2259 if (ret == NULL)
2260 goto out;
2261 tmplog = ret;
2262 last_isspace = 0;
2263 break;
2264
2265 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002266 get_localtime(logs->accept_date.tv_sec, &tm);
2267 ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08002268 if (ret == NULL)
2269 goto out;
2270 tmplog = ret;
2271 last_isspace = 0;
2272 break;
2273
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002274 case LOG_FMT_trl: // %trl = start of request date, local
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002275 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 +02002276 get_localtime(tv.tv_sec, &tm);
2277 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
2278 if (ret == NULL)
2279 goto out;
2280 tmplog = ret;
2281 last_isspace = 0;
2282 break;
2283
William Lallemand5f232402012-04-05 18:02:55 +02002284 case LOG_FMT_TS: // %Ts
William Lallemand5f232402012-04-05 18:02:55 +02002285 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002286 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec);
William Lallemand5f232402012-04-05 18:02:55 +02002287 if (iret < 0 || iret > dst + maxsize - tmplog)
2288 goto out;
2289 last_isspace = 0;
2290 tmplog += iret;
2291 } else {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002292 ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002293 if (ret == NULL)
2294 goto out;
2295 tmplog = ret;
2296 last_isspace = 0;
2297 }
2298 break;
2299
William Lallemand1d705562012-03-12 12:46:41 +01002300 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02002301 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002302 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000);
William Lallemand5f232402012-04-05 18:02:55 +02002303 if (iret < 0 || iret > dst + maxsize - tmplog)
2304 goto out;
2305 last_isspace = 0;
2306 tmplog += iret;
2307 } else {
2308 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002309 goto out;
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002310 ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000,
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002311 tmplog, 4);
2312 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002313 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01002314 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002315 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02002316 }
2317 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002318
William Lallemand1d705562012-03-12 12:46:41 +01002319 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01002320 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02002321 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002322 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002323 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002324 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002325 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002326 break;
2327
Willy Tarreau773d65f2012-10-12 14:56:11 +02002328 case LOG_FMT_FRONTEND_XPRT: // %ft
2329 src = fe->id;
2330 if (tmp->options & LOG_OPT_QUOTE)
2331 LOGCHAR('"');
2332 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
2333 if (iret == 0)
2334 goto out;
2335 tmplog += iret;
Christopher Faulet1ccbe122021-11-15 11:31:08 +01002336
2337 /* sess->listener may be undefined if the session's owner is a health-check */
Willy Tarreau807a3a52022-04-12 08:08:33 +02002338 if (sess->listener && sess->listener->bind_conf->xprt->get_ssl_sock_ctx)
Willy Tarreau773d65f2012-10-12 14:56:11 +02002339 LOGCHAR('~');
Willy Tarreau773d65f2012-10-12 14:56:11 +02002340 if (tmp->options & LOG_OPT_QUOTE)
2341 LOGCHAR('"');
2342 last_isspace = 0;
2343 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002344#ifdef USE_OPENSSL
2345 case LOG_FMT_SSL_CIPHER: // %sslc
2346 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002347 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002348 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002349 src = ssl_sock_get_cipher_name(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002350 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002351 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2352 if (ret == NULL)
2353 goto out;
2354 tmplog = ret;
2355 last_isspace = 0;
2356 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02002357
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002358 case LOG_FMT_SSL_VERSION: // %sslv
2359 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002360 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002361 if (conn) {
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02002362 src = ssl_sock_get_proto_version(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002363 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02002364 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2365 if (ret == NULL)
2366 goto out;
2367 tmplog = ret;
2368 last_isspace = 0;
2369 break;
2370#endif
William Lallemand1d705562012-03-12 12:46:41 +01002371 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01002372 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02002373 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002374 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002375 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002376 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002377 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002378 break;
2379
William Lallemand1d705562012-03-12 12:46:41 +01002380 case LOG_FMT_SERVER: // %s
Christopher Fauletfd818482021-04-14 14:01:41 +02002381 switch (obj_type(s ? s->target : sess->origin)) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002382 case OBJ_TYPE_SERVER:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002383 src = __objt_server(s->target)->id;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002384 break;
2385 case OBJ_TYPE_APPLET:
Willy Tarreau1aaf3242018-09-20 11:13:58 +02002386 src = __objt_applet(s->target)->name;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002387 break;
Christopher Fauletfd818482021-04-14 14:01:41 +02002388 case OBJ_TYPE_CHECK:
2389 src = (__objt_check(sess->origin)->server
2390 ? __objt_check(sess->origin)->server->id
2391 : "<NOSRV>");
2392 break;
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002393 default:
2394 src = "<NOSRV>";
2395 break;
2396 }
William Lallemand5f232402012-04-05 18:02:55 +02002397 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002398 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002399 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002400 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002401 last_isspace = 0;
2402 break;
2403
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002404 case LOG_FMT_Th: // %Th = handshake time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002405 ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002406 if (ret == NULL)
2407 goto out;
2408 tmplog = ret;
2409 last_isspace = 0;
2410 break;
2411
2412 case LOG_FMT_Ti: // %Ti = HTTP idle time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002413 ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog);
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002414 if (ret == NULL)
2415 goto out;
2416 tmplog = ret;
2417 last_isspace = 0;
2418 break;
2419
2420 case LOG_FMT_TR: // %TR = HTTP request time
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002421 ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1,
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002422 tmplog, dst + maxsize - tmplog);
2423 if (ret == NULL)
2424 goto out;
2425 tmplog = ret;
2426 last_isspace = 0;
2427 break;
2428
2429 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02002430 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002431 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002432 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002433 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002434 last_isspace = 0;
2435 break;
2436
William Lallemand1d705562012-03-12 12:46:41 +01002437 case LOG_FMT_TW: // %Tw
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002438 ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002439 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002440 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002441 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002442 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002443 last_isspace = 0;
2444 break;
2445
William Lallemand1d705562012-03-12 12:46:41 +01002446 case LOG_FMT_TC: // %Tc
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002447 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002448 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002449 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002450 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002451 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002452 last_isspace = 0;
2453 break;
2454
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002455 case LOG_FMT_Tr: // %Tr
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002456 ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02002457 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002458 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002459 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002460 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002461 last_isspace = 0;
2462 break;
2463
Willy Tarreau27b639d2016-05-17 17:55:27 +02002464 case LOG_FMT_TD: // %Td
Willy Tarreaua21c0e62018-09-05 15:07:15 +02002465 if (be->mode == PR_MODE_HTTP)
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002466 ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002467 tmplog, dst + maxsize - tmplog);
2468 else
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002469 ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1,
Willy Tarreau27b639d2016-05-17 17:55:27 +02002470 tmplog, dst + maxsize - tmplog);
2471 if (ret == NULL)
2472 goto out;
2473 tmplog = ret;
2474 last_isspace = 0;
2475 break;
2476
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002477 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
2478 if (!(fe->to_log & LW_BYTES))
2479 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002480 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 +02002481 tmplog, dst + maxsize - tmplog);
2482 if (ret == NULL)
2483 goto out;
2484 tmplog = ret;
2485 last_isspace = 0;
2486 break;
2487
2488 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002489 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002490 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002491 ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002492 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002493 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002494 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002495 last_isspace = 0;
2496 break;
2497
Damien Claisse57c8eb92020-04-28 12:09:19 +00002498 case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti
2499 if (!(fe->to_log & LW_BYTES))
2500 LOGCHAR('+');
2501 ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0),
2502 tmplog, dst + maxsize - tmplog);
2503 if (ret == NULL)
2504 goto out;
2505 tmplog = ret;
2506 last_isspace = 0;
2507 break;
2508
Willy Tarreau2beef582012-12-20 17:22:52 +01002509 case LOG_FMT_STATUS: // %ST
Christopher Fauletce5e6bc2020-10-06 15:11:43 +02002510 ret = ltoa_o(status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002511 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002512 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002513 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002514 last_isspace = 0;
2515 break;
2516
William Lallemand1d705562012-03-12 12:46:41 +01002517 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002518 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01002519 LOGCHAR('+');
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002520 ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002521 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002522 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002523 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002524 last_isspace = 0;
2525 break;
2526
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002527 case LOG_FMT_BYTES_UP: // %U
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002528 ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog);
Willy Tarreauc5259fd2012-12-20 15:38:04 +01002529 if (ret == NULL)
2530 goto out;
2531 tmplog = ret;
2532 last_isspace = 0;
2533 break;
2534
Willy Tarreau2beef582012-12-20 17:22:52 +01002535 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02002536 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002537 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002538 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002539 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002540 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002541 last_isspace = 0;
2542 break;
2543
Willy Tarreau2beef582012-12-20 17:22:52 +01002544 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02002545 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02002546 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002547 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002548 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002549 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002550 last_isspace = 0;
2551 break;
2552
William Lallemand1d705562012-03-12 12:46:41 +01002553 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002554 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2555 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01002556 *tmplog = '\0';
2557 last_isspace = 0;
2558 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002559
William Lallemand1d705562012-03-12 12:46:41 +01002560 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002561 LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
2562 LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02002563 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
2564 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 +01002565 last_isspace = 0;
2566 break;
2567
William Lallemand1d705562012-03-12 12:46:41 +01002568 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02002569 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002570 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002571 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002572 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002573 last_isspace = 0;
2574 break;
2575
William Lallemand1d705562012-03-12 12:46:41 +01002576 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02002577 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002578 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002579 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002580 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002581 last_isspace = 0;
2582 break;
2583
William Lallemand1d705562012-03-12 12:46:41 +01002584 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02002585 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002586 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002587 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002588 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002589 last_isspace = 0;
2590 break;
2591
William Lallemand1d705562012-03-12 12:46:41 +01002592 case LOG_FMT_SRVCONN: // %sc
Christopher Fauletfd818482021-04-14 14:01:41 +02002593 switch (obj_type(s ? s->target : sess->origin)) {
2594 case OBJ_TYPE_SERVER:
2595 ret = ultoa_o(__objt_server(s->target)->cur_sess,
2596 tmplog, dst + maxsize - tmplog);
2597 break;
2598 case OBJ_TYPE_CHECK:
2599 ret = ultoa_o(__objt_check(sess->origin)->server
2600 ? __objt_check(sess->origin)->server->cur_sess
2601 : 0, tmplog, dst + maxsize - tmplog);
2602 break;
2603 default:
2604 ret = ultoa_o(0, tmplog, dst + maxsize - tmplog);
2605 break;
2606 }
2607
William Lallemand1d705562012-03-12 12:46:41 +01002608 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002609 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002610 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002611 last_isspace = 0;
2612 break;
2613
William Lallemand1d705562012-03-12 12:46:41 +01002614 case LOG_FMT_RETRIES: // %rq
Willy Tarreaub8bc5252018-09-05 15:51:28 +02002615 if (s_flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01002616 LOGCHAR('+');
Christopher Faulet731c8e62022-03-29 16:08:44 +02002617 ret = ltoa_o((s ? s->conn_retries : 0), tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002618 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02002619 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002620 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002621 last_isspace = 0;
2622 break;
2623
William Lallemand1d705562012-03-12 12:46:41 +01002624 case LOG_FMT_SRVQUEUE: // %sq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002625 ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002626 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002627 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002628 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002629 last_isspace = 0;
2630 break;
2631
William Lallemand1d705562012-03-12 12:46:41 +01002632 case LOG_FMT_BCKQUEUE: // %bq
Willy Tarreau372ac5a2018-09-05 15:16:23 +02002633 ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01002634 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01002635 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002636 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002637 last_isspace = 0;
2638 break;
2639
William Lallemand1d705562012-03-12 12:46:41 +01002640 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01002641 /* request header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002642 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002643 if (tmp->options & LOG_OPT_QUOTE)
2644 LOGCHAR('"');
2645 LOGCHAR('{');
2646 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2647 if (hdr)
2648 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002649 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002650 ret = lf_encode_string(tmplog, dst + maxsize,
2651 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002652 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002653 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002654 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002655 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002656 }
2657 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02002658 if (tmp->options & LOG_OPT_QUOTE)
2659 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002660 last_isspace = 0;
2661 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002662 break;
2663
William Lallemand1d705562012-03-12 12:46:41 +01002664 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002665 /* request header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002666 if (fe->nb_req_cap && s && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002667 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
2668 if (hdr > 0)
2669 LOGCHAR(' ');
2670 if (tmp->options & LOG_OPT_QUOTE)
2671 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002672 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002673 ret = lf_encode_string(tmplog, dst + maxsize,
2674 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002675 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002676 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002677 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002678 } else if (!(tmp->options & LOG_OPT_QUOTE))
2679 LOGCHAR('-');
2680 if (tmp->options & LOG_OPT_QUOTE)
2681 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002682 last_isspace = 0;
2683 }
2684 }
2685 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686
William Lallemand1d705562012-03-12 12:46:41 +01002687
2688 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01002689 /* response header */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002690 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002691 if (tmp->options & LOG_OPT_QUOTE)
2692 LOGCHAR('"');
2693 LOGCHAR('{');
2694 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2695 if (hdr)
2696 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002697 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002698 ret = lf_encode_string(tmplog, dst + maxsize,
2699 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002700 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002701 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002702 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02002703 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002704 }
2705 LOGCHAR('}');
2706 last_isspace = 0;
2707 if (tmp->options & LOG_OPT_QUOTE)
2708 LOGCHAR('"');
2709 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01002710 break;
2711
William Lallemand1d705562012-03-12 12:46:41 +01002712 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01002713 /* response header list */
Willy Tarreaud4f91662018-09-05 15:28:07 +02002714 if (fe->nb_rsp_cap && s && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01002715 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
2716 if (hdr > 0)
2717 LOGCHAR(' ');
2718 if (tmp->options & LOG_OPT_QUOTE)
2719 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002720 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01002721 ret = lf_encode_string(tmplog, dst + maxsize,
2722 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002723 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002724 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002725 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002726 } else if (!(tmp->options & LOG_OPT_QUOTE))
2727 LOGCHAR('-');
2728 if (tmp->options & LOG_OPT_QUOTE)
2729 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002730 last_isspace = 0;
2731 }
2732 }
2733 break;
2734
William Lallemand1d705562012-03-12 12:46:41 +01002735 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002736 /* Request */
2737 if (tmp->options & LOG_OPT_QUOTE)
2738 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002739 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002740 ret = lf_encode_string(tmplog, dst + maxsize,
2741 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002742 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002743 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002744 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002745 if (tmp->options & LOG_OPT_QUOTE)
2746 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002747 last_isspace = 0;
2748 break;
William Lallemand5f232402012-04-05 18:02:55 +02002749
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002750 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002751 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002752
Willy Tarreaub7636d12015-06-17 19:58:02 +02002753 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002754 LOGCHAR('"');
2755
2756 end = uri + strlen(uri);
2757 // look for the first whitespace character
2758 while (uri < end && !HTTP_IS_SPHT(*uri))
2759 uri++;
2760
2761 // keep advancing past multiple spaces
2762 while (uri < end && HTTP_IS_SPHT(*uri)) {
2763 uri++; nspaces++;
2764 }
2765
2766 // look for first space or question mark after url
2767 spc = uri;
2768 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2769 spc++;
2770
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002771 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 chunk.area = "<BADREQ>";
2773 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002774 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002775 chunk.area = uri;
2776 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002777 }
2778
Dragan Dosen835b9212016-02-12 13:23:03 +01002779 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002780 if (ret == NULL || *ret != '\0')
2781 goto out;
2782
2783 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002784 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002785 LOGCHAR('"');
2786
2787 last_isspace = 0;
2788 break;
2789
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002790 case LOG_FMT_HTTP_PATH_ONLY: // %HPO
2791 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
2792
2793 if (tmp->options & LOG_OPT_QUOTE)
2794 LOGCHAR('"');
2795
2796 end = uri + strlen(uri);
2797
2798 // look for the first whitespace character
2799 while (uri < end && !HTTP_IS_SPHT(*uri))
2800 uri++;
2801
2802 // keep advancing past multiple spaces
2803 while (uri < end && HTTP_IS_SPHT(*uri)) {
2804 uri++; nspaces++;
2805 }
2806
2807 // look for first space after url
2808 spc = uri;
2809 while (spc < end && !HTTP_IS_SPHT(*spc))
2810 spc++;
2811
Tim Duesterhus92c696e2021-02-28 16:11:36 +01002812 path = ist2(uri, spc - uri);
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002813
2814 // extract relative path without query params from url
Amaury Denoyellec453f952021-07-06 11:40:12 +02002815 parser = http_uri_parser_init(path);
2816 path = iststop(http_parse_path(&parser), '?');
Maciej Zdebfcdfd852020-11-30 18:27:47 +00002817 if (!txn || !txn->uri || nspaces == 0) {
2818 chunk.area = "<BADREQ>";
2819 chunk.data = strlen("<BADREQ>");
2820 } else {
2821 chunk.area = path.ptr;
2822 chunk.data = path.len;
2823 }
2824
2825 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
2826 if (ret == NULL || *ret != '\0')
2827 goto out;
2828
2829 tmplog = ret;
2830 if (tmp->options & LOG_OPT_QUOTE)
2831 LOGCHAR('"');
2832
2833 last_isspace = 0;
2834 break;
2835
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002836 case LOG_FMT_HTTP_QUERY: // %HQ
2837 if (tmp->options & LOG_OPT_QUOTE)
2838 LOGCHAR('"');
2839
Willy Tarreau57bc8912016-04-25 17:09:40 +02002840 if (!txn || !txn->uri) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002841 chunk.area = "<BADREQ>";
2842 chunk.data = strlen("<BADREQ>");
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002843 } else {
2844 uri = txn->uri;
2845 end = uri + strlen(uri);
2846 // look for the first question mark
2847 while (uri < end && *uri != '?')
2848 uri++;
2849
2850 qmark = uri;
2851 // look for first space or question mark after url
2852 while (uri < end && !HTTP_IS_SPHT(*uri))
2853 uri++;
2854
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002855 chunk.area = qmark;
2856 chunk.data = uri - qmark;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002857 }
2858
Dragan Dosen835b9212016-02-12 13:23:03 +01002859 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002860 if (ret == NULL || *ret != '\0')
2861 goto out;
2862
2863 tmplog = ret;
2864 if (tmp->options & LOG_OPT_QUOTE)
2865 LOGCHAR('"');
2866
2867 last_isspace = 0;
2868 break;
2869
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002870 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002871 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002872
Willy Tarreaub7636d12015-06-17 19:58:02 +02002873 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002874 LOGCHAR('"');
2875
2876 end = uri + strlen(uri);
2877 // look for the first whitespace character
2878 while (uri < end && !HTTP_IS_SPHT(*uri))
2879 uri++;
2880
2881 // keep advancing past multiple spaces
2882 while (uri < end && HTTP_IS_SPHT(*uri)) {
2883 uri++; nspaces++;
2884 }
2885
2886 // look for first space after url
2887 spc = uri;
2888 while (spc < end && !HTTP_IS_SPHT(*spc))
2889 spc++;
2890
Willy Tarreau57bc8912016-04-25 17:09:40 +02002891 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002892 chunk.area = "<BADREQ>";
2893 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002894 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002895 chunk.area = uri;
2896 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002897 }
2898
Dragan Dosen835b9212016-02-12 13:23:03 +01002899 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002900 if (ret == NULL || *ret != '\0')
2901 goto out;
2902
2903 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002904 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002905 LOGCHAR('"');
2906
2907 last_isspace = 0;
2908 break;
2909
2910 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002911 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002912 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002913 LOGCHAR('"');
2914
2915 end = uri + strlen(uri);
2916 // look for the first whitespace character
2917 spc = uri;
2918 while (spc < end && !HTTP_IS_SPHT(*spc))
2919 spc++;
2920
2921 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002922 chunk.area = "<BADREQ>";
2923 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002924 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002925 chunk.area = uri;
2926 chunk.data = spc - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002927 }
2928
Dragan Dosen835b9212016-02-12 13:23:03 +01002929 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002930 if (ret == NULL || *ret != '\0')
2931 goto out;
2932
2933 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002934 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002935 LOGCHAR('"');
2936
2937 last_isspace = 0;
2938 break;
2939
2940 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002941 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002942 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002943 LOGCHAR('"');
2944
2945 end = uri + strlen(uri);
2946 // look for the first 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++; nspaces++;
2953 }
2954
2955 // look for the next whitespace character
2956 while (uri < end && !HTTP_IS_SPHT(*uri))
2957 uri++;
2958
2959 // keep advancing past multiple spaces
2960 while (uri < end && HTTP_IS_SPHT(*uri))
2961 uri++;
2962
Willy Tarreau57bc8912016-04-25 17:09:40 +02002963 if (!txn || !txn->uri || nspaces == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 chunk.area = "<BADREQ>";
2965 chunk.data = strlen("<BADREQ>");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002966 } else if (uri == end) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002967 chunk.area = "HTTP/0.9";
2968 chunk.data = strlen("HTTP/0.9");
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002969 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002970 chunk.area = uri;
2971 chunk.data = end - uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002972 }
2973
Dragan Dosen835b9212016-02-12 13:23:03 +01002974 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002975 if (ret == NULL || *ret != '\0')
2976 goto out;
2977
2978 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002979 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002980 LOGCHAR('"');
2981
2982 last_isspace = 0;
2983 break;
2984
William Lallemand5f232402012-04-05 18:02:55 +02002985 case LOG_FMT_COUNTER: // %rt
2986 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002987 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002988 if (iret < 0 || iret > dst + maxsize - tmplog)
2989 goto out;
2990 last_isspace = 0;
2991 tmplog += iret;
2992 } else {
Willy Tarreau5cacab62018-09-05 15:52:59 +02002993 ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002994 if (ret == NULL)
2995 goto out;
2996 tmplog = ret;
2997 last_isspace = 0;
2998 }
2999 break;
3000
Willy Tarreau7346acb2014-08-28 15:03:15 +02003001 case LOG_FMT_LOGCNT: // %lc
3002 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003003 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003004 if (iret < 0 || iret > dst + maxsize - tmplog)
3005 goto out;
3006 last_isspace = 0;
3007 tmplog += iret;
3008 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003009 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02003010 if (ret == NULL)
3011 goto out;
3012 tmplog = ret;
3013 last_isspace = 0;
3014 }
3015 break;
3016
William Lallemand5f232402012-04-05 18:02:55 +02003017 case LOG_FMT_HOSTNAME: // %H
3018 src = hostname;
3019 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
3020 if (ret == NULL)
3021 goto out;
3022 tmplog = ret;
3023 last_isspace = 0;
3024 break;
3025
3026 case LOG_FMT_PID: // %pid
3027 if (tmp->options & LOG_OPT_HEXA) {
3028 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
3029 if (iret < 0 || iret > dst + maxsize - tmplog)
3030 goto out;
3031 last_isspace = 0;
3032 tmplog += iret;
3033 } else {
3034 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
3035 if (ret == NULL)
3036 goto out;
3037 tmplog = ret;
3038 last_isspace = 0;
3039 }
3040 break;
William Lallemanda73203e2012-03-12 12:48:57 +01003041
3042 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003043 ret = NULL;
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003044 if (s)
3045 ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp);
3046 else
3047 ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01003048 if (ret == NULL)
3049 goto out;
3050 tmplog = ret;
3051 last_isspace = 0;
3052 break;
3053
William Lallemandbddd4fd2012-02-27 11:23:10 +01003054 }
3055 }
3056
3057out:
William Lallemand1d705562012-03-12 12:46:41 +01003058 /* *tmplog is a unused character */
3059 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01003060 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003061
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062}
3063
William Lallemand1d705562012-03-12 12:46:41 +01003064/*
Willy Tarreau87b09662015-04-03 00:22:06 +02003065 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01003066 * Will not log if the frontend has no log defined.
3067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003068void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01003069{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003070 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01003071 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003072 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01003073
3074 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003075 err = (s->flags & SF_REDISP) ||
3076 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
Christopher Fauleta892b7f2022-06-17 14:53:20 +02003077 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && s->conn_retries) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02003078 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003080 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01003081 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003082
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003083 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01003084 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01003085
Willy Tarreauabcd5142013-06-11 17:18:02 +02003086 if (s->logs.level) { /* loglevel was overridden */
3087 if (s->logs.level == -1) {
3088 s->logs.logwait = 0; /* logs disabled */
3089 return;
3090 }
3091 level = s->logs.level - 1;
3092 }
3093 else {
3094 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003095 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02003096 level = LOG_ERR;
3097 }
William Lallemand1d705562012-03-12 12:46:41 +01003098
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003099 /* if unique-id was not generated */
Tim Duesterhusa17e6622020-03-05 20:19:02 +01003100 if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhus2825b4b2020-02-28 15:13:34 +01003101 stream_generate_unique_id(s, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003102 }
3103
Dragan Dosen0b85ece2015-09-25 19:17:44 +02003104 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3105 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
3106 &sess->fe->logformat_sd);
3107 }
3108
Dragan Dosen59cee972015-09-19 22:09:02 +02003109 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01003110 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003111 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003112 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3113 logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01003114 s->logs.logwait = 0;
3115 }
3116}
William Lallemandbddd4fd2012-02-27 11:23:10 +01003117
Willy Tarreau53839352018-09-05 19:51:10 +02003118/*
3119 * send a minimalist log for the session. Will not log if the frontend has no
3120 * log defined. It is assumed that this is only used to report anomalies that
3121 * cannot lead to the creation of a regular stream. Because of this the log
3122 * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting
3123 * in the frontend. The caller must simply know that it should not call this
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003124 * function to report unimportant events. It is safe to call this function with
3125 * sess==NULL (will not do anything).
Willy Tarreau53839352018-09-05 19:51:10 +02003126 */
3127void sess_log(struct session *sess)
3128{
3129 int size, level;
3130 int sd_size = 0;
3131
Willy Tarreau9fa267d2018-10-05 10:22:27 +02003132 if (!sess)
3133 return;
3134
Willy Tarreau53839352018-09-05 19:51:10 +02003135 if (LIST_ISEMPTY(&sess->fe->logsrvs))
3136 return;
3137
3138 level = LOG_INFO;
3139 if (sess->fe->options2 & PR_O2_LOGERRORS)
3140 level = LOG_ERR;
3141
3142 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
3143 sd_size = sess_build_logline(sess, NULL,
3144 logline_rfc5424, global.max_syslog_len,
3145 &sess->fe->logformat_sd);
3146 }
3147
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02003148 if (!LIST_ISEMPTY(&sess->fe->logformat_error))
3149 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat_error);
3150 else
3151 size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat);
Willy Tarreau53839352018-09-05 19:51:10 +02003152 if (size > 0) {
Willy Tarreau4781b152021-04-06 13:53:36 +02003153 _HA_ATOMIC_INC(&sess->fe->log_count);
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003154 __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level,
3155 logline, size + 1, logline_rfc5424, sd_size);
Willy Tarreau53839352018-09-05 19:51:10 +02003156 }
3157}
3158
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003159void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
3160{
3161 va_list argp;
3162 int data_len;
3163
3164 if (level < 0 || format == NULL || logline == NULL)
3165 return;
3166
3167 va_start(argp, format);
3168 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
3169 if (data_len < 0 || data_len > global.max_syslog_len)
3170 data_len = global.max_syslog_len;
3171 va_end(argp);
3172
3173 __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2);
3174}
Emeric Brun54932b42020-07-07 09:43:24 +02003175/*
3176 * This function parse a received log message <buf>, of size <buflen>
3177 * it fills <level>, <facility> and <metadata> depending of the detected
3178 * header format and message will point on remaining payload of <size>
3179 *
3180 * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist)
3181 * struct ist len will be set to 0 if field is not found
3182 * <level> and <facility> will be set to -1 if not found.
3183 */
3184void parse_log_message(char *buf, size_t buflen, int *level, int *facility,
3185 struct ist *metadata, char **message, size_t *size)
3186{
3187
3188 char *p;
3189 int fac_level = 0;
3190
3191 *level = *facility = -1;
3192
3193 *message = buf;
3194 *size = buflen;
3195
3196 memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist));
3197
3198 p = buf;
3199 if (*size < 2 || *p != '<')
3200 return;
3201
3202 p++;
3203 while (*p != '>') {
3204 if (*p > '9' || *p < '0')
3205 return;
3206 fac_level = 10*fac_level + (*p - '0');
3207 p++;
3208 if ((p - buf) > buflen)
3209 return;
3210 }
3211
3212 *facility = fac_level >> 3;
3213 *level = fac_level & 0x7;
3214 p++;
3215
3216 metadata[LOG_META_PRIO] = ist2(buf, p - buf);
3217
3218 buflen -= p - buf;
3219 buf = p;
3220
3221 *size = buflen;
3222 *message = buf;
3223
3224 /* for rfc5424, prio is always followed by '1' and ' ' */
3225 if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) {
3226 /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA '
3227 * followed by message.
3228 * Each header field can present NILVALUE: '-'
3229 */
3230
3231 p += 2;
3232 /* timestamp is NILVALUE '-' */
3233 if (*size > 2 && (p[0] == '-') && p[1] == ' ') {
3234 metadata[LOG_META_TIME] = ist2(p, 1);
3235 p++;
3236 }
3237 else if (*size > LOG_ISOTIME_MINLEN) {
3238 metadata[LOG_META_TIME].ptr = p;
3239
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003240 /* check if optional secfrac is present
Emeric Brun54932b42020-07-07 09:43:24 +02003241 * in timestamp.
3242 * possible format are:
3243 * ex: '1970-01-01T00:00:00.000000Z'
3244 * '1970-01-01T00:00:00.000000+00:00'
3245 * '1970-01-01T00:00:00.000000-00:00'
3246 * '1970-01-01T00:00:00Z'
3247 * '1970-01-01T00:00:00+00:00'
3248 * '1970-01-01T00:00:00-00:00'
3249 */
3250 p += 19;
3251 if (*p == '.') {
3252 p++;
3253 if ((p - buf) >= buflen)
3254 goto bad_format;
3255 while (*p != 'Z' && *p != '+' && *p != '-') {
3256 if ((unsigned char)(*p - '0') > 9)
3257 goto bad_format;
3258
3259 p++;
3260 if ((p - buf) >= buflen)
3261 goto bad_format;
3262 }
3263 }
3264
3265 if (*p == 'Z')
3266 p++;
3267 else
3268 p += 6; /* case of '+00:00 or '-00:00' */
3269
3270 if ((p - buf) >= buflen || *p != ' ')
3271 goto bad_format;
3272 metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr;
3273 }
3274 else
3275 goto bad_format;
3276
3277
3278 p++;
3279 if ((p - buf) >= buflen || *p == ' ')
3280 goto bad_format;
3281
3282 metadata[LOG_META_HOST].ptr = p;
3283 while (*p != ' ') {
3284 p++;
3285 if ((p - buf) >= buflen)
3286 goto bad_format;
3287 }
3288 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3289 if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-')
3290 metadata[LOG_META_HOST].len = 0;
3291
3292 p++;
3293 if ((p - buf) >= buflen || *p == ' ')
3294 goto bad_format;
3295
3296 metadata[LOG_META_TAG].ptr = p;
3297 while (*p != ' ') {
3298 p++;
3299 if ((p - buf) >= buflen)
3300 goto bad_format;
3301 }
3302 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3303 if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-')
3304 metadata[LOG_META_TAG].len = 0;
3305
3306 p++;
3307 if ((p - buf) >= buflen || *p == ' ')
3308 goto bad_format;
3309
3310 metadata[LOG_META_PID].ptr = p;
3311 while (*p != ' ') {
3312 p++;
3313 if ((p - buf) >= buflen)
3314 goto bad_format;
3315 }
3316 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3317 if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-')
3318 metadata[LOG_META_PID].len = 0;
3319
3320 p++;
3321 if ((p - buf) >= buflen || *p == ' ')
3322 goto bad_format;
3323
3324 metadata[LOG_META_MSGID].ptr = p;
3325 while (*p != ' ') {
3326 p++;
3327 if ((p - buf) >= buflen)
3328 goto bad_format;
3329 }
3330 metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr;
3331 if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-')
3332 metadata[LOG_META_MSGID].len = 0;
3333
3334 p++;
3335 if ((p - buf) >= buflen || *p == ' ')
3336 goto bad_format;
3337
3338 /* structured data format is:
3339 * ex:
3340 * '[key1=value1 key2=value2][key3=value3]'
3341 *
3342 * space is invalid outside [] because
3343 * considered as the end of structured data field
3344 */
3345 metadata[LOG_META_STDATA].ptr = p;
3346 if (*p == '[') {
3347 int elem = 0;
3348
3349 while (1) {
3350 if (elem) {
3351 /* according to rfc this char is escaped in param values */
3352 if (*p == ']' && *(p-1) != '\\')
3353 elem = 0;
3354 }
3355 else {
3356 if (*p == '[')
3357 elem = 1;
3358 else if (*p == ' ')
3359 break;
3360 else
3361 goto bad_format;
3362 }
3363 p++;
3364 if ((p - buf) >= buflen)
3365 goto bad_format;
3366 }
3367 }
3368 else if (*p == '-') {
3369 /* case of NILVALUE */
3370 p++;
3371 if ((p - buf) >= buflen || *p != ' ')
3372 goto bad_format;
3373 }
3374 else
3375 goto bad_format;
3376
3377 metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr;
3378 if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-')
3379 metadata[LOG_META_STDATA].len = 0;
3380
3381 p++;
3382
3383 buflen -= p - buf;
3384 buf = p;
3385
3386 *size = buflen;
3387 *message = p;
3388 }
3389 else if (*size > LOG_LEGACYTIME_LEN) {
3390 int m;
3391
3392 /* supported header format according to rfc3164.
3393 * ex:
3394 * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: '
3395 * or 'Jan 1 00:00:00 HOSTNAME TAG: '
3396 * or 'Jan 1 00:00:00 HOSTNAME '
3397 * Note: HOSTNAME is mandatory, and day
3398 * of month uses a single space prefix if
3399 * less than 10 to ensure hour offset is
3400 * always the same.
3401 */
3402
3403 /* Check month to see if it correspond to a rfc3164
3404 * header ex 'Jan 1 00:00:00' */
3405 for (m = 0; m < 12; m++)
3406 if (!memcmp(monthname[m], p, 3))
3407 break;
3408 /* Month not found */
3409 if (m == 12)
3410 goto bad_format;
3411
3412 metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN);
3413
3414 p += LOG_LEGACYTIME_LEN;
3415 if ((p - buf) >= buflen || *p != ' ')
3416 goto bad_format;
3417
3418 p++;
3419 if ((p - buf) >= buflen || *p == ' ')
3420 goto bad_format;
3421
3422 metadata[LOG_META_HOST].ptr = p;
3423 while (*p != ' ') {
3424 p++;
3425 if ((p - buf) >= buflen)
3426 goto bad_format;
3427 }
3428 metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr;
3429
3430 /* TAG seems to no be mandatory */
3431 p++;
3432
3433 buflen -= p - buf;
3434 buf = p;
3435
3436 *size = buflen;
3437 *message = buf;
3438
3439 if (!buflen)
3440 return;
3441
3442 while (((p - buf) < buflen) && *p != ' ' && *p != ':')
3443 p++;
3444
3445 /* a tag must present a trailing ':' */
3446 if (((p - buf) >= buflen) || *p != ':')
3447 return;
3448 p++;
3449 /* followed by a space */
3450 if (((p - buf) >= buflen) || *p != ' ')
3451 return;
3452
3453 /* rewind to parse tag and pid */
3454 p = buf;
3455 metadata[LOG_META_TAG].ptr = p;
3456 /* we have the guarantee that ':' will be reach before size limit */
3457 while (*p != ':') {
3458 if (*p == '[') {
3459 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3460 metadata[LOG_META_PID].ptr = p + 1;
3461 }
Tim Duesterhus7b5777d2021-03-02 18:57:28 +01003462 else if (*p == ']' && isttest(metadata[LOG_META_PID])) {
Emeric Brun54932b42020-07-07 09:43:24 +02003463 if (p[1] != ':')
3464 return;
3465 metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr;
3466 }
3467 p++;
3468 }
3469 if (!metadata[LOG_META_TAG].len)
3470 metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr;
3471
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05003472 /* let pass ':' and ' ', we still have warranty size is large enough */
Emeric Brun54932b42020-07-07 09:43:24 +02003473 p += 2;
3474
3475 buflen -= p - buf;
3476 buf = p;
3477
3478 *size = buflen;
3479 *message = buf;
3480 }
3481
3482 return;
3483
3484bad_format:
3485 /* bad syslog format, we reset all parsed syslog fields
3486 * but priority is kept because we are able to re-build
3487 * this message using LOF_FORMAT_PRIO.
3488 */
3489 metadata[LOG_META_TIME].len = 0;
3490 metadata[LOG_META_HOST].len = 0;
3491 metadata[LOG_META_TAG].len = 0;
3492 metadata[LOG_META_PID].len = 0;
3493 metadata[LOG_META_MSGID].len = 0;
3494 metadata[LOG_META_STDATA].len = 0;
3495
3496 return;
3497}
3498
3499/*
3500 * UDP syslog fd handler
3501 */
3502void syslog_fd_handler(int fd)
3503{
3504 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
3505 ssize_t ret = 0;
3506 struct buffer *buf = get_trash_chunk();
3507 size_t size;
3508 char *message;
3509 int level;
3510 int facility;
3511 struct listener *l = objt_listener(fdtab[fd].owner);
3512 int max_accept;
3513
Tim Duesterhus16554242021-09-15 13:58:49 +02003514 BUG_ON(!l);
Emeric Brun54932b42020-07-07 09:43:24 +02003515
Willy Tarreauf5090652021-04-06 17:23:40 +02003516 if (fdtab[fd].state & FD_POLL_IN) {
Emeric Brun54932b42020-07-07 09:43:24 +02003517
3518 if (!fd_recv_ready(fd))
3519 return;
3520
3521 max_accept = l->maxaccept ? l->maxaccept : 1;
3522
3523 do {
3524 /* Source address */
3525 struct sockaddr_storage saddr = {0};
3526 socklen_t saddrlen;
3527
3528 saddrlen = sizeof(saddr);
3529
3530 ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen);
3531 if (ret < 0) {
3532 if (errno == EINTR)
3533 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +02003534 if (errno == EAGAIN || errno == EWOULDBLOCK)
Emeric Brun54932b42020-07-07 09:43:24 +02003535 fd_cant_recv(fd);
3536 goto out;
3537 }
3538 buf->data = ret;
3539
Emeric Brun45c457a2020-07-09 23:23:34 +02003540 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003541 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruna39ecbd2020-10-05 14:33:12 +02003542 proxy_inc_fe_req_ctr(l, l->bind_conf->frontend);
Emeric Brun45c457a2020-07-09 23:23:34 +02003543
Emeric Brun54932b42020-07-07 09:43:24 +02003544 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3545
3546 process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size);
3547
3548 } while (--max_accept);
3549 }
3550
3551out:
3552 return;
3553}
Christopher Faulet5c6fefc2019-08-11 19:40:12 +02003554
Emeric Brun12941c82020-07-07 14:19:42 +02003555/*
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003556 * IO Handler to handle message exchange with a syslog tcp client
3557 */
3558static void syslog_io_handler(struct appctx *appctx)
3559{
3560 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
Willy Tarreauc12b3212022-05-27 11:08:15 +02003561 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau270a4572022-05-27 10:36:36 +02003562 struct stream *s = __sc_strm(sc);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003563 struct proxy *frontend = strm_fe(s);
3564 struct listener *l = strm_li(s);
3565 struct buffer *buf = get_trash_chunk();
3566 int max_accept;
3567 int to_skip;
3568 int facility;
3569 int level;
3570 char *message;
3571 size_t size;
3572
3573 max_accept = l->maxaccept ? l->maxaccept : 1;
Willy Tarreau270a4572022-05-27 10:36:36 +02003574 while (co_data(sc_oc(sc))) {
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003575 char c;
3576
3577 if (max_accept <= 0)
3578 goto missing_budget;
3579 max_accept--;
3580
Willy Tarreau270a4572022-05-27 10:36:36 +02003581 to_skip = co_getchar(sc_oc(sc), &c);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003582 if (!to_skip)
3583 goto missing_data;
3584 else if (to_skip < 0)
3585 goto cli_abort;
3586
3587 if (c == '<') {
3588 /* rfc-6587, Non-Transparent-Framing: messages separated by
3589 * a trailing LF or CR LF
3590 */
Willy Tarreau270a4572022-05-27 10:36:36 +02003591 to_skip = co_getline(sc_oc(sc), buf->area, buf->size);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003592 if (!to_skip)
3593 goto missing_data;
3594 else if (to_skip < 0)
3595 goto cli_abort;
3596
3597 if (buf->area[to_skip - 1] != '\n')
3598 goto parse_error;
3599
3600 buf->data = to_skip - 1;
3601
3602 /* according to rfc-6587, some devices adds CR before LF */
3603 if (buf->data && buf->area[buf->data - 1] == '\r')
3604 buf->data--;
3605
3606 }
3607 else if ((unsigned char)(c - '1') <= 8) {
3608 /* rfc-6587, Octet-Counting: message length in ASCII
3609 * (first digit can not be ZERO), followed by a space
3610 * and message length
3611 */
3612 char *p = NULL;
3613 int msglen;
3614
Willy Tarreau270a4572022-05-27 10:36:36 +02003615 to_skip = co_getword(sc_oc(sc), buf->area, buf->size, ' ');
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003616 if (!to_skip)
3617 goto missing_data;
3618 else if (to_skip < 0)
3619 goto cli_abort;
3620
3621 if (buf->area[to_skip - 1] != ' ')
3622 goto parse_error;
3623
3624 msglen = strtol(trash.area, &p, 10);
3625 if (!msglen || p != &buf->area[to_skip - 1])
3626 goto parse_error;
3627
3628 /* message seems too large */
3629 if (msglen > buf->size)
3630 goto parse_error;
3631
Willy Tarreau270a4572022-05-27 10:36:36 +02003632 msglen = co_getblk(sc_oc(sc), buf->area, msglen, to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003633 if (!msglen)
3634 goto missing_data;
3635 else if (msglen < 0)
3636 goto cli_abort;
3637
3638
3639 buf->data = msglen;
3640 to_skip += msglen;
3641 }
3642 else
3643 goto parse_error;
3644
Willy Tarreau270a4572022-05-27 10:36:36 +02003645 co_skip(sc_oc(sc), to_skip);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003646
3647 /* update counters */
Willy Tarreau4781b152021-04-06 13:53:36 +02003648 _HA_ATOMIC_INC(&cum_log_messages);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003649 proxy_inc_fe_req_ctr(l, frontend);
3650
3651 parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size);
3652
3653 process_send_log(&frontend->logsrvs, level, facility, metadata, message, size);
3654
3655 }
3656
3657missing_data:
3658 /* we need more data to read */
Willy Tarreau270a4572022-05-27 10:36:36 +02003659 sc_oc(sc)->flags |= CF_READ_DONTWAIT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003660
3661 return;
3662
3663missing_budget:
3664 /* it may remain some stuff to do, let's retry later */
3665 appctx_wakeup(appctx);
3666
3667 return;
3668
3669parse_error:
3670 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003671 _HA_ATOMIC_INC(&l->counters->failed_req);
3672 _HA_ATOMIC_INC(&frontend->fe_counters.failed_req);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003673
3674 goto close;
3675
3676cli_abort:
3677 if (l->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02003678 _HA_ATOMIC_INC(&l->counters->cli_aborts);
3679 _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003680
3681close:
Willy Tarreau270a4572022-05-27 10:36:36 +02003682 sc_shutw(sc);
3683 sc_shutr(sc);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003684
Willy Tarreau270a4572022-05-27 10:36:36 +02003685 sc_ic(sc)->flags |= CF_READ_NULL;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003686
3687 return;
3688}
3689
3690static struct applet syslog_applet = {
3691 .obj_type = OBJ_TYPE_APPLET,
3692 .name = "<SYSLOG>", /* used for logging */
3693 .fct = syslog_io_handler,
3694 .release = NULL,
3695};
3696
3697/*
Emeric Brun12941c82020-07-07 14:19:42 +02003698 * Parse "log-forward" section and create corresponding sink buffer.
3699 *
3700 * The function returns 0 in success case, otherwise, it returns error
3701 * flags.
3702 */
3703int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
3704{
3705 int err_code = 0;
3706 struct proxy *px;
3707 char *errmsg = NULL;
3708 const char *err = NULL;
3709
3710 if (strcmp(args[0], "log-forward") == 0) {
3711 if (!*args[1]) {
3712 ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum);
3713 err_code |= ERR_ALERT | ERR_ABORT;
3714 goto out;
3715 }
3716
3717 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3718 goto out;
3719
3720 err = invalid_char(args[1]);
3721 if (err) {
3722 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3723 file, linenum, *err, args[0], args[1]);
3724 err_code |= ERR_ALERT | ERR_ABORT;
3725 goto out;
3726 }
3727
Emeric Brunb0c331f2020-10-07 17:05:59 +02003728 px = log_forward_by_name(args[1]);
3729 if (px) {
3730 ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n",
3731 file, linenum, args[1], px->conf.file, px->conf.line);
3732 err_code |= ERR_ALERT | ERR_FATAL;
3733 }
3734
3735 px = proxy_find_by_name(args[1], 0, 0);
3736 if (px) {
3737 ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n",
3738 file, linenum, args[1], proxy_type_str(px),
3739 px->id, px->conf.file, px->conf.line);
3740 err_code |= ERR_ALERT | ERR_FATAL;
Emeric Brun12941c82020-07-07 14:19:42 +02003741 }
3742
3743 px = calloc(1, sizeof *px);
3744 if (!px) {
3745 err_code |= ERR_ALERT | ERR_FATAL;
3746 goto out;
3747 }
3748
Emeric Brun2ad2b1c2021-12-01 12:08:42 +01003749 init_new_proxy(px);
Emeric Brun12941c82020-07-07 14:19:42 +02003750 px->next = cfg_log_forward;
3751 cfg_log_forward = px;
Emeric Brun12941c82020-07-07 14:19:42 +02003752 px->conf.file = strdup(file);
3753 px->conf.line = linenum;
3754 px->mode = PR_MODE_SYSLOG;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003755 px->last_change = now.tv_sec;
3756 px->cap = PR_CAP_FE;
3757 px->maxconn = 10;
3758 px->timeout.client = TICK_ETERNITY;
3759 px->accept = frontend_accept;
3760 px->default_target = &syslog_applet.obj_type;
Emeric Brun12941c82020-07-07 14:19:42 +02003761 px->id = strdup(args[1]);
3762
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003763 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003764 else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003765 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?"))
3766 err_code |= ERR_WARN;
3767
3768 if (*(args[1]) == 0) {
3769 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3770 err_code |= ERR_ALERT | ERR_FATAL;
3771 goto out;
3772 }
3773 cfg_log_forward->maxconn = atol(args[1]);
3774 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3775 goto out;
3776 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003777 else if (strcmp(args[0], "backlog") == 0) { /* backlog */
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003778 if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL))
3779 err_code |= ERR_WARN;
3780
3781 if (*(args[1]) == 0) {
3782 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
3783 err_code |= ERR_ALERT | ERR_FATAL;
3784 goto out;
3785 }
3786 cfg_log_forward->backlog = atol(args[1]);
3787 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3788 goto out;
3789 }
3790 else if (strcmp(args[0], "bind") == 0) {
3791 int cur_arg;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003792 struct bind_conf *bind_conf;
3793 struct bind_kw *kw;
3794 struct listener *l;
3795
3796 cur_arg = 1;
3797
3798 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3799 NULL, xprt_get(XPRT_RAW));
3800 if (!bind_conf) {
3801 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3802 err_code |= ERR_ALERT | ERR_FATAL;
3803 goto out;
3804 }
3805
3806 if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
3807 if (errmsg && *errmsg) {
3808 indent_msg(&errmsg, 2);
3809 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3810 }
3811 else {
3812 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3813 file, linenum, args[0], args[1], args[2]);
3814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817 }
3818 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau66161322021-02-19 15:50:27 +01003819 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003820 l->accept = session_accept_fd;
3821 l->analysers |= cfg_log_forward->fe_req_ana;
3822 l->default_target = cfg_log_forward->default_target;
3823 global.maxsock++;
3824 }
3825 cur_arg++;
3826
3827 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3828 int ret;
3829
3830 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3831 err_code |= ret;
3832 if (ret) {
3833 if (errmsg && *errmsg) {
3834 indent_msg(&errmsg, 2);
3835 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3836 }
3837 else
3838 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3839 file, linenum, args[cur_arg]);
3840 if (ret & ERR_FATAL)
3841 goto out;
3842 }
3843 cur_arg += 1 + kw->skip;
3844 }
3845 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003846 const char *best = bind_find_best_kw(args[cur_arg]);
3847 if (best)
3848 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3849 file, linenum, args[cur_arg], cursection, best);
3850 else
3851 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3852 file, linenum, args[cur_arg], cursection);
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003853 err_code |= ERR_ALERT | ERR_FATAL;
3854 goto out;
3855 }
Emeric Brun12941c82020-07-07 14:19:42 +02003856 }
Willy Tarreau76aaa7f2020-09-16 15:07:22 +02003857 else if (strcmp(args[0], "dgram-bind") == 0) {
Emeric Brun12941c82020-07-07 14:19:42 +02003858 int cur_arg;
Emeric Brun12941c82020-07-07 14:19:42 +02003859 struct bind_conf *bind_conf;
3860 struct bind_kw *kw;
3861 struct listener *l;
3862
3863 cur_arg = 1;
3864
3865 bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum,
3866 NULL, xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +02003867 if (!bind_conf) {
3868 ha_alert("parsing [%s:%d] : out of memory error.", file, linenum);
3869 err_code |= ERR_ALERT | ERR_FATAL;
3870 goto out;
3871 }
Emeric Brun12941c82020-07-07 14:19:42 +02003872
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003873 if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003874 if (errmsg && *errmsg) {
3875 indent_msg(&errmsg, 2);
3876 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
3877 }
3878 else {
3879 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n",
3880 file, linenum, args[0], args[1], args[2]);
Emeric Brun12941c82020-07-07 14:19:42 +02003881 }
Willy Tarreau3b139e52020-09-16 16:24:14 +02003882 err_code |= ERR_ALERT | ERR_FATAL;
3883 goto out;
Emeric Brun12941c82020-07-07 14:19:42 +02003884 }
3885 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
Willy Tarreau26ff5da2020-09-16 15:22:19 +02003886 /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */
Willy Tarreau66161322021-02-19 15:50:27 +01003887 l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT;
Willy Tarreaue140a692020-10-15 21:25:32 +02003888 l->rx.iocb = syslog_fd_handler;
Emeric Brun12941c82020-07-07 14:19:42 +02003889 global.maxsock++;
3890 }
3891 cur_arg++;
3892
3893 while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) {
3894 int ret;
3895
3896 ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg);
3897 err_code |= ret;
3898 if (ret) {
3899 if (errmsg && *errmsg) {
3900 indent_msg(&errmsg, 2);
3901 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
3902 }
3903 else
3904 ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n",
3905 file, linenum, args[cur_arg]);
3906 if (ret & ERR_FATAL)
3907 goto out;
3908 }
3909 cur_arg += 1 + kw->skip;
3910 }
3911 if (*args[cur_arg] != 0) {
Willy Tarreau433b05f2021-03-12 10:14:07 +01003912 const char *best = bind_find_best_kw(args[cur_arg]);
3913 if (best)
3914 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n",
3915 file, linenum, args[cur_arg], cursection, best);
3916 else
3917 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n",
3918 file, linenum, args[cur_arg], cursection);
Emeric Brun12941c82020-07-07 14:19:42 +02003919 err_code |= ERR_ALERT | ERR_FATAL;
3920 goto out;
3921 }
3922 }
3923 else if (strcmp(args[0], "log") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02003924 if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Emeric Brun12941c82020-07-07 14:19:42 +02003925 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3926 err_code |= ERR_ALERT | ERR_FATAL;
3927 goto out;
3928 }
Emeric Bruncbb7bf72020-10-05 14:39:35 +02003929 }
3930 else if (strcmp(args[0], "timeout") == 0) {
3931 const char *res;
3932 unsigned timeout;
3933
3934 if (strcmp(args[1], "client") != 0) {
3935 ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]);
3936 err_code |= ERR_ALERT | ERR_FATAL;
3937 goto out;
3938 }
3939
3940 if (*args[2] == 0) {
3941 ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum);
3942 err_code |= ERR_ALERT | ERR_FATAL;
3943 goto out;
3944 }
3945 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3946 if (res == PARSE_TIME_OVER) {
3947 memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]);
3948 }
3949 else if (res == PARSE_TIME_UNDER) {
3950 memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]);
3951 }
3952 else if (res) {
3953 memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res);
3954 return -1;
3955 }
3956
3957 if (res) {
3958 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3959 err_code |= ERR_ALERT | ERR_FATAL;
3960 goto out;
3961 }
3962 cfg_log_forward->timeout.client = MS_TO_TICKS(timeout);
Emeric Brun12941c82020-07-07 14:19:42 +02003963 }
Willy Tarreauf9feec22020-09-16 15:04:33 +02003964 else {
3965 ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]);
3966 err_code |= ERR_ALERT | ERR_ABORT;
3967 goto out;
3968 }
Emeric Brun12941c82020-07-07 14:19:42 +02003969out:
3970 return err_code;
3971}
3972
Willy Tarreau0108d902018-11-25 19:14:37 +01003973
Emeric Brun12941c82020-07-07 14:19:42 +02003974/* config parsers for this section */
3975REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL);
3976
Willy Tarreau082b6282019-05-22 14:42:12 +02003977REGISTER_PER_THREAD_ALLOC(init_log_buffers);
3978REGISTER_PER_THREAD_FREE(deinit_log_buffers);
Willy Tarreau172f5ce2018-11-26 11:21:50 +01003979
Willy Tarreaubaaee002006-06-26 02:48:02 +02003980/*
3981 * Local variables:
3982 * c-indent-level: 8
3983 * c-basic-offset: 8
3984 * End:
3985 */