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