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