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