blob: 01c66f399d446f155b6db94dd24a100bb8104cb2 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Olivier Houchardf886e342017-04-05 22:24:59 +020027#include <net/if.h>
28
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/activity.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020030#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/applet-t.h>
32#include <haproxy/base64.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020033#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020034#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020035#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020036#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/compression.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020038#include <haproxy/dns-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020039#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/fd.h>
41#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020042#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020043#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020044#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020045#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020046#include <haproxy/log.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020047#include <haproxy/mworker-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020048#include <haproxy/pattern-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020049#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020050#include <haproxy/pipe.h>
51#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020052#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020053#include <haproxy/sample-t.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreaua74cb382020-10-15 21:29:49 +020056#include <haproxy/sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020057#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020058#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020059#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020060#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020061#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020062#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020063#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020064#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020066#define PAYLOAD_PATTERN "<<"
67
William Lallemand74c24fb2016-11-21 17:18:36 +010068static struct applet cli_applet;
William Lallemand99e0bb92020-11-05 10:28:53 +010069static struct applet mcli_applet;
William Lallemand74c24fb2016-11-21 17:18:36 +010070
Willy Tarreau4975d142021-03-13 11:00:33 +010071static const char cli_permission_denied_msg[] =
William Lallemand74c24fb2016-11-21 17:18:36 +010072 "Permission denied\n"
73 "";
74
75
Christopher Faulet1bc04c72017-10-29 20:14:08 +010076static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010077
78/* List head of cli keywords */
79static struct cli_kw_list cli_keywords = {
80 .list = LIST_HEAD_INIT(cli_keywords.list)
81};
82
83extern const char *stat_status_codes[];
84
Eric Salama1b8dacc2021-03-16 15:12:17 +010085struct proxy *mworker_proxy; /* CLI proxy of the master */
William Lallemand8a022572018-10-26 14:47:35 +020086
Willy Tarreau92fbbcc2021-05-09 21:45:29 +020087static int cmp_kw_entries(const void *a, const void *b)
88{
89 const struct cli_kw *l = *(const struct cli_kw **)a;
90 const struct cli_kw *r = *(const struct cli_kw **)b;
91
92 return strcmp(l->usage ? l->usage : "", r->usage ? r->usage : "");
93}
94
Willy Tarreaub96a74c2021-03-12 17:13:28 +010095/* This will show the help message and list the commands supported at the
96 * current level that match all of the first words of <args> if args is not
97 * NULL, or all args if none matches or if args is null.
98 */
99static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args)
William Lallemand74c24fb2016-11-21 17:18:36 +0100100{
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200101 struct cli_kw *entries[CLI_MAX_HELP_ENTRIES];
William Lallemand74c24fb2016-11-21 17:18:36 +0100102 struct cli_kw_list *kw_list;
103 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200104 struct buffer *tmp = get_trash_chunk();
105 struct buffer out;
Willy Tarreaub7364582021-03-12 18:24:46 +0100106 struct { struct cli_kw *kw; int dist; } matches[CLI_MAX_MATCHES], swp;
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100107 int idx;
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200108 int ishelp = 0;
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100109 int length = 0;
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200110 int help_entries = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100111
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100112 ha_free(&dynamic_usage_msg);
William Lallemand74c24fb2016-11-21 17:18:36 +0100113
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200114 if (args && *args && strcmp(*args, "help") == 0) {
115 args++;
116 ishelp = 1;
117 }
118
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100119 /* first, let's measure the longest match */
120 list_for_each_entry(kw_list, &cli_keywords.list, list) {
121 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100122 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100123 continue;
124 if ((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
125 (ACCESS_MASTER_ONLY|ACCESS_MASTER))
126 continue;
127
128 /* OK this command is visible */
129 for (idx = 0; idx < CLI_PREFIX_KW_NB; idx++) {
130 if (!kw->str_kw[idx])
131 break; // end of keyword
132 if (!args || !args[idx] || !*args[idx])
133 break; // end of command line
134 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
135 break;
136 if (idx + 1 > length)
137 length = idx + 1;
138 }
139 }
140 }
141
Willy Tarreaub7364582021-03-12 18:24:46 +0100142 /* now <length> equals the number of exactly matching words */
William Lallemand74c24fb2016-11-21 17:18:36 +0100143 chunk_reset(tmp);
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200144 if (ishelp) // this is the help message.
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100145 chunk_strcat(tmp, "The following commands are valid at this level:\n");
Willy Tarreau5db446d2021-05-10 07:47:05 +0200146 else if (!length && (!args || !*args || !**args)) // no match
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100147 chunk_strcat(tmp, "Unknown command. Please enter one of the following commands only:\n");
148 else // partial match
149 chunk_strcat(tmp, "Unknown command, but maybe one of the following ones is a better match:\n");
150
Willy Tarreaub7364582021-03-12 18:24:46 +0100151 for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
152 matches[idx].kw = NULL;
153 matches[idx].dist = INT_MAX;
154 }
155
156 /* In case of partial match we'll look for the best matching entries
157 * starting from position <length>
158 */
Willy Tarreau9c187472021-03-13 12:25:43 +0100159 if (args && args[length] && *args[length]) {
Willy Tarreaub7364582021-03-12 18:24:46 +0100160 list_for_each_entry(kw_list, &cli_keywords.list, list) {
161 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100162 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreaub7364582021-03-12 18:24:46 +0100163 continue;
164 if ((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
165 (ACCESS_MASTER_ONLY|ACCESS_MASTER))
166 continue;
167
168 for (idx = 0; idx < length; idx++) {
169 if (!kw->str_kw[idx])
170 break; // end of keyword
171 if (!args || !args[idx] || !*args[idx])
172 break; // end of command line
173 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
174 break;
175 }
176
177 /* extra non-matching words are fuzzy-matched */
178 if (kw->usage && idx == length && args[idx] && *args[idx]) {
179 uint8_t word_sig[1024];
180 uint8_t list_sig[1024];
181 int dist = 0;
182 int totlen = 0;
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100183 int i;
Willy Tarreaub7364582021-03-12 18:24:46 +0100184
185 /* this one matches, let's compute the distance between the two
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100186 * on the remaining words. For this we're computing the signature
187 * of everything that remains and the cumulated length of the
188 * strings.
Willy Tarreaub7364582021-03-12 18:24:46 +0100189 */
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100190 memset(word_sig, 0, sizeof(word_sig));
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100191 for (i = idx; i < CLI_PREFIX_KW_NB && args[i] && *args[i]; i++) {
192 update_word_fingerprint(word_sig, args[i]);
193 totlen += strlen(args[i]);
194 }
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100195
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100196 memset(list_sig, 0, sizeof(list_sig));
197 for (i = idx; i < CLI_PREFIX_KW_NB && kw->str_kw[i]; i++) {
198 update_word_fingerprint(list_sig, kw->str_kw[i]);
199 totlen += strlen(kw->str_kw[i]);
Willy Tarreaub7364582021-03-12 18:24:46 +0100200 }
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100201
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100202 dist = word_fingerprint_distance(word_sig, list_sig);
Willy Tarreaub7364582021-03-12 18:24:46 +0100203
204 /* insert this one at its place if relevant, in order to keep only
205 * the best matches.
206 */
207 swp.kw = kw; swp.dist = dist;
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100208 if (dist < 5*totlen/2 && dist < matches[CLI_MAX_MATCHES-1].dist) {
Willy Tarreaub7364582021-03-12 18:24:46 +0100209 matches[CLI_MAX_MATCHES-1] = swp;
210 for (idx = CLI_MAX_MATCHES - 1; --idx >= 0;) {
211 if (matches[idx+1].dist >= matches[idx].dist)
212 break;
213 matches[idx+1] = matches[idx];
214 matches[idx] = swp;
215 }
216 }
217 }
218 }
219 }
220 }
221
Willy Tarreauec197e82021-03-15 10:35:04 +0100222 if (matches[0].kw) {
223 /* we have fuzzy matches, let's propose them */
224 for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
225 kw = matches[idx].kw;
226 if (!kw)
227 break;
228
229 /* stop the dump if some words look very unlikely candidates */
230 if (matches[idx].dist > 5*matches[0].dist/2)
231 break;
232
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200233 if (help_entries < CLI_MAX_HELP_ENTRIES)
234 entries[help_entries++] = kw;
Willy Tarreauec197e82021-03-15 10:35:04 +0100235 }
236 }
237
William Lallemand74c24fb2016-11-21 17:18:36 +0100238 list_for_each_entry(kw_list, &cli_keywords.list, list) {
Willy Tarreauec197e82021-03-15 10:35:04 +0100239 /* no full dump if we've already found nice candidates */
240 if (matches[0].kw)
241 break;
242
Willy Tarreau91bc3592021-03-12 15:20:39 +0100243 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
William Lallemand14721be2018-10-26 14:47:37 +0200244
Willy Tarreau91bc3592021-03-12 15:20:39 +0100245 /* in a worker or normal process, don't display master-only commands
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100246 * nor expert/experimental mode commands if not in this mode.
Willy Tarreau91bc3592021-03-12 15:20:39 +0100247 */
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100248 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreau91bc3592021-03-12 15:20:39 +0100249 continue;
William Lallemand14721be2018-10-26 14:47:37 +0200250
Willy Tarreau91bc3592021-03-12 15:20:39 +0100251 /* in master don't display commands that have neither the master bit
252 * nor the master-only bit.
253 */
254 if ((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
255 (ACCESS_MASTER_ONLY|ACCESS_MASTER))
256 continue;
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200257
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100258 for (idx = 0; idx < length; idx++) {
259 if (!kw->str_kw[idx])
260 break; // end of keyword
261 if (!args || !args[idx] || !*args[idx])
262 break; // end of command line
263 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
264 break;
265 }
266
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200267 if (kw->usage && idx == length && help_entries < CLI_MAX_HELP_ENTRIES)
268 entries[help_entries++] = kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100269 }
270 }
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100271
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200272 qsort(entries, help_entries, sizeof(*entries), cmp_kw_entries);
273
274 for (idx = 0; idx < help_entries; idx++)
275 chunk_appendf(tmp, " %s\n", entries[idx]->usage);
276
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100277 /* always show the prompt/help/quit commands */
278 chunk_strcat(tmp,
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200279 " help [<command>] : list matching or all commands\n"
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200280 " prompt : toggle interactive mode with prompt\n"
281 " quit : disconnect\n");
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100282
William Lallemand74c24fb2016-11-21 17:18:36 +0100283 chunk_init(&out, NULL, 0);
284 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200285 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200286
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100287 appctx->ctx.cli.severity = LOG_INFO;
288 appctx->ctx.cli.msg = dynamic_usage_msg;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200289 appctx->st0 = CLI_ST_PRINT;
290
William Lallemand74c24fb2016-11-21 17:18:36 +0100291 return dynamic_usage_msg;
292}
293
294struct cli_kw* cli_find_kw(char **args)
295{
296 struct cli_kw_list *kw_list;
297 struct cli_kw *kw;/* current cli_kw */
298 char **tmp_args;
299 const char **tmp_str_kw;
300 int found = 0;
301
302 if (LIST_ISEMPTY(&cli_keywords.list))
303 return NULL;
304
305 list_for_each_entry(kw_list, &cli_keywords.list, list) {
306 kw = &kw_list->kw[0];
307 while (*kw->str_kw) {
308 tmp_args = args;
309 tmp_str_kw = kw->str_kw;
310 while (*tmp_str_kw) {
311 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
312 found = 1;
313 } else {
314 found = 0;
315 break;
316 }
317 tmp_args++;
318 tmp_str_kw++;
319 }
320 if (found)
321 return (kw);
322 kw++;
323 }
324 }
325 return NULL;
326}
327
Thierry Fourniera51a1fd2020-11-28 20:10:08 +0100328struct cli_kw* cli_find_kw_exact(char **args)
329{
330 struct cli_kw_list *kw_list;
331 int found = 0;
332 int i;
333 int j;
334
335 if (LIST_ISEMPTY(&cli_keywords.list))
336 return NULL;
337
338 list_for_each_entry(kw_list, &cli_keywords.list, list) {
339 for (i = 0; kw_list->kw[i].str_kw[0]; i++) {
340 found = 1;
341 for (j = 0; j < CLI_PREFIX_KW_NB; j++) {
342 if (args[j] == NULL && kw_list->kw[i].str_kw[j] == NULL) {
343 break;
344 }
345 if (args[j] == NULL || kw_list->kw[i].str_kw[j] == NULL) {
346 found = 0;
347 break;
348 }
349 if (strcmp(args[j], kw_list->kw[i].str_kw[j]) != 0) {
350 found = 0;
351 break;
352 }
353 }
354 if (found)
355 return &kw_list->kw[i];
356 }
357 }
358 return NULL;
359}
360
William Lallemand74c24fb2016-11-21 17:18:36 +0100361void cli_register_kw(struct cli_kw_list *kw_list)
362{
Willy Tarreau2b718102021-04-21 07:32:39 +0200363 LIST_APPEND(&cli_keywords.list, &kw_list->list);
William Lallemand74c24fb2016-11-21 17:18:36 +0100364}
365
366
367/* allocate a new stats frontend named <name>, and return it
368 * (or NULL in case of lack of memory).
369 */
Willy Tarreau4975d142021-03-13 11:00:33 +0100370static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100371{
372 struct proxy *fe;
373
374 fe = calloc(1, sizeof(*fe));
375 if (!fe)
376 return NULL;
377
378 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100379 fe->next = proxies_list;
380 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100381 fe->last_change = now.tv_sec;
382 fe->id = strdup("GLOBAL");
383 fe->cap = PR_CAP_FE;
384 fe->maxconn = 10; /* default to 10 concurrent connections */
385 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
386 fe->conf.file = strdup(file);
387 fe->conf.line = line;
388 fe->accept = frontend_accept;
389 fe->default_target = &cli_applet.obj_type;
390
391 /* the stats frontend is the only one able to assign ID #0 */
392 fe->conf.id.key = fe->uuid = 0;
393 eb32_insert(&used_proxy_id, &fe->conf.id);
394 return fe;
395}
396
397/* This function parses a "stats" statement in the "global" section. It returns
398 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
399 * error message into the <err> buffer which will be preallocated. The trailing
400 * '\n' must not be written. The function must be called with <args> pointing to
401 * the first word after "stats".
402 */
Willy Tarreau4975d142021-03-13 11:00:33 +0100403static int cli_parse_global(char **args, int section_type, struct proxy *curpx,
404 const struct proxy *defpx, const char *file, int line,
405 char **err)
William Lallemand74c24fb2016-11-21 17:18:36 +0100406{
407 struct bind_conf *bind_conf;
408 struct listener *l;
409
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100410 if (strcmp(args[1], "socket") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100411 int cur_arg;
412
413 if (*args[2] == 0) {
414 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
415 return -1;
416 }
417
Willy Tarreau4975d142021-03-13 11:00:33 +0100418 if (!global.cli_fe) {
419 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100420 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
421 return -1;
422 }
423 }
424
Willy Tarreau4975d142021-03-13 11:00:33 +0100425 bind_conf = bind_conf_alloc(global.cli_fe, file, line, args[2], xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +0200426 if (!bind_conf) {
427 memprintf(err, "'%s %s' : out of memory trying to allocate a bind_conf", args[0], args[1]);
428 return -1;
429 }
William Lallemand07a62f72017-05-24 00:57:40 +0200430 bind_conf->level &= ~ACCESS_LVL_MASK;
431 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
William Lallemand74c24fb2016-11-21 17:18:36 +0100432
Willy Tarreau4975d142021-03-13 11:00:33 +0100433 if (!str2listener(args[2], global.cli_fe, bind_conf, file, line, err)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100434 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
435 file, line, args[0], args[1], err && *err ? *err : "error");
436 return -1;
437 }
438
439 cur_arg = 3;
440 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100441 struct bind_kw *kw;
Willy Tarreau433b05f2021-03-12 10:14:07 +0100442 const char *best;
William Lallemand74c24fb2016-11-21 17:18:36 +0100443
444 kw = bind_find_kw(args[cur_arg]);
445 if (kw) {
446 if (!kw->parse) {
447 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
448 args[0], args[1], args[cur_arg]);
449 return -1;
450 }
451
Willy Tarreau4975d142021-03-13 11:00:33 +0100452 if (kw->parse(args, cur_arg, global.cli_fe, bind_conf, err) != 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100453 if (err && *err)
454 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
455 else
456 memprintf(err, "'%s %s' : error encountered while processing '%s'",
457 args[0], args[1], args[cur_arg]);
458 return -1;
459 }
460
461 cur_arg += 1 + kw->skip;
462 continue;
463 }
464
Willy Tarreau433b05f2021-03-12 10:14:07 +0100465 best = bind_find_best_kw(args[cur_arg]);
466 if (best)
467 memprintf(err, "'%s %s' : unknown keyword '%s'. Did you mean '%s' maybe ?",
468 args[0], args[1], args[cur_arg], best);
469 else
470 memprintf(err, "'%s %s' : unknown keyword '%s'.",
471 args[0], args[1], args[cur_arg]);
William Lallemand74c24fb2016-11-21 17:18:36 +0100472 return -1;
473 }
474
475 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100476 l->accept = session_accept_fd;
Willy Tarreau4975d142021-03-13 11:00:33 +0100477 l->default_target = global.cli_fe->default_target;
William Lallemand74c24fb2016-11-21 17:18:36 +0100478 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
479 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100480 global.maxsock++; /* for the listening socket */
William Lallemand74c24fb2016-11-21 17:18:36 +0100481 }
482 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100483 else if (strcmp(args[1], "timeout") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100484 unsigned timeout;
485 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
486
Willy Tarreau9faebe32019-06-07 19:00:37 +0200487 if (res == PARSE_TIME_OVER) {
488 memprintf(err, "timer overflow in argument '%s' to '%s %s' (maximum value is 2147483647 ms or ~24.8 days)",
489 args[2], args[0], args[1]);
490 return -1;
491 }
492 else if (res == PARSE_TIME_UNDER) {
493 memprintf(err, "timer underflow in argument '%s' to '%s %s' (minimum non-null value is 1 ms)",
494 args[2], args[0], args[1]);
495 return -1;
496 }
497 else if (res) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100498 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
499 return -1;
500 }
501
502 if (!timeout) {
503 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
504 return -1;
505 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100506 if (!global.cli_fe) {
507 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100508 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
509 return -1;
510 }
511 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100512 global.cli_fe->timeout.client = MS_TO_TICKS(timeout);
William Lallemand74c24fb2016-11-21 17:18:36 +0100513 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100514 else if (strcmp(args[1], "maxconn") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100515 int maxconn = atol(args[2]);
516
517 if (maxconn <= 0) {
518 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
519 return -1;
520 }
521
Willy Tarreau4975d142021-03-13 11:00:33 +0100522 if (!global.cli_fe) {
523 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100524 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
525 return -1;
526 }
527 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100528 global.cli_fe->maxconn = maxconn;
William Lallemand74c24fb2016-11-21 17:18:36 +0100529 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100530 else if (strcmp(args[1], "bind-process") == 0) { /* enable the socket only on some processes */
William Lallemand74c24fb2016-11-21 17:18:36 +0100531 int cur_arg = 2;
532 unsigned long set = 0;
533
Willy Tarreau4975d142021-03-13 11:00:33 +0100534 if (!global.cli_fe) {
535 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100536 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
537 return -1;
538 }
539 }
540
541 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100542 if (strcmp(args[cur_arg], "all") == 0) {
543 set = 0;
544 break;
545 }
Willy Tarreauff9c9142019-02-07 10:39:36 +0100546 if (parse_process_number(args[cur_arg], &set, MAX_PROCS, NULL, err)) {
Christopher Fauletf1f0c5f2017-11-22 12:06:43 +0100547 memprintf(err, "'%s %s' : %s", args[0], args[1], *err);
William Lallemand74c24fb2016-11-21 17:18:36 +0100548 return -1;
549 }
550 cur_arg++;
551 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100552 global.cli_fe->bind_proc = set;
William Lallemand74c24fb2016-11-21 17:18:36 +0100553 }
554 else {
555 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
556 return -1;
557 }
558 return 0;
559}
560
William Lallemand33d29e22019-04-01 11:30:06 +0200561/*
William Lallemand9a37fd02019-04-12 16:09:24 +0200562 * This function exports the bound addresses of a <frontend> in the environment
563 * variable <varname>. Those addresses are separated by semicolons and prefixed
564 * with their type (abns@, unix@, sockpair@ etc)
565 * Return -1 upon error, 0 otherwise
William Lallemand33d29e22019-04-01 11:30:06 +0200566 */
William Lallemand9a37fd02019-04-12 16:09:24 +0200567int listeners_setenv(struct proxy *frontend, const char *varname)
William Lallemand33d29e22019-04-01 11:30:06 +0200568{
569 struct buffer *trash = get_trash_chunk();
570 struct bind_conf *bind_conf;
571
William Lallemand9a37fd02019-04-12 16:09:24 +0200572 if (frontend) {
573 list_for_each_entry(bind_conf, &frontend->conf.bind, by_fe) {
William Lallemand33d29e22019-04-01 11:30:06 +0200574 struct listener *l;
575
576 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
577 char addr[46];
578 char port[6];
579
William Lallemand620072b2019-04-12 16:09:25 +0200580 /* separate listener by semicolons */
581 if (trash->data)
582 chunk_appendf(trash, ";");
583
Willy Tarreau37159062020-08-27 07:48:42 +0200584 if (l->rx.addr.ss_family == AF_UNIX) {
William Lallemand33d29e22019-04-01 11:30:06 +0200585 const struct sockaddr_un *un;
586
Willy Tarreau37159062020-08-27 07:48:42 +0200587 un = (struct sockaddr_un *)&l->rx.addr;
William Lallemand33d29e22019-04-01 11:30:06 +0200588 if (un->sun_path[0] == '\0') {
589 chunk_appendf(trash, "abns@%s", un->sun_path+1);
590 } else {
591 chunk_appendf(trash, "unix@%s", un->sun_path);
592 }
Willy Tarreau37159062020-08-27 07:48:42 +0200593 } else if (l->rx.addr.ss_family == AF_INET) {
594 addr_to_str(&l->rx.addr, addr, sizeof(addr));
595 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemand33d29e22019-04-01 11:30:06 +0200596 chunk_appendf(trash, "ipv4@%s:%s", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +0200597 } else if (l->rx.addr.ss_family == AF_INET6) {
598 addr_to_str(&l->rx.addr, addr, sizeof(addr));
599 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemand33d29e22019-04-01 11:30:06 +0200600 chunk_appendf(trash, "ipv6@[%s]:%s", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +0200601 } else if (l->rx.addr.ss_family == AF_CUST_SOCKPAIR) {
602 chunk_appendf(trash, "sockpair@%d", ((struct sockaddr_in *)&l->rx.addr)->sin_addr.s_addr);
William Lallemand33d29e22019-04-01 11:30:06 +0200603 }
William Lallemand33d29e22019-04-01 11:30:06 +0200604 }
605 }
606 trash->area[trash->data++] = '\0';
William Lallemand9a37fd02019-04-12 16:09:24 +0200607 if (setenv(varname, trash->area, 1) < 0)
William Lallemand33d29e22019-04-01 11:30:06 +0200608 return -1;
609 }
610
611 return 0;
612}
613
William Lallemand9a37fd02019-04-12 16:09:24 +0200614int cli_socket_setenv()
615{
Willy Tarreau4975d142021-03-13 11:00:33 +0100616 if (listeners_setenv(global.cli_fe, "HAPROXY_CLI") < 0)
William Lallemand9a37fd02019-04-12 16:09:24 +0200617 return -1;
618 if (listeners_setenv(mworker_proxy, "HAPROXY_MASTER_CLI") < 0)
619 return -1;
620
621 return 0;
622}
623
William Lallemand33d29e22019-04-01 11:30:06 +0200624REGISTER_CONFIG_POSTPARSER("cli", cli_socket_setenv);
625
Willy Tarreaude57a572016-11-23 17:01:39 +0100626/* Verifies that the CLI at least has a level at least as high as <level>
627 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
628 * failure, an error message is prepared and the appctx's state is adjusted
629 * to print it so that a return 1 is enough to abort any processing.
630 */
631int cli_has_level(struct appctx *appctx, int level)
632{
Willy Tarreaude57a572016-11-23 17:01:39 +0100633
William Lallemandf630d012018-12-13 09:05:44 +0100634 if ((appctx->cli_level & ACCESS_LVL_MASK) < level) {
Willy Tarreau4975d142021-03-13 11:00:33 +0100635 cli_err(appctx, cli_permission_denied_msg);
Willy Tarreaude57a572016-11-23 17:01:39 +0100636 return 0;
637 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100638 return 1;
639}
640
William Lallemandb7ea1412018-12-13 09:05:47 +0100641/* same as cli_has_level but for the CLI proxy and without error message */
642int pcli_has_level(struct stream *s, int level)
643{
644 if ((s->pcli_flags & ACCESS_LVL_MASK) < level) {
645 return 0;
646 }
647 return 1;
648}
649
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200650/* Returns severity_output for the current session if set, or default for the socket */
651static int cli_get_severity_output(struct appctx *appctx)
652{
653 if (appctx->cli_severity_output)
654 return appctx->cli_severity_output;
655 return strm_li(si_strm(appctx->owner))->bind_conf->severity_output;
656}
William Lallemand74c24fb2016-11-21 17:18:36 +0100657
Willy Tarreau41908562016-11-24 16:23:38 +0100658/* Processes the CLI interpreter on the stats socket. This function is called
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100659 * from the CLI's IO handler running in an appctx context. The function returns
660 * 1 if the request was understood, otherwise zero (in which case an error
661 * message will be displayed). It is called with appctx->st0
Willy Tarreau41908562016-11-24 16:23:38 +0100662 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
663 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
664 * have its own I/O handler called again. Most of the time, parsers will only
665 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100666 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
667 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100668 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200669static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100670{
Willy Tarreauf14c7572021-03-13 10:59:23 +0100671 char *args[MAX_CLI_ARGS + 1], *p, *end, *payload = NULL;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200672 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100673 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100674
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200675 appctx->st2 = 0;
676 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
William Lallemand74c24fb2016-11-21 17:18:36 +0100677
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 p = appctx->chunk->area;
679 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100680
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200681 /*
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200682 * Get pointers on words.
683 * One extra slot is reserved to store a pointer on a null byte.
684 */
Willy Tarreauf14c7572021-03-13 10:59:23 +0100685 while (i < MAX_CLI_ARGS && p < end) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200686 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100687
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200688 /* skip leading spaces/tabs */
689 p += strspn(p, " \t");
690 if (!*p)
691 break;
Willy Tarreauc5c170a2021-09-17 11:07:45 +0200692
693 if (strcmp(p, PAYLOAD_PATTERN) == 0) {
694 /* payload pattern recognized here, this is not an arg anymore,
695 * the payload starts at the first byte that follows the zero
696 * after the pattern.
697 */
698 payload = p + strlen(PAYLOAD_PATTERN) + 1;
699 break;
700 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100701
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200702 args[i] = p;
Yves Lafonb08c6d02020-06-08 16:08:06 +0200703 while (1) {
704 p += strcspn(p, " \t\\");
705 /* escaped chars using backlashes (\) */
706 if (*p == '\\') {
707 if (!*++p)
708 break;
709 if (!*++p)
710 break;
711 } else {
712 break;
713 }
714 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200715 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100716
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200717 /* unescape backslashes (\) */
718 for (j = 0, k = 0; args[i][k]; k++) {
719 if (args[i][k] == '\\') {
720 if (args[i][k + 1] == '\\')
721 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100722 else
723 continue;
724 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200725 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100726 j++;
727 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200728 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100729
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200730 i++;
731 }
732 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 p = appctx->chunk->area + appctx->chunk->data;
Willy Tarreauf14c7572021-03-13 10:59:23 +0100734 for (; i < MAX_CLI_ARGS + 1; i++)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200735 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100736
Christopher Faulet0f1f4e22024-02-20 18:30:16 +0100737 if (appctx->st1 & APPCTX_CLI_ST1_SHUT_EXPECTED) {
738 /* The previous command line was finished by a \n in non-interactive mode.
739 * It should not be followed by another command line. In non-interactive mode,
740 * only one line should be processed. Because of a bug, it is not respected.
741 * So emit a warning, only once in the process life, to warn users their script
742 * must be updated.
743 */
744 appctx->st1 &= ~APPCTX_CLI_ST1_SHUT_EXPECTED;
745 if (ONLY_ONCE()) {
746 ha_warning("Commands sent to the CLI were chained using a new line character while in non-interactive mode."
747 " This is not reliable, not officially supported and will not be supported anymore in future versions. "
748 "Please use ';' to delimit commands instead.");
749 }
750 }
751
Willy Tarreau41908562016-11-24 16:23:38 +0100752 kw = cli_find_kw(args);
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100753 if (!kw ||
754 (kw->level & ~appctx->cli_level & ACCESS_MASTER_ONLY) ||
755 (appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) == (ACCESS_MASTER_ONLY|ACCESS_MASTER)) {
756 /* keyword not found in this mode */
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100757 cli_gen_usage_msg(appctx, args);
Willy Tarreau41908562016-11-24 16:23:38 +0100758 return 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100759 }
William Lallemand14721be2018-10-26 14:47:37 +0200760
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100761 /* don't handle expert mode commands if not in this mode. */
762 if (kw->level & ~appctx->cli_level & ACCESS_EXPERT) {
763 cli_err(appctx, "This command is restricted to expert mode only.\n");
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200764 return 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100765 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200766
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100767 if (kw->level & ~appctx->cli_level & ACCESS_EXPERIMENTAL) {
768 cli_err(appctx, "This command is restricted to experimental mode only.\n");
769 return 0;
770 }
771
Amaury Denoyellef4929922021-05-05 16:29:23 +0200772 if (kw->level == ACCESS_EXPERT)
773 mark_tainted(TAINTED_CLI_EXPERT_MODE);
774 else if (kw->level == ACCESS_EXPERIMENTAL)
775 mark_tainted(TAINTED_CLI_EXPERIMENTAL_MODE);
776
Willy Tarreau41908562016-11-24 16:23:38 +0100777 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200778 appctx->io_release = kw->io_release;
William Lallemand90b098c2019-10-25 21:10:14 +0200779
780 if (kw->parse && kw->parse(args, payload, appctx, kw->private) != 0)
781 goto fail;
782
783 /* kw->parse could set its own io_handler or io_release handler */
784 if (!appctx->io_handler)
785 goto fail;
786
787 appctx->st0 = CLI_ST_CALLBACK;
788 return 1;
789fail:
790 appctx->io_handler = NULL;
791 appctx->io_release = NULL;
Willy Tarreau41908562016-11-24 16:23:38 +0100792 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100793}
794
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200795/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
796static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
797{
Willy Tarreau83061a82018-07-13 11:56:34 +0200798 struct buffer *tmp;
Willy Tarreau166fdd92024-02-08 18:15:23 +0100799 struct ist imsg;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200800
801 tmp = get_trash_chunk();
802 chunk_reset(tmp);
803
Willy Tarreau166fdd92024-02-08 18:15:23 +0100804 if (likely(severity_output == CLI_SEVERITY_NONE))
805 goto send_it;
806
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200807 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100808 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200809 chunk_printf(tmp, "[%d]: ", severity);
810 }
811 else {
812 switch (severity_output) {
813 case CLI_SEVERITY_NUMBER:
814 chunk_printf(tmp, "[%d]: ", severity);
815 break;
816 case CLI_SEVERITY_STRING:
817 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
818 break;
819 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100820 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200821 }
822 }
Willy Tarreau166fdd92024-02-08 18:15:23 +0100823 send_it:
824 /* the vast majority of messages have their trailing LF but a few are
825 * still missing it, and very rare ones might even have two. For this
826 * reason, we'll first delete the trailing LFs if present, then
827 * systematically append one.
828 */
829 for (imsg = ist(msg); imsg.len > 0 && imsg.ptr[imsg.len - 1] == '\n'; imsg.len--)
830 ;
831
832 chunk_istcat(tmp, imsg);
833 chunk_istcat(tmp, ist("\n"));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200834
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200835 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200836}
837
William Lallemand74c24fb2016-11-21 17:18:36 +0100838/* This I/O handler runs as an applet embedded in a stream interface. It is
839 * used to processes I/O from/to the stats unix socket. The system relies on a
840 * state machine handling requests and various responses. We read a request,
841 * then we process it and send the response, and we possibly display a prompt.
842 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100843 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100844 * or not.
845 */
846static void cli_io_handler(struct appctx *appctx)
847{
848 struct stream_interface *si = appctx->owner;
849 struct channel *req = si_oc(si);
850 struct channel *res = si_ic(si);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200851 struct bind_conf *bind_conf = strm_li(si_strm(si))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100852 int reql;
853 int len;
Christopher Faulet0f1f4e22024-02-20 18:30:16 +0100854 int lf = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100855
856 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
857 goto out;
858
Joseph Herlant008b3ce2018-11-25 12:51:45 -0800859 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200860 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100861 /* buf.size==0 means we failed to get a buffer and were
862 * already subscribed to a wait list to get a buffer.
863 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100864 goto out;
865 }
866
William Lallemand74c24fb2016-11-21 17:18:36 +0100867 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100868 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100869 /* Stats output not initialized yet */
870 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200871 /* reset severity to default at init */
872 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100873 appctx->st0 = CLI_ST_GETREQ;
William Lallemandf630d012018-12-13 09:05:44 +0100874 appctx->cli_level = bind_conf->level;
William Lallemand74c24fb2016-11-21 17:18:36 +0100875 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100876 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100877 /* Let's close for real now. We just close the request
878 * side, the conditions below will complete if needed.
879 */
880 si_shutw(si);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200881 free_trash_chunk(appctx->chunk);
Willy Tarreau18b2a9d2021-05-04 16:27:45 +0200882 appctx->chunk = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +0100883 break;
884 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100885 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200886 char *str;
887
888 /* use a trash chunk to store received data */
889 if (!appctx->chunk) {
890 appctx->chunk = alloc_trash_chunk();
891 if (!appctx->chunk) {
892 appctx->st0 = CLI_ST_END;
893 continue;
894 }
895 }
896
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200897 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200898
William Lallemand74c24fb2016-11-21 17:18:36 +0100899 /* ensure we have some output room left in the event we
900 * would want to return some info right after parsing.
901 */
902 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100903 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100904 break;
905 }
906
Willy Tarreau02a73302022-01-19 17:23:52 +0100907 /* payload doesn't take escapes nor does it end on semi-colons, so
908 * we use the regular getline. Normal mode however must stop on
909 * LFs and semi-colons that are not prefixed by a backslash. Note
910 * that we reserve one byte at the end to insert a trailing nul byte.
911 */
912
913 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
914 reql = co_getline(si_oc(si), str,
915 appctx->chunk->size - appctx->chunk->data - 1);
916 else
917 reql = co_getdelim(si_oc(si), str,
918 appctx->chunk->size - appctx->chunk->data - 1,
919 "\n;", '\\');
920
William Lallemand74c24fb2016-11-21 17:18:36 +0100921 if (reql <= 0) { /* closed or EOL not found */
922 if (reql == 0)
923 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100924 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100925 continue;
926 }
927
Christopher Faulet0f1f4e22024-02-20 18:30:16 +0100928 if (str[reql-1] == '\n')
929 lf = 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100930
931 /* now it is time to check that we have a full line,
932 * remove the trailing \n and possibly \r, then cut the
933 * line.
934 */
935 len = reql - 1;
Christopher Fauletb3dd5d52024-02-20 16:37:11 +0100936 if (str[len] != '\n' && str[len] != ';') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100937 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100938 continue;
939 }
940
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200941 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100942 len--;
943
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200944 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200945 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200946
947 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200948 appctx->chunk->area[appctx->chunk->data] = '\n';
949 appctx->chunk->area[appctx->chunk->data + 1] = 0;
950 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200951 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100952
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100953 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200954
955 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
956 /* empty line */
957 if (!len) {
958 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200959 appctx->chunk->data -= 2;
960 appctx->chunk->area[appctx->chunk->data] = 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100961 cli_parse_request(appctx);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200962 chunk_reset(appctx->chunk);
963 /* NB: cli_sock_parse_request() may have put
964 * another CLI_ST_O_* into appctx->st0.
965 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200966 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
Christopher Faulet0f1f4e22024-02-20 18:30:16 +0100967 if (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && lf)
968 appctx->st1 |= APPCTX_CLI_ST1_SHUT_EXPECTED;
William Lallemand74c24fb2016-11-21 17:18:36 +0100969 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100970 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200971 else {
972 /*
973 * Look for the "payload start" pattern at the end of a line
974 * Its location is not remembered here, this is just to switch
975 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +0100976 */
Willy Tarreauc5c170a2021-09-17 11:07:45 +0200977 if (strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN) == 0) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200978 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Willy Tarreauc5c170a2021-09-17 11:07:45 +0200979 appctx->chunk->data++; // keep the trailing \0 after '<<'
980 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200981 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200982 /* no payload, the command is complete: parse the request */
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100983 cli_parse_request(appctx);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200984 chunk_reset(appctx->chunk);
Christopher Faulet0f1f4e22024-02-20 18:30:16 +0100985 if (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && lf)
986 appctx->st1 |= APPCTX_CLI_ST1_SHUT_EXPECTED;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +0200987 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100988 }
989
990 /* re-adjust req buffer */
Willy Tarreau06d80a92017-10-19 14:32:15 +0200991 co_skip(si_oc(si), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
993 }
994 else { /* output functions */
Willy Tarreaud50c7fe2019-08-09 09:57:36 +0200995 const char *msg;
996 int sev;
997
William Lallemand74c24fb2016-11-21 17:18:36 +0100998 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100999 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001001 case CLI_ST_PRINT: /* print const message in msg */
1002 case CLI_ST_PRINT_ERR: /* print const error in msg */
1003 case CLI_ST_PRINT_DYN: /* print dyn message in msg, free */
1004 case CLI_ST_PRINT_FREE: /* print dyn error in err, free */
1005 if (appctx->st0 == CLI_ST_PRINT || appctx->st0 == CLI_ST_PRINT_ERR) {
1006 sev = appctx->st0 == CLI_ST_PRINT_ERR ?
1007 LOG_ERR : appctx->ctx.cli.severity;
1008 msg = appctx->ctx.cli.msg;
1009 }
1010 else if (appctx->st0 == CLI_ST_PRINT_DYN || appctx->st0 == CLI_ST_PRINT_FREE) {
1011 sev = appctx->st0 == CLI_ST_PRINT_FREE ?
1012 LOG_ERR : appctx->ctx.cli.severity;
1013 msg = appctx->ctx.cli.err;
1014 if (!msg) {
1015 sev = LOG_ERR;
1016 msg = "Out of memory.\n";
1017 }
1018 }
1019 else {
1020 sev = LOG_ERR;
1021 msg = "Internal error.\n";
1022 }
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +02001023
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001024 if (cli_output_msg(res, msg, sev, cli_get_severity_output(appctx)) != -1) {
1025 if (appctx->st0 == CLI_ST_PRINT_FREE ||
1026 appctx->st0 == CLI_ST_PRINT_DYN) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001027 ha_free(&appctx->ctx.cli.err);
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001028 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001029 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +01001030 }
1031 else
Willy Tarreaudb398432018-11-15 11:08:52 +01001032 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01001033 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001034
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001035 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +01001036 if (appctx->io_handler)
1037 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001038 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 if (appctx->io_release) {
1040 appctx->io_release(appctx);
1041 appctx->io_release = NULL;
1042 }
1043 }
1044 break;
1045 default: /* abnormal state */
1046 si->flags |= SI_FL_ERR;
1047 break;
1048 }
1049
1050 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001051 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001052 const char *prompt = "";
1053
1054 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
1055 /*
1056 * when entering a payload with interactive mode, change the prompt
1057 * to emphasize that more data can still be sent
1058 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001059 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001060 prompt = "+ ";
1061 else
1062 prompt = "\n> ";
1063 }
1064 else {
William Lallemandad032882019-07-01 10:56:15 +02001065 if (!(appctx->st1 & (APPCTX_CLI_ST1_PAYLOAD|APPCTX_CLI_ST1_NOLF)))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001066 prompt = "\n";
1067 }
1068
1069 if (ci_putstr(si_ic(si), prompt) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001070 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +01001071 else
Willy Tarreaudb398432018-11-15 11:08:52 +01001072 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01001073 }
1074
1075 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001076 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001077 break;
1078
1079 /* Now we close the output if one of the writers did so,
1080 * or if we're not in interactive mode and the request
1081 * buffer is empty. This still allows pipelined requests
1082 * to be sent in non-interactive mode.
1083 */
William Lallemand3de09d52018-12-11 16:10:56 +01001084 if (((res->flags & (CF_SHUTW|CF_SHUTW_NOW))) ||
1085 (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req) && (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001086 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01001087 continue;
1088 }
1089
1090 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001091 appctx->st0 = CLI_ST_GETREQ;
William Lallemandad032882019-07-01 10:56:15 +02001092 /* reactivate the \n at the end of the response for the next command */
1093 appctx->st1 &= ~APPCTX_CLI_ST1_NOLF;
Willy Tarreaua767d352022-01-19 17:11:36 +01001094
1095 /* this forces us to yield between pipelined commands and
1096 * avoid extremely long latencies (e.g. "del map" etc). In
1097 * addition this increases the likelihood that the stream
1098 * refills the buffer with new bytes in non-interactive
1099 * mode, avoiding to close on apparently empty commands.
1100 */
1101 if (co_data(si_oc(si))) {
1102 appctx_wakeup(appctx);
1103 goto out;
1104 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001105 }
1106 }
1107
1108 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
1109 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1110 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1111 /* Other side has closed, let's abort if we have no more processing to do
1112 * and nothing more to consume. This is comparable to a broken pipe, so
1113 * we forward the close to the request side so that it flows upstream to
1114 * the client.
1115 */
1116 si_shutw(si);
1117 }
1118
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001119 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001120 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1121 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1122 /* We have no more processing to do, and nothing more to send, and
1123 * the client side has closed. So we'll forward this state downstream
1124 * on the response buffer.
1125 */
1126 si_shutr(si);
1127 res->flags |= CF_READ_NULL;
1128 }
1129
1130 out:
Christopher Faulet45073512018-07-20 10:16:29 +02001131 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%lu, rqs=%lu, rh=%lu, rs=%lu\n",
William Lallemand74c24fb2016-11-21 17:18:36 +01001132 __FUNCTION__, __LINE__,
Christopher Faulet45073512018-07-20 10:16:29 +02001133 si->state, req->flags, res->flags, ci_data(req), co_data(req), ci_data(res), co_data(res));
William Lallemand74c24fb2016-11-21 17:18:36 +01001134}
1135
William Lallemand74c24fb2016-11-21 17:18:36 +01001136/* This is called when the stream interface is closed. For instance, upon an
1137 * external abort, we won't call the i/o handler anymore so we may need to
1138 * remove back references to the stream currently being dumped.
1139 */
1140static void cli_release_handler(struct appctx *appctx)
1141{
Willy Tarreau18b2a9d2021-05-04 16:27:45 +02001142 free_trash_chunk(appctx->chunk);
1143 appctx->chunk = NULL;
1144
William Lallemand74c24fb2016-11-21 17:18:36 +01001145 if (appctx->io_release) {
1146 appctx->io_release(appctx);
1147 appctx->io_release = NULL;
1148 }
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001149 else if (appctx->st0 == CLI_ST_PRINT_FREE || appctx->st0 == CLI_ST_PRINT_DYN) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001150 ha_free(&appctx->ctx.cli.err);
William Lallemand74c24fb2016-11-21 17:18:36 +01001151 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001152}
1153
1154/* This function dumps all environmnent variables to the buffer. It returns 0
1155 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +01001156 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
1157 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +01001158 */
Willy Tarreau0a739292016-11-22 20:21:23 +01001159static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001160{
Willy Tarreau0a739292016-11-22 20:21:23 +01001161 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001162 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +01001163
1164 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1165 return 1;
1166
1167 chunk_reset(&trash);
1168
1169 /* we have two inner loops here, one for the proxy, the other one for
1170 * the buffer.
1171 */
Willy Tarreauf6710f82016-12-16 17:45:44 +01001172 while (*var) {
1173 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +01001174
Willy Tarreau06d80a92017-10-19 14:32:15 +02001175 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001176 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 return 0;
1178 }
1179 if (appctx->st2 == STAT_ST_END)
1180 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001181 var++;
1182 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +01001183 }
1184
1185 /* dump complete */
1186 return 1;
1187}
1188
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001189/* This function dumps all file descriptors states (or the requested one) to
1190 * the buffer. It returns 0 if the output buffer is full and it needs to be
1191 * called again, otherwise non-zero. Dumps only one entry if st2 == STAT_ST_END.
1192 * It uses cli.i0 as the fd number to restart from.
1193 */
1194static int cli_io_handler_show_fd(struct appctx *appctx)
1195{
1196 struct stream_interface *si = appctx->owner;
1197 int fd = appctx->ctx.cli.i0;
Willy Tarreauca1b1572018-12-18 15:45:11 +01001198 int ret = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001199
1200 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreauca1b1572018-12-18 15:45:11 +01001201 goto end;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001202
1203 chunk_reset(&trash);
1204
Willy Tarreauca1b1572018-12-18 15:45:11 +01001205 /* isolate the threads once per round. We're limited to a buffer worth
1206 * of output anyway, it cannot last very long.
1207 */
1208 thread_isolate();
1209
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001210 /* we have two inner loops here, one for the proxy, the other one for
1211 * the buffer.
1212 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +01001213 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001214 struct fdtab fdt;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001215 const struct listener *li = NULL;
1216 const struct server *sv = NULL;
1217 const struct proxy *px = NULL;
1218 const struct connection *conn = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +02001219 const struct mux_ops *mux = NULL;
Willy Tarreau37be9532021-01-20 14:40:04 +01001220 const struct xprt_ops *xprt = NULL;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001221 const void *ctx = NULL;
Willy Tarreau37be9532021-01-20 14:40:04 +01001222 const void *xprt_ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +02001223 uint32_t conn_flags = 0;
Willy Tarreaue9ca8072018-12-19 18:40:58 +01001224 int is_back = 0;
Willy Tarreau8050efe2021-01-21 08:26:06 +01001225 int suspicious = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001226
1227 fdt = fdtab[fd];
1228
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001229 /* When DEBUG_FD is set, we also report closed FDs that have a
1230 * non-null event count to detect stuck ones.
1231 */
Willy Tarreau13c1a012020-06-29 14:23:31 +02001232 if (!fdt.owner) {
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001233#ifdef DEBUG_FD
Willy Tarreau13c1a012020-06-29 14:23:31 +02001234 if (!fdt.event_count)
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001235#endif
Willy Tarreau13c1a012020-06-29 14:23:31 +02001236 goto skip; // closed
1237 }
Willy Tarreau586f71b2020-12-11 15:54:36 +01001238 else if (fdt.iocb == sock_conn_iocb) {
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001239 conn = (const struct connection *)fdt.owner;
1240 conn_flags = conn->flags;
1241 mux = conn->mux;
1242 ctx = conn->ctx;
Willy Tarreau37be9532021-01-20 14:40:04 +01001243 xprt = conn->xprt;
1244 xprt_ctx = conn->xprt_ctx;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001245 li = objt_listener(conn->target);
1246 sv = objt_server(conn->target);
1247 px = objt_proxy(conn->target);
1248 is_back = conn_is_back(conn);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001249 if (atleast2(fdt.thread_mask))
1250 suspicious = 1;
1251 if (conn->handle.fd != fd)
1252 suspicious = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001253 }
Willy Tarreaua74cb382020-10-15 21:29:49 +02001254 else if (fdt.iocb == sock_accept_iocb)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001255 li = fdt.owner;
1256
Willy Tarreaudacfde42021-01-21 09:07:29 +01001257 if (!fdt.thread_mask)
1258 suspicious = 1;
1259
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001260 chunk_printf(&trash,
Willy Tarreau184b2122021-04-07 08:48:12 +02001261 " %5d : st=0x%06x(%c%c %c%c%c%c%c W:%c%c%c R:%c%c%c) tmask=0x%lx umask=0x%lx owner=%p iocb=%p(",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001262 fd,
1263 fdt.state,
Willy Tarreau184b2122021-04-07 08:48:12 +02001264 (fdt.state & FD_CLONED) ? 'C' : 'c',
1265 (fdt.state & FD_LINGER_RISK) ? 'L' : 'l',
Willy Tarreauf5090652021-04-06 17:23:40 +02001266 (fdt.state & FD_POLL_HUP) ? 'H' : 'h',
1267 (fdt.state & FD_POLL_ERR) ? 'E' : 'e',
1268 (fdt.state & FD_POLL_OUT) ? 'O' : 'o',
1269 (fdt.state & FD_POLL_PRI) ? 'P' : 'p',
1270 (fdt.state & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau184b2122021-04-07 08:48:12 +02001271 (fdt.state & FD_EV_SHUT_W) ? 'S' : 's',
1272 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
1273 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
1274 (fdt.state & FD_EV_SHUT_R) ? 'S' : 's',
1275 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
1276 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
Willy Tarreauc754b342018-03-30 15:00:15 +02001277 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001278 fdt.owner,
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001279 fdt.iocb);
1280 resolve_sym_name(&trash, NULL, fdt.iocb);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001281
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001282 if (!fdt.owner) {
1283 chunk_appendf(&trash, ")");
1284 }
Willy Tarreau586f71b2020-12-11 15:54:36 +01001285 else if (fdt.iocb == sock_conn_iocb) {
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001286 chunk_appendf(&trash, ") back=%d cflg=0x%08x", is_back, conn_flags);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001287
1288 if (conn->handle.fd != fd) {
1289 chunk_appendf(&trash, " fd=%d(BOGUS)", conn->handle.fd);
1290 suspicious = 1;
Willy Tarreaued989202021-02-05 10:54:52 +01001291 } else {
1292 struct sockaddr_storage sa;
1293 socklen_t salen;
1294
1295 salen = sizeof(sa);
1296 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1297 if (sa.ss_family == AF_INET)
1298 chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1299 else if (sa.ss_family == AF_INET6)
1300 chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1301 else if (sa.ss_family == AF_UNIX)
1302 chunk_appendf(&trash, " fam=unix");
1303 }
1304
1305 salen = sizeof(sa);
1306 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1307 if (sa.ss_family == AF_INET)
1308 chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1309 else if (sa.ss_family == AF_INET6)
1310 chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1311 }
Willy Tarreaudacfde42021-01-21 09:07:29 +01001312 }
1313
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001314 if (px)
1315 chunk_appendf(&trash, " px=%s", px->id);
1316 else if (sv)
Willy Tarreauab407922021-07-06 11:41:10 +02001317 chunk_appendf(&trash, " sv=%s/%s", sv->proxy->id, sv->id);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001318 else if (li)
1319 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +02001320
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001321 if (mux) {
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001322 chunk_appendf(&trash, " mux=%s ctx=%p", mux->name, ctx);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001323 if (!ctx)
1324 suspicious = 1;
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001325 if (mux->show_fd)
Willy Tarreau8050efe2021-01-21 08:26:06 +01001326 suspicious |= mux->show_fd(&trash, fdt.owner);
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001327 }
Willy Tarreau35b1b482018-03-28 18:41:30 +02001328 else
1329 chunk_appendf(&trash, " nomux");
Willy Tarreau37be9532021-01-20 14:40:04 +01001330
1331 chunk_appendf(&trash, " xprt=%s", xprt ? xprt->name : "");
Willy Tarreau108a2712021-01-20 15:30:56 +01001332 if (xprt) {
1333 if (xprt_ctx || xprt->show_fd)
1334 chunk_appendf(&trash, " xprt_ctx=%p", xprt_ctx);
1335 if (xprt->show_fd)
Willy Tarreau8050efe2021-01-21 08:26:06 +01001336 suspicious |= xprt->show_fd(&trash, conn, xprt_ctx);
Willy Tarreau108a2712021-01-20 15:30:56 +01001337 }
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001338 }
Willy Tarreaua74cb382020-10-15 21:29:49 +02001339 else if (fdt.iocb == sock_accept_iocb) {
Willy Tarreaued989202021-02-05 10:54:52 +01001340 struct sockaddr_storage sa;
1341 socklen_t salen;
1342
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001343 chunk_appendf(&trash, ") l.st=%s fe=%s",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001344 listener_state_str(li),
1345 li->bind_conf->frontend->id);
Willy Tarreaued989202021-02-05 10:54:52 +01001346
1347 salen = sizeof(sa);
1348 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1349 if (sa.ss_family == AF_INET)
1350 chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1351 else if (sa.ss_family == AF_INET6)
1352 chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1353 else if (sa.ss_family == AF_UNIX)
1354 chunk_appendf(&trash, " fam=unix");
1355 }
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001356 }
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001357 else
1358 chunk_appendf(&trash, ")");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001359
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001360#ifdef DEBUG_FD
1361 chunk_appendf(&trash, " evcnt=%u", fdtab[fd].event_count);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001362 if (fdtab[fd].event_count >= 1000000)
1363 suspicious = 1;
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001364#endif
Willy Tarreau8050efe2021-01-21 08:26:06 +01001365 chunk_appendf(&trash, "%s\n", suspicious ? " !" : "");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001366
Willy Tarreau06d80a92017-10-19 14:32:15 +02001367 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001368 si_rx_room_blk(si);
Willy Tarreauca1b1572018-12-18 15:45:11 +01001369 appctx->ctx.cli.i0 = fd;
1370 ret = 0;
1371 break;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001372 }
1373 skip:
1374 if (appctx->st2 == STAT_ST_END)
1375 break;
1376
1377 fd++;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001378 }
1379
Willy Tarreauca1b1572018-12-18 15:45:11 +01001380 end:
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001381 /* dump complete */
Willy Tarreauca1b1572018-12-18 15:45:11 +01001382
1383 thread_release();
1384 return ret;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001385}
1386
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001387/* This function dumps some activity counters used by developers and support to
1388 * rule out some hypothesis during bug reports. It returns 0 if the output
1389 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
1390 * everything at once in the buffer and is not designed to do it in multiple
1391 * passes.
1392 */
1393static int cli_io_handler_show_activity(struct appctx *appctx)
1394{
1395 struct stream_interface *si = appctx->owner;
1396 int thr;
1397
1398 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1399 return 1;
1400
1401 chunk_reset(&trash);
1402
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001403#undef SHOW_TOT
1404#define SHOW_TOT(t, x) \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001405 do { \
1406 unsigned int _v[MAX_THREADS]; \
1407 unsigned int _tot; \
1408 const unsigned int _nbt = global.nbthread; \
Willy Tarreaub9aac362021-11-20 19:17:38 +01001409 _tot = t = 0; \
1410 do { \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001411 _tot += _v[t] = (x); \
Willy Tarreaub9aac362021-11-20 19:17:38 +01001412 } while (++t < _nbt); \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001413 if (_nbt == 1) { \
1414 chunk_appendf(&trash, " %u\n", _tot); \
1415 break; \
1416 } \
1417 chunk_appendf(&trash, " %u [", _tot); \
1418 for (t = 0; t < _nbt; t++) \
1419 chunk_appendf(&trash, " %u", _v[t]); \
1420 chunk_appendf(&trash, " ]\n"); \
1421 } while (0)
1422
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001423#undef SHOW_AVG
1424#define SHOW_AVG(t, x) \
1425 do { \
1426 unsigned int _v[MAX_THREADS]; \
1427 unsigned int _tot; \
1428 const unsigned int _nbt = global.nbthread; \
Willy Tarreaub9aac362021-11-20 19:17:38 +01001429 _tot = t = 0; \
1430 do { \
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001431 _tot += _v[t] = (x); \
Willy Tarreaub9aac362021-11-20 19:17:38 +01001432 } while (++t < _nbt); \
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001433 if (_nbt == 1) { \
1434 chunk_appendf(&trash, " %u\n", _tot); \
1435 break; \
1436 } \
1437 chunk_appendf(&trash, " %u [", (_tot + _nbt/2) / _nbt); \
1438 for (t = 0; t < _nbt; t++) \
1439 chunk_appendf(&trash, " %u", _v[t]); \
1440 chunk_appendf(&trash, " ]\n"); \
1441 } while (0)
1442
Willy Tarreaua0211b82019-05-28 15:09:08 +02001443 chunk_appendf(&trash, "thread_id: %u (%u..%u)\n", tid + 1, 1, global.nbthread);
1444 chunk_appendf(&trash, "date_now: %lu.%06lu\n", (long)now.tv_sec, (long)now.tv_usec);
Willy Tarreaua00cf9b2020-06-17 20:44:28 +02001445 chunk_appendf(&trash, "ctxsw:"); SHOW_TOT(thr, activity[thr].ctxsw);
1446 chunk_appendf(&trash, "tasksw:"); SHOW_TOT(thr, activity[thr].tasksw);
1447 chunk_appendf(&trash, "empty_rq:"); SHOW_TOT(thr, activity[thr].empty_rq);
1448 chunk_appendf(&trash, "long_rq:"); SHOW_TOT(thr, activity[thr].long_rq);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001449 chunk_appendf(&trash, "loops:"); SHOW_TOT(thr, activity[thr].loops);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001450 chunk_appendf(&trash, "wake_tasks:"); SHOW_TOT(thr, activity[thr].wake_tasks);
1451 chunk_appendf(&trash, "wake_signal:"); SHOW_TOT(thr, activity[thr].wake_signal);
Willy Tarreaue5451532020-06-17 20:25:18 +02001452 chunk_appendf(&trash, "poll_io:"); SHOW_TOT(thr, activity[thr].poll_io);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001453 chunk_appendf(&trash, "poll_exp:"); SHOW_TOT(thr, activity[thr].poll_exp);
Willy Tarreaue4063862020-06-17 20:35:33 +02001454 chunk_appendf(&trash, "poll_drop_fd:"); SHOW_TOT(thr, activity[thr].poll_drop_fd);
1455 chunk_appendf(&trash, "poll_dead_fd:"); SHOW_TOT(thr, activity[thr].poll_dead_fd);
1456 chunk_appendf(&trash, "poll_skip_fd:"); SHOW_TOT(thr, activity[thr].poll_skip_fd);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001457 chunk_appendf(&trash, "conn_dead:"); SHOW_TOT(thr, activity[thr].conn_dead);
Willy Tarreau7af4fa92020-06-17 20:49:49 +02001458 chunk_appendf(&trash, "stream_calls:"); SHOW_TOT(thr, activity[thr].stream_calls);
Willy Tarreaua8b2ce02019-05-28 17:04:16 +02001459 chunk_appendf(&trash, "pool_fail:"); SHOW_TOT(thr, activity[thr].pool_fail);
1460 chunk_appendf(&trash, "buf_wait:"); SHOW_TOT(thr, activity[thr].buf_wait);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001461 chunk_appendf(&trash, "cpust_ms_tot:"); SHOW_TOT(thr, activity[thr].cpust_total / 2);
1462 chunk_appendf(&trash, "cpust_ms_1s:"); SHOW_TOT(thr, read_freq_ctr(&activity[thr].cpust_1s) / 2);
1463 chunk_appendf(&trash, "cpust_ms_15s:"); SHOW_TOT(thr, read_freq_ctr_period(&activity[thr].cpust_15s, 15000) / 2);
1464 chunk_appendf(&trash, "avg_loop_us:"); SHOW_AVG(thr, swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES));
1465 chunk_appendf(&trash, "accepted:"); SHOW_TOT(thr, activity[thr].accepted);
1466 chunk_appendf(&trash, "accq_pushed:"); SHOW_TOT(thr, activity[thr].accq_pushed);
1467 chunk_appendf(&trash, "accq_full:"); SHOW_TOT(thr, activity[thr].accq_full);
Willy Tarreaue6182842019-04-15 18:54:10 +02001468#ifdef USE_THREAD
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001469 chunk_appendf(&trash, "accq_ring:"); SHOW_TOT(thr, (accept_queue_rings[thr].tail - accept_queue_rings[thr].head + ACCEPT_QUEUE_SIZE) % ACCEPT_QUEUE_SIZE);
Willy Tarreaub1591322020-06-29 14:17:59 +02001470 chunk_appendf(&trash, "fd_takeover:"); SHOW_TOT(thr, activity[thr].fd_takeover);
Willy Tarreaue6182842019-04-15 18:54:10 +02001471#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001472
Willy Tarreaud6a78502019-05-27 07:03:38 +02001473#if defined(DEBUG_DEV)
1474 /* keep these ones at the end */
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001475 chunk_appendf(&trash, "ctr0:"); SHOW_TOT(thr, activity[thr].ctr0);
1476 chunk_appendf(&trash, "ctr1:"); SHOW_TOT(thr, activity[thr].ctr1);
1477 chunk_appendf(&trash, "ctr2:"); SHOW_TOT(thr, activity[thr].ctr2);
Willy Tarreaud6a78502019-05-27 07:03:38 +02001478#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001479
1480 if (ci_putchk(si_ic(si), &trash) == -1) {
1481 chunk_reset(&trash);
1482 chunk_printf(&trash, "[output too large, cannot dump]\n");
Willy Tarreaudb398432018-11-15 11:08:52 +01001483 si_rx_room_blk(si);
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001484 }
1485
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001486#undef SHOW_AVG
1487#undef SHOW_TOT
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001488 /* dump complete */
1489 return 1;
1490}
1491
William Lallemandeceddf72016-12-15 18:06:44 +01001492/*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001493 * CLI IO handler for `show cli sockets`.
Willy Tarreaua265d382022-05-05 18:29:25 +02001494 * Uses ctx.cli.p0 to store the bind_conf pointer, and cli.p1 for the listener.
William Lallemandeceddf72016-12-15 18:06:44 +01001495 */
1496static int cli_io_handler_show_cli_sock(struct appctx *appctx)
1497{
Willy Tarreaua265d382022-05-05 18:29:25 +02001498 struct bind_conf *bind_conf = appctx->ctx.cli.p0;
William Lallemandeceddf72016-12-15 18:06:44 +01001499 struct stream_interface *si = appctx->owner;
1500
1501 chunk_reset(&trash);
1502
1503 switch (appctx->st2) {
1504 case STAT_ST_INIT:
1505 chunk_printf(&trash, "# socket lvl processes\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001506 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001507 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001508 return 0;
1509 }
William Lallemandeceddf72016-12-15 18:06:44 +01001510 appctx->st2 = STAT_ST_LIST;
Tim Duesterhus588b3142020-05-29 14:35:51 +02001511 /* fall through */
William Lallemandeceddf72016-12-15 18:06:44 +01001512
1513 case STAT_ST_LIST:
Willy Tarreau4975d142021-03-13 11:00:33 +01001514 if (global.cli_fe) {
Willy Tarreaua265d382022-05-05 18:29:25 +02001515 if (!bind_conf)
1516 bind_conf = LIST_ELEM(global.cli_fe->conf.bind.n, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +01001517
Willy Tarreaua265d382022-05-05 18:29:25 +02001518 list_for_each_entry_from(bind_conf, &global.cli_fe->conf.bind, by_fe) {
1519 struct listener *l = appctx->ctx.cli.p1;
William Lallemandeceddf72016-12-15 18:06:44 +01001520
Willy Tarreaua265d382022-05-05 18:29:25 +02001521 if (!l)
1522 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
William Lallemandeceddf72016-12-15 18:06:44 +01001523
Willy Tarreaua265d382022-05-05 18:29:25 +02001524 list_for_each_entry_from(l, &bind_conf->listeners, by_bind) {
William Lallemandeceddf72016-12-15 18:06:44 +01001525 char addr[46];
1526 char port[6];
1527
Willy Tarreau37159062020-08-27 07:48:42 +02001528 if (l->rx.addr.ss_family == AF_UNIX) {
William Lallemandeceddf72016-12-15 18:06:44 +01001529 const struct sockaddr_un *un;
1530
Willy Tarreau37159062020-08-27 07:48:42 +02001531 un = (struct sockaddr_un *)&l->rx.addr;
William Lallemande58915f2019-04-01 11:30:05 +02001532 if (un->sun_path[0] == '\0') {
William Lallemand75812a72019-04-01 11:30:04 +02001533 chunk_appendf(&trash, "abns@%s ", un->sun_path+1);
William Lallemande58915f2019-04-01 11:30:05 +02001534 } else {
1535 chunk_appendf(&trash, "unix@%s ", un->sun_path);
1536 }
Willy Tarreau37159062020-08-27 07:48:42 +02001537 } else if (l->rx.addr.ss_family == AF_INET) {
1538 addr_to_str(&l->rx.addr, addr, sizeof(addr));
1539 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemande58915f2019-04-01 11:30:05 +02001540 chunk_appendf(&trash, "ipv4@%s:%s ", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +02001541 } else if (l->rx.addr.ss_family == AF_INET6) {
1542 addr_to_str(&l->rx.addr, addr, sizeof(addr));
1543 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemande58915f2019-04-01 11:30:05 +02001544 chunk_appendf(&trash, "ipv6@[%s]:%s ", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +02001545 } else if (l->rx.addr.ss_family == AF_CUST_SOCKPAIR) {
1546 chunk_appendf(&trash, "sockpair@%d ", ((struct sockaddr_in *)&l->rx.addr)->sin_addr.s_addr);
William Lallemandeceddf72016-12-15 18:06:44 +01001547 } else
William Lallemand26314342018-10-26 14:47:41 +02001548 chunk_appendf(&trash, "unknown ");
William Lallemandeceddf72016-12-15 18:06:44 +01001549
William Lallemand07a62f72017-05-24 00:57:40 +02001550 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
William Lallemandeceddf72016-12-15 18:06:44 +01001551 chunk_appendf(&trash, "admin ");
William Lallemand07a62f72017-05-24 00:57:40 +02001552 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1553 chunk_appendf(&trash, "operator ");
1554 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1555 chunk_appendf(&trash, "user ");
William Lallemandeceddf72016-12-15 18:06:44 +01001556 else
1557 chunk_appendf(&trash, " ");
1558
Willy Tarreaue26993c2020-09-03 07:18:55 +02001559 if (bind_conf->settings.bind_proc != 0) {
William Lallemandeceddf72016-12-15 18:06:44 +01001560 int pos;
Willy Tarreaue26993c2020-09-03 07:18:55 +02001561 for (pos = 0; pos < 8 * sizeof(bind_conf->settings.bind_proc); pos++) {
1562 if (bind_conf->settings.bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +01001563 chunk_appendf(&trash, "%d,", pos+1);
1564 }
1565 }
1566 /* replace the latest comma by a newline */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 trash.area[trash.data-1] = '\n';
William Lallemandeceddf72016-12-15 18:06:44 +01001568
1569 } else {
1570 chunk_appendf(&trash, "all\n");
1571 }
1572
Willy Tarreau06d80a92017-10-19 14:32:15 +02001573 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaua265d382022-05-05 18:29:25 +02001574 /* buffer full, we must yield */
1575 appctx->ctx.cli.p0 = bind_conf;
1576 appctx->ctx.cli.p1 = l;
Willy Tarreaudb398432018-11-15 11:08:52 +01001577 si_rx_room_blk(si);
William Lallemandeceddf72016-12-15 18:06:44 +01001578 return 0;
1579 }
1580 }
William Lallemandeceddf72016-12-15 18:06:44 +01001581 }
1582 }
Tim Duesterhus588b3142020-05-29 14:35:51 +02001583 /* fall through */
William Lallemandeceddf72016-12-15 18:06:44 +01001584 default:
1585 appctx->st2 = STAT_ST_FIN;
1586 return 1;
1587 }
1588}
1589
1590
Willy Tarreau0a739292016-11-22 20:21:23 +01001591/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +01001592 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
1593 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001594 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001595static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001596{
1597 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001598 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001599
1600 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1601 return 1;
1602
Willy Tarreauf6710f82016-12-16 17:45:44 +01001603 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001604
1605 if (*args[2]) {
1606 int len = strlen(args[2]);
1607
Willy Tarreauf6710f82016-12-16 17:45:44 +01001608 for (; *var; var++) {
1609 if (strncmp(*var, args[2], len) == 0 &&
1610 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001611 break;
1612 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001613 if (!*var)
1614 return cli_err(appctx, "Variable not found\n");
1615
Willy Tarreau0a739292016-11-22 20:21:23 +01001616 appctx->st2 = STAT_ST_END;
1617 }
Willy Tarreauf6710f82016-12-16 17:45:44 +01001618 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001619 return 0;
1620}
1621
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001622/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
1623 * wants to stop here. It puts the FD number into cli.i0 if a specific FD is
1624 * requested and sets st2 to STAT_ST_END, otherwise leaves 0 in i0.
1625 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001626static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001627{
1628 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1629 return 1;
1630
1631 appctx->ctx.cli.i0 = 0;
1632
1633 if (*args[2]) {
1634 appctx->ctx.cli.i0 = atoi(args[2]);
1635 appctx->st2 = STAT_ST_END;
1636 }
1637 return 0;
1638}
1639
Willy Tarreau599852e2016-11-22 20:33:32 +01001640/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001641static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001642{
1643 struct stream_interface *si = appctx->owner;
1644 struct stream *s = si_strm(si);
1645
1646 if (strcmp(args[2], "cli") == 0) {
1647 unsigned timeout;
1648 const char *res;
1649
Willy Tarreau9d008692019-08-09 11:21:01 +02001650 if (!*args[3])
1651 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001652
1653 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
Willy Tarreau9d008692019-08-09 11:21:01 +02001654 if (res || timeout < 1)
1655 return cli_err(appctx, "Invalid timeout value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001656
1657 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1658 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1659 return 1;
1660 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001661
1662 return cli_err(appctx, "'set timeout' only supports 'cli'.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001663}
1664
Willy Tarreau2af99412016-11-23 11:10:59 +01001665/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001666static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001667{
1668 int v;
1669
1670 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1671 return 1;
1672
Willy Tarreau9d008692019-08-09 11:21:01 +02001673 if (!*args[3])
1674 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001675
1676 v = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02001677 if (v > global.hardmaxconn)
1678 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001679
1680 /* check for unlimited values */
1681 if (v <= 0)
1682 v = global.hardmaxconn;
1683
1684 global.maxconn = v;
1685
1686 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001687 dequeue_all_listeners();
Willy Tarreau2af99412016-11-23 11:10:59 +01001688
1689 return 1;
1690}
1691
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001692static int set_severity_output(int *target, char *argument)
1693{
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001694 if (strcmp(argument, "none") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001695 *target = CLI_SEVERITY_NONE;
1696 return 1;
1697 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001698 else if (strcmp(argument, "number") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001699 *target = CLI_SEVERITY_NUMBER;
1700 return 1;
1701 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001702 else if (strcmp(argument, "string") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001703 *target = CLI_SEVERITY_STRING;
1704 return 1;
1705 }
1706 return 0;
1707}
1708
1709/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001710static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001711{
1712 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1713 return 0;
1714
Willy Tarreau9d008692019-08-09 11:21:01 +02001715 return cli_err(appctx, "one of 'none', 'number', 'string' is a required argument\n");
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001716}
William Lallemandeceddf72016-12-15 18:06:44 +01001717
William Lallemand67a234f2018-12-13 09:05:45 +01001718
1719/* show the level of the current CLI session */
1720static int cli_parse_show_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1721{
William Lallemand67a234f2018-12-13 09:05:45 +01001722 if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
Willy Tarreau9d008692019-08-09 11:21:01 +02001723 return cli_msg(appctx, LOG_INFO, "admin\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001724 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001725 return cli_msg(appctx, LOG_INFO, "operator\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001726 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001727 return cli_msg(appctx, LOG_INFO, "user\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001728 else
Willy Tarreau9d008692019-08-09 11:21:01 +02001729 return cli_msg(appctx, LOG_INFO, "unknown\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001730}
1731
1732/* parse and set the CLI level dynamically */
1733static int cli_parse_set_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1734{
William Lallemandad032882019-07-01 10:56:15 +02001735 /* this will ask the applet to not output a \n after the command */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001736 if (strcmp(args[1], "-") == 0)
William Lallemandad032882019-07-01 10:56:15 +02001737 appctx->st1 |= APPCTX_CLI_ST1_NOLF;
1738
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001739 if (strcmp(args[0], "operator") == 0) {
William Lallemand67a234f2018-12-13 09:05:45 +01001740 if (!cli_has_level(appctx, ACCESS_LVL_OPER)) {
1741 return 1;
1742 }
1743 appctx->cli_level &= ~ACCESS_LVL_MASK;
1744 appctx->cli_level |= ACCESS_LVL_OPER;
1745
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001746 } else if (strcmp(args[0], "user") == 0) {
William Lallemand67a234f2018-12-13 09:05:45 +01001747 if (!cli_has_level(appctx, ACCESS_LVL_USER)) {
1748 return 1;
1749 }
1750 appctx->cli_level &= ~ACCESS_LVL_MASK;
1751 appctx->cli_level |= ACCESS_LVL_USER;
1752 }
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001753 appctx->cli_level &= ~(ACCESS_EXPERT|ACCESS_EXPERIMENTAL);
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001754 return 1;
1755}
1756
1757
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001758/* parse and set the CLI expert/experimental-mode dynamically */
1759static int cli_parse_expert_experimental_mode(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001760{
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001761 int level;
1762 char *level_str;
1763 char *output = NULL;
1764
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001765 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1766 return 1;
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001767
1768 if (!strcmp(args[0], "expert-mode")) {
1769 level = ACCESS_EXPERT;
1770 level_str = "expert-mode";
1771 }
1772 else if (!strcmp(args[0], "experimental-mode")) {
1773 level = ACCESS_EXPERIMENTAL;
1774 level_str = "experimental-mode";
1775 }
1776 else {
1777 return 1;
1778 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001779
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001780 if (!*args[1]) {
1781 memprintf(&output, "%s is %s\n", level_str,
1782 (appctx->cli_level & level) ? "ON" : "OFF");
1783 return cli_dynmsg(appctx, LOG_INFO, output);
1784 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001785
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001786 appctx->cli_level &= ~level;
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001787 if (strcmp(args[1], "on") == 0)
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001788 appctx->cli_level |= level;
William Lallemand67a234f2018-12-13 09:05:45 +01001789 return 1;
1790}
1791
William Lallemand7ceae112021-12-14 15:22:29 +01001792/* shows HAProxy version */
1793static int cli_parse_show_version(char **args, char *payload, struct appctx *appctx, void *private)
1794{
1795 char *msg = NULL;
1796
1797 return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", haproxy_version));
1798}
1799
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001800int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001801{
1802 return 0;
1803}
1804
Willy Tarreau45c742b2016-11-24 14:51:17 +01001805/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001806static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001807{
1808 int v;
1809 int *res;
1810 int mul = 1;
1811
1812 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1813 return 1;
1814
1815 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1816 res = &global.cps_lim;
1817 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1818 res = &global.sps_lim;
1819#ifdef USE_OPENSSL
1820 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1821 res = &global.ssl_lim;
1822#endif
1823 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1824 res = &global.comp_rate_lim;
1825 mul = 1024;
1826 }
1827 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02001828 return cli_err(appctx,
Willy Tarreau45c742b2016-11-24 14:51:17 +01001829 "'set rate-limit' only supports :\n"
1830 " - 'connections global' to set the per-process maximum connection rate\n"
1831 " - 'sessions global' to set the per-process maximum session rate\n"
1832#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01001833 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01001834#endif
Willy Tarreau9d008692019-08-09 11:21:01 +02001835 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001836 }
1837
Willy Tarreau9d008692019-08-09 11:21:01 +02001838 if (!*args[4])
1839 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001840
1841 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02001842 if (v < 0)
1843 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01001844
1845 *res = v * mul;
1846
1847 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001848 dequeue_all_listeners();
Willy Tarreau45c742b2016-11-24 14:51:17 +01001849
1850 return 1;
1851}
1852
William Lallemandf6975e92017-05-26 17:42:10 +02001853/* parse the "expose-fd" argument on the bind lines */
1854static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1855{
1856 if (!*args[cur_arg + 1]) {
1857 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
1858 return ERR_ALERT | ERR_FATAL;
1859 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001860 if (strcmp(args[cur_arg + 1], "listeners") == 0) {
William Lallemandf6975e92017-05-26 17:42:10 +02001861 conf->level |= ACCESS_FD_LISTENERS;
1862 } else {
1863 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
1864 args[cur_arg], args[cur_arg+1]);
1865 return ERR_ALERT | ERR_FATAL;
1866 }
1867
1868 return 0;
1869}
1870
William Lallemand74c24fb2016-11-21 17:18:36 +01001871/* parse the "level" argument on the bind lines */
1872static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1873{
1874 if (!*args[cur_arg + 1]) {
1875 memprintf(err, "'%s' : missing level", args[cur_arg]);
1876 return ERR_ALERT | ERR_FATAL;
1877 }
1878
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001879 if (strcmp(args[cur_arg + 1], "user") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02001880 conf->level &= ~ACCESS_LVL_MASK;
1881 conf->level |= ACCESS_LVL_USER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001882 } else if (strcmp(args[cur_arg + 1], "operator") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02001883 conf->level &= ~ACCESS_LVL_MASK;
1884 conf->level |= ACCESS_LVL_OPER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001885 } else if (strcmp(args[cur_arg + 1], "admin") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02001886 conf->level &= ~ACCESS_LVL_MASK;
1887 conf->level |= ACCESS_LVL_ADMIN;
1888 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1890 args[cur_arg], args[cur_arg+1]);
1891 return ERR_ALERT | ERR_FATAL;
1892 }
1893
1894 return 0;
1895}
1896
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001897static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
1898{
1899 if (!*args[cur_arg + 1]) {
1900 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
1901 return ERR_ALERT | ERR_FATAL;
1902 }
1903
1904 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
1905 return 0;
1906 else {
1907 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
1908 args[cur_arg], args[cur_arg+1]);
1909 return ERR_ALERT | ERR_FATAL;
1910 }
1911}
1912
Olivier Houchardf886e342017-04-05 22:24:59 +02001913/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001914static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02001915{
1916 char *cmsgbuf = NULL;
1917 unsigned char *tmpbuf = NULL;
1918 struct cmsghdr *cmsg;
1919 struct stream_interface *si = appctx->owner;
William Lallemandf6975e92017-05-26 17:42:10 +02001920 struct stream *s = si_strm(si);
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001921 struct connection *remote = cs_conn(objt_cs(si_opposite(si)->end));
Olivier Houchardf886e342017-04-05 22:24:59 +02001922 struct msghdr msghdr;
1923 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02001924 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02001925 const char *ns_name, *if_name;
1926 unsigned char ns_nlen, if_nlen;
1927 int nb_queued;
1928 int cur_fd = 0;
Olivier Houchardf886e342017-04-05 22:24:59 +02001929 int *tmpfd;
1930 int tot_fd_nb = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001931 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001932 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001933 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02001934 int ret;
1935
Willy Tarreauc2b7f802018-09-20 11:22:29 +02001936 if (!remote) {
1937 ha_warning("Only works on real connections\n");
1938 goto out;
1939 }
1940
1941 fd = remote->handle.fd;
1942
Olivier Houchardf886e342017-04-05 22:24:59 +02001943 /* Temporary set the FD in blocking mode, that will make our life easier */
1944 old_fcntl = fcntl(fd, F_GETFL);
1945 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001946 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001947 goto out;
1948 }
1949 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
1950 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001951 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001952 goto out;
1953 }
1954 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001955 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001956 goto out;
1957 }
Olivier Houchard54740872017-04-06 14:45:14 +02001958 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02001959 iov.iov_base = &tot_fd_nb;
1960 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02001961 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02001962 goto out;
1963 memset(&msghdr, 0, sizeof(msghdr));
1964 /*
1965 * First, calculates the total number of FD, so that we can let
Jackie Tapia749f74c2020-07-22 18:59:40 -05001966 * the caller know how much it should expect.
Olivier Houchardf886e342017-04-05 22:24:59 +02001967 */
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02001968 for (cur_fd = 0;cur_fd < global.maxsock; cur_fd++)
Willy Tarreau9063a662021-04-06 18:09:06 +02001969 tot_fd_nb += !!(fdtab[cur_fd].state & FD_EXPORTED);
William Lallemand5fd3b282020-01-16 15:32:08 +01001970
Olivier Houchardf886e342017-04-05 22:24:59 +02001971 if (tot_fd_nb == 0)
1972 goto out;
1973
1974 /* First send the total number of file descriptors, so that the
1975 * receiving end knows what to expect.
1976 */
1977 msghdr.msg_iov = &iov;
1978 msghdr.msg_iovlen = 1;
1979 ret = sendmsg(fd, &msghdr, 0);
1980 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001981 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02001982 goto out;
1983 }
1984
1985 /* Now send the fds */
1986 msghdr.msg_control = cmsgbuf;
1987 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
1988 cmsg = CMSG_FIRSTHDR(&msghdr);
1989 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
1990 cmsg->cmsg_level = SOL_SOCKET;
1991 cmsg->cmsg_type = SCM_RIGHTS;
1992 tmpfd = (int *)CMSG_DATA(cmsg);
1993
Olivier Houchardf886e342017-04-05 22:24:59 +02001994 /* For each socket, e message is sent, containing the following :
1995 * Size of the namespace name (or 0 if none), as an unsigned char.
1996 * The namespace name, if any
1997 * Size of the interface name (or 0 if none), as an unsigned char
1998 * The interface name, if any
Willy Tarreaucf1f1932020-08-26 10:30:09 +02001999 * 32 bits of zeroes (used to be listener options).
Olivier Houchardf886e342017-04-05 22:24:59 +02002000 */
2001 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
Ilya Shipitsind4259502020-04-08 01:07:56 +05002002 * buffer big enough to store the socket information.
Olivier Houchardf886e342017-04-05 22:24:59 +02002003 */
Olivier Houchardf143b802017-11-04 15:13:01 +01002004 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02002005 if (tmpbuf == NULL) {
Ilya Shipitsind4259502020-04-08 01:07:56 +05002006 ha_warning("Failed to allocate memory to transfer socket information\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002007 goto out;
2008 }
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002009
2010 nb_queued = 0;
Olivier Houchardf886e342017-04-05 22:24:59 +02002011 iov.iov_base = tmpbuf;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002012 for (cur_fd = 0; cur_fd < global.maxsock; cur_fd++) {
Willy Tarreau9063a662021-04-06 18:09:06 +02002013 if (!(fdtab[cur_fd].state & FD_EXPORTED))
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002014 continue;
2015
2016 ns_name = if_name = "";
2017 ns_nlen = if_nlen = 0;
2018
2019 /* for now we can only retrieve namespaces and interfaces from
2020 * pure listeners.
2021 */
Willy Tarreaua74cb382020-10-15 21:29:49 +02002022 if (fdtab[cur_fd].iocb == sock_accept_iocb) {
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002023 const struct listener *l = fdtab[cur_fd].owner;
2024
Willy Tarreau818a92e2020-09-03 07:50:19 +02002025 if (l->rx.settings->interface) {
2026 if_name = l->rx.settings->interface;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002027 if_nlen = strlen(if_name);
2028 }
2029
2030#ifdef USE_NS
Willy Tarreau818a92e2020-09-03 07:50:19 +02002031 if (l->rx.settings->netns) {
2032 ns_name = l->rx.settings->netns->node.key;
2033 ns_nlen = l->rx.settings->netns->name_len;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002034 }
2035#endif
2036 }
2037
2038 /* put the FD into the CMSG_DATA */
2039 tmpfd[nb_queued++] = cur_fd;
2040
2041 /* first block is <ns_name_len> <ns_name> */
2042 tmpbuf[curoff++] = ns_nlen;
2043 if (ns_nlen)
2044 memcpy(tmpbuf + curoff, ns_name, ns_nlen);
2045 curoff += ns_nlen;
2046
2047 /* second block is <if_name_len> <if_name> */
2048 tmpbuf[curoff++] = if_nlen;
2049 if (if_nlen)
2050 memcpy(tmpbuf + curoff, if_name, if_nlen);
2051 curoff += if_nlen;
2052
2053 /* we used to send the listener options here before 2.3 */
2054 memset(tmpbuf + curoff, 0, sizeof(int));
2055 curoff += sizeof(int);
2056
2057 /* there's a limit to how many FDs may be sent at once */
2058 if (nb_queued == MAX_SEND_FD) {
2059 iov.iov_len = curoff;
2060 if (sendmsg(fd, &msghdr, 0) != curoff) {
2061 ha_warning("Failed to transfer sockets\n");
2062 return -1;
2063 }
2064
2065 /* Wait for an ack */
2066 do {
2067 ret = recv(fd, &tot_fd_nb, sizeof(tot_fd_nb), 0);
2068 } while (ret == -1 && errno == EINTR);
2069
2070 if (ret <= 0) {
2071 ha_warning("Unexpected error while transferring sockets\n");
2072 return -1;
2073 }
2074 curoff = 0;
2075 nb_queued = 0;
2076 }
William Lallemand5fd3b282020-01-16 15:32:08 +01002077 }
2078
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002079 /* flush pending stuff */
2080 if (nb_queued) {
Olivier Houchardf886e342017-04-05 22:24:59 +02002081 iov.iov_len = curoff;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002082 cmsg->cmsg_len = CMSG_LEN(nb_queued * sizeof(int));
2083 msghdr.msg_controllen = CMSG_SPACE(nb_queued * sizeof(int));
Olivier Houchardf886e342017-04-05 22:24:59 +02002084 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002085 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002086 goto out;
2087 }
2088 }
2089
2090out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02002091 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002093 goto out;
2094 }
2095 appctx->st0 = CLI_ST_END;
2096 free(cmsgbuf);
2097 free(tmpbuf);
2098 return 1;
2099}
2100
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002101static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
2102{
2103 if (*args[0] == 'h')
2104 /* help */
Willy Tarreau0b1b8302021-05-09 20:59:23 +02002105 cli_gen_usage_msg(appctx, args);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002106 else if (*args[0] == 'p')
2107 /* prompt */
2108 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
2109 else if (*args[0] == 'q')
2110 /* quit */
2111 appctx->st0 = CLI_ST_END;
2112
2113 return 1;
2114}
Olivier Houchardf886e342017-04-05 22:24:59 +02002115
William Lallemand2f4ce202018-10-26 14:47:47 +02002116void pcli_write_prompt(struct stream *s)
2117{
2118 struct buffer *msg = get_trash_chunk();
2119 struct channel *oc = si_oc(&s->si[0]);
2120
William Lallemanddc12c2e2018-12-13 09:05:46 +01002121 if (!(s->pcli_flags & PCLI_F_PROMPT))
William Lallemand5b80fa22018-12-11 16:10:54 +01002122 return;
2123
William Lallemanddc12c2e2018-12-13 09:05:46 +01002124 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01002125 chunk_appendf(msg, "+ ");
2126 } else {
2127 if (s->pcli_next_pid == 0)
Willy Tarreau52880f92018-12-15 13:30:03 +01002128 chunk_appendf(msg, "master%s> ",
2129 (global.mode & MODE_MWORKER_WAIT) ? "[ReloadFailed]" : "");
William Lallemandebf61802018-12-11 16:10:57 +01002130 else
2131 chunk_appendf(msg, "%d> ", s->pcli_next_pid);
2132 }
William Lallemand2f4ce202018-10-26 14:47:47 +02002133 co_inject(oc, msg->area, msg->data);
2134}
2135
William Lallemand291810d2018-10-26 14:47:38 +02002136
William Lallemandcf62f7e2018-10-26 14:47:40 +02002137/* The pcli_* functions are used for the CLI proxy in the master */
2138
William Lallemanddeeaa592018-10-26 14:47:48 +02002139void pcli_reply_and_close(struct stream *s, const char *msg)
2140{
2141 struct buffer *buf = get_trash_chunk();
2142
2143 chunk_initstr(buf, msg);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002144 si_retnclose(&s->si[0], buf);
William Lallemanddeeaa592018-10-26 14:47:48 +02002145}
2146
William Lallemand291810d2018-10-26 14:47:38 +02002147static enum obj_type *pcli_pid_to_server(int proc_pid)
2148{
2149 struct mworker_proc *child;
2150
William Lallemand99e0bb92020-11-05 10:28:53 +01002151 /* return the mCLI applet of the master */
William Lallemandbddd33a2018-12-11 16:10:53 +01002152 if (proc_pid == 0)
William Lallemand99e0bb92020-11-05 10:28:53 +01002153 return &mcli_applet.obj_type;
William Lallemandbddd33a2018-12-11 16:10:53 +01002154
William Lallemand291810d2018-10-26 14:47:38 +02002155 list_for_each_entry(child, &proc_list, list) {
2156 if (child->pid == proc_pid){
2157 return &child->srv->obj_type;
2158 }
2159 }
2160 return NULL;
2161}
2162
2163/* Take a CLI prefix in argument (eg: @!1234 @master @1)
2164 * Return:
2165 * 0: master
2166 * > 0: pid of a worker
2167 * < 0: didn't find a worker
2168 */
2169static int pcli_prefix_to_pid(const char *prefix)
2170{
2171 int proc_pid;
2172 struct mworker_proc *child;
2173 char *errtol = NULL;
2174
2175 if (*prefix != '@') /* not a prefix, should not happen */
2176 return -1;
2177
2178 prefix++;
2179 if (!*prefix) /* sent @ alone, return the master */
2180 return 0;
2181
2182 if (strcmp("master", prefix) == 0) {
2183 return 0;
2184 } else if (*prefix == '!') {
2185 prefix++;
2186 if (!*prefix)
2187 return -1;
2188
2189 proc_pid = strtol(prefix, &errtol, 10);
2190 if (*errtol != '\0')
2191 return -1;
2192 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02002193 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01002194 continue;
William Lallemand291810d2018-10-26 14:47:38 +02002195 if (child->pid == proc_pid){
2196 return child->pid;
2197 }
2198 }
2199 } else {
2200 struct mworker_proc *chosen = NULL;
2201 /* this is a relative pid */
2202
2203 proc_pid = strtol(prefix, &errtol, 10);
2204 if (*errtol != '\0')
2205 return -1;
2206
2207 if (proc_pid == 0) /* return the master */
2208 return 0;
2209
2210 /* chose the right process, the current one is the one with the
2211 least number of reloads */
2212 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02002213 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01002214 continue;
William Lallemand291810d2018-10-26 14:47:38 +02002215 if (child->relative_pid == proc_pid){
2216 if (child->reloads == 0)
2217 return child->pid;
2218 else if (chosen == NULL || child->reloads < chosen->reloads)
2219 chosen = child;
2220 }
2221 }
2222 if (chosen)
2223 return chosen->pid;
2224 }
2225 return -1;
2226}
2227
William Lallemandbddd33a2018-12-11 16:10:53 +01002228/* Return::
2229 * >= 0 : number of words to escape
2230 * = -1 : error
2231 */
2232
2233int pcli_find_and_exec_kw(struct stream *s, char **args, int argl, char **errmsg, int *next_pid)
2234{
2235 if (argl < 1)
2236 return 0;
2237
2238 /* there is a prefix */
2239 if (args[0][0] == '@') {
2240 int target_pid = pcli_prefix_to_pid(args[0]);
2241
2242 if (target_pid == -1) {
2243 memprintf(errmsg, "Can't find the target PID matching the prefix '%s'\n", args[0]);
2244 return -1;
2245 }
2246
2247 /* if the prefix is alone, define a default target */
2248 if (argl == 1)
2249 s->pcli_next_pid = target_pid;
2250 else
2251 *next_pid = target_pid;
2252 return 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002253 } else if (strcmp("prompt", args[0]) == 0) {
William Lallemanddc12c2e2018-12-13 09:05:46 +01002254 s->pcli_flags ^= PCLI_F_PROMPT;
William Lallemand5b80fa22018-12-11 16:10:54 +01002255 return argl; /* return the number of elements in the array */
William Lallemand5f610682018-12-11 16:10:55 +01002256
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002257 } else if (strcmp("quit", args[0]) == 0) {
William Lallemand5f610682018-12-11 16:10:55 +01002258 channel_shutr_now(&s->req);
2259 channel_shutw_now(&s->res);
2260 return argl; /* return the number of elements in the array */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002261 } else if (strcmp(args[0], "operator") == 0) {
William Lallemandb7ea1412018-12-13 09:05:47 +01002262 if (!pcli_has_level(s, ACCESS_LVL_OPER)) {
2263 memprintf(errmsg, "Permission denied!\n");
2264 return -1;
2265 }
2266 s->pcli_flags &= ~ACCESS_LVL_MASK;
2267 s->pcli_flags |= ACCESS_LVL_OPER;
2268 return argl;
2269
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002270 } else if (strcmp(args[0], "user") == 0) {
William Lallemandb7ea1412018-12-13 09:05:47 +01002271 if (!pcli_has_level(s, ACCESS_LVL_USER)) {
2272 memprintf(errmsg, "Permission denied!\n");
2273 return -1;
2274 }
2275 s->pcli_flags &= ~ACCESS_LVL_MASK;
2276 s->pcli_flags |= ACCESS_LVL_USER;
2277 return argl;
William Lallemandbddd33a2018-12-11 16:10:53 +01002278 }
2279
2280 return 0;
2281}
2282
2283/*
2284 * Parse the CLI request:
2285 * - It does basically the same as the cli_io_handler, but as a proxy
2286 * - It can exec a command and strip non forwardable commands
William Lallemandcf62f7e2018-10-26 14:47:40 +02002287 *
2288 * Return:
William Lallemandbddd33a2018-12-11 16:10:53 +01002289 * - the number of characters to forward or
2290 * - 1 if there is an error or not enough data
William Lallemandcf62f7e2018-10-26 14:47:40 +02002291 */
William Lallemandbddd33a2018-12-11 16:10:53 +01002292int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int *next_pid)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002293{
Willy Tarreauad5bf142022-01-20 08:47:35 +01002294 char *str;
2295 char *end;
Willy Tarreauf14c7572021-03-13 10:59:23 +01002296 char *args[MAX_CLI_ARGS + 1]; /* +1 for storing a NULL */
William Lallemandbddd33a2018-12-11 16:10:53 +01002297 int argl; /* number of args */
2298 char *p;
2299 char *trim = NULL;
William Lallemandebf61802018-12-11 16:10:57 +01002300 char *payload = NULL;
William Lallemandbddd33a2018-12-11 16:10:53 +01002301 int wtrim = 0; /* number of words to trim */
2302 int reql = 0;
William Lallemandb7ea1412018-12-13 09:05:47 +01002303 int ret;
William Lallemandbddd33a2018-12-11 16:10:53 +01002304 int i = 0;
2305
Willy Tarreauad5bf142022-01-20 08:47:35 +01002306 /* we cannot deal with a wrapping buffer, so let's take care of this
2307 * first.
2308 */
2309 if (b_head(&req->buf) + b_data(&req->buf) > b_wrap(&req->buf))
2310 b_slow_realign(&req->buf, trash.area, co_data(req));
2311
2312 str = (char *)ci_head(req);
2313 end = (char *)ci_stop(req);
2314
William Lallemandbddd33a2018-12-11 16:10:53 +01002315 p = str;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002316
William Lallemanddc12c2e2018-12-13 09:05:46 +01002317 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002318
2319 /* Looks for the end of one command */
2320 while (p+reql < end) {
2321 /* handle escaping */
2322 if (p[reql] == '\\') {
William Lallemand02c255e2020-06-18 18:45:04 +02002323 reql+=2;
William Lallemandebf61802018-12-11 16:10:57 +01002324 continue;
2325 }
2326 if (p[reql] == ';' || p[reql] == '\n') {
2327 /* found the end of the command */
2328 p[reql] = '\n';
2329 reql++;
2330 break;
2331 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002332 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002333 }
William Lallemandebf61802018-12-11 16:10:57 +01002334 } else {
2335 while (p+reql < end) {
2336 if (p[reql] == '\n') {
2337 /* found the end of the line */
2338 reql++;
2339 break;
2340 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002341 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002342 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002343 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002344
William Lallemandbddd33a2018-12-11 16:10:53 +01002345 /* set end to first byte after the end of the command */
2346 end = p + reql;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002347
William Lallemandbddd33a2018-12-11 16:10:53 +01002348 /* there is no end to this command, need more to parse ! */
Willy Tarreau9b736eb2022-01-20 08:31:50 +01002349 if (!reql || *(end-1) != '\n') {
William Lallemandbddd33a2018-12-11 16:10:53 +01002350 return -1;
2351 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002352
William Lallemand3301f3e2018-12-13 09:05:48 +01002353 if (s->pcli_flags & PCLI_F_PAYLOAD) {
2354 if (reql == 1) /* last line of the payload */
2355 s->pcli_flags &= ~PCLI_F_PAYLOAD;
William Lallemandebf61802018-12-11 16:10:57 +01002356 return reql;
2357 }
2358
William Lallemandbddd33a2018-12-11 16:10:53 +01002359 *(end-1) = '\0';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002360
William Lallemandbddd33a2018-12-11 16:10:53 +01002361 /* splits the command in words */
Willy Tarreauf14c7572021-03-13 10:59:23 +01002362 while (i < MAX_CLI_ARGS && p < end) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002363 /* skip leading spaces/tabs */
2364 p += strspn(p, " \t");
2365 if (!*p)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002366 break;
2367
William Lallemandbddd33a2018-12-11 16:10:53 +01002368 args[i] = p;
William Lallemandfe249c32020-06-18 18:03:57 +02002369 while (1) {
2370 p += strcspn(p, " \t\\");
2371 /* escaped chars using backlashes (\) */
2372 if (*p == '\\') {
2373 if (!*++p)
2374 break;
2375 if (!*++p)
2376 break;
2377 } else {
2378 break;
William Lallemandbddd33a2018-12-11 16:10:53 +01002379 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002380 }
William Lallemandfe249c32020-06-18 18:03:57 +02002381 *p++ = 0;
William Lallemandbddd33a2018-12-11 16:10:53 +01002382 i++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002383 }
2384
William Lallemandbddd33a2018-12-11 16:10:53 +01002385 argl = i;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002386
Willy Tarreauf14c7572021-03-13 10:59:23 +01002387 for (; i < MAX_CLI_ARGS + 1; i++)
William Lallemandbddd33a2018-12-11 16:10:53 +01002388 args[i] = NULL;
2389
2390 wtrim = pcli_find_and_exec_kw(s, args, argl, errmsg, next_pid);
2391
2392 /* End of words are ending by \0, we need to replace the \0s by spaces
23931 before forwarding them */
2394 p = str;
William Lallemand3301f3e2018-12-13 09:05:48 +01002395 while (p < end-1) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002396 if (*p == '\0')
2397 *p = ' ';
2398 p++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002399 }
2400
William Lallemand3301f3e2018-12-13 09:05:48 +01002401 payload = strstr(str, PAYLOAD_PATTERN);
2402 if ((end - 1) == (payload + strlen(PAYLOAD_PATTERN))) {
2403 /* if the payload pattern is at the end */
2404 s->pcli_flags |= PCLI_F_PAYLOAD;
William Lallemand3301f3e2018-12-13 09:05:48 +01002405 }
2406
William Lallemandbddd33a2018-12-11 16:10:53 +01002407 *(end-1) = '\n';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002408
William Lallemandbddd33a2018-12-11 16:10:53 +01002409 if (wtrim > 0) {
2410 trim = &args[wtrim][0];
2411 if (trim == NULL) /* if this was the last word in the table */
2412 trim = end;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002413
William Lallemandbddd33a2018-12-11 16:10:53 +01002414 b_del(&req->buf, trim - str);
2415
William Lallemandb7ea1412018-12-13 09:05:47 +01002416 ret = end - trim;
William Lallemandbddd33a2018-12-11 16:10:53 +01002417 } else if (wtrim < 0) {
2418 /* parsing error */
2419 return -1;
William Lallemandb7ea1412018-12-13 09:05:47 +01002420 } else {
2421 /* the whole string */
2422 ret = end - str;
William Lallemandbddd33a2018-12-11 16:10:53 +01002423 }
2424
William Lallemandb7ea1412018-12-13 09:05:47 +01002425 if (ret > 1) {
2426 if (pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2427 goto end;
2428 } else if (pcli_has_level(s, ACCESS_LVL_OPER)) {
William Lallemandad032882019-07-01 10:56:15 +02002429 ci_insert_line2(req, 0, "operator -", strlen("operator -"));
2430 ret += strlen("operator -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002431 } else if (pcli_has_level(s, ACCESS_LVL_USER)) {
William Lallemandad032882019-07-01 10:56:15 +02002432 ci_insert_line2(req, 0, "user -", strlen("user -"));
2433 ret += strlen("user -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002434 }
2435 }
2436end:
William Lallemandbddd33a2018-12-11 16:10:53 +01002437
William Lallemandb7ea1412018-12-13 09:05:47 +01002438 return ret;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002439}
2440
2441int pcli_wait_for_request(struct stream *s, struct channel *req, int an_bit)
2442{
William Lallemandbddd33a2018-12-11 16:10:53 +01002443 int next_pid = -1;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002444 int to_forward;
William Lallemandbddd33a2018-12-11 16:10:53 +01002445 char *errmsg = NULL;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002446
Christopher Fauletbe416712021-10-18 14:52:49 +02002447 /* Don't read the next command if still processing the reponse of the
2448 * current one. Just wait. At this stage, errors should be handled by
2449 * the response analyzer.
2450 */
2451 if (s->res.analysers & AN_RES_WAIT_CLI)
2452 return 0;
2453
William Lallemandb7ea1412018-12-13 09:05:47 +01002454 if ((s->pcli_flags & ACCESS_LVL_MASK) == ACCESS_LVL_NONE)
2455 s->pcli_flags |= strm_li(s)->bind_conf->level & ACCESS_LVL_MASK;
2456
William Lallemandcf62f7e2018-10-26 14:47:40 +02002457read_again:
2458 /* if the channel is closed for read, we won't receive any more data
2459 from the client, but we don't want to forward this close to the
2460 server */
2461 channel_dont_close(req);
2462
2463 /* We don't know yet to which server we will connect */
2464 channel_dont_connect(req);
2465
William Lallemandcf62f7e2018-10-26 14:47:40 +02002466 req->flags |= CF_READ_DONTWAIT;
2467
2468 /* need more data */
2469 if (!ci_data(req))
Christopher Fauletc8c4e3f2022-01-18 08:44:23 +01002470 goto missing_data;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002471
2472 /* If there is data available for analysis, log the end of the idle time. */
2473 if (c_data(req) && s->logs.t_idle == -1)
2474 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2475
William Lallemandbddd33a2018-12-11 16:10:53 +01002476 to_forward = pcli_parse_request(s, req, &errmsg, &next_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002477 if (to_forward > 0) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002478 int target_pid;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002479 /* enough data */
2480
William Lallemandcf62f7e2018-10-26 14:47:40 +02002481 /* forward only 1 command */
2482 channel_forward(req, to_forward);
William Lallemandebf61802018-12-11 16:10:57 +01002483
William Lallemanddc12c2e2018-12-13 09:05:46 +01002484 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002485 /* we send only 1 command per request, and we write close after it */
2486 channel_shutw_now(req);
2487 } else {
2488 pcli_write_prompt(s);
2489 }
2490
2491 s->res.flags |= CF_WAKE_ONCE; /* need to be called again */
William Lallemandcf62f7e2018-10-26 14:47:40 +02002492 s->res.analysers |= AN_RES_WAIT_CLI;
2493
William Lallemandebf61802018-12-11 16:10:57 +01002494 if (!(s->flags & SF_ASSIGNED)) {
2495 if (next_pid > -1)
2496 target_pid = next_pid;
2497 else
2498 target_pid = s->pcli_next_pid;
2499 /* we can connect now */
2500 s->target = pcli_pid_to_server(target_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002501
William Lallemanddd29e892021-12-10 14:14:53 +01002502 if (!s->target)
2503 goto server_disconnect;
2504
William Lallemandebf61802018-12-11 16:10:57 +01002505 s->flags |= (SF_DIRECT | SF_ASSIGNED);
2506 channel_auto_connect(req);
2507 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002508
2509 } else if (to_forward == 0) {
William Lallemandcf62f7e2018-10-26 14:47:40 +02002510 /* we trimmed things but we might have other commands to consume */
William Lallemandbddd33a2018-12-11 16:10:53 +01002511 pcli_write_prompt(s);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002512 goto read_again;
Christopher Fauletc8c4e3f2022-01-18 08:44:23 +01002513 } else if (to_forward == -1) {
2514 if (errmsg) {
2515 /* there was an error during the parsing */
2516 pcli_reply_and_close(s, errmsg);
2517 s->req.analysers &= ~AN_REQ_WAIT_CLI;
2518 return 0;
2519 }
2520 goto missing_data;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002521 }
2522
2523 return 0;
2524
2525send_help:
2526 b_reset(&req->buf);
2527 b_putblk(&req->buf, "help\n", 5);
2528 goto read_again;
William Lallemanddd29e892021-12-10 14:14:53 +01002529
Christopher Fauletc8c4e3f2022-01-18 08:44:23 +01002530missing_data:
2531 if (req->flags & CF_SHUTR) {
2532 /* There is no more request or a only a partial one and we
2533 * receive a close from the client, we can leave */
2534 channel_shutw_now(&s->res);
2535 s->req.analysers &= ~AN_REQ_WAIT_CLI;
2536 return 1;
2537 }
2538 else if (channel_full(req, global.tune.maxrewrite)) {
2539 /* buffer is full and we didn't catch the end of a command */
2540 goto send_help;
2541 }
2542 return 0;
2543
William Lallemanddd29e892021-12-10 14:14:53 +01002544server_disconnect:
2545 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
2546 return 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002547}
2548
2549int pcli_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
2550{
2551 struct proxy *fe = strm_fe(s);
2552 struct proxy *be = s->be;
2553
Christopher Fauletc3c71f82022-03-07 18:20:21 +01002554 if ((rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
2555 ((rep->flags & CF_SHUTW) && (rep->to_forward || co_data(rep)))) {
William Lallemand6b7cd0a2018-11-06 17:37:11 +01002556 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
Christopher Fauletbe416712021-10-18 14:52:49 +02002557 s->req.analysers &= ~AN_REQ_WAIT_CLI;
William Lallemand6b7cd0a2018-11-06 17:37:11 +01002558 s->res.analysers &= ~AN_RES_WAIT_CLI;
2559 return 0;
2560 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002561 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2562 rep->flags |= CF_NEVER_WAIT;
2563
2564 /* don't forward the close */
2565 channel_dont_close(&s->res);
2566 channel_dont_close(&s->req);
2567
William Lallemanddc12c2e2018-12-13 09:05:46 +01002568 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01002569 s->res.analysers &= ~AN_RES_WAIT_CLI;
2570 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2571 return 0;
2572 }
2573
William Lallemandcf62f7e2018-10-26 14:47:40 +02002574 /* forward the data */
2575 if (ci_data(rep)) {
2576 c_adv(rep, ci_data(rep));
2577 return 0;
2578 }
2579
2580 if ((rep->flags & (CF_SHUTR|CF_READ_NULL))) {
2581 /* stream cleanup */
2582
William Lallemand2f4ce202018-10-26 14:47:47 +02002583 pcli_write_prompt(s);
2584
William Lallemandcf62f7e2018-10-26 14:47:40 +02002585 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
2586 si_shutr(&s->si[1]);
2587 si_shutw(&s->si[1]);
2588
2589 /*
2590 * starting from there this the same code as
2591 * http_end_txn_clean_session().
2592 *
2593 * It allows to do frontend keepalive while reconnecting to a
2594 * new server for each request.
2595 */
2596
2597 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002598 HA_ATOMIC_DEC(&be->beconn);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002599 if (unlikely(s->srv_conn))
2600 sess_change_server(s, NULL);
2601 }
2602
2603 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2604 stream_process_counters(s);
2605
2606 /* don't count other requests' data */
2607 s->logs.bytes_in -= ci_data(&s->req);
2608 s->logs.bytes_out -= ci_data(&s->res);
2609
2610 /* we may need to know the position in the queue */
2611 pendconn_free(s);
2612
2613 /* let's do a final log if we need it */
2614 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
2615 !(s->flags & SF_MONITOR) &&
2616 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
2617 s->do_log(s);
2618 }
2619
2620 /* stop tracking content-based counters */
2621 stream_stop_content_counters(s);
2622 stream_update_time_stats(s);
2623
2624 s->logs.accept_date = date; /* user-visible date for logging */
2625 s->logs.tv_accept = now; /* corrected date for internal use */
2626 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
2627 s->logs.t_idle = -1;
2628 tv_zero(&s->logs.tv_request);
2629 s->logs.t_queue = -1;
2630 s->logs.t_connect = -1;
2631 s->logs.t_data = -1;
2632 s->logs.t_close = 0;
2633 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
2634 s->logs.srv_queue_pos = 0; /* we will get this number soon */
2635
2636 s->logs.bytes_in = s->req.total = ci_data(&s->req);
2637 s->logs.bytes_out = s->res.total = ci_data(&s->res);
2638
2639 stream_del_srv_conn(s);
2640 if (objt_server(s->target)) {
2641 if (s->flags & SF_CURR_SESS) {
2642 s->flags &= ~SF_CURR_SESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002643 HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002644 }
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002645 if (may_dequeue_tasks(__objt_server(s->target), be))
Amaury Denoyellecaaafd02021-06-18 11:11:36 +02002646 process_srv_queue(__objt_server(s->target), 0);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002647 }
2648
2649 s->target = NULL;
2650
2651 /* only release our endpoint if we don't intend to reuse the
2652 * connection.
2653 */
2654 if (!si_conn_ready(&s->si[1])) {
2655 si_release_endpoint(&s->si[1]);
2656 s->srv_conn = NULL;
2657 }
2658
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002659 sockaddr_free(&s->target_addr);
2660
William Lallemandcf62f7e2018-10-26 14:47:40 +02002661 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
2662 s->si[1].err_type = SI_ET_NONE;
2663 s->si[1].conn_retries = 0; /* used for logging too */
2664 s->si[1].exp = TICK_ETERNITY;
2665 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Christopher Faulet22dc2482019-07-16 14:43:08 +02002666 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WROTE_DATA);
William Lallemand19338012019-06-25 18:00:19 +02002667 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_READ_NULL);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002668 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
2669 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
2670 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
2671 /* reinitialise the current rule list pointer to NULL. We are sure that
2672 * any rulelist match the NULL pointer.
2673 */
2674 s->current_rule_list = NULL;
2675
2676 s->be = strm_fe(s);
2677 s->logs.logwait = strm_fe(s)->to_log;
2678 s->logs.level = 0;
2679 stream_del_srv_conn(s);
2680 s->target = NULL;
2681 /* re-init store persistence */
2682 s->store_count = 0;
2683 s->uniq_id = global.req_count++;
2684
2685 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
2686
2687 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2688
William Lallemandcf62f7e2018-10-26 14:47:40 +02002689 s->res.analysers &= ~AN_RES_WAIT_CLI;
2690
2691 /* We must trim any excess data from the response buffer, because we
2692 * may have blocked an invalid response from a server that we don't
Ilya Shipitsind4259502020-04-08 01:07:56 +05002693 * want to accidentally forward once we disable the analysers, nor do
William Lallemandcf62f7e2018-10-26 14:47:40 +02002694 * we want those data to come along with next response. A typical
2695 * example of such data would be from a buggy server responding to
2696 * a HEAD with some data, or sending more than the advertised
2697 * content-length.
2698 */
2699 if (unlikely(ci_data(&s->res)))
2700 b_set_data(&s->res.buf, co_data(&s->res));
2701
2702 /* Now we can realign the response buffer */
2703 c_realign_if_empty(&s->res);
2704
2705 s->req.rto = strm_fe(s)->timeout.client;
2706 s->req.wto = TICK_ETERNITY;
2707
2708 s->res.rto = TICK_ETERNITY;
2709 s->res.wto = strm_fe(s)->timeout.client;
2710
2711 s->req.rex = TICK_ETERNITY;
2712 s->req.wex = TICK_ETERNITY;
2713 s->req.analyse_exp = TICK_ETERNITY;
2714 s->res.rex = TICK_ETERNITY;
2715 s->res.wex = TICK_ETERNITY;
2716 s->res.analyse_exp = TICK_ETERNITY;
2717 s->si[1].hcto = TICK_ETERNITY;
2718
2719 /* we're removing the analysers, we MUST re-enable events detection.
2720 * We don't enable close on the response channel since it's either
2721 * already closed, or in keep-alive with an idle connection handler.
2722 */
2723 channel_auto_read(&s->req);
2724 channel_auto_close(&s->req);
2725 channel_auto_read(&s->res);
2726
2727
2728 return 1;
2729 }
2730 return 0;
2731}
2732
William Lallemand8a022572018-10-26 14:47:35 +02002733/*
2734 * The mworker functions are used to initialize the CLI in the master process
2735 */
2736
William Lallemand309dc9a2018-10-26 14:47:45 +02002737 /*
2738 * Stop the mworker proxy
2739 */
2740void mworker_cli_proxy_stop()
2741{
William Lallemand550db6d2018-11-06 17:37:12 +01002742 if (mworker_proxy)
2743 stop_proxy(mworker_proxy);
William Lallemand309dc9a2018-10-26 14:47:45 +02002744}
2745
William Lallemand8a022572018-10-26 14:47:35 +02002746/*
2747 * Create the mworker CLI proxy
2748 */
2749int mworker_cli_proxy_create()
2750{
2751 struct mworker_proc *child;
William Lallemand744a0892018-11-22 16:46:51 +01002752 char *msg = NULL;
2753 char *errmsg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02002754
2755 mworker_proxy = calloc(1, sizeof(*mworker_proxy));
2756 if (!mworker_proxy)
2757 return -1;
2758
2759 init_new_proxy(mworker_proxy);
2760 mworker_proxy->next = proxies_list;
2761 proxies_list = mworker_proxy;
2762 mworker_proxy->id = strdup("MASTER");
William Lallemandcf62f7e2018-10-26 14:47:40 +02002763 mworker_proxy->mode = PR_MODE_CLI;
William Lallemand8a022572018-10-26 14:47:35 +02002764 mworker_proxy->last_change = now.tv_sec;
2765 mworker_proxy->cap = PR_CAP_LISTEN; /* this is a listen section */
2766 mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */
2767 mworker_proxy->timeout.client = 0; /* no timeout */
2768 mworker_proxy->conf.file = strdup("MASTER");
2769 mworker_proxy->conf.line = 0;
2770 mworker_proxy->accept = frontend_accept;
2771 mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE;
2772
2773 /* Does not init the default target the CLI applet, but must be done in
2774 * the request parsing code */
2775 mworker_proxy->default_target = NULL;
2776
2777 /* the check_config_validity() will get an ID for the proxy */
2778 mworker_proxy->uuid = -1;
2779
2780 proxy_store_name(mworker_proxy);
2781
2782 /* create all servers using the mworker_proc list */
2783 list_for_each_entry(child, &proc_list, list) {
William Lallemand8a022572018-10-26 14:47:35 +02002784 struct server *newsrv = NULL;
2785 struct sockaddr_storage *sk;
2786 int port1, port2, port;
2787 struct protocol *proto;
William Lallemand8a022572018-10-26 14:47:35 +02002788
William Lallemanda31b09e2020-01-14 15:25:02 +01002789 /* only the workers support the master CLI */
2790 if (!(child->options & PROC_O_TYPE_WORKER))
2791 continue;
2792
William Lallemand8a022572018-10-26 14:47:35 +02002793 newsrv = new_server(mworker_proxy);
2794 if (!newsrv)
William Lallemand744a0892018-11-22 16:46:51 +01002795 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002796
2797 /* we don't know the new pid yet */
2798 if (child->pid == -1)
2799 memprintf(&msg, "cur-%d", child->relative_pid);
2800 else
2801 memprintf(&msg, "old-%d", child->pid);
2802
2803 newsrv->next = mworker_proxy->srv;
2804 mworker_proxy->srv = newsrv;
2805 newsrv->conf.file = strdup(msg);
2806 newsrv->id = strdup(msg);
2807 newsrv->conf.line = 0;
2808
2809 memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
Willy Tarreau5fc93282020-09-16 18:25:03 +02002810 if ((sk = str2sa_range(msg, &port, &port1, &port2, NULL, &proto,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02002811 &errmsg, NULL, NULL, PA_O_STREAM)) == 0) {
William Lallemand744a0892018-11-22 16:46:51 +01002812 goto error;
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002813 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002814 ha_free(&msg);
William Lallemand8a022572018-10-26 14:47:35 +02002815
Willy Tarreau5fc93282020-09-16 18:25:03 +02002816 if (!proto->connect) {
William Lallemand744a0892018-11-22 16:46:51 +01002817 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02002818 }
2819
2820 /* no port specified */
2821 newsrv->flags |= SRV_F_MAPPORTS;
2822 newsrv->addr = *sk;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002823 /* don't let the server participate to load balancing */
2824 newsrv->iweight = 0;
2825 newsrv->uweight = 0;
William Lallemand8a022572018-10-26 14:47:35 +02002826 srv_lb_commit_status(newsrv);
William Lallemand291810d2018-10-26 14:47:38 +02002827
2828 child->srv = newsrv;
William Lallemand8a022572018-10-26 14:47:35 +02002829 }
2830 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01002831
2832error:
2833 ha_alert("%s\n", errmsg);
2834
2835 list_for_each_entry(child, &proc_list, list) {
2836 free((char *)child->srv->conf.file); /* cast because of const char * */
2837 free(child->srv->id);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002838 ha_free(&child->srv);
William Lallemand744a0892018-11-22 16:46:51 +01002839 }
2840 free(mworker_proxy->id);
2841 free(mworker_proxy->conf.file);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002842 ha_free(&mworker_proxy);
William Lallemand744a0892018-11-22 16:46:51 +01002843 free(errmsg);
2844 free(msg);
2845
2846 return -1;
William Lallemand8a022572018-10-26 14:47:35 +02002847}
Olivier Houchardf886e342017-04-05 22:24:59 +02002848
William Lallemandce83b4a2018-10-26 14:47:30 +02002849/*
William Lallemande7361152018-10-26 14:47:36 +02002850 * Create a new listener for the master CLI proxy
2851 */
2852int mworker_cli_proxy_new_listener(char *line)
2853{
2854 struct bind_conf *bind_conf;
2855 struct listener *l;
2856 char *err = NULL;
2857 char *args[MAX_LINE_ARGS + 1];
2858 int arg;
2859 int cur_arg;
2860
William Lallemand2e945c82019-11-25 09:58:37 +01002861 arg = 1;
William Lallemande7361152018-10-26 14:47:36 +02002862 args[0] = line;
2863
2864 /* args is a bind configuration with spaces replaced by commas */
2865 while (*line && arg < MAX_LINE_ARGS) {
2866
2867 if (*line == ',') {
2868 *line++ = '\0';
2869 while (*line == ',')
2870 line++;
William Lallemand2e945c82019-11-25 09:58:37 +01002871 args[arg++] = line;
William Lallemande7361152018-10-26 14:47:36 +02002872 }
2873 line++;
2874 }
2875
William Lallemand2e945c82019-11-25 09:58:37 +01002876 args[arg] = "\0";
William Lallemande7361152018-10-26 14:47:36 +02002877
2878 bind_conf = bind_conf_alloc(mworker_proxy, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002879 if (!bind_conf)
2880 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002881
2882 bind_conf->level &= ~ACCESS_LVL_MASK;
2883 bind_conf->level |= ACCESS_LVL_ADMIN;
Willy Tarreaue283ee62021-03-12 15:00:57 +01002884 bind_conf->level |= ACCESS_MASTER | ACCESS_MASTER_ONLY;
William Lallemande7361152018-10-26 14:47:36 +02002885
2886 if (!str2listener(args[0], mworker_proxy, bind_conf, "master-socket", 0, &err)) {
2887 ha_alert("Cannot create the listener of the master CLI\n");
William Lallemand744a0892018-11-22 16:46:51 +01002888 goto err;
William Lallemande7361152018-10-26 14:47:36 +02002889 }
2890
2891 cur_arg = 1;
2892
2893 while (*args[cur_arg]) {
William Lallemande7361152018-10-26 14:47:36 +02002894 struct bind_kw *kw;
Willy Tarreau433b05f2021-03-12 10:14:07 +01002895 const char *best;
William Lallemande7361152018-10-26 14:47:36 +02002896
2897 kw = bind_find_kw(args[cur_arg]);
2898 if (kw) {
2899 if (!kw->parse) {
2900 memprintf(&err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
2901 args[0], args[1], args[cur_arg]);
2902 goto err;
2903 }
2904
Willy Tarreau4975d142021-03-13 11:00:33 +01002905 if (kw->parse(args, cur_arg, global.cli_fe, bind_conf, &err) != 0) {
William Lallemande7361152018-10-26 14:47:36 +02002906 if (err)
2907 memprintf(&err, "'%s %s' : '%s'", args[0], args[1], err);
2908 else
2909 memprintf(&err, "'%s %s' : error encountered while processing '%s'",
2910 args[0], args[1], args[cur_arg]);
2911 goto err;
2912 }
2913
2914 cur_arg += 1 + kw->skip;
2915 continue;
2916 }
2917
Willy Tarreau433b05f2021-03-12 10:14:07 +01002918 best = bind_find_best_kw(args[cur_arg]);
2919 if (best)
2920 memprintf(&err, "'%s %s' : unknown keyword '%s'. Did you mean '%s' maybe ?",
2921 args[0], args[1], args[cur_arg], best);
2922 else
2923 memprintf(&err, "'%s %s' : unknown keyword '%s'.",
2924 args[0], args[1], args[cur_arg]);
William Lallemande7361152018-10-26 14:47:36 +02002925 goto err;
2926 }
2927
2928
2929 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemande7361152018-10-26 14:47:36 +02002930 l->accept = session_accept_fd;
2931 l->default_target = mworker_proxy->default_target;
2932 /* don't make the peers subject to global limits and don't close it in the master */
Willy Tarreau18c20d22020-10-09 16:11:46 +02002933 l->options |= LI_O_UNLIMITED;
2934 l->rx.flags |= RX_F_MWORKER; /* we are keeping this FD in the master */
William Lallemande7361152018-10-26 14:47:36 +02002935 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01002936 global.maxsock++; /* for the listening socket */
William Lallemande7361152018-10-26 14:47:36 +02002937 }
Willy Tarreau18215cb2019-02-27 16:25:28 +01002938 global.maxsock += mworker_proxy->maxconn;
William Lallemande7361152018-10-26 14:47:36 +02002939
2940 return 0;
2941
2942err:
2943 ha_alert("%s\n", err);
William Lallemand744a0892018-11-22 16:46:51 +01002944 free(err);
2945 free(bind_conf);
William Lallemande7361152018-10-26 14:47:36 +02002946 return -1;
2947
2948}
2949
2950/*
William Lallemandce83b4a2018-10-26 14:47:30 +02002951 * Create a new CLI socket using a socketpair for a worker process
2952 * <mworker_proc> is the process structure, and <proc> is the process number
2953 */
2954int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
2955{
2956 struct bind_conf *bind_conf;
2957 struct listener *l;
2958 char *path = NULL;
2959 char *err = NULL;
2960
2961 /* master pipe to ensure the master is still alive */
2962 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
2963 ha_alert("Cannot create worker socketpair.\n");
2964 return -1;
2965 }
2966
2967 /* XXX: we might want to use a separate frontend at some point */
Willy Tarreau4975d142021-03-13 11:00:33 +01002968 if (!global.cli_fe) {
2969 if ((global.cli_fe = cli_alloc_fe("GLOBAL", "master-socket", 0)) == NULL) {
William Lallemandce83b4a2018-10-26 14:47:30 +02002970 ha_alert("out of memory trying to allocate the stats frontend");
William Lallemand744a0892018-11-22 16:46:51 +01002971 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002972 }
2973 }
2974
Willy Tarreau4975d142021-03-13 11:00:33 +01002975 bind_conf = bind_conf_alloc(global.cli_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01002976 if (!bind_conf)
2977 goto error;
2978
William Lallemandce83b4a2018-10-26 14:47:30 +02002979 bind_conf->level &= ~ACCESS_LVL_MASK;
2980 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
2981
Willy Tarreaue26993c2020-09-03 07:18:55 +02002982 bind_conf->settings.bind_proc = 1UL << proc;
Willy Tarreau4975d142021-03-13 11:00:33 +01002983 global.cli_fe->bind_proc = 0; /* XXX: we should be careful with that, it can be removed by configuration */
William Lallemandce83b4a2018-10-26 14:47:30 +02002984
2985 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
2986 ha_alert("Cannot allocate listener.\n");
William Lallemand744a0892018-11-22 16:46:51 +01002987 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002988 }
2989
Willy Tarreau4975d142021-03-13 11:00:33 +01002990 if (!str2listener(path, global.cli_fe, bind_conf, "master-socket", 0, &err)) {
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01002991 free(path);
William Lallemandce83b4a2018-10-26 14:47:30 +02002992 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
William Lallemand744a0892018-11-22 16:46:51 +01002993 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02002994 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002995 ha_free(&path);
William Lallemandce83b4a2018-10-26 14:47:30 +02002996
2997 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemandce83b4a2018-10-26 14:47:30 +02002998 l->accept = session_accept_fd;
Willy Tarreau4975d142021-03-13 11:00:33 +01002999 l->default_target = global.cli_fe->default_target;
William Lallemanda3372292018-11-16 16:57:22 +01003000 l->options |= (LI_O_UNLIMITED | LI_O_NOSTOP);
Willy Tarreau4781b152021-04-06 13:53:36 +02003001 HA_ATOMIC_INC(&unstoppable_jobs);
William Lallemandce83b4a2018-10-26 14:47:30 +02003002 /* it's a sockpair but we don't want to keep the fd in the master */
Willy Tarreau43046fa2020-09-01 15:41:59 +02003003 l->rx.flags &= ~RX_F_INHERITED;
William Lallemandce83b4a2018-10-26 14:47:30 +02003004 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01003005 global.maxsock++; /* for the listening socket */
William Lallemandce83b4a2018-10-26 14:47:30 +02003006 }
3007
3008 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01003009
3010error:
3011 close(mworker_proc->ipc_fd[0]);
3012 close(mworker_proc->ipc_fd[1]);
3013 free(err);
3014
3015 return -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02003016}
3017
William Lallemand74c24fb2016-11-21 17:18:36 +01003018static struct applet cli_applet = {
3019 .obj_type = OBJ_TYPE_APPLET,
3020 .name = "<CLI>", /* used for logging */
3021 .fct = cli_io_handler,
3022 .release = cli_release_handler,
3023};
3024
William Lallemand99e0bb92020-11-05 10:28:53 +01003025/* master CLI */
3026static struct applet mcli_applet = {
3027 .obj_type = OBJ_TYPE_APPLET,
3028 .name = "<MCLI>", /* used for logging */
3029 .fct = cli_io_handler,
3030 .release = cli_release_handler,
3031};
3032
Willy Tarreau0a739292016-11-22 20:21:23 +01003033/* register cli keywords */
3034static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003035 { { "help", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3036 { { "prompt", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3037 { { "quit", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3038 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
3039 { { "expert-mode", NULL }, NULL, cli_parse_expert_experimental_mode, NULL }, // not listed
3040 { { "experimental-mode", NULL }, NULL, cli_parse_expert_experimental_mode, NULL }, // not listed
3041 { { "set", "maxconn", "global", NULL }, "set maxconn global <value> : change the per-process maxconn setting", cli_parse_set_maxconn_global, NULL },
3042 { { "set", "rate-limit", NULL }, "set rate-limit <setting> <value> : change a rate limiting value", cli_parse_set_ratelimit, NULL },
3043 { { "set", "severity-output", NULL }, "set severity-output [none|number|string]: set presence of severity level in feedback information", cli_parse_set_severity_output, NULL, NULL },
3044 { { "set", "timeout", NULL }, "set timeout [cli] <delay> : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
3045 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
3046 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL, NULL, ACCESS_MASTER },
3047 { { "show", "cli", "level", NULL }, "show cli level : display the level of the current CLI session", cli_parse_show_lvl, NULL, NULL, NULL, ACCESS_MASTER},
3048 { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use or a specific one", cli_parse_show_fd, cli_io_handler_show_fd, NULL },
3049 { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL },
William Lallemand7ceae112021-12-14 15:22:29 +01003050 { { "show", "version", NULL }, "show version : show version of the current process", cli_parse_show_version, NULL, NULL, NULL, ACCESS_MASTER },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003051 { { "operator", NULL }, "operator : lower the level of the current CLI session to operator", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER},
3052 { { "user", NULL }, "user : lower the level of the current CLI session to user", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER},
Willy Tarreau0a739292016-11-22 20:21:23 +01003053 {{},}
3054}};
3055
Willy Tarreau0108d902018-11-25 19:14:37 +01003056INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3057
William Lallemand74c24fb2016-11-21 17:18:36 +01003058static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau4975d142021-03-13 11:00:33 +01003059 { CFG_GLOBAL, "stats", cli_parse_global },
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 { 0, NULL, NULL },
3061}};
3062
Willy Tarreau0108d902018-11-25 19:14:37 +01003063INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3064
William Lallemand74c24fb2016-11-21 17:18:36 +01003065static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02003066 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
3067 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02003068 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01003069 { NULL, NULL, 0 },
3070}};
3071
Willy Tarreau0108d902018-11-25 19:14:37 +01003072INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01003073
3074/*
3075 * Local variables:
3076 * c-indent-level: 8
3077 * c-basic-offset: 8
3078 * End:
3079 */