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