blob: 4d496cdc0dca41959cf0917ab684814d46db4331 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Dragan Dosen43885c72015-10-01 13:18:13 +020044struct log_fmt {
45 char *name;
46 struct {
47 struct chunk sep1; /* first pid separator */
48 struct chunk sep2; /* second pid separator */
49 } pid;
50};
51
52static const struct log_fmt log_formats[LOG_FORMATS] = {
53 [LOG_FORMAT_RFC3164] = {
54 .name = "rfc3164",
55 .pid = {
56 .sep1 = { .str = "[", .len = 1 },
57 .sep2 = { .str = "]: ", .len = 3 }
58 }
59 },
60 [LOG_FORMAT_RFC5424] = {
61 .name = "rfc5424",
62 .pid = {
63 .sep1 = { .str = " ", .len = 1 },
64 .sep2 = { .str = " - ", .len = 3 }
65 }
66 }
Dragan Dosen1322d092015-09-22 16:05:32 +020067};
68
Dragan Dosen835b9212016-02-12 13:23:03 +010069#define FD_SETS_ARE_BITFIELDS
70#ifdef FD_SETS_ARE_BITFIELDS
71/*
72 * This map is used with all the FD_* macros to check whether a particular bit
73 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
74 * which should be escaped. When FD_ISSET() returns non-zero, it means that the
75 * byte should be escaped. Be careful to always pass bytes from 0 to 255
76 * exclusively to the macros.
77 */
78fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
79
80#else
81#error "Check if your OS uses bitfields for fd_sets"
82#endif
83
Willy Tarreaubaaee002006-06-26 02:48:02 +020084const char *log_facilities[NB_LOG_FACILITIES] = {
85 "kern", "user", "mail", "daemon",
86 "auth", "syslog", "lpr", "news",
87 "uucp", "cron", "auth2", "ftp",
88 "ntp", "audit", "alert", "cron2",
89 "local0", "local1", "local2", "local3",
90 "local4", "local5", "local6", "local7"
91};
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093const char *log_levels[NB_LOG_LEVELS] = {
94 "emerg", "alert", "crit", "err",
95 "warning", "notice", "info", "debug"
96};
97
Willy Tarreau570f2212013-06-10 16:42:09 +020098const 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 +020099const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
William Lallemand723b73a2012-02-08 16:37:49 +0100101
102/* log_format */
103struct logformat_type {
104 char *name;
105 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100106 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200107 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100108 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100109 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100110};
111
William Lallemandb7ff6a32012-03-02 14:35:21 +0100112int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
113
William Lallemand723b73a2012-02-08 16:37:49 +0100114/* log_format variable names */
115static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200116 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117
118 /* please keep these lines sorted ! */
119 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
120 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
121 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
122 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
123 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200124 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200125 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +0100126 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +0800127 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +0200128 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +0200129 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +0100130 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200131 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100132 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
133 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200134 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100135 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200136 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100137 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
138 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100140 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
141 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
142 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
143 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
144 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
145 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
146 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200147 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
148 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
149 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
150 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000151 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
152 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000153 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000154 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
155 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200156 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100157 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200158 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100159 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
160 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100161 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100162 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
163 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
164 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
165 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
166 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200167 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
168 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100169 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
170 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
171 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
172
173 /* The following tags are deprecated and will be removed soon */
174 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
175 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
176 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
177 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
178 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
179 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
180 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
181 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
182 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
183 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
184 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200185 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100186};
187
Willy Tarreau2beef582012-12-20 17:22:52 +0100188char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
189char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
190char 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 +0100191char *log_format = NULL;
192
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200193/* Default string used for structured-data part in RFC5424 formatted
194 * syslog messages.
195 */
196char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200197
198/* This is a global syslog header, common to all outgoing messages in
199 * RFC3164 format. It begins with time-based part and is updated by
200 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200201 */
202char *logheader = NULL;
203
Dragan Dosen1322d092015-09-22 16:05:32 +0200204/* This is a global syslog header for messages in RFC5424 format. It is
205 * updated by update_log_hdr_rfc5424().
206 */
207char *logheader_rfc5424 = NULL;
208
Dragan Dosen59cee972015-09-19 22:09:02 +0200209/* This is a global syslog message buffer, common to all outgoing
210 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100211 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200212char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100213
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200214/* A global syslog message buffer, common to all RFC5424 syslog messages.
215 * Currently, it is used for generating the structured-data part.
216 */
217char *logline_rfc5424 = NULL;
218
William Lallemand723b73a2012-02-08 16:37:49 +0100219struct logformat_var_args {
220 char *name;
221 int mask;
222};
223
224struct logformat_var_args var_args_list[] = {
225// global
226 { "M", LOG_OPT_MANDATORY },
227 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200228 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100229 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100230 { 0, 0 }
231};
232
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200233/* return the name of the directive used in the current proxy for which we're
234 * currently parsing a header, when it is known.
235 */
236static inline const char *fmt_directive(const struct proxy *curproxy)
237{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100238 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200239 case ARGC_ACL:
240 return "acl";
241 case ARGC_STK:
242 return "stick";
243 case ARGC_TRK:
244 return "track-sc";
245 case ARGC_LOG:
246 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200247 case ARGC_LOGSD:
248 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100249 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100250 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100251 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100252 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200253 case ARGC_UIF:
254 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100255 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200256 return "redirect";
257 case ARGC_CAP:
258 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200259 case ARGC_SRV:
260 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100261 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200262 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100263 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200264}
265
William Lallemand723b73a2012-02-08 16:37:49 +0100266/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100267 * callback used to configure addr source retrieval
268 */
269int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
270{
271 curproxy->options2 |= PR_O2_SRC_ADDR;
272
273 return 0;
274}
275
276
277/*
William Lallemand723b73a2012-02-08 16:37:49 +0100278 * Parse args in a logformat_var
279 */
280int parse_logformat_var_args(char *args, struct logformat_node *node)
281{
282 int i = 0;
283 int end = 0;
284 int flags = 0; // 1 = + 2 = -
285 char *sp = NULL; // start pointer
286
287 if (args == NULL)
288 return 1;
289
290 while (1) {
291 if (*args == '\0')
292 end = 1;
293
294 if (*args == '+') {
295 // add flag
296 sp = args + 1;
297 flags = 1;
298 }
299 if (*args == '-') {
300 // delete flag
301 sp = args + 1;
302 flags = 2;
303 }
304
305 if (*args == '\0' || *args == ',') {
306 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100307 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100308 if (strcmp(sp, var_args_list[i].name) == 0) {
309 if (flags == 1) {
310 node->options |= var_args_list[i].mask;
311 break;
312 } else if (flags == 2) {
313 node->options &= ~var_args_list[i].mask;
314 break;
315 }
316 }
317 }
318 sp = NULL;
319 if (end)
320 break;
321 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100322 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100323 }
324 return 0;
325}
326
327/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100328 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
329 * must pass the args part in the <arg> pointer with its length in <arg_len>,
330 * and varname with its length in <var> and <var_len> respectively. <arg> is
331 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100332 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100333int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100334{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100335 int j;
336 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100337
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100338 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
339 if (strlen(logformat_keywords[j].name) == var_len &&
340 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
341 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
342 node = calloc(1, sizeof(struct logformat_node));
343 node->type = logformat_keywords[j].type;
344 node->options = *defoptions;
345 if (arg_len) {
346 node->arg = my_strndup(arg, arg_len);
347 parse_logformat_var_args(node->arg, node);
348 }
349 if (node->type == LOG_FMT_GLOBAL) {
350 *defoptions = node->options;
351 free(node->arg);
352 free(node);
353 } else {
354 if (logformat_keywords[j].config_callback &&
355 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100356 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100357 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100358 curproxy->to_log |= logformat_keywords[j].lw;
359 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100360 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100361 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100362 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200363 curproxy->conf.args.file, curproxy->conf.args.line,
364 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100365 return 0;
366 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100367 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200368 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
369 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100371 }
William Lallemand723b73a2012-02-08 16:37:49 +0100372 }
373 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100374
375 j = var[var_len];
376 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100377 Warning("parsing [%s:%d] : no such format variable '%s' in '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%' in log-format expressions.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200378 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100380 return -1;
381}
382
383/*
384 * push to the logformat linked list
385 *
386 * start: start pointer
387 * end: end text pointer
388 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100389 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100390 *
391 * LOG_TEXT: copy chars from start to end excluding end.
392 *
393*/
William Lallemand1d705562012-03-12 12:46:41 +0100394void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100395{
396 char *str;
397
Willy Tarreaua3571662012-12-20 21:59:12 +0100398 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100399 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100400 str = calloc(end - start + 1, 1);
401 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100402 str[end - start] = '\0';
403 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100404 node->type = LOG_FMT_TEXT; // type string
405 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100406 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100407 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100408 node->type = LOG_FMT_SEPARATOR;
409 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100410 }
411}
412
413/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100414 * Parse the sample fetch expression <text> and add a node to <list_format> upon
415 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200416 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100417 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100418void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, const char *file, int line)
Willy Tarreauc8368452012-12-21 00:09:23 +0100419{
420 char *cmd[2];
421 struct sample_expr *expr;
422 struct logformat_node *node;
423 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100424 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100425
426 cmd[0] = text;
427 cmd[1] = "";
428 cmd_arg = 0;
429
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100430 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100431 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200432 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
433 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100434 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100435 return;
436 }
437
438 node = calloc(1, sizeof(struct logformat_node));
439 node->type = LOG_FMT_EXPR;
440 node->expr = expr;
441 node->options = options;
442
443 if (arg_len) {
444 node->arg = my_strndup(arg, arg_len);
445 parse_logformat_var_args(node->arg, node);
446 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100447 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100448 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
449
Willy Tarreau434c57c2013-01-08 01:10:24 +0100450 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100451 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
452
Willy Tarreau434c57c2013-01-08 01:10:24 +0100453 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200454 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
455 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
456 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100457
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
459 /* 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;
William Lallemand65ad6e12014-01-31 15:08:02 +0100467 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100468 LIST_ADDQ(list_format, &node->list);
469}
470
471/*
William Lallemand723b73a2012-02-08 16:37:49 +0100472 * Parse the log_format string and fill a linked list.
473 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200474 * You can set arguments using { } : %{many arguments}varname.
475 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100476 *
477 * str: the string to parse
478 * curproxy: the proxy affected
479 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100480 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100481 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100482 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100483void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line)
William Lallemand723b73a2012-02-08 16:37:49 +0100484{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100485 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100486 char *arg = NULL; /* start pointer for args */
487 char *var = NULL; /* start pointer for vars */
488 int arg_len = 0;
489 int var_len = 0;
490 int cformat; /* current token format */
491 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100492 struct logformat_node *tmplf, *back;
493
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100494 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200495 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200496
William Lallemand723b73a2012-02-08 16:37:49 +0100497 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100498 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100499 LIST_DEL(&tmplf->list);
500 free(tmplf);
501 }
502
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100503 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100504 pformat = cformat;
505
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100506 if (!*str)
507 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100508
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100509 /* The prinicple of the two-step state machine below is to first detect a change, and
510 * second have all common paths processed at one place. The common paths are the ones
511 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
512 * We use the common LF_INIT state to dispatch to the different final states.
513 */
514 switch (pformat) {
515 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100516 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100517 arg_len = var_len = 0;
518 if (*str == '{') { // optional argument
519 cformat = LF_STARG;
520 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100521 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100522 else if (*str == '[') {
523 cformat = LF_STEXPR;
524 var = str + 1; // store expr in variable name
525 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100526 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100527 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100528 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100529 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100530 else if (*str == '%')
531 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100532 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100533 /* single '%' followed by blank or digit, send them both */
534 cformat = LF_TEXT;
535 pformat = LF_TEXT; /* finally we include the previous char as well */
536 sp = str - 1; /* send both the '%' and the current char */
537 Warning("parsing [%s:%d] : Fixed missing '%%' before '%c' at position %d in %s line : '%s'. Please use '%%%%' when you need the '%%' character in a log-format expression.\n",
538 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
539
540 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100541 else
542 cformat = LF_INIT; // handle other cases of litterals
543 break;
544
545 case LF_STARG: // text immediately following '%{'
546 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100547 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100548 arg_len = str - arg;
549 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100550 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100551 break;
552
553 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100554 if (*str == '[') {
555 cformat = LF_STEXPR;
556 var = str + 1; // store expr in variable name
557 break;
558 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100559 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100560 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 var = str;
562 break;
563 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200564 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
565 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100566 cformat = LF_INIT;
567 break;
568
Willy Tarreauc8368452012-12-21 00:09:23 +0100569 case LF_STEXPR: // text immediately following '%['
570 if (*str == ']') { // end of arg
571 cformat = LF_EDEXPR;
572 var_len = str - var;
573 *str = 0; // needed for parsing the expression
574 }
575 break;
576
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100577 case LF_VAR: // text part of a variable name
578 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100579 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 cformat = LF_INIT; // not variable name anymore
581 break;
582
Willy Tarreauc8368452012-12-21 00:09:23 +0100583 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100584 cformat = LF_INIT;
585 }
586
587 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
588 switch (*str) {
589 case '%': cformat = LF_STARTVAR; break;
590 case ' ': cformat = LF_SEPARATOR; break;
591 case 0 : cformat = LF_END; break;
592 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100593 }
594 }
595
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100596 if (cformat != pformat || pformat == LF_SEPARATOR) {
597 switch (pformat) {
598 case LF_VAR:
599 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
600 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100601 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100602 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100603 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100604 case LF_TEXT:
605 case LF_SEPARATOR:
606 add_to_logformat_list(sp, str, pformat, list_format);
607 break;
608 }
609 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100610 }
611 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100612
Willy Tarreauc8368452012-12-21 00:09:23 +0100613 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200614 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
615 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
616 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100617
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100618 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100619}
620
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621/*
622 * Displays the message on stderr with the date and pid. Overrides the quiet
623 * mode during startup.
624 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200625void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626{
627 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200628 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
631 va_start(argp, fmt);
632
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200633 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200635 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 vfprintf(stderr, fmt, argp);
637 fflush(stderr);
638 va_end(argp);
639 }
640}
641
642
643/*
644 * Displays the message on stderr with the date and pid.
645 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200646void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647{
648 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200649 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650
651 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
652 va_start(argp, fmt);
653
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200654 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200656 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 vfprintf(stderr, fmt, argp);
658 fflush(stderr);
659 va_end(argp);
660 }
661}
662
663/*
664 * Displays the message on <out> only if quiet mode is not set.
665 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200666void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667{
668 va_list argp;
669
670 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
671 va_start(argp, fmt);
672 vfprintf(out, fmt, argp);
673 fflush(out);
674 va_end(argp);
675 }
676}
677
678/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200679 * returns log format for <fmt> or -1 if not found.
680 */
681int get_log_format(const char *fmt)
682{
683 int format;
684
685 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200686 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200687 format--;
688
689 return format;
690}
691
692/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693 * returns log level for <lev> or -1 if not found.
694 */
695int get_log_level(const char *lev)
696{
697 int level;
698
699 level = NB_LOG_LEVELS - 1;
700 while (level >= 0 && strcmp(log_levels[level], lev))
701 level--;
702
703 return level;
704}
705
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706/*
707 * returns log facility for <fac> or -1 if not found.
708 */
709int get_log_facility(const char *fac)
710{
711 int facility;
712
713 facility = NB_LOG_FACILITIES - 1;
714 while (facility >= 0 && strcmp(log_facilities[facility], fac))
715 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100716
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 return facility;
718}
719
William Lallemanda1cc3812012-02-08 16:38:44 +0100720/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100721 * Encode the string.
722 *
723 * When using the +E log format option, it will try to escape '"\]'
724 * characters with '\' as prefix. The same prefix should not be used as
725 * <escape>.
726 */
727static char *lf_encode_string(char *start, char *stop,
728 const char escape, const fd_set *map,
729 const char *string,
730 struct logformat_node *node)
731{
732 if (node->options & LOG_OPT_ESC) {
733 if (start < stop) {
734 stop--; /* reserve one byte for the final '\0' */
735 while (start < stop && *string != '\0') {
736 if (!FD_ISSET((unsigned char)(*string), map)) {
737 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
738 *start++ = *string;
739 else {
740 if (start + 2 >= stop)
741 break;
742 *start++ = '\\';
743 *start++ = *string;
744 }
745 }
746 else {
747 if (start + 3 >= stop)
748 break;
749 *start++ = escape;
750 *start++ = hextab[(*string >> 4) & 15];
751 *start++ = hextab[*string & 15];
752 }
753 string++;
754 }
755 *start = '\0';
756 }
757 }
758 else {
759 return encode_string(start, stop, escape, map, string);
760 }
761
762 return start;
763}
764
765/*
766 * Encode the chunk.
767 *
768 * When using the +E log format option, it will try to escape '"\]'
769 * characters with '\' as prefix. The same prefix should not be used as
770 * <escape>.
771 */
772static char *lf_encode_chunk(char *start, char *stop,
773 const char escape, const fd_set *map,
774 const struct chunk *chunk,
775 struct logformat_node *node)
776{
777 char *str, *end;
778
779 if (node->options & LOG_OPT_ESC) {
780 if (start < stop) {
781 str = chunk->str;
782 end = chunk->str + chunk->len;
783
784 stop--; /* reserve one byte for the final '\0' */
785 while (start < stop && str < end) {
786 if (!FD_ISSET((unsigned char)(*str), map)) {
787 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
788 *start++ = *str;
789 else {
790 if (start + 2 >= stop)
791 break;
792 *start++ = '\\';
793 *start++ = *str;
794 }
795 }
796 else {
797 if (start + 3 >= stop)
798 break;
799 *start++ = escape;
800 *start++ = hextab[(*str >> 4) & 15];
801 *start++ = hextab[*str & 15];
802 }
803 str++;
804 }
805 *start = '\0';
806 }
807 }
808 else {
809 return encode_chunk(start, stop, escape, map, chunk);
810 }
811
812 return start;
813}
814
815/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100816 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100817 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100818 *
819 * Return the adress of the \0 character, or NULL on error
820 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100821char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100822{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100823 if (size < 2)
824 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100825
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100826 if (node->options & LOG_OPT_QUOTE) {
827 *(dst++) = '"';
828 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100829 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100830
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100831 if (src && len) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100832 if (node->options & LOG_OPT_ESC) {
833 struct chunk chunk;
834 char *ret;
835
836 chunk_initlen(&chunk, (char *)src, 0, len);
837 ret = escape_chunk(dst, dst + size, '\\', rfc5424_escape_map, &chunk);
838 if (ret == NULL || *ret != '\0')
839 return NULL;
840 len = ret - dst;
841 }
842 else {
843 if (++len > size)
844 len = size;
845 len = strlcpy2(dst, src, len);
846 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100847
848 size -= len;
849 dst += len;
850 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100851 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
852 if (size < 2)
853 return NULL;
854 *(dst++) = '-';
855 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100856
857 if (node->options & LOG_OPT_QUOTE) {
858 if (size < 2)
859 return NULL;
860 *(dst++) = '"';
861 }
862
863 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100864 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100865}
866
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100867static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
868{
869 return lf_text_len(dst, src, size, size, node);
870}
871
William Lallemand5f232402012-04-05 18:02:55 +0200872/*
873 * Write a IP adress to the log string
874 * +X option write in hexadecimal notation, most signifant byte on the left
875 */
876char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
877{
878 char *ret = dst;
879 int iret;
880 char pn[INET6_ADDRSTRLEN];
881
882 if (node->options & LOG_OPT_HEXA) {
883 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
884 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
885 if (iret < 0 || iret > size)
886 return NULL;
887 ret += iret;
888 } else {
889 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
890 ret = lf_text(dst, pn, size, node);
891 if (ret == NULL)
892 return NULL;
893 }
894 return ret;
895}
896
897/*
898 * Write a port to the log
899 * +X option write in hexadecimal notation, most signifant byte on the left
900 */
901char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
902{
903 char *ret = dst;
904 int iret;
905
906 if (node->options & LOG_OPT_HEXA) {
907 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
908 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
909 if (iret < 0 || iret > size)
910 return NULL;
911 ret += iret;
912 } else {
913 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
914 if (ret == NULL)
915 return NULL;
916 }
917 return ret;
918}
919
Dragan Dosen1322d092015-09-22 16:05:32 +0200920/* Re-generate time-based part of the syslog header in RFC3164 format at
921 * the beginning of logheader once a second and return the pointer to the
922 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100923 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200924static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100926 static long tvsec;
927 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200928 static struct chunk host = { NULL, 0, 0 };
929 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200931 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200933 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100934 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200935
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200936 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200937 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938
Dragan Dosen43885c72015-10-01 13:18:13 +0200939 if (unlikely(global.log_send_hostname != host.str)) {
940 host.str = global.log_send_hostname;
941 host.len = host.str ? strlen(host.str) : 0;
942 sep = host.len ? 1 : 0;
943 }
944
Dragan Dosen59cee972015-09-19 22:09:02 +0200945 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200946 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200947 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200948 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
949 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 /* WARNING: depending upon implementations, snprintf may return
951 * either -1 or the number of bytes that would be needed to store
952 * the total message. In both cases, we must adjust it.
953 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200954 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
955 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956
Dragan Dosen59cee972015-09-19 22:09:02 +0200957 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 }
959
Willy Tarreau094af4e2015-01-07 15:03:42 +0100960 dataptr[0] = 0; // ensure we get rid of any previous attempt
961
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200962 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100963}
964
Dragan Dosen1322d092015-09-22 16:05:32 +0200965/* Re-generate time-based part of the syslog header in RFC5424 format at
966 * the beginning of logheader_rfc5424 once a second and return the pointer
967 * to the first character after it.
968 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200969static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200970{
971 static long tvsec;
972 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +0200973 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +0200974
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200975 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200976 /* this string is rebuild only once a second */
977 struct tm tm;
978 int hdr_len;
979
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200980 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200981 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +0200982 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +0200983
984 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +0200985 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +0200986 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +0200987 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +0200988 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +0200989 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +0200990 /* WARNING: depending upon implementations, snprintf may return
991 * either -1 or the number of bytes that would be needed to store
992 * the total message. In both cases, we must adjust it.
993 */
994 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
995 hdr_len = global.max_syslog_len;
996
997 dataptr = logheader_rfc5424 + hdr_len;
998 }
999
1000 dataptr[0] = 0; // ensure we get rid of any previous attempt
1001
1002 return dataptr;
1003}
1004
William Lallemand2a4a44f2012-02-06 16:00:33 +01001005/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001006 * This function sends the syslog message using a printf format string. It
1007 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001008 */
1009void send_log(struct proxy *p, int level, const char *format, ...)
1010{
1011 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001012 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001013
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001014 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001015 return;
1016
William Lallemand2a4a44f2012-02-06 16:00:33 +01001017 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001018 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001019 if (data_len < 0 || data_len > global.max_syslog_len)
1020 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001022
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001023 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001024}
1025
1026/*
1027 * This function sends a syslog message.
1028 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001029 * It overrides the last byte of the message vector with an LF character.
1030 * The arguments <sd> and <sd_size> are used for the structured-data part
1031 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001032 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001033void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001034{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001035 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1036 static struct msghdr msghdr = {
1037 .msg_iov = iovec,
1038 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1039 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001040 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1041 static int logfdinet = -1; /* syslog to AF_INET socket */
1042 static char *dataptr = NULL;
1043 int fac_level;
1044 struct list *logsrvs = NULL;
1045 struct logsrv *tmp = NULL;
1046 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001047 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001048 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001049 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001050 struct chunk *tag = &global.log_tag;
1051 static int curr_pid;
1052 static char pidstr[100];
1053 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001054
1055 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001056
1057 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001058 if (!LIST_ISEMPTY(&global.logsrvs)) {
1059 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 }
1061 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001062 if (!LIST_ISEMPTY(&p->logsrvs)) {
1063 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001065 if (p->log_tag.str) {
1066 tag = &p->log_tag;
1067 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001068 }
1069
William Lallemand0f99e342011-10-12 17:50:54 +02001070 if (!logsrvs)
1071 return;
1072
Dragan Dosen43885c72015-10-01 13:18:13 +02001073 if (unlikely(curr_pid != getpid())) {
1074 curr_pid = getpid();
1075 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1076 chunk_initstr(&pid, pidstr);
1077 }
1078
Robert Tsai81ae1952007-12-05 10:47:29 +01001079 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001080 nblogger = 0;
1081 list_for_each_entry(tmp, logsrvs, list) {
1082 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001083 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001084 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001085 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001086 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001087 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001088 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001089 int tag_max = 0;
1090 int pid_sep1_max = 0;
1091 int pid_max = 0;
1092 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001093 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001094 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001095
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001096 nblogger++;
1097
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001099 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001101
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001102 if (unlikely(*plogfd < 0)) {
1103 /* socket not successfully initialized yet */
1104 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1105
1106 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1107 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1108 nblogger, strerror(errno), errno);
1109 continue;
1110 }
1111 /* we don't want to receive anything on this socket */
1112 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1113 /* does nothing under Linux, maybe needed for others */
1114 shutdown(*plogfd, SHUT_RD);
1115 }
1116
Dragan Dosen1322d092015-09-22 16:05:32 +02001117 switch (logsrv->format) {
1118 case LOG_FORMAT_RFC3164:
1119 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001120 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001121 break;
1122
1123 case LOG_FORMAT_RFC5424:
1124 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001125 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001126 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001127 break;
1128
1129 default:
1130 continue; /* must never happen */
1131 }
1132
1133 hdr_size = hdr_ptr - hdr;
1134
Willy Tarreaubaaee002006-06-26 02:48:02 +02001135 /* For each target, we may have a different facility.
1136 * We can also have a different log level for each message.
1137 * This induces variations in the message header length.
1138 * Since we don't want to recompute it each time, nor copy it every
1139 * time, we only change the facility in the pre-computed header,
1140 * and we change the pointer to the header accordingly.
1141 */
William Lallemand0f99e342011-10-12 17:50:54 +02001142 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001143 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001145 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001147 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001148 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001149 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001150
Dragan Dosen1322d092015-09-22 16:05:32 +02001151 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001152
Dragan Dosen43885c72015-10-01 13:18:13 +02001153 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001154 if (unlikely(hdr_size >= logsrv->maxlen)) {
1155 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001156 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001157 goto send;
1158 }
1159
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001160 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001161
Dragan Dosen43885c72015-10-01 13:18:13 +02001162 /* tag */
1163 tag_max = tag->len;
1164 if (unlikely(tag_max >= maxlen)) {
1165 tag_max = maxlen - 1;
1166 sd_max = 0;
1167 goto send;
1168 }
1169
1170 maxlen -= tag_max;
1171
1172 /* first pid separator */
1173 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1174 if (unlikely(pid_sep1_max >= maxlen)) {
1175 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001176 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001177 goto send;
1178 }
1179
Dragan Dosen43885c72015-10-01 13:18:13 +02001180 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1181 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001182
Dragan Dosen43885c72015-10-01 13:18:13 +02001183 /* pid */
1184 pid_max = pid.len;
1185 if (unlikely(pid_max >= maxlen)) {
1186 pid_max = maxlen - 1;
1187 sd_max = 0;
1188 goto send;
1189 }
1190
1191 maxlen -= pid_max;
1192
1193 /* second pid separator */
1194 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1195 if (unlikely(pid_sep2_max >= maxlen)) {
1196 pid_sep2_max = maxlen - 1;
1197 sd_max = 0;
1198 goto send;
1199 }
1200
1201 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1202 maxlen -= pid_sep2_max;
1203
1204 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001205 if (sd_max >= maxlen) {
1206 sd_max = maxlen - 1;
1207 goto send;
1208 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001209
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001210 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001211send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001212 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001213 iovec[0].iov_len = hdr_max;
1214 iovec[1].iov_base = tag->str;
1215 iovec[1].iov_len = tag_max;
1216 iovec[2].iov_base = pid_sep1;
1217 iovec[2].iov_len = pid_sep1_max;
1218 iovec[3].iov_base = pid.str;
1219 iovec[3].iov_len = pid_max;
1220 iovec[4].iov_base = pid_sep2;
1221 iovec[4].iov_len = pid_sep2_max;
1222 iovec[5].iov_base = sd;
1223 iovec[5].iov_len = sd_max;
1224 iovec[6].iov_base = dataptr;
1225 iovec[6].iov_len = max;
1226 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1227 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001228
1229 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1230 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1231
1232 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001233
Robert Tsai81ae1952007-12-05 10:47:29 +01001234 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001235 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001236 nblogger, strerror(errno), errno);
1237 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238 }
1239}
1240
William Lallemandbddd4fd2012-02-27 11:23:10 +01001241extern fd_set hdr_encode_map[];
1242extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001243extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001244
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001246const 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 +01001247const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1248 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1249 Set-cookie Updated, unknown, unknown */
1250
William Lallemand1d705562012-03-12 12:46:41 +01001251/*
1252 * try to write a character if there is enough space, or goto out
1253 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001254#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001255 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001256 *(tmplog++) = (x); \
1257 } else { \
1258 goto out; \
1259 } \
1260 } while(0)
1261
Dragan Dosen835b9212016-02-12 13:23:03 +01001262
1263/* Initializes some log data.
1264 */
1265void init_log()
1266{
1267 char *tmp;
1268
1269 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1270 * inside PARAM-VALUE should be escaped with '\' as prefix.
1271 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1272 * details.
1273 */
1274 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1275
1276 tmp = "\"\\]";
1277 while (*tmp) {
1278 FD_SET(*tmp, rfc5424_escape_map);
1279 tmp++;
1280 }
1281}
William Lallemand1d705562012-03-12 12:46:41 +01001282
Willy Tarreaudf974472012-12-28 02:44:01 +01001283/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1284 * <maxsize> characters. Returns the size of the output string in characters,
1285 * not counting the trailing zero which is always added if the resulting size
1286 * is not zero.
1287 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001288int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001290 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001291 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001292 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001293 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001294 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001295 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001296 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001297 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001298 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001299 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001300 int t_request;
1301 int hdr;
1302 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001303 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001304 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001305 char *ret;
1306 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001308
William Lallemandbddd4fd2012-02-27 11:23:10 +01001309 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001310
William Lallemandbddd4fd2012-02-27 11:23:10 +01001311 t_request = -1;
1312 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1313 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1314
William Lallemand1d705562012-03-12 12:46:41 +01001315 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001316
William Lallemandbddd4fd2012-02-27 11:23:10 +01001317 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001318 if (LIST_ISEMPTY(list_format))
1319 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001320
William Lallemand1d705562012-03-12 12:46:41 +01001321 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001322 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001323 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001324 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001325 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001326
Willy Tarreauc8368452012-12-21 00:09:23 +01001327 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001328 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001329 if (!last_isspace) {
1330 LOGCHAR(' ');
1331 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332 }
1333 break;
1334
William Lallemand1d705562012-03-12 12:46:41 +01001335 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001336 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001337 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001338 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001340 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001341 last_isspace = 0;
1342 break;
1343
Willy Tarreauc8368452012-12-21 00:09:23 +01001344 case LOG_FMT_EXPR: // sample expression, may be request or response
1345 key = NULL;
1346 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001347 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreauc8368452012-12-21 00:09:23 +01001348 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001349 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001350 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001351 ret = lf_encode_chunk(tmplog, dst + maxsize,
1352 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001353 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001354 ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001355 if (ret == 0)
1356 goto out;
1357 tmplog = ret;
1358 last_isspace = 0;
1359 break;
1360
Willy Tarreau2beef582012-12-20 17:22:52 +01001361 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001362 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001363 if (conn)
1364 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1365 else
1366 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001367 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001368 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001369 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001370 last_isspace = 0;
1371 break;
1372
Willy Tarreau2beef582012-12-20 17:22:52 +01001373 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001374 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001375 if (conn) {
1376 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001377 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001378 } else {
1379 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1380 dst + maxsize - tmplog, tmp);
1381 }
William Lallemand5f232402012-04-05 18:02:55 +02001382 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001383 else
1384 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1385
William Lallemand5f232402012-04-05 18:02:55 +02001386 if (ret == NULL)
1387 goto out;
1388 tmplog = ret;
1389 last_isspace = 0;
1390 break;
1391
Willy Tarreau2beef582012-12-20 17:22:52 +01001392 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001393 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001394 if (conn) {
1395 conn_get_to_addr(conn);
1396 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1397 }
1398 else
1399 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1400
William Lallemand1d705562012-03-12 12:46:41 +01001401 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001403 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001404 last_isspace = 0;
1405 break;
1406
Willy Tarreau2beef582012-12-20 17:22:52 +01001407 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001408 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001409 if (conn) {
1410 conn_get_to_addr(conn);
1411 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001412 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001413 else
1414 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001415 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001416 else
1417 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1418
William Lallemand5f232402012-04-05 18:02:55 +02001419 if (ret == NULL)
1420 goto out;
1421 tmplog = ret;
1422 last_isspace = 0;
1423 break;
1424
Willy Tarreau2beef582012-12-20 17:22:52 +01001425 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001426 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001427 if (conn)
1428 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1429 else
1430 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1431
William Lallemand1d705562012-03-12 12:46:41 +01001432 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001433 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001434 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001435 last_isspace = 0;
1436 break;
1437
Willy Tarreau2beef582012-12-20 17:22:52 +01001438 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001439 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001440 if (conn)
1441 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1442 else
1443 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1444
William Lallemand5f232402012-04-05 18:02:55 +02001445 if (ret == NULL)
1446 goto out;
1447 tmplog = ret;
1448 last_isspace = 0;
1449 break;
1450
Willy Tarreau2beef582012-12-20 17:22:52 +01001451 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001452 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001453 if (conn)
1454 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1455 else
1456 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1457
William Lallemand5f232402012-04-05 18:02:55 +02001458 if (ret == NULL)
1459 goto out;
1460 tmplog = ret;
1461 last_isspace = 0;
1462 break;
1463
Willy Tarreau2beef582012-12-20 17:22:52 +01001464 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001465 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001466 if (conn)
1467 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1468 else
1469 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1470
William Lallemand1d705562012-03-12 12:46:41 +01001471 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001472 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001473 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001474 last_isspace = 0;
1475 break;
1476
William Lallemand1d705562012-03-12 12:46:41 +01001477 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001478 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001479 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1480 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001481 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001483 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001484 last_isspace = 0;
1485 break;
1486
William Lallemand1d705562012-03-12 12:46:41 +01001487 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001488 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001489 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001490 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001491 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001492 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001493 last_isspace = 0;
1494 break;
1495
Yuxans Yao4e25b012012-10-19 10:36:09 +08001496 case LOG_FMT_DATELOCAL: // %Tl
1497 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001498 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001499 if (ret == NULL)
1500 goto out;
1501 tmplog = ret;
1502 last_isspace = 0;
1503 break;
1504
William Lallemand5f232402012-04-05 18:02:55 +02001505 case LOG_FMT_TS: // %Ts
1506 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1507 if (tmp->options & LOG_OPT_HEXA) {
1508 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1509 if (iret < 0 || iret > dst + maxsize - tmplog)
1510 goto out;
1511 last_isspace = 0;
1512 tmplog += iret;
1513 } else {
1514 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1515 if (ret == NULL)
1516 goto out;
1517 tmplog = ret;
1518 last_isspace = 0;
1519 }
1520 break;
1521
William Lallemand1d705562012-03-12 12:46:41 +01001522 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001523 if (tmp->options & LOG_OPT_HEXA) {
1524 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1525 if (iret < 0 || iret > dst + maxsize - tmplog)
1526 goto out;
1527 last_isspace = 0;
1528 tmplog += iret;
1529 } else {
1530 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001531 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001532 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1533 tmplog, 4);
1534 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001535 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001536 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001537 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001538 }
1539 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001540
William Lallemand1d705562012-03-12 12:46:41 +01001541 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001542 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001543 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001544 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001545 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001546 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001547 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001548 break;
1549
Willy Tarreau773d65f2012-10-12 14:56:11 +02001550 case LOG_FMT_FRONTEND_XPRT: // %ft
1551 src = fe->id;
1552 if (tmp->options & LOG_OPT_QUOTE)
1553 LOGCHAR('"');
1554 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1555 if (iret == 0)
1556 goto out;
1557 tmplog += iret;
1558#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001559 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001560 LOGCHAR('~');
1561#endif
1562 if (tmp->options & LOG_OPT_QUOTE)
1563 LOGCHAR('"');
1564 last_isspace = 0;
1565 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001566#ifdef USE_OPENSSL
1567 case LOG_FMT_SSL_CIPHER: // %sslc
1568 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001569 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001570 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001571 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001572 src = ssl_sock_get_cipher_name(conn);
1573 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001574 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1575 if (ret == NULL)
1576 goto out;
1577 tmplog = ret;
1578 last_isspace = 0;
1579 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001580
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001581 case LOG_FMT_SSL_VERSION: // %sslv
1582 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001583 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001584 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001585 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001586 src = ssl_sock_get_proto_version(conn);
1587 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001588 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1589 if (ret == NULL)
1590 goto out;
1591 tmplog = ret;
1592 last_isspace = 0;
1593 break;
1594#endif
William Lallemand1d705562012-03-12 12:46:41 +01001595 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001596 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001597 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001598 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001599 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001600 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001601 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001602 break;
1603
William Lallemand1d705562012-03-12 12:46:41 +01001604 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001605 switch (obj_type(s->target)) {
1606 case OBJ_TYPE_SERVER:
1607 src = objt_server(s->target)->id;
1608 break;
1609 case OBJ_TYPE_APPLET:
1610 src = objt_applet(s->target)->name;
1611 break;
1612 default:
1613 src = "<NOSRV>";
1614 break;
1615 }
William Lallemand5f232402012-04-05 18:02:55 +02001616 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001617 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001618 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001619 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001620 last_isspace = 0;
1621 break;
1622
William Lallemand1d705562012-03-12 12:46:41 +01001623 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001624 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001625 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001626 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001627 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001628 last_isspace = 0;
1629 break;
1630
William Lallemand1d705562012-03-12 12:46:41 +01001631 case LOG_FMT_TW: // %Tw
1632 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001633 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001634 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001635 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001636 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001637 last_isspace = 0;
1638 break;
1639
William Lallemand1d705562012-03-12 12:46:41 +01001640 case LOG_FMT_TC: // %Tc
1641 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001642 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001643 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001644 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001645 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001646 last_isspace = 0;
1647 break;
1648
William Lallemand1d705562012-03-12 12:46:41 +01001649 case LOG_FMT_TR: // %Tr
1650 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001651 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001652 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001653 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001654 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001655 last_isspace = 0;
1656 break;
1657
William Lallemand1d705562012-03-12 12:46:41 +01001658 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001659 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001660 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001661 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001662 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001663 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001664 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001665 last_isspace = 0;
1666 break;
1667
Willy Tarreau2beef582012-12-20 17:22:52 +01001668 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001669 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001670 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001671 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001672 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001673 last_isspace = 0;
1674 break;
1675
William Lallemand1d705562012-03-12 12:46:41 +01001676 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001677 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001678 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001679 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001680 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001681 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001682 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001683 last_isspace = 0;
1684 break;
1685
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001686 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001687 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1688 if (ret == NULL)
1689 goto out;
1690 tmplog = ret;
1691 last_isspace = 0;
1692 break;
1693
Willy Tarreau2beef582012-12-20 17:22:52 +01001694 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001695 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001696 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001697 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001698 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001699 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001700 last_isspace = 0;
1701 break;
1702
Willy Tarreau2beef582012-12-20 17:22:52 +01001703 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001704 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001705 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001706 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001707 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001708 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001709 last_isspace = 0;
1710 break;
1711
William Lallemand1d705562012-03-12 12:46:41 +01001712 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001713 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1714 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001715 *tmplog = '\0';
1716 last_isspace = 0;
1717 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001718
William Lallemand1d705562012-03-12 12:46:41 +01001719 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001720 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1721 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001722 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1723 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001724 last_isspace = 0;
1725 break;
1726
William Lallemand1d705562012-03-12 12:46:41 +01001727 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001728 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001729 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001730 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001731 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001732 last_isspace = 0;
1733 break;
1734
William Lallemand1d705562012-03-12 12:46:41 +01001735 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001736 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001737 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001738 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001739 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001740 last_isspace = 0;
1741 break;
1742
William Lallemand1d705562012-03-12 12:46:41 +01001743 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001744 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001745 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001746 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001747 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001748 last_isspace = 0;
1749 break;
1750
William Lallemand1d705562012-03-12 12:46:41 +01001751 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001752 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001753 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001754 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001755 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001756 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001757 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001758 last_isspace = 0;
1759 break;
1760
William Lallemand1d705562012-03-12 12:46:41 +01001761 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001762 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001763 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001764 ret = ltoa_o((s->si[1].conn_retries>0) ?
1765 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001766 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001767 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001768 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001769 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001770 last_isspace = 0;
1771 break;
1772
William Lallemand1d705562012-03-12 12:46:41 +01001773 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001774 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001775 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001776 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001777 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001778 last_isspace = 0;
1779 break;
1780
William Lallemand1d705562012-03-12 12:46:41 +01001781 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001782 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001783 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001784 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001785 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001786 last_isspace = 0;
1787 break;
1788
William Lallemand1d705562012-03-12 12:46:41 +01001789 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001790 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001791 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001792 if (tmp->options & LOG_OPT_QUOTE)
1793 LOGCHAR('"');
1794 LOGCHAR('{');
1795 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1796 if (hdr)
1797 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001798 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001799 ret = lf_encode_string(tmplog, dst + maxsize,
1800 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001801 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001802 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001803 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001804 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001805 }
1806 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001807 if (tmp->options & LOG_OPT_QUOTE)
1808 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001809 last_isspace = 0;
1810 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001811 break;
1812
William Lallemand1d705562012-03-12 12:46:41 +01001813 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001814 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001815 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001816 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1817 if (hdr > 0)
1818 LOGCHAR(' ');
1819 if (tmp->options & LOG_OPT_QUOTE)
1820 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001821 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001822 ret = lf_encode_string(tmplog, dst + maxsize,
1823 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001824 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001825 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001826 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001827 } else if (!(tmp->options & LOG_OPT_QUOTE))
1828 LOGCHAR('-');
1829 if (tmp->options & LOG_OPT_QUOTE)
1830 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001831 last_isspace = 0;
1832 }
1833 }
1834 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001835
William Lallemand1d705562012-03-12 12:46:41 +01001836
1837 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001838 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001839 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001840 if (tmp->options & LOG_OPT_QUOTE)
1841 LOGCHAR('"');
1842 LOGCHAR('{');
1843 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1844 if (hdr)
1845 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001846 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001847 ret = lf_encode_string(tmplog, dst + maxsize,
1848 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001849 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001850 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001851 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001852 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001853 }
1854 LOGCHAR('}');
1855 last_isspace = 0;
1856 if (tmp->options & LOG_OPT_QUOTE)
1857 LOGCHAR('"');
1858 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001859 break;
1860
William Lallemand1d705562012-03-12 12:46:41 +01001861 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001862 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001863 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001864 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1865 if (hdr > 0)
1866 LOGCHAR(' ');
1867 if (tmp->options & LOG_OPT_QUOTE)
1868 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001869 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001870 ret = lf_encode_string(tmplog, dst + maxsize,
1871 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001872 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001873 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001874 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001875 } else if (!(tmp->options & LOG_OPT_QUOTE))
1876 LOGCHAR('-');
1877 if (tmp->options & LOG_OPT_QUOTE)
1878 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001879 last_isspace = 0;
1880 }
1881 }
1882 break;
1883
William Lallemand1d705562012-03-12 12:46:41 +01001884 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001885 /* Request */
1886 if (tmp->options & LOG_OPT_QUOTE)
1887 LOGCHAR('"');
1888 uri = txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01001889 ret = lf_encode_string(tmplog, dst + maxsize,
1890 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001891 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001892 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001893 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001894 if (tmp->options & LOG_OPT_QUOTE)
1895 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001896 last_isspace = 0;
1897 break;
William Lallemand5f232402012-04-05 18:02:55 +02001898
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001899 case LOG_FMT_HTTP_PATH: // %HP
1900 uri = txn->uri ? txn->uri : "<BADREQ>";
1901
Willy Tarreaub7636d12015-06-17 19:58:02 +02001902 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001903 LOGCHAR('"');
1904
1905 end = uri + strlen(uri);
1906 // look for the first whitespace character
1907 while (uri < end && !HTTP_IS_SPHT(*uri))
1908 uri++;
1909
1910 // keep advancing past multiple spaces
1911 while (uri < end && HTTP_IS_SPHT(*uri)) {
1912 uri++; nspaces++;
1913 }
1914
1915 // look for first space or question mark after url
1916 spc = uri;
1917 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1918 spc++;
1919
1920 if (!txn->uri || nspaces == 0) {
1921 chunk.str = "<BADREQ>";
1922 chunk.len = strlen("<BADREQ>");
1923 } else {
1924 chunk.str = uri;
1925 chunk.len = spc - uri;
1926 }
1927
Dragan Dosen835b9212016-02-12 13:23:03 +01001928 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001929 if (ret == NULL || *ret != '\0')
1930 goto out;
1931
1932 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001933 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001934 LOGCHAR('"');
1935
1936 last_isspace = 0;
1937 break;
1938
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001939 case LOG_FMT_HTTP_QUERY: // %HQ
1940 if (tmp->options & LOG_OPT_QUOTE)
1941 LOGCHAR('"');
1942
1943 if (!txn->uri) {
1944 chunk.str = "<BADREQ>";
1945 chunk.len = strlen("<BADREQ>");
1946 } else {
1947 uri = txn->uri;
1948 end = uri + strlen(uri);
1949 // look for the first question mark
1950 while (uri < end && *uri != '?')
1951 uri++;
1952
1953 qmark = uri;
1954 // look for first space or question mark after url
1955 while (uri < end && !HTTP_IS_SPHT(*uri))
1956 uri++;
1957
1958 chunk.str = qmark;
1959 chunk.len = uri - qmark;
1960 }
1961
Dragan Dosen835b9212016-02-12 13:23:03 +01001962 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001963 if (ret == NULL || *ret != '\0')
1964 goto out;
1965
1966 tmplog = ret;
1967 if (tmp->options & LOG_OPT_QUOTE)
1968 LOGCHAR('"');
1969
1970 last_isspace = 0;
1971 break;
1972
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001973 case LOG_FMT_HTTP_URI: // %HU
1974 uri = txn->uri ? txn->uri : "<BADREQ>";
1975
Willy Tarreaub7636d12015-06-17 19:58:02 +02001976 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001977 LOGCHAR('"');
1978
1979 end = uri + strlen(uri);
1980 // look for the first whitespace character
1981 while (uri < end && !HTTP_IS_SPHT(*uri))
1982 uri++;
1983
1984 // keep advancing past multiple spaces
1985 while (uri < end && HTTP_IS_SPHT(*uri)) {
1986 uri++; nspaces++;
1987 }
1988
1989 // look for first space after url
1990 spc = uri;
1991 while (spc < end && !HTTP_IS_SPHT(*spc))
1992 spc++;
1993
1994 if (!txn->uri || nspaces == 0) {
1995 chunk.str = "<BADREQ>";
1996 chunk.len = strlen("<BADREQ>");
1997 } else {
1998 chunk.str = uri;
1999 chunk.len = spc - uri;
2000 }
2001
Dragan Dosen835b9212016-02-12 13:23:03 +01002002 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002003 if (ret == NULL || *ret != '\0')
2004 goto out;
2005
2006 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002007 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002008 LOGCHAR('"');
2009
2010 last_isspace = 0;
2011 break;
2012
2013 case LOG_FMT_HTTP_METHOD: // %HM
2014 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002015 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002016 LOGCHAR('"');
2017
2018 end = uri + strlen(uri);
2019 // look for the first whitespace character
2020 spc = uri;
2021 while (spc < end && !HTTP_IS_SPHT(*spc))
2022 spc++;
2023
2024 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2025 chunk.str = "<BADREQ>";
2026 chunk.len = strlen("<BADREQ>");
2027 } else {
2028 chunk.str = uri;
2029 chunk.len = spc - uri;
2030 }
2031
Dragan Dosen835b9212016-02-12 13:23:03 +01002032 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002033 if (ret == NULL || *ret != '\0')
2034 goto out;
2035
2036 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002037 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002038 LOGCHAR('"');
2039
2040 last_isspace = 0;
2041 break;
2042
2043 case LOG_FMT_HTTP_VERSION: // %HV
2044 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002045 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002046 LOGCHAR('"');
2047
2048 end = uri + strlen(uri);
2049 // look for the first whitespace character
2050 while (uri < end && !HTTP_IS_SPHT(*uri))
2051 uri++;
2052
2053 // keep advancing past multiple spaces
2054 while (uri < end && HTTP_IS_SPHT(*uri)) {
2055 uri++; nspaces++;
2056 }
2057
2058 // look for the next whitespace character
2059 while (uri < end && !HTTP_IS_SPHT(*uri))
2060 uri++;
2061
2062 // keep advancing past multiple spaces
2063 while (uri < end && HTTP_IS_SPHT(*uri))
2064 uri++;
2065
2066 if (!txn->uri || nspaces == 0) {
2067 chunk.str = "<BADREQ>";
2068 chunk.len = strlen("<BADREQ>");
2069 } else if (uri == end) {
2070 chunk.str = "HTTP/0.9";
2071 chunk.len = strlen("HTTP/0.9");
2072 } else {
2073 chunk.str = uri;
2074 chunk.len = end - uri;
2075 }
2076
Dragan Dosen835b9212016-02-12 13:23:03 +01002077 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002078 if (ret == NULL || *ret != '\0')
2079 goto out;
2080
2081 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002082 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002083 LOGCHAR('"');
2084
2085 last_isspace = 0;
2086 break;
2087
William Lallemand5f232402012-04-05 18:02:55 +02002088 case LOG_FMT_COUNTER: // %rt
2089 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002090 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002091 if (iret < 0 || iret > dst + maxsize - tmplog)
2092 goto out;
2093 last_isspace = 0;
2094 tmplog += iret;
2095 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002096 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002097 if (ret == NULL)
2098 goto out;
2099 tmplog = ret;
2100 last_isspace = 0;
2101 }
2102 break;
2103
Willy Tarreau7346acb2014-08-28 15:03:15 +02002104 case LOG_FMT_LOGCNT: // %lc
2105 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002106 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002107 if (iret < 0 || iret > dst + maxsize - tmplog)
2108 goto out;
2109 last_isspace = 0;
2110 tmplog += iret;
2111 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002112 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002113 if (ret == NULL)
2114 goto out;
2115 tmplog = ret;
2116 last_isspace = 0;
2117 }
2118 break;
2119
William Lallemand5f232402012-04-05 18:02:55 +02002120 case LOG_FMT_HOSTNAME: // %H
2121 src = hostname;
2122 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2123 if (ret == NULL)
2124 goto out;
2125 tmplog = ret;
2126 last_isspace = 0;
2127 break;
2128
2129 case LOG_FMT_PID: // %pid
2130 if (tmp->options & LOG_OPT_HEXA) {
2131 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2132 if (iret < 0 || iret > dst + maxsize - tmplog)
2133 goto out;
2134 last_isspace = 0;
2135 tmplog += iret;
2136 } else {
2137 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2138 if (ret == NULL)
2139 goto out;
2140 tmplog = ret;
2141 last_isspace = 0;
2142 }
2143 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002144
2145 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002146 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002147 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002148 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002149 if (ret == NULL)
2150 goto out;
2151 tmplog = ret;
2152 last_isspace = 0;
2153 break;
2154
William Lallemandbddd4fd2012-02-27 11:23:10 +01002155 }
2156 }
2157
2158out:
William Lallemand1d705562012-03-12 12:46:41 +01002159 /* *tmplog is a unused character */
2160 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002161 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002162
Willy Tarreaubaaee002006-06-26 02:48:02 +02002163}
2164
William Lallemand1d705562012-03-12 12:46:41 +01002165/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002166 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002167 * Will not log if the frontend has no log defined.
2168 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002169void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002170{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002171 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002172 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002173 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002174
2175 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002176 err = (s->flags & SF_REDISP) ||
2177 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2178 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002179 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002180 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002182 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002183 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002184
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002185 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002186 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002187
Willy Tarreauabcd5142013-06-11 17:18:02 +02002188 if (s->logs.level) { /* loglevel was overridden */
2189 if (s->logs.level == -1) {
2190 s->logs.logwait = 0; /* logs disabled */
2191 return;
2192 }
2193 level = s->logs.level - 1;
2194 }
2195 else {
2196 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002197 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002198 level = LOG_ERR;
2199 }
William Lallemand1d705562012-03-12 12:46:41 +01002200
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002201 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002202 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002203 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002204 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002205 }
2206
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002207 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2208 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2209 &sess->fe->logformat_sd);
2210 }
2211
Dragan Dosen59cee972015-09-19 22:09:02 +02002212 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002213 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002214 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002215 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002216 s->logs.logwait = 0;
2217 }
2218}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002219
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220/*
2221 * Local variables:
2222 * c-indent-level: 8
2223 * c-basic-offset: 8
2224 * End:
2225 */