blob: 888deb7626e046a7d3b048b3ffa242ec6750c87c [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>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010036#include <proto/sample.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010037#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020038#ifdef USE_OPENSSL
39#include <proto/ssl_sock.h>
40#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreaubaaee002006-06-26 02:48:02 +020042const char *log_facilities[NB_LOG_FACILITIES] = {
43 "kern", "user", "mail", "daemon",
44 "auth", "syslog", "lpr", "news",
45 "uucp", "cron", "auth2", "ftp",
46 "ntp", "audit", "alert", "cron2",
47 "local0", "local1", "local2", "local3",
48 "local4", "local5", "local6", "local7"
49};
50
51
52const char *log_levels[NB_LOG_LEVELS] = {
53 "emerg", "alert", "crit", "err",
54 "warning", "notice", "info", "debug"
55};
56
Willy Tarreau570f2212013-06-10 16:42:09 +020057const 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 +020058const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
William Lallemand723b73a2012-02-08 16:37:49 +010060
61/* log_format */
62struct logformat_type {
63 char *name;
64 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010065 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020066 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010067 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010068 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010069};
70
William Lallemandb7ff6a32012-03-02 14:35:21 +010071int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
72
William Lallemand723b73a2012-02-08 16:37:49 +010073/* log_format variable names */
74static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020075 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010076
77 /* please keep these lines sorted ! */
78 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
79 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
80 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
81 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
82 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020083 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020084 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010085 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080086 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020087 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020088 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010089 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
92 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020093 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010094 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
97 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +020098 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +010099 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
100 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
101 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
102 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
103 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
104 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
105 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200106 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
107 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
108 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
109 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Willy Tarreau2beef582012-12-20 17:22:52 +0100110 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200111 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100112 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
113 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100114 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100115 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
116 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
117 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
118 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
119 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200120 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
121 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
123 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
124 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
125
126 /* The following tags are deprecated and will be removed soon */
127 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
128 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
129 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
130 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
131 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
132 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
133 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
134 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
135 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
136 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
137 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200138 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100139};
140
Willy Tarreau2beef582012-12-20 17:22:52 +0100141char 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
142char 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";
143char 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 +0100144char *log_format = NULL;
145
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100146/* This is a global syslog line, common to all outgoing messages. It begins
147 * with the syslog tag and the date that are updated by update_log_hdr().
148 */
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200149char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100150
William Lallemand723b73a2012-02-08 16:37:49 +0100151struct logformat_var_args {
152 char *name;
153 int mask;
154};
155
156struct logformat_var_args var_args_list[] = {
157// global
158 { "M", LOG_OPT_MANDATORY },
159 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200160 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100161 { 0, 0 }
162};
163
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200164/* return the name of the directive used in the current proxy for which we're
165 * currently parsing a header, when it is known.
166 */
167static inline const char *fmt_directive(const struct proxy *curproxy)
168{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100169 switch (curproxy->conf.args.ctx) {
Willy Tarreaudf0a5962015-07-09 11:20:00 +0200170 case ARGC_ACL:
171 return "acl";
172 case ARGC_STK:
173 return "stick";
174 case ARGC_TRK:
175 return "track-sc";
176 case ARGC_LOG:
177 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100178 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100179 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100180 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100181 return "http-response";
Willy Tarreaudf0a5962015-07-09 11:20:00 +0200182 case ARGC_UIF:
183 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100184 case ARGC_RDR:
Willy Tarreaudf0a5962015-07-09 11:20:00 +0200185 return "redirect";
186 case ARGC_CAP:
187 return "capture";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100188 default:
Willy Tarreaudf0a5962015-07-09 11:20:00 +0200189 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100190 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200191}
192
William Lallemand723b73a2012-02-08 16:37:49 +0100193/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100194 * callback used to configure addr source retrieval
195 */
196int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
197{
198 curproxy->options2 |= PR_O2_SRC_ADDR;
199
200 return 0;
201}
202
203
204/*
William Lallemand723b73a2012-02-08 16:37:49 +0100205 * Parse args in a logformat_var
206 */
207int parse_logformat_var_args(char *args, struct logformat_node *node)
208{
209 int i = 0;
210 int end = 0;
211 int flags = 0; // 1 = + 2 = -
212 char *sp = NULL; // start pointer
213
214 if (args == NULL)
215 return 1;
216
217 while (1) {
218 if (*args == '\0')
219 end = 1;
220
221 if (*args == '+') {
222 // add flag
223 sp = args + 1;
224 flags = 1;
225 }
226 if (*args == '-') {
227 // delete flag
228 sp = args + 1;
229 flags = 2;
230 }
231
232 if (*args == '\0' || *args == ',') {
233 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100234 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100235 if (strcmp(sp, var_args_list[i].name) == 0) {
236 if (flags == 1) {
237 node->options |= var_args_list[i].mask;
238 break;
239 } else if (flags == 2) {
240 node->options &= ~var_args_list[i].mask;
241 break;
242 }
243 }
244 }
245 sp = NULL;
246 if (end)
247 break;
248 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100249 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100250 }
251 return 0;
252}
253
254/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100255 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
256 * must pass the args part in the <arg> pointer with its length in <arg_len>,
257 * and varname with its length in <var> and <var_len> respectively. <arg> is
258 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100259 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100260int 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 +0100261{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100262 int j;
263 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100264
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100265 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
266 if (strlen(logformat_keywords[j].name) == var_len &&
267 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
268 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
269 node = calloc(1, sizeof(struct logformat_node));
270 node->type = logformat_keywords[j].type;
271 node->options = *defoptions;
272 if (arg_len) {
273 node->arg = my_strndup(arg, arg_len);
274 parse_logformat_var_args(node->arg, node);
275 }
276 if (node->type == LOG_FMT_GLOBAL) {
277 *defoptions = node->options;
278 free(node->arg);
279 free(node);
280 } else {
281 if (logformat_keywords[j].config_callback &&
282 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100283 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100284 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100285 curproxy->to_log |= logformat_keywords[j].lw;
286 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100287 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100288 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100289 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200290 curproxy->conf.args.file, curproxy->conf.args.line,
291 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100292 return 0;
293 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100294 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200295 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
296 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100297 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100298 }
William Lallemand723b73a2012-02-08 16:37:49 +0100299 }
300 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100301
302 j = var[var_len];
303 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100304 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 +0200305 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100306 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100307 return -1;
308}
309
310/*
311 * push to the logformat linked list
312 *
313 * start: start pointer
314 * end: end text pointer
315 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100316 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100317 *
318 * LOG_TEXT: copy chars from start to end excluding end.
319 *
320*/
William Lallemand1d705562012-03-12 12:46:41 +0100321void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100322{
323 char *str;
324
Willy Tarreaua3571662012-12-20 21:59:12 +0100325 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100326 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100327 str = calloc(end - start + 1, 1);
328 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100329 str[end - start] = '\0';
330 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100331 node->type = LOG_FMT_TEXT; // type string
332 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100333 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100334 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100335 node->type = LOG_FMT_SEPARATOR;
336 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100337 }
338}
339
340/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100341 * Parse the sample fetch expression <text> and add a node to <list_format> upon
342 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200343 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100344 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100345void 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 +0100346{
347 char *cmd[2];
348 struct sample_expr *expr;
349 struct logformat_node *node;
350 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100351 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100352
353 cmd[0] = text;
354 cmd[1] = "";
355 cmd_arg = 0;
356
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100357 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100358 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200359 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
360 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100361 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100362 return;
363 }
364
365 node = calloc(1, sizeof(struct logformat_node));
366 node->type = LOG_FMT_EXPR;
367 node->expr = expr;
368 node->options = options;
369
370 if (arg_len) {
371 node->arg = my_strndup(arg, arg_len);
372 parse_logformat_var_args(node->arg, node);
373 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100374 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100375 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
376
Willy Tarreau434c57c2013-01-08 01:10:24 +0100377 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100378 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
379
Willy Tarreau434c57c2013-01-08 01:10:24 +0100380 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200381 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
382 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
383 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100384
Willy Tarreauc8368452012-12-21 00:09:23 +0100385 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
386 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100387 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100388
William Lallemand65ad6e12014-01-31 15:08:02 +0100389 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
390 * needed with some sample fetches (eg: ssl*). We always set it for
391 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100392 */
393 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100394 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100395 LIST_ADDQ(list_format, &node->list);
396}
397
398/*
William Lallemand723b73a2012-02-08 16:37:49 +0100399 * Parse the log_format string and fill a linked list.
400 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200401 * You can set arguments using { } : %{many arguments}varname.
402 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100403 *
404 * str: the string to parse
405 * curproxy: the proxy affected
406 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100407 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100408 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100409 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100410void 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 +0100411{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100412 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100413 char *arg = NULL; /* start pointer for args */
414 char *var = NULL; /* start pointer for vars */
415 int arg_len = 0;
416 int var_len = 0;
417 int cformat; /* current token format */
418 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100419 struct logformat_node *tmplf, *back;
420
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100421 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200422 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200423
William Lallemand723b73a2012-02-08 16:37:49 +0100424 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100425 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100426 LIST_DEL(&tmplf->list);
427 free(tmplf);
428 }
429
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100430 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100431 pformat = cformat;
432
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100433 if (!*str)
434 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100435
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100436 /* The prinicple of the two-step state machine below is to first detect a change, and
437 * second have all common paths processed at one place. The common paths are the ones
438 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
439 * We use the common LF_INIT state to dispatch to the different final states.
440 */
441 switch (pformat) {
442 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100443 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100444 arg_len = var_len = 0;
445 if (*str == '{') { // optional argument
446 cformat = LF_STARG;
447 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100448 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100449 else if (*str == '[') {
450 cformat = LF_STEXPR;
451 var = str + 1; // store expr in variable name
452 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100453 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100454 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100455 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100456 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100457 else if (*str == '%')
458 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100459 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100460 /* single '%' followed by blank or digit, send them both */
461 cformat = LF_TEXT;
462 pformat = LF_TEXT; /* finally we include the previous char as well */
463 sp = str - 1; /* send both the '%' and the current char */
464 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",
465 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
466
467 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100468 else
469 cformat = LF_INIT; // handle other cases of litterals
470 break;
471
472 case LF_STARG: // text immediately following '%{'
473 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100474 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100475 arg_len = str - arg;
476 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100477 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100478 break;
479
480 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100481 if (*str == '[') {
482 cformat = LF_STEXPR;
483 var = str + 1; // store expr in variable name
484 break;
485 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100486 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100487 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100488 var = str;
489 break;
490 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200491 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
492 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100493 cformat = LF_INIT;
494 break;
495
Willy Tarreauc8368452012-12-21 00:09:23 +0100496 case LF_STEXPR: // text immediately following '%['
497 if (*str == ']') { // end of arg
498 cformat = LF_EDEXPR;
499 var_len = str - var;
500 *str = 0; // needed for parsing the expression
501 }
502 break;
503
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100504 case LF_VAR: // text part of a variable name
505 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100506 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100507 cformat = LF_INIT; // not variable name anymore
508 break;
509
Willy Tarreauc8368452012-12-21 00:09:23 +0100510 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100511 cformat = LF_INIT;
512 }
513
514 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
515 switch (*str) {
516 case '%': cformat = LF_STARTVAR; break;
517 case ' ': cformat = LF_SEPARATOR; break;
518 case 0 : cformat = LF_END; break;
519 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100520 }
521 }
522
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100523 if (cformat != pformat || pformat == LF_SEPARATOR) {
524 switch (pformat) {
525 case LF_VAR:
526 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
527 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100528 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100529 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100530 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100531 case LF_TEXT:
532 case LF_SEPARATOR:
533 add_to_logformat_list(sp, str, pformat, list_format);
534 break;
535 }
536 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100537 }
538 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100539
Willy Tarreauc8368452012-12-21 00:09:23 +0100540 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200541 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
542 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
543 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100544
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100545 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100546}
547
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548/*
549 * Displays the message on stderr with the date and pid. Overrides the quiet
550 * mode during startup.
551 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200552void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553{
554 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200555 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556
557 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
558 va_start(argp, fmt);
559
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200560 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200561 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200562 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563 vfprintf(stderr, fmt, argp);
564 fflush(stderr);
565 va_end(argp);
566 }
567}
568
569
570/*
571 * Displays the message on stderr with the date and pid.
572 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200573void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574{
575 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200576 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577
578 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
579 va_start(argp, fmt);
580
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200581 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200583 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584 vfprintf(stderr, fmt, argp);
585 fflush(stderr);
586 va_end(argp);
587 }
588}
589
natalie.chen7ac0ca72015-04-07 15:06:01 +0800590void make_literal(char const *input, char *output) {
591 // the following two arrays must be maintained in matching order:
592 static char inputs[] = "\a\b\f\n\r\t\v\\\"\'";
593 static char outputs[] = "abfnrtv\\\"\'";
594
595 char *p, *pos;
596
597 for (;*input;input++) {
598 if (NULL!= (pos=strchr(inputs, *input))) {
599 *output++ = '\\';
600 *output++ = outputs[pos-inputs];
601 }
602 else
603 *output++ = *input;
604 }
605 *output = '\0';
606}
natalie.chen7c92a9b2015-04-07 11:27:02 +0800607
608void Emaila(const char *fmt, ...)
609{
610 va_list argp;
natalie.chen2d0e45b2015-04-22 14:10:12 +0800611 char tmp[256];
612 char buf[500];
613 char alertcmd[1024];
614 int i;
natalie.chen7c92a9b2015-04-07 11:27:02 +0800615 int sysreturn;
natalie.chen2d0e45b2015-04-22 14:10:12 +0800616 size_t len = 0, len1 = 0, len2 = 0, len3 = 0;
natalie.chen7c92a9b2015-04-07 11:27:02 +0800617
618 if (global.email_alert) {
619 va_start(argp, fmt);
natalie.chen7ac0ca72015-04-07 15:06:01 +0800620 vsnprintf(tmp,128,fmt, argp);
621 make_literal(tmp, buf);
natalie.chen2d0e45b2015-04-22 14:10:12 +0800622
623 len1 = strlen(global.email_to[0]);
624 len2 = strlen(global.email_to[1]);
625 len3 = strlen(global.email_to[2]);
626 memcpy(tmp, global.email_to[0], len1);
627 tmp[len1] = ' ';
628 len = len1 + 1;
629 if (len2 > 0) {
630 memcpy(tmp + len, global.email_to[1], len2); // includes terminating null
631 len += len2;
632 tmp[len] = ' ';
633 len += 1;
634 if (len3 > 0) {
635 memcpy(tmp + len, global.email_to[2], len3); // includes terminating null
636 len += len3;
637 tmp[len] = ' ';
638 len += 1;
639 }
natalie.chen7ac0ca72015-04-07 15:06:01 +0800640 }
natalie.chen2d0e45b2015-04-22 14:10:12 +0800641 tmp[len] = '\0';
CP Cheng10f5f102019-09-20 09:54:24 +0800642 snprintf(alertcmd, 1024, "echo \"%s\" | mail -s \"$(echo -e \"[HAProxy alert %s] %.60s...\nFrom:HAProxy <%s>\n\")\" %s&", buf, global.email_from, buf, global.email_from, tmp);
natalie.chen2d0e45b2015-04-22 14:10:12 +0800643
natalie.chen7ac0ca72015-04-07 15:06:01 +0800644 sysreturn = system(alertcmd);
645 if (sysreturn == -1) {
646 Warning("There was an error sending the email alert");
natalie.chen7c92a9b2015-04-07 11:27:02 +0800647 }
648 vfprintf(stderr, fmt, argp);
649 fflush(stderr);
650 va_end(argp);
651 }
652}
653
654
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655/*
656 * Displays the message on <out> only if quiet mode is not set.
657 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200658void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659{
660 va_list argp;
661
662 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
663 va_start(argp, fmt);
664 vfprintf(out, fmt, argp);
665 fflush(out);
666 va_end(argp);
667 }
668}
669
670/*
671 * returns log level for <lev> or -1 if not found.
672 */
673int get_log_level(const char *lev)
674{
675 int level;
676
677 level = NB_LOG_LEVELS - 1;
678 while (level >= 0 && strcmp(log_levels[level], lev))
679 level--;
680
681 return level;
682}
683
684
685/*
686 * returns log facility for <fac> or -1 if not found.
687 */
688int get_log_facility(const char *fac)
689{
690 int facility;
691
692 facility = NB_LOG_FACILITIES - 1;
693 while (facility >= 0 && strcmp(log_facilities[facility], fac))
694 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100695
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 return facility;
697}
698
William Lallemanda1cc3812012-02-08 16:38:44 +0100699/*
700 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200701 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100702 *
703 * Return the adress of the \0 character, or NULL on error
704 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100705char *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 +0100706{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100707 if (size < 2)
708 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100709
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100710 if (node->options & LOG_OPT_QUOTE) {
711 *(dst++) = '"';
712 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100713 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100714
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100715 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100716 if (++len > size)
717 len = size;
718 len = strlcpy2(dst, src, len);
719
720 size -= len;
721 dst += len;
722 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100723 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
724 if (size < 2)
725 return NULL;
726 *(dst++) = '-';
727 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100728
729 if (node->options & LOG_OPT_QUOTE) {
730 if (size < 2)
731 return NULL;
732 *(dst++) = '"';
733 }
734
735 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100736 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100737}
738
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100739static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
740{
741 return lf_text_len(dst, src, size, size, node);
742}
743
William Lallemand5f232402012-04-05 18:02:55 +0200744/*
745 * Write a IP adress to the log string
746 * +X option write in hexadecimal notation, most signifant byte on the left
747 */
748char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
749{
750 char *ret = dst;
751 int iret;
752 char pn[INET6_ADDRSTRLEN];
753
754 if (node->options & LOG_OPT_HEXA) {
755 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
756 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
757 if (iret < 0 || iret > size)
758 return NULL;
759 ret += iret;
760 } else {
761 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
762 ret = lf_text(dst, pn, size, node);
763 if (ret == NULL)
764 return NULL;
765 }
766 return ret;
767}
768
769/*
770 * Write a port to the log
771 * +X option write in hexadecimal notation, most signifant byte on the left
772 */
773char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
774{
775 char *ret = dst;
776 int iret;
777
778 if (node->options & LOG_OPT_HEXA) {
779 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
780 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
781 if (iret < 0 || iret > size)
782 return NULL;
783 ret += iret;
784 } else {
785 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
786 if (ret == NULL)
787 return NULL;
788 }
789 return ret;
790}
791
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100792/* Re-generate the syslog header at the beginning of logline once a second and
793 * return the pointer to the first character after the header.
794 */
795static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100797 static long tvsec;
798 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200800 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200802 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100803 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200804
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200805 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200806 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200808 hdr_len = snprintf(logline, global.max_syslog_len,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100809 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200810 monthname[tm.tm_mon],
811 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100812 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000813 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 /* WARNING: depending upon implementations, snprintf may return
815 * either -1 or the number of bytes that would be needed to store
816 * the total message. In both cases, we must adjust it.
817 */
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200818 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
819 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100821 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 }
823
William Lallemand2a4a44f2012-02-06 16:00:33 +0100824 return dataptr;
825}
826
827/*
828 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100829 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100830 */
831void send_log(struct proxy *p, int level, const char *format, ...)
832{
833 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100834 char *dataptr;
835 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100836
Willy Tarreauab34c8f2015-01-15 16:29:53 +0100837 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100838 return;
839
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100840 dataptr = update_log_hdr(); /* update log header and skip it */
841 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100842
843 va_start(argp, format);
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200844 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
845 if (data_len < 0 || data_len > global.max_syslog_len)
846 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100848
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100849 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100850}
851
852/*
853 * This function sends a syslog message.
854 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100855 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100856 */
857void __send_log(struct proxy *p, int level, char *message, size_t size)
858{
859 static int logfdunix = -1; /* syslog to AF_UNIX socket */
860 static int logfdinet = -1; /* syslog to AF_INET socket */
861 static char *dataptr = NULL;
862 int fac_level;
863 struct list *logsrvs = NULL;
864 struct logsrv *tmp = NULL;
865 int nblogger;
866 char *log_ptr;
867
868 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869
870 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200871 if (!LIST_ISEMPTY(&global.logsrvs)) {
872 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 }
874 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200875 if (!LIST_ISEMPTY(&p->logsrvs)) {
876 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100878 }
879
William Lallemand0f99e342011-10-12 17:50:54 +0200880 if (!logsrvs)
881 return;
882
Robert Tsai81ae1952007-12-05 10:47:29 +0100883 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200884 nblogger = 0;
885 list_for_each_entry(tmp, logsrvs, list) {
886 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100887 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100888 &logfdunix : &logfdinet;
889 int sent;
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200890 int max;
891 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100892
Willy Tarreau53045692014-06-23 18:07:15 +0200893 nblogger++;
894
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200896 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100898
Willy Tarreau53045692014-06-23 18:07:15 +0200899 if (unlikely(*plogfd < 0)) {
900 /* socket not successfully initialized yet */
901 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
902
903 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
904 Alert("socket for logger #%d failed: %s (errno=%d)\n",
905 nblogger, strerror(errno), errno);
906 continue;
907 }
908 /* we don't want to receive anything on this socket */
909 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
910 /* does nothing under Linux, maybe needed for others */
911 shutdown(*plogfd, SHUT_RD);
912 }
913
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 /* For each target, we may have a different facility.
915 * We can also have a different log level for each message.
916 * This induces variations in the message header length.
917 * Since we don't want to recompute it each time, nor copy it every
918 * time, we only change the facility in the pre-computed header,
919 * and we change the pointer to the header accordingly.
920 */
William Lallemand0f99e342011-10-12 17:50:54 +0200921 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100922 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923 do {
924 *log_ptr = '0' + fac_level % 10;
925 fac_level /= 10;
926 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100927 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100929
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200930 max = size - (log_ptr - dataptr);
931 if (max > logsrv->maxlen)
932 max = logsrv->maxlen;
933
934 /* insert a \n at the end of the message, but save what was
935 * there first because we could have different max lengths
936 * for different log targets.
937 */
938 backup = log_ptr[max - 1];
939 log_ptr[max - 1] = '\n';
940
941 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200942 MSG_DONTWAIT | MSG_NOSIGNAL,
943 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200944
945 log_ptr[max - 1] = backup;
946
Robert Tsai81ae1952007-12-05 10:47:29 +0100947 if (sent < 0) {
948 Alert("sendto logger #%d failed: %s (errno=%d)\n",
949 nblogger, strerror(errno), errno);
950 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 }
952}
953
William Lallemandbddd4fd2012-02-27 11:23:10 +0100954extern fd_set hdr_encode_map[];
955extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100956extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100957
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200959const 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 +0100960const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
961 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
962 Set-cookie Updated, unknown, unknown */
963
William Lallemand1d705562012-03-12 12:46:41 +0100964/*
965 * try to write a character if there is enough space, or goto out
966 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100967#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100968 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100969 *(tmplog++) = (x); \
970 } else { \
971 goto out; \
972 } \
973 } while(0)
974
William Lallemand1d705562012-03-12 12:46:41 +0100975
Willy Tarreaudf974472012-12-28 02:44:01 +0100976/* Builds a log line in <dst> based on <list_format>, and stops before reaching
977 * <maxsize> characters. Returns the size of the output string in characters,
978 * not counting the trailing zero which is always added if the resulting size
979 * is not zero.
980 */
William Lallemand1d705562012-03-12 12:46:41 +0100981int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200982{
Willy Tarreau73de9892006-11-30 11:40:23 +0100983 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100984 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100985 struct http_txn *txn = &s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100986 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200987 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100988 int t_request;
989 int hdr;
990 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100991 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100992 char *ret;
993 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100994 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995
William Lallemandbddd4fd2012-02-27 11:23:10 +0100996 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997
William Lallemandbddd4fd2012-02-27 11:23:10 +0100998 t_request = -1;
999 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1000 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1001
William Lallemand1d705562012-03-12 12:46:41 +01001002 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001003
William Lallemandbddd4fd2012-02-27 11:23:10 +01001004 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001005 if (LIST_ISEMPTY(list_format))
1006 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001007
William Lallemand1d705562012-03-12 12:46:41 +01001008 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001009 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001010 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001011 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001012 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001013
Willy Tarreauc8368452012-12-21 00:09:23 +01001014 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001015 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001016 if (!last_isspace) {
1017 LOGCHAR(' ');
1018 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001019 }
1020 break;
1021
William Lallemand1d705562012-03-12 12:46:41 +01001022 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001023 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001024 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001025 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001026 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001027 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001028 last_isspace = 0;
1029 break;
1030
Willy Tarreauc8368452012-12-21 00:09:23 +01001031 case LOG_FMT_EXPR: // sample expression, may be request or response
1032 key = NULL;
1033 if (tmp->options & LOG_OPT_REQ_CAP)
1034 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
1035 if (!key && (tmp->options & LOG_OPT_RES_CAP))
1036 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001037 if (tmp->options & LOG_OPT_HTTP)
1038 ret = encode_chunk(tmplog, dst + maxsize,
1039 '%', http_encode_map, key ? &key->data.str : &empty);
1040 else
1041 ret = lf_text_len(tmplog, key ? key->data.str.str : NULL, key ? key->data.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001042 if (ret == 0)
1043 goto out;
1044 tmplog = ret;
1045 last_isspace = 0;
1046 break;
1047
Willy Tarreau2beef582012-12-20 17:22:52 +01001048 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001049 conn = objt_conn(s->req->prod->end);
1050 if (conn)
1051 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1052 else
1053 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001054 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001055 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001056 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001057 last_isspace = 0;
1058 break;
1059
Willy Tarreau2beef582012-12-20 17:22:52 +01001060 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001061 conn = objt_conn(s->req->prod->end);
1062 if (conn) {
1063 if (conn->addr.from.ss_family == AF_UNIX) {
1064 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1065 } else {
1066 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1067 dst + maxsize - tmplog, tmp);
1068 }
William Lallemand5f232402012-04-05 18:02:55 +02001069 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001070 else
1071 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1072
William Lallemand5f232402012-04-05 18:02:55 +02001073 if (ret == NULL)
1074 goto out;
1075 tmplog = ret;
1076 last_isspace = 0;
1077 break;
1078
Willy Tarreau2beef582012-12-20 17:22:52 +01001079 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001080 conn = objt_conn(s->req->prod->end);
1081 if (conn) {
1082 conn_get_to_addr(conn);
1083 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1084 }
1085 else
1086 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1087
William Lallemand1d705562012-03-12 12:46:41 +01001088 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001089 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001090 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001091 last_isspace = 0;
1092 break;
1093
Willy Tarreau2beef582012-12-20 17:22:52 +01001094 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001095 conn = objt_conn(s->req->prod->end);
1096 if (conn) {
1097 conn_get_to_addr(conn);
1098 if (conn->addr.to.ss_family == AF_UNIX)
1099 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1100 else
1101 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001102 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001103 else
1104 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1105
William Lallemand5f232402012-04-05 18:02:55 +02001106 if (ret == NULL)
1107 goto out;
1108 tmplog = ret;
1109 last_isspace = 0;
1110 break;
1111
Willy Tarreau2beef582012-12-20 17:22:52 +01001112 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001113 conn = objt_conn(s->req->cons->end);
1114 if (conn)
1115 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1116 else
1117 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1118
William Lallemand1d705562012-03-12 12:46:41 +01001119 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001120 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001121 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001122 last_isspace = 0;
1123 break;
1124
Willy Tarreau2beef582012-12-20 17:22:52 +01001125 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001126 conn = objt_conn(s->req->cons->end);
1127 if (conn)
1128 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1129 else
1130 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1131
William Lallemand5f232402012-04-05 18:02:55 +02001132 if (ret == NULL)
1133 goto out;
1134 tmplog = ret;
1135 last_isspace = 0;
1136 break;
1137
Willy Tarreau2beef582012-12-20 17:22:52 +01001138 case LOG_FMT_SERVERIP: // %si
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001139 conn = objt_conn(s->req->cons->end);
1140 if (conn)
1141 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1142 else
1143 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1144
William Lallemand5f232402012-04-05 18:02:55 +02001145 if (ret == NULL)
1146 goto out;
1147 tmplog = ret;
1148 last_isspace = 0;
1149 break;
1150
Willy Tarreau2beef582012-12-20 17:22:52 +01001151 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001152 conn = objt_conn(s->req->cons->end);
1153 if (conn)
1154 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1155 else
1156 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1157
William Lallemand1d705562012-03-12 12:46:41 +01001158 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001159 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001160 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001161 last_isspace = 0;
1162 break;
1163
William Lallemand1d705562012-03-12 12:46:41 +01001164 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001165 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001166 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1167 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001168 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001169 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001170 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001171 last_isspace = 0;
1172 break;
1173
William Lallemand1d705562012-03-12 12:46:41 +01001174 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001175 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001176 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001177 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001178 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001179 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001180 last_isspace = 0;
1181 break;
1182
Yuxans Yao4e25b012012-10-19 10:36:09 +08001183 case LOG_FMT_DATELOCAL: // %Tl
1184 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIER517cdce2016-03-27 03:04:16 +02001185 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001186 if (ret == NULL)
1187 goto out;
1188 tmplog = ret;
1189 last_isspace = 0;
1190 break;
1191
William Lallemand5f232402012-04-05 18:02:55 +02001192 case LOG_FMT_TS: // %Ts
1193 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1194 if (tmp->options & LOG_OPT_HEXA) {
1195 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1196 if (iret < 0 || iret > dst + maxsize - tmplog)
1197 goto out;
1198 last_isspace = 0;
1199 tmplog += iret;
1200 } else {
1201 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1202 if (ret == NULL)
1203 goto out;
1204 tmplog = ret;
1205 last_isspace = 0;
1206 }
1207 break;
1208
William Lallemand1d705562012-03-12 12:46:41 +01001209 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001210 if (tmp->options & LOG_OPT_HEXA) {
1211 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1212 if (iret < 0 || iret > dst + maxsize - tmplog)
1213 goto out;
1214 last_isspace = 0;
1215 tmplog += iret;
1216 } else {
1217 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001218 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001219 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1220 tmplog, 4);
1221 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001222 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001223 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001224 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001225 }
1226 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001227
William Lallemand1d705562012-03-12 12:46:41 +01001228 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001229 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001230 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001231 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001232 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001233 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001234 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001235 break;
1236
Willy Tarreau773d65f2012-10-12 14:56:11 +02001237 case LOG_FMT_FRONTEND_XPRT: // %ft
1238 src = fe->id;
1239 if (tmp->options & LOG_OPT_QUOTE)
1240 LOGCHAR('"');
1241 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1242 if (iret == 0)
1243 goto out;
1244 tmplog += iret;
1245#ifdef USE_OPENSSL
1246 if (s->listener->xprt == &ssl_sock)
1247 LOGCHAR('~');
1248#endif
1249 if (tmp->options & LOG_OPT_QUOTE)
1250 LOGCHAR('"');
1251 last_isspace = 0;
1252 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001253#ifdef USE_OPENSSL
1254 case LOG_FMT_SSL_CIPHER: // %sslc
1255 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001256 conn = objt_conn(s->si[0].end);
1257 if (conn) {
1258 if (s->listener->xprt == &ssl_sock)
1259 src = ssl_sock_get_cipher_name(conn);
1260 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001261 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1262 if (ret == NULL)
1263 goto out;
1264 tmplog = ret;
1265 last_isspace = 0;
1266 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001267
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001268 case LOG_FMT_SSL_VERSION: // %sslv
1269 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001270 conn = objt_conn(s->si[0].end);
1271 if (conn) {
1272 if (s->listener->xprt == &ssl_sock)
1273 src = ssl_sock_get_proto_version(conn);
1274 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001275 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1276 if (ret == NULL)
1277 goto out;
1278 tmplog = ret;
1279 last_isspace = 0;
1280 break;
1281#endif
William Lallemand1d705562012-03-12 12:46:41 +01001282 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001283 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001284 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001285 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001286 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001287 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001288 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001289 break;
1290
William Lallemand1d705562012-03-12 12:46:41 +01001291 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001292 switch (obj_type(s->target)) {
1293 case OBJ_TYPE_SERVER:
1294 src = objt_server(s->target)->id;
1295 break;
1296 case OBJ_TYPE_APPLET:
1297 src = objt_applet(s->target)->name;
1298 break;
1299 default:
1300 src = "<NOSRV>";
1301 break;
1302 }
William Lallemand5f232402012-04-05 18:02:55 +02001303 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001304 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001305 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001306 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 last_isspace = 0;
1308 break;
1309
William Lallemand1d705562012-03-12 12:46:41 +01001310 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001311 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001312 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001313 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001314 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 last_isspace = 0;
1316 break;
1317
William Lallemand1d705562012-03-12 12:46:41 +01001318 case LOG_FMT_TW: // %Tw
1319 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001320 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001321 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001322 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001323 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001324 last_isspace = 0;
1325 break;
1326
William Lallemand1d705562012-03-12 12:46:41 +01001327 case LOG_FMT_TC: // %Tc
1328 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001329 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001330 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001332 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001333 last_isspace = 0;
1334 break;
1335
William Lallemand1d705562012-03-12 12:46:41 +01001336 case LOG_FMT_TR: // %Tr
1337 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001338 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001339 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001340 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001341 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001342 last_isspace = 0;
1343 break;
1344
William Lallemand1d705562012-03-12 12:46:41 +01001345 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001346 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001347 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001348 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001349 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001350 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001351 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001352 last_isspace = 0;
1353 break;
1354
Willy Tarreau2beef582012-12-20 17:22:52 +01001355 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001356 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001357 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001358 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001359 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001360 last_isspace = 0;
1361 break;
1362
William Lallemand1d705562012-03-12 12:46:41 +01001363 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001364 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001365 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001366 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
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 Tarreauc5259fd2012-12-20 15:38:04 +01001373 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001374 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1375 if (ret == NULL)
1376 goto out;
1377 tmplog = ret;
1378 last_isspace = 0;
1379 break;
1380
Willy Tarreau2beef582012-12-20 17:22:52 +01001381 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001382 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001383 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001384 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001385 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001386 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001387 last_isspace = 0;
1388 break;
1389
Willy Tarreau2beef582012-12-20 17:22:52 +01001390 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001391 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001392 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001393 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001394 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001395 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001396 last_isspace = 0;
1397 break;
1398
William Lallemand1d705562012-03-12 12:46:41 +01001399 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001400 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1401 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1402 *tmplog = '\0';
1403 last_isspace = 0;
1404 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001405
William Lallemand1d705562012-03-12 12:46:41 +01001406 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001407 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1408 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001409 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1410 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 +01001411 last_isspace = 0;
1412 break;
1413
William Lallemand1d705562012-03-12 12:46:41 +01001414 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001415 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001416 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001418 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 last_isspace = 0;
1420 break;
1421
William Lallemand1d705562012-03-12 12:46:41 +01001422 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001423 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001424 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001425 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001426 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001427 last_isspace = 0;
1428 break;
1429
William Lallemand1d705562012-03-12 12:46:41 +01001430 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001431 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001432 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001433 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001434 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001435 last_isspace = 0;
1436 break;
1437
William Lallemand1d705562012-03-12 12:46:41 +01001438 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001439 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001440 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001441 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001442 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001443 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001444 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001445 last_isspace = 0;
1446 break;
1447
William Lallemand1d705562012-03-12 12:46:41 +01001448 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001449 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001450 LOGCHAR('+');
1451 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1452 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001453 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001454 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001455 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001456 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001457 last_isspace = 0;
1458 break;
1459
William Lallemand1d705562012-03-12 12:46:41 +01001460 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001461 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001462 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001463 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001464 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001465 last_isspace = 0;
1466 break;
1467
William Lallemand1d705562012-03-12 12:46:41 +01001468 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001469 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001470 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001471 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001472 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001473 last_isspace = 0;
1474 break;
1475
William Lallemand1d705562012-03-12 12:46:41 +01001476 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001477 /* request header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001478 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001479 if (tmp->options & LOG_OPT_QUOTE)
1480 LOGCHAR('"');
1481 LOGCHAR('{');
1482 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1483 if (hdr)
1484 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001485 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001486 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001487 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001488 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001489 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001490 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001491 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001492 }
1493 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001494 if (tmp->options & LOG_OPT_QUOTE)
1495 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001496 last_isspace = 0;
1497 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001498 break;
1499
William Lallemand1d705562012-03-12 12:46:41 +01001500 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001501 /* request header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001502 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001503 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1504 if (hdr > 0)
1505 LOGCHAR(' ');
1506 if (tmp->options & LOG_OPT_QUOTE)
1507 LOGCHAR('"');
1508 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001509 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001511 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001512 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001513 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001514 } else if (!(tmp->options & LOG_OPT_QUOTE))
1515 LOGCHAR('-');
1516 if (tmp->options & LOG_OPT_QUOTE)
1517 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001518 last_isspace = 0;
1519 }
1520 }
1521 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522
William Lallemand1d705562012-03-12 12:46:41 +01001523
1524 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001525 /* response header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001526 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001527 if (tmp->options & LOG_OPT_QUOTE)
1528 LOGCHAR('"');
1529 LOGCHAR('{');
1530 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1531 if (hdr)
1532 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001533 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001534 ret = encode_string(tmplog, dst + maxsize,
1535 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1536 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001537 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001538 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001539 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001540 }
1541 LOGCHAR('}');
1542 last_isspace = 0;
1543 if (tmp->options & LOG_OPT_QUOTE)
1544 LOGCHAR('"');
1545 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001546 break;
1547
William Lallemand1d705562012-03-12 12:46:41 +01001548 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001549 /* response header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001550 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001551 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1552 if (hdr > 0)
1553 LOGCHAR(' ');
1554 if (tmp->options & LOG_OPT_QUOTE)
1555 LOGCHAR('"');
1556 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001557 ret = encode_string(tmplog, dst + maxsize,
1558 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1559 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001560 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001561 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001562 } else if (!(tmp->options & LOG_OPT_QUOTE))
1563 LOGCHAR('-');
1564 if (tmp->options & LOG_OPT_QUOTE)
1565 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001566 last_isspace = 0;
1567 }
1568 }
1569 break;
1570
William Lallemand1d705562012-03-12 12:46:41 +01001571 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001572 /* Request */
1573 if (tmp->options & LOG_OPT_QUOTE)
1574 LOGCHAR('"');
1575 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001576 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001577 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001578 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001579 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001580 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001581 if (tmp->options & LOG_OPT_QUOTE)
1582 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001583 last_isspace = 0;
1584 break;
William Lallemand5f232402012-04-05 18:02:55 +02001585
1586 case LOG_FMT_COUNTER: // %rt
1587 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001588 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001589 if (iret < 0 || iret > dst + maxsize - tmplog)
1590 goto out;
1591 last_isspace = 0;
1592 tmplog += iret;
1593 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001594 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001595 if (ret == NULL)
1596 goto out;
1597 tmplog = ret;
1598 last_isspace = 0;
1599 }
1600 break;
1601
1602 case LOG_FMT_HOSTNAME: // %H
1603 src = hostname;
1604 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1605 if (ret == NULL)
1606 goto out;
1607 tmplog = ret;
1608 last_isspace = 0;
1609 break;
1610
1611 case LOG_FMT_PID: // %pid
1612 if (tmp->options & LOG_OPT_HEXA) {
1613 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1614 if (iret < 0 || iret > dst + maxsize - tmplog)
1615 goto out;
1616 last_isspace = 0;
1617 tmplog += iret;
1618 } else {
1619 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1620 if (ret == NULL)
1621 goto out;
1622 tmplog = ret;
1623 last_isspace = 0;
1624 }
1625 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001626
1627 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001628 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001629 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001630 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001631 if (ret == NULL)
1632 goto out;
1633 tmplog = ret;
1634 last_isspace = 0;
1635 break;
1636
William Lallemandbddd4fd2012-02-27 11:23:10 +01001637 }
1638 }
1639
1640out:
William Lallemand1d705562012-03-12 12:46:41 +01001641 /* *tmplog is a unused character */
1642 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001643 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001644
Willy Tarreaubaaee002006-06-26 02:48:02 +02001645}
1646
William Lallemand1d705562012-03-12 12:46:41 +01001647/*
1648 * send a log for the session when we have enough info about it.
1649 * Will not log if the frontend has no log defined.
1650 */
1651void sess_log(struct session *s)
1652{
1653 char *tmplog;
1654 int size, err, level;
1655
1656 /* if we don't want to log normal traffic, return now */
Willy Tarreau570f2212013-06-10 16:42:09 +02001657 err = (s->flags & SN_REDISP) ||
1658 ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) ||
1659 (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) &&
1660 (s->req->cons->conn_retries != s->be->conn_retries)) ||
1661 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001662
William Lallemand1d705562012-03-12 12:46:41 +01001663 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1664 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001665
William Lallemand1d705562012-03-12 12:46:41 +01001666 if (LIST_ISEMPTY(&s->fe->logsrvs))
1667 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001668
Willy Tarreauabcd5142013-06-11 17:18:02 +02001669 if (s->logs.level) { /* loglevel was overridden */
1670 if (s->logs.level == -1) {
1671 s->logs.logwait = 0; /* logs disabled */
1672 return;
1673 }
1674 level = s->logs.level - 1;
1675 }
1676 else {
1677 level = LOG_INFO;
1678 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1679 level = LOG_ERR;
1680 }
William Lallemand1d705562012-03-12 12:46:41 +01001681
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001682 /* if unique-id was not generated */
1683 if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) {
1684 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
1685 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
1686 }
1687
William Lallemand1d705562012-03-12 12:46:41 +01001688 tmplog = update_log_hdr();
1689 size = tmplog - logline;
Willy Tarreaudc2695c2014-06-27 18:10:07 +02001690 size += build_logline(s, tmplog, global.max_syslog_len - size, &s->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001691 if (size > 0) {
Willy Tarreaudf974472012-12-28 02:44:01 +01001692 __send_log(s->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001693 s->logs.logwait = 0;
1694 }
1695}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001696
Willy Tarreaubaaee002006-06-26 02:48:02 +02001697/*
1698 * Local variables:
1699 * c-indent-level: 8
1700 * c-basic-offset: 8
1701 * End:
1702 */