Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General logging functions. |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #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 Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
| 24 | #include <sys/time.h> |
Willy Tarreau | 077edcb | 2016-08-10 18:30:56 +0200 | [diff] [blame] | 25 | #include <sys/uio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 29 | #include <common/initcall.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 31 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 33 | #include <types/cli.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 35 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 36 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 37 | #include <proto/applet.h> |
| 38 | #include <proto/cli.h> |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 39 | #include <proto/frontend.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 40 | #include <proto/log.h> |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 41 | #include <proto/sample.h> |
Willy Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 42 | #include <proto/ssl_sock.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 43 | #include <proto/stream.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 44 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 46 | struct log_fmt { |
| 47 | char *name; |
| 48 | struct { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 49 | struct buffer sep1; /* first pid separator */ |
| 50 | struct buffer sep2; /* second pid separator */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 51 | } pid; |
| 52 | }; |
| 53 | |
| 54 | static const struct log_fmt log_formats[LOG_FORMATS] = { |
| 55 | [LOG_FORMAT_RFC3164] = { |
| 56 | .name = "rfc3164", |
| 57 | .pid = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 58 | .sep1 = { .area = "[", .data = 1 }, |
| 59 | .sep2 = { .area = "]: ", .data = 3 } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 60 | } |
| 61 | }, |
| 62 | [LOG_FORMAT_RFC5424] = { |
| 63 | .name = "rfc5424", |
| 64 | .pid = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 65 | .sep1 = { .area = " ", .data = 1 }, |
| 66 | .sep2 = { .area = " - ", .data = 3 } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 67 | } |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 68 | }, |
| 69 | [LOG_FORMAT_SHORT] = { |
| 70 | .name = "short", |
| 71 | .pid = { |
| 72 | .sep1 = { .area = "", .data = 0 }, |
| 73 | .sep2 = { .area = " ", .data = 1 }, |
| 74 | } |
| 75 | }, |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 76 | [LOG_FORMAT_RAW] = { |
| 77 | .name = "raw", |
| 78 | .pid = { |
| 79 | .sep1 = { .area = "", .data = 0 }, |
| 80 | .sep2 = { .area = "", .data = 0 }, |
| 81 | } |
| 82 | }, |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 85 | #define FD_SETS_ARE_BITFIELDS |
| 86 | #ifdef FD_SETS_ARE_BITFIELDS |
| 87 | /* |
| 88 | * This map is used with all the FD_* macros to check whether a particular bit |
| 89 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 90 | * which should be escaped. When FD_ISSET() returns non-zero, it means that the |
| 91 | * byte should be escaped. Be careful to always pass bytes from 0 to 255 |
| 92 | * exclusively to the macros. |
| 93 | */ |
| 94 | fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 95 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 96 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 97 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 98 | |
| 99 | #else |
| 100 | #error "Check if your OS uses bitfields for fd_sets" |
| 101 | #endif |
| 102 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 104 | "kern", "user", "mail", "daemon", |
| 105 | "auth", "syslog", "lpr", "news", |
| 106 | "uucp", "cron", "auth2", "ftp", |
| 107 | "ntp", "audit", "alert", "cron2", |
| 108 | "local0", "local1", "local2", "local3", |
| 109 | "local4", "local5", "local6", "local7" |
| 110 | }; |
| 111 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | const char *log_levels[NB_LOG_LEVELS] = { |
| 113 | "emerg", "alert", "crit", "err", |
| 114 | "warning", "notice", "info", "debug" |
| 115 | }; |
| 116 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 117 | const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 118 | const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 120 | |
| 121 | /* log_format */ |
| 122 | struct logformat_type { |
| 123 | char *name; |
| 124 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 125 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 126 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 127 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 128 | const char *replace_by; /* new option to use instead of old one */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 131 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 132 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 133 | /* log_format variable names */ |
| 134 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 135 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 136 | |
| 137 | /* please keep these lines sorted ! */ |
| 138 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 139 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 140 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 141 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
| 142 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 143 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 144 | { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 145 | { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 146 | { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 147 | { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */ |
| 148 | { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */ |
| 149 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
| 150 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */ |
| 151 | { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
| 152 | { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */ |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 153 | { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 154 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 155 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 156 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 157 | { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 158 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 159 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 160 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 161 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 162 | { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 163 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 164 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */ |
| 165 | { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 166 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 167 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 168 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */ |
| 169 | { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 170 | { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */ |
Willy Tarreau | d9ed3d2 | 2014-06-13 12:23:06 +0200 | [diff] [blame] | 171 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 172 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 173 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 174 | { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 175 | { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */ |
| 176 | { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */ |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 177 | { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 178 | { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */ |
| 179 | { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */ |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 180 | { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 181 | { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 182 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 183 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 184 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 185 | { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 186 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 187 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 188 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 189 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 190 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 191 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 192 | { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 193 | { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 194 | { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */ |
| 195 | { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */ |
| 196 | { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 197 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 198 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
| 199 | |
| 200 | /* The following tags are deprecated and will be removed soon */ |
| 201 | { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */ |
| 202 | { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 203 | { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */ |
| 204 | { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */ |
| 205 | { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */ |
| 206 | { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 207 | { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */ |
| 208 | { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */ |
| 209 | { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */ |
| 210 | { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */ |
| 211 | { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 212 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 213 | }; |
| 214 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 215 | char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format |
| 216 | char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"; |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 217 | char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 218 | char *log_format = NULL; |
| 219 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 220 | /* Default string used for structured-data part in RFC5424 formatted |
| 221 | * syslog messages. |
| 222 | */ |
| 223 | char default_rfc5424_sd_log_format[] = "- "; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 224 | |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 225 | /* total number of dropped logs */ |
| 226 | unsigned int dropped_logs = 0; |
| 227 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 228 | /* This is a global syslog header, common to all outgoing messages in |
| 229 | * RFC3164 format. It begins with time-based part and is updated by |
| 230 | * update_log_hdr(). |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 231 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 232 | THREAD_LOCAL char *logheader = NULL; |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 233 | THREAD_LOCAL char *logheader_end = NULL; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 234 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 235 | /* This is a global syslog header for messages in RFC5424 format. It is |
| 236 | * updated by update_log_hdr_rfc5424(). |
| 237 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 238 | THREAD_LOCAL char *logheader_rfc5424 = NULL; |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 239 | THREAD_LOCAL char *logheader_rfc5424_end = NULL; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 240 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 241 | /* This is a global syslog message buffer, common to all outgoing |
| 242 | * messages. It contains only the data part. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 243 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 244 | THREAD_LOCAL char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 245 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 246 | /* A global syslog message buffer, common to all RFC5424 syslog messages. |
| 247 | * Currently, it is used for generating the structured-data part. |
| 248 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 249 | THREAD_LOCAL char *logline_rfc5424 = NULL; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 250 | |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 251 | /* A global buffer used to store all startup alerts/warnings. It will then be |
| 252 | * retrieve on the CLI. */ |
Willy Tarreau | a648399 | 2018-12-15 16:55:36 +0100 | [diff] [blame] | 253 | static char *startup_logs = NULL; |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 254 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 255 | struct logformat_var_args { |
| 256 | char *name; |
| 257 | int mask; |
| 258 | }; |
| 259 | |
| 260 | struct logformat_var_args var_args_list[] = { |
| 261 | // global |
| 262 | { "M", LOG_OPT_MANDATORY }, |
| 263 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 264 | { "X", LOG_OPT_HEXA }, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 265 | { "E", LOG_OPT_ESC }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 266 | { 0, 0 } |
| 267 | }; |
| 268 | |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 269 | /* return the name of the directive used in the current proxy for which we're |
| 270 | * currently parsing a header, when it is known. |
| 271 | */ |
| 272 | static inline const char *fmt_directive(const struct proxy *curproxy) |
| 273 | { |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 274 | switch (curproxy->conf.args.ctx) { |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 275 | case ARGC_ACL: |
| 276 | return "acl"; |
| 277 | case ARGC_STK: |
| 278 | return "stick"; |
| 279 | case ARGC_TRK: |
| 280 | return "track-sc"; |
| 281 | case ARGC_LOG: |
| 282 | return "log-format"; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 283 | case ARGC_LOGSD: |
| 284 | return "log-format-sd"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 285 | case ARGC_HRQ: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 286 | return "http-request"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 287 | case ARGC_HRS: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 288 | return "http-response"; |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 289 | case ARGC_UIF: |
| 290 | return "unique-id-format"; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 291 | case ARGC_RDR: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 292 | return "redirect"; |
| 293 | case ARGC_CAP: |
| 294 | return "capture"; |
Willy Tarreau | 28d976d | 2015-07-09 11:39:33 +0200 | [diff] [blame] | 295 | case ARGC_SRV: |
| 296 | return "server"; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 297 | case ARGC_SPOE: |
| 298 | return "spoe-message"; |
Thierry FOURNIER / OZON.IO | 4ed1c95 | 2016-11-24 23:57:54 +0100 | [diff] [blame] | 299 | case ARGC_UBK: |
| 300 | return "use_backend"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 301 | default: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 302 | return "undefined(please report this bug)"; /* must never happen */ |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 303 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 304 | } |
| 305 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 306 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 307 | * callback used to configure addr source retrieval |
| 308 | */ |
| 309 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 310 | { |
| 311 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 318 | * Parse args in a logformat_var. Returns 0 in error |
| 319 | * case, otherwise, it returns 1. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 320 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 321 | int parse_logformat_var_args(char *args, struct logformat_node *node, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 322 | { |
| 323 | int i = 0; |
| 324 | int end = 0; |
| 325 | int flags = 0; // 1 = + 2 = - |
| 326 | char *sp = NULL; // start pointer |
| 327 | |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 328 | if (args == NULL) { |
| 329 | memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'"); |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 330 | return 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 331 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 332 | |
| 333 | while (1) { |
| 334 | if (*args == '\0') |
| 335 | end = 1; |
| 336 | |
| 337 | if (*args == '+') { |
| 338 | // add flag |
| 339 | sp = args + 1; |
| 340 | flags = 1; |
| 341 | } |
| 342 | if (*args == '-') { |
| 343 | // delete flag |
| 344 | sp = args + 1; |
| 345 | flags = 2; |
| 346 | } |
| 347 | |
| 348 | if (*args == '\0' || *args == ',') { |
| 349 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 350 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 351 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 352 | if (flags == 1) { |
| 353 | node->options |= var_args_list[i].mask; |
| 354 | break; |
| 355 | } else if (flags == 2) { |
| 356 | node->options &= ~var_args_list[i].mask; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | sp = NULL; |
| 362 | if (end) |
| 363 | break; |
| 364 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 365 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 366 | } |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 367 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 371 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 372 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 373 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 374 | * ignored when arg_len is 0. Neither <var> nor <var_len> may be null. |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 375 | * Returns false in error case and err is filled, otherwise returns true. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 376 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 377 | int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 378 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 379 | int j; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 380 | struct logformat_node *node = NULL; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 382 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 383 | if (strlen(logformat_keywords[j].name) == var_len && |
| 384 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 385 | if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 386 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 387 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 388 | memprintf(err, "out of memory error"); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 389 | goto error_free; |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 390 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 391 | node->type = logformat_keywords[j].type; |
| 392 | node->options = *defoptions; |
| 393 | if (arg_len) { |
| 394 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 395 | if (!parse_logformat_var_args(node->arg, node, err)) |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 396 | goto error_free; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 397 | } |
| 398 | if (node->type == LOG_FMT_GLOBAL) { |
| 399 | *defoptions = node->options; |
| 400 | free(node->arg); |
| 401 | free(node); |
| 402 | } else { |
| 403 | if (logformat_keywords[j].config_callback && |
| 404 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 405 | goto error_free; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 406 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 407 | curproxy->to_log |= logformat_keywords[j].lw; |
| 408 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 409 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 410 | if (logformat_keywords[j].replace_by) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 411 | ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n", |
| 412 | curproxy->conf.args.file, curproxy->conf.args.line, |
| 413 | logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by); |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 414 | return 1; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 415 | } else { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 416 | memprintf(err, "format variable '%s' is reserved for HTTP mode", |
| 417 | logformat_keywords[j].name); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 418 | goto error_free; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 419 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 422 | |
| 423 | j = var[var_len]; |
| 424 | var[var_len] = 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 425 | memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 426 | var[var_len] = j; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 427 | |
| 428 | error_free: |
| 429 | if (node) { |
| 430 | free(node->arg); |
| 431 | free(node); |
| 432 | } |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 433 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
| 437 | * push to the logformat linked list |
| 438 | * |
| 439 | * start: start pointer |
| 440 | * end: end text pointer |
| 441 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 442 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 443 | * |
| 444 | * LOG_TEXT: copy chars from start to end excluding end. |
| 445 | * |
| 446 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 447 | int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 448 | { |
| 449 | char *str; |
| 450 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 451 | if (type == LF_TEXT) { /* type text */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 452 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 453 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 454 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 455 | return 0; |
| 456 | } |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 457 | str = calloc(1, end - start + 1); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 458 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 459 | str[end - start] = '\0'; |
| 460 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 461 | node->type = LOG_FMT_TEXT; // type string |
| 462 | LIST_ADDQ(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 463 | } else if (type == LF_SEPARATOR) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 464 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 465 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 466 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 467 | return 0; |
| 468 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 469 | node->type = LOG_FMT_SEPARATOR; |
| 470 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 471 | } |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 472 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 476 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 477 | * success. At the moment, sample converters are not yet supported but fetch arguments |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 478 | * should work. The curpx->conf.args.ctx must be set by the caller. |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 479 | * |
| 480 | * In error case, the function returns 0, otherwise it returns 1. |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 481 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 482 | int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 483 | { |
| 484 | char *cmd[2]; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 485 | struct sample_expr *expr = NULL; |
| 486 | struct logformat_node *node = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 487 | int cmd_arg; |
| 488 | |
| 489 | cmd[0] = text; |
| 490 | cmd[1] = ""; |
| 491 | cmd_arg = 0; |
| 492 | |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 493 | expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 494 | if (!expr) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 495 | memprintf(err, "failed to parse sample expression <%s> : %s", text, *err); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 496 | goto error_free; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 499 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 500 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 501 | memprintf(err, "out of memory error"); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 502 | goto error_free; |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 503 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 504 | node->type = LOG_FMT_EXPR; |
| 505 | node->expr = expr; |
| 506 | node->options = options; |
| 507 | |
| 508 | if (arg_len) { |
| 509 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 510 | if (!parse_logformat_var_args(node->arg, node, err)) |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 511 | goto error_free; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 512 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 513 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 514 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 515 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 516 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 517 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 518 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 519 | if (!(expr->fetch->val & cap)) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 520 | memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here", |
| 521 | text, sample_src_names(expr->fetch->use)); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 522 | goto error_free; |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 523 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 524 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 525 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 526 | /* Note, we may also need to set curpx->to_log with certain fetches */ |
Willy Tarreau | 25320b2 | 2013-03-24 07:22:08 +0100 | [diff] [blame] | 527 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 528 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 529 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 530 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 531 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 532 | */ |
| 533 | curpx->to_log |= LW_XPRT; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 534 | curpx->to_log |= LW_REQ; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 535 | LIST_ADDQ(list_format, &node->list); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 536 | return 1; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 537 | |
| 538 | error_free: |
| 539 | release_sample_expr(expr); |
| 540 | if (node) { |
| 541 | free(node->arg); |
| 542 | free(node); |
| 543 | } |
| 544 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 548 | * Parse the log_format string and fill a linked list. |
| 549 | * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 550 | * You can set arguments using { } : %{many arguments}varname. |
| 551 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 552 | * |
| 553 | * str: the string to parse |
| 554 | * curproxy: the proxy affected |
| 555 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 556 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 557 | * cap: all SMP_VAL_* flags supported by the consumer |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 558 | * |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 559 | * The function returns 1 in success case, otherwise, it returns 0 and err is filled. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 560 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 561 | int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 562 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 563 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 564 | char *arg = NULL; /* start pointer for args */ |
| 565 | char *var = NULL; /* start pointer for vars */ |
| 566 | int arg_len = 0; |
| 567 | int var_len = 0; |
| 568 | int cformat; /* current token format */ |
| 569 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 570 | struct logformat_node *tmplf, *back; |
| 571 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 572 | sp = str = backfmt = strdup(fmt); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 573 | if (!str) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 574 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 575 | return 0; |
| 576 | } |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 577 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 578 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 579 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 580 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 581 | LIST_DEL(&tmplf->list); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 582 | release_sample_expr(tmplf->expr); |
| 583 | free(tmplf->arg); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 584 | free(tmplf); |
| 585 | } |
| 586 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 587 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 588 | pformat = cformat; |
| 589 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 590 | if (!*str) |
| 591 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 592 | |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 593 | /* The principle of the two-step state machine below is to first detect a change, and |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 594 | * second have all common paths processed at one place. The common paths are the ones |
| 595 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 596 | * We use the common LF_INIT state to dispatch to the different final states. |
| 597 | */ |
| 598 | switch (pformat) { |
| 599 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 600 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 601 | arg_len = var_len = 0; |
| 602 | if (*str == '{') { // optional argument |
| 603 | cformat = LF_STARG; |
| 604 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 605 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 606 | else if (*str == '[') { |
| 607 | cformat = LF_STEXPR; |
| 608 | var = str + 1; // store expr in variable name |
| 609 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 610 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 611 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 612 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 613 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 614 | else if (*str == '%') |
| 615 | cformat = LF_TEXT; // convert this character to a litteral (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 616 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 617 | /* single '%' followed by blank or digit, send them both */ |
| 618 | cformat = LF_TEXT; |
| 619 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 620 | sp = str - 1; /* send both the '%' and the current char */ |
Jim Freeman | a2278c8 | 2017-04-15 08:01:59 -0600 | [diff] [blame] | 621 | memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'", |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 622 | *str, (int)(str - backfmt), fmt); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 623 | return 0; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 624 | |
| 625 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 626 | else |
| 627 | cformat = LF_INIT; // handle other cases of litterals |
| 628 | break; |
| 629 | |
| 630 | case LF_STARG: // text immediately following '%{' |
| 631 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 632 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 633 | arg_len = str - arg; |
| 634 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 635 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 636 | break; |
| 637 | |
| 638 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 639 | if (*str == '[') { |
| 640 | cformat = LF_STEXPR; |
| 641 | var = str + 1; // store expr in variable name |
| 642 | break; |
| 643 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 644 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 645 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 646 | var = str; |
| 647 | break; |
| 648 | } |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 649 | memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 650 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 651 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 652 | case LF_STEXPR: // text immediately following '%[' |
| 653 | if (*str == ']') { // end of arg |
| 654 | cformat = LF_EDEXPR; |
| 655 | var_len = str - var; |
| 656 | *str = 0; // needed for parsing the expression |
| 657 | } |
| 658 | break; |
| 659 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 660 | case LF_VAR: // text part of a variable name |
| 661 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 662 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 663 | cformat = LF_INIT; // not variable name anymore |
| 664 | break; |
| 665 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 666 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 667 | cformat = LF_INIT; |
| 668 | } |
| 669 | |
| 670 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 671 | switch (*str) { |
| 672 | case '%': cformat = LF_STARTVAR; break; |
| 673 | case ' ': cformat = LF_SEPARATOR; break; |
| 674 | case 0 : cformat = LF_END; break; |
| 675 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 679 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 680 | switch (pformat) { |
| 681 | case LF_VAR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 682 | if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 683 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 684 | break; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 685 | case LF_STEXPR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 686 | if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 687 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 688 | break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 689 | case LF_TEXT: |
| 690 | case LF_SEPARATOR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 691 | if (!add_to_logformat_list(sp, str, pformat, list_format, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 692 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | sp = str; /* new start of text at every state switch and at every separator */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 696 | } |
| 697 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 698 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 699 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 700 | memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%"); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 701 | return 0; |
| 702 | } |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 703 | free(backfmt); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 704 | |
| 705 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 708 | /* |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 709 | * Parse the first range of indexes from a string made of a list of comma seperated |
| 710 | * ranges of indexes. Note that an index may be considered as a particular range |
| 711 | * with a high limit to the low limit. |
| 712 | */ |
| 713 | int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err) |
| 714 | { |
| 715 | char *end, *p; |
| 716 | |
| 717 | *low = *high = 0; |
| 718 | |
| 719 | p = *arg; |
| 720 | end = strchr(p, ','); |
| 721 | if (!end) |
| 722 | end = p + strlen(p); |
| 723 | |
| 724 | *high = *low = read_uint((const char **)&p, end); |
| 725 | if (!*low || (p != end && *p != '-')) |
| 726 | goto err; |
| 727 | |
| 728 | if (p == end) |
| 729 | goto done; |
| 730 | |
| 731 | p++; |
| 732 | *high = read_uint((const char **)&p, end); |
| 733 | if (!*high || *high <= *low || p != end) |
| 734 | goto err; |
| 735 | |
| 736 | done: |
| 737 | if (*end == ',') |
| 738 | end++; |
| 739 | *arg = end; |
| 740 | return 1; |
| 741 | |
| 742 | err: |
| 743 | memprintf(err, "wrong sample range '%s'", *arg); |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Returns 1 if the range defined by <low> and <high> overlaps |
| 749 | * one of them in <rgs> array of ranges with <sz> the size of this |
| 750 | * array, 0 if not. |
| 751 | */ |
| 752 | int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz, |
| 753 | unsigned int low, unsigned int high, char **err) |
| 754 | { |
| 755 | size_t i; |
| 756 | |
| 757 | for (i = 0; i < sz; i++) { |
| 758 | if ((low >= rgs[i].low && low <= rgs[i].high) || |
| 759 | (high >= rgs[i].low && high <= rgs[i].high)) { |
| 760 | memprintf(err, "ranges are overlapping"); |
| 761 | return 1; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | int smp_log_range_cmp(const void *a, const void *b) |
| 769 | { |
| 770 | const struct smp_log_range *rg_a = a; |
| 771 | const struct smp_log_range *rg_b = b; |
| 772 | |
| 773 | if (rg_a->high < rg_b->low) |
| 774 | return -1; |
| 775 | else if (rg_a->low > rg_b->high) |
| 776 | return 1; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /* |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 782 | * Parse "log" keyword and update <logsrvs> list accordingly. |
| 783 | * |
| 784 | * When <do_del> is set, it means the "no log" line was parsed, so all log |
| 785 | * servers in <logsrvs> are released. |
| 786 | * |
| 787 | * Otherwise, we try to parse the "log" line. First of all, when the list is not |
| 788 | * the global one, we look for the parameter "global". If we find it, |
| 789 | * global.logsrvs is copied. Else we parse each arguments. |
| 790 | * |
| 791 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 792 | * filled. |
| 793 | */ |
| 794 | int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err) |
| 795 | { |
| 796 | struct sockaddr_storage *sk; |
| 797 | struct logsrv *logsrv = NULL; |
| 798 | int port1, port2; |
| 799 | int cur_arg; |
| 800 | |
| 801 | /* |
| 802 | * "no log": delete previous herited or defined syslog |
| 803 | * servers. |
| 804 | */ |
| 805 | if (do_del) { |
| 806 | struct logsrv *back; |
| 807 | |
| 808 | if (*(args[1]) != 0) { |
| 809 | memprintf(err, "'no log' does not expect arguments"); |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | list_for_each_entry_safe(logsrv, back, logsrvs, list) { |
| 814 | LIST_DEL(&logsrv->list); |
| 815 | free(logsrv); |
| 816 | } |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * "log global": copy global.logrsvs linked list to the end of logsrvs |
| 822 | * list. But first, we check (logsrvs != global.logsrvs). |
| 823 | */ |
| 824 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 825 | if (logsrvs == &global.logsrvs) { |
| 826 | memprintf(err, "'global' is not supported for a global syslog server"); |
| 827 | goto error; |
| 828 | } |
| 829 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 830 | struct logsrv *node; |
| 831 | |
| 832 | list_for_each_entry(node, logsrvs, list) { |
| 833 | if (node->ref == logsrv) |
| 834 | goto skip_logsrv; |
| 835 | } |
| 836 | |
| 837 | node = malloc(sizeof(*node)); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 838 | memcpy(node, logsrv, sizeof(struct logsrv)); |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 839 | node->ref = logsrv; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 840 | LIST_INIT(&node->list); |
| 841 | LIST_ADDQ(logsrvs, &node->list); |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 842 | |
| 843 | skip_logsrv: |
| 844 | continue; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 845 | } |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * "log <address> ...: parse a syslog server line |
| 851 | */ |
| 852 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 853 | memprintf(err, "expects <address> and <facility> %s as arguments", |
| 854 | ((logsrvs == &global.logsrvs) ? "" : "or global")); |
| 855 | goto error; |
| 856 | } |
| 857 | |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 858 | /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */ |
| 859 | if (strcmp(args[1], "stdout") == 0) |
| 860 | args[1] = "fd@1"; |
| 861 | else if (strcmp(args[1], "stderr") == 0) |
| 862 | args[1] = "fd@2"; |
| 863 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 864 | logsrv = calloc(1, sizeof(*logsrv)); |
| 865 | if (!logsrv) { |
| 866 | memprintf(err, "out of memory"); |
| 867 | goto error; |
| 868 | } |
| 869 | |
| 870 | /* skip address for now, it will be parsed at the end */ |
| 871 | cur_arg = 2; |
| 872 | |
| 873 | /* just after the address, a length may be specified */ |
| 874 | logsrv->maxlen = MAX_SYSLOG_LEN; |
| 875 | if (strcmp(args[cur_arg], "len") == 0) { |
| 876 | int len = atoi(args[cur_arg+1]); |
| 877 | if (len < 80 || len > 65535) { |
| 878 | memprintf(err, "invalid log length '%s', must be between 80 and 65535", |
| 879 | args[cur_arg+1]); |
| 880 | goto error; |
| 881 | } |
| 882 | logsrv->maxlen = len; |
| 883 | cur_arg += 2; |
| 884 | } |
| 885 | if (logsrv->maxlen > global.max_syslog_len) |
| 886 | global.max_syslog_len = logsrv->maxlen; |
| 887 | |
| 888 | /* after the length, a format may be specified */ |
| 889 | if (strcmp(args[cur_arg], "format") == 0) { |
| 890 | logsrv->format = get_log_format(args[cur_arg+1]); |
| 891 | if (logsrv->format < 0) { |
| 892 | memprintf(err, "unknown log format '%s'", args[cur_arg+1]); |
| 893 | goto error; |
| 894 | } |
| 895 | cur_arg += 2; |
| 896 | } |
| 897 | |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 898 | if (strcmp(args[cur_arg], "sample") == 0) { |
| 899 | unsigned low, high; |
| 900 | char *p, *beg, *end, *smp_sz_str; |
| 901 | struct smp_log_range *smp_rgs = NULL; |
| 902 | size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz; |
| 903 | |
| 904 | p = args[cur_arg+1]; |
| 905 | smp_sz_str = strchr(p, ':'); |
| 906 | if (!smp_sz_str) { |
| 907 | memprintf(err, "Missing sample size"); |
| 908 | goto error; |
| 909 | } |
| 910 | |
| 911 | *smp_sz_str++ = '\0'; |
| 912 | |
| 913 | end = p + strlen(p); |
| 914 | |
| 915 | while (p != end) { |
| 916 | if (!get_logsrv_smp_range(&low, &high, &p, err)) |
| 917 | goto error; |
| 918 | |
| 919 | if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err)) |
| 920 | goto error; |
| 921 | |
| 922 | smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs); |
| 923 | if (!smp_rgs) { |
| 924 | memprintf(err, "out of memory error"); |
| 925 | goto error; |
| 926 | } |
| 927 | |
| 928 | smp_rgs[smp_rgs_sz].low = low; |
| 929 | smp_rgs[smp_rgs_sz].high = high; |
| 930 | smp_rgs[smp_rgs_sz].sz = high - low + 1; |
| 931 | smp_rgs[smp_rgs_sz].curr_idx = 0; |
| 932 | if (smp_rgs[smp_rgs_sz].high > smp_sz) |
| 933 | smp_sz = smp_rgs[smp_rgs_sz].high; |
| 934 | smp_rgs_sz++; |
| 935 | } |
| 936 | |
| 937 | beg = smp_sz_str; |
| 938 | end = beg + strlen(beg); |
| 939 | new_smp_sz = read_uint((const char **)&beg, end); |
| 940 | if (!new_smp_sz || beg != end) { |
| 941 | memprintf(err, "wrong sample size '%s' for sample range '%s'", |
| 942 | smp_sz_str, args[cur_arg+1]); |
| 943 | goto error; |
| 944 | } |
| 945 | |
| 946 | if (new_smp_sz < smp_sz) { |
| 947 | memprintf(err, "sample size %zu should be greater or equal to " |
| 948 | "%zu the maximum of the high ranges limits", |
| 949 | new_smp_sz, smp_sz); |
| 950 | goto error; |
| 951 | } |
| 952 | smp_sz = new_smp_sz; |
| 953 | |
| 954 | /* Let's order <smp_rgs> array. */ |
| 955 | qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp); |
| 956 | |
| 957 | logsrv->lb.smp_rgs = smp_rgs; |
| 958 | logsrv->lb.smp_rgs_sz = smp_rgs_sz; |
| 959 | logsrv->lb.smp_sz = smp_sz; |
| 960 | |
| 961 | cur_arg += 2; |
| 962 | } |
Frédéric Lécaille | d803e47 | 2019-04-25 07:42:09 +0200 | [diff] [blame] | 963 | HA_SPIN_INIT(&logsrv->lock); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 964 | /* parse the facility */ |
| 965 | logsrv->facility = get_log_facility(args[cur_arg]); |
| 966 | if (logsrv->facility < 0) { |
| 967 | memprintf(err, "unknown log facility '%s'", args[cur_arg]); |
| 968 | goto error; |
| 969 | } |
| 970 | cur_arg++; |
| 971 | |
| 972 | /* parse the max syslog level (default: debug) */ |
| 973 | logsrv->level = 7; |
| 974 | if (*(args[cur_arg])) { |
| 975 | logsrv->level = get_log_level(args[cur_arg]); |
| 976 | if (logsrv->level < 0) { |
| 977 | memprintf(err, "unknown optional log level '%s'", args[cur_arg]); |
| 978 | goto error; |
| 979 | } |
| 980 | cur_arg++; |
| 981 | } |
| 982 | |
| 983 | /* parse the limit syslog level (default: emerg) */ |
| 984 | logsrv->minlvl = 0; |
| 985 | if (*(args[cur_arg])) { |
| 986 | logsrv->minlvl = get_log_level(args[cur_arg]); |
| 987 | if (logsrv->minlvl < 0) { |
| 988 | memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]); |
| 989 | goto error; |
| 990 | } |
| 991 | cur_arg++; |
| 992 | } |
| 993 | |
| 994 | /* Too many args */ |
| 995 | if (*(args[cur_arg])) { |
| 996 | memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]); |
| 997 | goto error; |
| 998 | } |
| 999 | |
| 1000 | /* now, back to the address */ |
| 1001 | sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1); |
| 1002 | if (!sk) |
| 1003 | goto error; |
| 1004 | logsrv->addr = *sk; |
| 1005 | |
| 1006 | if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) { |
| 1007 | if (port1 != port2) { |
| 1008 | memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]); |
| 1009 | goto error; |
| 1010 | } |
| 1011 | logsrv->addr = *sk; |
| 1012 | if (!port1) |
| 1013 | set_host_port(&logsrv->addr, SYSLOG_PORT); |
| 1014 | } |
| 1015 | LIST_ADDQ(logsrvs, &logsrv->list); |
| 1016 | return 1; |
| 1017 | |
| 1018 | error: |
| 1019 | free(logsrv); |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 1024 | /* Generic function to display messages prefixed by a label */ |
| 1025 | static void print_message(const char *label, const char *fmt, va_list argp) |
| 1026 | { |
| 1027 | struct tm tm; |
| 1028 | char *head, *msg; |
| 1029 | |
| 1030 | head = msg = NULL; |
| 1031 | |
| 1032 | get_localtime(date.tv_sec, &tm); |
| 1033 | memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ", |
| 1034 | label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
| 1035 | memvprintf(&msg, fmt, argp); |
| 1036 | |
| 1037 | if (global.mode & MODE_STARTING) |
| 1038 | memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg); |
| 1039 | |
| 1040 | fprintf(stderr, "%s%s", head, msg); |
| 1041 | fflush(stderr); |
| 1042 | |
| 1043 | free(head); |
| 1044 | free(msg); |
| 1045 | } |
| 1046 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1047 | /* |
| 1048 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 1049 | * mode during startup. |
| 1050 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1051 | void ha_alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1052 | { |
| 1053 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1054 | |
| 1055 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 1056 | va_start(argp, fmt); |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 1057 | print_message("ALERT", fmt, argp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1058 | va_end(argp); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /* |
| 1064 | * Displays the message on stderr with the date and pid. |
| 1065 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1066 | void ha_warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1067 | { |
| 1068 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1069 | |
| 1070 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 1071 | va_start(argp, fmt); |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 1072 | print_message("WARNING", fmt, argp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1073 | va_end(argp); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* |
William Lallemand | 9c56a22 | 2018-11-21 18:04:52 +0100 | [diff] [blame] | 1078 | * Displays the message on stderr with the date and pid. |
| 1079 | */ |
| 1080 | void ha_notice(const char *fmt, ...) |
| 1081 | { |
| 1082 | va_list argp; |
| 1083 | |
| 1084 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 1085 | va_start(argp, fmt); |
| 1086 | print_message("NOTICE", fmt, argp); |
| 1087 | va_end(argp); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1092 | * Displays the message on <out> only if quiet mode is not set. |
| 1093 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 1094 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1095 | { |
| 1096 | va_list argp; |
| 1097 | |
| 1098 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 1099 | va_start(argp, fmt); |
| 1100 | vfprintf(out, fmt, argp); |
| 1101 | fflush(out); |
| 1102 | va_end(argp); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1107 | * returns log format for <fmt> or -1 if not found. |
| 1108 | */ |
| 1109 | int get_log_format(const char *fmt) |
| 1110 | { |
| 1111 | int format; |
| 1112 | |
| 1113 | format = LOG_FORMATS - 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1114 | while (format >= 0 && strcmp(log_formats[format].name, fmt)) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1115 | format--; |
| 1116 | |
| 1117 | return format; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1121 | * returns log level for <lev> or -1 if not found. |
| 1122 | */ |
| 1123 | int get_log_level(const char *lev) |
| 1124 | { |
| 1125 | int level; |
| 1126 | |
| 1127 | level = NB_LOG_LEVELS - 1; |
| 1128 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 1129 | level--; |
| 1130 | |
| 1131 | return level; |
| 1132 | } |
| 1133 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1134 | /* |
| 1135 | * returns log facility for <fac> or -1 if not found. |
| 1136 | */ |
| 1137 | int get_log_facility(const char *fac) |
| 1138 | { |
| 1139 | int facility; |
| 1140 | |
| 1141 | facility = NB_LOG_FACILITIES - 1; |
| 1142 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 1143 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1144 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1145 | return facility; |
| 1146 | } |
| 1147 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1148 | /* |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1149 | * Encode the string. |
| 1150 | * |
| 1151 | * When using the +E log format option, it will try to escape '"\]' |
| 1152 | * characters with '\' as prefix. The same prefix should not be used as |
| 1153 | * <escape>. |
| 1154 | */ |
| 1155 | static char *lf_encode_string(char *start, char *stop, |
| 1156 | const char escape, const fd_set *map, |
| 1157 | const char *string, |
| 1158 | struct logformat_node *node) |
| 1159 | { |
| 1160 | if (node->options & LOG_OPT_ESC) { |
| 1161 | if (start < stop) { |
| 1162 | stop--; /* reserve one byte for the final '\0' */ |
| 1163 | while (start < stop && *string != '\0') { |
| 1164 | if (!FD_ISSET((unsigned char)(*string), map)) { |
| 1165 | if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map)) |
| 1166 | *start++ = *string; |
| 1167 | else { |
| 1168 | if (start + 2 >= stop) |
| 1169 | break; |
| 1170 | *start++ = '\\'; |
| 1171 | *start++ = *string; |
| 1172 | } |
| 1173 | } |
| 1174 | else { |
| 1175 | if (start + 3 >= stop) |
| 1176 | break; |
| 1177 | *start++ = escape; |
| 1178 | *start++ = hextab[(*string >> 4) & 15]; |
| 1179 | *start++ = hextab[*string & 15]; |
| 1180 | } |
| 1181 | string++; |
| 1182 | } |
| 1183 | *start = '\0'; |
| 1184 | } |
| 1185 | } |
| 1186 | else { |
| 1187 | return encode_string(start, stop, escape, map, string); |
| 1188 | } |
| 1189 | |
| 1190 | return start; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * Encode the chunk. |
| 1195 | * |
| 1196 | * When using the +E log format option, it will try to escape '"\]' |
| 1197 | * characters with '\' as prefix. The same prefix should not be used as |
| 1198 | * <escape>. |
| 1199 | */ |
| 1200 | static char *lf_encode_chunk(char *start, char *stop, |
| 1201 | const char escape, const fd_set *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1202 | const struct buffer *chunk, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1203 | struct logformat_node *node) |
| 1204 | { |
| 1205 | char *str, *end; |
| 1206 | |
| 1207 | if (node->options & LOG_OPT_ESC) { |
| 1208 | if (start < stop) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1209 | str = chunk->area; |
| 1210 | end = chunk->area + chunk->data; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1211 | |
| 1212 | stop--; /* reserve one byte for the final '\0' */ |
| 1213 | while (start < stop && str < end) { |
| 1214 | if (!FD_ISSET((unsigned char)(*str), map)) { |
| 1215 | if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map)) |
| 1216 | *start++ = *str; |
| 1217 | else { |
| 1218 | if (start + 2 >= stop) |
| 1219 | break; |
| 1220 | *start++ = '\\'; |
| 1221 | *start++ = *str; |
| 1222 | } |
| 1223 | } |
| 1224 | else { |
| 1225 | if (start + 3 >= stop) |
| 1226 | break; |
| 1227 | *start++ = escape; |
| 1228 | *start++ = hextab[(*str >> 4) & 15]; |
| 1229 | *start++ = hextab[*str & 15]; |
| 1230 | } |
| 1231 | str++; |
| 1232 | } |
| 1233 | *start = '\0'; |
| 1234 | } |
| 1235 | } |
| 1236 | else { |
| 1237 | return encode_chunk(start, stop, escape, map, chunk); |
| 1238 | } |
| 1239 | |
| 1240 | return start; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1244 | * Write a string in the log string |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1245 | * Take cares of quote and escape options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1246 | * |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1247 | * Return the address of the \0 character, or NULL on error |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1248 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1249 | char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node) |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1250 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1251 | if (size < 2) |
| 1252 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1253 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1254 | if (node->options & LOG_OPT_QUOTE) { |
| 1255 | *(dst++) = '"'; |
| 1256 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1257 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1258 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1259 | if (src && len) { |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1260 | if (++len > size) |
| 1261 | len = size; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1262 | if (node->options & LOG_OPT_ESC) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1263 | char *ret; |
| 1264 | |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1265 | ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1266 | if (ret == NULL || *ret != '\0') |
| 1267 | return NULL; |
| 1268 | len = ret - dst; |
| 1269 | } |
| 1270 | else { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1271 | len = strlcpy2(dst, src, len); |
| 1272 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1273 | |
| 1274 | size -= len; |
| 1275 | dst += len; |
| 1276 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1277 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 1278 | if (size < 2) |
| 1279 | return NULL; |
| 1280 | *(dst++) = '-'; |
| 1281 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1282 | |
| 1283 | if (node->options & LOG_OPT_QUOTE) { |
| 1284 | if (size < 2) |
| 1285 | return NULL; |
| 1286 | *(dst++) = '"'; |
| 1287 | } |
| 1288 | |
| 1289 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1290 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1293 | static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node) |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1294 | { |
| 1295 | return lf_text_len(dst, src, size, size, node); |
| 1296 | } |
| 1297 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1298 | /* |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1299 | * Write a IP address to the log string |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1300 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 1301 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1302 | char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node) |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1303 | { |
| 1304 | char *ret = dst; |
| 1305 | int iret; |
| 1306 | char pn[INET6_ADDRSTRLEN]; |
| 1307 | |
| 1308 | if (node->options & LOG_OPT_HEXA) { |
Radek Zajic | 594c456 | 2019-03-22 10:21:54 +0000 | [diff] [blame] | 1309 | unsigned char *addr = NULL; |
| 1310 | switch (sockaddr->sa_family) { |
| 1311 | case AF_INET: |
| 1312 | addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 1313 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 1314 | break; |
| 1315 | case AF_INET6: |
| 1316 | addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr; |
| 1317 | iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", |
| 1318 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], |
| 1319 | addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]); |
| 1320 | break; |
| 1321 | default: |
| 1322 | return NULL; |
| 1323 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1324 | if (iret < 0 || iret > size) |
| 1325 | return NULL; |
| 1326 | ret += iret; |
| 1327 | } else { |
| 1328 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 1329 | ret = lf_text(dst, pn, size, node); |
| 1330 | if (ret == NULL) |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | return ret; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Write a port to the log |
| 1338 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 1339 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1340 | char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node) |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1341 | { |
| 1342 | char *ret = dst; |
| 1343 | int iret; |
| 1344 | |
| 1345 | if (node->options & LOG_OPT_HEXA) { |
| 1346 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 1347 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 1348 | if (iret < 0 || iret > size) |
| 1349 | return NULL; |
| 1350 | ret += iret; |
| 1351 | } else { |
| 1352 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 1353 | if (ret == NULL) |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1359 | /* Re-generate time-based part of the syslog header in RFC3164 format at |
| 1360 | * the beginning of logheader once a second and return the pointer to the |
| 1361 | * first character after it. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1362 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1363 | static char *update_log_hdr(const time_t time) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1364 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1365 | static THREAD_LOCAL long tvsec; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1366 | static THREAD_LOCAL struct buffer host = { }; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1367 | static THREAD_LOCAL int sep = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1368 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1369 | if (unlikely(time != tvsec || logheader_end == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1370 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1371 | struct tm tm; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1372 | int hdr_len; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1373 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1374 | tvsec = time; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1375 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1376 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1377 | if (unlikely(global.log_send_hostname != host.area)) { |
| 1378 | host.area = global.log_send_hostname; |
| 1379 | host.data = host.area ? strlen(host.area) : 0; |
| 1380 | sep = host.data ? 1 : 0; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1383 | hdr_len = snprintf(logheader, global.max_syslog_len, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1384 | "<<<<>%s %2d %02d:%02d:%02d %.*s%*s", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1385 | monthname[tm.tm_mon], |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1386 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1387 | (int)host.data, host.area, sep, ""); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1388 | /* WARNING: depending upon implementations, snprintf may return |
| 1389 | * either -1 or the number of bytes that would be needed to store |
| 1390 | * the total message. In both cases, we must adjust it. |
| 1391 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1392 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 1393 | hdr_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1394 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1395 | logheader_end = logheader + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1398 | logheader_end[0] = 0; // ensure we get rid of any previous attempt |
Willy Tarreau | 094af4e | 2015-01-07 15:03:42 +0100 | [diff] [blame] | 1399 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1400 | return logheader_end; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1401 | } |
| 1402 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1403 | /* Re-generate time-based part of the syslog header in RFC5424 format at |
| 1404 | * the beginning of logheader_rfc5424 once a second and return the pointer |
| 1405 | * to the first character after it. |
| 1406 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1407 | static char *update_log_hdr_rfc5424(const time_t time) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1408 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1409 | static THREAD_LOCAL long tvsec; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1410 | const char *gmt_offset; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1411 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1412 | if (unlikely(time != tvsec || logheader_rfc5424_end == NULL)) { |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1413 | /* this string is rebuild only once a second */ |
| 1414 | struct tm tm; |
| 1415 | int hdr_len; |
| 1416 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1417 | tvsec = time; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1418 | get_localtime(tvsec, &tm); |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 1419 | gmt_offset = get_gmt_offset(time, &tm); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1420 | |
| 1421 | hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 1422 | "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ", |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1423 | tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 1424 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1425 | gmt_offset, gmt_offset+3, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1426 | global.log_send_hostname ? global.log_send_hostname : hostname); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1427 | /* WARNING: depending upon implementations, snprintf may return |
| 1428 | * either -1 or the number of bytes that would be needed to store |
| 1429 | * the total message. In both cases, we must adjust it. |
| 1430 | */ |
| 1431 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 1432 | hdr_len = global.max_syslog_len; |
| 1433 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1434 | logheader_rfc5424_end = logheader_rfc5424 + hdr_len; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1437 | logheader_rfc5424_end[0] = 0; // ensure we get rid of any previous attempt |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1438 | |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1439 | return logheader_rfc5424_end; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1442 | /* |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1443 | * This function sends the syslog message using a printf format string. It |
| 1444 | * expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1445 | */ |
| 1446 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 1447 | { |
| 1448 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1449 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1450 | |
Willy Tarreau | 8c97ab5 | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 1451 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1452 | return; |
| 1453 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1454 | va_start(argp, format); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1455 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1456 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 1457 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1458 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1459 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1460 | __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1464 | * This function sends a syslog message to <logsrv>. |
| 1465 | * <pid_str> is the string to be used for the PID of the caller, <pid_size> is length. |
| 1466 | * Same thing for <sd> and <sd_size> which are used for the structured-data part |
| 1467 | * in RFC5424 formatted syslog messages, and <tag_str> and <tag_size> the syslog tag. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1468 | * It overrides the last byte of the message vector with an LF character. |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1469 | * Does not return any error, |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1470 | */ |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1471 | static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_str, size_t pid_size, |
| 1472 | int level, char *message, size_t size, char *sd, size_t sd_size, |
| 1473 | char *tag_str, size_t tag_size) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1474 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1475 | static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { }; |
| 1476 | static THREAD_LOCAL struct msghdr msghdr = { |
| 1477 | //.msg_iov = iovec, |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1478 | .msg_iovlen = NB_MSG_IOVEC_ELEMENTS |
| 1479 | }; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1480 | static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 1481 | static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */ |
| 1482 | static THREAD_LOCAL char *dataptr = NULL; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1483 | time_t time = date.tv_sec; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1484 | char *hdr, *hdr_ptr; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1485 | size_t hdr_size; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1486 | struct buffer *tag = &global.log_tag; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1487 | int fac_level; |
| 1488 | int *plogfd; |
| 1489 | char *pid_sep1 = "", *pid_sep2 = ""; |
| 1490 | char logheader_short[3]; |
| 1491 | int sent; |
| 1492 | int maxlen; |
| 1493 | int hdr_max = 0; |
| 1494 | int tag_max = 0; |
| 1495 | int pid_sep1_max = 0; |
| 1496 | int pid_sep2_max = 0; |
| 1497 | int sd_max = 0; |
| 1498 | int max = 0; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1499 | |
| 1500 | msghdr.msg_iov = iovec; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1501 | |
| 1502 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1503 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1504 | if (logsrv->addr.ss_family == AF_UNSPEC) { |
| 1505 | /* the socket's address is a file descriptor */ |
| 1506 | plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr; |
| 1507 | if (unlikely(!((struct sockaddr_in *)&logsrv->addr)->sin_port)) { |
| 1508 | /* FD not yet initialized to non-blocking mode. |
| 1509 | * DON'T DO IT ON A TERMINAL! |
| 1510 | */ |
| 1511 | if (!isatty(*plogfd)) |
| 1512 | fcntl(*plogfd, F_SETFL, O_NONBLOCK); |
| 1513 | ((struct sockaddr_in *)&logsrv->addr)->sin_port = 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1514 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1515 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1516 | else if (logsrv->addr.ss_family == AF_UNIX) |
| 1517 | plogfd = &logfdunix; |
| 1518 | else |
| 1519 | plogfd = &logfdinet; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1520 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1521 | if (unlikely(*plogfd < 0)) { |
| 1522 | /* socket not successfully initialized yet */ |
| 1523 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, |
| 1524 | (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) { |
| 1525 | static char once; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1526 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1527 | if (!once) { |
| 1528 | once = 1; /* note: no need for atomic ops here */ |
| 1529 | ha_alert("socket() failed in logger #%d: %s (errno=%d)\n", |
| 1530 | nblogger, strerror(errno), errno); |
| 1531 | } |
| 1532 | return; |
| 1533 | } else { |
| 1534 | /* we don't want to receive anything on this socket */ |
| 1535 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 1536 | /* does nothing under Linux, maybe needed for others */ |
| 1537 | shutdown(*plogfd, SHUT_RD); |
| 1538 | fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC); |
| 1539 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1542 | switch (logsrv->format) { |
| 1543 | case LOG_FORMAT_RFC3164: |
| 1544 | hdr = logheader; |
| 1545 | hdr_ptr = update_log_hdr(time); |
| 1546 | break; |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1547 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1548 | case LOG_FORMAT_RFC5424: |
| 1549 | hdr = logheader_rfc5424; |
| 1550 | hdr_ptr = update_log_hdr_rfc5424(time); |
| 1551 | sd_max = sd_size; /* the SD part allowed only in RFC5424 */ |
| 1552 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1553 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1554 | case LOG_FORMAT_SHORT: |
| 1555 | /* all fields are known, skip the header generation */ |
| 1556 | hdr = logheader_short; |
| 1557 | hdr[0] = '<'; |
| 1558 | hdr[1] = '0' + MAX(level, logsrv->minlvl); |
| 1559 | hdr[2] = '>'; |
| 1560 | hdr_ptr = hdr; |
| 1561 | hdr_max = 3; |
| 1562 | maxlen = logsrv->maxlen - hdr_max; |
| 1563 | max = MIN(size, maxlen) - 1; |
| 1564 | goto send; |
Willy Tarreau | 204e3f1 | 2018-12-15 15:48:48 +0100 | [diff] [blame] | 1565 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1566 | case LOG_FORMAT_RAW: |
| 1567 | /* all fields are known, skip the header generation */ |
| 1568 | hdr_ptr = hdr = ""; |
| 1569 | hdr_max = 0; |
| 1570 | maxlen = logsrv->maxlen; |
| 1571 | max = MIN(size, maxlen) - 1; |
| 1572 | goto send; |
Willy Tarreau | c98aebc | 2018-03-20 11:17:29 +0100 | [diff] [blame] | 1573 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1574 | default: |
| 1575 | return; /* must never happen */ |
| 1576 | } |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1577 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1578 | hdr_size = hdr_ptr - hdr; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1579 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1580 | /* For each target, we may have a different facility. |
| 1581 | * We can also have a different log level for each message. |
| 1582 | * This induces variations in the message header length. |
| 1583 | * Since we don't want to recompute it each time, nor copy it every |
| 1584 | * time, we only change the facility in the pre-computed header, |
| 1585 | * and we change the pointer to the header accordingly. |
| 1586 | */ |
| 1587 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
| 1588 | hdr_ptr = hdr + 3; /* last digit of the log level */ |
| 1589 | do { |
| 1590 | *hdr_ptr = '0' + fac_level % 10; |
| 1591 | fac_level /= 10; |
| 1592 | hdr_ptr--; |
| 1593 | } while (fac_level && hdr_ptr > hdr); |
| 1594 | *hdr_ptr = '<'; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1595 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1596 | hdr_max = hdr_size - (hdr_ptr - hdr); |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 1597 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1598 | /* time-based header */ |
| 1599 | if (unlikely(hdr_size >= logsrv->maxlen)) { |
| 1600 | hdr_max = MIN(hdr_max, logsrv->maxlen) - 1; |
| 1601 | sd_max = 0; |
| 1602 | goto send; |
| 1603 | } |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 1604 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1605 | maxlen = logsrv->maxlen - hdr_max; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1606 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1607 | /* tag */ |
| 1608 | tag_max = tag->data; |
| 1609 | if (unlikely(tag_max >= maxlen)) { |
| 1610 | tag_max = maxlen - 1; |
| 1611 | sd_max = 0; |
| 1612 | goto send; |
| 1613 | } |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1614 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1615 | maxlen -= tag_max; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1616 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1617 | /* first pid separator */ |
| 1618 | pid_sep1_max = log_formats[logsrv->format].pid.sep1.data; |
| 1619 | if (unlikely(pid_sep1_max >= maxlen)) { |
| 1620 | pid_sep1_max = maxlen - 1; |
| 1621 | sd_max = 0; |
| 1622 | goto send; |
| 1623 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1624 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1625 | pid_sep1 = log_formats[logsrv->format].pid.sep1.area; |
| 1626 | maxlen -= pid_sep1_max; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1627 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1628 | /* pid */ |
| 1629 | if (unlikely(pid_size >= maxlen)) { |
| 1630 | pid_size = maxlen - 1; |
| 1631 | sd_max = 0; |
| 1632 | goto send; |
| 1633 | } |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1634 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1635 | maxlen -= pid_size; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1636 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1637 | /* second pid separator */ |
| 1638 | pid_sep2_max = log_formats[logsrv->format].pid.sep2.data; |
| 1639 | if (unlikely(pid_sep2_max >= maxlen)) { |
| 1640 | pid_sep2_max = maxlen - 1; |
| 1641 | sd_max = 0; |
| 1642 | goto send; |
| 1643 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1644 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1645 | pid_sep2 = log_formats[logsrv->format].pid.sep2.area; |
| 1646 | maxlen -= pid_sep2_max; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1647 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1648 | /* structured-data */ |
| 1649 | if (sd_max >= maxlen) { |
| 1650 | sd_max = maxlen - 1; |
| 1651 | goto send; |
| 1652 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1653 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1654 | max = MIN(size, maxlen - sd_max) - 1; |
| 1655 | send: |
| 1656 | iovec[0].iov_base = hdr_ptr; |
| 1657 | iovec[0].iov_len = hdr_max; |
| 1658 | iovec[1].iov_base = tag_str; |
| 1659 | iovec[1].iov_len = tag_size; |
| 1660 | iovec[2].iov_base = pid_sep1; |
| 1661 | iovec[2].iov_len = pid_sep1_max; |
| 1662 | iovec[3].iov_base = pid_str; |
| 1663 | iovec[3].iov_len = pid_size; |
| 1664 | iovec[4].iov_base = pid_sep2; |
| 1665 | iovec[4].iov_len = pid_sep2_max; |
| 1666 | iovec[5].iov_base = sd; |
| 1667 | iovec[5].iov_len = sd_max; |
| 1668 | iovec[6].iov_base = dataptr; |
| 1669 | iovec[6].iov_len = max; |
| 1670 | iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1671 | iovec[7].iov_len = 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1672 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1673 | if (logsrv->addr.ss_family == AF_UNSPEC) { |
| 1674 | /* the target is a direct file descriptor */ |
| 1675 | sent = writev(*plogfd, iovec, 8); |
| 1676 | } |
| 1677 | else { |
| 1678 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1679 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1680 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1681 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 1682 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1683 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1684 | if (sent < 0) { |
| 1685 | static char once; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1686 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1687 | if (errno == EAGAIN) |
| 1688 | _HA_ATOMIC_ADD(&dropped_logs, 1); |
| 1689 | else if (!once) { |
| 1690 | once = 1; /* note: no need for atomic ops here */ |
| 1691 | ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n", |
| 1692 | nblogger, strerror(errno), errno); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1693 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1694 | } |
| 1695 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1696 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1697 | /* |
| 1698 | * This function sends a syslog message. |
| 1699 | * It doesn't care about errors nor does it report them. |
| 1700 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 1701 | * in RFC5424 formatted syslog messages. |
| 1702 | */ |
| 1703 | void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size) |
| 1704 | { |
| 1705 | struct list *logsrvs = NULL; |
| 1706 | struct logsrv *logsrv; |
| 1707 | int nblogger; |
| 1708 | static THREAD_LOCAL int curr_pid; |
| 1709 | static THREAD_LOCAL char pidstr[100]; |
| 1710 | static THREAD_LOCAL struct buffer pid; |
| 1711 | struct buffer *tag = &global.log_tag; |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1712 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1713 | if (p == NULL) { |
| 1714 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 1715 | logsrvs = &global.logsrvs; |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 1716 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1717 | } else { |
| 1718 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 1719 | logsrvs = &p->logsrvs; |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 1720 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1721 | if (p->log_tag.area) { |
| 1722 | tag = &p->log_tag; |
| 1723 | } |
| 1724 | } |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1725 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1726 | if (!logsrvs) |
| 1727 | return; |
Willy Tarreau | c98aebc | 2018-03-20 11:17:29 +0100 | [diff] [blame] | 1728 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1729 | if (unlikely(curr_pid != getpid())) { |
| 1730 | curr_pid = getpid(); |
| 1731 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 1732 | chunk_initstr(&pid, pidstr); |
| 1733 | } |
| 1734 | |
| 1735 | /* Send log messages to syslog server. */ |
| 1736 | nblogger = 0; |
| 1737 | list_for_each_entry(logsrv, logsrvs, list) { |
Frédéric Lécaille | d803e47 | 2019-04-25 07:42:09 +0200 | [diff] [blame] | 1738 | static THREAD_LOCAL int in_range = 1; |
| 1739 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1740 | /* we can filter the level of the messages that are sent to each logger */ |
| 1741 | if (level > logsrv->level) |
| 1742 | continue; |
| 1743 | |
Frédéric Lécaille | d803e47 | 2019-04-25 07:42:09 +0200 | [diff] [blame] | 1744 | if (logsrv->lb.smp_rgs) { |
| 1745 | struct smp_log_range *curr_rg; |
| 1746 | |
| 1747 | HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock); |
| 1748 | curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg]; |
| 1749 | in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx); |
| 1750 | if (in_range) { |
| 1751 | /* Let's consume this range. */ |
| 1752 | curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz; |
| 1753 | if (!curr_rg->curr_idx) { |
| 1754 | /* If consumed, let's select the next range. */ |
| 1755 | logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz; |
| 1756 | } |
| 1757 | } |
| 1758 | logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz; |
| 1759 | HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock); |
| 1760 | } |
| 1761 | if (in_range) |
| 1762 | __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level, |
| 1763 | message, size, sd, sd_size, tag->area, tag->data); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1767 | extern fd_set hdr_encode_map[]; |
| 1768 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1769 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1770 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1771 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1772 | const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1773 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1774 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1775 | Set-cookie Updated, unknown, unknown */ |
| 1776 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1777 | /* |
| 1778 | * try to write a character if there is enough space, or goto out |
| 1779 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1780 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1781 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1782 | *(tmplog++) = (x); \ |
| 1783 | } else { \ |
| 1784 | goto out; \ |
| 1785 | } \ |
| 1786 | } while(0) |
| 1787 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1788 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1789 | /* Initializes some log data at boot */ |
| 1790 | static void init_log() |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1791 | { |
| 1792 | char *tmp; |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1793 | int i; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* Initialize the escape map for the RFC5424 structured-data : '"\]' |
| 1796 | * inside PARAM-VALUE should be escaped with '\' as prefix. |
| 1797 | * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more |
| 1798 | * details. |
| 1799 | */ |
| 1800 | memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map)); |
| 1801 | |
| 1802 | tmp = "\"\\]"; |
| 1803 | while (*tmp) { |
| 1804 | FD_SET(*tmp, rfc5424_escape_map); |
| 1805 | tmp++; |
| 1806 | } |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1807 | |
| 1808 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 1809 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 1810 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 1811 | * printable characters above. |
| 1812 | */ |
| 1813 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 1814 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 1815 | for (i = 0; i < 32; i++) { |
| 1816 | FD_SET(i, hdr_encode_map); |
| 1817 | FD_SET(i, url_encode_map); |
| 1818 | } |
| 1819 | for (i = 127; i < 256; i++) { |
| 1820 | FD_SET(i, hdr_encode_map); |
| 1821 | FD_SET(i, url_encode_map); |
| 1822 | } |
| 1823 | |
| 1824 | tmp = "\"#{|}"; |
| 1825 | while (*tmp) { |
| 1826 | FD_SET(*tmp, hdr_encode_map); |
| 1827 | tmp++; |
| 1828 | } |
| 1829 | |
| 1830 | tmp = "\"#"; |
| 1831 | while (*tmp) { |
| 1832 | FD_SET(*tmp, url_encode_map); |
| 1833 | tmp++; |
| 1834 | } |
| 1835 | |
| 1836 | /* initialize the http header encoding map. The draft httpbis define the |
| 1837 | * header content as: |
| 1838 | * |
| 1839 | * HTTP-message = start-line |
| 1840 | * *( header-field CRLF ) |
| 1841 | * CRLF |
| 1842 | * [ message-body ] |
| 1843 | * header-field = field-name ":" OWS field-value OWS |
| 1844 | * field-value = *( field-content / obs-fold ) |
| 1845 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 1846 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 1847 | * field-vchar = VCHAR / obs-text |
| 1848 | * VCHAR = %x21-7E |
| 1849 | * obs-text = %x80-FF |
| 1850 | * |
| 1851 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 1852 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1853 | * "obs-fold" is voluntarily forgotten because haproxy remove this. |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1854 | */ |
| 1855 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 1856 | for (i = 0x00; i <= 0x08; i++) |
| 1857 | FD_SET(i, http_encode_map); |
| 1858 | for (i = 0x0a; i <= 0x1f; i++) |
| 1859 | FD_SET(i, http_encode_map); |
| 1860 | FD_SET(0x7f, http_encode_map); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1861 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1862 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1863 | INITCALL0(STG_PREPARE, init_log); |
| 1864 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1865 | static int init_log_buffers_per_thread() |
| 1866 | { |
| 1867 | return init_log_buffers(); |
| 1868 | } |
| 1869 | |
| 1870 | static void deinit_log_buffers_per_thread() |
| 1871 | { |
| 1872 | deinit_log_buffers(); |
| 1873 | } |
| 1874 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1875 | /* Initialize log buffers used for syslog messages */ |
| 1876 | int init_log_buffers() |
| 1877 | { |
| 1878 | logheader = my_realloc2(logheader, global.max_syslog_len + 1); |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1879 | logheader_end = NULL; |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1880 | logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1); |
Willy Tarreau | 55e2f5a | 2019-05-05 10:11:39 +0200 | [diff] [blame] | 1881 | logheader_rfc5424_end = NULL; |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1882 | logline = my_realloc2(logline, global.max_syslog_len + 1); |
| 1883 | logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1); |
| 1884 | if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424) |
| 1885 | return 0; |
| 1886 | return 1; |
| 1887 | } |
| 1888 | |
| 1889 | /* Deinitialize log buffers used for syslog messages */ |
| 1890 | void deinit_log_buffers() |
| 1891 | { |
Olivier Houchard | 7c49711 | 2019-03-07 14:19:24 +0100 | [diff] [blame] | 1892 | void *tmp_startup_logs; |
| 1893 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1894 | free(logheader); |
| 1895 | free(logheader_rfc5424); |
| 1896 | free(logline); |
| 1897 | free(logline_rfc5424); |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 1898 | tmp_startup_logs = _HA_ATOMIC_XCHG(&startup_logs, NULL); |
Olivier Houchard | 7c49711 | 2019-03-07 14:19:24 +0100 | [diff] [blame] | 1899 | free(tmp_startup_logs); |
| 1900 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1901 | logheader = NULL; |
| 1902 | logheader_rfc5424 = NULL; |
| 1903 | logline = NULL; |
| 1904 | logline_rfc5424 = NULL; |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 1905 | startup_logs = NULL; |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1908 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1909 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1910 | * not counting the trailing zero which is always added if the resulting size |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1911 | * is not zero. It requires a valid session and optionally a stream. If the |
| 1912 | * stream is NULL, default values will be assumed for the stream part. |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1913 | */ |
Willy Tarreau | 43c538e | 2018-09-05 14:58:15 +0200 | [diff] [blame] | 1914 | int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1915 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1916 | struct proxy *fe = sess->fe; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1917 | struct proxy *be; |
| 1918 | struct http_txn *txn; |
| 1919 | const struct strm_logs *logs; |
| 1920 | const struct connection *be_conn; |
| 1921 | unsigned int s_flags; |
| 1922 | unsigned int uniq_id; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1923 | struct buffer chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1924 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1925 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1926 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1927 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1928 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1929 | int t_request; |
| 1930 | int hdr; |
| 1931 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1932 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1933 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1934 | char *ret; |
| 1935 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1936 | struct logformat_node *tmp; |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1937 | struct timeval tv; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1938 | struct strm_logs tmp_strm_log; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1939 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1940 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1941 | |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1942 | if (likely(s)) { |
| 1943 | be = s->be; |
| 1944 | txn = s->txn; |
| 1945 | be_conn = cs_conn(objt_cs(s->si[1].end)); |
| 1946 | s_flags = s->flags; |
| 1947 | uniq_id = s->uniq_id; |
| 1948 | logs = &s->logs; |
| 1949 | } else { |
| 1950 | /* we have no stream so we first need to initialize a few |
| 1951 | * things that are needed later. We do increment the request |
| 1952 | * ID so that it's uniquely assigned to this request just as |
| 1953 | * if the request had reached the point of being processed. |
| 1954 | * A request error is reported as it's the only element we have |
| 1955 | * here and which justifies emitting such a log. |
| 1956 | */ |
| 1957 | be = fe; |
| 1958 | txn = NULL; |
| 1959 | be_conn = NULL; |
| 1960 | s_flags = SF_ERR_PRXCOND | SF_FINST_R; |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 1961 | uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1962 | |
| 1963 | /* prepare a valid log structure */ |
| 1964 | tmp_strm_log.tv_accept = sess->tv_accept; |
| 1965 | tmp_strm_log.accept_date = sess->accept_date; |
| 1966 | tmp_strm_log.t_handshake = sess->t_handshake; |
| 1967 | tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake; |
| 1968 | tv_zero(&tmp_strm_log.tv_request); |
| 1969 | tmp_strm_log.t_queue = -1; |
| 1970 | tmp_strm_log.t_connect = -1; |
| 1971 | tmp_strm_log.t_data = -1; |
| 1972 | tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now); |
| 1973 | tmp_strm_log.bytes_in = 0; |
| 1974 | tmp_strm_log.bytes_out = 0; |
| 1975 | tmp_strm_log.prx_queue_pos = 0; |
| 1976 | tmp_strm_log.srv_queue_pos = 0; |
| 1977 | |
| 1978 | logs = &tmp_strm_log; |
| 1979 | } |
| 1980 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1981 | t_request = -1; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 1982 | if (tv_isge(&logs->tv_request, &logs->tv_accept)) |
| 1983 | t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1984 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1985 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1986 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1987 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1988 | if (LIST_ISEMPTY(list_format)) |
| 1989 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1990 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1991 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1992 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 1993 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1994 | struct sample *key; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1995 | const struct buffer empty = { }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1996 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1997 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1998 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1999 | if (!last_isspace) { |
| 2000 | LOGCHAR(' '); |
| 2001 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2002 | } |
| 2003 | break; |
| 2004 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2005 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2006 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2007 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2008 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2009 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2010 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2011 | last_isspace = 0; |
| 2012 | break; |
| 2013 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2014 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 2015 | key = NULL; |
Olivier Houchard | f90db44 | 2018-12-15 14:00:06 +0100 | [diff] [blame] | 2016 | if (tmp->options & LOG_OPT_REQ_CAP && s) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 2017 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Olivier Houchard | f90db44 | 2018-12-15 14:00:06 +0100 | [diff] [blame] | 2018 | if (!key && (tmp->options & LOG_OPT_RES_CAP) && s) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 2019 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 2020 | if (tmp->options & LOG_OPT_HTTP) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2021 | ret = lf_encode_chunk(tmplog, dst + maxsize, |
| 2022 | '%', http_encode_map, key ? &key->data.u.str : &empty, tmp); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 2023 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2024 | ret = lf_text_len(tmplog, |
| 2025 | key ? key->data.u.str.area : NULL, |
| 2026 | key ? key->data.u.str.data : 0, |
| 2027 | dst + maxsize - tmplog, |
| 2028 | tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2029 | if (ret == 0) |
| 2030 | goto out; |
| 2031 | tmplog = ret; |
| 2032 | last_isspace = 0; |
| 2033 | break; |
| 2034 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2035 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2036 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2037 | if (conn) |
| 2038 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 2039 | else |
| 2040 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2041 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2042 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2043 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2044 | last_isspace = 0; |
| 2045 | break; |
| 2046 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2047 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2048 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2049 | if (conn) { |
| 2050 | if (conn->addr.from.ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2051 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2052 | } else { |
| 2053 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 2054 | dst + maxsize - tmplog, tmp); |
| 2055 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2056 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2057 | else |
| 2058 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2059 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2060 | if (ret == NULL) |
| 2061 | goto out; |
| 2062 | tmplog = ret; |
| 2063 | last_isspace = 0; |
| 2064 | break; |
| 2065 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2066 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2067 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2068 | if (conn) { |
| 2069 | conn_get_to_addr(conn); |
| 2070 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 2071 | } |
| 2072 | else |
| 2073 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2074 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2075 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2076 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2077 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2078 | last_isspace = 0; |
| 2079 | break; |
| 2080 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2081 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2082 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2083 | if (conn) { |
| 2084 | conn_get_to_addr(conn); |
| 2085 | if (conn->addr.to.ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2086 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2087 | else |
| 2088 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2089 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2090 | else |
| 2091 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2092 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2093 | if (ret == NULL) |
| 2094 | goto out; |
| 2095 | tmplog = ret; |
| 2096 | last_isspace = 0; |
| 2097 | break; |
| 2098 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2099 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 2100 | if (be_conn) |
| 2101 | ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2102 | else |
| 2103 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2104 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2105 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2106 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2107 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2108 | last_isspace = 0; |
| 2109 | break; |
| 2110 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2111 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 2112 | if (be_conn) |
| 2113 | ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2114 | else |
| 2115 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2116 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2117 | if (ret == NULL) |
| 2118 | goto out; |
| 2119 | tmplog = ret; |
| 2120 | last_isspace = 0; |
| 2121 | break; |
| 2122 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2123 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 2124 | if (be_conn) |
| 2125 | ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2126 | else |
| 2127 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2128 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2129 | if (ret == NULL) |
| 2130 | goto out; |
| 2131 | tmplog = ret; |
| 2132 | last_isspace = 0; |
| 2133 | break; |
| 2134 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2135 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 2136 | if (be_conn) |
| 2137 | ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2138 | else |
| 2139 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2140 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2141 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2142 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2143 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2144 | last_isspace = 0; |
| 2145 | break; |
| 2146 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2147 | case LOG_FMT_DATE: // %t = accept date |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2148 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 2149 | ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2150 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2151 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2152 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2153 | last_isspace = 0; |
| 2154 | break; |
| 2155 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2156 | case LOG_FMT_tr: // %tr = start of request date |
| 2157 | /* Note that the timers are valid if we get here */ |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2158 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2159 | get_localtime(tv.tv_sec, &tm); |
| 2160 | ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog); |
| 2161 | if (ret == NULL) |
| 2162 | goto out; |
| 2163 | tmplog = ret; |
| 2164 | last_isspace = 0; |
| 2165 | break; |
| 2166 | |
| 2167 | case LOG_FMT_DATEGMT: // %T = accept date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2168 | get_gmtime(logs->accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2169 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2170 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2171 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2172 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2173 | last_isspace = 0; |
| 2174 | break; |
| 2175 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2176 | case LOG_FMT_trg: // %trg = start of request date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2177 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2178 | get_gmtime(tv.tv_sec, &tm); |
| 2179 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 2180 | if (ret == NULL) |
| 2181 | goto out; |
| 2182 | tmplog = ret; |
| 2183 | last_isspace = 0; |
| 2184 | break; |
| 2185 | |
| 2186 | case LOG_FMT_DATELOCAL: // %Tl = accept date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2187 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 2188 | ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog); |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2189 | if (ret == NULL) |
| 2190 | goto out; |
| 2191 | tmplog = ret; |
| 2192 | last_isspace = 0; |
| 2193 | break; |
| 2194 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2195 | case LOG_FMT_trl: // %trl = start of request date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2196 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2197 | get_localtime(tv.tv_sec, &tm); |
| 2198 | ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog); |
| 2199 | if (ret == NULL) |
| 2200 | goto out; |
| 2201 | tmplog = ret; |
| 2202 | last_isspace = 0; |
| 2203 | break; |
| 2204 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2205 | case LOG_FMT_TS: // %Ts |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2206 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2207 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2208 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2209 | goto out; |
| 2210 | last_isspace = 0; |
| 2211 | tmplog += iret; |
| 2212 | } else { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2213 | ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2214 | if (ret == NULL) |
| 2215 | goto out; |
| 2216 | tmplog = ret; |
| 2217 | last_isspace = 0; |
| 2218 | } |
| 2219 | break; |
| 2220 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2221 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2222 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2223 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2224 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2225 | goto out; |
| 2226 | last_isspace = 0; |
| 2227 | tmplog += iret; |
| 2228 | } else { |
| 2229 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2230 | goto out; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2231 | ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000, |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2232 | tmplog, 4); |
| 2233 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2234 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2235 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2236 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2237 | } |
| 2238 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2239 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2240 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2241 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2242 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2243 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2244 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2245 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2246 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2247 | break; |
| 2248 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2249 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 2250 | src = fe->id; |
| 2251 | if (tmp->options & LOG_OPT_QUOTE) |
| 2252 | LOGCHAR('"'); |
| 2253 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 2254 | if (iret == 0) |
| 2255 | goto out; |
| 2256 | tmplog += iret; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 2257 | if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL)) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2258 | LOGCHAR('~'); |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2259 | if (tmp->options & LOG_OPT_QUOTE) |
| 2260 | LOGCHAR('"'); |
| 2261 | last_isspace = 0; |
| 2262 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2263 | #ifdef USE_OPENSSL |
| 2264 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 2265 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2266 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2267 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2268 | src = ssl_sock_get_cipher_name(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2269 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2270 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2271 | if (ret == NULL) |
| 2272 | goto out; |
| 2273 | tmplog = ret; |
| 2274 | last_isspace = 0; |
| 2275 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2276 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2277 | case LOG_FMT_SSL_VERSION: // %sslv |
| 2278 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2279 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2280 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2281 | src = ssl_sock_get_proto_version(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2282 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2283 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2284 | if (ret == NULL) |
| 2285 | goto out; |
| 2286 | tmplog = ret; |
| 2287 | last_isspace = 0; |
| 2288 | break; |
| 2289 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2290 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2291 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2292 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2293 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2294 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2295 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2296 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2297 | break; |
| 2298 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2299 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | e1809df | 2018-09-05 15:30:16 +0200 | [diff] [blame] | 2300 | switch (obj_type(s ? s->target : NULL)) { |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2301 | case OBJ_TYPE_SERVER: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2302 | src = __objt_server(s->target)->id; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2303 | break; |
| 2304 | case OBJ_TYPE_APPLET: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2305 | src = __objt_applet(s->target)->name; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2306 | break; |
| 2307 | default: |
| 2308 | src = "<NOSRV>"; |
| 2309 | break; |
| 2310 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2311 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2312 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2313 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2314 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2315 | last_isspace = 0; |
| 2316 | break; |
| 2317 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2318 | case LOG_FMT_Th: // %Th = handshake time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2319 | ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2320 | if (ret == NULL) |
| 2321 | goto out; |
| 2322 | tmplog = ret; |
| 2323 | last_isspace = 0; |
| 2324 | break; |
| 2325 | |
| 2326 | case LOG_FMT_Ti: // %Ti = HTTP idle time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2327 | ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2328 | if (ret == NULL) |
| 2329 | goto out; |
| 2330 | tmplog = ret; |
| 2331 | last_isspace = 0; |
| 2332 | break; |
| 2333 | |
| 2334 | case LOG_FMT_TR: // %TR = HTTP request time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2335 | ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1, |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2336 | tmplog, dst + maxsize - tmplog); |
| 2337 | if (ret == NULL) |
| 2338 | goto out; |
| 2339 | tmplog = ret; |
| 2340 | last_isspace = 0; |
| 2341 | break; |
| 2342 | |
| 2343 | case LOG_FMT_TQ: // %Tq = Th + Ti + TR |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2344 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2345 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2346 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2347 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2348 | last_isspace = 0; |
| 2349 | break; |
| 2350 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2351 | case LOG_FMT_TW: // %Tw |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2352 | ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2353 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2354 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2355 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2356 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2357 | last_isspace = 0; |
| 2358 | break; |
| 2359 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2360 | case LOG_FMT_TC: // %Tc |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2361 | ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2362 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2363 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2364 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2365 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2366 | last_isspace = 0; |
| 2367 | break; |
| 2368 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2369 | case LOG_FMT_Tr: // %Tr |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2370 | ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2371 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2372 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2373 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2374 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2375 | last_isspace = 0; |
| 2376 | break; |
| 2377 | |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2378 | case LOG_FMT_TD: // %Td |
Willy Tarreau | a21c0e6 | 2018-09-05 15:07:15 +0200 | [diff] [blame] | 2379 | if (be->mode == PR_MODE_HTTP) |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2380 | ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1, |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2381 | tmplog, dst + maxsize - tmplog); |
| 2382 | else |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2383 | ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1, |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2384 | tmplog, dst + maxsize - tmplog); |
| 2385 | if (ret == NULL) |
| 2386 | goto out; |
| 2387 | tmplog = ret; |
| 2388 | last_isspace = 0; |
| 2389 | break; |
| 2390 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2391 | case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti |
| 2392 | if (!(fe->to_log & LW_BYTES)) |
| 2393 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2394 | ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0), |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2395 | tmplog, dst + maxsize - tmplog); |
| 2396 | if (ret == NULL) |
| 2397 | goto out; |
| 2398 | tmplog = ret; |
| 2399 | last_isspace = 0; |
| 2400 | break; |
| 2401 | |
| 2402 | case LOG_FMT_TT: // %Tt = total time |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2403 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2404 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2405 | ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2406 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2407 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2408 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2409 | last_isspace = 0; |
| 2410 | break; |
| 2411 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2412 | case LOG_FMT_STATUS: // %ST |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2413 | ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2414 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2415 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2416 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2417 | last_isspace = 0; |
| 2418 | break; |
| 2419 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2420 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2421 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2422 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2423 | ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2424 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2425 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2426 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2427 | last_isspace = 0; |
| 2428 | break; |
| 2429 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2430 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2431 | ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2432 | if (ret == NULL) |
| 2433 | goto out; |
| 2434 | tmplog = ret; |
| 2435 | last_isspace = 0; |
| 2436 | break; |
| 2437 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2438 | case LOG_FMT_CCLIENT: // %CC |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2439 | src = txn ? txn->cli_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2440 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2441 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2442 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2443 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2444 | last_isspace = 0; |
| 2445 | break; |
| 2446 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2447 | case LOG_FMT_CSERVER: // %CS |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2448 | src = txn ? txn->srv_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2449 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2450 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2451 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2452 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2453 | last_isspace = 0; |
| 2454 | break; |
| 2455 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2456 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2457 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2458 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 2459 | *tmplog = '\0'; |
| 2460 | last_isspace = 0; |
| 2461 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2462 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2463 | case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP) |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2464 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2465 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2466 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 2467 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2468 | last_isspace = 0; |
| 2469 | break; |
| 2470 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2471 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2472 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2473 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2474 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2475 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2476 | last_isspace = 0; |
| 2477 | break; |
| 2478 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2479 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2480 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2481 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2482 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2483 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2484 | last_isspace = 0; |
| 2485 | break; |
| 2486 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2487 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2488 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2489 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2490 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2491 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2492 | last_isspace = 0; |
| 2493 | break; |
| 2494 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2495 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | e1809df | 2018-09-05 15:30:16 +0200 | [diff] [blame] | 2496 | ret = ultoa_o(objt_server(s ? s->target : NULL) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2497 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2498 | 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2499 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2500 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2501 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2502 | last_isspace = 0; |
| 2503 | break; |
| 2504 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2505 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2506 | if (s_flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2507 | LOGCHAR('+'); |
Willy Tarreau | abd71a5 | 2018-09-04 19:21:44 +0200 | [diff] [blame] | 2508 | ret = ltoa_o((s && s->si[1].conn_retries > 0) ? |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2509 | (be->conn_retries - s->si[1].conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2510 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2511 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2512 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2513 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2514 | last_isspace = 0; |
| 2515 | break; |
| 2516 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2517 | case LOG_FMT_SRVQUEUE: // %sq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2518 | ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2519 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2520 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2521 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2522 | last_isspace = 0; |
| 2523 | break; |
| 2524 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2525 | case LOG_FMT_BCKQUEUE: // %bq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2526 | ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2527 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2528 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2529 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2530 | last_isspace = 0; |
| 2531 | break; |
| 2532 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2533 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2534 | /* request header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2535 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2536 | if (tmp->options & LOG_OPT_QUOTE) |
| 2537 | LOGCHAR('"'); |
| 2538 | LOGCHAR('{'); |
| 2539 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2540 | if (hdr) |
| 2541 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2542 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2543 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2544 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2545 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2546 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2547 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2548 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2549 | } |
| 2550 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2551 | if (tmp->options & LOG_OPT_QUOTE) |
| 2552 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2553 | last_isspace = 0; |
| 2554 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2555 | break; |
| 2556 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2557 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2558 | /* request header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2559 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2560 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2561 | if (hdr > 0) |
| 2562 | LOGCHAR(' '); |
| 2563 | if (tmp->options & LOG_OPT_QUOTE) |
| 2564 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2565 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2566 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2567 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2568 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2569 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2570 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2571 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2572 | LOGCHAR('-'); |
| 2573 | if (tmp->options & LOG_OPT_QUOTE) |
| 2574 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2575 | last_isspace = 0; |
| 2576 | } |
| 2577 | } |
| 2578 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2579 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2580 | |
| 2581 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2582 | /* response header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2583 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2584 | if (tmp->options & LOG_OPT_QUOTE) |
| 2585 | LOGCHAR('"'); |
| 2586 | LOGCHAR('{'); |
| 2587 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2588 | if (hdr) |
| 2589 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2590 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2591 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2592 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2593 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2594 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2595 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2596 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2597 | } |
| 2598 | LOGCHAR('}'); |
| 2599 | last_isspace = 0; |
| 2600 | if (tmp->options & LOG_OPT_QUOTE) |
| 2601 | LOGCHAR('"'); |
| 2602 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2603 | break; |
| 2604 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2605 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2606 | /* response header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2607 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2608 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2609 | if (hdr > 0) |
| 2610 | LOGCHAR(' '); |
| 2611 | if (tmp->options & LOG_OPT_QUOTE) |
| 2612 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2613 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2614 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2615 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2616 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2617 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2618 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2619 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2620 | LOGCHAR('-'); |
| 2621 | if (tmp->options & LOG_OPT_QUOTE) |
| 2622 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2623 | last_isspace = 0; |
| 2624 | } |
| 2625 | } |
| 2626 | break; |
| 2627 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2628 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2629 | /* Request */ |
| 2630 | if (tmp->options & LOG_OPT_QUOTE) |
| 2631 | LOGCHAR('"'); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2632 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2633 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2634 | '#', url_encode_map, uri, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2635 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2636 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2637 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2638 | if (tmp->options & LOG_OPT_QUOTE) |
| 2639 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2640 | last_isspace = 0; |
| 2641 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2642 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2643 | case LOG_FMT_HTTP_PATH: // %HP |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2644 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2645 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2646 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2647 | LOGCHAR('"'); |
| 2648 | |
| 2649 | end = uri + strlen(uri); |
| 2650 | // look for the first whitespace character |
| 2651 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2652 | uri++; |
| 2653 | |
| 2654 | // keep advancing past multiple spaces |
| 2655 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2656 | uri++; nspaces++; |
| 2657 | } |
| 2658 | |
| 2659 | // look for first space or question mark after url |
| 2660 | spc = uri; |
| 2661 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 2662 | spc++; |
| 2663 | |
Nenad Merdanovic | 54e439f | 2016-04-26 01:39:02 +0200 | [diff] [blame] | 2664 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2665 | chunk.area = "<BADREQ>"; |
| 2666 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2667 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2668 | chunk.area = uri; |
| 2669 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2672 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2673 | if (ret == NULL || *ret != '\0') |
| 2674 | goto out; |
| 2675 | |
| 2676 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2677 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2678 | LOGCHAR('"'); |
| 2679 | |
| 2680 | last_isspace = 0; |
| 2681 | break; |
| 2682 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2683 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 2684 | if (tmp->options & LOG_OPT_QUOTE) |
| 2685 | LOGCHAR('"'); |
| 2686 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2687 | if (!txn || !txn->uri) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2688 | chunk.area = "<BADREQ>"; |
| 2689 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2690 | } else { |
| 2691 | uri = txn->uri; |
| 2692 | end = uri + strlen(uri); |
| 2693 | // look for the first question mark |
| 2694 | while (uri < end && *uri != '?') |
| 2695 | uri++; |
| 2696 | |
| 2697 | qmark = uri; |
| 2698 | // look for first space or question mark after url |
| 2699 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2700 | uri++; |
| 2701 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2702 | chunk.area = qmark; |
| 2703 | chunk.data = uri - qmark; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2706 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2707 | if (ret == NULL || *ret != '\0') |
| 2708 | goto out; |
| 2709 | |
| 2710 | tmplog = ret; |
| 2711 | if (tmp->options & LOG_OPT_QUOTE) |
| 2712 | LOGCHAR('"'); |
| 2713 | |
| 2714 | last_isspace = 0; |
| 2715 | break; |
| 2716 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2717 | case LOG_FMT_HTTP_URI: // %HU |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2718 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2719 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2720 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2721 | LOGCHAR('"'); |
| 2722 | |
| 2723 | end = uri + strlen(uri); |
| 2724 | // look for the first whitespace character |
| 2725 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2726 | uri++; |
| 2727 | |
| 2728 | // keep advancing past multiple spaces |
| 2729 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2730 | uri++; nspaces++; |
| 2731 | } |
| 2732 | |
| 2733 | // look for first space after url |
| 2734 | spc = uri; |
| 2735 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2736 | spc++; |
| 2737 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2738 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2739 | chunk.area = "<BADREQ>"; |
| 2740 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2741 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2742 | chunk.area = uri; |
| 2743 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2746 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2747 | if (ret == NULL || *ret != '\0') |
| 2748 | goto out; |
| 2749 | |
| 2750 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2751 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2752 | LOGCHAR('"'); |
| 2753 | |
| 2754 | last_isspace = 0; |
| 2755 | break; |
| 2756 | |
| 2757 | case LOG_FMT_HTTP_METHOD: // %HM |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2758 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2759 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2760 | LOGCHAR('"'); |
| 2761 | |
| 2762 | end = uri + strlen(uri); |
| 2763 | // look for the first whitespace character |
| 2764 | spc = uri; |
| 2765 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2766 | spc++; |
| 2767 | |
| 2768 | if (spc == end) { // odd case, we have txn->uri, but we only got a verb |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2769 | chunk.area = "<BADREQ>"; |
| 2770 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2771 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2772 | chunk.area = uri; |
| 2773 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2776 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2777 | if (ret == NULL || *ret != '\0') |
| 2778 | goto out; |
| 2779 | |
| 2780 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2781 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2782 | LOGCHAR('"'); |
| 2783 | |
| 2784 | last_isspace = 0; |
| 2785 | break; |
| 2786 | |
| 2787 | case LOG_FMT_HTTP_VERSION: // %HV |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2788 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2789 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2790 | LOGCHAR('"'); |
| 2791 | |
| 2792 | end = uri + strlen(uri); |
| 2793 | // look for the first whitespace character |
| 2794 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2795 | uri++; |
| 2796 | |
| 2797 | // keep advancing past multiple spaces |
| 2798 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2799 | uri++; nspaces++; |
| 2800 | } |
| 2801 | |
| 2802 | // look for the next whitespace character |
| 2803 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2804 | uri++; |
| 2805 | |
| 2806 | // keep advancing past multiple spaces |
| 2807 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 2808 | uri++; |
| 2809 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2810 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2811 | chunk.area = "<BADREQ>"; |
| 2812 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2813 | } else if (uri == end) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2814 | chunk.area = "HTTP/0.9"; |
| 2815 | chunk.data = strlen("HTTP/0.9"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2816 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2817 | chunk.area = uri; |
| 2818 | chunk.data = end - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2821 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2822 | if (ret == NULL || *ret != '\0') |
| 2823 | goto out; |
| 2824 | |
| 2825 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2826 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2827 | LOGCHAR('"'); |
| 2828 | |
| 2829 | last_isspace = 0; |
| 2830 | break; |
| 2831 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2832 | case LOG_FMT_COUNTER: // %rt |
| 2833 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2834 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2835 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2836 | goto out; |
| 2837 | last_isspace = 0; |
| 2838 | tmplog += iret; |
| 2839 | } else { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2840 | ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2841 | if (ret == NULL) |
| 2842 | goto out; |
| 2843 | tmplog = ret; |
| 2844 | last_isspace = 0; |
| 2845 | } |
| 2846 | break; |
| 2847 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2848 | case LOG_FMT_LOGCNT: // %lc |
| 2849 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2850 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2851 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2852 | goto out; |
| 2853 | last_isspace = 0; |
| 2854 | tmplog += iret; |
| 2855 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2856 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2857 | if (ret == NULL) |
| 2858 | goto out; |
| 2859 | tmplog = ret; |
| 2860 | last_isspace = 0; |
| 2861 | } |
| 2862 | break; |
| 2863 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2864 | case LOG_FMT_HOSTNAME: // %H |
| 2865 | src = hostname; |
| 2866 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2867 | if (ret == NULL) |
| 2868 | goto out; |
| 2869 | tmplog = ret; |
| 2870 | last_isspace = 0; |
| 2871 | break; |
| 2872 | |
| 2873 | case LOG_FMT_PID: // %pid |
| 2874 | if (tmp->options & LOG_OPT_HEXA) { |
| 2875 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 2876 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2877 | goto out; |
| 2878 | last_isspace = 0; |
| 2879 | tmplog += iret; |
| 2880 | } else { |
| 2881 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 2882 | if (ret == NULL) |
| 2883 | goto out; |
| 2884 | tmplog = ret; |
| 2885 | last_isspace = 0; |
| 2886 | } |
| 2887 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2888 | |
| 2889 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2890 | ret = NULL; |
Willy Tarreau | 02fdf4f | 2018-09-05 15:49:01 +0200 | [diff] [blame] | 2891 | src = s ? s->unique_id : NULL; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 2892 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2893 | if (ret == NULL) |
| 2894 | goto out; |
| 2895 | tmplog = ret; |
| 2896 | last_isspace = 0; |
| 2897 | break; |
| 2898 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2903 | /* *tmplog is a unused character */ |
| 2904 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 2905 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2906 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2907 | } |
| 2908 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2909 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2910 | * send a log for the stream when we have enough info about it. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2911 | * Will not log if the frontend has no log defined. |
| 2912 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2913 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2914 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2915 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2916 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2917 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2918 | |
| 2919 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2920 | err = (s->flags & SF_REDISP) || |
| 2921 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 2922 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2923 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2924 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2926 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2927 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2929 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2930 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2931 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2932 | if (s->logs.level) { /* loglevel was overridden */ |
| 2933 | if (s->logs.level == -1) { |
| 2934 | s->logs.logwait = 0; /* logs disabled */ |
| 2935 | return; |
| 2936 | } |
| 2937 | level = s->logs.level - 1; |
| 2938 | } |
| 2939 | else { |
| 2940 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2941 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2942 | level = LOG_ERR; |
| 2943 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2944 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2945 | /* if unique-id was not generated */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2946 | if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2947 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2948 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2951 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2952 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 2953 | &sess->fe->logformat_sd); |
| 2954 | } |
| 2955 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 2956 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2957 | if (size > 0) { |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 2958 | _HA_ATOMIC_ADD(&sess->fe->log_count, 1); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2959 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2960 | s->logs.logwait = 0; |
| 2961 | } |
| 2962 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2963 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2964 | /* |
| 2965 | * send a minimalist log for the session. Will not log if the frontend has no |
| 2966 | * log defined. It is assumed that this is only used to report anomalies that |
| 2967 | * cannot lead to the creation of a regular stream. Because of this the log |
| 2968 | * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting |
| 2969 | * in the frontend. The caller must simply know that it should not call this |
Willy Tarreau | 9fa267d | 2018-10-05 10:22:27 +0200 | [diff] [blame] | 2970 | * function to report unimportant events. It is safe to call this function with |
| 2971 | * sess==NULL (will not do anything). |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2972 | */ |
| 2973 | void sess_log(struct session *sess) |
| 2974 | { |
| 2975 | int size, level; |
| 2976 | int sd_size = 0; |
| 2977 | |
Willy Tarreau | 9fa267d | 2018-10-05 10:22:27 +0200 | [diff] [blame] | 2978 | if (!sess) |
| 2979 | return; |
| 2980 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2981 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
| 2982 | return; |
| 2983 | |
| 2984 | level = LOG_INFO; |
| 2985 | if (sess->fe->options2 & PR_O2_LOGERRORS) |
| 2986 | level = LOG_ERR; |
| 2987 | |
| 2988 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2989 | sd_size = sess_build_logline(sess, NULL, |
| 2990 | logline_rfc5424, global.max_syslog_len, |
| 2991 | &sess->fe->logformat_sd); |
| 2992 | } |
| 2993 | |
| 2994 | size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat); |
| 2995 | if (size > 0) { |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 2996 | _HA_ATOMIC_ADD(&sess->fe->log_count, 1); |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2997 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
| 2998 | } |
| 2999 | } |
| 3000 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 3001 | static int cli_io_handler_show_startup_logs(struct appctx *appctx) |
| 3002 | { |
| 3003 | struct stream_interface *si = appctx->owner; |
| 3004 | const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n"); |
| 3005 | |
| 3006 | if (ci_putstr(si_ic(si), msg) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3007 | si_rx_room_blk(si); |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 3008 | return 0; |
| 3009 | } |
| 3010 | return 1; |
| 3011 | } |
| 3012 | |
| 3013 | /* register cli keywords */ |
| 3014 | static struct cli_kw_list cli_kws = {{ },{ |
| 3015 | { { "show", "startup-logs", NULL }, |
| 3016 | "show startup-logs : report logs emitted during HAProxy startup", |
| 3017 | NULL, cli_io_handler_show_startup_logs }, |
| 3018 | {{},} |
| 3019 | }}; |
| 3020 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3021 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 3022 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 3023 | REGISTER_PER_THREAD_INIT(init_log_buffers_per_thread); |
| 3024 | REGISTER_PER_THREAD_DEINIT(deinit_log_buffers_per_thread); |
| 3025 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3026 | /* |
| 3027 | * Local variables: |
| 3028 | * c-indent-level: 8 |
| 3029 | * c-basic-offset: 8 |
| 3030 | * End: |
| 3031 | */ |