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