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