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 | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/applet-t.h> |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 29 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/fd.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 31 | #include <haproxy/frontend.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 33 | #include <haproxy/http.h> |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 34 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 35 | #include <haproxy/log.h> |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 36 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/sample.h> |
| 38 | #include <haproxy/sink.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 39 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 40 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 41 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 42 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 43 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Emeric Brun | 45c457a | 2020-07-09 23:23:34 +0200 | [diff] [blame] | 45 | /* global recv logs counter */ |
| 46 | int cum_log_messages; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 48 | /* log forward proxy list */ |
| 49 | struct proxy *cfg_log_forward; |
| 50 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 51 | struct log_fmt_st { |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 52 | char *name; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 55 | static const struct log_fmt_st log_formats[LOG_FORMATS] = { |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 56 | [LOG_FORMAT_LOCAL] = { |
| 57 | .name = "local", |
| 58 | }, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 59 | [LOG_FORMAT_RFC3164] = { |
| 60 | .name = "rfc3164", |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 61 | }, |
| 62 | [LOG_FORMAT_RFC5424] = { |
| 63 | .name = "rfc5424", |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 64 | }, |
| 65 | [LOG_FORMAT_PRIO] = { |
| 66 | .name = "priority", |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 67 | }, |
| 68 | [LOG_FORMAT_SHORT] = { |
| 69 | .name = "short", |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 70 | }, |
| 71 | [LOG_FORMAT_TIMED] = { |
| 72 | .name = "timed", |
| 73 | }, |
| 74 | [LOG_FORMAT_ISO] = { |
| 75 | .name = "iso", |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 76 | }, |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 77 | [LOG_FORMAT_RAW] = { |
| 78 | .name = "raw", |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 79 | }, |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 82 | /* |
| 83 | * This map is used with all the FD_* macros to check whether a particular bit |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 84 | * is set or not. Each bit represents an ACSII code. ha_bit_set() sets those |
| 85 | * bytes which should be escaped. When ha_bit_test() returns non-zero, it means |
| 86 | * that the byte should be escaped. Be careful to always pass bytes from 0 to |
| 87 | * 255 exclusively to the macros. |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 88 | */ |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 89 | long rfc5424_escape_map[(256/8) / sizeof(long)]; |
| 90 | long hdr_encode_map[(256/8) / sizeof(long)]; |
| 91 | long url_encode_map[(256/8) / sizeof(long)]; |
| 92 | long http_encode_map[(256/8) / sizeof(long)]; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 93 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 94 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 96 | "kern", "user", "mail", "daemon", |
| 97 | "auth", "syslog", "lpr", "news", |
| 98 | "uucp", "cron", "auth2", "ftp", |
| 99 | "ntp", "audit", "alert", "cron2", |
| 100 | "local0", "local1", "local2", "local3", |
| 101 | "local4", "local5", "local6", "local7" |
| 102 | }; |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | const char *log_levels[NB_LOG_LEVELS] = { |
| 105 | "emerg", "alert", "crit", "err", |
| 106 | "warning", "notice", "info", "debug" |
| 107 | }; |
| 108 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 109 | 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] | 110 | 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] | 111 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 112 | |
| 113 | /* log_format */ |
| 114 | struct logformat_type { |
| 115 | char *name; |
| 116 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 117 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 118 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 119 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 122 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 123 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 124 | /* log_format variable names */ |
| 125 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 126 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 127 | |
| 128 | /* please keep these lines sorted ! */ |
| 129 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 130 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 131 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 132 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 133 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_TCP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 134 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 135 | { "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] | 136 | { "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] | 137 | { "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] | 138 | { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */ |
| 139 | { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */ |
| 140 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
| 141 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */ |
| 142 | { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
| 143 | { "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] | 144 | { "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] | 145 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 146 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Damien Claisse | 57c8eb9 | 2020-04-28 12:09:19 +0000 | [diff] [blame] | 147 | { "Tu", LOG_FMT_TU, PR_MODE_TCP, LW_BYTES, NULL }, /* Tu = Tt -Ti */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 148 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 149 | { "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] | 150 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 151 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 152 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 153 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 154 | { "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] | 155 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 156 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */ |
| 157 | { "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] | 158 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 159 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 160 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */ |
| 161 | { "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] | 162 | { "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] | 163 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 164 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 165 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 166 | { "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] | 167 | { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */ |
Maciej Zdeb | fcdfd85 | 2020-11-30 18:27:47 +0000 | [diff] [blame] | 168 | { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP relative or absolute path */ |
| 169 | { "HPO", LOG_FMT_HTTP_PATH_ONLY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path only (without host nor query string) */ |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 170 | { "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] | 171 | { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */ |
| 172 | { "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] | 173 | { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 174 | { "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] | 175 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 176 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 177 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 178 | { "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] | 179 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 180 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 181 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 182 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 183 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 184 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 185 | { "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] | 186 | { "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] | 187 | { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */ |
| 188 | { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */ |
| 189 | { "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] | 190 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 191 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 192 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 195 | 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 |
Remi Tricot-Le Breton | 98b930d | 2021-07-29 09:45:52 +0200 | [diff] [blame] | 196 | char default_https_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r %[fc_conn_err]/%[ssl_fc_hsk_err,hex]/%[ssl_c_err]/%[ssl_c_ca_err] %sslv/%sslc"; |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 197 | 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] | 198 | 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] | 199 | char *log_format = NULL; |
| 200 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 201 | /* Default string used for structured-data part in RFC5424 formatted |
| 202 | * syslog messages. |
| 203 | */ |
| 204 | char default_rfc5424_sd_log_format[] = "- "; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 205 | |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 206 | /* total number of dropped logs */ |
| 207 | unsigned int dropped_logs = 0; |
| 208 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 209 | /* This is a global syslog message buffer, common to all outgoing |
| 210 | * messages. It contains only the data part. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 211 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 212 | THREAD_LOCAL char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 213 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 214 | /* A global syslog message buffer, common to all RFC5424 syslog messages. |
| 215 | * Currently, it is used for generating the structured-data part. |
| 216 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 217 | THREAD_LOCAL char *logline_rfc5424 = NULL; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 218 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 219 | struct logformat_var_args { |
| 220 | char *name; |
| 221 | int mask; |
| 222 | }; |
| 223 | |
| 224 | struct logformat_var_args var_args_list[] = { |
| 225 | // global |
| 226 | { "M", LOG_OPT_MANDATORY }, |
| 227 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 228 | { "X", LOG_OPT_HEXA }, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 229 | { "E", LOG_OPT_ESC }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 230 | { 0, 0 } |
| 231 | }; |
| 232 | |
| 233 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 234 | * callback used to configure addr source retrieval |
| 235 | */ |
| 236 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 237 | { |
| 238 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 245 | * Parse args in a logformat_var. Returns 0 in error |
| 246 | * case, otherwise, it returns 1. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 247 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 248 | 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] | 249 | { |
| 250 | int i = 0; |
| 251 | int end = 0; |
| 252 | int flags = 0; // 1 = + 2 = - |
| 253 | char *sp = NULL; // start pointer |
| 254 | |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 255 | if (args == NULL) { |
| 256 | 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] | 257 | return 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 258 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 259 | |
| 260 | while (1) { |
| 261 | if (*args == '\0') |
| 262 | end = 1; |
| 263 | |
| 264 | if (*args == '+') { |
| 265 | // add flag |
| 266 | sp = args + 1; |
| 267 | flags = 1; |
| 268 | } |
| 269 | if (*args == '-') { |
| 270 | // delete flag |
| 271 | sp = args + 1; |
| 272 | flags = 2; |
| 273 | } |
| 274 | |
| 275 | if (*args == '\0' || *args == ',') { |
| 276 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 277 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 278 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 279 | if (flags == 1) { |
| 280 | node->options |= var_args_list[i].mask; |
| 281 | break; |
| 282 | } else if (flags == 2) { |
| 283 | node->options &= ~var_args_list[i].mask; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | sp = NULL; |
| 289 | if (end) |
| 290 | break; |
| 291 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 292 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 293 | } |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 294 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 298 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 299 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 300 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 301 | * 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] | 302 | * Returns false in error case and err is filled, otherwise returns true. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 303 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 306 | int j; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 307 | struct logformat_node *node = NULL; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 308 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 309 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 310 | if (strlen(logformat_keywords[j].name) == var_len && |
| 311 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 312 | 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] | 313 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 314 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 315 | memprintf(err, "out of memory error"); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 316 | goto error_free; |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 317 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 318 | node->type = logformat_keywords[j].type; |
| 319 | node->options = *defoptions; |
| 320 | if (arg_len) { |
| 321 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 322 | if (!parse_logformat_var_args(node->arg, node, err)) |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 323 | goto error_free; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 324 | } |
| 325 | if (node->type == LOG_FMT_GLOBAL) { |
| 326 | *defoptions = node->options; |
| 327 | free(node->arg); |
| 328 | free(node); |
| 329 | } else { |
| 330 | if (logformat_keywords[j].config_callback && |
| 331 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 332 | goto error_free; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 333 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 334 | curproxy->to_log |= logformat_keywords[j].lw; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 335 | LIST_APPEND(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 336 | } |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 337 | return 1; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 338 | } else { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 339 | memprintf(err, "format variable '%s' is reserved for HTTP mode", |
| 340 | logformat_keywords[j].name); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 341 | goto error_free; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 342 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 345 | |
| 346 | j = var[var_len]; |
| 347 | var[var_len] = 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 348 | 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] | 349 | var[var_len] = j; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 350 | |
| 351 | error_free: |
| 352 | if (node) { |
| 353 | free(node->arg); |
| 354 | free(node); |
| 355 | } |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 356 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * push to the logformat linked list |
| 361 | * |
| 362 | * start: start pointer |
| 363 | * end: end text pointer |
| 364 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 365 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 366 | * |
| 367 | * LOG_TEXT: copy chars from start to end excluding end. |
| 368 | * |
| 369 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 370 | 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] | 371 | { |
| 372 | char *str; |
| 373 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 374 | if (type == LF_TEXT) { /* type text */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 375 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 376 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 377 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 378 | return 0; |
| 379 | } |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 380 | str = calloc(1, end - start + 1); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 381 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 382 | str[end - start] = '\0'; |
| 383 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 384 | node->type = LOG_FMT_TEXT; // type string |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 385 | LIST_APPEND(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 386 | } else if (type == LF_SEPARATOR) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 387 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 388 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 389 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 390 | return 0; |
| 391 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 392 | node->type = LOG_FMT_SEPARATOR; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 393 | LIST_APPEND(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 394 | } |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 395 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 399 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 400 | * success. At the moment, sample converters are not yet supported but fetch arguments |
Willy Tarreau | cd0d2ed | 2020-02-14 17:33:06 +0100 | [diff] [blame] | 401 | * should work. The curpx->conf.args.ctx must be set by the caller. If an end pointer |
| 402 | * is passed in <endptr>, it will be updated with the pointer to the first character |
| 403 | * not part of the sample expression. |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 404 | * |
| 405 | * In error case, the function returns 0, otherwise it returns 1. |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 406 | */ |
Willy Tarreau | cd0d2ed | 2020-02-14 17:33:06 +0100 | [diff] [blame] | 407 | 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, char **endptr) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 408 | { |
| 409 | char *cmd[2]; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 410 | struct sample_expr *expr = NULL; |
| 411 | struct logformat_node *node = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 412 | int cmd_arg; |
| 413 | |
| 414 | cmd[0] = text; |
| 415 | cmd[1] = ""; |
| 416 | cmd_arg = 0; |
| 417 | |
Willy Tarreau | cd0d2ed | 2020-02-14 17:33:06 +0100 | [diff] [blame] | 418 | expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args, endptr); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 419 | if (!expr) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 420 | memprintf(err, "failed to parse sample expression <%s> : %s", text, *err); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 421 | goto error_free; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 424 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 425 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 426 | memprintf(err, "out of memory error"); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 427 | goto error_free; |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 428 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 429 | node->type = LOG_FMT_EXPR; |
| 430 | node->expr = expr; |
| 431 | node->options = options; |
| 432 | |
| 433 | if (arg_len) { |
| 434 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 435 | if (!parse_logformat_var_args(node->arg, node, err)) |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 436 | goto error_free; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 437 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 438 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 439 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 440 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 441 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 442 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 443 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 444 | if (!(expr->fetch->val & cap)) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 445 | memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here", |
| 446 | text, sample_src_names(expr->fetch->use)); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 447 | goto error_free; |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 448 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 449 | |
Christopher Faulet | 581db2b | 2021-03-26 10:02:46 +0100 | [diff] [blame] | 450 | if ((options & LOG_OPT_HTTP) && (expr->fetch->use & (SMP_USE_L6REQ|SMP_USE_L6RES))) { |
| 451 | ha_warning("parsing [%s:%d] : L6 sample fetch <%s> ignored in HTTP log-format string.\n", |
| 452 | curpx->conf.args.file, curpx->conf.args.line, text); |
| 453 | } |
| 454 | |
Christopher Faulet | 711ed6a | 2019-07-16 14:16:10 +0200 | [diff] [blame] | 455 | /* check if we need to allocate an http_txn struct for HTTP parsing */ |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 456 | /* 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] | 457 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 458 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 459 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 460 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 461 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 462 | */ |
| 463 | curpx->to_log |= LW_XPRT; |
Christopher Faulet | d2236cd | 2020-04-06 18:29:14 +0200 | [diff] [blame] | 464 | if (curpx->http_needed) |
| 465 | curpx->to_log |= LW_REQ; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 466 | LIST_APPEND(list_format, &node->list); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 467 | return 1; |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 468 | |
| 469 | error_free: |
| 470 | release_sample_expr(expr); |
| 471 | if (node) { |
| 472 | free(node->arg); |
| 473 | free(node); |
| 474 | } |
| 475 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 479 | * Parse the log_format string and fill a linked list. |
| 480 | * 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] | 481 | * You can set arguments using { } : %{many arguments}varname. |
| 482 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 483 | * |
Ilya Shipitsin | ae40dbc | 2020-04-04 12:59:53 +0500 | [diff] [blame] | 484 | * fmt: the string to parse |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 485 | * curproxy: the proxy affected |
| 486 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 487 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 488 | * cap: all SMP_VAL_* flags supported by the consumer |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 489 | * |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 490 | * 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] | 491 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 492 | 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] | 493 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 494 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 495 | char *arg = NULL; /* start pointer for args */ |
| 496 | char *var = NULL; /* start pointer for vars */ |
| 497 | int arg_len = 0; |
| 498 | int var_len = 0; |
| 499 | int cformat; /* current token format */ |
| 500 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 501 | struct logformat_node *tmplf, *back; |
| 502 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 503 | sp = str = backfmt = strdup(fmt); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 504 | if (!str) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 505 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 506 | return 0; |
| 507 | } |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 508 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 509 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 510 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 511 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 512 | LIST_DELETE(&tmplf->list); |
Dragan Dosen | 61302da | 2019-04-30 00:40:02 +0200 | [diff] [blame] | 513 | release_sample_expr(tmplf->expr); |
| 514 | free(tmplf->arg); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 515 | free(tmplf); |
| 516 | } |
| 517 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 518 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 519 | pformat = cformat; |
| 520 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 521 | if (!*str) |
| 522 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 523 | |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 524 | /* 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] | 525 | * second have all common paths processed at one place. The common paths are the ones |
| 526 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 527 | * We use the common LF_INIT state to dispatch to the different final states. |
| 528 | */ |
| 529 | switch (pformat) { |
| 530 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 531 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 532 | arg_len = var_len = 0; |
| 533 | if (*str == '{') { // optional argument |
| 534 | cformat = LF_STARG; |
| 535 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 536 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 537 | else if (*str == '[') { |
| 538 | cformat = LF_STEXPR; |
| 539 | var = str + 1; // store expr in variable name |
| 540 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 541 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 542 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 543 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 544 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 545 | else if (*str == '%') |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 546 | cformat = LF_TEXT; // convert this character to a literal (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 547 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 548 | /* single '%' followed by blank or digit, send them both */ |
| 549 | cformat = LF_TEXT; |
| 550 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 551 | sp = str - 1; /* send both the '%' and the current char */ |
Jim Freeman | a2278c8 | 2017-04-15 08:01:59 -0600 | [diff] [blame] | 552 | 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] | 553 | *str, (int)(str - backfmt), fmt); |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 554 | goto fail; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 555 | |
| 556 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 557 | else |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 558 | cformat = LF_INIT; // handle other cases of literals |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 559 | break; |
| 560 | |
| 561 | case LF_STARG: // text immediately following '%{' |
| 562 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 563 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 564 | arg_len = str - arg; |
| 565 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 566 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 570 | if (*str == '[') { |
| 571 | cformat = LF_STEXPR; |
| 572 | var = str + 1; // store expr in variable name |
| 573 | break; |
| 574 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 575 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 576 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 577 | var = str; |
| 578 | break; |
| 579 | } |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 580 | memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg); |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 581 | goto fail; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 582 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 583 | case LF_STEXPR: // text immediately following '%[' |
Willy Tarreau | cd0d2ed | 2020-02-14 17:33:06 +0100 | [diff] [blame] | 584 | /* the whole sample expression is parsed at once, |
| 585 | * returning the pointer to the first character not |
| 586 | * part of the expression, which MUST be the trailing |
| 587 | * angle bracket. |
| 588 | */ |
| 589 | if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err, &str)) |
| 590 | goto fail; |
| 591 | |
| 592 | if (*str == ']') { |
| 593 | // end of arg, go on with next state |
| 594 | cformat = pformat = LF_EDEXPR; |
| 595 | sp = str; |
| 596 | } |
| 597 | else { |
| 598 | char c = *str; |
| 599 | *str = 0; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 600 | if (isprint((unsigned char)c)) |
Willy Tarreau | cd0d2ed | 2020-02-14 17:33:06 +0100 | [diff] [blame] | 601 | memprintf(err, "expected ']' after '%s', but found '%c'", var, c); |
| 602 | else |
| 603 | memprintf(err, "missing ']' after '%s'", var); |
Dragan Dosen | 2866acf | 2020-06-30 21:16:43 +0200 | [diff] [blame] | 604 | goto fail; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 605 | } |
| 606 | break; |
| 607 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 608 | case LF_VAR: // text part of a variable name |
| 609 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 610 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 611 | cformat = LF_INIT; // not variable name anymore |
| 612 | break; |
| 613 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 614 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 615 | cformat = LF_INIT; |
| 616 | } |
| 617 | |
| 618 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 619 | switch (*str) { |
| 620 | case '%': cformat = LF_STARTVAR; break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 621 | case 0 : cformat = LF_END; break; |
Dragan Dosen | 1e3b16f | 2020-06-23 18:16:44 +0200 | [diff] [blame] | 622 | case ' ': |
| 623 | if (options & LOG_OPT_MERGE_SPACES) { |
| 624 | cformat = LF_SEPARATOR; |
| 625 | break; |
| 626 | } |
| 627 | /* fall through */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 628 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 632 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 633 | switch (pformat) { |
| 634 | case LF_VAR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 635 | if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err)) |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 636 | goto fail; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 637 | break; |
| 638 | case LF_TEXT: |
| 639 | case LF_SEPARATOR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 640 | if (!add_to_logformat_list(sp, str, pformat, list_format, err)) |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 641 | goto fail; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | 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] | 645 | } |
| 646 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 647 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 648 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 649 | memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%"); |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 650 | goto fail; |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 651 | } |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 652 | free(backfmt); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 653 | |
| 654 | return 1; |
Willy Tarreau | 51013e8 | 2019-12-11 12:05:39 +0100 | [diff] [blame] | 655 | fail: |
| 656 | free(backfmt); |
| 657 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 660 | /* |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 661 | * Parse the first range of indexes from a string made of a list of comma separated |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 662 | * ranges of indexes. Note that an index may be considered as a particular range |
| 663 | * with a high limit to the low limit. |
| 664 | */ |
| 665 | int get_logsrv_smp_range(unsigned int *low, unsigned int *high, char **arg, char **err) |
| 666 | { |
| 667 | char *end, *p; |
| 668 | |
| 669 | *low = *high = 0; |
| 670 | |
| 671 | p = *arg; |
| 672 | end = strchr(p, ','); |
| 673 | if (!end) |
| 674 | end = p + strlen(p); |
| 675 | |
| 676 | *high = *low = read_uint((const char **)&p, end); |
| 677 | if (!*low || (p != end && *p != '-')) |
| 678 | goto err; |
| 679 | |
| 680 | if (p == end) |
| 681 | goto done; |
| 682 | |
| 683 | p++; |
| 684 | *high = read_uint((const char **)&p, end); |
| 685 | if (!*high || *high <= *low || p != end) |
| 686 | goto err; |
| 687 | |
| 688 | done: |
| 689 | if (*end == ',') |
| 690 | end++; |
| 691 | *arg = end; |
| 692 | return 1; |
| 693 | |
| 694 | err: |
| 695 | memprintf(err, "wrong sample range '%s'", *arg); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Returns 1 if the range defined by <low> and <high> overlaps |
| 701 | * one of them in <rgs> array of ranges with <sz> the size of this |
| 702 | * array, 0 if not. |
| 703 | */ |
| 704 | int smp_log_ranges_overlap(struct smp_log_range *rgs, size_t sz, |
| 705 | unsigned int low, unsigned int high, char **err) |
| 706 | { |
| 707 | size_t i; |
| 708 | |
| 709 | for (i = 0; i < sz; i++) { |
| 710 | if ((low >= rgs[i].low && low <= rgs[i].high) || |
| 711 | (high >= rgs[i].low && high <= rgs[i].high)) { |
| 712 | memprintf(err, "ranges are overlapping"); |
| 713 | return 1; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | int smp_log_range_cmp(const void *a, const void *b) |
| 721 | { |
| 722 | const struct smp_log_range *rg_a = a; |
| 723 | const struct smp_log_range *rg_b = b; |
| 724 | |
| 725 | if (rg_a->high < rg_b->low) |
| 726 | return -1; |
| 727 | else if (rg_a->low > rg_b->high) |
| 728 | return 1; |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | /* |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 734 | * Parse "log" keyword and update <logsrvs> list accordingly. |
| 735 | * |
| 736 | * When <do_del> is set, it means the "no log" line was parsed, so all log |
| 737 | * servers in <logsrvs> are released. |
| 738 | * |
| 739 | * Otherwise, we try to parse the "log" line. First of all, when the list is not |
| 740 | * the global one, we look for the parameter "global". If we find it, |
| 741 | * global.logsrvs is copied. Else we parse each arguments. |
| 742 | * |
| 743 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 744 | * filled. |
| 745 | */ |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 746 | int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err) |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 747 | { |
Willy Tarreau | ae32ac7 | 2020-10-27 09:51:37 +0100 | [diff] [blame] | 748 | struct smp_log_range *smp_rgs = NULL; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 749 | struct sockaddr_storage *sk; |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 750 | struct protocol *proto; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 751 | struct logsrv *logsrv = NULL; |
| 752 | int port1, port2; |
| 753 | int cur_arg; |
Willy Tarreau | 8959926 | 2020-09-15 14:03:26 +0200 | [diff] [blame] | 754 | int fd; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * "no log": delete previous herited or defined syslog |
| 758 | * servers. |
| 759 | */ |
| 760 | if (do_del) { |
| 761 | struct logsrv *back; |
| 762 | |
| 763 | if (*(args[1]) != 0) { |
| 764 | memprintf(err, "'no log' does not expect arguments"); |
| 765 | goto error; |
| 766 | } |
| 767 | |
| 768 | list_for_each_entry_safe(logsrv, back, logsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 769 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 770 | free(logsrv); |
| 771 | } |
| 772 | return 1; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * "log global": copy global.logrsvs linked list to the end of logsrvs |
| 777 | * list. But first, we check (logsrvs != global.logsrvs). |
| 778 | */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 779 | if (*(args[1]) && *(args[2]) == 0 && strcmp(args[1], "global") == 0) { |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 780 | if (logsrvs == &global.logsrvs) { |
| 781 | memprintf(err, "'global' is not supported for a global syslog server"); |
| 782 | goto error; |
| 783 | } |
| 784 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 785 | struct logsrv *node; |
| 786 | |
| 787 | list_for_each_entry(node, logsrvs, list) { |
| 788 | if (node->ref == logsrv) |
| 789 | goto skip_logsrv; |
| 790 | } |
| 791 | |
| 792 | node = malloc(sizeof(*node)); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 793 | memcpy(node, logsrv, sizeof(struct logsrv)); |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 794 | node->ref = logsrv; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 795 | LIST_INIT(&node->list); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 796 | LIST_APPEND(logsrvs, &node->list); |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 797 | node->conf.file = strdup(file); |
| 798 | node->conf.line = linenum; |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 799 | |
| 800 | skip_logsrv: |
| 801 | continue; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 802 | } |
| 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * "log <address> ...: parse a syslog server line |
| 808 | */ |
| 809 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 810 | memprintf(err, "expects <address> and <facility> %s as arguments", |
| 811 | ((logsrvs == &global.logsrvs) ? "" : "or global")); |
| 812 | goto error; |
| 813 | } |
| 814 | |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 815 | /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */ |
| 816 | if (strcmp(args[1], "stdout") == 0) |
| 817 | args[1] = "fd@1"; |
| 818 | else if (strcmp(args[1], "stderr") == 0) |
| 819 | args[1] = "fd@2"; |
| 820 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 821 | logsrv = calloc(1, sizeof(*logsrv)); |
| 822 | if (!logsrv) { |
| 823 | memprintf(err, "out of memory"); |
| 824 | goto error; |
| 825 | } |
| 826 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 827 | logsrv->conf.file = strdup(file); |
| 828 | logsrv->conf.line = linenum; |
| 829 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 830 | /* skip address for now, it will be parsed at the end */ |
| 831 | cur_arg = 2; |
| 832 | |
| 833 | /* just after the address, a length may be specified */ |
| 834 | logsrv->maxlen = MAX_SYSLOG_LEN; |
| 835 | if (strcmp(args[cur_arg], "len") == 0) { |
| 836 | int len = atoi(args[cur_arg+1]); |
| 837 | if (len < 80 || len > 65535) { |
| 838 | memprintf(err, "invalid log length '%s', must be between 80 and 65535", |
| 839 | args[cur_arg+1]); |
| 840 | goto error; |
| 841 | } |
| 842 | logsrv->maxlen = len; |
| 843 | cur_arg += 2; |
| 844 | } |
| 845 | if (logsrv->maxlen > global.max_syslog_len) |
| 846 | global.max_syslog_len = logsrv->maxlen; |
| 847 | |
| 848 | /* after the length, a format may be specified */ |
| 849 | if (strcmp(args[cur_arg], "format") == 0) { |
| 850 | logsrv->format = get_log_format(args[cur_arg+1]); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 851 | if (logsrv->format == LOG_FORMAT_UNSPEC) { |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 852 | memprintf(err, "unknown log format '%s'", args[cur_arg+1]); |
| 853 | goto error; |
| 854 | } |
| 855 | cur_arg += 2; |
| 856 | } |
| 857 | |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 858 | if (strcmp(args[cur_arg], "sample") == 0) { |
| 859 | unsigned low, high; |
| 860 | char *p, *beg, *end, *smp_sz_str; |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 861 | size_t smp_rgs_sz = 0, smp_sz = 0, new_smp_sz; |
| 862 | |
| 863 | p = args[cur_arg+1]; |
| 864 | smp_sz_str = strchr(p, ':'); |
| 865 | if (!smp_sz_str) { |
| 866 | memprintf(err, "Missing sample size"); |
| 867 | goto error; |
| 868 | } |
| 869 | |
| 870 | *smp_sz_str++ = '\0'; |
| 871 | |
| 872 | end = p + strlen(p); |
| 873 | |
| 874 | while (p != end) { |
| 875 | if (!get_logsrv_smp_range(&low, &high, &p, err)) |
| 876 | goto error; |
| 877 | |
| 878 | if (smp_rgs && smp_log_ranges_overlap(smp_rgs, smp_rgs_sz, low, high, err)) |
| 879 | goto error; |
| 880 | |
| 881 | smp_rgs = my_realloc2(smp_rgs, (smp_rgs_sz + 1) * sizeof *smp_rgs); |
| 882 | if (!smp_rgs) { |
| 883 | memprintf(err, "out of memory error"); |
| 884 | goto error; |
| 885 | } |
| 886 | |
| 887 | smp_rgs[smp_rgs_sz].low = low; |
| 888 | smp_rgs[smp_rgs_sz].high = high; |
| 889 | smp_rgs[smp_rgs_sz].sz = high - low + 1; |
| 890 | smp_rgs[smp_rgs_sz].curr_idx = 0; |
| 891 | if (smp_rgs[smp_rgs_sz].high > smp_sz) |
| 892 | smp_sz = smp_rgs[smp_rgs_sz].high; |
| 893 | smp_rgs_sz++; |
| 894 | } |
| 895 | |
Tim Duesterhus | 2164800 | 2019-06-23 22:10:10 +0200 | [diff] [blame] | 896 | if (smp_rgs == NULL) { |
| 897 | memprintf(err, "no sampling ranges given"); |
| 898 | goto error; |
| 899 | } |
| 900 | |
Frédéric Lécaille | d95ea28 | 2019-04-24 16:14:33 +0200 | [diff] [blame] | 901 | beg = smp_sz_str; |
| 902 | end = beg + strlen(beg); |
| 903 | new_smp_sz = read_uint((const char **)&beg, end); |
| 904 | if (!new_smp_sz || beg != end) { |
| 905 | memprintf(err, "wrong sample size '%s' for sample range '%s'", |
| 906 | smp_sz_str, args[cur_arg+1]); |
| 907 | goto error; |
| 908 | } |
| 909 | |
| 910 | if (new_smp_sz < smp_sz) { |
| 911 | memprintf(err, "sample size %zu should be greater or equal to " |
| 912 | "%zu the maximum of the high ranges limits", |
| 913 | new_smp_sz, smp_sz); |
| 914 | goto error; |
| 915 | } |
| 916 | smp_sz = new_smp_sz; |
| 917 | |
| 918 | /* Let's order <smp_rgs> array. */ |
| 919 | qsort(smp_rgs, smp_rgs_sz, sizeof(struct smp_log_range), smp_log_range_cmp); |
| 920 | |
| 921 | logsrv->lb.smp_rgs = smp_rgs; |
| 922 | logsrv->lb.smp_rgs_sz = smp_rgs_sz; |
| 923 | logsrv->lb.smp_sz = smp_sz; |
| 924 | |
| 925 | cur_arg += 2; |
| 926 | } |
Frédéric Lécaille | d803e47 | 2019-04-25 07:42:09 +0200 | [diff] [blame] | 927 | HA_SPIN_INIT(&logsrv->lock); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 928 | /* parse the facility */ |
| 929 | logsrv->facility = get_log_facility(args[cur_arg]); |
| 930 | if (logsrv->facility < 0) { |
| 931 | memprintf(err, "unknown log facility '%s'", args[cur_arg]); |
| 932 | goto error; |
| 933 | } |
| 934 | cur_arg++; |
| 935 | |
| 936 | /* parse the max syslog level (default: debug) */ |
| 937 | logsrv->level = 7; |
| 938 | if (*(args[cur_arg])) { |
| 939 | logsrv->level = get_log_level(args[cur_arg]); |
| 940 | if (logsrv->level < 0) { |
| 941 | memprintf(err, "unknown optional log level '%s'", args[cur_arg]); |
| 942 | goto error; |
| 943 | } |
| 944 | cur_arg++; |
| 945 | } |
| 946 | |
| 947 | /* parse the limit syslog level (default: emerg) */ |
| 948 | logsrv->minlvl = 0; |
| 949 | if (*(args[cur_arg])) { |
| 950 | logsrv->minlvl = get_log_level(args[cur_arg]); |
| 951 | if (logsrv->minlvl < 0) { |
| 952 | memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]); |
| 953 | goto error; |
| 954 | } |
| 955 | cur_arg++; |
| 956 | } |
| 957 | |
| 958 | /* Too many args */ |
| 959 | if (*(args[cur_arg])) { |
| 960 | memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]); |
| 961 | goto error; |
| 962 | } |
| 963 | |
| 964 | /* now, back to the address */ |
Willy Tarreau | f3dc30f | 2019-08-30 14:18:44 +0200 | [diff] [blame] | 965 | logsrv->type = LOG_TARGET_DGRAM; |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 966 | if (strncmp(args[1], "ring@", 5) == 0) { |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 967 | logsrv->addr.ss_family = AF_UNSPEC; |
| 968 | logsrv->type = LOG_TARGET_BUFFER; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 969 | logsrv->sink = NULL; |
| 970 | logsrv->ring_name = strdup(args[1] + 5); |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 971 | goto done; |
| 972 | } |
| 973 | |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 974 | sk = str2sa_range(args[1], NULL, &port1, &port2, &fd, &proto, |
| 975 | err, NULL, NULL, |
| 976 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 977 | if (!sk) |
| 978 | goto error; |
Willy Tarreau | 8959926 | 2020-09-15 14:03:26 +0200 | [diff] [blame] | 979 | |
| 980 | if (fd != -1) |
| 981 | logsrv->type = LOG_TARGET_FD; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 982 | logsrv->addr = *sk; |
| 983 | |
| 984 | if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) { |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 985 | if (!port1) |
| 986 | set_host_port(&logsrv->addr, SYSLOG_PORT); |
| 987 | } |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 988 | |
Emeric Brun | 2675490 | 2021-04-07 14:26:44 +0200 | [diff] [blame] | 989 | if (proto && proto->ctrl_type == SOCK_STREAM) { |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 990 | static unsigned long ring_ids; |
| 991 | |
| 992 | /* Implicit sink buffer will be |
| 993 | * initialized in post_check |
| 994 | */ |
| 995 | logsrv->type = LOG_TARGET_BUFFER; |
| 996 | logsrv->sink = NULL; |
| 997 | /* compute uniq name for the ring */ |
| 998 | memprintf(&logsrv->ring_name, "ring#%lu", ++ring_ids); |
| 999 | } |
| 1000 | |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 1001 | done: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1002 | LIST_APPEND(logsrvs, &logsrv->list); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 1003 | return 1; |
| 1004 | |
| 1005 | error: |
Willy Tarreau | ae32ac7 | 2020-10-27 09:51:37 +0100 | [diff] [blame] | 1006 | free(smp_rgs); |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 1007 | if (logsrv) { |
| 1008 | free(logsrv->conf.file); |
Willy Tarreau | a0133fc | 2020-10-27 10:35:32 +0100 | [diff] [blame] | 1009 | free(logsrv->ring_name); |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 1010 | } |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 1011 | free(logsrv); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Amaury Denoyelle | 7b01a8d | 2021-03-29 10:29:07 +0200 | [diff] [blame] | 1015 | |
| 1016 | /* |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1017 | * returns log format, LOG_FORMAT_UNSPEC is return if not found. |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1018 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1019 | enum log_fmt get_log_format(const char *fmt) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1020 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1021 | enum log_fmt format; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1022 | |
| 1023 | format = LOG_FORMATS - 1; |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1024 | while (format > 0 && log_formats[format].name |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1025 | && strcmp(log_formats[format].name, fmt) != 0) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1026 | format--; |
| 1027 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1028 | /* Note: 0 is LOG_FORMAT_UNSPEC */ |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1029 | return format; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1033 | * returns log level for <lev> or -1 if not found. |
| 1034 | */ |
| 1035 | int get_log_level(const char *lev) |
| 1036 | { |
| 1037 | int level; |
| 1038 | |
| 1039 | level = NB_LOG_LEVELS - 1; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1040 | while (level >= 0 && strcmp(log_levels[level], lev) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1041 | level--; |
| 1042 | |
| 1043 | return level; |
| 1044 | } |
| 1045 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1046 | /* |
| 1047 | * returns log facility for <fac> or -1 if not found. |
| 1048 | */ |
| 1049 | int get_log_facility(const char *fac) |
| 1050 | { |
| 1051 | int facility; |
| 1052 | |
| 1053 | facility = NB_LOG_FACILITIES - 1; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1054 | while (facility >= 0 && strcmp(log_facilities[facility], fac) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1055 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1056 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1057 | return facility; |
| 1058 | } |
| 1059 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1060 | /* |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1061 | * Encode the string. |
| 1062 | * |
| 1063 | * When using the +E log format option, it will try to escape '"\]' |
| 1064 | * characters with '\' as prefix. The same prefix should not be used as |
| 1065 | * <escape>. |
| 1066 | */ |
| 1067 | static char *lf_encode_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1068 | const char escape, const long *map, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1069 | const char *string, |
| 1070 | struct logformat_node *node) |
| 1071 | { |
| 1072 | if (node->options & LOG_OPT_ESC) { |
| 1073 | if (start < stop) { |
| 1074 | stop--; /* reserve one byte for the final '\0' */ |
| 1075 | while (start < stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1076 | if (!ha_bit_test((unsigned char)(*string), map)) { |
| 1077 | if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map)) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1078 | *start++ = *string; |
| 1079 | else { |
| 1080 | if (start + 2 >= stop) |
| 1081 | break; |
| 1082 | *start++ = '\\'; |
| 1083 | *start++ = *string; |
| 1084 | } |
| 1085 | } |
| 1086 | else { |
| 1087 | if (start + 3 >= stop) |
| 1088 | break; |
| 1089 | *start++ = escape; |
| 1090 | *start++ = hextab[(*string >> 4) & 15]; |
| 1091 | *start++ = hextab[*string & 15]; |
| 1092 | } |
| 1093 | string++; |
| 1094 | } |
| 1095 | *start = '\0'; |
| 1096 | } |
| 1097 | } |
| 1098 | else { |
| 1099 | return encode_string(start, stop, escape, map, string); |
| 1100 | } |
| 1101 | |
| 1102 | return start; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Encode the chunk. |
| 1107 | * |
| 1108 | * When using the +E log format option, it will try to escape '"\]' |
| 1109 | * characters with '\' as prefix. The same prefix should not be used as |
| 1110 | * <escape>. |
| 1111 | */ |
| 1112 | static char *lf_encode_chunk(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1113 | const char escape, const long *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1114 | const struct buffer *chunk, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1115 | struct logformat_node *node) |
| 1116 | { |
| 1117 | char *str, *end; |
| 1118 | |
| 1119 | if (node->options & LOG_OPT_ESC) { |
| 1120 | if (start < stop) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1121 | str = chunk->area; |
| 1122 | end = chunk->area + chunk->data; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1123 | |
| 1124 | stop--; /* reserve one byte for the final '\0' */ |
| 1125 | while (start < stop && str < end) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1126 | if (!ha_bit_test((unsigned char)(*str), map)) { |
| 1127 | if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map)) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1128 | *start++ = *str; |
| 1129 | else { |
| 1130 | if (start + 2 >= stop) |
| 1131 | break; |
| 1132 | *start++ = '\\'; |
| 1133 | *start++ = *str; |
| 1134 | } |
| 1135 | } |
| 1136 | else { |
| 1137 | if (start + 3 >= stop) |
| 1138 | break; |
| 1139 | *start++ = escape; |
| 1140 | *start++ = hextab[(*str >> 4) & 15]; |
| 1141 | *start++ = hextab[*str & 15]; |
| 1142 | } |
| 1143 | str++; |
| 1144 | } |
| 1145 | *start = '\0'; |
| 1146 | } |
| 1147 | } |
| 1148 | else { |
| 1149 | return encode_chunk(start, stop, escape, map, chunk); |
| 1150 | } |
| 1151 | |
| 1152 | return start; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1156 | * Write a string in the log string |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1157 | * Take cares of quote and escape options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1158 | * |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1159 | * Return the address of the \0 character, or NULL on error |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1160 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1161 | 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] | 1162 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1163 | if (size < 2) |
| 1164 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1165 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1166 | if (node->options & LOG_OPT_QUOTE) { |
| 1167 | *(dst++) = '"'; |
| 1168 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1169 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1171 | if (src && len) { |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1172 | if (++len > size) |
| 1173 | len = size; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1174 | if (node->options & LOG_OPT_ESC) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1175 | char *ret; |
| 1176 | |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1177 | ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1178 | if (ret == NULL || *ret != '\0') |
| 1179 | return NULL; |
| 1180 | len = ret - dst; |
| 1181 | } |
| 1182 | else { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1183 | len = strlcpy2(dst, src, len); |
| 1184 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1185 | |
| 1186 | size -= len; |
| 1187 | dst += len; |
| 1188 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1189 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 1190 | if (size < 2) |
| 1191 | return NULL; |
| 1192 | *(dst++) = '-'; |
| 1193 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1194 | |
| 1195 | if (node->options & LOG_OPT_QUOTE) { |
| 1196 | if (size < 2) |
| 1197 | return NULL; |
| 1198 | *(dst++) = '"'; |
| 1199 | } |
| 1200 | |
| 1201 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1202 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1205 | 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] | 1206 | { |
| 1207 | return lf_text_len(dst, src, size, size, node); |
| 1208 | } |
| 1209 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1210 | /* |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1211 | * Write a IP address to the log string |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1212 | * +X option write in hexadecimal notation, most significant byte on the left |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1213 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1214 | 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] | 1215 | { |
| 1216 | char *ret = dst; |
| 1217 | int iret; |
| 1218 | char pn[INET6_ADDRSTRLEN]; |
| 1219 | |
| 1220 | if (node->options & LOG_OPT_HEXA) { |
Radek Zajic | 594c456 | 2019-03-22 10:21:54 +0000 | [diff] [blame] | 1221 | unsigned char *addr = NULL; |
| 1222 | switch (sockaddr->sa_family) { |
| 1223 | case AF_INET: |
| 1224 | addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 1225 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 1226 | break; |
| 1227 | case AF_INET6: |
| 1228 | addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr; |
| 1229 | iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", |
| 1230 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], |
| 1231 | addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]); |
| 1232 | break; |
| 1233 | default: |
| 1234 | return NULL; |
| 1235 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1236 | if (iret < 0 || iret > size) |
| 1237 | return NULL; |
| 1238 | ret += iret; |
| 1239 | } else { |
| 1240 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 1241 | ret = lf_text(dst, pn, size, node); |
| 1242 | if (ret == NULL) |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Write a port to the log |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1250 | * +X option write in hexadecimal notation, most significant byte on the left |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1251 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1252 | 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] | 1253 | { |
| 1254 | char *ret = dst; |
| 1255 | int iret; |
| 1256 | |
| 1257 | if (node->options & LOG_OPT_HEXA) { |
| 1258 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 1259 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 1260 | if (iret < 0 || iret > size) |
| 1261 | return NULL; |
| 1262 | ret += iret; |
| 1263 | } else { |
| 1264 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 1265 | if (ret == NULL) |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1271 | |
| 1272 | /* |
| 1273 | * This function sends the syslog message using a printf format string. It |
| 1274 | * expects an LF-terminated message. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1275 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1276 | void send_log(struct proxy *p, int level, const char *format, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1277 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1278 | va_list argp; |
| 1279 | int data_len; |
| 1280 | |
| 1281 | if (level < 0 || format == NULL || logline == NULL) |
| 1282 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1283 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1284 | va_start(argp, format); |
| 1285 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
| 1286 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 1287 | data_len = global.max_syslog_len; |
| 1288 | va_end(argp); |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1289 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1290 | __send_log((p ? &p->logsrvs : NULL), (p ? &p->log_tag : NULL), level, |
| 1291 | logline, data_len, default_rfc5424_sd_log_format, 2); |
| 1292 | } |
| 1293 | /* |
| 1294 | * This function builds a log header of given format using given |
| 1295 | * metadata, if format is set to LOF_FORMAT_UNSPEC, it tries |
| 1296 | * to determine format based on given metadas. It is useful |
| 1297 | * for log-forwarding to be able to forward any format without |
| 1298 | * settings. |
| 1299 | * This function returns a struct ist array of elements of the header |
| 1300 | * nbelem is set to the number of available elements. |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1301 | * This function returns currently a maximum of NB_LOG_HDR_IST_ELEMENTS |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1302 | * elements. |
| 1303 | */ |
| 1304 | struct ist *build_log_header(enum log_fmt format, int level, int facility, |
| 1305 | struct ist *metadata, size_t *nbelem) |
| 1306 | { |
| 1307 | static THREAD_LOCAL struct { |
| 1308 | struct ist ist_vector[NB_LOG_HDR_MAX_ELEMENTS]; |
| 1309 | char timestamp_buffer[LOG_LEGACYTIME_LEN+1+1]; |
| 1310 | time_t cur_legacy_time; |
| 1311 | char priority_buffer[6]; |
| 1312 | } hdr_ctx = { .priority_buffer = "<<<<>" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1313 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1314 | struct tm logtime; |
| 1315 | int len; |
| 1316 | int fac_level = 0; |
| 1317 | time_t time = date.tv_sec; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1318 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1319 | *nbelem = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1320 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1321 | |
| 1322 | if (format == LOG_FORMAT_UNSPEC) { |
| 1323 | format = LOG_FORMAT_RAW; |
| 1324 | if (metadata) { |
| 1325 | /* If a hostname is set, it appears we want to perform syslog |
| 1326 | * because only rfc5427 or rfc3164 support an hostname. |
| 1327 | */ |
| 1328 | if (metadata[LOG_META_HOST].len) { |
| 1329 | /* If a rfc5424 compliant timestamp is used we consider |
| 1330 | * that output format is rfc5424, else legacy format |
| 1331 | * is used as specified default for local logs |
| 1332 | * in documentation. |
| 1333 | */ |
| 1334 | if ((metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') |
| 1335 | || (metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN)) |
| 1336 | format = LOG_FORMAT_RFC5424; |
| 1337 | else |
| 1338 | format = LOG_FORMAT_RFC3164; |
| 1339 | } |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1340 | else if (metadata[LOG_META_TAG].len) { |
| 1341 | /* Tag is present but no hostname, we should |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 1342 | * consider we try to emit a local log |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1343 | * in legacy format (analog to RFC3164 but |
| 1344 | * with stripped hostname). |
| 1345 | */ |
| 1346 | format = LOG_FORMAT_LOCAL; |
| 1347 | } |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1348 | else if (metadata[LOG_META_PRIO].len) { |
| 1349 | /* the source seems a parsed message |
| 1350 | * offering a valid level/prio prefix |
| 1351 | * so we consider this format. |
| 1352 | */ |
| 1353 | format = LOG_FORMAT_PRIO; |
| 1354 | } |
| 1355 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1358 | /* prepare priority, stored into 1 single elem */ |
| 1359 | switch (format) { |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1360 | case LOG_FORMAT_LOCAL: |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1361 | case LOG_FORMAT_RFC3164: |
| 1362 | case LOG_FORMAT_RFC5424: |
| 1363 | case LOG_FORMAT_PRIO: |
| 1364 | fac_level = facility << 3; |
| 1365 | /* further format ignore the facility */ |
| 1366 | /* fall through */ |
| 1367 | case LOG_FORMAT_TIMED: |
| 1368 | case LOG_FORMAT_SHORT: |
| 1369 | fac_level += level; |
| 1370 | hdr_ctx.ist_vector[*nbelem].ptr = &hdr_ctx.priority_buffer[3]; /* last digit of the log level */ |
| 1371 | do { |
| 1372 | *hdr_ctx.ist_vector[*nbelem].ptr = '0' + fac_level % 10; |
| 1373 | fac_level /= 10; |
| 1374 | hdr_ctx.ist_vector[*nbelem].ptr--; |
| 1375 | } while (fac_level && hdr_ctx.ist_vector[*nbelem].ptr > &hdr_ctx.priority_buffer[0]); |
| 1376 | *hdr_ctx.ist_vector[*nbelem].ptr = '<'; |
| 1377 | hdr_ctx.ist_vector[(*nbelem)++].len = &hdr_ctx.priority_buffer[5] - hdr_ctx.ist_vector[0].ptr; |
| 1378 | break; |
| 1379 | case LOG_FORMAT_ISO: |
| 1380 | case LOG_FORMAT_RAW: |
| 1381 | break; |
| 1382 | case LOG_FORMAT_UNSPEC: |
| 1383 | case LOG_FORMATS: |
| 1384 | ABORT_NOW(); |
| 1385 | } |
Willy Tarreau | 094af4e | 2015-01-07 15:03:42 +0100 | [diff] [blame] | 1386 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1387 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1388 | /* prepare timestamp, stored into a max of 4 elems */ |
| 1389 | switch (format) { |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1390 | case LOG_FORMAT_LOCAL: |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1391 | case LOG_FORMAT_RFC3164: |
| 1392 | /* rfc3164 ex: 'Jan 1 00:00:00 ' */ |
| 1393 | if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) { |
| 1394 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME]; |
| 1395 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1396 | /* time is set, break immediately */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1397 | break; |
| 1398 | } |
| 1399 | else if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) { |
| 1400 | int month; |
| 1401 | char *timestamp = metadata[LOG_META_TIME].ptr; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1402 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1403 | /* iso time always begins like this: '1970-01-01T00:00:00' */ |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1404 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1405 | /* compute month */ |
| 1406 | month = 10*(timestamp[5] - '0') + (timestamp[6] - '0'); |
| 1407 | if (month) |
| 1408 | month--; |
| 1409 | if (month <= 11) { |
| 1410 | /* builds log prefix ex: 'Jan 1 ' */ |
| 1411 | len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer), |
| 1412 | "%s %c%c ", monthname[month], |
| 1413 | timestamp[8] != '0' ? timestamp[8] : ' ', |
| 1414 | timestamp[9]); |
| 1415 | /* we reused the timestamp_buffer, signal that it does not |
| 1416 | * contain local time anymore |
| 1417 | */ |
| 1418 | hdr_ctx.cur_legacy_time = 0; |
| 1419 | if (len == 7) { |
| 1420 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len); |
| 1421 | /* adds 'HH:MM:SS' from iso time */ |
| 1422 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(×tamp[11], 8); |
| 1423 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1424 | /* we successfully reuse iso time, we can break */ |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | /* Failed to reuse isotime time, fallback to local legacy time */ |
| 1429 | } |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1430 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1431 | if (unlikely(time != hdr_ctx.cur_legacy_time)) { |
| 1432 | /* re-builds timestamp from the current local time */ |
| 1433 | get_localtime(time, &logtime); |
| 1434 | |
| 1435 | len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer), |
| 1436 | "%s %2d %02d:%02d:%02d ", |
| 1437 | monthname[logtime.tm_mon], |
| 1438 | logtime.tm_mday, logtime.tm_hour, logtime.tm_min, logtime.tm_sec); |
| 1439 | if (len != LOG_LEGACYTIME_LEN+1) |
| 1440 | hdr_ctx.cur_legacy_time = 0; |
| 1441 | else |
| 1442 | hdr_ctx.cur_legacy_time = time; |
| 1443 | } |
| 1444 | if (likely(hdr_ctx.cur_legacy_time)) |
| 1445 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], LOG_LEGACYTIME_LEN+1); |
| 1446 | else |
| 1447 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("Jan 1 00:00:00 ", LOG_LEGACYTIME_LEN+1); |
| 1448 | break; |
| 1449 | case LOG_FORMAT_RFC5424: |
| 1450 | /* adds rfc5425 version prefix */ |
| 1451 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("1 ", 2); |
| 1452 | if (metadata && metadata[LOG_META_TIME].len == 1 && metadata[LOG_META_TIME].ptr[0] == '-') { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1453 | /* submitted len is NILVALUE, it is a valid timestamp for rfc5425 */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1454 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME]; |
| 1455 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1456 | break; |
| 1457 | } |
| 1458 | /* let continue as 'timed' and 'iso' format for usual timestamp */ |
| 1459 | /* fall through */ |
| 1460 | case LOG_FORMAT_TIMED: |
| 1461 | case LOG_FORMAT_ISO: |
| 1462 | /* ISO format ex: '1900:01:01T12:00:00.123456Z' |
| 1463 | * '1900:01:01T14:00:00+02:00' |
| 1464 | * '1900:01:01T10:00:00.123456-02:00' |
| 1465 | */ |
| 1466 | if (metadata && metadata[LOG_META_TIME].len >= LOG_ISOTIME_MINLEN) { |
| 1467 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TIME]; |
| 1468 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1469 | /* time is set, break immediately */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1470 | break; |
| 1471 | } |
| 1472 | else if (metadata && metadata[LOG_META_TIME].len == LOG_LEGACYTIME_LEN) { |
| 1473 | int month; |
| 1474 | char *timestamp = metadata[LOG_META_TIME].ptr; |
| 1475 | |
| 1476 | for (month = 0; month < 12; month++) |
| 1477 | if (!memcmp(monthname[month], timestamp, 3)) |
| 1478 | break; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1479 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1480 | if (month < 12) { |
| 1481 | |
| 1482 | /* get local time to retrieve year */ |
| 1483 | get_localtime(time, &logtime); |
| 1484 | |
| 1485 | /* year seems changed since log */ |
| 1486 | if (logtime.tm_mon < month) |
| 1487 | logtime.tm_year--; |
| 1488 | |
| 1489 | /* builds rfc5424 prefix ex: '1900-01-01T' */ |
| 1490 | len = snprintf(hdr_ctx.timestamp_buffer, sizeof(hdr_ctx.timestamp_buffer), |
| 1491 | "%4d-%02d-%c%cT", |
| 1492 | logtime.tm_year+1900, month+1, |
| 1493 | timestamp[4] != ' ' ? timestamp[4] : '0', |
| 1494 | timestamp[5]); |
| 1495 | |
| 1496 | /* we reused the timestamp_buffer, signal that it does not |
| 1497 | * contain local time anymore |
| 1498 | */ |
| 1499 | hdr_ctx.cur_legacy_time = 0; |
| 1500 | if (len == 11) { |
| 1501 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(&hdr_ctx.timestamp_buffer[0], len); |
| 1502 | /* adds HH:MM:SS from legacy timestamp */ |
| 1503 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(×tamp[7], 8); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1504 | /* skip secfraq because it is optional */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1505 | /* according to rfc: -00:00 means we don't know the timezone */ |
| 1506 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("-00:00 ", 7); |
| 1507 | /* we successfully reuse legacy time, we can break */ |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | /* Failed to reuse legacy time, fallback to local iso time */ |
| 1512 | } |
| 1513 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(timeofday_as_iso_us(1), LOG_ISOTIME_MAXLEN + 1); |
| 1514 | break; |
| 1515 | case LOG_FORMAT_PRIO: |
| 1516 | case LOG_FORMAT_SHORT: |
| 1517 | case LOG_FORMAT_RAW: |
| 1518 | break; |
| 1519 | case LOG_FORMAT_UNSPEC: |
| 1520 | case LOG_FORMATS: |
| 1521 | ABORT_NOW(); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1524 | /* prepare other meta data, stored into a max of 10 elems */ |
| 1525 | switch (format) { |
| 1526 | case LOG_FORMAT_RFC3164: |
| 1527 | if (metadata && metadata[LOG_META_HOST].len) { |
| 1528 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST]; |
| 1529 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1530 | } |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1531 | else /* the caller MUST fill the hostname, this field is mandatory */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1532 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("localhost ", 10); |
Emeric Brun | 0237c4e | 2020-11-27 16:24:34 +0100 | [diff] [blame] | 1533 | /* fall through */ |
| 1534 | case LOG_FORMAT_LOCAL: |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1535 | if (!metadata || !metadata[LOG_META_TAG].len) |
| 1536 | break; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1537 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1538 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG]; |
| 1539 | if (metadata[LOG_META_PID].len) { |
| 1540 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("[", 1); |
| 1541 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID]; |
| 1542 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("]", 1); |
| 1543 | } |
| 1544 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(": ", 2); |
| 1545 | break; |
| 1546 | case LOG_FORMAT_RFC5424: |
| 1547 | if (metadata && metadata[LOG_META_HOST].len) { |
| 1548 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_HOST]; |
| 1549 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1550 | } |
| 1551 | else |
| 1552 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1553 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1554 | if (metadata && metadata[LOG_META_TAG].len) { |
| 1555 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_TAG]; |
| 1556 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1557 | } |
| 1558 | else |
| 1559 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1560 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1561 | if (metadata && metadata[LOG_META_PID].len) { |
| 1562 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_PID]; |
| 1563 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1564 | } |
| 1565 | else |
| 1566 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1567 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1568 | if (metadata && metadata[LOG_META_MSGID].len) { |
| 1569 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_MSGID]; |
| 1570 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1571 | } |
| 1572 | else |
| 1573 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1574 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1575 | if (metadata && metadata[LOG_META_STDATA].len) { |
| 1576 | hdr_ctx.ist_vector[(*nbelem)++] = metadata[LOG_META_STDATA]; |
| 1577 | hdr_ctx.ist_vector[(*nbelem)++] = ist2(" ", 1); |
| 1578 | } |
| 1579 | else |
| 1580 | hdr_ctx.ist_vector[(*nbelem)++] = ist2("- ", 2); |
| 1581 | break; |
| 1582 | case LOG_FORMAT_PRIO: |
| 1583 | case LOG_FORMAT_SHORT: |
| 1584 | case LOG_FORMAT_TIMED: |
| 1585 | case LOG_FORMAT_ISO: |
| 1586 | case LOG_FORMAT_RAW: |
| 1587 | break; |
| 1588 | case LOG_FORMAT_UNSPEC: |
| 1589 | case LOG_FORMATS: |
| 1590 | ABORT_NOW(); |
| 1591 | } |
| 1592 | |
| 1593 | return hdr_ctx.ist_vector; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1597 | * This function sends a syslog message to <logsrv>. |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1598 | * The argument <metadata> MUST be an array of size |
| 1599 | * LOG_META_FIELDS*sizeof(struct ist) containing data to build the header. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1600 | * 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] | 1601 | * Does not return any error, |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1602 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1603 | static inline void __do_send_log(struct logsrv *logsrv, int nblogger, int level, int facility, struct ist *metadata, char *message, size_t size) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1604 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1605 | static THREAD_LOCAL struct iovec iovec[NB_LOG_HDR_MAX_ELEMENTS+1+1] = { }; /* header elements + message + LF */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1606 | static THREAD_LOCAL struct msghdr msghdr = { |
| 1607 | //.msg_iov = iovec, |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1608 | .msg_iovlen = NB_LOG_HDR_MAX_ELEMENTS+2 |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1609 | }; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1610 | static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 1611 | static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */ |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1612 | int *plogfd; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1613 | int sent; |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1614 | size_t nbelem; |
| 1615 | struct ist *msg_header = NULL; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1616 | |
| 1617 | msghdr.msg_iov = iovec; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1618 | |
Emeric Brun | fa9d780 | 2020-05-28 14:21:33 +0200 | [diff] [blame] | 1619 | /* historically some messages used to already contain the trailing LF |
| 1620 | * or Zero. Let's remove all trailing LF or Zero |
| 1621 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1622 | while (size && (message[size-1] == '\n' || (message[size-1] == 0))) |
Emeric Brun | 9e8ea0a | 2020-05-12 19:33:15 +0200 | [diff] [blame] | 1623 | size--; |
| 1624 | |
Willy Tarreau | a5b325f | 2020-09-04 16:44:20 +0200 | [diff] [blame] | 1625 | if (logsrv->type == LOG_TARGET_BUFFER) { |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 1626 | plogfd = NULL; |
Emeric Brun | e709e1e | 2020-05-06 17:23:59 +0200 | [diff] [blame] | 1627 | goto send; |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 1628 | } |
Willy Tarreau | a5b325f | 2020-09-04 16:44:20 +0200 | [diff] [blame] | 1629 | else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) { |
| 1630 | /* the socket's address is a file descriptor */ |
| 1631 | plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr; |
| 1632 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1633 | else if (logsrv->addr.ss_family == AF_UNIX) |
| 1634 | plogfd = &logfdunix; |
| 1635 | else |
| 1636 | plogfd = &logfdinet; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1637 | |
Willy Tarreau | c046d16 | 2019-08-30 15:24:59 +0200 | [diff] [blame] | 1638 | if (plogfd && unlikely(*plogfd < 0)) { |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1639 | /* socket not successfully initialized yet */ |
| 1640 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, |
| 1641 | (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) { |
| 1642 | static char once; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1643 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1644 | if (!once) { |
| 1645 | once = 1; /* note: no need for atomic ops here */ |
| 1646 | ha_alert("socket() failed in logger #%d: %s (errno=%d)\n", |
| 1647 | nblogger, strerror(errno), errno); |
| 1648 | } |
| 1649 | return; |
| 1650 | } else { |
| 1651 | /* we don't want to receive anything on this socket */ |
| 1652 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 1653 | /* does nothing under Linux, maybe needed for others */ |
| 1654 | shutdown(*plogfd, SHUT_RD); |
| 1655 | fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC); |
| 1656 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1659 | msg_header = build_log_header(logsrv->format, level, facility, metadata, &nbelem); |
| 1660 | send: |
Willy Tarreau | a5b325f | 2020-09-04 16:44:20 +0200 | [diff] [blame] | 1661 | if (logsrv->type == LOG_TARGET_BUFFER) { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1662 | struct ist msg; |
| 1663 | |
| 1664 | msg = ist2(message, size); |
| 1665 | if (msg.len > logsrv->maxlen) |
| 1666 | msg.len = logsrv->maxlen; |
Willy Tarreau | d52a7f8 | 2019-08-30 14:05:35 +0200 | [diff] [blame] | 1667 | |
Willy Tarreau | a5b325f | 2020-09-04 16:44:20 +0200 | [diff] [blame] | 1668 | sent = sink_write(logsrv->sink, &msg, 1, level, logsrv->facility, metadata); |
| 1669 | } |
| 1670 | else if (logsrv->addr.ss_family == AF_CUST_EXISTING_FD) { |
| 1671 | struct ist msg; |
| 1672 | |
| 1673 | msg = ist2(message, size); |
| 1674 | if (msg.len > logsrv->maxlen) |
| 1675 | msg.len = logsrv->maxlen; |
| 1676 | |
| 1677 | sent = fd_write_frag_line(*plogfd, logsrv->maxlen, msg_header, nbelem, &msg, 1, 1); |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1678 | } |
| 1679 | else { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1680 | int i = 0; |
| 1681 | int totlen = logsrv->maxlen; |
| 1682 | |
| 1683 | for (i = 0 ; i < nbelem ; i++ ) { |
| 1684 | iovec[i].iov_base = msg_header[i].ptr; |
| 1685 | iovec[i].iov_len = msg_header[i].len; |
| 1686 | if (totlen <= iovec[i].iov_len) { |
| 1687 | iovec[i].iov_len = totlen; |
| 1688 | totlen = 0; |
| 1689 | break; |
| 1690 | } |
| 1691 | totlen -= iovec[i].iov_len; |
| 1692 | } |
| 1693 | if (totlen) { |
| 1694 | iovec[i].iov_base = message; |
| 1695 | iovec[i].iov_len = size; |
| 1696 | if (totlen <= iovec[i].iov_len) |
| 1697 | iovec[i].iov_len = totlen; |
| 1698 | i++; |
| 1699 | } |
| 1700 | iovec[i].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1701 | iovec[i].iov_len = 1; |
| 1702 | i++; |
Willy Tarreau | d52a7f8 | 2019-08-30 14:05:35 +0200 | [diff] [blame] | 1703 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1704 | msghdr.msg_iovlen = i; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1705 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1706 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1707 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1708 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 1709 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1710 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1711 | if (sent < 0) { |
| 1712 | static char once; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1713 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1714 | if (errno == EAGAIN) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1715 | _HA_ATOMIC_INC(&dropped_logs); |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1716 | else if (!once) { |
| 1717 | once = 1; /* note: no need for atomic ops here */ |
| 1718 | ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n", |
| 1719 | nblogger, strerror(errno), errno); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1720 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1721 | } |
| 1722 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1723 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1724 | /* |
| 1725 | * This function sends a syslog message. |
| 1726 | * It doesn't care about errors nor does it report them. |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1727 | * The argument <metadata> MUST be an array of size |
| 1728 | * LOG_META_FIELDS*sizeof(struct ist) containing |
| 1729 | * data to build the header. |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1730 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1731 | void process_send_log(struct list *logsrvs, int level, int facility, |
| 1732 | struct ist *metadata, char *message, size_t size) |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1733 | { |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1734 | struct logsrv *logsrv; |
| 1735 | int nblogger; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1736 | |
| 1737 | /* Send log messages to syslog server. */ |
| 1738 | nblogger = 0; |
| 1739 | list_for_each_entry(logsrv, logsrvs, list) { |
Emeric Brun | 2f4cc28 | 2020-07-10 15:47:11 +0200 | [diff] [blame] | 1740 | int in_range = 1; |
Frédéric Lécaille | d803e47 | 2019-04-25 07:42:09 +0200 | [diff] [blame] | 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) |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1764 | __do_send_log(logsrv, ++nblogger, MAX(level, logsrv->minlvl), |
| 1765 | (facility == -1) ? logsrv->facility : facility, |
| 1766 | metadata, message, size); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1770 | /* |
| 1771 | * This function sends a syslog message. |
| 1772 | * It doesn't care about errors nor does it report them. |
| 1773 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 1774 | * in RFC5424 formatted syslog messages. |
| 1775 | */ |
| 1776 | void __send_log(struct list *logsrvs, struct buffer *tagb, int level, |
| 1777 | char *message, size_t size, char *sd, size_t sd_size) |
| 1778 | { |
| 1779 | static THREAD_LOCAL pid_t curr_pid; |
| 1780 | static THREAD_LOCAL char pidstr[16]; |
| 1781 | static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS]; |
| 1782 | |
| 1783 | if (logsrvs == NULL) { |
| 1784 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 1785 | logsrvs = &global.logsrvs; |
| 1786 | } |
| 1787 | } |
| 1788 | if (!logsrvs || LIST_ISEMPTY(logsrvs)) |
| 1789 | return; |
| 1790 | |
| 1791 | if (!metadata[LOG_META_HOST].len) { |
| 1792 | if (global.log_send_hostname) |
| 1793 | metadata[LOG_META_HOST] = ist2(global.log_send_hostname, strlen(global.log_send_hostname)); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | if (!tagb || !tagb->area) |
| 1797 | tagb = &global.log_tag; |
| 1798 | |
| 1799 | if (tagb) |
| 1800 | metadata[LOG_META_TAG] = ist2(tagb->area, tagb->data); |
| 1801 | |
| 1802 | if (unlikely(curr_pid != getpid())) |
| 1803 | metadata[LOG_META_PID].len = 0; |
| 1804 | |
| 1805 | if (!metadata[LOG_META_PID].len) { |
| 1806 | curr_pid = getpid(); |
| 1807 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 1808 | metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr)); |
| 1809 | } |
| 1810 | |
| 1811 | metadata[LOG_META_STDATA] = ist2(sd, sd_size); |
| 1812 | |
| 1813 | /* Remove trailing space of structured data */ |
| 1814 | while (metadata[LOG_META_STDATA].len && metadata[LOG_META_STDATA].ptr[metadata[LOG_META_STDATA].len-1] == ' ') |
| 1815 | metadata[LOG_META_STDATA].len--; |
| 1816 | |
| 1817 | return process_send_log(logsrvs, level, -1, metadata, message, size); |
| 1818 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1819 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1820 | 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] | 1821 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1822 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1823 | Set-cookie Updated, unknown, unknown */ |
| 1824 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1825 | /* |
| 1826 | * try to write a character if there is enough space, or goto out |
| 1827 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1828 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1829 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1830 | *(tmplog++) = (x); \ |
| 1831 | } else { \ |
| 1832 | goto out; \ |
| 1833 | } \ |
| 1834 | } while(0) |
| 1835 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1837 | /* Initializes some log data at boot */ |
| 1838 | static void init_log() |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1839 | { |
| 1840 | char *tmp; |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1841 | int i; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1842 | |
| 1843 | /* Initialize the escape map for the RFC5424 structured-data : '"\]' |
| 1844 | * inside PARAM-VALUE should be escaped with '\' as prefix. |
| 1845 | * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more |
| 1846 | * details. |
| 1847 | */ |
| 1848 | memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map)); |
| 1849 | |
| 1850 | tmp = "\"\\]"; |
| 1851 | while (*tmp) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1852 | ha_bit_set(*tmp, rfc5424_escape_map); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1853 | tmp++; |
| 1854 | } |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1855 | |
| 1856 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 1857 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 1858 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 1859 | * printable characters above. |
| 1860 | */ |
| 1861 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 1862 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 1863 | for (i = 0; i < 32; i++) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1864 | ha_bit_set(i, hdr_encode_map); |
| 1865 | ha_bit_set(i, url_encode_map); |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1866 | } |
| 1867 | for (i = 127; i < 256; i++) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1868 | ha_bit_set(i, hdr_encode_map); |
| 1869 | ha_bit_set(i, url_encode_map); |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | tmp = "\"#{|}"; |
| 1873 | while (*tmp) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1874 | ha_bit_set(*tmp, hdr_encode_map); |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1875 | tmp++; |
| 1876 | } |
| 1877 | |
| 1878 | tmp = "\"#"; |
| 1879 | while (*tmp) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1880 | ha_bit_set(*tmp, url_encode_map); |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1881 | tmp++; |
| 1882 | } |
| 1883 | |
| 1884 | /* initialize the http header encoding map. The draft httpbis define the |
| 1885 | * header content as: |
| 1886 | * |
| 1887 | * HTTP-message = start-line |
| 1888 | * *( header-field CRLF ) |
| 1889 | * CRLF |
| 1890 | * [ message-body ] |
| 1891 | * header-field = field-name ":" OWS field-value OWS |
| 1892 | * field-value = *( field-content / obs-fold ) |
| 1893 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 1894 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 1895 | * field-vchar = VCHAR / obs-text |
| 1896 | * VCHAR = %x21-7E |
| 1897 | * obs-text = %x80-FF |
| 1898 | * |
| 1899 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 1900 | * 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] | 1901 | * "obs-fold" is voluntarily forgotten because haproxy remove this. |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1902 | */ |
| 1903 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 1904 | for (i = 0x00; i <= 0x08; i++) |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1905 | ha_bit_set(i, http_encode_map); |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1906 | for (i = 0x0a; i <= 0x1f; i++) |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1907 | ha_bit_set(i, http_encode_map); |
| 1908 | ha_bit_set(0x7f, http_encode_map); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1909 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1910 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1911 | INITCALL0(STG_PREPARE, init_log); |
| 1912 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1913 | /* Initialize log buffers used for syslog messages */ |
| 1914 | int init_log_buffers() |
| 1915 | { |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1916 | logline = my_realloc2(logline, global.max_syslog_len + 1); |
| 1917 | logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1918 | if (!logline || !logline_rfc5424) |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1919 | return 0; |
| 1920 | return 1; |
| 1921 | } |
| 1922 | |
| 1923 | /* Deinitialize log buffers used for syslog messages */ |
| 1924 | void deinit_log_buffers() |
| 1925 | { |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1926 | free(logline); |
| 1927 | free(logline_rfc5424); |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1928 | logline = NULL; |
| 1929 | logline_rfc5424 = NULL; |
| 1930 | } |
| 1931 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1932 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1933 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1934 | * 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] | 1935 | * is not zero. It requires a valid session and optionally a stream. If the |
| 1936 | * stream is NULL, default values will be assumed for the stream part. |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1937 | */ |
Willy Tarreau | 43c538e | 2018-09-05 14:58:15 +0200 | [diff] [blame] | 1938 | 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] | 1939 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1940 | struct proxy *fe = sess->fe; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1941 | struct proxy *be; |
| 1942 | struct http_txn *txn; |
| 1943 | const struct strm_logs *logs; |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 1944 | struct connection *fe_conn, *be_conn; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1945 | unsigned int s_flags; |
| 1946 | unsigned int uniq_id; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1947 | struct buffer chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1948 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1949 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1950 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1951 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1952 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1953 | int t_request; |
| 1954 | int hdr; |
| 1955 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1956 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1957 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1958 | char *ret; |
| 1959 | int iret; |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 1960 | int status; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1961 | struct logformat_node *tmp; |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1962 | struct timeval tv; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1963 | struct strm_logs tmp_strm_log; |
Maciej Zdeb | fcdfd85 | 2020-11-30 18:27:47 +0000 | [diff] [blame] | 1964 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1965 | struct http_uri_parser parser; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1966 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1967 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1968 | |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1969 | if (likely(s)) { |
| 1970 | be = s->be; |
| 1971 | txn = s->txn; |
| 1972 | be_conn = cs_conn(objt_cs(s->si[1].end)); |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 1973 | status = (txn ? txn->status : 0); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1974 | s_flags = s->flags; |
| 1975 | uniq_id = s->uniq_id; |
| 1976 | logs = &s->logs; |
| 1977 | } else { |
| 1978 | /* we have no stream so we first need to initialize a few |
| 1979 | * things that are needed later. We do increment the request |
| 1980 | * ID so that it's uniquely assigned to this request just as |
| 1981 | * if the request had reached the point of being processed. |
| 1982 | * A request error is reported as it's the only element we have |
| 1983 | * here and which justifies emitting such a log. |
| 1984 | */ |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 1985 | be = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? __objt_check(sess->origin)->proxy : fe); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1986 | txn = NULL; |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 1987 | fe_conn = objt_conn(sess->origin); |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 1988 | be_conn = ((obj_type(sess->origin) == OBJ_TYPE_CHECK) ? cs_conn(__objt_check(sess->origin)->cs) : NULL); |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 1989 | status = 0; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1990 | s_flags = SF_ERR_PRXCOND | SF_FINST_R; |
Willy Tarreau | 1851572 | 2021-04-06 11:57:41 +0200 | [diff] [blame] | 1991 | uniq_id = _HA_ATOMIC_FETCH_ADD(&global.req_count, 1); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1992 | |
| 1993 | /* prepare a valid log structure */ |
| 1994 | tmp_strm_log.tv_accept = sess->tv_accept; |
| 1995 | tmp_strm_log.accept_date = sess->accept_date; |
| 1996 | tmp_strm_log.t_handshake = sess->t_handshake; |
Christopher Faulet | dd78921 | 2020-09-30 15:10:07 +0200 | [diff] [blame] | 1997 | tmp_strm_log.t_idle = (sess->t_idle >= 0 ? sess->t_idle : 0); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1998 | tv_zero(&tmp_strm_log.tv_request); |
| 1999 | tmp_strm_log.t_queue = -1; |
| 2000 | tmp_strm_log.t_connect = -1; |
| 2001 | tmp_strm_log.t_data = -1; |
| 2002 | tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now); |
| 2003 | tmp_strm_log.bytes_in = 0; |
| 2004 | tmp_strm_log.bytes_out = 0; |
| 2005 | tmp_strm_log.prx_queue_pos = 0; |
| 2006 | tmp_strm_log.srv_queue_pos = 0; |
| 2007 | |
| 2008 | logs = &tmp_strm_log; |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 2009 | |
| 2010 | if ((fe->mode == PR_MODE_HTTP) && fe_conn && fe_conn->mux && fe_conn->mux->ctl) { |
| 2011 | enum mux_exit_status es = fe_conn->mux->ctl(fe_conn, MUX_EXIT_STATUS, NULL); |
| 2012 | |
| 2013 | switch (es) { |
| 2014 | case MUX_ES_SUCCESS: |
| 2015 | break; |
| 2016 | case MUX_ES_INVALID_ERR: |
| 2017 | status = 400; |
| 2018 | if ((fe_conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(fe_conn)) |
| 2019 | s_flags = SF_ERR_CLICL | SF_FINST_R; |
| 2020 | else |
| 2021 | s_flags = SF_ERR_PRXCOND | SF_FINST_R; |
| 2022 | break; |
| 2023 | case MUX_ES_TOUT_ERR: |
| 2024 | status = 408; |
| 2025 | s_flags = SF_ERR_CLITO | SF_FINST_R; |
| 2026 | break; |
Christopher Faulet | 142dd33 | 2020-12-07 11:24:37 +0100 | [diff] [blame] | 2027 | case MUX_ES_NOTIMPL_ERR: |
| 2028 | status = 501; |
| 2029 | s_flags = SF_ERR_PRXCOND | SF_FINST_R; |
| 2030 | break; |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 2031 | case MUX_ES_INTERNAL_ERR: |
| 2032 | status = 500; |
| 2033 | s_flags = SF_ERR_INTERNAL | SF_FINST_R; |
| 2034 | break; |
| 2035 | default: |
| 2036 | break; |
| 2037 | } |
| 2038 | } |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 2039 | } |
| 2040 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2041 | t_request = -1; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2042 | if (tv_isge(&logs->tv_request, &logs->tv_accept)) |
| 2043 | t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2044 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2045 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 2046 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2047 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2048 | if (LIST_ISEMPTY(list_format)) |
| 2049 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2050 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2051 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2052 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 2053 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2054 | struct sample *key; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2055 | const struct buffer empty = { }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2056 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2057 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2058 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2059 | if (!last_isspace) { |
| 2060 | LOGCHAR(' '); |
| 2061 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2062 | } |
| 2063 | break; |
| 2064 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2065 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2066 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2067 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2068 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2069 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2070 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2071 | last_isspace = 0; |
| 2072 | break; |
| 2073 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2074 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 2075 | key = NULL; |
Christopher Faulet | 5f94070 | 2020-04-06 10:40:02 +0200 | [diff] [blame] | 2076 | if (tmp->options & LOG_OPT_REQ_CAP) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 2077 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Christopher Faulet | 5f94070 | 2020-04-06 10:40:02 +0200 | [diff] [blame] | 2078 | if (!key && (tmp->options & LOG_OPT_RES_CAP)) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 2079 | 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] | 2080 | if (tmp->options & LOG_OPT_HTTP) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2081 | ret = lf_encode_chunk(tmplog, dst + maxsize, |
| 2082 | '%', http_encode_map, key ? &key->data.u.str : &empty, tmp); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 2083 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2084 | ret = lf_text_len(tmplog, |
| 2085 | key ? key->data.u.str.area : NULL, |
| 2086 | key ? key->data.u.str.data : 0, |
| 2087 | dst + maxsize - tmplog, |
| 2088 | tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 2089 | if (ret == 0) |
| 2090 | goto out; |
| 2091 | tmplog = ret; |
| 2092 | last_isspace = 0; |
| 2093 | break; |
| 2094 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2095 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2096 | conn = objt_conn(sess->origin); |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2097 | if (conn && conn_get_src(conn)) |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2098 | ret = lf_ip(tmplog, (struct sockaddr *)conn->src, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2099 | else |
| 2100 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2101 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2102 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2103 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2104 | last_isspace = 0; |
| 2105 | break; |
| 2106 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2107 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2108 | conn = objt_conn(sess->origin); |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2109 | if (conn && conn_get_src(conn)) { |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2110 | if (conn->src->ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2111 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2112 | } else { |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2113 | ret = lf_port(tmplog, (struct sockaddr *)conn->src, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2114 | dst + maxsize - tmplog, tmp); |
| 2115 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2116 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2117 | else |
| 2118 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2119 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2120 | if (ret == NULL) |
| 2121 | goto out; |
| 2122 | tmplog = ret; |
| 2123 | last_isspace = 0; |
| 2124 | break; |
| 2125 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2126 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2127 | conn = objt_conn(sess->origin); |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2128 | if (conn && conn_get_dst(conn)) { |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2129 | ret = lf_ip(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2130 | } |
| 2131 | else |
| 2132 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2133 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2134 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2135 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2136 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2137 | last_isspace = 0; |
| 2138 | break; |
| 2139 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2140 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2141 | conn = objt_conn(sess->origin); |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2142 | if (conn && conn_get_dst(conn)) { |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2143 | if (conn->dst->ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2144 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2145 | else |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2146 | ret = lf_port(tmplog, (struct sockaddr *)conn->dst, dst + maxsize - tmplog, tmp); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2147 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2148 | else |
| 2149 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2150 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2151 | if (ret == NULL) |
| 2152 | goto out; |
| 2153 | tmplog = ret; |
| 2154 | last_isspace = 0; |
| 2155 | break; |
| 2156 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2157 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2158 | if (be_conn && conn_get_src(be_conn)) |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2159 | ret = lf_ip(tmplog, (const struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2160 | else |
| 2161 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2162 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2163 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2164 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2165 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2166 | last_isspace = 0; |
| 2167 | break; |
| 2168 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2169 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2170 | if (be_conn && conn_get_src(be_conn)) |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2171 | ret = lf_port(tmplog, (struct sockaddr *)be_conn->src, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2172 | else |
| 2173 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2174 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2175 | if (ret == NULL) |
| 2176 | goto out; |
| 2177 | tmplog = ret; |
| 2178 | last_isspace = 0; |
| 2179 | break; |
| 2180 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2181 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2182 | if (be_conn && conn_get_dst(be_conn)) |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2183 | ret = lf_ip(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2184 | else |
| 2185 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2186 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2187 | if (ret == NULL) |
| 2188 | goto out; |
| 2189 | tmplog = ret; |
| 2190 | last_isspace = 0; |
| 2191 | break; |
| 2192 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2193 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 8fa9984 | 2019-07-17 11:47:11 +0200 | [diff] [blame] | 2194 | if (be_conn && conn_get_dst(be_conn)) |
Willy Tarreau | 6c6365f | 2019-07-17 16:48:18 +0200 | [diff] [blame] | 2195 | ret = lf_port(tmplog, (struct sockaddr *)be_conn->dst, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2196 | else |
| 2197 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 2198 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2199 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2200 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2201 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 2202 | last_isspace = 0; |
| 2203 | break; |
| 2204 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2205 | case LOG_FMT_DATE: // %t = accept date |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2206 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 2207 | ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2208 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2209 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2210 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2211 | last_isspace = 0; |
| 2212 | break; |
| 2213 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2214 | case LOG_FMT_tr: // %tr = start of request date |
| 2215 | /* Note that the timers are valid if we get here */ |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2216 | 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] | 2217 | get_localtime(tv.tv_sec, &tm); |
| 2218 | ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog); |
| 2219 | if (ret == NULL) |
| 2220 | goto out; |
| 2221 | tmplog = ret; |
| 2222 | last_isspace = 0; |
| 2223 | break; |
| 2224 | |
| 2225 | case LOG_FMT_DATEGMT: // %T = accept date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2226 | get_gmtime(logs->accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2227 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2228 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2229 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2230 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2231 | last_isspace = 0; |
| 2232 | break; |
| 2233 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2234 | case LOG_FMT_trg: // %trg = start of request date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2235 | 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] | 2236 | get_gmtime(tv.tv_sec, &tm); |
| 2237 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 2238 | if (ret == NULL) |
| 2239 | goto out; |
| 2240 | tmplog = ret; |
| 2241 | last_isspace = 0; |
| 2242 | break; |
| 2243 | |
| 2244 | case LOG_FMT_DATELOCAL: // %Tl = accept date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2245 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 2246 | 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] | 2247 | if (ret == NULL) |
| 2248 | goto out; |
| 2249 | tmplog = ret; |
| 2250 | last_isspace = 0; |
| 2251 | break; |
| 2252 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2253 | case LOG_FMT_trl: // %trl = start of request date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2254 | 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] | 2255 | get_localtime(tv.tv_sec, &tm); |
| 2256 | ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog); |
| 2257 | if (ret == NULL) |
| 2258 | goto out; |
| 2259 | tmplog = ret; |
| 2260 | last_isspace = 0; |
| 2261 | break; |
| 2262 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2263 | case LOG_FMT_TS: // %Ts |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2264 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2265 | 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] | 2266 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2267 | goto out; |
| 2268 | last_isspace = 0; |
| 2269 | tmplog += iret; |
| 2270 | } else { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2271 | ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2272 | if (ret == NULL) |
| 2273 | goto out; |
| 2274 | tmplog = ret; |
| 2275 | last_isspace = 0; |
| 2276 | } |
| 2277 | break; |
| 2278 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2279 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2280 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2281 | 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] | 2282 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2283 | goto out; |
| 2284 | last_isspace = 0; |
| 2285 | tmplog += iret; |
| 2286 | } else { |
| 2287 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2288 | goto out; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2289 | ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000, |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2290 | tmplog, 4); |
| 2291 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2292 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2293 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2294 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2295 | } |
| 2296 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2297 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2298 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2299 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2300 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2301 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2302 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2303 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2304 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2305 | break; |
| 2306 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2307 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 2308 | src = fe->id; |
| 2309 | if (tmp->options & LOG_OPT_QUOTE) |
| 2310 | LOGCHAR('"'); |
| 2311 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 2312 | if (iret == 0) |
| 2313 | goto out; |
| 2314 | tmplog += iret; |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 2315 | if (sess->listener && sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL)) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2316 | LOGCHAR('~'); |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2317 | if (tmp->options & LOG_OPT_QUOTE) |
| 2318 | LOGCHAR('"'); |
| 2319 | last_isspace = 0; |
| 2320 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2321 | #ifdef USE_OPENSSL |
| 2322 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 2323 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2324 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2325 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2326 | src = ssl_sock_get_cipher_name(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2327 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2328 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2329 | if (ret == NULL) |
| 2330 | goto out; |
| 2331 | tmplog = ret; |
| 2332 | last_isspace = 0; |
| 2333 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2334 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2335 | case LOG_FMT_SSL_VERSION: // %sslv |
| 2336 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2337 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2338 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2339 | src = ssl_sock_get_proto_version(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2340 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2341 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2342 | if (ret == NULL) |
| 2343 | goto out; |
| 2344 | tmplog = ret; |
| 2345 | last_isspace = 0; |
| 2346 | break; |
| 2347 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2348 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2349 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2350 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2351 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2352 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2353 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2354 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2355 | break; |
| 2356 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2357 | case LOG_FMT_SERVER: // %s |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 2358 | switch (obj_type(s ? s->target : sess->origin)) { |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2359 | case OBJ_TYPE_SERVER: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2360 | src = __objt_server(s->target)->id; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2361 | break; |
| 2362 | case OBJ_TYPE_APPLET: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2363 | src = __objt_applet(s->target)->name; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2364 | break; |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 2365 | case OBJ_TYPE_CHECK: |
| 2366 | src = (__objt_check(sess->origin)->server |
| 2367 | ? __objt_check(sess->origin)->server->id |
| 2368 | : "<NOSRV>"); |
| 2369 | break; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2370 | default: |
| 2371 | src = "<NOSRV>"; |
| 2372 | break; |
| 2373 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2374 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2375 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2376 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2377 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2378 | last_isspace = 0; |
| 2379 | break; |
| 2380 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2381 | case LOG_FMT_Th: // %Th = handshake time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2382 | ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2383 | if (ret == NULL) |
| 2384 | goto out; |
| 2385 | tmplog = ret; |
| 2386 | last_isspace = 0; |
| 2387 | break; |
| 2388 | |
| 2389 | case LOG_FMT_Ti: // %Ti = HTTP idle time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2390 | ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2391 | if (ret == NULL) |
| 2392 | goto out; |
| 2393 | tmplog = ret; |
| 2394 | last_isspace = 0; |
| 2395 | break; |
| 2396 | |
| 2397 | case LOG_FMT_TR: // %TR = HTTP request time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2398 | 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] | 2399 | tmplog, dst + maxsize - tmplog); |
| 2400 | if (ret == NULL) |
| 2401 | goto out; |
| 2402 | tmplog = ret; |
| 2403 | last_isspace = 0; |
| 2404 | break; |
| 2405 | |
| 2406 | case LOG_FMT_TQ: // %Tq = Th + Ti + TR |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2407 | ret = ltoa_o(t_request, 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 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2414 | case LOG_FMT_TW: // %Tw |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2415 | 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] | 2416 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2417 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2418 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2419 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2420 | last_isspace = 0; |
| 2421 | break; |
| 2422 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2423 | case LOG_FMT_TC: // %Tc |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2424 | 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] | 2425 | 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 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2432 | case LOG_FMT_Tr: // %Tr |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2433 | 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] | 2434 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2435 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2436 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2437 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2438 | last_isspace = 0; |
| 2439 | break; |
| 2440 | |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2441 | case LOG_FMT_TD: // %Td |
Willy Tarreau | a21c0e6 | 2018-09-05 15:07:15 +0200 | [diff] [blame] | 2442 | if (be->mode == PR_MODE_HTTP) |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2443 | 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] | 2444 | tmplog, dst + maxsize - tmplog); |
| 2445 | else |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2446 | 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] | 2447 | tmplog, dst + maxsize - tmplog); |
| 2448 | if (ret == NULL) |
| 2449 | goto out; |
| 2450 | tmplog = ret; |
| 2451 | last_isspace = 0; |
| 2452 | break; |
| 2453 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2454 | case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti |
| 2455 | if (!(fe->to_log & LW_BYTES)) |
| 2456 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2457 | 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] | 2458 | tmplog, dst + maxsize - tmplog); |
| 2459 | if (ret == NULL) |
| 2460 | goto out; |
| 2461 | tmplog = ret; |
| 2462 | last_isspace = 0; |
| 2463 | break; |
| 2464 | |
| 2465 | case LOG_FMT_TT: // %Tt = total time |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2466 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2467 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2468 | ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2469 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2470 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2471 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2472 | last_isspace = 0; |
| 2473 | break; |
| 2474 | |
Damien Claisse | 57c8eb9 | 2020-04-28 12:09:19 +0000 | [diff] [blame] | 2475 | case LOG_FMT_TU: // %Tu = total time seen by user = Tt - Ti |
| 2476 | if (!(fe->to_log & LW_BYTES)) |
| 2477 | LOGCHAR('+'); |
| 2478 | ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle : 0), |
| 2479 | tmplog, dst + maxsize - tmplog); |
| 2480 | if (ret == NULL) |
| 2481 | goto out; |
| 2482 | tmplog = ret; |
| 2483 | last_isspace = 0; |
| 2484 | break; |
| 2485 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2486 | case LOG_FMT_STATUS: // %ST |
Christopher Faulet | ce5e6bc | 2020-10-06 15:11:43 +0200 | [diff] [blame] | 2487 | ret = ltoa_o(status, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2488 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2489 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2490 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2491 | last_isspace = 0; |
| 2492 | break; |
| 2493 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2494 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2495 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2496 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2497 | ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2498 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2499 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2500 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2501 | last_isspace = 0; |
| 2502 | break; |
| 2503 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2504 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2505 | ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2506 | if (ret == NULL) |
| 2507 | goto out; |
| 2508 | tmplog = ret; |
| 2509 | last_isspace = 0; |
| 2510 | break; |
| 2511 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2512 | case LOG_FMT_CCLIENT: // %CC |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2513 | src = txn ? txn->cli_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2514 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2515 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2516 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2517 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2518 | last_isspace = 0; |
| 2519 | break; |
| 2520 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2521 | case LOG_FMT_CSERVER: // %CS |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2522 | src = txn ? txn->srv_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2523 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2524 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2525 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2526 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2527 | last_isspace = 0; |
| 2528 | break; |
| 2529 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2530 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2531 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2532 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 2533 | *tmplog = '\0'; |
| 2534 | last_isspace = 0; |
| 2535 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2536 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2537 | 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] | 2538 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2539 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2540 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 2541 | 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] | 2542 | last_isspace = 0; |
| 2543 | break; |
| 2544 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2545 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2546 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2547 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2548 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2549 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2550 | last_isspace = 0; |
| 2551 | break; |
| 2552 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2553 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2554 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2555 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2556 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2557 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2558 | last_isspace = 0; |
| 2559 | break; |
| 2560 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2561 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2562 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2563 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2564 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2565 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2566 | last_isspace = 0; |
| 2567 | break; |
| 2568 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2569 | case LOG_FMT_SRVCONN: // %sc |
Christopher Faulet | fd81848 | 2021-04-14 14:01:41 +0200 | [diff] [blame] | 2570 | switch (obj_type(s ? s->target : sess->origin)) { |
| 2571 | case OBJ_TYPE_SERVER: |
| 2572 | ret = ultoa_o(__objt_server(s->target)->cur_sess, |
| 2573 | tmplog, dst + maxsize - tmplog); |
| 2574 | break; |
| 2575 | case OBJ_TYPE_CHECK: |
| 2576 | ret = ultoa_o(__objt_check(sess->origin)->server |
| 2577 | ? __objt_check(sess->origin)->server->cur_sess |
| 2578 | : 0, tmplog, dst + maxsize - tmplog); |
| 2579 | break; |
| 2580 | default: |
| 2581 | ret = ultoa_o(0, tmplog, dst + maxsize - tmplog); |
| 2582 | break; |
| 2583 | } |
| 2584 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2585 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2586 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2587 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2588 | last_isspace = 0; |
| 2589 | break; |
| 2590 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2591 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2592 | if (s_flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2593 | LOGCHAR('+'); |
Christopher Faulet | 1d26f22 | 2021-04-16 11:24:20 +0200 | [diff] [blame] | 2594 | ret = ltoa_o(((s && s->si[1].conn_retries > 0) |
| 2595 | ? (be->conn_retries - s->si[1].conn_retries) |
| 2596 | : ((s && s->si[1].state != SI_ST_INI) ? be->conn_retries : 0)), |
| 2597 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2598 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2599 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2600 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2601 | last_isspace = 0; |
| 2602 | break; |
| 2603 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2604 | case LOG_FMT_SRVQUEUE: // %sq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2605 | ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2606 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2607 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2608 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2609 | last_isspace = 0; |
| 2610 | break; |
| 2611 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2612 | case LOG_FMT_BCKQUEUE: // %bq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2613 | ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2614 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2615 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2616 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2617 | last_isspace = 0; |
| 2618 | break; |
| 2619 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2620 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2621 | /* request header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2622 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2623 | if (tmp->options & LOG_OPT_QUOTE) |
| 2624 | LOGCHAR('"'); |
| 2625 | LOGCHAR('{'); |
| 2626 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2627 | if (hdr) |
| 2628 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2629 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2630 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2631 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2632 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2633 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2634 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2635 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2636 | } |
| 2637 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2638 | if (tmp->options & LOG_OPT_QUOTE) |
| 2639 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2640 | last_isspace = 0; |
| 2641 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2642 | break; |
| 2643 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2644 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2645 | /* request header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2646 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2647 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2648 | if (hdr > 0) |
| 2649 | LOGCHAR(' '); |
| 2650 | if (tmp->options & LOG_OPT_QUOTE) |
| 2651 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2652 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2653 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2654 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2655 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2656 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2657 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2658 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2659 | LOGCHAR('-'); |
| 2660 | if (tmp->options & LOG_OPT_QUOTE) |
| 2661 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2662 | last_isspace = 0; |
| 2663 | } |
| 2664 | } |
| 2665 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2666 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2667 | |
| 2668 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2669 | /* response header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2670 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2671 | if (tmp->options & LOG_OPT_QUOTE) |
| 2672 | LOGCHAR('"'); |
| 2673 | LOGCHAR('{'); |
| 2674 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2675 | if (hdr) |
| 2676 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2677 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2678 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2679 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2680 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2681 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2682 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2683 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2684 | } |
| 2685 | LOGCHAR('}'); |
| 2686 | last_isspace = 0; |
| 2687 | if (tmp->options & LOG_OPT_QUOTE) |
| 2688 | LOGCHAR('"'); |
| 2689 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2690 | break; |
| 2691 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2692 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2693 | /* response header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2694 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2695 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2696 | if (hdr > 0) |
| 2697 | LOGCHAR(' '); |
| 2698 | if (tmp->options & LOG_OPT_QUOTE) |
| 2699 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2700 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2701 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2702 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2703 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2704 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2705 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2706 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2707 | LOGCHAR('-'); |
| 2708 | if (tmp->options & LOG_OPT_QUOTE) |
| 2709 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2710 | last_isspace = 0; |
| 2711 | } |
| 2712 | } |
| 2713 | break; |
| 2714 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2715 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2716 | /* Request */ |
| 2717 | if (tmp->options & LOG_OPT_QUOTE) |
| 2718 | LOGCHAR('"'); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2719 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2720 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2721 | '#', url_encode_map, uri, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2722 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2723 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2724 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2725 | if (tmp->options & LOG_OPT_QUOTE) |
| 2726 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2727 | last_isspace = 0; |
| 2728 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2729 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2730 | case LOG_FMT_HTTP_PATH: // %HP |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2731 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2732 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2733 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2734 | LOGCHAR('"'); |
| 2735 | |
| 2736 | end = uri + strlen(uri); |
| 2737 | // look for the first whitespace character |
| 2738 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2739 | uri++; |
| 2740 | |
| 2741 | // keep advancing past multiple spaces |
| 2742 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2743 | uri++; nspaces++; |
| 2744 | } |
| 2745 | |
| 2746 | // look for first space or question mark after url |
| 2747 | spc = uri; |
| 2748 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 2749 | spc++; |
| 2750 | |
Nenad Merdanovic | 54e439f | 2016-04-26 01:39:02 +0200 | [diff] [blame] | 2751 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2752 | chunk.area = "<BADREQ>"; |
| 2753 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2754 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2755 | chunk.area = uri; |
| 2756 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2759 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2760 | if (ret == NULL || *ret != '\0') |
| 2761 | goto out; |
| 2762 | |
| 2763 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2764 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2765 | LOGCHAR('"'); |
| 2766 | |
| 2767 | last_isspace = 0; |
| 2768 | break; |
| 2769 | |
Maciej Zdeb | fcdfd85 | 2020-11-30 18:27:47 +0000 | [diff] [blame] | 2770 | case LOG_FMT_HTTP_PATH_ONLY: // %HPO |
| 2771 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
| 2772 | |
| 2773 | if (tmp->options & LOG_OPT_QUOTE) |
| 2774 | LOGCHAR('"'); |
| 2775 | |
| 2776 | end = uri + strlen(uri); |
| 2777 | |
| 2778 | // look for the first whitespace character |
| 2779 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2780 | uri++; |
| 2781 | |
| 2782 | // keep advancing past multiple spaces |
| 2783 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2784 | uri++; nspaces++; |
| 2785 | } |
| 2786 | |
| 2787 | // look for first space after url |
| 2788 | spc = uri; |
| 2789 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2790 | spc++; |
| 2791 | |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 2792 | path = ist2(uri, spc - uri); |
Maciej Zdeb | fcdfd85 | 2020-11-30 18:27:47 +0000 | [diff] [blame] | 2793 | |
| 2794 | // extract relative path without query params from url |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2795 | parser = http_uri_parser_init(path); |
| 2796 | path = iststop(http_parse_path(&parser), '?'); |
Maciej Zdeb | fcdfd85 | 2020-11-30 18:27:47 +0000 | [diff] [blame] | 2797 | if (!txn || !txn->uri || nspaces == 0) { |
| 2798 | chunk.area = "<BADREQ>"; |
| 2799 | chunk.data = strlen("<BADREQ>"); |
| 2800 | } else { |
| 2801 | chunk.area = path.ptr; |
| 2802 | chunk.data = path.len; |
| 2803 | } |
| 2804 | |
| 2805 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
| 2806 | if (ret == NULL || *ret != '\0') |
| 2807 | goto out; |
| 2808 | |
| 2809 | tmplog = ret; |
| 2810 | if (tmp->options & LOG_OPT_QUOTE) |
| 2811 | LOGCHAR('"'); |
| 2812 | |
| 2813 | last_isspace = 0; |
| 2814 | break; |
| 2815 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2816 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 2817 | if (tmp->options & LOG_OPT_QUOTE) |
| 2818 | LOGCHAR('"'); |
| 2819 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2820 | if (!txn || !txn->uri) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2821 | chunk.area = "<BADREQ>"; |
| 2822 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2823 | } else { |
| 2824 | uri = txn->uri; |
| 2825 | end = uri + strlen(uri); |
| 2826 | // look for the first question mark |
| 2827 | while (uri < end && *uri != '?') |
| 2828 | uri++; |
| 2829 | |
| 2830 | qmark = uri; |
| 2831 | // look for first space or question mark after url |
| 2832 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2833 | uri++; |
| 2834 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2835 | chunk.area = qmark; |
| 2836 | chunk.data = uri - qmark; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2839 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2840 | if (ret == NULL || *ret != '\0') |
| 2841 | goto out; |
| 2842 | |
| 2843 | tmplog = ret; |
| 2844 | if (tmp->options & LOG_OPT_QUOTE) |
| 2845 | LOGCHAR('"'); |
| 2846 | |
| 2847 | last_isspace = 0; |
| 2848 | break; |
| 2849 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2850 | case LOG_FMT_HTTP_URI: // %HU |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2851 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2852 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2853 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2854 | LOGCHAR('"'); |
| 2855 | |
| 2856 | end = uri + strlen(uri); |
| 2857 | // look for the first whitespace character |
| 2858 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2859 | uri++; |
| 2860 | |
| 2861 | // keep advancing past multiple spaces |
| 2862 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2863 | uri++; nspaces++; |
| 2864 | } |
| 2865 | |
| 2866 | // look for first space after url |
| 2867 | spc = uri; |
| 2868 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2869 | spc++; |
| 2870 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2871 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2872 | chunk.area = "<BADREQ>"; |
| 2873 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2874 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2875 | chunk.area = uri; |
| 2876 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2879 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2880 | if (ret == NULL || *ret != '\0') |
| 2881 | goto out; |
| 2882 | |
| 2883 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2884 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2885 | LOGCHAR('"'); |
| 2886 | |
| 2887 | last_isspace = 0; |
| 2888 | break; |
| 2889 | |
| 2890 | case LOG_FMT_HTTP_METHOD: // %HM |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2891 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2892 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2893 | LOGCHAR('"'); |
| 2894 | |
| 2895 | end = uri + strlen(uri); |
| 2896 | // look for the first whitespace character |
| 2897 | spc = uri; |
| 2898 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2899 | spc++; |
| 2900 | |
| 2901 | 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] | 2902 | chunk.area = "<BADREQ>"; |
| 2903 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2904 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2905 | chunk.area = uri; |
| 2906 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2909 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2910 | if (ret == NULL || *ret != '\0') |
| 2911 | goto out; |
| 2912 | |
| 2913 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2914 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2915 | LOGCHAR('"'); |
| 2916 | |
| 2917 | last_isspace = 0; |
| 2918 | break; |
| 2919 | |
| 2920 | case LOG_FMT_HTTP_VERSION: // %HV |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2921 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2922 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2923 | LOGCHAR('"'); |
| 2924 | |
| 2925 | end = uri + strlen(uri); |
| 2926 | // look for the first whitespace character |
| 2927 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2928 | uri++; |
| 2929 | |
| 2930 | // keep advancing past multiple spaces |
| 2931 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2932 | uri++; nspaces++; |
| 2933 | } |
| 2934 | |
| 2935 | // look for the next whitespace character |
| 2936 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2937 | uri++; |
| 2938 | |
| 2939 | // keep advancing past multiple spaces |
| 2940 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 2941 | uri++; |
| 2942 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2943 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2944 | chunk.area = "<BADREQ>"; |
| 2945 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2946 | } else if (uri == end) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2947 | chunk.area = "HTTP/0.9"; |
| 2948 | chunk.data = strlen("HTTP/0.9"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2949 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2950 | chunk.area = uri; |
| 2951 | chunk.data = end - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2954 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2955 | if (ret == NULL || *ret != '\0') |
| 2956 | goto out; |
| 2957 | |
| 2958 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2959 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2960 | LOGCHAR('"'); |
| 2961 | |
| 2962 | last_isspace = 0; |
| 2963 | break; |
| 2964 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2965 | case LOG_FMT_COUNTER: // %rt |
| 2966 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2967 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2968 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2969 | goto out; |
| 2970 | last_isspace = 0; |
| 2971 | tmplog += iret; |
| 2972 | } else { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2973 | ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2974 | if (ret == NULL) |
| 2975 | goto out; |
| 2976 | tmplog = ret; |
| 2977 | last_isspace = 0; |
| 2978 | } |
| 2979 | break; |
| 2980 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2981 | case LOG_FMT_LOGCNT: // %lc |
| 2982 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2983 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2984 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2985 | goto out; |
| 2986 | last_isspace = 0; |
| 2987 | tmplog += iret; |
| 2988 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2989 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2990 | if (ret == NULL) |
| 2991 | goto out; |
| 2992 | tmplog = ret; |
| 2993 | last_isspace = 0; |
| 2994 | } |
| 2995 | break; |
| 2996 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2997 | case LOG_FMT_HOSTNAME: // %H |
| 2998 | src = hostname; |
| 2999 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 3000 | if (ret == NULL) |
| 3001 | goto out; |
| 3002 | tmplog = ret; |
| 3003 | last_isspace = 0; |
| 3004 | break; |
| 3005 | |
| 3006 | case LOG_FMT_PID: // %pid |
| 3007 | if (tmp->options & LOG_OPT_HEXA) { |
| 3008 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 3009 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 3010 | goto out; |
| 3011 | last_isspace = 0; |
| 3012 | tmplog += iret; |
| 3013 | } else { |
| 3014 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 3015 | if (ret == NULL) |
| 3016 | goto out; |
| 3017 | tmplog = ret; |
| 3018 | last_isspace = 0; |
| 3019 | } |
| 3020 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3021 | |
| 3022 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3023 | ret = NULL; |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 3024 | if (s) |
| 3025 | ret = lf_text_len(tmplog, s->unique_id.ptr, s->unique_id.len, maxsize - (tmplog - dst), tmp); |
| 3026 | else |
| 3027 | ret = lf_text_len(tmplog, NULL, 0, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3028 | if (ret == NULL) |
| 3029 | goto out; |
| 3030 | tmplog = ret; |
| 3031 | last_isspace = 0; |
| 3032 | break; |
| 3033 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3038 | /* *tmplog is a unused character */ |
| 3039 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 3040 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 3041 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3044 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3045 | * 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] | 3046 | * Will not log if the frontend has no log defined. |
| 3047 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3048 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3049 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3050 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3051 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 3052 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3053 | |
| 3054 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3055 | err = (s->flags & SF_REDISP) || |
| 3056 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 3057 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3058 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3059 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3061 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3062 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 3063 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3064 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3065 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 3066 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 3067 | if (s->logs.level) { /* loglevel was overridden */ |
| 3068 | if (s->logs.level == -1) { |
| 3069 | s->logs.logwait = 0; /* logs disabled */ |
| 3070 | return; |
| 3071 | } |
| 3072 | level = s->logs.level - 1; |
| 3073 | } |
| 3074 | else { |
| 3075 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3076 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 3077 | level = LOG_ERR; |
| 3078 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3079 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3080 | /* if unique-id was not generated */ |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 3081 | if (!isttest(s->unique_id) && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 3082 | stream_generate_unique_id(s, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3083 | } |
| 3084 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 3085 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 3086 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 3087 | &sess->fe->logformat_sd); |
| 3088 | } |
| 3089 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 3090 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3091 | if (size > 0) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3092 | _HA_ATOMIC_INC(&sess->fe->log_count); |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 3093 | __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level, |
| 3094 | logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 3095 | s->logs.logwait = 0; |
| 3096 | } |
| 3097 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 3098 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 3099 | /* |
| 3100 | * send a minimalist log for the session. Will not log if the frontend has no |
| 3101 | * log defined. It is assumed that this is only used to report anomalies that |
| 3102 | * cannot lead to the creation of a regular stream. Because of this the log |
| 3103 | * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting |
| 3104 | * 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] | 3105 | * function to report unimportant events. It is safe to call this function with |
| 3106 | * sess==NULL (will not do anything). |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 3107 | */ |
| 3108 | void sess_log(struct session *sess) |
| 3109 | { |
| 3110 | int size, level; |
| 3111 | int sd_size = 0; |
| 3112 | |
Willy Tarreau | 9fa267d | 2018-10-05 10:22:27 +0200 | [diff] [blame] | 3113 | if (!sess) |
| 3114 | return; |
| 3115 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 3116 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
| 3117 | return; |
| 3118 | |
| 3119 | level = LOG_INFO; |
| 3120 | if (sess->fe->options2 & PR_O2_LOGERRORS) |
| 3121 | level = LOG_ERR; |
| 3122 | |
| 3123 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 3124 | sd_size = sess_build_logline(sess, NULL, |
| 3125 | logline_rfc5424, global.max_syslog_len, |
| 3126 | &sess->fe->logformat_sd); |
| 3127 | } |
| 3128 | |
| 3129 | size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat); |
| 3130 | if (size > 0) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3131 | _HA_ATOMIC_INC(&sess->fe->log_count); |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 3132 | __send_log(&sess->fe->logsrvs, &sess->fe->log_tag, level, |
| 3133 | logline, size + 1, logline_rfc5424, sd_size); |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 3134 | } |
| 3135 | } |
| 3136 | |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 3137 | void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...) |
| 3138 | { |
| 3139 | va_list argp; |
| 3140 | int data_len; |
| 3141 | |
| 3142 | if (level < 0 || format == NULL || logline == NULL) |
| 3143 | return; |
| 3144 | |
| 3145 | va_start(argp, format); |
| 3146 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
| 3147 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 3148 | data_len = global.max_syslog_len; |
| 3149 | va_end(argp); |
| 3150 | |
| 3151 | __send_log(logsrvs, tag, level, logline, data_len, default_rfc5424_sd_log_format, 2); |
| 3152 | } |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3153 | /* |
| 3154 | * This function parse a received log message <buf>, of size <buflen> |
| 3155 | * it fills <level>, <facility> and <metadata> depending of the detected |
| 3156 | * header format and message will point on remaining payload of <size> |
| 3157 | * |
| 3158 | * <metadata> must point on a preallocated array of LOG_META_FIELDS*sizeof(struct ist) |
| 3159 | * struct ist len will be set to 0 if field is not found |
| 3160 | * <level> and <facility> will be set to -1 if not found. |
| 3161 | */ |
| 3162 | void parse_log_message(char *buf, size_t buflen, int *level, int *facility, |
| 3163 | struct ist *metadata, char **message, size_t *size) |
| 3164 | { |
| 3165 | |
| 3166 | char *p; |
| 3167 | int fac_level = 0; |
| 3168 | |
| 3169 | *level = *facility = -1; |
| 3170 | |
| 3171 | *message = buf; |
| 3172 | *size = buflen; |
| 3173 | |
| 3174 | memset(metadata, 0, LOG_META_FIELDS*sizeof(struct ist)); |
| 3175 | |
| 3176 | p = buf; |
| 3177 | if (*size < 2 || *p != '<') |
| 3178 | return; |
| 3179 | |
| 3180 | p++; |
| 3181 | while (*p != '>') { |
| 3182 | if (*p > '9' || *p < '0') |
| 3183 | return; |
| 3184 | fac_level = 10*fac_level + (*p - '0'); |
| 3185 | p++; |
| 3186 | if ((p - buf) > buflen) |
| 3187 | return; |
| 3188 | } |
| 3189 | |
| 3190 | *facility = fac_level >> 3; |
| 3191 | *level = fac_level & 0x7; |
| 3192 | p++; |
| 3193 | |
| 3194 | metadata[LOG_META_PRIO] = ist2(buf, p - buf); |
| 3195 | |
| 3196 | buflen -= p - buf; |
| 3197 | buf = p; |
| 3198 | |
| 3199 | *size = buflen; |
| 3200 | *message = buf; |
| 3201 | |
| 3202 | /* for rfc5424, prio is always followed by '1' and ' ' */ |
| 3203 | if ((*size > 2) && (p[0] == '1') && (p[1] == ' ')) { |
| 3204 | /* format is always '1 TIMESTAMP HOSTNAME TAG PID MSGID STDATA ' |
| 3205 | * followed by message. |
| 3206 | * Each header field can present NILVALUE: '-' |
| 3207 | */ |
| 3208 | |
| 3209 | p += 2; |
| 3210 | /* timestamp is NILVALUE '-' */ |
| 3211 | if (*size > 2 && (p[0] == '-') && p[1] == ' ') { |
| 3212 | metadata[LOG_META_TIME] = ist2(p, 1); |
| 3213 | p++; |
| 3214 | } |
| 3215 | else if (*size > LOG_ISOTIME_MINLEN) { |
| 3216 | metadata[LOG_META_TIME].ptr = p; |
| 3217 | |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 3218 | /* check if optional secfrac is present |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3219 | * in timestamp. |
| 3220 | * possible format are: |
| 3221 | * ex: '1970-01-01T00:00:00.000000Z' |
| 3222 | * '1970-01-01T00:00:00.000000+00:00' |
| 3223 | * '1970-01-01T00:00:00.000000-00:00' |
| 3224 | * '1970-01-01T00:00:00Z' |
| 3225 | * '1970-01-01T00:00:00+00:00' |
| 3226 | * '1970-01-01T00:00:00-00:00' |
| 3227 | */ |
| 3228 | p += 19; |
| 3229 | if (*p == '.') { |
| 3230 | p++; |
| 3231 | if ((p - buf) >= buflen) |
| 3232 | goto bad_format; |
| 3233 | while (*p != 'Z' && *p != '+' && *p != '-') { |
| 3234 | if ((unsigned char)(*p - '0') > 9) |
| 3235 | goto bad_format; |
| 3236 | |
| 3237 | p++; |
| 3238 | if ((p - buf) >= buflen) |
| 3239 | goto bad_format; |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | if (*p == 'Z') |
| 3244 | p++; |
| 3245 | else |
| 3246 | p += 6; /* case of '+00:00 or '-00:00' */ |
| 3247 | |
| 3248 | if ((p - buf) >= buflen || *p != ' ') |
| 3249 | goto bad_format; |
| 3250 | metadata[LOG_META_TIME].len = p - metadata[LOG_META_TIME].ptr; |
| 3251 | } |
| 3252 | else |
| 3253 | goto bad_format; |
| 3254 | |
| 3255 | |
| 3256 | p++; |
| 3257 | if ((p - buf) >= buflen || *p == ' ') |
| 3258 | goto bad_format; |
| 3259 | |
| 3260 | metadata[LOG_META_HOST].ptr = p; |
| 3261 | while (*p != ' ') { |
| 3262 | p++; |
| 3263 | if ((p - buf) >= buflen) |
| 3264 | goto bad_format; |
| 3265 | } |
| 3266 | metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr; |
| 3267 | if (metadata[LOG_META_HOST].len == 1 && metadata[LOG_META_HOST].ptr[0] == '-') |
| 3268 | metadata[LOG_META_HOST].len = 0; |
| 3269 | |
| 3270 | p++; |
| 3271 | if ((p - buf) >= buflen || *p == ' ') |
| 3272 | goto bad_format; |
| 3273 | |
| 3274 | metadata[LOG_META_TAG].ptr = p; |
| 3275 | while (*p != ' ') { |
| 3276 | p++; |
| 3277 | if ((p - buf) >= buflen) |
| 3278 | goto bad_format; |
| 3279 | } |
| 3280 | metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr; |
| 3281 | if (metadata[LOG_META_TAG].len == 1 && metadata[LOG_META_TAG].ptr[0] == '-') |
| 3282 | metadata[LOG_META_TAG].len = 0; |
| 3283 | |
| 3284 | p++; |
| 3285 | if ((p - buf) >= buflen || *p == ' ') |
| 3286 | goto bad_format; |
| 3287 | |
| 3288 | metadata[LOG_META_PID].ptr = p; |
| 3289 | while (*p != ' ') { |
| 3290 | p++; |
| 3291 | if ((p - buf) >= buflen) |
| 3292 | goto bad_format; |
| 3293 | } |
| 3294 | metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr; |
| 3295 | if (metadata[LOG_META_PID].len == 1 && metadata[LOG_META_PID].ptr[0] == '-') |
| 3296 | metadata[LOG_META_PID].len = 0; |
| 3297 | |
| 3298 | p++; |
| 3299 | if ((p - buf) >= buflen || *p == ' ') |
| 3300 | goto bad_format; |
| 3301 | |
| 3302 | metadata[LOG_META_MSGID].ptr = p; |
| 3303 | while (*p != ' ') { |
| 3304 | p++; |
| 3305 | if ((p - buf) >= buflen) |
| 3306 | goto bad_format; |
| 3307 | } |
| 3308 | metadata[LOG_META_MSGID].len = p - metadata[LOG_META_MSGID].ptr; |
| 3309 | if (metadata[LOG_META_MSGID].len == 1 && metadata[LOG_META_MSGID].ptr[0] == '-') |
| 3310 | metadata[LOG_META_MSGID].len = 0; |
| 3311 | |
| 3312 | p++; |
| 3313 | if ((p - buf) >= buflen || *p == ' ') |
| 3314 | goto bad_format; |
| 3315 | |
| 3316 | /* structured data format is: |
| 3317 | * ex: |
| 3318 | * '[key1=value1 key2=value2][key3=value3]' |
| 3319 | * |
| 3320 | * space is invalid outside [] because |
| 3321 | * considered as the end of structured data field |
| 3322 | */ |
| 3323 | metadata[LOG_META_STDATA].ptr = p; |
| 3324 | if (*p == '[') { |
| 3325 | int elem = 0; |
| 3326 | |
| 3327 | while (1) { |
| 3328 | if (elem) { |
| 3329 | /* according to rfc this char is escaped in param values */ |
| 3330 | if (*p == ']' && *(p-1) != '\\') |
| 3331 | elem = 0; |
| 3332 | } |
| 3333 | else { |
| 3334 | if (*p == '[') |
| 3335 | elem = 1; |
| 3336 | else if (*p == ' ') |
| 3337 | break; |
| 3338 | else |
| 3339 | goto bad_format; |
| 3340 | } |
| 3341 | p++; |
| 3342 | if ((p - buf) >= buflen) |
| 3343 | goto bad_format; |
| 3344 | } |
| 3345 | } |
| 3346 | else if (*p == '-') { |
| 3347 | /* case of NILVALUE */ |
| 3348 | p++; |
| 3349 | if ((p - buf) >= buflen || *p != ' ') |
| 3350 | goto bad_format; |
| 3351 | } |
| 3352 | else |
| 3353 | goto bad_format; |
| 3354 | |
| 3355 | metadata[LOG_META_STDATA].len = p - metadata[LOG_META_STDATA].ptr; |
| 3356 | if (metadata[LOG_META_STDATA].len == 1 && metadata[LOG_META_STDATA].ptr[0] == '-') |
| 3357 | metadata[LOG_META_STDATA].len = 0; |
| 3358 | |
| 3359 | p++; |
| 3360 | |
| 3361 | buflen -= p - buf; |
| 3362 | buf = p; |
| 3363 | |
| 3364 | *size = buflen; |
| 3365 | *message = p; |
| 3366 | } |
| 3367 | else if (*size > LOG_LEGACYTIME_LEN) { |
| 3368 | int m; |
| 3369 | |
| 3370 | /* supported header format according to rfc3164. |
| 3371 | * ex: |
| 3372 | * 'Jan 1 00:00:00 HOSTNAME TAG[PID]: ' |
| 3373 | * or 'Jan 1 00:00:00 HOSTNAME TAG: ' |
| 3374 | * or 'Jan 1 00:00:00 HOSTNAME ' |
| 3375 | * Note: HOSTNAME is mandatory, and day |
| 3376 | * of month uses a single space prefix if |
| 3377 | * less than 10 to ensure hour offset is |
| 3378 | * always the same. |
| 3379 | */ |
| 3380 | |
| 3381 | /* Check month to see if it correspond to a rfc3164 |
| 3382 | * header ex 'Jan 1 00:00:00' */ |
| 3383 | for (m = 0; m < 12; m++) |
| 3384 | if (!memcmp(monthname[m], p, 3)) |
| 3385 | break; |
| 3386 | /* Month not found */ |
| 3387 | if (m == 12) |
| 3388 | goto bad_format; |
| 3389 | |
| 3390 | metadata[LOG_META_TIME] = ist2(p, LOG_LEGACYTIME_LEN); |
| 3391 | |
| 3392 | p += LOG_LEGACYTIME_LEN; |
| 3393 | if ((p - buf) >= buflen || *p != ' ') |
| 3394 | goto bad_format; |
| 3395 | |
| 3396 | p++; |
| 3397 | if ((p - buf) >= buflen || *p == ' ') |
| 3398 | goto bad_format; |
| 3399 | |
| 3400 | metadata[LOG_META_HOST].ptr = p; |
| 3401 | while (*p != ' ') { |
| 3402 | p++; |
| 3403 | if ((p - buf) >= buflen) |
| 3404 | goto bad_format; |
| 3405 | } |
| 3406 | metadata[LOG_META_HOST].len = p - metadata[LOG_META_HOST].ptr; |
| 3407 | |
| 3408 | /* TAG seems to no be mandatory */ |
| 3409 | p++; |
| 3410 | |
| 3411 | buflen -= p - buf; |
| 3412 | buf = p; |
| 3413 | |
| 3414 | *size = buflen; |
| 3415 | *message = buf; |
| 3416 | |
| 3417 | if (!buflen) |
| 3418 | return; |
| 3419 | |
| 3420 | while (((p - buf) < buflen) && *p != ' ' && *p != ':') |
| 3421 | p++; |
| 3422 | |
| 3423 | /* a tag must present a trailing ':' */ |
| 3424 | if (((p - buf) >= buflen) || *p != ':') |
| 3425 | return; |
| 3426 | p++; |
| 3427 | /* followed by a space */ |
| 3428 | if (((p - buf) >= buflen) || *p != ' ') |
| 3429 | return; |
| 3430 | |
| 3431 | /* rewind to parse tag and pid */ |
| 3432 | p = buf; |
| 3433 | metadata[LOG_META_TAG].ptr = p; |
| 3434 | /* we have the guarantee that ':' will be reach before size limit */ |
| 3435 | while (*p != ':') { |
| 3436 | if (*p == '[') { |
| 3437 | metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr; |
| 3438 | metadata[LOG_META_PID].ptr = p + 1; |
| 3439 | } |
Tim Duesterhus | 7b5777d | 2021-03-02 18:57:28 +0100 | [diff] [blame] | 3440 | else if (*p == ']' && isttest(metadata[LOG_META_PID])) { |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3441 | if (p[1] != ':') |
| 3442 | return; |
| 3443 | metadata[LOG_META_PID].len = p - metadata[LOG_META_PID].ptr; |
| 3444 | } |
| 3445 | p++; |
| 3446 | } |
| 3447 | if (!metadata[LOG_META_TAG].len) |
| 3448 | metadata[LOG_META_TAG].len = p - metadata[LOG_META_TAG].ptr; |
| 3449 | |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 3450 | /* let pass ':' and ' ', we still have warranty size is large enough */ |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3451 | p += 2; |
| 3452 | |
| 3453 | buflen -= p - buf; |
| 3454 | buf = p; |
| 3455 | |
| 3456 | *size = buflen; |
| 3457 | *message = buf; |
| 3458 | } |
| 3459 | |
| 3460 | return; |
| 3461 | |
| 3462 | bad_format: |
| 3463 | /* bad syslog format, we reset all parsed syslog fields |
| 3464 | * but priority is kept because we are able to re-build |
| 3465 | * this message using LOF_FORMAT_PRIO. |
| 3466 | */ |
| 3467 | metadata[LOG_META_TIME].len = 0; |
| 3468 | metadata[LOG_META_HOST].len = 0; |
| 3469 | metadata[LOG_META_TAG].len = 0; |
| 3470 | metadata[LOG_META_PID].len = 0; |
| 3471 | metadata[LOG_META_MSGID].len = 0; |
| 3472 | metadata[LOG_META_STDATA].len = 0; |
| 3473 | |
| 3474 | return; |
| 3475 | } |
| 3476 | |
| 3477 | /* |
| 3478 | * UDP syslog fd handler |
| 3479 | */ |
| 3480 | void syslog_fd_handler(int fd) |
| 3481 | { |
| 3482 | static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS]; |
| 3483 | ssize_t ret = 0; |
| 3484 | struct buffer *buf = get_trash_chunk(); |
| 3485 | size_t size; |
| 3486 | char *message; |
| 3487 | int level; |
| 3488 | int facility; |
| 3489 | struct listener *l = objt_listener(fdtab[fd].owner); |
| 3490 | int max_accept; |
| 3491 | |
| 3492 | if(!l) |
| 3493 | ABORT_NOW(); |
| 3494 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 3495 | if (fdtab[fd].state & FD_POLL_IN) { |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3496 | |
| 3497 | if (!fd_recv_ready(fd)) |
| 3498 | return; |
| 3499 | |
| 3500 | max_accept = l->maxaccept ? l->maxaccept : 1; |
| 3501 | |
| 3502 | do { |
| 3503 | /* Source address */ |
| 3504 | struct sockaddr_storage saddr = {0}; |
| 3505 | socklen_t saddrlen; |
| 3506 | |
| 3507 | saddrlen = sizeof(saddr); |
| 3508 | |
| 3509 | ret = recvfrom(fd, buf->area, buf->size, 0, (struct sockaddr *)&saddr, &saddrlen); |
| 3510 | if (ret < 0) { |
| 3511 | if (errno == EINTR) |
| 3512 | continue; |
| 3513 | if (errno == EAGAIN) |
| 3514 | fd_cant_recv(fd); |
| 3515 | goto out; |
| 3516 | } |
| 3517 | buf->data = ret; |
| 3518 | |
Emeric Brun | 45c457a | 2020-07-09 23:23:34 +0200 | [diff] [blame] | 3519 | /* update counters */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3520 | _HA_ATOMIC_INC(&cum_log_messages); |
Emeric Brun | a39ecbd | 2020-10-05 14:33:12 +0200 | [diff] [blame] | 3521 | proxy_inc_fe_req_ctr(l, l->bind_conf->frontend); |
Emeric Brun | 45c457a | 2020-07-09 23:23:34 +0200 | [diff] [blame] | 3522 | |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 3523 | parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size); |
| 3524 | |
| 3525 | process_send_log(&l->bind_conf->frontend->logsrvs, level, facility, metadata, message, size); |
| 3526 | |
| 3527 | } while (--max_accept); |
| 3528 | } |
| 3529 | |
| 3530 | out: |
| 3531 | return; |
| 3532 | } |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 3533 | |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3534 | /* |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3535 | * IO Handler to handle message exchange with a syslog tcp client |
| 3536 | */ |
| 3537 | static void syslog_io_handler(struct appctx *appctx) |
| 3538 | { |
| 3539 | static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS]; |
| 3540 | struct stream_interface *si = appctx->owner; |
| 3541 | struct stream *s = si_strm(si); |
| 3542 | struct proxy *frontend = strm_fe(s); |
| 3543 | struct listener *l = strm_li(s); |
| 3544 | struct buffer *buf = get_trash_chunk(); |
| 3545 | int max_accept; |
| 3546 | int to_skip; |
| 3547 | int facility; |
| 3548 | int level; |
| 3549 | char *message; |
| 3550 | size_t size; |
| 3551 | |
| 3552 | max_accept = l->maxaccept ? l->maxaccept : 1; |
| 3553 | while (co_data(si_oc(si))) { |
| 3554 | char c; |
| 3555 | |
| 3556 | if (max_accept <= 0) |
| 3557 | goto missing_budget; |
| 3558 | max_accept--; |
| 3559 | |
| 3560 | to_skip = co_getchar(si_oc(si), &c); |
| 3561 | if (!to_skip) |
| 3562 | goto missing_data; |
| 3563 | else if (to_skip < 0) |
| 3564 | goto cli_abort; |
| 3565 | |
| 3566 | if (c == '<') { |
| 3567 | /* rfc-6587, Non-Transparent-Framing: messages separated by |
| 3568 | * a trailing LF or CR LF |
| 3569 | */ |
| 3570 | to_skip = co_getline(si_oc(si), buf->area, buf->size); |
| 3571 | if (!to_skip) |
| 3572 | goto missing_data; |
| 3573 | else if (to_skip < 0) |
| 3574 | goto cli_abort; |
| 3575 | |
| 3576 | if (buf->area[to_skip - 1] != '\n') |
| 3577 | goto parse_error; |
| 3578 | |
| 3579 | buf->data = to_skip - 1; |
| 3580 | |
| 3581 | /* according to rfc-6587, some devices adds CR before LF */ |
| 3582 | if (buf->data && buf->area[buf->data - 1] == '\r') |
| 3583 | buf->data--; |
| 3584 | |
| 3585 | } |
| 3586 | else if ((unsigned char)(c - '1') <= 8) { |
| 3587 | /* rfc-6587, Octet-Counting: message length in ASCII |
| 3588 | * (first digit can not be ZERO), followed by a space |
| 3589 | * and message length |
| 3590 | */ |
| 3591 | char *p = NULL; |
| 3592 | int msglen; |
| 3593 | |
| 3594 | to_skip = co_getword(si_oc(si), buf->area, buf->size, ' '); |
| 3595 | if (!to_skip) |
| 3596 | goto missing_data; |
| 3597 | else if (to_skip < 0) |
| 3598 | goto cli_abort; |
| 3599 | |
| 3600 | if (buf->area[to_skip - 1] != ' ') |
| 3601 | goto parse_error; |
| 3602 | |
| 3603 | msglen = strtol(trash.area, &p, 10); |
| 3604 | if (!msglen || p != &buf->area[to_skip - 1]) |
| 3605 | goto parse_error; |
| 3606 | |
| 3607 | /* message seems too large */ |
| 3608 | if (msglen > buf->size) |
| 3609 | goto parse_error; |
| 3610 | |
| 3611 | msglen = co_getblk(si_oc(si), buf->area, msglen, to_skip); |
| 3612 | if (!msglen) |
| 3613 | goto missing_data; |
| 3614 | else if (msglen < 0) |
| 3615 | goto cli_abort; |
| 3616 | |
| 3617 | |
| 3618 | buf->data = msglen; |
| 3619 | to_skip += msglen; |
| 3620 | } |
| 3621 | else |
| 3622 | goto parse_error; |
| 3623 | |
| 3624 | co_skip(si_oc(si), to_skip); |
| 3625 | |
| 3626 | /* update counters */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3627 | _HA_ATOMIC_INC(&cum_log_messages); |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3628 | proxy_inc_fe_req_ctr(l, frontend); |
| 3629 | |
| 3630 | parse_log_message(buf->area, buf->data, &level, &facility, metadata, &message, &size); |
| 3631 | |
| 3632 | process_send_log(&frontend->logsrvs, level, facility, metadata, message, size); |
| 3633 | |
| 3634 | } |
| 3635 | |
| 3636 | missing_data: |
| 3637 | /* we need more data to read */ |
| 3638 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
| 3639 | |
| 3640 | return; |
| 3641 | |
| 3642 | missing_budget: |
| 3643 | /* it may remain some stuff to do, let's retry later */ |
| 3644 | appctx_wakeup(appctx); |
| 3645 | |
| 3646 | return; |
| 3647 | |
| 3648 | parse_error: |
| 3649 | if (l->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3650 | _HA_ATOMIC_INC(&l->counters->failed_req); |
| 3651 | _HA_ATOMIC_INC(&frontend->fe_counters.failed_req); |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3652 | |
| 3653 | goto close; |
| 3654 | |
| 3655 | cli_abort: |
| 3656 | if (l->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 3657 | _HA_ATOMIC_INC(&l->counters->cli_aborts); |
| 3658 | _HA_ATOMIC_INC(&frontend->fe_counters.cli_aborts); |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3659 | |
| 3660 | close: |
| 3661 | si_shutw(si); |
| 3662 | si_shutr(si); |
| 3663 | |
| 3664 | si_ic(si)->flags |= CF_READ_NULL; |
| 3665 | |
| 3666 | return; |
| 3667 | } |
| 3668 | |
| 3669 | static struct applet syslog_applet = { |
| 3670 | .obj_type = OBJ_TYPE_APPLET, |
| 3671 | .name = "<SYSLOG>", /* used for logging */ |
| 3672 | .fct = syslog_io_handler, |
| 3673 | .release = NULL, |
| 3674 | }; |
| 3675 | |
| 3676 | /* |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3677 | * Parse "log-forward" section and create corresponding sink buffer. |
| 3678 | * |
| 3679 | * The function returns 0 in success case, otherwise, it returns error |
| 3680 | * flags. |
| 3681 | */ |
| 3682 | int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm) |
| 3683 | { |
| 3684 | int err_code = 0; |
| 3685 | struct proxy *px; |
| 3686 | char *errmsg = NULL; |
| 3687 | const char *err = NULL; |
| 3688 | |
| 3689 | if (strcmp(args[0], "log-forward") == 0) { |
| 3690 | if (!*args[1]) { |
| 3691 | ha_alert("parsing [%s:%d] : missing name for ip-forward section.\n", file, linenum); |
| 3692 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3693 | goto out; |
| 3694 | } |
| 3695 | |
| 3696 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3697 | goto out; |
| 3698 | |
| 3699 | err = invalid_char(args[1]); |
| 3700 | if (err) { |
| 3701 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3702 | file, linenum, *err, args[0], args[1]); |
| 3703 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3704 | goto out; |
| 3705 | } |
| 3706 | |
Emeric Brun | b0c331f | 2020-10-07 17:05:59 +0200 | [diff] [blame] | 3707 | px = log_forward_by_name(args[1]); |
| 3708 | if (px) { |
| 3709 | ha_alert("Parsing [%s:%d]: log-forward section '%s' has the same name as another log-forward section declared at %s:%d.\n", |
| 3710 | file, linenum, args[1], px->conf.file, px->conf.line); |
| 3711 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3712 | } |
| 3713 | |
| 3714 | px = proxy_find_by_name(args[1], 0, 0); |
| 3715 | if (px) { |
| 3716 | ha_alert("Parsing [%s:%d]: log forward section '%s' has the same name as %s '%s' declared at %s:%d.\n", |
| 3717 | file, linenum, args[1], proxy_type_str(px), |
| 3718 | px->id, px->conf.file, px->conf.line); |
| 3719 | err_code |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | px = calloc(1, sizeof *px); |
| 3723 | if (!px) { |
| 3724 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3725 | goto out; |
| 3726 | } |
| 3727 | |
| 3728 | px->next = cfg_log_forward; |
| 3729 | cfg_log_forward = px; |
| 3730 | |
| 3731 | init_new_proxy(px); |
| 3732 | px->conf.file = strdup(file); |
| 3733 | px->conf.line = linenum; |
| 3734 | px->mode = PR_MODE_SYSLOG; |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3735 | px->last_change = now.tv_sec; |
| 3736 | px->cap = PR_CAP_FE; |
| 3737 | px->maxconn = 10; |
| 3738 | px->timeout.client = TICK_ETERNITY; |
| 3739 | px->accept = frontend_accept; |
| 3740 | px->default_target = &syslog_applet.obj_type; |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3741 | px->id = strdup(args[1]); |
| 3742 | |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3743 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3744 | else if (strcmp(args[0], "maxconn") == 0) { /* maxconn */ |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3745 | if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], " Maybe you want 'fullconn' instead ?")) |
| 3746 | err_code |= ERR_WARN; |
| 3747 | |
| 3748 | if (*(args[1]) == 0) { |
| 3749 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 3750 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3751 | goto out; |
| 3752 | } |
| 3753 | cfg_log_forward->maxconn = atol(args[1]); |
| 3754 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3755 | goto out; |
| 3756 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3757 | else if (strcmp(args[0], "backlog") == 0) { /* backlog */ |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3758 | if (warnifnotcap(cfg_log_forward, PR_CAP_FE, file, linenum, args[0], NULL)) |
| 3759 | err_code |= ERR_WARN; |
| 3760 | |
| 3761 | if (*(args[1]) == 0) { |
| 3762 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 3763 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3764 | goto out; |
| 3765 | } |
| 3766 | cfg_log_forward->backlog = atol(args[1]); |
| 3767 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3768 | goto out; |
| 3769 | } |
| 3770 | else if (strcmp(args[0], "bind") == 0) { |
| 3771 | int cur_arg; |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3772 | struct bind_conf *bind_conf; |
| 3773 | struct bind_kw *kw; |
| 3774 | struct listener *l; |
| 3775 | |
| 3776 | cur_arg = 1; |
| 3777 | |
| 3778 | bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum, |
| 3779 | NULL, xprt_get(XPRT_RAW)); |
| 3780 | if (!bind_conf) { |
| 3781 | ha_alert("parsing [%s:%d] : out of memory error.", file, linenum); |
| 3782 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3783 | goto out; |
| 3784 | } |
| 3785 | |
| 3786 | if (!str2listener(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) { |
| 3787 | if (errmsg && *errmsg) { |
| 3788 | indent_msg(&errmsg, 2); |
| 3789 | ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 3790 | } |
| 3791 | else { |
| 3792 | ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n", |
| 3793 | file, linenum, args[0], args[1], args[2]); |
| 3794 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3795 | goto out; |
| 3796 | } |
| 3797 | } |
| 3798 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
Willy Tarreau | 6616132 | 2021-02-19 15:50:27 +0100 | [diff] [blame] | 3799 | l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT; |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3800 | l->accept = session_accept_fd; |
| 3801 | l->analysers |= cfg_log_forward->fe_req_ana; |
| 3802 | l->default_target = cfg_log_forward->default_target; |
| 3803 | global.maxsock++; |
| 3804 | } |
| 3805 | cur_arg++; |
| 3806 | |
| 3807 | while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) { |
| 3808 | int ret; |
| 3809 | |
| 3810 | ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg); |
| 3811 | err_code |= ret; |
| 3812 | if (ret) { |
| 3813 | if (errmsg && *errmsg) { |
| 3814 | indent_msg(&errmsg, 2); |
| 3815 | ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg); |
| 3816 | } |
| 3817 | else |
| 3818 | ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n", |
| 3819 | file, linenum, args[cur_arg]); |
| 3820 | if (ret & ERR_FATAL) |
| 3821 | goto out; |
| 3822 | } |
| 3823 | cur_arg += 1 + kw->skip; |
| 3824 | } |
| 3825 | if (*args[cur_arg] != 0) { |
Willy Tarreau | 433b05f | 2021-03-12 10:14:07 +0100 | [diff] [blame] | 3826 | const char *best = bind_find_best_kw(args[cur_arg]); |
| 3827 | if (best) |
| 3828 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n", |
| 3829 | file, linenum, args[cur_arg], cursection, best); |
| 3830 | else |
| 3831 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n", |
| 3832 | file, linenum, args[cur_arg], cursection); |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3833 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3834 | goto out; |
| 3835 | } |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3836 | } |
Willy Tarreau | 76aaa7f | 2020-09-16 15:07:22 +0200 | [diff] [blame] | 3837 | else if (strcmp(args[0], "dgram-bind") == 0) { |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3838 | int cur_arg; |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3839 | struct bind_conf *bind_conf; |
| 3840 | struct bind_kw *kw; |
| 3841 | struct listener *l; |
| 3842 | |
| 3843 | cur_arg = 1; |
| 3844 | |
| 3845 | bind_conf = bind_conf_alloc(cfg_log_forward, file, linenum, |
| 3846 | NULL, xprt_get(XPRT_RAW)); |
Christopher Faulet | 0c6d1dc | 2021-04-12 16:56:37 +0200 | [diff] [blame] | 3847 | if (!bind_conf) { |
| 3848 | ha_alert("parsing [%s:%d] : out of memory error.", file, linenum); |
| 3849 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3850 | goto out; |
| 3851 | } |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3852 | |
Willy Tarreau | 26ff5da | 2020-09-16 15:22:19 +0200 | [diff] [blame] | 3853 | if (!str2receiver(args[1], cfg_log_forward, bind_conf, file, linenum, &errmsg)) { |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3854 | if (errmsg && *errmsg) { |
| 3855 | indent_msg(&errmsg, 2); |
| 3856 | ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 3857 | } |
| 3858 | else { |
| 3859 | ha_alert("parsing [%s:%d] : '%s %s' : error encountered while parsing listening address %s.\n", |
| 3860 | file, linenum, args[0], args[1], args[2]); |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3861 | } |
Willy Tarreau | 3b139e5 | 2020-09-16 16:24:14 +0200 | [diff] [blame] | 3862 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3863 | goto out; |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3864 | } |
| 3865 | list_for_each_entry(l, &bind_conf->listeners, by_bind) { |
Willy Tarreau | 26ff5da | 2020-09-16 15:22:19 +0200 | [diff] [blame] | 3866 | /* the fact that the sockets are of type dgram is guaranteed by str2receiver() */ |
Willy Tarreau | 6616132 | 2021-02-19 15:50:27 +0100 | [diff] [blame] | 3867 | l->maxaccept = global.tune.maxaccept ? global.tune.maxaccept : MAX_ACCEPT; |
Willy Tarreau | e140a69 | 2020-10-15 21:25:32 +0200 | [diff] [blame] | 3868 | l->rx.iocb = syslog_fd_handler; |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3869 | global.maxsock++; |
| 3870 | } |
| 3871 | cur_arg++; |
| 3872 | |
| 3873 | while (*args[cur_arg] && (kw = bind_find_kw(args[cur_arg]))) { |
| 3874 | int ret; |
| 3875 | |
| 3876 | ret = kw->parse(args, cur_arg, cfg_log_forward, bind_conf, &errmsg); |
| 3877 | err_code |= ret; |
| 3878 | if (ret) { |
| 3879 | if (errmsg && *errmsg) { |
| 3880 | indent_msg(&errmsg, 2); |
| 3881 | ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg); |
| 3882 | } |
| 3883 | else |
| 3884 | ha_alert("parsing [%s:%d]: error encountered while processing '%s'\n", |
| 3885 | file, linenum, args[cur_arg]); |
| 3886 | if (ret & ERR_FATAL) |
| 3887 | goto out; |
| 3888 | } |
| 3889 | cur_arg += 1 + kw->skip; |
| 3890 | } |
| 3891 | if (*args[cur_arg] != 0) { |
Willy Tarreau | 433b05f | 2021-03-12 10:14:07 +0100 | [diff] [blame] | 3892 | const char *best = bind_find_best_kw(args[cur_arg]); |
| 3893 | if (best) |
| 3894 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section; did you mean '%s' maybe ?\n", |
| 3895 | file, linenum, args[cur_arg], cursection, best); |
| 3896 | else |
| 3897 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section.\n", |
| 3898 | file, linenum, args[cur_arg], cursection); |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3899 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3900 | goto out; |
| 3901 | } |
| 3902 | } |
| 3903 | else if (strcmp(args[0], "log") == 0) { |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3904 | if (!parse_logsrv(args, &cfg_log_forward->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) { |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3905 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3906 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3907 | goto out; |
| 3908 | } |
Emeric Brun | cbb7bf7 | 2020-10-05 14:39:35 +0200 | [diff] [blame] | 3909 | } |
| 3910 | else if (strcmp(args[0], "timeout") == 0) { |
| 3911 | const char *res; |
| 3912 | unsigned timeout; |
| 3913 | |
| 3914 | if (strcmp(args[1], "client") != 0) { |
| 3915 | ha_alert("parsing [%s:%d] : unknown keyword '%s %s' in log-forward section.\n", file, linenum, args[0], args[1]); |
| 3916 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3917 | goto out; |
| 3918 | } |
| 3919 | |
| 3920 | if (*args[2] == 0) { |
| 3921 | ha_alert("parsing [%s:%d] : missing timeout client value.\n", file, linenum); |
| 3922 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3923 | goto out; |
| 3924 | } |
| 3925 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
| 3926 | if (res == PARSE_TIME_OVER) { |
| 3927 | memprintf(&errmsg, "timer overflow in argument '%s' to 'timeout client' (maximum value is 2147483647 ms or ~24.8 days)", args[2]); |
| 3928 | } |
| 3929 | else if (res == PARSE_TIME_UNDER) { |
| 3930 | memprintf(&errmsg, "timer underflow in argument '%s' to 'timeout client' (minimum non-null value is 1 ms)", args[2]); |
| 3931 | } |
| 3932 | else if (res) { |
| 3933 | memprintf(&errmsg, "unexpected character '%c' in 'timeout client'", *res); |
| 3934 | return -1; |
| 3935 | } |
| 3936 | |
| 3937 | if (res) { |
| 3938 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3939 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3940 | goto out; |
| 3941 | } |
| 3942 | cfg_log_forward->timeout.client = MS_TO_TICKS(timeout); |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3943 | } |
Willy Tarreau | f9feec2 | 2020-09-16 15:04:33 +0200 | [diff] [blame] | 3944 | else { |
| 3945 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in log-forward section.\n", file, linenum, args[0]); |
| 3946 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3947 | goto out; |
| 3948 | } |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3949 | out: |
| 3950 | return err_code; |
| 3951 | } |
| 3952 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3953 | |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 3954 | /* config parsers for this section */ |
| 3955 | REGISTER_CONFIG_SECTION("log-forward", cfg_parse_log_forward, NULL); |
| 3956 | |
Willy Tarreau | 082b628 | 2019-05-22 14:42:12 +0200 | [diff] [blame] | 3957 | REGISTER_PER_THREAD_ALLOC(init_log_buffers); |
| 3958 | REGISTER_PER_THREAD_FREE(deinit_log_buffers); |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 3959 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3960 | /* |
| 3961 | * Local variables: |
| 3962 | * c-indent-level: 8 |
| 3963 | * c-basic-offset: 8 |
| 3964 | * End: |
| 3965 | */ |