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