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