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