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