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