blob: 2b3ea3a3c9d8e0341e544a7d9e4619e3537fde5b [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>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020031#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#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>
William Lallemand3ba7c7b2021-11-10 10:57:18 +010047#include <haproxy/mworker.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020048#include <haproxy/mworker-t.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020049#include <haproxy/pattern-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020050#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020051#include <haproxy/pipe.h>
52#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020053#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020054#include <haproxy/sample-t.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020055#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020056#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020057#include <haproxy/session.h>
Willy Tarreaua74cb382020-10-15 21:29:49 +020058#include <haproxy/sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020059#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020060#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020061#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020062#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020063#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020064#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020065#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020066#include <haproxy/version.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010067
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020068#define PAYLOAD_PATTERN "<<"
69
William Lallemand74c24fb2016-11-21 17:18:36 +010070static struct applet cli_applet;
William Lallemand99e0bb92020-11-05 10:28:53 +010071static struct applet mcli_applet;
William Lallemand74c24fb2016-11-21 17:18:36 +010072
Willy Tarreau4975d142021-03-13 11:00:33 +010073static const char cli_permission_denied_msg[] =
William Lallemand74c24fb2016-11-21 17:18:36 +010074 "Permission denied\n"
75 "";
76
77
Christopher Faulet1bc04c72017-10-29 20:14:08 +010078static THREAD_LOCAL char *dynamic_usage_msg = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +010079
80/* List head of cli keywords */
81static struct cli_kw_list cli_keywords = {
82 .list = LIST_HEAD_INIT(cli_keywords.list)
83};
84
85extern const char *stat_status_codes[];
86
Eric Salama1b8dacc2021-03-16 15:12:17 +010087struct proxy *mworker_proxy; /* CLI proxy of the master */
William Lallemand56f73b22022-09-24 15:56:25 +020088struct bind_conf *mcli_reload_bind_conf;
William Lallemand8a022572018-10-26 14:47:35 +020089
Willy Tarreau9a7fa902022-07-15 16:51:16 +020090/* CLI context for the "show activity" command */
91struct show_activity_ctx {
92 int thr; /* thread ID to show or -1 for all */
93};
94
Willy Tarreau307dbb32022-05-05 17:45:52 +020095/* CLI context for the "show env" command */
96struct show_env_ctx {
97 char **var; /* first variable to show */
98 int show_one; /* stop after showing the first one */
99};
100
Willy Tarreau741a5a92022-05-05 17:56:58 +0200101/* CLI context for the "show fd" command */
102struct show_fd_ctx {
103 int fd; /* first FD to show */
104 int show_one; /* stop after showing one FD */
105};
106
Willy Tarreaub128f492022-05-05 19:11:05 +0200107/* CLI context for the "show cli sockets" command */
108struct show_sock_ctx {
109 struct bind_conf *bind_conf;
110 struct listener *listener;
111};
112
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200113static int cmp_kw_entries(const void *a, const void *b)
114{
115 const struct cli_kw *l = *(const struct cli_kw **)a;
116 const struct cli_kw *r = *(const struct cli_kw **)b;
117
118 return strcmp(l->usage ? l->usage : "", r->usage ? r->usage : "");
119}
120
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100121/* This will show the help message and list the commands supported at the
122 * current level that match all of the first words of <args> if args is not
123 * NULL, or all args if none matches or if args is null.
124 */
125static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args)
William Lallemand74c24fb2016-11-21 17:18:36 +0100126{
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200127 struct cli_kw *entries[CLI_MAX_HELP_ENTRIES];
William Lallemand74c24fb2016-11-21 17:18:36 +0100128 struct cli_kw_list *kw_list;
129 struct cli_kw *kw;
Willy Tarreau83061a82018-07-13 11:56:34 +0200130 struct buffer *tmp = get_trash_chunk();
131 struct buffer out;
Willy Tarreaub7364582021-03-12 18:24:46 +0100132 struct { struct cli_kw *kw; int dist; } matches[CLI_MAX_MATCHES], swp;
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100133 int idx;
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200134 int ishelp = 0;
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100135 int length = 0;
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200136 int help_entries = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100137
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100138 ha_free(&dynamic_usage_msg);
William Lallemand74c24fb2016-11-21 17:18:36 +0100139
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200140 if (args && *args && strcmp(*args, "help") == 0) {
141 args++;
142 ishelp = 1;
143 }
144
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100145 /* first, let's measure the longest match */
146 list_for_each_entry(kw_list, &cli_keywords.list, list) {
147 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100148 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100149 continue;
William Lallemand2a171912022-02-02 11:43:20 +0100150 if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
151 (appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100152 (ACCESS_MASTER_ONLY|ACCESS_MASTER))
153 continue;
154
155 /* OK this command is visible */
156 for (idx = 0; idx < CLI_PREFIX_KW_NB; idx++) {
157 if (!kw->str_kw[idx])
158 break; // end of keyword
159 if (!args || !args[idx] || !*args[idx])
160 break; // end of command line
161 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
162 break;
163 if (idx + 1 > length)
164 length = idx + 1;
165 }
166 }
167 }
168
Willy Tarreaub7364582021-03-12 18:24:46 +0100169 /* now <length> equals the number of exactly matching words */
William Lallemand74c24fb2016-11-21 17:18:36 +0100170 chunk_reset(tmp);
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200171 if (ishelp) // this is the help message.
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100172 chunk_strcat(tmp, "The following commands are valid at this level:\n");
Abhijeet Rastogic8601502022-11-17 04:42:38 -0800173 else {
174 chunk_strcat(tmp, "Unknown command: '");
175 if (args && *args)
176 chunk_strcat(tmp, *args);
177 chunk_strcat(tmp, "'");
178
179 if (!length && (!args || !*args || !**args)) // no match
180 chunk_strcat(tmp, ". Please enter one of the following commands only:\n");
181 else // partial match
182 chunk_strcat(tmp, ", but maybe one of the following ones is a better match:\n");
183 }
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100184
Willy Tarreaub7364582021-03-12 18:24:46 +0100185 for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
186 matches[idx].kw = NULL;
187 matches[idx].dist = INT_MAX;
188 }
189
190 /* In case of partial match we'll look for the best matching entries
191 * starting from position <length>
192 */
Willy Tarreau9c187472021-03-13 12:25:43 +0100193 if (args && args[length] && *args[length]) {
Willy Tarreaub7364582021-03-12 18:24:46 +0100194 list_for_each_entry(kw_list, &cli_keywords.list, list) {
195 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100196 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreaub7364582021-03-12 18:24:46 +0100197 continue;
William Lallemand2a171912022-02-02 11:43:20 +0100198 if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
199 ((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
200 (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
Willy Tarreaub7364582021-03-12 18:24:46 +0100201 continue;
202
203 for (idx = 0; idx < length; idx++) {
204 if (!kw->str_kw[idx])
205 break; // end of keyword
206 if (!args || !args[idx] || !*args[idx])
207 break; // end of command line
208 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
209 break;
210 }
211
212 /* extra non-matching words are fuzzy-matched */
213 if (kw->usage && idx == length && args[idx] && *args[idx]) {
214 uint8_t word_sig[1024];
215 uint8_t list_sig[1024];
216 int dist = 0;
217 int totlen = 0;
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100218 int i;
Willy Tarreaub7364582021-03-12 18:24:46 +0100219
220 /* this one matches, let's compute the distance between the two
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100221 * on the remaining words. For this we're computing the signature
222 * of everything that remains and the cumulated length of the
223 * strings.
Willy Tarreaub7364582021-03-12 18:24:46 +0100224 */
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100225 memset(word_sig, 0, sizeof(word_sig));
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100226 for (i = idx; i < CLI_PREFIX_KW_NB && args[i] && *args[i]; i++) {
227 update_word_fingerprint(word_sig, args[i]);
228 totlen += strlen(args[i]);
229 }
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100230
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100231 memset(list_sig, 0, sizeof(list_sig));
232 for (i = idx; i < CLI_PREFIX_KW_NB && kw->str_kw[i]; i++) {
233 update_word_fingerprint(list_sig, kw->str_kw[i]);
234 totlen += strlen(kw->str_kw[i]);
Willy Tarreaub7364582021-03-12 18:24:46 +0100235 }
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100236
Willy Tarreauc57dcfe2021-03-12 19:01:59 +0100237 dist = word_fingerprint_distance(word_sig, list_sig);
Willy Tarreaub7364582021-03-12 18:24:46 +0100238
239 /* insert this one at its place if relevant, in order to keep only
240 * the best matches.
241 */
242 swp.kw = kw; swp.dist = dist;
Willy Tarreaua9aa6282021-03-15 10:00:29 +0100243 if (dist < 5*totlen/2 && dist < matches[CLI_MAX_MATCHES-1].dist) {
Willy Tarreaub7364582021-03-12 18:24:46 +0100244 matches[CLI_MAX_MATCHES-1] = swp;
245 for (idx = CLI_MAX_MATCHES - 1; --idx >= 0;) {
246 if (matches[idx+1].dist >= matches[idx].dist)
247 break;
248 matches[idx+1] = matches[idx];
249 matches[idx] = swp;
250 }
251 }
252 }
253 }
254 }
255 }
256
Willy Tarreauec197e82021-03-15 10:35:04 +0100257 if (matches[0].kw) {
258 /* we have fuzzy matches, let's propose them */
259 for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
260 kw = matches[idx].kw;
261 if (!kw)
262 break;
263
264 /* stop the dump if some words look very unlikely candidates */
265 if (matches[idx].dist > 5*matches[0].dist/2)
266 break;
267
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200268 if (help_entries < CLI_MAX_HELP_ENTRIES)
269 entries[help_entries++] = kw;
Willy Tarreauec197e82021-03-15 10:35:04 +0100270 }
271 }
272
William Lallemand74c24fb2016-11-21 17:18:36 +0100273 list_for_each_entry(kw_list, &cli_keywords.list, list) {
Willy Tarreauec197e82021-03-15 10:35:04 +0100274 /* no full dump if we've already found nice candidates */
275 if (matches[0].kw)
276 break;
277
Willy Tarreau91bc3592021-03-12 15:20:39 +0100278 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
William Lallemand14721be2018-10-26 14:47:37 +0200279
Willy Tarreau91bc3592021-03-12 15:20:39 +0100280 /* in a worker or normal process, don't display master-only commands
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100281 * nor expert/experimental mode commands if not in this mode.
Willy Tarreau91bc3592021-03-12 15:20:39 +0100282 */
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100283 if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
Willy Tarreau91bc3592021-03-12 15:20:39 +0100284 continue;
William Lallemand14721be2018-10-26 14:47:37 +0200285
William Lallemand2a171912022-02-02 11:43:20 +0100286 /* in master, if the CLI don't have the
287 * ACCESS_MCLI_DEBUG don't display commands that have
288 * neither the master bit nor the master-only bit.
Willy Tarreau91bc3592021-03-12 15:20:39 +0100289 */
William Lallemand2a171912022-02-02 11:43:20 +0100290 if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
291 ((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
292 (ACCESS_MASTER_ONLY|ACCESS_MASTER)))
Willy Tarreau91bc3592021-03-12 15:20:39 +0100293 continue;
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200294
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100295 for (idx = 0; idx < length; idx++) {
296 if (!kw->str_kw[idx])
297 break; // end of keyword
298 if (!args || !args[idx] || !*args[idx])
299 break; // end of command line
300 if (strcmp(kw->str_kw[idx], args[idx]) != 0)
301 break;
302 }
303
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200304 if (kw->usage && idx == length && help_entries < CLI_MAX_HELP_ENTRIES)
305 entries[help_entries++] = kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100306 }
307 }
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100308
Willy Tarreau92fbbcc2021-05-09 21:45:29 +0200309 qsort(entries, help_entries, sizeof(*entries), cmp_kw_entries);
310
311 for (idx = 0; idx < help_entries; idx++)
312 chunk_appendf(tmp, " %s\n", entries[idx]->usage);
313
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100314 /* always show the prompt/help/quit commands */
315 chunk_strcat(tmp,
Willy Tarreau0b1b8302021-05-09 20:59:23 +0200316 " help [<command>] : list matching or all commands\n"
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200317 " prompt : toggle interactive mode with prompt\n"
318 " quit : disconnect\n");
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100319
William Lallemand74c24fb2016-11-21 17:18:36 +0100320 chunk_init(&out, NULL, 0);
321 chunk_dup(&out, tmp);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200322 dynamic_usage_msg = out.area;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200323
Willy Tarreau1c0715b2022-05-06 17:16:35 +0200324 cli_msg(appctx, LOG_INFO, dynamic_usage_msg);
William Lallemand74c24fb2016-11-21 17:18:36 +0100325 return dynamic_usage_msg;
326}
327
328struct cli_kw* cli_find_kw(char **args)
329{
330 struct cli_kw_list *kw_list;
331 struct cli_kw *kw;/* current cli_kw */
332 char **tmp_args;
333 const char **tmp_str_kw;
334 int found = 0;
335
336 if (LIST_ISEMPTY(&cli_keywords.list))
337 return NULL;
338
339 list_for_each_entry(kw_list, &cli_keywords.list, list) {
340 kw = &kw_list->kw[0];
341 while (*kw->str_kw) {
342 tmp_args = args;
343 tmp_str_kw = kw->str_kw;
344 while (*tmp_str_kw) {
345 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
346 found = 1;
347 } else {
348 found = 0;
349 break;
350 }
351 tmp_args++;
352 tmp_str_kw++;
353 }
354 if (found)
355 return (kw);
356 kw++;
357 }
358 }
359 return NULL;
360}
361
Thierry Fourniera51a1fd2020-11-28 20:10:08 +0100362struct cli_kw* cli_find_kw_exact(char **args)
363{
364 struct cli_kw_list *kw_list;
365 int found = 0;
366 int i;
367 int j;
368
369 if (LIST_ISEMPTY(&cli_keywords.list))
370 return NULL;
371
372 list_for_each_entry(kw_list, &cli_keywords.list, list) {
373 for (i = 0; kw_list->kw[i].str_kw[0]; i++) {
374 found = 1;
375 for (j = 0; j < CLI_PREFIX_KW_NB; j++) {
376 if (args[j] == NULL && kw_list->kw[i].str_kw[j] == NULL) {
377 break;
378 }
379 if (args[j] == NULL || kw_list->kw[i].str_kw[j] == NULL) {
380 found = 0;
381 break;
382 }
383 if (strcmp(args[j], kw_list->kw[i].str_kw[j]) != 0) {
384 found = 0;
385 break;
386 }
387 }
388 if (found)
389 return &kw_list->kw[i];
390 }
391 }
392 return NULL;
393}
394
William Lallemand74c24fb2016-11-21 17:18:36 +0100395void cli_register_kw(struct cli_kw_list *kw_list)
396{
Willy Tarreau2b718102021-04-21 07:32:39 +0200397 LIST_APPEND(&cli_keywords.list, &kw_list->list);
William Lallemand74c24fb2016-11-21 17:18:36 +0100398}
399
Willy Tarreau06d0e2e2022-03-29 15:25:30 +0200400/* list all known keywords on stdout, one per line */
401void cli_list_keywords(void)
402{
403 struct cli_kw_list *kw_list;
Willy Tarreau44651712022-03-30 12:02:35 +0200404 struct cli_kw *kwp, *kwn, *kw;
Willy Tarreau06d0e2e2022-03-29 15:25:30 +0200405 int idx;
406
Willy Tarreau44651712022-03-30 12:02:35 +0200407 for (kwn = kwp = NULL;; kwp = kwn) {
408 list_for_each_entry(kw_list, &cli_keywords.list, list) {
409 /* note: we sort based on the usage message when available,
410 * otherwise we fall back to the first keyword.
411 */
412 for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
413 if (strordered(kwp ? kwp->usage ? kwp->usage : kwp->str_kw[0] : NULL,
414 kw->usage ? kw->usage : kw->str_kw[0],
415 kwn != kwp ? kwn->usage ? kwn->usage : kwn->str_kw[0] : NULL))
416 kwn = kw;
Willy Tarreau06d0e2e2022-03-29 15:25:30 +0200417 }
Willy Tarreau44651712022-03-30 12:02:35 +0200418 }
419
420 if (kwn == kwp)
421 break;
422
423 for (idx = 0; kwn->str_kw[idx]; idx++) {
424 printf("%s ", kwn->str_kw[idx]);
Willy Tarreau06d0e2e2022-03-29 15:25:30 +0200425 }
Willy Tarreau44651712022-03-30 12:02:35 +0200426 if (kwn->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER))
427 printf("[MASTER] ");
428 if (!(kwn->level & ACCESS_MASTER_ONLY))
429 printf("[WORKER] ");
430 if (kwn->level & ACCESS_EXPERT)
431 printf("[EXPERT] ");
432 if (kwn->level & ACCESS_EXPERIMENTAL)
433 printf("[EXPERIM] ");
434 printf("\n");
Willy Tarreau06d0e2e2022-03-29 15:25:30 +0200435 }
436}
William Lallemand74c24fb2016-11-21 17:18:36 +0100437
438/* allocate a new stats frontend named <name>, and return it
439 * (or NULL in case of lack of memory).
440 */
Willy Tarreau4975d142021-03-13 11:00:33 +0100441static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100442{
443 struct proxy *fe;
444
445 fe = calloc(1, sizeof(*fe));
446 if (!fe)
447 return NULL;
448
449 init_new_proxy(fe);
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100450 fe->next = proxies_list;
451 proxies_list = fe;
William Lallemand74c24fb2016-11-21 17:18:36 +0100452 fe->last_change = now.tv_sec;
453 fe->id = strdup("GLOBAL");
William Lallemand6640dbb2021-08-13 15:31:33 +0200454 fe->cap = PR_CAP_FE|PR_CAP_INT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100455 fe->maxconn = 10; /* default to 10 concurrent connections */
456 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
457 fe->conf.file = strdup(file);
458 fe->conf.line = line;
459 fe->accept = frontend_accept;
460 fe->default_target = &cli_applet.obj_type;
461
462 /* the stats frontend is the only one able to assign ID #0 */
463 fe->conf.id.key = fe->uuid = 0;
464 eb32_insert(&used_proxy_id, &fe->conf.id);
465 return fe;
466}
467
468/* This function parses a "stats" statement in the "global" section. It returns
469 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
470 * error message into the <err> buffer which will be preallocated. The trailing
471 * '\n' must not be written. The function must be called with <args> pointing to
472 * the first word after "stats".
473 */
Willy Tarreau4975d142021-03-13 11:00:33 +0100474static int cli_parse_global(char **args, int section_type, struct proxy *curpx,
475 const struct proxy *defpx, const char *file, int line,
476 char **err)
William Lallemand74c24fb2016-11-21 17:18:36 +0100477{
478 struct bind_conf *bind_conf;
479 struct listener *l;
480
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100481 if (strcmp(args[1], "socket") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100482 int cur_arg;
483
484 if (*args[2] == 0) {
485 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
486 return -1;
487 }
488
Willy Tarreau4975d142021-03-13 11:00:33 +0100489 if (!global.cli_fe) {
490 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100491 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
492 return -1;
493 }
494 }
495
Willy Tarreau4975d142021-03-13 11:00:33 +0100496 bind_conf = bind_conf_alloc(global.cli_fe, file, line, args[2], xprt_get(XPRT_RAW));
Christopher Faulet0c6d1dc2021-04-12 16:56:37 +0200497 if (!bind_conf) {
498 memprintf(err, "'%s %s' : out of memory trying to allocate a bind_conf", args[0], args[1]);
499 return -1;
500 }
William Lallemand07a62f72017-05-24 00:57:40 +0200501 bind_conf->level &= ~ACCESS_LVL_MASK;
502 bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
Willy Tarreau6bdf9452022-07-06 11:52:24 +0200503 bind_conf->bind_tgroup = 1; // bind to a single group in any case
William Lallemand74c24fb2016-11-21 17:18:36 +0100504
Willy Tarreau4975d142021-03-13 11:00:33 +0100505 if (!str2listener(args[2], global.cli_fe, bind_conf, file, line, err)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100506 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
507 file, line, args[0], args[1], err && *err ? *err : "error");
508 return -1;
509 }
510
511 cur_arg = 3;
512 while (*args[cur_arg]) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100513 struct bind_kw *kw;
Willy Tarreau433b05f2021-03-12 10:14:07 +0100514 const char *best;
Willy Tarreau0a1e1cb2021-11-20 20:10:41 +0100515 int code;
William Lallemand74c24fb2016-11-21 17:18:36 +0100516
517 kw = bind_find_kw(args[cur_arg]);
518 if (kw) {
519 if (!kw->parse) {
520 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
521 args[0], args[1], args[cur_arg]);
522 return -1;
523 }
524
Willy Tarreau0a1e1cb2021-11-20 20:10:41 +0100525 code = kw->parse(args, cur_arg, global.cli_fe, bind_conf, err);
526
527 /* FIXME: this is ugly, we don't have a way to collect warnings,
528 * yet some important bind keywords may report warnings that we
529 * must display.
530 */
531 if (((code & (ERR_WARN|ERR_FATAL|ERR_ALERT)) == ERR_WARN) && err && *err) {
532 indent_msg(err, 2);
533 ha_warning("parsing [%s:%d] : '%s %s' : %s\n", file, line, args[0], args[1], *err);
534 ha_free(err);
535 }
536
537 if (code & ~ERR_WARN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100538 if (err && *err)
539 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
540 else
541 memprintf(err, "'%s %s' : error encountered while processing '%s'",
542 args[0], args[1], args[cur_arg]);
543 return -1;
544 }
545
546 cur_arg += 1 + kw->skip;
547 continue;
548 }
549
Willy Tarreau433b05f2021-03-12 10:14:07 +0100550 best = bind_find_best_kw(args[cur_arg]);
551 if (best)
552 memprintf(err, "'%s %s' : unknown keyword '%s'. Did you mean '%s' maybe ?",
553 args[0], args[1], args[cur_arg], best);
554 else
555 memprintf(err, "'%s %s' : unknown keyword '%s'.",
556 args[0], args[1], args[cur_arg]);
William Lallemand74c24fb2016-11-21 17:18:36 +0100557 return -1;
558 }
559
560 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100561 l->accept = session_accept_fd;
Willy Tarreau4975d142021-03-13 11:00:33 +0100562 l->default_target = global.cli_fe->default_target;
William Lallemand74c24fb2016-11-21 17:18:36 +0100563 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
564 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +0100565 global.maxsock++; /* for the listening socket */
William Lallemand74c24fb2016-11-21 17:18:36 +0100566 }
567 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100568 else if (strcmp(args[1], "timeout") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100569 unsigned timeout;
570 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
571
Willy Tarreau9faebe32019-06-07 19:00:37 +0200572 if (res == PARSE_TIME_OVER) {
573 memprintf(err, "timer overflow in argument '%s' to '%s %s' (maximum value is 2147483647 ms or ~24.8 days)",
574 args[2], args[0], args[1]);
575 return -1;
576 }
577 else if (res == PARSE_TIME_UNDER) {
578 memprintf(err, "timer underflow in argument '%s' to '%s %s' (minimum non-null value is 1 ms)",
579 args[2], args[0], args[1]);
580 return -1;
581 }
582 else if (res) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100583 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
584 return -1;
585 }
586
587 if (!timeout) {
588 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
589 return -1;
590 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100591 if (!global.cli_fe) {
592 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100593 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
594 return -1;
595 }
596 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100597 global.cli_fe->timeout.client = MS_TO_TICKS(timeout);
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100599 else if (strcmp(args[1], "maxconn") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 int maxconn = atol(args[2]);
601
602 if (maxconn <= 0) {
603 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
604 return -1;
605 }
606
Willy Tarreau4975d142021-03-13 11:00:33 +0100607 if (!global.cli_fe) {
608 if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
610 return -1;
611 }
612 }
Willy Tarreau4975d142021-03-13 11:00:33 +0100613 global.cli_fe->maxconn = maxconn;
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 }
Willy Tarreau94f763b2022-07-15 17:14:40 +0200615 else if (strcmp(args[1], "bind-process") == 0) {
616 memprintf(err, "'%s' is not supported anymore.", args[0]);
617 return -1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100618 }
619 else {
620 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
621 return -1;
622 }
623 return 0;
624}
625
William Lallemand33d29e22019-04-01 11:30:06 +0200626/*
William Lallemand9a37fd02019-04-12 16:09:24 +0200627 * This function exports the bound addresses of a <frontend> in the environment
628 * variable <varname>. Those addresses are separated by semicolons and prefixed
629 * with their type (abns@, unix@, sockpair@ etc)
630 * Return -1 upon error, 0 otherwise
William Lallemand33d29e22019-04-01 11:30:06 +0200631 */
William Lallemand9a37fd02019-04-12 16:09:24 +0200632int listeners_setenv(struct proxy *frontend, const char *varname)
William Lallemand33d29e22019-04-01 11:30:06 +0200633{
634 struct buffer *trash = get_trash_chunk();
635 struct bind_conf *bind_conf;
636
William Lallemand9a37fd02019-04-12 16:09:24 +0200637 if (frontend) {
638 list_for_each_entry(bind_conf, &frontend->conf.bind, by_fe) {
William Lallemand33d29e22019-04-01 11:30:06 +0200639 struct listener *l;
640
641 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
642 char addr[46];
643 char port[6];
644
William Lallemand620072b2019-04-12 16:09:25 +0200645 /* separate listener by semicolons */
646 if (trash->data)
647 chunk_appendf(trash, ";");
648
Willy Tarreau37159062020-08-27 07:48:42 +0200649 if (l->rx.addr.ss_family == AF_UNIX) {
William Lallemand33d29e22019-04-01 11:30:06 +0200650 const struct sockaddr_un *un;
651
Willy Tarreau37159062020-08-27 07:48:42 +0200652 un = (struct sockaddr_un *)&l->rx.addr;
William Lallemand33d29e22019-04-01 11:30:06 +0200653 if (un->sun_path[0] == '\0') {
654 chunk_appendf(trash, "abns@%s", un->sun_path+1);
655 } else {
656 chunk_appendf(trash, "unix@%s", un->sun_path);
657 }
Willy Tarreau37159062020-08-27 07:48:42 +0200658 } else if (l->rx.addr.ss_family == AF_INET) {
659 addr_to_str(&l->rx.addr, addr, sizeof(addr));
660 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemand33d29e22019-04-01 11:30:06 +0200661 chunk_appendf(trash, "ipv4@%s:%s", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +0200662 } else if (l->rx.addr.ss_family == AF_INET6) {
663 addr_to_str(&l->rx.addr, addr, sizeof(addr));
664 port_to_str(&l->rx.addr, port, sizeof(port));
William Lallemand33d29e22019-04-01 11:30:06 +0200665 chunk_appendf(trash, "ipv6@[%s]:%s", addr, port);
Willy Tarreau37159062020-08-27 07:48:42 +0200666 } else if (l->rx.addr.ss_family == AF_CUST_SOCKPAIR) {
667 chunk_appendf(trash, "sockpair@%d", ((struct sockaddr_in *)&l->rx.addr)->sin_addr.s_addr);
William Lallemand33d29e22019-04-01 11:30:06 +0200668 }
William Lallemand33d29e22019-04-01 11:30:06 +0200669 }
670 }
671 trash->area[trash->data++] = '\0';
William Lallemand9a37fd02019-04-12 16:09:24 +0200672 if (setenv(varname, trash->area, 1) < 0)
William Lallemand33d29e22019-04-01 11:30:06 +0200673 return -1;
674 }
675
676 return 0;
677}
678
William Lallemand9a37fd02019-04-12 16:09:24 +0200679int cli_socket_setenv()
680{
Willy Tarreau4975d142021-03-13 11:00:33 +0100681 if (listeners_setenv(global.cli_fe, "HAPROXY_CLI") < 0)
William Lallemand9a37fd02019-04-12 16:09:24 +0200682 return -1;
683 if (listeners_setenv(mworker_proxy, "HAPROXY_MASTER_CLI") < 0)
684 return -1;
685
686 return 0;
687}
688
William Lallemand33d29e22019-04-01 11:30:06 +0200689REGISTER_CONFIG_POSTPARSER("cli", cli_socket_setenv);
690
Willy Tarreaude57a572016-11-23 17:01:39 +0100691/* Verifies that the CLI at least has a level at least as high as <level>
692 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
693 * failure, an error message is prepared and the appctx's state is adjusted
694 * to print it so that a return 1 is enough to abort any processing.
695 */
696int cli_has_level(struct appctx *appctx, int level)
697{
Willy Tarreaude57a572016-11-23 17:01:39 +0100698
William Lallemandf630d012018-12-13 09:05:44 +0100699 if ((appctx->cli_level & ACCESS_LVL_MASK) < level) {
Willy Tarreau4975d142021-03-13 11:00:33 +0100700 cli_err(appctx, cli_permission_denied_msg);
Willy Tarreaude57a572016-11-23 17:01:39 +0100701 return 0;
702 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100703 return 1;
704}
705
William Lallemandb7ea1412018-12-13 09:05:47 +0100706/* same as cli_has_level but for the CLI proxy and without error message */
707int pcli_has_level(struct stream *s, int level)
708{
709 if ((s->pcli_flags & ACCESS_LVL_MASK) < level) {
710 return 0;
711 }
712 return 1;
713}
714
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200715/* Returns severity_output for the current session if set, or default for the socket */
716static int cli_get_severity_output(struct appctx *appctx)
717{
718 if (appctx->cli_severity_output)
719 return appctx->cli_severity_output;
Willy Tarreau0698c802022-05-11 14:09:57 +0200720 return strm_li(appctx_strm(appctx))->bind_conf->severity_output;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200721}
William Lallemand74c24fb2016-11-21 17:18:36 +0100722
Willy Tarreau41908562016-11-24 16:23:38 +0100723/* Processes the CLI interpreter on the stats socket. This function is called
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100724 * from the CLI's IO handler running in an appctx context. The function returns
725 * 1 if the request was understood, otherwise zero (in which case an error
726 * message will be displayed). It is called with appctx->st0
Willy Tarreau41908562016-11-24 16:23:38 +0100727 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
728 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
729 * have its own I/O handler called again. Most of the time, parsers will only
730 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100731 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
732 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100733 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200734static int cli_parse_request(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100735{
Willy Tarreauf14c7572021-03-13 10:59:23 +0100736 char *args[MAX_CLI_ARGS + 1], *p, *end, *payload = NULL;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200737 int i = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100738 struct cli_kw *kw;
William Lallemand74c24fb2016-11-21 17:18:36 +0100739
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200740 p = appctx->chunk->area;
741 end = p + appctx->chunk->data;
William Lallemand74c24fb2016-11-21 17:18:36 +0100742
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200743 /*
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200744 * Get pointers on words.
745 * One extra slot is reserved to store a pointer on a null byte.
746 */
Willy Tarreauf14c7572021-03-13 10:59:23 +0100747 while (i < MAX_CLI_ARGS && p < end) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200748 int j, k;
William Lallemand74c24fb2016-11-21 17:18:36 +0100749
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200750 /* skip leading spaces/tabs */
751 p += strspn(p, " \t");
752 if (!*p)
753 break;
Willy Tarreauf2dda522021-09-17 11:07:45 +0200754
755 if (strcmp(p, PAYLOAD_PATTERN) == 0) {
756 /* payload pattern recognized here, this is not an arg anymore,
757 * the payload starts at the first byte that follows the zero
758 * after the pattern.
759 */
760 payload = p + strlen(PAYLOAD_PATTERN) + 1;
761 break;
762 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100763
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200764 args[i] = p;
Yves Lafonb08c6d02020-06-08 16:08:06 +0200765 while (1) {
766 p += strcspn(p, " \t\\");
767 /* escaped chars using backlashes (\) */
768 if (*p == '\\') {
769 if (!*++p)
770 break;
771 if (!*++p)
772 break;
773 } else {
774 break;
775 }
776 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200777 *p++ = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100778
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200779 /* unescape backslashes (\) */
780 for (j = 0, k = 0; args[i][k]; k++) {
781 if (args[i][k] == '\\') {
782 if (args[i][k + 1] == '\\')
783 k++;
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100784 else
785 continue;
786 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200787 args[i][j] = args[i][k];
William Lallemand74c24fb2016-11-21 17:18:36 +0100788 j++;
789 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200790 args[i][j] = 0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100791
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200792 i++;
793 }
794 /* fill unused slots */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200795 p = appctx->chunk->area + appctx->chunk->data;
Willy Tarreauf14c7572021-03-13 10:59:23 +0100796 for (; i < MAX_CLI_ARGS + 1; i++)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200797 args[i] = p;
Willy Tarreau41908562016-11-24 16:23:38 +0100798
799 kw = cli_find_kw(args);
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100800 if (!kw ||
801 (kw->level & ~appctx->cli_level & ACCESS_MASTER_ONLY) ||
William Lallemand2a171912022-02-02 11:43:20 +0100802 (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
803 (appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) == (ACCESS_MASTER_ONLY|ACCESS_MASTER))) {
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100804 /* keyword not found in this mode */
Willy Tarreaub96a74c2021-03-12 17:13:28 +0100805 cli_gen_usage_msg(appctx, args);
Willy Tarreau41908562016-11-24 16:23:38 +0100806 return 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100807 }
William Lallemand14721be2018-10-26 14:47:37 +0200808
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100809 /* don't handle expert mode commands if not in this mode. */
810 if (kw->level & ~appctx->cli_level & ACCESS_EXPERT) {
811 cli_err(appctx, "This command is restricted to expert mode only.\n");
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200812 return 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +0100813 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +0200814
Amaury Denoyelle18487fb2021-03-18 15:32:53 +0100815 if (kw->level & ~appctx->cli_level & ACCESS_EXPERIMENTAL) {
816 cli_err(appctx, "This command is restricted to experimental mode only.\n");
817 return 0;
818 }
819
Amaury Denoyellef4929922021-05-05 16:29:23 +0200820 if (kw->level == ACCESS_EXPERT)
821 mark_tainted(TAINTED_CLI_EXPERT_MODE);
822 else if (kw->level == ACCESS_EXPERIMENTAL)
823 mark_tainted(TAINTED_CLI_EXPERIMENTAL_MODE);
824
Willy Tarreau41908562016-11-24 16:23:38 +0100825 appctx->io_handler = kw->io_handler;
Emeric Brund6871f72017-06-29 19:54:13 +0200826 appctx->io_release = kw->io_release;
William Lallemand90b098c2019-10-25 21:10:14 +0200827
828 if (kw->parse && kw->parse(args, payload, appctx, kw->private) != 0)
829 goto fail;
830
831 /* kw->parse could set its own io_handler or io_release handler */
832 if (!appctx->io_handler)
833 goto fail;
834
835 appctx->st0 = CLI_ST_CALLBACK;
836 return 1;
837fail:
838 appctx->io_handler = NULL;
839 appctx->io_release = NULL;
Willy Tarreau41908562016-11-24 16:23:38 +0100840 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100841}
842
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200843/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
844static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
845{
Willy Tarreau83061a82018-07-13 11:56:34 +0200846 struct buffer *tmp;
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200847
848 if (likely(severity_output == CLI_SEVERITY_NONE))
Willy Tarreau06d80a92017-10-19 14:32:15 +0200849 return ci_putblk(chn, msg, strlen(msg));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200850
851 tmp = get_trash_chunk();
852 chunk_reset(tmp);
853
854 if (severity < 0 || severity > 7) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100855 ha_warning("socket command feedback with invalid severity %d", severity);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200856 chunk_printf(tmp, "[%d]: ", severity);
857 }
858 else {
859 switch (severity_output) {
860 case CLI_SEVERITY_NUMBER:
861 chunk_printf(tmp, "[%d]: ", severity);
862 break;
863 case CLI_SEVERITY_STRING:
864 chunk_printf(tmp, "[%s]: ", log_levels[severity]);
865 break;
866 default:
Christopher Faulet767a84b2017-11-24 16:50:31 +0100867 ha_warning("Unrecognized severity output %d", severity_output);
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200868 }
869 }
870 chunk_appendf(tmp, "%s", msg);
871
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200872 return ci_putblk(chn, tmp->area, strlen(tmp->area));
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200873}
874
Willy Tarreau4596fe22022-05-17 19:07:51 +0200875/* This I/O handler runs as an applet embedded in a stream connector. It is
William Lallemand74c24fb2016-11-21 17:18:36 +0100876 * used to processes I/O from/to the stats unix socket. The system relies on a
877 * state machine handling requests and various responses. We read a request,
878 * then we process it and send the response, and we possibly display a prompt.
879 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100880 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100881 * or not.
882 */
883static void cli_io_handler(struct appctx *appctx)
884{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200885 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +0200886 struct channel *req = sc_oc(sc);
887 struct channel *res = sc_ic(sc);
888 struct bind_conf *bind_conf = strm_li(__sc_strm(sc))->bind_conf;
William Lallemand74c24fb2016-11-21 17:18:36 +0100889 int reql;
890 int len;
891
Willy Tarreau475e4632022-05-27 10:26:46 +0200892 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
William Lallemand74c24fb2016-11-21 17:18:36 +0100893 goto out;
894
Joseph Herlant008b3ce2018-11-25 12:51:45 -0800895 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200896 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100897 /* buf.size==0 means we failed to get a buffer and were
898 * already subscribed to a wait list to get a buffer.
899 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100900 goto out;
901 }
902
William Lallemand74c24fb2016-11-21 17:18:36 +0100903 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100904 if (appctx->st0 == CLI_ST_INIT) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +0200905 /* reset severity to default at init */
906 appctx->cli_severity_output = bind_conf->severity_output;
Willy Tarreau1addf8b2022-08-18 18:04:37 +0200907 applet_reset_svcctx(appctx);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100908 appctx->st0 = CLI_ST_GETREQ;
William Lallemandf630d012018-12-13 09:05:44 +0100909 appctx->cli_level = bind_conf->level;
William Lallemand74c24fb2016-11-21 17:18:36 +0100910 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100911 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100912 /* Let's close for real now. We just close the request
913 * side, the conditions below will complete if needed.
914 */
Willy Tarreau475e4632022-05-27 10:26:46 +0200915 sc_shutw(sc);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200916 free_trash_chunk(appctx->chunk);
Willy Tarreau18b2a9d2021-05-04 16:27:45 +0200917 appctx->chunk = NULL;
William Lallemand74c24fb2016-11-21 17:18:36 +0100918 break;
919 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100920 else if (appctx->st0 == CLI_ST_GETREQ) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200921 char *str;
922
923 /* use a trash chunk to store received data */
924 if (!appctx->chunk) {
925 appctx->chunk = alloc_trash_chunk();
926 if (!appctx->chunk) {
927 appctx->st0 = CLI_ST_END;
928 continue;
929 }
930 }
931
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200932 str = appctx->chunk->area + appctx->chunk->data;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200933
William Lallemand74c24fb2016-11-21 17:18:36 +0100934 /* ensure we have some output room left in the event we
935 * would want to return some info right after parsing.
936 */
Willy Tarreau475e4632022-05-27 10:26:46 +0200937 if (buffer_almost_full(sc_ib(sc))) {
938 sc_need_room(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +0100939 break;
940 }
941
Willy Tarreau0011c252022-01-19 17:23:52 +0100942 /* payload doesn't take escapes nor does it end on semi-colons, so
943 * we use the regular getline. Normal mode however must stop on
944 * LFs and semi-colons that are not prefixed by a backslash. Note
945 * that we reserve one byte at the end to insert a trailing nul byte.
946 */
947
948 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Willy Tarreau475e4632022-05-27 10:26:46 +0200949 reql = co_getline(sc_oc(sc), str,
Willy Tarreau0011c252022-01-19 17:23:52 +0100950 appctx->chunk->size - appctx->chunk->data - 1);
951 else
Willy Tarreau475e4632022-05-27 10:26:46 +0200952 reql = co_getdelim(sc_oc(sc), str,
Willy Tarreau0011c252022-01-19 17:23:52 +0100953 appctx->chunk->size - appctx->chunk->data - 1,
954 "\n;", '\\');
955
William Lallemand74c24fb2016-11-21 17:18:36 +0100956 if (reql <= 0) { /* closed or EOL not found */
957 if (reql == 0)
958 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100959 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 continue;
961 }
962
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200963 if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
964 /* seek for a possible unescaped semi-colon. If we find
965 * one, we replace it with an LF and skip only this part.
966 */
967 for (len = 0; len < reql; len++) {
968 if (str[len] == '\\') {
969 len++;
970 continue;
971 }
972 if (str[len] == ';') {
973 str[len] = '\n';
974 reql = len + 1;
975 break;
976 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100977 }
978 }
979
980 /* now it is time to check that we have a full line,
981 * remove the trailing \n and possibly \r, then cut the
982 * line.
983 */
984 len = reql - 1;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200985 if (str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100986 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100987 continue;
988 }
989
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200990 if (len && str[len-1] == '\r')
William Lallemand74c24fb2016-11-21 17:18:36 +0100991 len--;
992
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200993 str[len] = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200994 appctx->chunk->data += len;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +0200995
996 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200997 appctx->chunk->area[appctx->chunk->data] = '\n';
998 appctx->chunk->area[appctx->chunk->data + 1] = 0;
999 appctx->chunk->data++;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001000 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001001
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001002 appctx->st0 = CLI_ST_PROMPT;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001003
1004 if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
1005 /* empty line */
1006 if (!len) {
1007 /* remove the last two \n */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001008 appctx->chunk->data -= 2;
1009 appctx->chunk->area[appctx->chunk->data] = 0;
Willy Tarreauf3697dd2021-03-12 15:57:47 +01001010 cli_parse_request(appctx);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001011 chunk_reset(appctx->chunk);
1012 /* NB: cli_sock_parse_request() may have put
1013 * another CLI_ST_O_* into appctx->st0.
1014 */
1015
1016 appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
William Lallemand74c24fb2016-11-21 17:18:36 +01001017 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001018 }
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001019 else {
1020 /*
1021 * Look for the "payload start" pattern at the end of a line
1022 * Its location is not remembered here, this is just to switch
1023 * to a gathering mode.
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 */
Willy Tarreauf2dda522021-09-17 11:07:45 +02001025 if (strcmp(appctx->chunk->area + appctx->chunk->data - strlen(PAYLOAD_PATTERN), PAYLOAD_PATTERN) == 0) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001026 appctx->st1 |= APPCTX_CLI_ST1_PAYLOAD;
Willy Tarreauf2dda522021-09-17 11:07:45 +02001027 appctx->chunk->data++; // keep the trailing \0 after '<<'
1028 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001029 else {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001030 /* no payload, the command is complete: parse the request */
Willy Tarreauf3697dd2021-03-12 15:57:47 +01001031 cli_parse_request(appctx);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001032 chunk_reset(appctx->chunk);
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02001033 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 }
1035
1036 /* re-adjust req buffer */
Willy Tarreau475e4632022-05-27 10:26:46 +02001037 co_skip(sc_oc(sc), reql);
William Lallemand74c24fb2016-11-21 17:18:36 +01001038 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
1039 }
1040 else { /* output functions */
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001041 struct cli_print_ctx *ctx;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001042 const char *msg;
1043 int sev;
1044
William Lallemand74c24fb2016-11-21 17:18:36 +01001045 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001046 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +01001047 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001048 case CLI_ST_PRINT: /* print const message in msg */
1049 case CLI_ST_PRINT_ERR: /* print const error in msg */
1050 case CLI_ST_PRINT_DYN: /* print dyn message in msg, free */
Amaury Denoyelle56f50a02022-11-10 11:47:36 +01001051 case CLI_ST_PRINT_DYNERR: /* print dyn error in err, free */
Amaury Denoyelle24e99612022-11-10 14:24:51 +01001052 case CLI_ST_PRINT_UMSG: /* print usermsgs_ctx and reset it */
1053 case CLI_ST_PRINT_UMSGERR: /* print usermsgs_ctx as error and reset it */
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001054 /* the message is in the svcctx */
1055 ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001056 if (appctx->st0 == CLI_ST_PRINT || appctx->st0 == CLI_ST_PRINT_ERR) {
1057 sev = appctx->st0 == CLI_ST_PRINT_ERR ?
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001058 LOG_ERR : ctx->severity;
1059 msg = ctx->msg;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001060 }
Amaury Denoyelle56f50a02022-11-10 11:47:36 +01001061 else if (appctx->st0 == CLI_ST_PRINT_DYN || appctx->st0 == CLI_ST_PRINT_DYNERR) {
1062 sev = appctx->st0 == CLI_ST_PRINT_DYNERR ?
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001063 LOG_ERR : ctx->severity;
1064 msg = ctx->err;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001065 if (!msg) {
1066 sev = LOG_ERR;
1067 msg = "Out of memory.\n";
1068 }
1069 }
Amaury Denoyelle24e99612022-11-10 14:24:51 +01001070 else if (appctx->st0 == CLI_ST_PRINT_UMSG ||
1071 appctx->st0 == CLI_ST_PRINT_UMSGERR) {
1072 sev = appctx->st0 == CLI_ST_PRINT_UMSGERR ?
1073 LOG_ERR : ctx->severity;
1074 msg = usermsgs_str();
1075 }
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001076 else {
1077 sev = LOG_ERR;
1078 msg = "Internal error.\n";
1079 }
Aurélien Nephtalic511b7c2018-04-16 18:50:19 +02001080
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001081 if (cli_output_msg(res, msg, sev, cli_get_severity_output(appctx)) != -1) {
Amaury Denoyelle56f50a02022-11-10 11:47:36 +01001082 if (appctx->st0 == CLI_ST_PRINT_DYN ||
1083 appctx->st0 == CLI_ST_PRINT_DYNERR) {
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001084 ha_free(&ctx->err);
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001085 }
Amaury Denoyelle24e99612022-11-10 14:24:51 +01001086 else if (appctx->st0 == CLI_ST_PRINT_UMSG ||
1087 appctx->st0 == CLI_ST_PRINT_UMSGERR) {
1088 usermsgs_clr(NULL);
1089 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001090 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +01001091 }
1092 else
Willy Tarreau475e4632022-05-27 10:26:46 +02001093 sc_need_room(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01001094 break;
Willy Tarreaud50c7fe2019-08-09 09:57:36 +02001095
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001096 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +01001097 if (appctx->io_handler)
1098 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001099 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +01001100 if (appctx->io_release) {
1101 appctx->io_release(appctx);
1102 appctx->io_release = NULL;
1103 }
1104 }
1105 break;
1106 default: /* abnormal state */
Willy Tarreaud869e132022-05-17 18:05:31 +02001107 se_fl_set(appctx->sedesc, SE_FL_ERROR);
William Lallemand74c24fb2016-11-21 17:18:36 +01001108 break;
1109 }
1110
1111 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001112 if (appctx->st0 == CLI_ST_PROMPT) {
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001113 const char *prompt = "";
1114
1115 if (appctx->st1 & APPCTX_CLI_ST1_PROMPT) {
1116 /*
1117 * when entering a payload with interactive mode, change the prompt
1118 * to emphasize that more data can still be sent
1119 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001120 if (appctx->chunk->data && appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001121 prompt = "+ ";
1122 else
1123 prompt = "\n> ";
1124 }
1125 else {
William Lallemandad032882019-07-01 10:56:15 +02001126 if (!(appctx->st1 & (APPCTX_CLI_ST1_PAYLOAD|APPCTX_CLI_ST1_NOLF)))
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001127 prompt = "\n";
1128 }
1129
Willy Tarreau1addf8b2022-08-18 18:04:37 +02001130 if (applet_putstr(appctx, prompt) != -1) {
1131 applet_reset_svcctx(appctx);
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001132 appctx->st0 = CLI_ST_GETREQ;
Willy Tarreau1addf8b2022-08-18 18:04:37 +02001133 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001134 }
1135
1136 /* If the output functions are still there, it means they require more room. */
Christopher Faulet4167e052022-06-01 17:25:42 +02001137 if (appctx->st0 >= CLI_ST_OUTPUT) {
1138 applet_wont_consume(appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001139 break;
Christopher Faulet4167e052022-06-01 17:25:42 +02001140 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001141
1142 /* Now we close the output if one of the writers did so,
1143 * or if we're not in interactive mode and the request
1144 * buffer is empty. This still allows pipelined requests
1145 * to be sent in non-interactive mode.
1146 */
William Lallemand3de09d52018-12-11 16:10:56 +01001147 if (((res->flags & (CF_SHUTW|CF_SHUTW_NOW))) ||
1148 (!(appctx->st1 & APPCTX_CLI_ST1_PROMPT) && !co_data(req) && (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)))) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001149 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01001150 continue;
1151 }
1152
1153 /* switch state back to GETREQ to read next requests */
Willy Tarreau1addf8b2022-08-18 18:04:37 +02001154 applet_reset_svcctx(appctx);
Willy Tarreau3b6e5472016-11-24 15:53:53 +01001155 appctx->st0 = CLI_ST_GETREQ;
Christopher Faulet4167e052022-06-01 17:25:42 +02001156 applet_will_consume(appctx);
1157
William Lallemandad032882019-07-01 10:56:15 +02001158 /* reactivate the \n at the end of the response for the next command */
1159 appctx->st1 &= ~APPCTX_CLI_ST1_NOLF;
Willy Tarreaufa7b4f62022-01-19 17:11:36 +01001160
1161 /* this forces us to yield between pipelined commands and
1162 * avoid extremely long latencies (e.g. "del map" etc). In
1163 * addition this increases the likelihood that the stream
1164 * refills the buffer with new bytes in non-interactive
1165 * mode, avoiding to close on apparently empty commands.
1166 */
Willy Tarreau475e4632022-05-27 10:26:46 +02001167 if (co_data(sc_oc(sc))) {
Willy Tarreaufa7b4f62022-01-19 17:11:36 +01001168 appctx_wakeup(appctx);
1169 goto out;
1170 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001171 }
1172 }
1173
Willy Tarreau475e4632022-05-27 10:26:46 +02001174 if ((res->flags & CF_SHUTR) && (sc->state == SC_ST_EST)) {
1175 DPRINTF(stderr, "%s@%d: sc to buf closed. req=%08x, res=%08x, st=%d\n",
1176 __FUNCTION__, __LINE__, req->flags, res->flags, sc->state);
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 /* Other side has closed, let's abort if we have no more processing to do
1178 * and nothing more to consume. This is comparable to a broken pipe, so
1179 * we forward the close to the request side so that it flows upstream to
1180 * the client.
1181 */
Willy Tarreau475e4632022-05-27 10:26:46 +02001182 sc_shutw(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01001183 }
1184
Willy Tarreau475e4632022-05-27 10:26:46 +02001185 if ((req->flags & CF_SHUTW) && (sc->state == SC_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
1186 DPRINTF(stderr, "%s@%d: buf to sc closed. req=%08x, res=%08x, st=%d\n",
1187 __FUNCTION__, __LINE__, req->flags, res->flags, sc->state);
William Lallemand74c24fb2016-11-21 17:18:36 +01001188 /* We have no more processing to do, and nothing more to send, and
1189 * the client side has closed. So we'll forward this state downstream
1190 * on the response buffer.
1191 */
Willy Tarreau475e4632022-05-27 10:26:46 +02001192 sc_shutr(sc);
William Lallemand74c24fb2016-11-21 17:18:36 +01001193 res->flags |= CF_READ_NULL;
1194 }
1195
1196 out:
Christopher Faulet45073512018-07-20 10:16:29 +02001197 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 +01001198 __FUNCTION__, __LINE__,
Willy Tarreau475e4632022-05-27 10:26:46 +02001199 sc->state, req->flags, res->flags, ci_data(req), co_data(req), ci_data(res), co_data(res));
William Lallemand74c24fb2016-11-21 17:18:36 +01001200}
1201
Willy Tarreau4596fe22022-05-17 19:07:51 +02001202/* This is called when the stream connector is closed. For instance, upon an
William Lallemand74c24fb2016-11-21 17:18:36 +01001203 * external abort, we won't call the i/o handler anymore so we may need to
1204 * remove back references to the stream currently being dumped.
1205 */
1206static void cli_release_handler(struct appctx *appctx)
1207{
Willy Tarreau18b2a9d2021-05-04 16:27:45 +02001208 free_trash_chunk(appctx->chunk);
1209 appctx->chunk = NULL;
1210
William Lallemand74c24fb2016-11-21 17:18:36 +01001211 if (appctx->io_release) {
1212 appctx->io_release(appctx);
1213 appctx->io_release = NULL;
1214 }
Amaury Denoyelle56f50a02022-11-10 11:47:36 +01001215 else if (appctx->st0 == CLI_ST_PRINT_DYN || appctx->st0 == CLI_ST_PRINT_DYNERR) {
Willy Tarreau1c0715b2022-05-06 17:16:35 +02001216 struct cli_print_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1217
1218 ha_free(&ctx->err);
William Lallemand74c24fb2016-11-21 17:18:36 +01001219 }
Amaury Denoyelle24e99612022-11-10 14:24:51 +01001220 else if (appctx->st0 == CLI_ST_PRINT_UMSG || appctx->st0 == CLI_ST_PRINT_UMSGERR) {
1221 usermsgs_clr(NULL);
1222 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001223}
1224
1225/* This function dumps all environmnent variables to the buffer. It returns 0
1226 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreau307dbb32022-05-05 17:45:52 +02001227 * non-zero. It takes its context from the show_env_ctx in svcctx, and will
1228 * start from ->var and dump only one variable if ->show_one is set.
William Lallemand74c24fb2016-11-21 17:18:36 +01001229 */
Willy Tarreau0a739292016-11-22 20:21:23 +01001230static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001231{
Willy Tarreau307dbb32022-05-05 17:45:52 +02001232 struct show_env_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001233 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau307dbb32022-05-05 17:45:52 +02001234 char **var = ctx->var;
William Lallemand74c24fb2016-11-21 17:18:36 +01001235
Willy Tarreau475e4632022-05-27 10:26:46 +02001236 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
William Lallemand74c24fb2016-11-21 17:18:36 +01001237 return 1;
1238
1239 chunk_reset(&trash);
1240
1241 /* we have two inner loops here, one for the proxy, the other one for
1242 * the buffer.
1243 */
Willy Tarreauf6710f82016-12-16 17:45:44 +01001244 while (*var) {
1245 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +01001246
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001247 if (applet_putchk(appctx, &trash) == -1)
William Lallemand74c24fb2016-11-21 17:18:36 +01001248 return 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001249
Willy Tarreau307dbb32022-05-05 17:45:52 +02001250 if (ctx->show_one)
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001252 var++;
Willy Tarreau307dbb32022-05-05 17:45:52 +02001253 ctx->var = var;
William Lallemand74c24fb2016-11-21 17:18:36 +01001254 }
1255
1256 /* dump complete */
1257 return 1;
1258}
1259
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001260/* This function dumps all file descriptors states (or the requested one) to
1261 * the buffer. It returns 0 if the output buffer is full and it needs to be
Willy Tarreau741a5a92022-05-05 17:56:58 +02001262 * called again, otherwise non-zero. It takes its context from the show_fd_ctx
1263 * in svcctx, only dumps one entry if ->show_one is non-zero, and (re)starts
1264 * from ->fd.
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001265 */
1266static int cli_io_handler_show_fd(struct appctx *appctx)
1267{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001268 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau741a5a92022-05-05 17:56:58 +02001269 struct show_fd_ctx *fdctx = appctx->svcctx;
1270 int fd = fdctx->fd;
Willy Tarreauca1b1572018-12-18 15:45:11 +01001271 int ret = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001272
Willy Tarreau475e4632022-05-27 10:26:46 +02001273 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreauca1b1572018-12-18 15:45:11 +01001274 goto end;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001275
1276 chunk_reset(&trash);
1277
Willy Tarreauca1b1572018-12-18 15:45:11 +01001278 /* isolate the threads once per round. We're limited to a buffer worth
1279 * of output anyway, it cannot last very long.
1280 */
1281 thread_isolate();
1282
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001283 /* we have two inner loops here, one for the proxy, the other one for
1284 * the buffer.
1285 */
Aurélien Nephtali498a1152018-03-09 18:51:16 +01001286 while (fd >= 0 && fd < global.maxsock) {
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001287 struct fdtab fdt;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001288 const struct listener *li = NULL;
1289 const struct server *sv = NULL;
1290 const struct proxy *px = NULL;
1291 const struct connection *conn = NULL;
Willy Tarreaua833cd92018-03-29 13:19:37 +02001292 const struct mux_ops *mux = NULL;
Willy Tarreau37be9532021-01-20 14:40:04 +01001293 const struct xprt_ops *xprt = NULL;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001294 const void *ctx = NULL;
Willy Tarreau37be9532021-01-20 14:40:04 +01001295 const void *xprt_ctx = NULL;
Willy Tarreau286ec682017-08-09 16:35:44 +02001296 uint32_t conn_flags = 0;
Willy Tarreaue9ca8072018-12-19 18:40:58 +01001297 int is_back = 0;
Willy Tarreau8050efe2021-01-21 08:26:06 +01001298 int suspicious = 0;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001299
1300 fdt = fdtab[fd];
1301
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001302 /* When DEBUG_FD is set, we also report closed FDs that have a
1303 * non-null event count to detect stuck ones.
1304 */
Willy Tarreau13c1a012020-06-29 14:23:31 +02001305 if (!fdt.owner) {
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001306#ifdef DEBUG_FD
Willy Tarreau13c1a012020-06-29 14:23:31 +02001307 if (!fdt.event_count)
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001308#endif
Willy Tarreau13c1a012020-06-29 14:23:31 +02001309 goto skip; // closed
1310 }
Willy Tarreau586f71b2020-12-11 15:54:36 +01001311 else if (fdt.iocb == sock_conn_iocb) {
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001312 conn = (const struct connection *)fdt.owner;
1313 conn_flags = conn->flags;
1314 mux = conn->mux;
1315 ctx = conn->ctx;
Willy Tarreau37be9532021-01-20 14:40:04 +01001316 xprt = conn->xprt;
1317 xprt_ctx = conn->xprt_ctx;
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001318 li = objt_listener(conn->target);
1319 sv = objt_server(conn->target);
1320 px = objt_proxy(conn->target);
1321 is_back = conn_is_back(conn);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001322 if (atleast2(fdt.thread_mask))
1323 suspicious = 1;
1324 if (conn->handle.fd != fd)
1325 suspicious = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001326 }
Willy Tarreaua74cb382020-10-15 21:29:49 +02001327 else if (fdt.iocb == sock_accept_iocb)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001328 li = fdt.owner;
1329
Willy Tarreaudacfde42021-01-21 09:07:29 +01001330 if (!fdt.thread_mask)
1331 suspicious = 1;
1332
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001333 chunk_printf(&trash,
Willy Tarreauc2431822022-07-08 10:23:01 +02001334 " %5d : st=0x%06x(%c%c %c%c%c%c%c W:%c%c%c R:%c%c%c) ref=%#x gid=%d tmask=0x%lx umask=0x%lx owner=%p iocb=%p(",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001335 fd,
1336 fdt.state,
Willy Tarreau184b2122021-04-07 08:48:12 +02001337 (fdt.state & FD_CLONED) ? 'C' : 'c',
1338 (fdt.state & FD_LINGER_RISK) ? 'L' : 'l',
Willy Tarreauf5090652021-04-06 17:23:40 +02001339 (fdt.state & FD_POLL_HUP) ? 'H' : 'h',
1340 (fdt.state & FD_POLL_ERR) ? 'E' : 'e',
1341 (fdt.state & FD_POLL_OUT) ? 'O' : 'o',
1342 (fdt.state & FD_POLL_PRI) ? 'P' : 'p',
1343 (fdt.state & FD_POLL_IN) ? 'I' : 'i',
Willy Tarreau184b2122021-04-07 08:48:12 +02001344 (fdt.state & FD_EV_SHUT_W) ? 'S' : 's',
1345 (fdt.state & FD_EV_READY_W) ? 'R' : 'r',
1346 (fdt.state & FD_EV_ACTIVE_W) ? 'A' : 'a',
1347 (fdt.state & FD_EV_SHUT_R) ? 'S' : 's',
1348 (fdt.state & FD_EV_READY_R) ? 'R' : 'r',
1349 (fdt.state & FD_EV_ACTIVE_R) ? 'A' : 'a',
Willy Tarreauc2431822022-07-08 10:23:01 +02001350 (fdt.refc_tgid >> 4) & 0xffff,
1351 (fdt.refc_tgid) & 0xffff,
Willy Tarreauc754b342018-03-30 15:00:15 +02001352 fdt.thread_mask, fdt.update_mask,
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001353 fdt.owner,
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001354 fdt.iocb);
1355 resolve_sym_name(&trash, NULL, fdt.iocb);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001356
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001357 if (!fdt.owner) {
1358 chunk_appendf(&trash, ")");
1359 }
Willy Tarreau586f71b2020-12-11 15:54:36 +01001360 else if (fdt.iocb == sock_conn_iocb) {
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001361 chunk_appendf(&trash, ") back=%d cflg=0x%08x", is_back, conn_flags);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001362
1363 if (conn->handle.fd != fd) {
1364 chunk_appendf(&trash, " fd=%d(BOGUS)", conn->handle.fd);
1365 suspicious = 1;
Willy Tarreaued989202021-02-05 10:54:52 +01001366 } else {
1367 struct sockaddr_storage sa;
1368 socklen_t salen;
1369
1370 salen = sizeof(sa);
1371 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1372 if (sa.ss_family == AF_INET)
1373 chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1374 else if (sa.ss_family == AF_INET6)
1375 chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1376 else if (sa.ss_family == AF_UNIX)
1377 chunk_appendf(&trash, " fam=unix");
1378 }
1379
1380 salen = sizeof(sa);
1381 if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
1382 if (sa.ss_family == AF_INET)
1383 chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1384 else if (sa.ss_family == AF_INET6)
1385 chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1386 }
Willy Tarreaudacfde42021-01-21 09:07:29 +01001387 }
1388
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001389 if (px)
1390 chunk_appendf(&trash, " px=%s", px->id);
1391 else if (sv)
Willy Tarreaudfb34a82021-07-06 11:41:10 +02001392 chunk_appendf(&trash, " sv=%s/%s", sv->proxy->id, sv->id);
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001393 else if (li)
1394 chunk_appendf(&trash, " fe=%s", li->bind_conf->frontend->id);
Willy Tarreau35b1b482018-03-28 18:41:30 +02001395
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001396 if (mux) {
Willy Tarreau3d2ee552018-12-19 14:12:10 +01001397 chunk_appendf(&trash, " mux=%s ctx=%p", mux->name, ctx);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001398 if (!ctx)
1399 suspicious = 1;
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001400 if (mux->show_fd)
Willy Tarreau8050efe2021-01-21 08:26:06 +01001401 suspicious |= mux->show_fd(&trash, fdt.owner);
Willy Tarreaub011d8f2018-03-30 14:41:19 +02001402 }
Willy Tarreau35b1b482018-03-28 18:41:30 +02001403 else
1404 chunk_appendf(&trash, " nomux");
Willy Tarreau37be9532021-01-20 14:40:04 +01001405
1406 chunk_appendf(&trash, " xprt=%s", xprt ? xprt->name : "");
Willy Tarreau108a2712021-01-20 15:30:56 +01001407 if (xprt) {
1408 if (xprt_ctx || xprt->show_fd)
1409 chunk_appendf(&trash, " xprt_ctx=%p", xprt_ctx);
1410 if (xprt->show_fd)
Willy Tarreau8050efe2021-01-21 08:26:06 +01001411 suspicious |= xprt->show_fd(&trash, conn, xprt_ctx);
Willy Tarreau108a2712021-01-20 15:30:56 +01001412 }
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001413 }
Willy Tarreaua74cb382020-10-15 21:29:49 +02001414 else if (fdt.iocb == sock_accept_iocb) {
Willy Tarreaued989202021-02-05 10:54:52 +01001415 struct sockaddr_storage sa;
1416 socklen_t salen;
1417
Willy Tarreaucf12f2e2020-03-03 17:29:58 +01001418 chunk_appendf(&trash, ") l.st=%s fe=%s",
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001419 listener_state_str(li),
1420 li->bind_conf->frontend->id);
Willy Tarreaued989202021-02-05 10:54:52 +01001421
1422 salen = sizeof(sa);
1423 if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
1424 if (sa.ss_family == AF_INET)
1425 chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
1426 else if (sa.ss_family == AF_INET6)
1427 chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
1428 else if (sa.ss_family == AF_UNIX)
1429 chunk_appendf(&trash, " fam=unix");
1430 }
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001431 }
Willy Tarreaueb0595d2021-01-20 14:13:46 +01001432 else
1433 chunk_appendf(&trash, ")");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001434
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001435#ifdef DEBUG_FD
1436 chunk_appendf(&trash, " evcnt=%u", fdtab[fd].event_count);
Willy Tarreaudacfde42021-01-21 09:07:29 +01001437 if (fdtab[fd].event_count >= 1000000)
1438 suspicious = 1;
Willy Tarreau38e8a1c2020-06-23 10:04:54 +02001439#endif
Willy Tarreau8050efe2021-01-21 08:26:06 +01001440 chunk_appendf(&trash, "%s\n", suspicious ? " !" : "");
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001441
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001442 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau741a5a92022-05-05 17:56:58 +02001443 fdctx->fd = fd;
Willy Tarreauca1b1572018-12-18 15:45:11 +01001444 ret = 0;
1445 break;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001446 }
1447 skip:
Willy Tarreau741a5a92022-05-05 17:56:58 +02001448 if (fdctx->show_one)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001449 break;
1450
1451 fd++;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001452 }
1453
Willy Tarreauca1b1572018-12-18 15:45:11 +01001454 end:
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001455 /* dump complete */
Willy Tarreauca1b1572018-12-18 15:45:11 +01001456
1457 thread_release();
1458 return ret;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001459}
1460
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001461/* This function dumps some activity counters used by developers and support to
1462 * rule out some hypothesis during bug reports. It returns 0 if the output
1463 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
1464 * everything at once in the buffer and is not designed to do it in multiple
1465 * passes.
1466 */
1467static int cli_io_handler_show_activity(struct appctx *appctx)
1468{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001469 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau9a7fa902022-07-15 16:51:16 +02001470 struct show_activity_ctx *actctx = appctx->svcctx;
1471 int tgt = actctx->thr; // target thread, -1 for all, 0 for total only
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001472 int thr;
1473
Willy Tarreau475e4632022-05-27 10:26:46 +02001474 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001475 return 1;
1476
1477 chunk_reset(&trash);
1478
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001479#undef SHOW_TOT
1480#define SHOW_TOT(t, x) \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001481 do { \
1482 unsigned int _v[MAX_THREADS]; \
1483 unsigned int _tot; \
1484 const unsigned int _nbt = global.nbthread; \
Willy Tarreau97b5d072021-11-20 19:17:38 +01001485 _tot = t = 0; \
1486 do { \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001487 _tot += _v[t] = (x); \
Willy Tarreau97b5d072021-11-20 19:17:38 +01001488 } while (++t < _nbt); \
Willy Tarreaua0211b82019-05-28 15:09:08 +02001489 if (_nbt == 1) { \
1490 chunk_appendf(&trash, " %u\n", _tot); \
1491 break; \
1492 } \
Willy Tarreau9a7fa902022-07-15 16:51:16 +02001493 if (tgt == -1) { \
1494 chunk_appendf(&trash, " %u [", _tot); \
1495 for (t = 0; t < _nbt; t++) \
1496 chunk_appendf(&trash, " %u", _v[t]); \
1497 chunk_appendf(&trash, " ]\n"); \
1498 } else if (tgt == 0) \
1499 chunk_appendf(&trash, " %u\n", _tot); \
1500 else \
1501 chunk_appendf(&trash, " %u\n", _v[tgt-1]);\
Willy Tarreaua0211b82019-05-28 15:09:08 +02001502 } while (0)
1503
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001504#undef SHOW_AVG
1505#define SHOW_AVG(t, x) \
1506 do { \
1507 unsigned int _v[MAX_THREADS]; \
1508 unsigned int _tot; \
1509 const unsigned int _nbt = global.nbthread; \
Willy Tarreau97b5d072021-11-20 19:17:38 +01001510 _tot = t = 0; \
1511 do { \
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001512 _tot += _v[t] = (x); \
Willy Tarreau97b5d072021-11-20 19:17:38 +01001513 } while (++t < _nbt); \
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001514 if (_nbt == 1) { \
1515 chunk_appendf(&trash, " %u\n", _tot); \
1516 break; \
1517 } \
Willy Tarreau9a7fa902022-07-15 16:51:16 +02001518 if (tgt == -1) { \
1519 chunk_appendf(&trash, " %u [", (_tot + _nbt/2) / _nbt); \
1520 for (t = 0; t < _nbt; t++) \
1521 chunk_appendf(&trash, " %u", _v[t]); \
1522 chunk_appendf(&trash, " ]\n"); \
1523 } else if (tgt == 0) \
1524 chunk_appendf(&trash, " %u\n", (_tot + _nbt/2) / _nbt); \
1525 else \
1526 chunk_appendf(&trash, " %u\n", _v[tgt-1]);\
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001527 } while (0)
1528
Willy Tarreaua0211b82019-05-28 15:09:08 +02001529 chunk_appendf(&trash, "thread_id: %u (%u..%u)\n", tid + 1, 1, global.nbthread);
1530 chunk_appendf(&trash, "date_now: %lu.%06lu\n", (long)now.tv_sec, (long)now.tv_usec);
Willy Tarreaua00cf9b2020-06-17 20:44:28 +02001531 chunk_appendf(&trash, "ctxsw:"); SHOW_TOT(thr, activity[thr].ctxsw);
1532 chunk_appendf(&trash, "tasksw:"); SHOW_TOT(thr, activity[thr].tasksw);
1533 chunk_appendf(&trash, "empty_rq:"); SHOW_TOT(thr, activity[thr].empty_rq);
1534 chunk_appendf(&trash, "long_rq:"); SHOW_TOT(thr, activity[thr].long_rq);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001535 chunk_appendf(&trash, "loops:"); SHOW_TOT(thr, activity[thr].loops);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001536 chunk_appendf(&trash, "wake_tasks:"); SHOW_TOT(thr, activity[thr].wake_tasks);
1537 chunk_appendf(&trash, "wake_signal:"); SHOW_TOT(thr, activity[thr].wake_signal);
Willy Tarreaue5451532020-06-17 20:25:18 +02001538 chunk_appendf(&trash, "poll_io:"); SHOW_TOT(thr, activity[thr].poll_io);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001539 chunk_appendf(&trash, "poll_exp:"); SHOW_TOT(thr, activity[thr].poll_exp);
Willy Tarreaue4063862020-06-17 20:35:33 +02001540 chunk_appendf(&trash, "poll_drop_fd:"); SHOW_TOT(thr, activity[thr].poll_drop_fd);
Willy Tarreaue4063862020-06-17 20:35:33 +02001541 chunk_appendf(&trash, "poll_skip_fd:"); SHOW_TOT(thr, activity[thr].poll_skip_fd);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001542 chunk_appendf(&trash, "conn_dead:"); SHOW_TOT(thr, activity[thr].conn_dead);
Willy Tarreau7af4fa92020-06-17 20:49:49 +02001543 chunk_appendf(&trash, "stream_calls:"); SHOW_TOT(thr, activity[thr].stream_calls);
Willy Tarreaua8b2ce02019-05-28 17:04:16 +02001544 chunk_appendf(&trash, "pool_fail:"); SHOW_TOT(thr, activity[thr].pool_fail);
1545 chunk_appendf(&trash, "buf_wait:"); SHOW_TOT(thr, activity[thr].buf_wait);
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001546 chunk_appendf(&trash, "cpust_ms_tot:"); SHOW_TOT(thr, activity[thr].cpust_total / 2);
1547 chunk_appendf(&trash, "cpust_ms_1s:"); SHOW_TOT(thr, read_freq_ctr(&activity[thr].cpust_1s) / 2);
1548 chunk_appendf(&trash, "cpust_ms_15s:"); SHOW_TOT(thr, read_freq_ctr_period(&activity[thr].cpust_15s, 15000) / 2);
1549 chunk_appendf(&trash, "avg_loop_us:"); SHOW_AVG(thr, swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES));
1550 chunk_appendf(&trash, "accepted:"); SHOW_TOT(thr, activity[thr].accepted);
1551 chunk_appendf(&trash, "accq_pushed:"); SHOW_TOT(thr, activity[thr].accq_pushed);
1552 chunk_appendf(&trash, "accq_full:"); SHOW_TOT(thr, activity[thr].accq_full);
Willy Tarreaue6182842019-04-15 18:54:10 +02001553#ifdef USE_THREAD
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001554 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 +02001555 chunk_appendf(&trash, "fd_takeover:"); SHOW_TOT(thr, activity[thr].fd_takeover);
Willy Tarreaue6182842019-04-15 18:54:10 +02001556#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001557
Willy Tarreaud6a78502019-05-27 07:03:38 +02001558#if defined(DEBUG_DEV)
1559 /* keep these ones at the end */
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001560 chunk_appendf(&trash, "ctr0:"); SHOW_TOT(thr, activity[thr].ctr0);
1561 chunk_appendf(&trash, "ctr1:"); SHOW_TOT(thr, activity[thr].ctr1);
1562 chunk_appendf(&trash, "ctr2:"); SHOW_TOT(thr, activity[thr].ctr2);
Willy Tarreaud6a78502019-05-27 07:03:38 +02001563#endif
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001564
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001565 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001566 chunk_reset(&trash);
1567 chunk_printf(&trash, "[output too large, cannot dump]\n");
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001568 }
1569
Willy Tarreaud78d08f2019-05-28 16:32:06 +02001570#undef SHOW_AVG
1571#undef SHOW_TOT
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001572 /* dump complete */
1573 return 1;
1574}
1575
William Lallemandeceddf72016-12-15 18:06:44 +01001576/*
Willy Tarreau3af9d832016-12-16 12:58:09 +01001577 * CLI IO handler for `show cli sockets`.
Willy Tarreaub128f492022-05-05 19:11:05 +02001578 * Uses the svcctx as a show_sock_ctx to store/retrieve the bind_conf and the
1579 * listener pointers.
William Lallemandeceddf72016-12-15 18:06:44 +01001580 */
1581static int cli_io_handler_show_cli_sock(struct appctx *appctx)
1582{
Willy Tarreaub128f492022-05-05 19:11:05 +02001583 struct show_sock_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1584 struct bind_conf *bind_conf = ctx->bind_conf;
William Lallemandeceddf72016-12-15 18:06:44 +01001585
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001586 if (!global.cli_fe)
1587 goto done;
William Lallemandeceddf72016-12-15 18:06:44 +01001588
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001589 chunk_reset(&trash);
William Lallemandeceddf72016-12-15 18:06:44 +01001590
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001591 if (!bind_conf) {
1592 /* first call */
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001593 if (applet_putstr(appctx, "# socket lvl processes\n") == -1)
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001594 goto full;
1595 bind_conf = LIST_ELEM(global.cli_fe->conf.bind.n, typeof(bind_conf), by_fe);
1596 }
William Lallemandeceddf72016-12-15 18:06:44 +01001597
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001598 list_for_each_entry_from(bind_conf, &global.cli_fe->conf.bind, by_fe) {
Willy Tarreaub128f492022-05-05 19:11:05 +02001599 struct listener *l = ctx->listener;
William Lallemandeceddf72016-12-15 18:06:44 +01001600
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001601 if (!l)
1602 l = LIST_ELEM(bind_conf->listeners.n, typeof(l), by_bind);
William Lallemandeceddf72016-12-15 18:06:44 +01001603
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001604 list_for_each_entry_from(l, &bind_conf->listeners, by_bind) {
1605 char addr[46];
1606 char port[6];
William Lallemandeceddf72016-12-15 18:06:44 +01001607
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001608 if (l->rx.addr.ss_family == AF_UNIX) {
1609 const struct sockaddr_un *un;
William Lallemandeceddf72016-12-15 18:06:44 +01001610
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001611 un = (struct sockaddr_un *)&l->rx.addr;
1612 if (un->sun_path[0] == '\0') {
1613 chunk_appendf(&trash, "abns@%s ", un->sun_path+1);
1614 } else {
1615 chunk_appendf(&trash, "unix@%s ", un->sun_path);
1616 }
1617 } else if (l->rx.addr.ss_family == AF_INET) {
1618 addr_to_str(&l->rx.addr, addr, sizeof(addr));
1619 port_to_str(&l->rx.addr, port, sizeof(port));
1620 chunk_appendf(&trash, "ipv4@%s:%s ", addr, port);
1621 } else if (l->rx.addr.ss_family == AF_INET6) {
1622 addr_to_str(&l->rx.addr, addr, sizeof(addr));
1623 port_to_str(&l->rx.addr, port, sizeof(port));
1624 chunk_appendf(&trash, "ipv6@[%s]:%s ", addr, port);
1625 } else if (l->rx.addr.ss_family == AF_CUST_SOCKPAIR) {
1626 chunk_appendf(&trash, "sockpair@%d ", ((struct sockaddr_in *)&l->rx.addr)->sin_addr.s_addr);
1627 } else
1628 chunk_appendf(&trash, "unknown ");
William Lallemandeceddf72016-12-15 18:06:44 +01001629
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001630 if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
1631 chunk_appendf(&trash, "admin ");
1632 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
1633 chunk_appendf(&trash, "operator ");
1634 else if ((bind_conf->level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
1635 chunk_appendf(&trash, "user ");
1636 else
1637 chunk_appendf(&trash, " ");
William Lallemandeceddf72016-12-15 18:06:44 +01001638
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001639 chunk_appendf(&trash, "all\n");
William Lallemandeceddf72016-12-15 18:06:44 +01001640
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001641 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreaub128f492022-05-05 19:11:05 +02001642 ctx->bind_conf = bind_conf;
1643 ctx->listener = l;
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001644 goto full;
William Lallemandeceddf72016-12-15 18:06:44 +01001645 }
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001646 }
William Lallemandeceddf72016-12-15 18:06:44 +01001647 }
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001648 done:
1649 return 1;
1650 full:
Willy Tarreau4df54eb2022-05-05 18:52:36 +02001651 return 0;
William Lallemandeceddf72016-12-15 18:06:44 +01001652}
1653
1654
Willy Tarreau9a7fa902022-07-15 16:51:16 +02001655/* parse a "show activity" CLI request. Returns 0 if it needs to continue, 1 if it
1656 * wants to stop here. It sets a show_activity_ctx context where, if a specific
1657 * thread is requested, it puts the thread number into ->thr otherwise sets it to
1658 * -1.
1659 */
1660static int cli_parse_show_activity(char **args, char *payload, struct appctx *appctx, void *private)
1661{
1662 struct show_activity_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1663
1664 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1665 return 1;
1666
1667 ctx->thr = -1; // show all by default
1668 if (*args[2])
1669 ctx->thr = atoi(args[2]);
1670
1671 if (ctx->thr < -1 || ctx->thr > global.nbthread)
1672 return cli_err(appctx, "Thread ID number must be between -1 and nbthread\n");
1673
1674 return 0;
1675}
1676
Willy Tarreau0a739292016-11-22 20:21:23 +01001677/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreau307dbb32022-05-05 17:45:52 +02001678 * wants to stop here. It reserves a sohw_env_ctx where it puts the variable to
1679 * be dumped as well as a flag if a single variable is requested, otherwise puts
1680 * environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +01001681 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001682static int cli_parse_show_env(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0a739292016-11-22 20:21:23 +01001683{
Willy Tarreau307dbb32022-05-05 17:45:52 +02001684 struct show_env_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau0a739292016-11-22 20:21:23 +01001685 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +01001686 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001687
1688 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1689 return 1;
1690
Willy Tarreauf6710f82016-12-16 17:45:44 +01001691 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +01001692
1693 if (*args[2]) {
1694 int len = strlen(args[2]);
1695
Willy Tarreauf6710f82016-12-16 17:45:44 +01001696 for (; *var; var++) {
1697 if (strncmp(*var, args[2], len) == 0 &&
1698 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +01001699 break;
1700 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001701 if (!*var)
1702 return cli_err(appctx, "Variable not found\n");
1703
Willy Tarreau307dbb32022-05-05 17:45:52 +02001704 ctx->show_one = 1;
Willy Tarreau0a739292016-11-22 20:21:23 +01001705 }
Willy Tarreau307dbb32022-05-05 17:45:52 +02001706 ctx->var = var;
Willy Tarreau0a739292016-11-22 20:21:23 +01001707 return 0;
1708}
1709
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001710/* parse a "show fd" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreau741a5a92022-05-05 17:56:58 +02001711 * wants to stop here. It sets a show_fd_ctx context where, if a specific fd is
1712 * requested, it puts the FD number into ->fd and sets ->show_one to 1.
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001713 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001714static int cli_parse_show_fd(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001715{
Willy Tarreau741a5a92022-05-05 17:56:58 +02001716 struct show_fd_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1717
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001718 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1719 return 1;
1720
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001721 if (*args[2]) {
Willy Tarreau741a5a92022-05-05 17:56:58 +02001722 ctx->fd = atoi(args[2]);
1723 ctx->show_one = 1;
Willy Tarreau7a4a0ac2017-07-25 19:32:50 +02001724 }
1725 return 0;
1726}
1727
Willy Tarreau599852e2016-11-22 20:33:32 +01001728/* parse a "set timeout" CLI request. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001729static int cli_parse_set_timeout(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau599852e2016-11-22 20:33:32 +01001730{
Willy Tarreau0698c802022-05-11 14:09:57 +02001731 struct stream *s = appctx_strm(appctx);
Willy Tarreau599852e2016-11-22 20:33:32 +01001732
1733 if (strcmp(args[2], "cli") == 0) {
1734 unsigned timeout;
1735 const char *res;
1736
Willy Tarreau9d008692019-08-09 11:21:01 +02001737 if (!*args[3])
1738 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001739
1740 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
Willy Tarreau9d008692019-08-09 11:21:01 +02001741 if (res || timeout < 1)
1742 return cli_err(appctx, "Invalid timeout value.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001743
1744 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1745 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1746 return 1;
1747 }
Willy Tarreau9d008692019-08-09 11:21:01 +02001748
1749 return cli_err(appctx, "'set timeout' only supports 'cli'.\n");
Willy Tarreau599852e2016-11-22 20:33:32 +01001750}
1751
Willy Tarreau2af99412016-11-23 11:10:59 +01001752/* parse a "set maxconn global" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001753static int cli_parse_set_maxconn_global(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2af99412016-11-23 11:10:59 +01001754{
1755 int v;
1756
1757 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1758 return 1;
1759
Willy Tarreau9d008692019-08-09 11:21:01 +02001760 if (!*args[3])
1761 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001762
1763 v = atoi(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02001764 if (v > global.hardmaxconn)
1765 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau2af99412016-11-23 11:10:59 +01001766
1767 /* check for unlimited values */
1768 if (v <= 0)
1769 v = global.hardmaxconn;
1770
1771 global.maxconn = v;
1772
1773 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01001774 dequeue_all_listeners();
Willy Tarreau2af99412016-11-23 11:10:59 +01001775
1776 return 1;
1777}
1778
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001779static int set_severity_output(int *target, char *argument)
1780{
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001781 if (strcmp(argument, "none") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001782 *target = CLI_SEVERITY_NONE;
1783 return 1;
1784 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001785 else if (strcmp(argument, "number") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001786 *target = CLI_SEVERITY_NUMBER;
1787 return 1;
1788 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001789 else if (strcmp(argument, "string") == 0) {
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001790 *target = CLI_SEVERITY_STRING;
1791 return 1;
1792 }
1793 return 0;
1794}
1795
1796/* parse a "set severity-output" command. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001797static int cli_parse_set_severity_output(char **args, char *payload, struct appctx *appctx, void *private)
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001798{
1799 if (*args[2] && set_severity_output(&appctx->cli_severity_output, args[2]))
1800 return 0;
1801
Willy Tarreau9d008692019-08-09 11:21:01 +02001802 return cli_err(appctx, "one of 'none', 'number', 'string' is a required argument\n");
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02001803}
William Lallemandeceddf72016-12-15 18:06:44 +01001804
William Lallemand67a234f2018-12-13 09:05:45 +01001805
1806/* show the level of the current CLI session */
1807static int cli_parse_show_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1808{
William Lallemand67a234f2018-12-13 09:05:45 +01001809 if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_ADMIN)
Willy Tarreau9d008692019-08-09 11:21:01 +02001810 return cli_msg(appctx, LOG_INFO, "admin\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001811 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_OPER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001812 return cli_msg(appctx, LOG_INFO, "operator\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001813 else if ((appctx->cli_level & ACCESS_LVL_MASK) == ACCESS_LVL_USER)
Willy Tarreau9d008692019-08-09 11:21:01 +02001814 return cli_msg(appctx, LOG_INFO, "user\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001815 else
Willy Tarreau9d008692019-08-09 11:21:01 +02001816 return cli_msg(appctx, LOG_INFO, "unknown\n");
William Lallemand67a234f2018-12-13 09:05:45 +01001817}
1818
1819/* parse and set the CLI level dynamically */
1820static int cli_parse_set_lvl(char **args, char *payload, struct appctx *appctx, void *private)
1821{
William Lallemandad032882019-07-01 10:56:15 +02001822 /* this will ask the applet to not output a \n after the command */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001823 if (strcmp(args[1], "-") == 0)
William Lallemandad032882019-07-01 10:56:15 +02001824 appctx->st1 |= APPCTX_CLI_ST1_NOLF;
1825
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001826 if (strcmp(args[0], "operator") == 0) {
William Lallemand67a234f2018-12-13 09:05:45 +01001827 if (!cli_has_level(appctx, ACCESS_LVL_OPER)) {
1828 return 1;
1829 }
1830 appctx->cli_level &= ~ACCESS_LVL_MASK;
1831 appctx->cli_level |= ACCESS_LVL_OPER;
1832
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001833 } else if (strcmp(args[0], "user") == 0) {
William Lallemand67a234f2018-12-13 09:05:45 +01001834 if (!cli_has_level(appctx, ACCESS_LVL_USER)) {
1835 return 1;
1836 }
1837 appctx->cli_level &= ~ACCESS_LVL_MASK;
1838 appctx->cli_level |= ACCESS_LVL_USER;
1839 }
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001840 appctx->cli_level &= ~(ACCESS_EXPERT|ACCESS_EXPERIMENTAL);
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001841 return 1;
1842}
1843
1844
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001845/* parse and set the CLI expert/experimental-mode dynamically */
1846static int cli_parse_expert_experimental_mode(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001847{
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001848 int level;
1849 char *level_str;
1850 char *output = NULL;
1851
William Lallemand7267f782022-02-01 16:08:50 +01001852 /* this will ask the applet to not output a \n after the command */
1853 if (*args[1] && *args[2] && strcmp(args[2], "-") == 0)
1854 appctx->st1 |= APPCTX_CLI_ST1_NOLF;
1855
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001856 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1857 return 1;
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001858
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02001859 if (strcmp(args[0], "expert-mode") == 0) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001860 level = ACCESS_EXPERT;
1861 level_str = "expert-mode";
1862 }
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02001863 else if (strcmp(args[0], "experimental-mode") == 0) {
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001864 level = ACCESS_EXPERIMENTAL;
1865 level_str = "experimental-mode";
1866 }
William Lallemand2a171912022-02-02 11:43:20 +01001867 else if (strcmp(args[0], "mcli-debug-mode") == 0) {
1868 level = ACCESS_MCLI_DEBUG;
1869 level_str = "mcli-debug-mode";
1870 }
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001871 else {
1872 return 1;
1873 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001874
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001875 if (!*args[1]) {
1876 memprintf(&output, "%s is %s\n", level_str,
1877 (appctx->cli_level & level) ? "ON" : "OFF");
1878 return cli_dynmsg(appctx, LOG_INFO, output);
1879 }
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001880
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001881 appctx->cli_level &= ~level;
Willy Tarreauabb9f9b2019-10-24 17:55:53 +02001882 if (strcmp(args[1], "on") == 0)
Amaury Denoyelle18487fb2021-03-18 15:32:53 +01001883 appctx->cli_level |= level;
William Lallemand67a234f2018-12-13 09:05:45 +01001884 return 1;
1885}
1886
William Lallemand740629e2021-12-14 15:22:29 +01001887/* shows HAProxy version */
1888static int cli_parse_show_version(char **args, char *payload, struct appctx *appctx, void *private)
1889{
1890 char *msg = NULL;
1891
1892 return cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", haproxy_version));
1893}
1894
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001895int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemandeceddf72016-12-15 18:06:44 +01001896{
1897 return 0;
1898}
1899
Erwan Le Goas54966df2022-09-14 17:24:22 +02001900/* enable or disable the anonymized mode, it returns 1 when it works or displays an error message if it doesn't. */
1901static int cli_parse_set_anon(char **args, char *payload, struct appctx *appctx, void *private)
1902{
1903 uint32_t tmp;
1904 long long key;
1905
1906 if (strcmp(args[2], "on") == 0) {
Erwan Le Goas3f4ae612022-09-28 17:04:29 +02001907
1908 if (*args[3]) {
1909 key = atoll(args[3]);
1910 if (key < 1 || key > UINT_MAX)
1911 return cli_err(appctx, "Value out of range (1 to 4294967295 expected).\n");
1912 appctx->cli_anon_key = key;
1913 }
Erwan Le Goas54966df2022-09-14 17:24:22 +02001914 else {
Erwan Le Goas3f4ae612022-09-28 17:04:29 +02001915 tmp = HA_ATOMIC_LOAD(&global.anon_key);
1916 if (tmp != 0)
1917 appctx->cli_anon_key = tmp;
1918 else
1919 appctx->cli_anon_key = ha_random32();
Erwan Le Goas54966df2022-09-14 17:24:22 +02001920 }
1921 }
1922 else if (strcmp(args[2], "off") == 0) {
Erwan Le Goas3f4ae612022-09-28 17:04:29 +02001923
1924 if (*args[3]) {
Erwan Le Goas54966df2022-09-14 17:24:22 +02001925 return cli_err(appctx, "Key can't be added while disabling anonymized mode\n");
1926 }
1927 else {
1928 appctx->cli_anon_key = 0;
1929 }
1930 }
1931 else {
1932 return cli_err(appctx,
1933 "'set anon' only supports :\n"
1934 " - 'on' [key] to enable the anonymized mode\n"
1935 " - 'off' to disable the anonymized mode");
1936 }
1937 return 1;
1938}
1939
Erwan Le Goasfad9da82022-09-14 17:24:22 +02001940/* This function set the global anonyzing key, restricted to level 'admin' */
1941static int cli_parse_set_global_key(char **args, char *payload, struct appctx *appctx, void *private)
1942{
1943 long long key;
1944
1945 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1946 return cli_err(appctx, "Permission denied\n");
1947 if (!*args[2])
1948 return cli_err(appctx, "Expects an integer value.\n");
1949
1950 key = atoll(args[2]);
1951 if (key < 0 || key > UINT_MAX)
1952 return cli_err(appctx, "Value out of range (0 to 4294967295 expected).\n");
1953
1954 HA_ATOMIC_STORE(&global.anon_key, key);
1955 return 1;
1956}
1957
Erwan Le Goas54966df2022-09-14 17:24:22 +02001958/* shows the anonymized mode state to everyone, and the key except for users, it always returns 1. */
1959static int cli_parse_show_anon(char **args, char *payload, struct appctx *appctx, void *private)
1960{
1961 char *msg = NULL;
1962 char *anon_mode = NULL;
1963 uint32_t c_key = appctx->cli_anon_key;
1964
1965 if (!c_key)
1966 anon_mode = "Anonymized mode disabled";
1967 else
1968 anon_mode = "Anonymized mode enabled";
1969
1970 if ( !((appctx->cli_level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER) && c_key != 0) {
1971 cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\nKey : %u\n", anon_mode, c_key));
1972 }
1973 else {
1974 cli_dynmsg(appctx, LOG_INFO, memprintf(&msg, "%s\n", anon_mode));
1975 }
1976
1977 return 1;
1978}
1979
Willy Tarreau45c742b2016-11-24 14:51:17 +01001980/* parse a "set rate-limit" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001981static int cli_parse_set_ratelimit(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau45c742b2016-11-24 14:51:17 +01001982{
1983 int v;
1984 int *res;
1985 int mul = 1;
1986
1987 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1988 return 1;
1989
1990 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
1991 res = &global.cps_lim;
1992 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
1993 res = &global.sps_lim;
1994#ifdef USE_OPENSSL
1995 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
1996 res = &global.ssl_lim;
1997#endif
1998 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
1999 res = &global.comp_rate_lim;
2000 mul = 1024;
2001 }
2002 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02002003 return cli_err(appctx,
Willy Tarreau45c742b2016-11-24 14:51:17 +01002004 "'set rate-limit' only supports :\n"
2005 " - 'connections global' to set the per-process maximum connection rate\n"
2006 " - 'sessions global' to set the per-process maximum session rate\n"
2007#ifdef USE_OPENSSL
Aurélien Nephtalib53e2082018-03-11 16:55:02 +01002008 " - 'ssl-sessions global' to set the per-process maximum SSL session rate\n"
Willy Tarreau45c742b2016-11-24 14:51:17 +01002009#endif
Willy Tarreau9d008692019-08-09 11:21:01 +02002010 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01002011 }
2012
Willy Tarreau9d008692019-08-09 11:21:01 +02002013 if (!*args[4])
2014 return cli_err(appctx, "Expects an integer value.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01002015
2016 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002017 if (v < 0)
2018 return cli_err(appctx, "Value out of range.\n");
Willy Tarreau45c742b2016-11-24 14:51:17 +01002019
2020 *res = v * mul;
2021
2022 /* Dequeues all of the listeners waiting for a resource */
Willy Tarreau241797a2019-12-10 14:10:52 +01002023 dequeue_all_listeners();
Willy Tarreau45c742b2016-11-24 14:51:17 +01002024
2025 return 1;
2026}
2027
William Lallemandf6975e92017-05-26 17:42:10 +02002028/* parse the "expose-fd" argument on the bind lines */
2029static int bind_parse_expose_fd(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2030{
2031 if (!*args[cur_arg + 1]) {
2032 memprintf(err, "'%s' : missing fd type", args[cur_arg]);
2033 return ERR_ALERT | ERR_FATAL;
2034 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002035 if (strcmp(args[cur_arg + 1], "listeners") == 0) {
William Lallemandf6975e92017-05-26 17:42:10 +02002036 conf->level |= ACCESS_FD_LISTENERS;
2037 } else {
2038 memprintf(err, "'%s' only supports 'listeners' (got '%s')",
2039 args[cur_arg], args[cur_arg+1]);
2040 return ERR_ALERT | ERR_FATAL;
2041 }
2042
2043 return 0;
2044}
2045
William Lallemand74c24fb2016-11-21 17:18:36 +01002046/* parse the "level" argument on the bind lines */
2047static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2048{
2049 if (!*args[cur_arg + 1]) {
2050 memprintf(err, "'%s' : missing level", args[cur_arg]);
2051 return ERR_ALERT | ERR_FATAL;
2052 }
2053
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002054 if (strcmp(args[cur_arg + 1], "user") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02002055 conf->level &= ~ACCESS_LVL_MASK;
2056 conf->level |= ACCESS_LVL_USER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002057 } else if (strcmp(args[cur_arg + 1], "operator") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02002058 conf->level &= ~ACCESS_LVL_MASK;
2059 conf->level |= ACCESS_LVL_OPER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002060 } else if (strcmp(args[cur_arg + 1], "admin") == 0) {
William Lallemand07a62f72017-05-24 00:57:40 +02002061 conf->level &= ~ACCESS_LVL_MASK;
2062 conf->level |= ACCESS_LVL_ADMIN;
2063 } else {
William Lallemand74c24fb2016-11-21 17:18:36 +01002064 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
2065 args[cur_arg], args[cur_arg+1]);
2066 return ERR_ALERT | ERR_FATAL;
2067 }
2068
2069 return 0;
2070}
2071
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02002072static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2073{
2074 if (!*args[cur_arg + 1]) {
2075 memprintf(err, "'%s' : missing severity format", args[cur_arg]);
2076 return ERR_ALERT | ERR_FATAL;
2077 }
2078
2079 if (set_severity_output(&conf->severity_output, args[cur_arg+1]))
2080 return 0;
2081 else {
2082 memprintf(err, "'%s' only supports 'none', 'number', and 'string' (got '%s')",
2083 args[cur_arg], args[cur_arg+1]);
2084 return ERR_ALERT | ERR_FATAL;
2085 }
2086}
2087
Olivier Houchardf886e342017-04-05 22:24:59 +02002088/* Send all the bound sockets, always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002089static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchardf886e342017-04-05 22:24:59 +02002090{
William Lallemandb5d062d2022-07-28 15:33:41 +02002091 static int already_sent = 0;
Olivier Houchardf886e342017-04-05 22:24:59 +02002092 char *cmsgbuf = NULL;
2093 unsigned char *tmpbuf = NULL;
2094 struct cmsghdr *cmsg;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002095 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02002096 struct stream *s = __sc_strm(sc);
2097 struct connection *remote = sc_conn(sc_opposite(sc));
Olivier Houchardf886e342017-04-05 22:24:59 +02002098 struct msghdr msghdr;
2099 struct iovec iov;
Olivier Houchard54740872017-04-06 14:45:14 +02002100 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002101 const char *ns_name, *if_name;
2102 unsigned char ns_nlen, if_nlen;
2103 int nb_queued;
2104 int cur_fd = 0;
Olivier Houchardf886e342017-04-05 22:24:59 +02002105 int *tmpfd;
2106 int tot_fd_nb = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02002107 int fd = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02002108 int curoff = 0;
Willy Tarreauc2b7f802018-09-20 11:22:29 +02002109 int old_fcntl = -1;
Olivier Houchardf886e342017-04-05 22:24:59 +02002110 int ret;
2111
Willy Tarreauc2b7f802018-09-20 11:22:29 +02002112 if (!remote) {
2113 ha_warning("Only works on real connections\n");
2114 goto out;
2115 }
2116
2117 fd = remote->handle.fd;
2118
Olivier Houchardf886e342017-04-05 22:24:59 +02002119 /* Temporary set the FD in blocking mode, that will make our life easier */
2120 old_fcntl = fcntl(fd, F_GETFL);
2121 if (old_fcntl < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002122 ha_warning("Couldn't get the flags for the unix socket\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002123 goto out;
2124 }
2125 cmsgbuf = malloc(CMSG_SPACE(sizeof(int) * MAX_SEND_FD));
2126 if (!cmsgbuf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002127 ha_warning("Failed to allocate memory to send sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002128 goto out;
2129 }
2130 if (fcntl(fd, F_SETFL, old_fcntl &~ O_NONBLOCK) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002131 ha_warning("Cannot make the unix socket blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002132 goto out;
2133 }
Olivier Houchard54740872017-04-06 14:45:14 +02002134 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
Olivier Houchardf886e342017-04-05 22:24:59 +02002135 iov.iov_base = &tot_fd_nb;
2136 iov.iov_len = sizeof(tot_fd_nb);
William Lallemandf6975e92017-05-26 17:42:10 +02002137 if (!(strm_li(s)->bind_conf->level & ACCESS_FD_LISTENERS))
Olivier Houchardf886e342017-04-05 22:24:59 +02002138 goto out;
2139 memset(&msghdr, 0, sizeof(msghdr));
2140 /*
2141 * First, calculates the total number of FD, so that we can let
Jackie Tapia749f74c2020-07-22 18:59:40 -05002142 * the caller know how much it should expect.
Olivier Houchardf886e342017-04-05 22:24:59 +02002143 */
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002144 for (cur_fd = 0;cur_fd < global.maxsock; cur_fd++)
Willy Tarreau9063a662021-04-06 18:09:06 +02002145 tot_fd_nb += !!(fdtab[cur_fd].state & FD_EXPORTED);
William Lallemand5fd3b282020-01-16 15:32:08 +01002146
William Lallemandb5d062d2022-07-28 15:33:41 +02002147 if (tot_fd_nb == 0) {
2148 if (already_sent)
2149 ha_warning("_getsocks: attempt to get sockets but they were already sent and closed in this process!\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002150 goto out;
William Lallemandb5d062d2022-07-28 15:33:41 +02002151 }
Olivier Houchardf886e342017-04-05 22:24:59 +02002152
2153 /* First send the total number of file descriptors, so that the
2154 * receiving end knows what to expect.
2155 */
2156 msghdr.msg_iov = &iov;
2157 msghdr.msg_iovlen = 1;
2158 ret = sendmsg(fd, &msghdr, 0);
2159 if (ret != sizeof(tot_fd_nb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002160 ha_warning("Failed to send the number of sockets to send\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002161 goto out;
2162 }
2163
2164 /* Now send the fds */
2165 msghdr.msg_control = cmsgbuf;
2166 msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_SEND_FD);
2167 cmsg = CMSG_FIRSTHDR(&msghdr);
2168 cmsg->cmsg_len = CMSG_LEN(MAX_SEND_FD * sizeof(int));
2169 cmsg->cmsg_level = SOL_SOCKET;
2170 cmsg->cmsg_type = SCM_RIGHTS;
2171 tmpfd = (int *)CMSG_DATA(cmsg);
2172
Olivier Houchardf886e342017-04-05 22:24:59 +02002173 /* For each socket, e message is sent, containing the following :
2174 * Size of the namespace name (or 0 if none), as an unsigned char.
2175 * The namespace name, if any
2176 * Size of the interface name (or 0 if none), as an unsigned char
2177 * The interface name, if any
Willy Tarreaucf1f1932020-08-26 10:30:09 +02002178 * 32 bits of zeroes (used to be listener options).
Olivier Houchardf886e342017-04-05 22:24:59 +02002179 */
2180 /* We will send sockets MAX_SEND_FD per MAX_SEND_FD, allocate a
Ilya Shipitsind4259502020-04-08 01:07:56 +05002181 * buffer big enough to store the socket information.
Olivier Houchardf886e342017-04-05 22:24:59 +02002182 */
Olivier Houchardf143b802017-11-04 15:13:01 +01002183 tmpbuf = malloc(MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
Olivier Houchardf886e342017-04-05 22:24:59 +02002184 if (tmpbuf == NULL) {
Ilya Shipitsind4259502020-04-08 01:07:56 +05002185 ha_warning("Failed to allocate memory to transfer socket information\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002186 goto out;
2187 }
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002188
2189 nb_queued = 0;
Olivier Houchardf886e342017-04-05 22:24:59 +02002190 iov.iov_base = tmpbuf;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002191 for (cur_fd = 0; cur_fd < global.maxsock; cur_fd++) {
Willy Tarreau9063a662021-04-06 18:09:06 +02002192 if (!(fdtab[cur_fd].state & FD_EXPORTED))
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002193 continue;
2194
2195 ns_name = if_name = "";
2196 ns_nlen = if_nlen = 0;
2197
2198 /* for now we can only retrieve namespaces and interfaces from
2199 * pure listeners.
2200 */
Willy Tarreaua74cb382020-10-15 21:29:49 +02002201 if (fdtab[cur_fd].iocb == sock_accept_iocb) {
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002202 const struct listener *l = fdtab[cur_fd].owner;
2203
Willy Tarreau818a92e2020-09-03 07:50:19 +02002204 if (l->rx.settings->interface) {
2205 if_name = l->rx.settings->interface;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002206 if_nlen = strlen(if_name);
2207 }
2208
2209#ifdef USE_NS
Willy Tarreau818a92e2020-09-03 07:50:19 +02002210 if (l->rx.settings->netns) {
2211 ns_name = l->rx.settings->netns->node.key;
2212 ns_nlen = l->rx.settings->netns->name_len;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002213 }
2214#endif
2215 }
2216
2217 /* put the FD into the CMSG_DATA */
2218 tmpfd[nb_queued++] = cur_fd;
2219
2220 /* first block is <ns_name_len> <ns_name> */
2221 tmpbuf[curoff++] = ns_nlen;
2222 if (ns_nlen)
2223 memcpy(tmpbuf + curoff, ns_name, ns_nlen);
2224 curoff += ns_nlen;
2225
2226 /* second block is <if_name_len> <if_name> */
2227 tmpbuf[curoff++] = if_nlen;
2228 if (if_nlen)
2229 memcpy(tmpbuf + curoff, if_name, if_nlen);
2230 curoff += if_nlen;
2231
2232 /* we used to send the listener options here before 2.3 */
2233 memset(tmpbuf + curoff, 0, sizeof(int));
2234 curoff += sizeof(int);
2235
2236 /* there's a limit to how many FDs may be sent at once */
2237 if (nb_queued == MAX_SEND_FD) {
2238 iov.iov_len = curoff;
2239 if (sendmsg(fd, &msghdr, 0) != curoff) {
2240 ha_warning("Failed to transfer sockets\n");
2241 return -1;
2242 }
2243
2244 /* Wait for an ack */
2245 do {
2246 ret = recv(fd, &tot_fd_nb, sizeof(tot_fd_nb), 0);
2247 } while (ret == -1 && errno == EINTR);
2248
2249 if (ret <= 0) {
2250 ha_warning("Unexpected error while transferring sockets\n");
2251 return -1;
2252 }
2253 curoff = 0;
2254 nb_queued = 0;
2255 }
William Lallemand5fd3b282020-01-16 15:32:08 +01002256 }
2257
William Lallemandb5d062d2022-07-28 15:33:41 +02002258 already_sent = 1;
2259
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002260 /* flush pending stuff */
2261 if (nb_queued) {
Olivier Houchardf886e342017-04-05 22:24:59 +02002262 iov.iov_len = curoff;
Willy Tarreaub5a1f9e2020-08-19 17:03:55 +02002263 cmsg->cmsg_len = CMSG_LEN(nb_queued * sizeof(int));
2264 msghdr.msg_controllen = CMSG_SPACE(nb_queued * sizeof(int));
Olivier Houchardf886e342017-04-05 22:24:59 +02002265 if (sendmsg(fd, &msghdr, 0) != curoff) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002266 ha_warning("Failed to transfer sockets\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002267 goto out;
2268 }
2269 }
2270
2271out:
Willy Tarreauc2b7f802018-09-20 11:22:29 +02002272 if (fd >= 0 && old_fcntl >= 0 && fcntl(fd, F_SETFL, old_fcntl) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002273 ha_warning("Cannot make the unix socket non-blocking\n");
Olivier Houchardf886e342017-04-05 22:24:59 +02002274 goto out;
2275 }
2276 appctx->st0 = CLI_ST_END;
2277 free(cmsgbuf);
2278 free(tmpbuf);
2279 return 1;
2280}
2281
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002282static int cli_parse_simple(char **args, char *payload, struct appctx *appctx, void *private)
2283{
2284 if (*args[0] == 'h')
2285 /* help */
Willy Tarreau0b1b8302021-05-09 20:59:23 +02002286 cli_gen_usage_msg(appctx, args);
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002287 else if (*args[0] == 'p')
2288 /* prompt */
2289 appctx->st1 ^= APPCTX_CLI_ST1_PROMPT;
2290 else if (*args[0] == 'q')
2291 /* quit */
2292 appctx->st0 = CLI_ST_END;
2293
2294 return 1;
2295}
Olivier Houchardf886e342017-04-05 22:24:59 +02002296
William Lallemand2f4ce202018-10-26 14:47:47 +02002297void pcli_write_prompt(struct stream *s)
2298{
2299 struct buffer *msg = get_trash_chunk();
Willy Tarreau40a9c322022-05-18 15:55:18 +02002300 struct channel *oc = sc_oc(s->scf);
William Lallemand2f4ce202018-10-26 14:47:47 +02002301
William Lallemanddc12c2e2018-12-13 09:05:46 +01002302 if (!(s->pcli_flags & PCLI_F_PROMPT))
William Lallemand5b80fa22018-12-11 16:10:54 +01002303 return;
2304
William Lallemanddc12c2e2018-12-13 09:05:46 +01002305 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01002306 chunk_appendf(msg, "+ ");
2307 } else {
2308 if (s->pcli_next_pid == 0)
William Lallemanddae12c72022-02-02 14:13:54 +01002309 chunk_appendf(msg, "master%s",
William Lallemand3ba7c7b2021-11-10 10:57:18 +01002310 (proc_self->failedreloads > 0) ? "[ReloadFailed]" : "");
William Lallemandebf61802018-12-11 16:10:57 +01002311 else
William Lallemanddae12c72022-02-02 14:13:54 +01002312 chunk_appendf(msg, "%d", s->pcli_next_pid);
2313
2314 if (s->pcli_flags & (ACCESS_EXPERIMENTAL|ACCESS_EXPERT|ACCESS_MCLI_DEBUG)) {
2315 chunk_appendf(msg, "(");
2316
2317 if (s->pcli_flags & ACCESS_EXPERIMENTAL)
2318 chunk_appendf(msg, "x");
2319
2320 if (s->pcli_flags & ACCESS_EXPERT)
2321 chunk_appendf(msg, "e");
2322
2323 if (s->pcli_flags & ACCESS_MCLI_DEBUG)
2324 chunk_appendf(msg, "d");
2325
2326 chunk_appendf(msg, ")");
2327 }
2328
2329 chunk_appendf(msg, "> ");
2330
2331
William Lallemandebf61802018-12-11 16:10:57 +01002332 }
William Lallemand2f4ce202018-10-26 14:47:47 +02002333 co_inject(oc, msg->area, msg->data);
2334}
2335
William Lallemand291810d2018-10-26 14:47:38 +02002336
William Lallemandcf62f7e2018-10-26 14:47:40 +02002337/* The pcli_* functions are used for the CLI proxy in the master */
2338
William Lallemanddeeaa592018-10-26 14:47:48 +02002339void pcli_reply_and_close(struct stream *s, const char *msg)
2340{
2341 struct buffer *buf = get_trash_chunk();
2342
2343 chunk_initstr(buf, msg);
Christopher Faulet9125f3c2022-03-31 09:47:24 +02002344 stream_retnclose(s, buf);
William Lallemanddeeaa592018-10-26 14:47:48 +02002345}
2346
William Lallemand291810d2018-10-26 14:47:38 +02002347static enum obj_type *pcli_pid_to_server(int proc_pid)
2348{
2349 struct mworker_proc *child;
2350
William Lallemand99e0bb92020-11-05 10:28:53 +01002351 /* return the mCLI applet of the master */
William Lallemandbddd33a2018-12-11 16:10:53 +01002352 if (proc_pid == 0)
William Lallemand99e0bb92020-11-05 10:28:53 +01002353 return &mcli_applet.obj_type;
William Lallemandbddd33a2018-12-11 16:10:53 +01002354
William Lallemand291810d2018-10-26 14:47:38 +02002355 list_for_each_entry(child, &proc_list, list) {
2356 if (child->pid == proc_pid){
2357 return &child->srv->obj_type;
2358 }
2359 }
2360 return NULL;
2361}
2362
2363/* Take a CLI prefix in argument (eg: @!1234 @master @1)
2364 * Return:
2365 * 0: master
2366 * > 0: pid of a worker
2367 * < 0: didn't find a worker
2368 */
2369static int pcli_prefix_to_pid(const char *prefix)
2370{
2371 int proc_pid;
2372 struct mworker_proc *child;
2373 char *errtol = NULL;
2374
2375 if (*prefix != '@') /* not a prefix, should not happen */
2376 return -1;
2377
2378 prefix++;
2379 if (!*prefix) /* sent @ alone, return the master */
2380 return 0;
2381
2382 if (strcmp("master", prefix) == 0) {
2383 return 0;
2384 } else if (*prefix == '!') {
2385 prefix++;
2386 if (!*prefix)
2387 return -1;
2388
2389 proc_pid = strtol(prefix, &errtol, 10);
2390 if (*errtol != '\0')
2391 return -1;
2392 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02002393 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01002394 continue;
William Lallemand291810d2018-10-26 14:47:38 +02002395 if (child->pid == proc_pid){
2396 return child->pid;
2397 }
2398 }
2399 } else {
2400 struct mworker_proc *chosen = NULL;
2401 /* this is a relative pid */
2402
2403 proc_pid = strtol(prefix, &errtol, 10);
2404 if (*errtol != '\0')
2405 return -1;
2406
2407 if (proc_pid == 0) /* return the master */
2408 return 0;
2409
William Lallemandbac3a822022-07-20 14:30:56 +02002410 if (proc_pid != 1) /* only the "@1" relative PID is supported */
2411 return -1;
2412
William Lallemand291810d2018-10-26 14:47:38 +02002413 /* chose the right process, the current one is the one with the
2414 least number of reloads */
2415 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +02002416 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand16dd1b32018-11-19 18:46:18 +01002417 continue;
Willy Tarreaue8422bf2021-06-15 09:08:18 +02002418 if (child->reloads == 0)
2419 return child->pid;
2420 else if (chosen == NULL || child->reloads < chosen->reloads)
2421 chosen = child;
William Lallemand291810d2018-10-26 14:47:38 +02002422 }
2423 if (chosen)
2424 return chosen->pid;
2425 }
2426 return -1;
2427}
2428
William Lallemandbddd33a2018-12-11 16:10:53 +01002429/* Return::
2430 * >= 0 : number of words to escape
2431 * = -1 : error
2432 */
2433
2434int pcli_find_and_exec_kw(struct stream *s, char **args, int argl, char **errmsg, int *next_pid)
2435{
2436 if (argl < 1)
2437 return 0;
2438
2439 /* there is a prefix */
2440 if (args[0][0] == '@') {
2441 int target_pid = pcli_prefix_to_pid(args[0]);
2442
2443 if (target_pid == -1) {
2444 memprintf(errmsg, "Can't find the target PID matching the prefix '%s'\n", args[0]);
2445 return -1;
2446 }
2447
2448 /* if the prefix is alone, define a default target */
2449 if (argl == 1)
2450 s->pcli_next_pid = target_pid;
2451 else
2452 *next_pid = target_pid;
2453 return 1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002454 } else if (strcmp("prompt", args[0]) == 0) {
William Lallemanddc12c2e2018-12-13 09:05:46 +01002455 s->pcli_flags ^= PCLI_F_PROMPT;
William Lallemand5b80fa22018-12-11 16:10:54 +01002456 return argl; /* return the number of elements in the array */
William Lallemand5f610682018-12-11 16:10:55 +01002457
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002458 } else if (strcmp("quit", args[0]) == 0) {
William Lallemand5f610682018-12-11 16:10:55 +01002459 channel_shutr_now(&s->req);
2460 channel_shutw_now(&s->res);
2461 return argl; /* return the number of elements in the array */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002462 } else if (strcmp(args[0], "operator") == 0) {
William Lallemandb7ea1412018-12-13 09:05:47 +01002463 if (!pcli_has_level(s, ACCESS_LVL_OPER)) {
2464 memprintf(errmsg, "Permission denied!\n");
2465 return -1;
2466 }
2467 s->pcli_flags &= ~ACCESS_LVL_MASK;
2468 s->pcli_flags |= ACCESS_LVL_OPER;
2469 return argl;
2470
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002471 } else if (strcmp(args[0], "user") == 0) {
William Lallemandb7ea1412018-12-13 09:05:47 +01002472 if (!pcli_has_level(s, ACCESS_LVL_USER)) {
2473 memprintf(errmsg, "Permission denied!\n");
2474 return -1;
2475 }
2476 s->pcli_flags &= ~ACCESS_LVL_MASK;
2477 s->pcli_flags |= ACCESS_LVL_USER;
2478 return argl;
William Lallemand7267f782022-02-01 16:08:50 +01002479
2480 } else if (strcmp(args[0], "expert-mode") == 0) {
2481 if (!pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2482 memprintf(errmsg, "Permission denied!\n");
2483 return -1;
2484 }
2485
2486 s->pcli_flags &= ~ACCESS_EXPERT;
2487 if ((argl > 1) && (strcmp(args[1], "on") == 0))
2488 s->pcli_flags |= ACCESS_EXPERT;
2489 return argl;
2490
2491 } else if (strcmp(args[0], "experimental-mode") == 0) {
2492 if (!pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2493 memprintf(errmsg, "Permission denied!\n");
2494 return -1;
2495 }
2496 s->pcli_flags &= ~ACCESS_EXPERIMENTAL;
2497 if ((argl > 1) && (strcmp(args[1], "on") == 0))
2498 s->pcli_flags |= ACCESS_EXPERIMENTAL;
2499 return argl;
William Lallemand2a171912022-02-02 11:43:20 +01002500 } else if (strcmp(args[0], "mcli-debug-mode") == 0) {
2501 if (!pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2502 memprintf(errmsg, "Permission denied!\n");
2503 return -1;
2504 }
2505 s->pcli_flags &= ~ACCESS_MCLI_DEBUG;
2506 if ((argl > 1) && (strcmp(args[1], "on") == 0))
2507 s->pcli_flags |= ACCESS_MCLI_DEBUG;
2508 return argl;
William Lallemandbddd33a2018-12-11 16:10:53 +01002509 }
2510
2511 return 0;
2512}
2513
2514/*
2515 * Parse the CLI request:
2516 * - It does basically the same as the cli_io_handler, but as a proxy
2517 * - It can exec a command and strip non forwardable commands
William Lallemandcf62f7e2018-10-26 14:47:40 +02002518 *
2519 * Return:
William Lallemandbddd33a2018-12-11 16:10:53 +01002520 * - the number of characters to forward or
2521 * - 1 if there is an error or not enough data
William Lallemandcf62f7e2018-10-26 14:47:40 +02002522 */
William Lallemandbddd33a2018-12-11 16:10:53 +01002523int pcli_parse_request(struct stream *s, struct channel *req, char **errmsg, int *next_pid)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002524{
Willy Tarreaua4e4d662022-01-20 08:47:35 +01002525 char *str;
2526 char *end;
Willy Tarreauf14c7572021-03-13 10:59:23 +01002527 char *args[MAX_CLI_ARGS + 1]; /* +1 for storing a NULL */
William Lallemandbddd33a2018-12-11 16:10:53 +01002528 int argl; /* number of args */
2529 char *p;
2530 char *trim = NULL;
William Lallemandebf61802018-12-11 16:10:57 +01002531 char *payload = NULL;
William Lallemandbddd33a2018-12-11 16:10:53 +01002532 int wtrim = 0; /* number of words to trim */
2533 int reql = 0;
William Lallemandb7ea1412018-12-13 09:05:47 +01002534 int ret;
William Lallemandbddd33a2018-12-11 16:10:53 +01002535 int i = 0;
2536
Willy Tarreaua4e4d662022-01-20 08:47:35 +01002537 /* we cannot deal with a wrapping buffer, so let's take care of this
2538 * first.
2539 */
2540 if (b_head(&req->buf) + b_data(&req->buf) > b_wrap(&req->buf))
2541 b_slow_realign(&req->buf, trash.area, co_data(req));
2542
2543 str = (char *)ci_head(req);
2544 end = (char *)ci_stop(req);
2545
William Lallemandbddd33a2018-12-11 16:10:53 +01002546 p = str;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002547
William Lallemanddc12c2e2018-12-13 09:05:46 +01002548 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002549
2550 /* Looks for the end of one command */
2551 while (p+reql < end) {
2552 /* handle escaping */
2553 if (p[reql] == '\\') {
William Lallemand02c255e2020-06-18 18:45:04 +02002554 reql+=2;
William Lallemandebf61802018-12-11 16:10:57 +01002555 continue;
2556 }
2557 if (p[reql] == ';' || p[reql] == '\n') {
2558 /* found the end of the command */
2559 p[reql] = '\n';
2560 reql++;
2561 break;
2562 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002563 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002564 }
William Lallemandebf61802018-12-11 16:10:57 +01002565 } else {
2566 while (p+reql < end) {
2567 if (p[reql] == '\n') {
2568 /* found the end of the line */
2569 reql++;
2570 break;
2571 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002572 reql++;
William Lallemandbddd33a2018-12-11 16:10:53 +01002573 }
William Lallemandbddd33a2018-12-11 16:10:53 +01002574 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002575
William Lallemandbddd33a2018-12-11 16:10:53 +01002576 /* set end to first byte after the end of the command */
2577 end = p + reql;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002578
William Lallemandbddd33a2018-12-11 16:10:53 +01002579 /* there is no end to this command, need more to parse ! */
Willy Tarreau6cd93f52022-01-20 08:31:50 +01002580 if (!reql || *(end-1) != '\n') {
William Lallemandbddd33a2018-12-11 16:10:53 +01002581 return -1;
2582 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002583
William Lallemand3301f3e2018-12-13 09:05:48 +01002584 if (s->pcli_flags & PCLI_F_PAYLOAD) {
2585 if (reql == 1) /* last line of the payload */
2586 s->pcli_flags &= ~PCLI_F_PAYLOAD;
William Lallemandebf61802018-12-11 16:10:57 +01002587 return reql;
2588 }
2589
William Lallemandbddd33a2018-12-11 16:10:53 +01002590 *(end-1) = '\0';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002591
William Lallemandbddd33a2018-12-11 16:10:53 +01002592 /* splits the command in words */
Willy Tarreauf14c7572021-03-13 10:59:23 +01002593 while (i < MAX_CLI_ARGS && p < end) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002594 /* skip leading spaces/tabs */
2595 p += strspn(p, " \t");
2596 if (!*p)
William Lallemandcf62f7e2018-10-26 14:47:40 +02002597 break;
2598
William Lallemandbddd33a2018-12-11 16:10:53 +01002599 args[i] = p;
William Lallemandfe249c32020-06-18 18:03:57 +02002600 while (1) {
2601 p += strcspn(p, " \t\\");
2602 /* escaped chars using backlashes (\) */
2603 if (*p == '\\') {
2604 if (!*++p)
2605 break;
2606 if (!*++p)
2607 break;
2608 } else {
2609 break;
William Lallemandbddd33a2018-12-11 16:10:53 +01002610 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002611 }
William Lallemandfe249c32020-06-18 18:03:57 +02002612 *p++ = 0;
William Lallemandbddd33a2018-12-11 16:10:53 +01002613 i++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002614 }
2615
William Lallemandbddd33a2018-12-11 16:10:53 +01002616 argl = i;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002617
Willy Tarreauf14c7572021-03-13 10:59:23 +01002618 for (; i < MAX_CLI_ARGS + 1; i++)
William Lallemandbddd33a2018-12-11 16:10:53 +01002619 args[i] = NULL;
2620
2621 wtrim = pcli_find_and_exec_kw(s, args, argl, errmsg, next_pid);
2622
2623 /* End of words are ending by \0, we need to replace the \0s by spaces
William Lallemandfe618fb2022-02-02 14:07:08 +01002624 before forwarding them */
William Lallemandbddd33a2018-12-11 16:10:53 +01002625 p = str;
William Lallemand3301f3e2018-12-13 09:05:48 +01002626 while (p < end-1) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002627 if (*p == '\0')
2628 *p = ' ';
2629 p++;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002630 }
2631
William Lallemand3301f3e2018-12-13 09:05:48 +01002632 payload = strstr(str, PAYLOAD_PATTERN);
2633 if ((end - 1) == (payload + strlen(PAYLOAD_PATTERN))) {
2634 /* if the payload pattern is at the end */
2635 s->pcli_flags |= PCLI_F_PAYLOAD;
William Lallemand3301f3e2018-12-13 09:05:48 +01002636 }
2637
William Lallemandbddd33a2018-12-11 16:10:53 +01002638 *(end-1) = '\n';
William Lallemandcf62f7e2018-10-26 14:47:40 +02002639
William Lallemandbddd33a2018-12-11 16:10:53 +01002640 if (wtrim > 0) {
2641 trim = &args[wtrim][0];
2642 if (trim == NULL) /* if this was the last word in the table */
2643 trim = end;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002644
William Lallemandbddd33a2018-12-11 16:10:53 +01002645 b_del(&req->buf, trim - str);
2646
William Lallemandb7ea1412018-12-13 09:05:47 +01002647 ret = end - trim;
William Lallemandbddd33a2018-12-11 16:10:53 +01002648 } else if (wtrim < 0) {
2649 /* parsing error */
2650 return -1;
William Lallemandb7ea1412018-12-13 09:05:47 +01002651 } else {
2652 /* the whole string */
2653 ret = end - str;
William Lallemandbddd33a2018-12-11 16:10:53 +01002654 }
2655
William Lallemandb7ea1412018-12-13 09:05:47 +01002656 if (ret > 1) {
William Lallemand2a171912022-02-02 11:43:20 +01002657
2658 /* the mcli-debug-mode is only sent to the applet of the master */
2659 if ((s->pcli_flags & ACCESS_MCLI_DEBUG) && *next_pid <= 0) {
2660 ci_insert_line2(req, 0, "mcli-debug-mode on -", strlen("mcli-debug-mode on -"));
2661 ret += strlen("mcli-debug-mode on -") + 2;
2662 }
William Lallemand7267f782022-02-01 16:08:50 +01002663 if (s->pcli_flags & ACCESS_EXPERIMENTAL) {
2664 ci_insert_line2(req, 0, "experimental-mode on -", strlen("experimental-mode on -"));
2665 ret += strlen("experimental-mode on -") + 2;
2666 }
2667 if (s->pcli_flags & ACCESS_EXPERT) {
2668 ci_insert_line2(req, 0, "expert-mode on -", strlen("expert-mode on -"));
2669 ret += strlen("expert-mode on -") + 2;
2670 }
2671
William Lallemandb7ea1412018-12-13 09:05:47 +01002672 if (pcli_has_level(s, ACCESS_LVL_ADMIN)) {
2673 goto end;
2674 } else if (pcli_has_level(s, ACCESS_LVL_OPER)) {
William Lallemandad032882019-07-01 10:56:15 +02002675 ci_insert_line2(req, 0, "operator -", strlen("operator -"));
2676 ret += strlen("operator -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002677 } else if (pcli_has_level(s, ACCESS_LVL_USER)) {
William Lallemandad032882019-07-01 10:56:15 +02002678 ci_insert_line2(req, 0, "user -", strlen("user -"));
2679 ret += strlen("user -") + 2;
William Lallemandb7ea1412018-12-13 09:05:47 +01002680 }
2681 }
2682end:
William Lallemandbddd33a2018-12-11 16:10:53 +01002683
William Lallemandb7ea1412018-12-13 09:05:47 +01002684 return ret;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002685}
2686
2687int pcli_wait_for_request(struct stream *s, struct channel *req, int an_bit)
2688{
William Lallemandbddd33a2018-12-11 16:10:53 +01002689 int next_pid = -1;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002690 int to_forward;
William Lallemandbddd33a2018-12-11 16:10:53 +01002691 char *errmsg = NULL;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002692
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05002693 /* Don't read the next command if still processing the response of the
Christopher Fauletd98da3b2021-10-18 14:52:49 +02002694 * current one. Just wait. At this stage, errors should be handled by
2695 * the response analyzer.
2696 */
2697 if (s->res.analysers & AN_RES_WAIT_CLI)
2698 return 0;
2699
William Lallemandb7ea1412018-12-13 09:05:47 +01002700 if ((s->pcli_flags & ACCESS_LVL_MASK) == ACCESS_LVL_NONE)
2701 s->pcli_flags |= strm_li(s)->bind_conf->level & ACCESS_LVL_MASK;
2702
William Lallemand0a0512f2022-09-24 16:14:50 +02002703 /* stream that comes from the reload listener only responses the reload
2704 * status and quits */
2705 if (!(s->pcli_flags & PCLI_F_RELOAD)
2706 && strm_li(s)->bind_conf == mcli_reload_bind_conf)
2707 goto send_status;
2708
2709
William Lallemandcf62f7e2018-10-26 14:47:40 +02002710read_again:
2711 /* if the channel is closed for read, we won't receive any more data
2712 from the client, but we don't want to forward this close to the
2713 server */
2714 channel_dont_close(req);
2715
2716 /* We don't know yet to which server we will connect */
2717 channel_dont_connect(req);
2718
William Lallemandcf62f7e2018-10-26 14:47:40 +02002719 req->flags |= CF_READ_DONTWAIT;
2720
2721 /* need more data */
2722 if (!ci_data(req))
Christopher Faulet0f727da2022-01-18 08:44:23 +01002723 goto missing_data;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002724
2725 /* If there is data available for analysis, log the end of the idle time. */
2726 if (c_data(req) && s->logs.t_idle == -1)
2727 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2728
William Lallemandbddd33a2018-12-11 16:10:53 +01002729 to_forward = pcli_parse_request(s, req, &errmsg, &next_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002730 if (to_forward > 0) {
William Lallemandbddd33a2018-12-11 16:10:53 +01002731 int target_pid;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002732 /* enough data */
2733
William Lallemandcf62f7e2018-10-26 14:47:40 +02002734 /* forward only 1 command */
2735 channel_forward(req, to_forward);
William Lallemandebf61802018-12-11 16:10:57 +01002736
William Lallemanddc12c2e2018-12-13 09:05:46 +01002737 if (!(s->pcli_flags & PCLI_F_PAYLOAD)) {
William Lallemandebf61802018-12-11 16:10:57 +01002738 /* we send only 1 command per request, and we write close after it */
2739 channel_shutw_now(req);
2740 } else {
2741 pcli_write_prompt(s);
2742 }
2743
2744 s->res.flags |= CF_WAKE_ONCE; /* need to be called again */
William Lallemandcf62f7e2018-10-26 14:47:40 +02002745 s->res.analysers |= AN_RES_WAIT_CLI;
2746
William Lallemandebf61802018-12-11 16:10:57 +01002747 if (!(s->flags & SF_ASSIGNED)) {
2748 if (next_pid > -1)
2749 target_pid = next_pid;
2750 else
2751 target_pid = s->pcli_next_pid;
2752 /* we can connect now */
2753 s->target = pcli_pid_to_server(target_pid);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002754
William Lallemanddcbe7b92021-12-10 14:14:53 +01002755 if (!s->target)
2756 goto server_disconnect;
2757
William Lallemandebf61802018-12-11 16:10:57 +01002758 s->flags |= (SF_DIRECT | SF_ASSIGNED);
2759 channel_auto_connect(req);
2760 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002761
2762 } else if (to_forward == 0) {
William Lallemandcf62f7e2018-10-26 14:47:40 +02002763 /* we trimmed things but we might have other commands to consume */
William Lallemandbddd33a2018-12-11 16:10:53 +01002764 pcli_write_prompt(s);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002765 goto read_again;
Christopher Faulet0f727da2022-01-18 08:44:23 +01002766 } else if (to_forward == -1) {
2767 if (errmsg) {
2768 /* there was an error during the parsing */
2769 pcli_reply_and_close(s, errmsg);
2770 s->req.analysers &= ~AN_REQ_WAIT_CLI;
2771 return 0;
2772 }
2773 goto missing_data;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002774 }
2775
2776 return 0;
2777
2778send_help:
2779 b_reset(&req->buf);
2780 b_putblk(&req->buf, "help\n", 5);
2781 goto read_again;
William Lallemanddcbe7b92021-12-10 14:14:53 +01002782
William Lallemand0a0512f2022-09-24 16:14:50 +02002783send_status:
2784 s->pcli_flags |= PCLI_F_RELOAD;
2785 /* dont' use ci_putblk here because SHUTW could have been sent */
2786 b_reset(&req->buf);
2787 b_putblk(&req->buf, "_loadstatus;quit\n", 17);
2788 goto read_again;
2789
Christopher Faulet0f727da2022-01-18 08:44:23 +01002790missing_data:
2791 if (req->flags & CF_SHUTR) {
2792 /* There is no more request or a only a partial one and we
2793 * receive a close from the client, we can leave */
2794 channel_shutw_now(&s->res);
2795 s->req.analysers &= ~AN_REQ_WAIT_CLI;
2796 return 1;
2797 }
2798 else if (channel_full(req, global.tune.maxrewrite)) {
2799 /* buffer is full and we didn't catch the end of a command */
2800 goto send_help;
2801 }
2802 return 0;
2803
William Lallemanddcbe7b92021-12-10 14:14:53 +01002804server_disconnect:
2805 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
2806 return 0;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002807}
2808
2809int pcli_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
2810{
2811 struct proxy *fe = strm_fe(s);
2812 struct proxy *be = s->be;
2813
Christopher Faulete8cefac2022-03-07 18:20:21 +01002814 if ((rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
2815 ((rep->flags & CF_SHUTW) && (rep->to_forward || co_data(rep)))) {
William Lallemand6b7cd0a2018-11-06 17:37:11 +01002816 pcli_reply_and_close(s, "Can't connect to the target CLI!\n");
Christopher Fauletd98da3b2021-10-18 14:52:49 +02002817 s->req.analysers &= ~AN_REQ_WAIT_CLI;
William Lallemand6b7cd0a2018-11-06 17:37:11 +01002818 s->res.analysers &= ~AN_RES_WAIT_CLI;
2819 return 0;
2820 }
William Lallemandcf62f7e2018-10-26 14:47:40 +02002821 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2822 rep->flags |= CF_NEVER_WAIT;
2823
2824 /* don't forward the close */
2825 channel_dont_close(&s->res);
2826 channel_dont_close(&s->req);
2827
William Lallemanddc12c2e2018-12-13 09:05:46 +01002828 if (s->pcli_flags & PCLI_F_PAYLOAD) {
William Lallemandebf61802018-12-11 16:10:57 +01002829 s->res.analysers &= ~AN_RES_WAIT_CLI;
2830 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2831 return 0;
2832 }
2833
William Lallemandcf62f7e2018-10-26 14:47:40 +02002834 /* forward the data */
2835 if (ci_data(rep)) {
2836 c_adv(rep, ci_data(rep));
2837 return 0;
2838 }
2839
2840 if ((rep->flags & (CF_SHUTR|CF_READ_NULL))) {
2841 /* stream cleanup */
2842
William Lallemand2f4ce202018-10-26 14:47:47 +02002843 pcli_write_prompt(s);
2844
Willy Tarreaucb041662022-05-17 19:44:42 +02002845 s->scb->flags |= SC_FL_NOLINGER | SC_FL_NOHALF;
Willy Tarreauf61dd192022-05-27 09:00:19 +02002846 sc_shutr(s->scb);
2847 sc_shutw(s->scb);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002848
2849 /*
2850 * starting from there this the same code as
2851 * http_end_txn_clean_session().
2852 *
2853 * It allows to do frontend keepalive while reconnecting to a
2854 * new server for each request.
2855 */
2856
2857 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002858 HA_ATOMIC_DEC(&be->beconn);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002859 if (unlikely(s->srv_conn))
2860 sess_change_server(s, NULL);
2861 }
2862
2863 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2864 stream_process_counters(s);
2865
2866 /* don't count other requests' data */
2867 s->logs.bytes_in -= ci_data(&s->req);
2868 s->logs.bytes_out -= ci_data(&s->res);
2869
2870 /* we may need to know the position in the queue */
2871 pendconn_free(s);
2872
2873 /* let's do a final log if we need it */
2874 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
2875 !(s->flags & SF_MONITOR) &&
2876 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
2877 s->do_log(s);
2878 }
2879
2880 /* stop tracking content-based counters */
2881 stream_stop_content_counters(s);
2882 stream_update_time_stats(s);
2883
2884 s->logs.accept_date = date; /* user-visible date for logging */
2885 s->logs.tv_accept = now; /* corrected date for internal use */
2886 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
2887 s->logs.t_idle = -1;
2888 tv_zero(&s->logs.tv_request);
2889 s->logs.t_queue = -1;
2890 s->logs.t_connect = -1;
2891 s->logs.t_data = -1;
2892 s->logs.t_close = 0;
2893 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
2894 s->logs.srv_queue_pos = 0; /* we will get this number soon */
2895
2896 s->logs.bytes_in = s->req.total = ci_data(&s->req);
2897 s->logs.bytes_out = s->res.total = ci_data(&s->res);
2898
2899 stream_del_srv_conn(s);
2900 if (objt_server(s->target)) {
2901 if (s->flags & SF_CURR_SESS) {
2902 s->flags &= ~SF_CURR_SESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002903 HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002904 }
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002905 if (may_dequeue_tasks(__objt_server(s->target), be))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002906 process_srv_queue(__objt_server(s->target));
William Lallemandcf62f7e2018-10-26 14:47:40 +02002907 }
2908
2909 s->target = NULL;
2910
2911 /* only release our endpoint if we don't intend to reuse the
2912 * connection.
2913 */
Willy Tarreau462b9892022-05-18 18:06:53 +02002914 if (!sc_conn_ready(s->scb)) {
William Lallemandcf62f7e2018-10-26 14:47:40 +02002915 s->srv_conn = NULL;
Willy Tarreau19c65a92022-05-27 08:49:24 +02002916 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet50264b42022-03-30 19:39:30 +02002917 if (!s->conn_err_type)
2918 s->conn_err_type = STRM_ET_CONN_OTHER;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002919 if (s->srv_error)
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02002920 s->srv_error(s, s->scb);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002921 return 1;
2922 }
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02002923 se_fl_clr(s->scb->sedesc, ~SE_FL_DETACHED);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002924 }
2925
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02002926 sockaddr_free(&s->scb->dst);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002927
Willy Tarreau74568cf2022-05-27 09:03:30 +02002928 sc_set_state(s->scb, SC_ST_INI);
Willy Tarreaucb041662022-05-17 19:44:42 +02002929 s->scb->flags &= SC_FL_ISBACK | SC_FL_DONT_WAKE; /* we're in the context of process_stream */
Christopher Faulet22dc2482019-07-16 14:43:08 +02002930 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 +02002931 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);
Willy Tarreau03bd3952022-05-02 16:36:47 +02002932 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002933 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
2934 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Christopher Faulet909f3182022-03-29 15:42:09 +02002935 s->conn_retries = 0; /* used for logging too */
Christopher Fauletae024ce2022-03-29 19:02:31 +02002936 s->conn_exp = TICK_ETERNITY;
Christopher Faulet50264b42022-03-30 19:39:30 +02002937 s->conn_err_type = STRM_ET_NONE;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002938 /* reinitialise the current rule list pointer to NULL. We are sure that
2939 * any rulelist match the NULL pointer.
2940 */
2941 s->current_rule_list = NULL;
2942
2943 s->be = strm_fe(s);
2944 s->logs.logwait = strm_fe(s)->to_log;
2945 s->logs.level = 0;
2946 stream_del_srv_conn(s);
2947 s->target = NULL;
2948 /* re-init store persistence */
2949 s->store_count = 0;
2950 s->uniq_id = global.req_count++;
2951
2952 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
2953
2954 s->req.flags |= CF_WAKE_ONCE; /* need to be called again if there is some command left in the request */
2955
William Lallemandcf62f7e2018-10-26 14:47:40 +02002956 s->res.analysers &= ~AN_RES_WAIT_CLI;
2957
2958 /* We must trim any excess data from the response buffer, because we
2959 * may have blocked an invalid response from a server that we don't
Ilya Shipitsind4259502020-04-08 01:07:56 +05002960 * want to accidentally forward once we disable the analysers, nor do
William Lallemandcf62f7e2018-10-26 14:47:40 +02002961 * we want those data to come along with next response. A typical
2962 * example of such data would be from a buggy server responding to
2963 * a HEAD with some data, or sending more than the advertised
2964 * content-length.
2965 */
2966 if (unlikely(ci_data(&s->res)))
2967 b_set_data(&s->res.buf, co_data(&s->res));
2968
2969 /* Now we can realign the response buffer */
2970 c_realign_if_empty(&s->res);
2971
2972 s->req.rto = strm_fe(s)->timeout.client;
2973 s->req.wto = TICK_ETERNITY;
2974
2975 s->res.rto = TICK_ETERNITY;
2976 s->res.wto = strm_fe(s)->timeout.client;
2977
2978 s->req.rex = TICK_ETERNITY;
2979 s->req.wex = TICK_ETERNITY;
2980 s->req.analyse_exp = TICK_ETERNITY;
2981 s->res.rex = TICK_ETERNITY;
2982 s->res.wex = TICK_ETERNITY;
2983 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02002984 s->scb->hcto = TICK_ETERNITY;
William Lallemandcf62f7e2018-10-26 14:47:40 +02002985
2986 /* we're removing the analysers, we MUST re-enable events detection.
2987 * We don't enable close on the response channel since it's either
2988 * already closed, or in keep-alive with an idle connection handler.
2989 */
2990 channel_auto_read(&s->req);
2991 channel_auto_close(&s->req);
2992 channel_auto_read(&s->res);
2993
2994
2995 return 1;
2996 }
2997 return 0;
2998}
2999
William Lallemand8a022572018-10-26 14:47:35 +02003000/*
3001 * The mworker functions are used to initialize the CLI in the master process
3002 */
3003
William Lallemand309dc9a2018-10-26 14:47:45 +02003004 /*
3005 * Stop the mworker proxy
3006 */
3007void mworker_cli_proxy_stop()
3008{
William Lallemand550db6d2018-11-06 17:37:12 +01003009 if (mworker_proxy)
3010 stop_proxy(mworker_proxy);
William Lallemand309dc9a2018-10-26 14:47:45 +02003011}
3012
William Lallemand8a022572018-10-26 14:47:35 +02003013/*
3014 * Create the mworker CLI proxy
3015 */
3016int mworker_cli_proxy_create()
3017{
3018 struct mworker_proc *child;
William Lallemand744a0892018-11-22 16:46:51 +01003019 char *msg = NULL;
3020 char *errmsg = NULL;
William Lallemand8a022572018-10-26 14:47:35 +02003021
William Lallemandae787ba2021-07-29 15:13:22 +02003022 mworker_proxy = alloc_new_proxy("MASTER", PR_CAP_LISTEN|PR_CAP_INT, &errmsg);
William Lallemand8a022572018-10-26 14:47:35 +02003023 if (!mworker_proxy)
William Lallemandae787ba2021-07-29 15:13:22 +02003024 goto error_proxy;
William Lallemand8a022572018-10-26 14:47:35 +02003025
William Lallemandcf62f7e2018-10-26 14:47:40 +02003026 mworker_proxy->mode = PR_MODE_CLI;
William Lallemand8a022572018-10-26 14:47:35 +02003027 mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */
3028 mworker_proxy->timeout.client = 0; /* no timeout */
3029 mworker_proxy->conf.file = strdup("MASTER");
3030 mworker_proxy->conf.line = 0;
3031 mworker_proxy->accept = frontend_accept;
3032 mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE;
3033
3034 /* Does not init the default target the CLI applet, but must be done in
3035 * the request parsing code */
3036 mworker_proxy->default_target = NULL;
3037
William Lallemand8a022572018-10-26 14:47:35 +02003038 /* create all servers using the mworker_proc list */
3039 list_for_each_entry(child, &proc_list, list) {
William Lallemand8a022572018-10-26 14:47:35 +02003040 struct server *newsrv = NULL;
3041 struct sockaddr_storage *sk;
3042 int port1, port2, port;
3043 struct protocol *proto;
William Lallemand8a022572018-10-26 14:47:35 +02003044
William Lallemanda31b09e2020-01-14 15:25:02 +01003045 /* only the workers support the master CLI */
3046 if (!(child->options & PROC_O_TYPE_WORKER))
3047 continue;
3048
William Lallemand8a022572018-10-26 14:47:35 +02003049 newsrv = new_server(mworker_proxy);
3050 if (!newsrv)
William Lallemand744a0892018-11-22 16:46:51 +01003051 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02003052
3053 /* we don't know the new pid yet */
3054 if (child->pid == -1)
Willy Tarreaue8422bf2021-06-15 09:08:18 +02003055 memprintf(&msg, "cur-%d", 1);
William Lallemand8a022572018-10-26 14:47:35 +02003056 else
3057 memprintf(&msg, "old-%d", child->pid);
3058
3059 newsrv->next = mworker_proxy->srv;
3060 mworker_proxy->srv = newsrv;
3061 newsrv->conf.file = strdup(msg);
3062 newsrv->id = strdup(msg);
3063 newsrv->conf.line = 0;
3064
3065 memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
Willy Tarreau5fc93282020-09-16 18:25:03 +02003066 if ((sk = str2sa_range(msg, &port, &port1, &port2, NULL, &proto,
Willy Tarreaua93e5c72020-09-15 14:01:16 +02003067 &errmsg, NULL, NULL, PA_O_STREAM)) == 0) {
William Lallemand744a0892018-11-22 16:46:51 +01003068 goto error;
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01003069 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003070 ha_free(&msg);
William Lallemand8a022572018-10-26 14:47:35 +02003071
Willy Tarreau5fc93282020-09-16 18:25:03 +02003072 if (!proto->connect) {
William Lallemand744a0892018-11-22 16:46:51 +01003073 goto error;
William Lallemand8a022572018-10-26 14:47:35 +02003074 }
3075
3076 /* no port specified */
3077 newsrv->flags |= SRV_F_MAPPORTS;
3078 newsrv->addr = *sk;
William Lallemandcf62f7e2018-10-26 14:47:40 +02003079 /* don't let the server participate to load balancing */
3080 newsrv->iweight = 0;
3081 newsrv->uweight = 0;
William Lallemand8a022572018-10-26 14:47:35 +02003082 srv_lb_commit_status(newsrv);
William Lallemand291810d2018-10-26 14:47:38 +02003083
3084 child->srv = newsrv;
William Lallemand8a022572018-10-26 14:47:35 +02003085 }
William Lallemandae787ba2021-07-29 15:13:22 +02003086
3087 mworker_proxy->next = proxies_list;
3088 proxies_list = mworker_proxy;
3089
William Lallemand8a022572018-10-26 14:47:35 +02003090 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01003091
3092error:
William Lallemand744a0892018-11-22 16:46:51 +01003093
3094 list_for_each_entry(child, &proc_list, list) {
3095 free((char *)child->srv->conf.file); /* cast because of const char * */
3096 free(child->srv->id);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003097 ha_free(&child->srv);
William Lallemand744a0892018-11-22 16:46:51 +01003098 }
William Lallemandae787ba2021-07-29 15:13:22 +02003099 free_proxy(mworker_proxy);
William Lallemand744a0892018-11-22 16:46:51 +01003100 free(msg);
3101
William Lallemandae787ba2021-07-29 15:13:22 +02003102error_proxy:
3103 ha_alert("%s\n", errmsg);
3104 free(errmsg);
3105
William Lallemand744a0892018-11-22 16:46:51 +01003106 return -1;
William Lallemand8a022572018-10-26 14:47:35 +02003107}
Olivier Houchardf886e342017-04-05 22:24:59 +02003108
William Lallemandce83b4a2018-10-26 14:47:30 +02003109/*
William Lallemande7361152018-10-26 14:47:36 +02003110 * Create a new listener for the master CLI proxy
3111 */
William Lallemand21623b52022-09-24 15:51:27 +02003112struct bind_conf *mworker_cli_proxy_new_listener(char *line)
William Lallemande7361152018-10-26 14:47:36 +02003113{
3114 struct bind_conf *bind_conf;
3115 struct listener *l;
3116 char *err = NULL;
3117 char *args[MAX_LINE_ARGS + 1];
3118 int arg;
3119 int cur_arg;
3120
William Lallemand2e945c82019-11-25 09:58:37 +01003121 arg = 1;
William Lallemande7361152018-10-26 14:47:36 +02003122 args[0] = line;
3123
3124 /* args is a bind configuration with spaces replaced by commas */
3125 while (*line && arg < MAX_LINE_ARGS) {
3126
3127 if (*line == ',') {
3128 *line++ = '\0';
3129 while (*line == ',')
3130 line++;
William Lallemand2e945c82019-11-25 09:58:37 +01003131 args[arg++] = line;
William Lallemande7361152018-10-26 14:47:36 +02003132 }
3133 line++;
3134 }
3135
William Lallemand2e945c82019-11-25 09:58:37 +01003136 args[arg] = "\0";
William Lallemande7361152018-10-26 14:47:36 +02003137
3138 bind_conf = bind_conf_alloc(mworker_proxy, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01003139 if (!bind_conf)
3140 goto err;
William Lallemande7361152018-10-26 14:47:36 +02003141
3142 bind_conf->level &= ~ACCESS_LVL_MASK;
3143 bind_conf->level |= ACCESS_LVL_ADMIN;
Willy Tarreaue283ee62021-03-12 15:00:57 +01003144 bind_conf->level |= ACCESS_MASTER | ACCESS_MASTER_ONLY;
Willy Tarreaudcbd7632022-07-06 11:46:34 +02003145 bind_conf->bind_tgroup = 1; // bind to a single group in any case
William Lallemande7361152018-10-26 14:47:36 +02003146
3147 if (!str2listener(args[0], mworker_proxy, bind_conf, "master-socket", 0, &err)) {
3148 ha_alert("Cannot create the listener of the master CLI\n");
William Lallemand744a0892018-11-22 16:46:51 +01003149 goto err;
William Lallemande7361152018-10-26 14:47:36 +02003150 }
3151
3152 cur_arg = 1;
3153
3154 while (*args[cur_arg]) {
William Lallemande7361152018-10-26 14:47:36 +02003155 struct bind_kw *kw;
Willy Tarreau433b05f2021-03-12 10:14:07 +01003156 const char *best;
William Lallemande7361152018-10-26 14:47:36 +02003157
3158 kw = bind_find_kw(args[cur_arg]);
3159 if (kw) {
3160 if (!kw->parse) {
3161 memprintf(&err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
3162 args[0], args[1], args[cur_arg]);
3163 goto err;
3164 }
3165
Willy Tarreau4975d142021-03-13 11:00:33 +01003166 if (kw->parse(args, cur_arg, global.cli_fe, bind_conf, &err) != 0) {
William Lallemande7361152018-10-26 14:47:36 +02003167 if (err)
3168 memprintf(&err, "'%s %s' : '%s'", args[0], args[1], err);
3169 else
3170 memprintf(&err, "'%s %s' : error encountered while processing '%s'",
3171 args[0], args[1], args[cur_arg]);
3172 goto err;
3173 }
3174
3175 cur_arg += 1 + kw->skip;
3176 continue;
3177 }
3178
Willy Tarreau433b05f2021-03-12 10:14:07 +01003179 best = bind_find_best_kw(args[cur_arg]);
3180 if (best)
3181 memprintf(&err, "'%s %s' : unknown keyword '%s'. Did you mean '%s' maybe ?",
3182 args[0], args[1], args[cur_arg], best);
3183 else
3184 memprintf(&err, "'%s %s' : unknown keyword '%s'.",
3185 args[0], args[1], args[cur_arg]);
William Lallemande7361152018-10-26 14:47:36 +02003186 goto err;
3187 }
3188
3189
3190 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemande7361152018-10-26 14:47:36 +02003191 l->accept = session_accept_fd;
3192 l->default_target = mworker_proxy->default_target;
3193 /* don't make the peers subject to global limits and don't close it in the master */
Willy Tarreau18c20d22020-10-09 16:11:46 +02003194 l->options |= LI_O_UNLIMITED;
3195 l->rx.flags |= RX_F_MWORKER; /* we are keeping this FD in the master */
William Lallemande7361152018-10-26 14:47:36 +02003196 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01003197 global.maxsock++; /* for the listening socket */
William Lallemande7361152018-10-26 14:47:36 +02003198 }
Willy Tarreau18215cb2019-02-27 16:25:28 +01003199 global.maxsock += mworker_proxy->maxconn;
William Lallemande7361152018-10-26 14:47:36 +02003200
William Lallemand21623b52022-09-24 15:51:27 +02003201 return bind_conf;
William Lallemande7361152018-10-26 14:47:36 +02003202
3203err:
3204 ha_alert("%s\n", err);
William Lallemand744a0892018-11-22 16:46:51 +01003205 free(err);
3206 free(bind_conf);
William Lallemand21623b52022-09-24 15:51:27 +02003207 return NULL;
William Lallemande7361152018-10-26 14:47:36 +02003208
3209}
3210
3211/*
William Lallemandce83b4a2018-10-26 14:47:30 +02003212 * Create a new CLI socket using a socketpair for a worker process
3213 * <mworker_proc> is the process structure, and <proc> is the process number
3214 */
3215int mworker_cli_sockpair_new(struct mworker_proc *mworker_proc, int proc)
3216{
3217 struct bind_conf *bind_conf;
3218 struct listener *l;
3219 char *path = NULL;
3220 char *err = NULL;
3221
3222 /* master pipe to ensure the master is still alive */
3223 if (socketpair(AF_UNIX, SOCK_STREAM, 0, mworker_proc->ipc_fd) < 0) {
3224 ha_alert("Cannot create worker socketpair.\n");
3225 return -1;
3226 }
3227
3228 /* XXX: we might want to use a separate frontend at some point */
Willy Tarreau4975d142021-03-13 11:00:33 +01003229 if (!global.cli_fe) {
3230 if ((global.cli_fe = cli_alloc_fe("GLOBAL", "master-socket", 0)) == NULL) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003231 ha_alert("out of memory trying to allocate the stats frontend");
William Lallemand744a0892018-11-22 16:46:51 +01003232 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02003233 }
3234 }
3235
Willy Tarreau4975d142021-03-13 11:00:33 +01003236 bind_conf = bind_conf_alloc(global.cli_fe, "master-socket", 0, "", xprt_get(XPRT_RAW));
William Lallemand744a0892018-11-22 16:46:51 +01003237 if (!bind_conf)
3238 goto error;
3239
William Lallemandce83b4a2018-10-26 14:47:30 +02003240 bind_conf->level &= ~ACCESS_LVL_MASK;
3241 bind_conf->level |= ACCESS_LVL_ADMIN; /* TODO: need to lower the rights with a CLI keyword*/
William Lallemand2be557f2021-11-24 18:45:37 +01003242 bind_conf->level |= ACCESS_FD_LISTENERS;
Willy Tarreaudcbd7632022-07-06 11:46:34 +02003243 bind_conf->bind_tgroup = 1; // bind to a single group in any case
William Lallemandce83b4a2018-10-26 14:47:30 +02003244
William Lallemandce83b4a2018-10-26 14:47:30 +02003245 if (!memprintf(&path, "sockpair@%d", mworker_proc->ipc_fd[1])) {
3246 ha_alert("Cannot allocate listener.\n");
William Lallemand744a0892018-11-22 16:46:51 +01003247 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02003248 }
3249
Willy Tarreau4975d142021-03-13 11:00:33 +01003250 if (!str2listener(path, global.cli_fe, bind_conf, "master-socket", 0, &err)) {
Tim Duesterhus4cae3b22018-11-22 16:46:50 +01003251 free(path);
William Lallemandce83b4a2018-10-26 14:47:30 +02003252 ha_alert("Cannot create a CLI sockpair listener for process #%d\n", proc);
William Lallemand744a0892018-11-22 16:46:51 +01003253 goto error;
William Lallemandce83b4a2018-10-26 14:47:30 +02003254 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003255 ha_free(&path);
William Lallemandce83b4a2018-10-26 14:47:30 +02003256
3257 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
William Lallemandce83b4a2018-10-26 14:47:30 +02003258 l->accept = session_accept_fd;
Willy Tarreau4975d142021-03-13 11:00:33 +01003259 l->default_target = global.cli_fe->default_target;
William Lallemanda3372292018-11-16 16:57:22 +01003260 l->options |= (LI_O_UNLIMITED | LI_O_NOSTOP);
Willy Tarreau4781b152021-04-06 13:53:36 +02003261 HA_ATOMIC_INC(&unstoppable_jobs);
William Lallemandce83b4a2018-10-26 14:47:30 +02003262 /* it's a sockpair but we don't want to keep the fd in the master */
Willy Tarreau43046fa2020-09-01 15:41:59 +02003263 l->rx.flags &= ~RX_F_INHERITED;
William Lallemandce83b4a2018-10-26 14:47:30 +02003264 l->nice = -64; /* we want to boost priority for local stats */
Willy Tarreau18215cb2019-02-27 16:25:28 +01003265 global.maxsock++; /* for the listening socket */
William Lallemandce83b4a2018-10-26 14:47:30 +02003266 }
3267
3268 return 0;
William Lallemand744a0892018-11-22 16:46:51 +01003269
3270error:
3271 close(mworker_proc->ipc_fd[0]);
3272 close(mworker_proc->ipc_fd[1]);
3273 free(err);
3274
3275 return -1;
William Lallemandce83b4a2018-10-26 14:47:30 +02003276}
3277
William Lallemand74c24fb2016-11-21 17:18:36 +01003278static struct applet cli_applet = {
3279 .obj_type = OBJ_TYPE_APPLET,
3280 .name = "<CLI>", /* used for logging */
3281 .fct = cli_io_handler,
3282 .release = cli_release_handler,
3283};
3284
William Lallemand99e0bb92020-11-05 10:28:53 +01003285/* master CLI */
3286static struct applet mcli_applet = {
3287 .obj_type = OBJ_TYPE_APPLET,
3288 .name = "<MCLI>", /* used for logging */
3289 .fct = cli_io_handler,
3290 .release = cli_release_handler,
3291};
3292
Willy Tarreau0a739292016-11-22 20:21:23 +01003293/* register cli keywords */
3294static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003295 { { "help", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3296 { { "prompt", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3297 { { "quit", NULL }, NULL, cli_parse_simple, NULL, NULL, NULL, ACCESS_MASTER },
3298 { { "_getsocks", NULL }, NULL, _getsocks, NULL },
William Lallemandd9c28072022-02-02 11:23:58 +01003299 { { "expert-mode", NULL }, NULL, cli_parse_expert_experimental_mode, NULL, NULL, NULL, ACCESS_MASTER }, // not listed
3300 { { "experimental-mode", NULL }, NULL, cli_parse_expert_experimental_mode, NULL, NULL, NULL, ACCESS_MASTER }, // not listed
William Lallemand2a171912022-02-02 11:43:20 +01003301 { { "mcli-debug-mode", NULL }, NULL, cli_parse_expert_experimental_mode, NULL, NULL, NULL, ACCESS_MASTER_ONLY }, // not listed
Erwan Le Goas54966df2022-09-14 17:24:22 +02003302 { { "set", "anon", NULL }, "set anon <setting> [value] : change the anonymized mode setting", cli_parse_set_anon, NULL, NULL },
Erwan Le Goasd7869312022-09-29 10:36:11 +02003303 { { "set", "anon", "global-key", NULL }, "set anon global-key <value> : change the global anonymizing key", cli_parse_set_global_key, NULL, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003304 { { "set", "maxconn", "global", NULL }, "set maxconn global <value> : change the per-process maxconn setting", cli_parse_set_maxconn_global, NULL },
3305 { { "set", "rate-limit", NULL }, "set rate-limit <setting> <value> : change a rate limiting value", cli_parse_set_ratelimit, NULL },
3306 { { "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 },
3307 { { "set", "timeout", NULL }, "set timeout [cli] <delay> : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Erwan Le Goas54966df2022-09-14 17:24:22 +02003308 { { "show", "anon", NULL }, "show anon : display the current state of anonymized mode", cli_parse_show_anon, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003309 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
3310 { { "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 },
3311 { { "show", "cli", "level", NULL }, "show cli level : display the level of the current CLI session", cli_parse_show_lvl, NULL, NULL, NULL, ACCESS_MASTER},
3312 { { "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 },
Willy Tarreau9a7fa902022-07-15 16:51:16 +02003313 { { "show", "activity", NULL }, "show activity [-1|0|thread_num] : show per-thread activity stats (for support/developers)", cli_parse_show_activity, cli_io_handler_show_activity, NULL },
William Lallemand740629e2021-12-14 15:22:29 +01003314 { { "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 +02003315 { { "operator", NULL }, "operator : lower the level of the current CLI session to operator", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER},
3316 { { "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 +01003317 {{},}
3318}};
3319
Willy Tarreau0108d902018-11-25 19:14:37 +01003320INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3321
William Lallemand74c24fb2016-11-21 17:18:36 +01003322static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau4975d142021-03-13 11:00:33 +01003323 { CFG_GLOBAL, "stats", cli_parse_global },
William Lallemand74c24fb2016-11-21 17:18:36 +01003324 { 0, NULL, NULL },
3325}};
3326
Willy Tarreau0108d902018-11-25 19:14:37 +01003327INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
3328
William Lallemand74c24fb2016-11-21 17:18:36 +01003329static struct bind_kw_list bind_kws = { "STAT", { }, {
William Lallemandf6975e92017-05-26 17:42:10 +02003330 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
3331 { "expose-fd", bind_parse_expose_fd, 1 }, /* set the unix socket expose fd rights */
Andjelko Iharosc4df59e2017-07-20 11:59:48 +02003332 { "severity-output", bind_parse_severity_output, 1 }, /* set the severity output format */
William Lallemand74c24fb2016-11-21 17:18:36 +01003333 { NULL, NULL, 0 },
3334}};
3335
Willy Tarreau0108d902018-11-25 19:14:37 +01003336INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01003337
3338/*
3339 * Local variables:
3340 * c-indent-level: 8
3341 * c-basic-offset: 8
3342 * End:
3343 */