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