blob: e6e9b45a855b52aa2873d416b6078d07d545f779 [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
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/ticks.h>
35#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38#include <common/base64.h>
39
40#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010042#include <types/global.h>
43#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045
46#include <proto/backend.h>
47#include <proto/channel.h>
48#include <proto/checks.h>
49#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010050#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/fd.h>
52#include <proto/freq_ctr.h>
53#include <proto/frontend.h>
54#include <proto/log.h>
55#include <proto/pattern.h>
56#include <proto/pipe.h>
57#include <proto/listener.h>
58#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010059#include <proto/proto_uxst.h>
60#include <proto/proxy.h>
61#include <proto/sample.h>
62#include <proto/session.h>
63#include <proto/stream.h>
64#include <proto/server.h>
65#include <proto/raw_sock.h>
66#include <proto/stream_interface.h>
67#include <proto/task.h>
68
William Lallemand74c24fb2016-11-21 17:18:36 +010069static struct applet cli_applet;
70
71static const char stats_sock_usage_msg[] =
72 "Unknown command. Please enter one of the following commands only :\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +010076 "";
77
78static const char stats_permission_denied_msg[] =
79 "Permission denied\n"
80 "";
81
82
83static char *dynamic_usage_msg = NULL;
84
85/* List head of cli keywords */
86static struct cli_kw_list cli_keywords = {
87 .list = LIST_HEAD_INIT(cli_keywords.list)
88};
89
90extern const char *stat_status_codes[];
91
92char *cli_gen_usage_msg()
93{
94 struct cli_kw_list *kw_list;
95 struct cli_kw *kw;
96 struct chunk *tmp = get_trash_chunk();
97 struct chunk out;
98
99 free(dynamic_usage_msg);
100 dynamic_usage_msg = NULL;
101
102 if (LIST_ISEMPTY(&cli_keywords.list))
103 return NULL;
104
105 chunk_reset(tmp);
106 chunk_strcat(tmp, stats_sock_usage_msg);
107 list_for_each_entry(kw_list, &cli_keywords.list, list) {
108 kw = &kw_list->kw[0];
109 while (kw->usage) {
110 chunk_appendf(tmp, " %s\n", kw->usage);
111 kw++;
112 }
113 }
114 chunk_init(&out, NULL, 0);
115 chunk_dup(&out, tmp);
116 dynamic_usage_msg = out.str;
117 return dynamic_usage_msg;
118}
119
120struct cli_kw* cli_find_kw(char **args)
121{
122 struct cli_kw_list *kw_list;
123 struct cli_kw *kw;/* current cli_kw */
124 char **tmp_args;
125 const char **tmp_str_kw;
126 int found = 0;
127
128 if (LIST_ISEMPTY(&cli_keywords.list))
129 return NULL;
130
131 list_for_each_entry(kw_list, &cli_keywords.list, list) {
132 kw = &kw_list->kw[0];
133 while (*kw->str_kw) {
134 tmp_args = args;
135 tmp_str_kw = kw->str_kw;
136 while (*tmp_str_kw) {
137 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
138 found = 1;
139 } else {
140 found = 0;
141 break;
142 }
143 tmp_args++;
144 tmp_str_kw++;
145 }
146 if (found)
147 return (kw);
148 kw++;
149 }
150 }
151 return NULL;
152}
153
154void cli_register_kw(struct cli_kw_list *kw_list)
155{
156 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
157}
158
159
160/* allocate a new stats frontend named <name>, and return it
161 * (or NULL in case of lack of memory).
162 */
163static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
164{
165 struct proxy *fe;
166
167 fe = calloc(1, sizeof(*fe));
168 if (!fe)
169 return NULL;
170
171 init_new_proxy(fe);
172 fe->next = proxy;
173 proxy = fe;
174 fe->last_change = now.tv_sec;
175 fe->id = strdup("GLOBAL");
176 fe->cap = PR_CAP_FE;
177 fe->maxconn = 10; /* default to 10 concurrent connections */
178 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
179 fe->conf.file = strdup(file);
180 fe->conf.line = line;
181 fe->accept = frontend_accept;
182 fe->default_target = &cli_applet.obj_type;
183
184 /* the stats frontend is the only one able to assign ID #0 */
185 fe->conf.id.key = fe->uuid = 0;
186 eb32_insert(&used_proxy_id, &fe->conf.id);
187 return fe;
188}
189
190/* This function parses a "stats" statement in the "global" section. It returns
191 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
192 * error message into the <err> buffer which will be preallocated. The trailing
193 * '\n' must not be written. The function must be called with <args> pointing to
194 * the first word after "stats".
195 */
196static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
197 struct proxy *defpx, const char *file, int line,
198 char **err)
199{
200 struct bind_conf *bind_conf;
201 struct listener *l;
202
203 if (!strcmp(args[1], "socket")) {
204 int cur_arg;
205
206 if (*args[2] == 0) {
207 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
208 return -1;
209 }
210
211 if (!global.stats_fe) {
212 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
213 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
214 return -1;
215 }
216 }
217
218 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
219 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
220
221 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
222 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
223 file, line, args[0], args[1], err && *err ? *err : "error");
224 return -1;
225 }
226
227 cur_arg = 3;
228 while (*args[cur_arg]) {
229 static int bind_dumped;
230 struct bind_kw *kw;
231
232 kw = bind_find_kw(args[cur_arg]);
233 if (kw) {
234 if (!kw->parse) {
235 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
236 args[0], args[1], args[cur_arg]);
237 return -1;
238 }
239
240 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
241 if (err && *err)
242 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
243 else
244 memprintf(err, "'%s %s' : error encountered while processing '%s'",
245 args[0], args[1], args[cur_arg]);
246 return -1;
247 }
248
249 cur_arg += 1 + kw->skip;
250 continue;
251 }
252
253 if (!bind_dumped) {
254 bind_dump_kws(err);
255 indent_msg(err, 4);
256 bind_dumped = 1;
257 }
258
259 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
260 args[0], args[1], args[cur_arg],
261 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
262 return -1;
263 }
264
265 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
266 l->maxconn = global.stats_fe->maxconn;
267 l->backlog = global.stats_fe->backlog;
268 l->accept = session_accept_fd;
269 l->handler = process_stream;
270 l->default_target = global.stats_fe->default_target;
271 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
272 l->nice = -64; /* we want to boost priority for local stats */
273 global.maxsock += l->maxconn;
274 }
275 }
276 else if (!strcmp(args[1], "timeout")) {
277 unsigned timeout;
278 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
279
280 if (res) {
281 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
282 return -1;
283 }
284
285 if (!timeout) {
286 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
287 return -1;
288 }
289 if (!global.stats_fe) {
290 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
291 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
292 return -1;
293 }
294 }
295 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
296 }
297 else if (!strcmp(args[1], "maxconn")) {
298 int maxconn = atol(args[2]);
299
300 if (maxconn <= 0) {
301 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
302 return -1;
303 }
304
305 if (!global.stats_fe) {
306 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
307 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
308 return -1;
309 }
310 }
311 global.stats_fe->maxconn = maxconn;
312 }
313 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
314 int cur_arg = 2;
315 unsigned long set = 0;
316
317 if (!global.stats_fe) {
318 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
319 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
320 return -1;
321 }
322 }
323
324 while (*args[cur_arg]) {
325 unsigned int low, high;
326
327 if (strcmp(args[cur_arg], "all") == 0) {
328 set = 0;
329 break;
330 }
331 else if (strcmp(args[cur_arg], "odd") == 0) {
332 set |= ~0UL/3UL; /* 0x555....555 */
333 }
334 else if (strcmp(args[cur_arg], "even") == 0) {
335 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
336 }
337 else if (isdigit((int)*args[cur_arg])) {
338 char *dash = strchr(args[cur_arg], '-');
339
340 low = high = str2uic(args[cur_arg]);
341 if (dash)
342 high = str2uic(dash + 1);
343
344 if (high < low) {
345 unsigned int swap = low;
346 low = high;
347 high = swap;
348 }
349
350 if (low < 1 || high > LONGBITS) {
351 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
352 args[0], args[1], LONGBITS);
353 return -1;
354 }
355 while (low <= high)
356 set |= 1UL << (low++ - 1);
357 }
358 else {
359 memprintf(err,
360 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
361 args[0], args[1], LONGBITS);
362 return -1;
363 }
364 cur_arg++;
365 }
366 global.stats_fe->bind_proc = set;
367 }
368 else {
369 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
370 return -1;
371 }
372 return 0;
373}
374
Willy Tarreaude57a572016-11-23 17:01:39 +0100375/* Verifies that the CLI at least has a level at least as high as <level>
376 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
377 * failure, an error message is prepared and the appctx's state is adjusted
378 * to print it so that a return 1 is enough to abort any processing.
379 */
380int cli_has_level(struct appctx *appctx, int level)
381{
382 struct stream_interface *si = appctx->owner;
383 struct stream *s = si_strm(si);
384
385 if (strm_li(s)->bind_conf->level < level) {
386 appctx->ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100387 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaude57a572016-11-23 17:01:39 +0100388 return 0;
389 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100390 return 1;
391}
392
William Lallemand74c24fb2016-11-21 17:18:36 +0100393
Willy Tarreau41908562016-11-24 16:23:38 +0100394/* Processes the CLI interpreter on the stats socket. This function is called
395 * from the CLI's IO handler running in an appctx context. The function returns 1
396 * if the request was understood, otherwise zero. It is called with appctx->st0
397 * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
398 * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
399 * have its own I/O handler called again. Most of the time, parsers will only
400 * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
Willy Tarreaueaffde32016-12-16 17:59:25 +0100401 * If a keyword parser is NULL and an I/O handler is declared, the I/O handler
402 * will automatically be used.
William Lallemand74c24fb2016-11-21 17:18:36 +0100403 */
Willy Tarreau41908562016-11-24 16:23:38 +0100404static int cli_parse_request(struct appctx *appctx, char *line)
William Lallemand74c24fb2016-11-21 17:18:36 +0100405{
William Lallemand74c24fb2016-11-21 17:18:36 +0100406 char *args[MAX_STATS_ARGS + 1];
407 struct cli_kw *kw;
408 int arg;
409 int i, j;
410
411 while (isspace((unsigned char)*line))
412 line++;
413
414 arg = 0;
415 args[arg] = line;
416
417 while (*line && arg < MAX_STATS_ARGS) {
418 if (*line == '\\') {
419 line++;
420 if (*line == '\0')
421 break;
422 }
423 else if (isspace((unsigned char)*line)) {
424 *line++ = '\0';
425
426 while (isspace((unsigned char)*line))
427 line++;
428
429 args[++arg] = line;
430 continue;
431 }
432
433 line++;
434 }
435
436 while (++arg <= MAX_STATS_ARGS)
437 args[arg] = line;
438
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100439 /* unescape '\' */
William Lallemand74c24fb2016-11-21 17:18:36 +0100440 arg = 0;
441 while (*args[arg] != '\0') {
442 j = 0;
443 for (i=0; args[arg][i] != '\0'; i++) {
Dragan Dosena1c35ab2016-11-24 11:33:12 +0100444 if (args[arg][i] == '\\') {
445 if (args[arg][i+1] == '\\')
446 i++;
447 else
448 continue;
449 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100450 args[arg][j] = args[arg][i];
451 j++;
452 }
453 args[arg][j] = '\0';
454 arg++;
455 }
456
Willy Tarreau41908562016-11-24 16:23:38 +0100457 appctx->st2 = 0;
Willy Tarreaua2d58722016-12-16 12:37:03 +0100458 memset(&appctx->ctx.cli, 0, sizeof(appctx->ctx.cli));
Willy Tarreau41908562016-11-24 16:23:38 +0100459
460 kw = cli_find_kw(args);
Willy Tarreaueaffde32016-12-16 17:59:25 +0100461 if (!kw)
Willy Tarreau41908562016-11-24 16:23:38 +0100462 return 0;
463
464 appctx->io_handler = kw->io_handler;
Willy Tarreaueaffde32016-12-16 17:59:25 +0100465 if ((!kw->parse || kw->parse(args, appctx, kw->private) == 0) && appctx->io_handler) {
Willy Tarreau41908562016-11-24 16:23:38 +0100466 appctx->st0 = CLI_ST_CALLBACK;
467 appctx->io_release = kw->io_release;
William Lallemand74c24fb2016-11-21 17:18:36 +0100468 }
Willy Tarreau41908562016-11-24 16:23:38 +0100469 return 1;
William Lallemand74c24fb2016-11-21 17:18:36 +0100470}
471
472/* This I/O handler runs as an applet embedded in a stream interface. It is
473 * used to processes I/O from/to the stats unix socket. The system relies on a
474 * state machine handling requests and various responses. We read a request,
475 * then we process it and send the response, and we possibly display a prompt.
476 * Then we can read again. The state is stored in appctx->st0 and is one of the
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100477 * CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
William Lallemand74c24fb2016-11-21 17:18:36 +0100478 * or not.
479 */
480static void cli_io_handler(struct appctx *appctx)
481{
482 struct stream_interface *si = appctx->owner;
483 struct channel *req = si_oc(si);
484 struct channel *res = si_ic(si);
485 int reql;
486 int len;
487
488 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
489 goto out;
490
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100491 /* Check if the input buffer is avalaible. */
492 if (res->buf->size == 0) {
493 si_applet_cant_put(si);
494 goto out;
495 }
496
William Lallemand74c24fb2016-11-21 17:18:36 +0100497 while (1) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100498 if (appctx->st0 == CLI_ST_INIT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100499 /* Stats output not initialized yet */
500 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100501 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100502 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100503 else if (appctx->st0 == CLI_ST_END) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100504 /* Let's close for real now. We just close the request
505 * side, the conditions below will complete if needed.
506 */
507 si_shutw(si);
508 break;
509 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100510 else if (appctx->st0 == CLI_ST_GETREQ) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100511 /* ensure we have some output room left in the event we
512 * would want to return some info right after parsing.
513 */
514 if (buffer_almost_full(si_ib(si))) {
515 si_applet_cant_put(si);
516 break;
517 }
518
519 reql = bo_getline(si_oc(si), trash.str, trash.size);
520 if (reql <= 0) { /* closed or EOL not found */
521 if (reql == 0)
522 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100523 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100524 continue;
525 }
526
527 /* seek for a possible unescaped semi-colon. If we find
528 * one, we replace it with an LF and skip only this part.
529 */
530 for (len = 0; len < reql; len++) {
531 if (trash.str[len] == '\\') {
532 len++;
533 continue;
534 }
535 if (trash.str[len] == ';') {
536 trash.str[len] = '\n';
537 reql = len + 1;
538 break;
539 }
540 }
541
542 /* now it is time to check that we have a full line,
543 * remove the trailing \n and possibly \r, then cut the
544 * line.
545 */
546 len = reql - 1;
547 if (trash.str[len] != '\n') {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100548 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100549 continue;
550 }
551
552 if (len && trash.str[len-1] == '\r')
553 len--;
554
555 trash.str[len] = '\0';
556
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100557 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100558 if (len) {
559 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100560 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100561 continue;
562 }
563 else if (strcmp(trash.str, "prompt") == 0)
564 appctx->st1 = !appctx->st1;
565 else if (strcmp(trash.str, "help") == 0 ||
Willy Tarreau41908562016-11-24 16:23:38 +0100566 !cli_parse_request(appctx, trash.str)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100567 cli_gen_usage_msg();
568 if (dynamic_usage_msg)
569 appctx->ctx.cli.msg = dynamic_usage_msg;
570 else
571 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100572 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100573 }
574 /* NB: stats_sock_parse_request() may have put
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100575 * another CLI_ST_O_* into appctx->st0.
William Lallemand74c24fb2016-11-21 17:18:36 +0100576 */
577 }
578 else if (!appctx->st1) {
579 /* if prompt is disabled, print help on empty lines,
580 * so that the user at least knows how to enable
581 * prompt and find help.
582 */
583 cli_gen_usage_msg();
584 if (dynamic_usage_msg)
585 appctx->ctx.cli.msg = dynamic_usage_msg;
586 else
587 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100588 appctx->st0 = CLI_ST_PRINT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100589 }
590
591 /* re-adjust req buffer */
592 bo_skip(si_oc(si), reql);
593 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
594 }
595 else { /* output functions */
596 switch (appctx->st0) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100597 case CLI_ST_PROMPT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100598 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100599 case CLI_ST_PRINT:
William Lallemand74c24fb2016-11-21 17:18:36 +0100600 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100601 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100602 else
603 si_applet_cant_put(si);
604 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100605 case CLI_ST_PRINT_FREE:
William Lallemand74c24fb2016-11-21 17:18:36 +0100606 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
607 free(appctx->ctx.cli.err);
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100608 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100609 }
610 else
611 si_applet_cant_put(si);
612 break;
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100613 case CLI_ST_CALLBACK: /* use custom pointer */
William Lallemand74c24fb2016-11-21 17:18:36 +0100614 if (appctx->io_handler)
615 if (appctx->io_handler(appctx)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100616 appctx->st0 = CLI_ST_PROMPT;
William Lallemand74c24fb2016-11-21 17:18:36 +0100617 if (appctx->io_release) {
618 appctx->io_release(appctx);
619 appctx->io_release = NULL;
620 }
621 }
622 break;
623 default: /* abnormal state */
624 si->flags |= SI_FL_ERR;
625 break;
626 }
627
628 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100629 if (appctx->st0 == CLI_ST_PROMPT) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100630 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100631 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100632 else
633 si_applet_cant_put(si);
634 }
635
636 /* If the output functions are still there, it means they require more room. */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100637 if (appctx->st0 >= CLI_ST_OUTPUT)
William Lallemand74c24fb2016-11-21 17:18:36 +0100638 break;
639
640 /* Now we close the output if one of the writers did so,
641 * or if we're not in interactive mode and the request
642 * buffer is empty. This still allows pipelined requests
643 * to be sent in non-interactive mode.
644 */
645 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100646 appctx->st0 = CLI_ST_END;
William Lallemand74c24fb2016-11-21 17:18:36 +0100647 continue;
648 }
649
650 /* switch state back to GETREQ to read next requests */
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100651 appctx->st0 = CLI_ST_GETREQ;
William Lallemand74c24fb2016-11-21 17:18:36 +0100652 }
653 }
654
655 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
656 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
657 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
658 /* Other side has closed, let's abort if we have no more processing to do
659 * and nothing more to consume. This is comparable to a broken pipe, so
660 * we forward the close to the request side so that it flows upstream to
661 * the client.
662 */
663 si_shutw(si);
664 }
665
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100666 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < CLI_ST_OUTPUT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100667 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
668 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
669 /* We have no more processing to do, and nothing more to send, and
670 * the client side has closed. So we'll forward this state downstream
671 * on the response buffer.
672 */
673 si_shutr(si);
674 res->flags |= CF_READ_NULL;
675 }
676
677 out:
678 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
679 __FUNCTION__, __LINE__,
680 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
681}
682
William Lallemand74c24fb2016-11-21 17:18:36 +0100683/* This is called when the stream interface is closed. For instance, upon an
684 * external abort, we won't call the i/o handler anymore so we may need to
685 * remove back references to the stream currently being dumped.
686 */
687static void cli_release_handler(struct appctx *appctx)
688{
689 if (appctx->io_release) {
690 appctx->io_release(appctx);
691 appctx->io_release = NULL;
692 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100693 else if (appctx->st0 == CLI_ST_PRINT_FREE) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100694 free(appctx->ctx.cli.err);
695 appctx->ctx.cli.err = NULL;
696 }
William Lallemand74c24fb2016-11-21 17:18:36 +0100697}
698
699/* This function dumps all environmnent variables to the buffer. It returns 0
700 * if the output buffer is full and it needs to be called again, otherwise
Willy Tarreauf6710f82016-12-16 17:45:44 +0100701 * non-zero. Dumps only one entry if st2 == STAT_ST_END. It uses cli.p0 as the
702 * pointer to the current variable.
William Lallemand74c24fb2016-11-21 17:18:36 +0100703 */
Willy Tarreau0a739292016-11-22 20:21:23 +0100704static int cli_io_handler_show_env(struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +0100705{
Willy Tarreau0a739292016-11-22 20:21:23 +0100706 struct stream_interface *si = appctx->owner;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100707 char **var = appctx->ctx.cli.p0;
William Lallemand74c24fb2016-11-21 17:18:36 +0100708
709 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
710 return 1;
711
712 chunk_reset(&trash);
713
714 /* we have two inner loops here, one for the proxy, the other one for
715 * the buffer.
716 */
Willy Tarreauf6710f82016-12-16 17:45:44 +0100717 while (*var) {
718 chunk_printf(&trash, "%s\n", *var);
William Lallemand74c24fb2016-11-21 17:18:36 +0100719
720 if (bi_putchk(si_ic(si), &trash) == -1) {
721 si_applet_cant_put(si);
722 return 0;
723 }
724 if (appctx->st2 == STAT_ST_END)
725 break;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100726 var++;
727 appctx->ctx.cli.p0 = var;
William Lallemand74c24fb2016-11-21 17:18:36 +0100728 }
729
730 /* dump complete */
731 return 1;
732}
733
William Lallemandeceddf72016-12-15 18:06:44 +0100734/*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100735 * CLI IO handler for `show cli sockets`.
736 * Uses ctx.cli.p0 to store the restart pointer.
William Lallemandeceddf72016-12-15 18:06:44 +0100737 */
738static int cli_io_handler_show_cli_sock(struct appctx *appctx)
739{
740 struct bind_conf *bind_conf;
741 struct stream_interface *si = appctx->owner;
742
743 chunk_reset(&trash);
744
745 switch (appctx->st2) {
746 case STAT_ST_INIT:
747 chunk_printf(&trash, "# socket lvl processes\n");
748 if (bi_putchk(si_ic(si), &trash) == -1) {
749 si_applet_cant_put(si);
750 return 0;
751 }
William Lallemandeceddf72016-12-15 18:06:44 +0100752 appctx->st2 = STAT_ST_LIST;
753
754 case STAT_ST_LIST:
755 if (global.stats_fe) {
756 list_for_each_entry(bind_conf, &global.stats_fe->conf.bind, by_fe) {
757 struct listener *l;
758
759 /*
Willy Tarreau3af9d832016-12-16 12:58:09 +0100760 * get the latest dumped node in appctx->ctx.cli.p0
William Lallemandeceddf72016-12-15 18:06:44 +0100761 * if the current node is the first of the list
762 */
763
Willy Tarreau3af9d832016-12-16 12:58:09 +0100764 if (appctx->ctx.cli.p0 &&
765 &bind_conf->by_fe == (&global.stats_fe->conf.bind)->n) {
William Lallemandeceddf72016-12-15 18:06:44 +0100766 /* change the current node to the latest dumped and continue the loop */
Willy Tarreau3af9d832016-12-16 12:58:09 +0100767 bind_conf = LIST_ELEM(appctx->ctx.cli.p0, typeof(bind_conf), by_fe);
William Lallemandeceddf72016-12-15 18:06:44 +0100768 continue;
769 }
770
771 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
772
773 char addr[46];
774 char port[6];
775
776 if (l->addr.ss_family == AF_UNIX) {
777 const struct sockaddr_un *un;
778
779 un = (struct sockaddr_un *)&l->addr;
780 chunk_appendf(&trash, "%s ", un->sun_path);
781 } else if (l->addr.ss_family == AF_INET) {
782 addr_to_str(&l->addr, addr, sizeof(addr));
783 port_to_str(&l->addr, port, sizeof(port));
784 chunk_appendf(&trash, "%s:%s ", addr, port);
785 } else if (l->addr.ss_family == AF_INET6) {
786 addr_to_str(&l->addr, addr, sizeof(addr));
787 port_to_str(&l->addr, port, sizeof(port));
788 chunk_appendf(&trash, "[%s]:%s ", addr, port);
789 } else
790 continue;
791
792 if (bind_conf->level == ACCESS_LVL_USER)
793 chunk_appendf(&trash, "user ");
794 else if (bind_conf->level == ACCESS_LVL_OPER)
795 chunk_appendf(&trash, "operator ");
796 else if (bind_conf->level == ACCESS_LVL_ADMIN)
797 chunk_appendf(&trash, "admin ");
798 else
799 chunk_appendf(&trash, " ");
800
801 if (bind_conf->bind_proc != 0) {
802 int pos;
Willy Tarreau20c5e522016-12-16 12:50:55 +0100803 for (pos = 0; pos < 8 * sizeof(bind_conf->bind_proc); pos++) {
Willy Tarreau4305ac72016-12-16 12:56:31 +0100804 if (bind_conf->bind_proc & (1UL << pos)) {
William Lallemandeceddf72016-12-15 18:06:44 +0100805 chunk_appendf(&trash, "%d,", pos+1);
806 }
807 }
808 /* replace the latest comma by a newline */
809 trash.str[trash.len-1] = '\n';
810
811 } else {
812 chunk_appendf(&trash, "all\n");
813 }
814
815 if (bi_putchk(si_ic(si), &trash) == -1) {
816 si_applet_cant_put(si);
817 return 0;
818 }
819 }
Willy Tarreau3af9d832016-12-16 12:58:09 +0100820 appctx->ctx.cli.p0 = &bind_conf->by_fe; /* store the latest list node dumped */
William Lallemandeceddf72016-12-15 18:06:44 +0100821 }
822 }
823 default:
824 appctx->st2 = STAT_ST_FIN;
825 return 1;
826 }
827}
828
829
Willy Tarreau0a739292016-11-22 20:21:23 +0100830/* parse a "show env" CLI request. Returns 0 if it needs to continue, 1 if it
Willy Tarreauf6710f82016-12-16 17:45:44 +0100831 * wants to stop here. It puts the variable to be dumped into cli.p0 if a single
832 * variable is requested otherwise puts environ there.
Willy Tarreau0a739292016-11-22 20:21:23 +0100833 */
834static int cli_parse_show_env(char **args, struct appctx *appctx, void *private)
835{
836 extern char **environ;
Willy Tarreauf6710f82016-12-16 17:45:44 +0100837 char **var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100838
839 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
840 return 1;
841
Willy Tarreauf6710f82016-12-16 17:45:44 +0100842 var = environ;
Willy Tarreau0a739292016-11-22 20:21:23 +0100843
844 if (*args[2]) {
845 int len = strlen(args[2]);
846
Willy Tarreauf6710f82016-12-16 17:45:44 +0100847 for (; *var; var++) {
848 if (strncmp(*var, args[2], len) == 0 &&
849 (*var)[len] == '=')
Willy Tarreau0a739292016-11-22 20:21:23 +0100850 break;
851 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100852 if (!*var) {
Willy Tarreau0a739292016-11-22 20:21:23 +0100853 appctx->ctx.cli.msg = "Variable not found\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100854 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau0a739292016-11-22 20:21:23 +0100855 return 1;
856 }
857 appctx->st2 = STAT_ST_END;
858 }
Willy Tarreauf6710f82016-12-16 17:45:44 +0100859 appctx->ctx.cli.p0 = var;
Willy Tarreau0a739292016-11-22 20:21:23 +0100860 return 0;
861}
862
Willy Tarreau599852e2016-11-22 20:33:32 +0100863/* parse a "set timeout" CLI request. It always returns 1. */
864static int cli_parse_set_timeout(char **args, struct appctx *appctx, void *private)
865{
866 struct stream_interface *si = appctx->owner;
867 struct stream *s = si_strm(si);
868
869 if (strcmp(args[2], "cli") == 0) {
870 unsigned timeout;
871 const char *res;
872
873 if (!*args[3]) {
874 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100875 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100876 return 1;
877 }
878
879 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
880 if (res || timeout < 1) {
881 appctx->ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100882 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100883 return 1;
884 }
885
886 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
887 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
888 return 1;
889 }
890 else {
891 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100892 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau599852e2016-11-22 20:33:32 +0100893 return 1;
894 }
895}
896
Willy Tarreau2af99412016-11-23 11:10:59 +0100897/* parse a "set maxconn global" command. It always returns 1. */
898static int cli_parse_set_maxconn_global(char **args, struct appctx *appctx, void *private)
899{
900 int v;
901
902 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
903 return 1;
904
905 if (!*args[3]) {
906 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100907 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100908 return 1;
909 }
910
911 v = atoi(args[3]);
912 if (v > global.hardmaxconn) {
913 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100914 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau2af99412016-11-23 11:10:59 +0100915 return 1;
916 }
917
918 /* check for unlimited values */
919 if (v <= 0)
920 v = global.hardmaxconn;
921
922 global.maxconn = v;
923
924 /* Dequeues all of the listeners waiting for a resource */
925 if (!LIST_ISEMPTY(&global_listener_queue))
926 dequeue_all_listeners(&global_listener_queue);
927
928 return 1;
929}
930
William Lallemandeceddf72016-12-15 18:06:44 +0100931
932int cli_parse_default(char **args, struct appctx *appctx, void *private)
933{
934 return 0;
935}
936
Willy Tarreau45c742b2016-11-24 14:51:17 +0100937/* parse a "set rate-limit" command. It always returns 1. */
938static int cli_parse_set_ratelimit(char **args, struct appctx *appctx, void *private)
939{
940 int v;
941 int *res;
942 int mul = 1;
943
944 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
945 return 1;
946
947 if (strcmp(args[2], "connections") == 0 && strcmp(args[3], "global") == 0)
948 res = &global.cps_lim;
949 else if (strcmp(args[2], "sessions") == 0 && strcmp(args[3], "global") == 0)
950 res = &global.sps_lim;
951#ifdef USE_OPENSSL
952 else if (strcmp(args[2], "ssl-sessions") == 0 && strcmp(args[3], "global") == 0)
953 res = &global.ssl_lim;
954#endif
955 else if (strcmp(args[2], "http-compression") == 0 && strcmp(args[3], "global") == 0) {
956 res = &global.comp_rate_lim;
957 mul = 1024;
958 }
959 else {
960 appctx->ctx.cli.msg =
961 "'set rate-limit' only supports :\n"
962 " - 'connections global' to set the per-process maximum connection rate\n"
963 " - 'sessions global' to set the per-process maximum session rate\n"
964#ifdef USE_OPENSSL
965 " - 'ssl-session global' to set the per-process maximum SSL session rate\n"
966#endif
967 " - 'http-compression global' to set the per-process maximum compression speed in kB/s\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100968 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100969 return 1;
970 }
971
972 if (!*args[4]) {
973 appctx->ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100974 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100975 return 1;
976 }
977
978 v = atoi(args[4]);
979 if (v < 0) {
980 appctx->ctx.cli.msg = "Value out of range.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +0100981 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau45c742b2016-11-24 14:51:17 +0100982 return 1;
983 }
984
985 *res = v * mul;
986
987 /* Dequeues all of the listeners waiting for a resource */
988 if (!LIST_ISEMPTY(&global_listener_queue))
989 dequeue_all_listeners(&global_listener_queue);
990
991 return 1;
992}
993
William Lallemand74c24fb2016-11-21 17:18:36 +0100994/* parse the "level" argument on the bind lines */
995static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
996{
997 if (!*args[cur_arg + 1]) {
998 memprintf(err, "'%s' : missing level", args[cur_arg]);
999 return ERR_ALERT | ERR_FATAL;
1000 }
1001
1002 if (!strcmp(args[cur_arg+1], "user"))
1003 conf->level = ACCESS_LVL_USER;
1004 else if (!strcmp(args[cur_arg+1], "operator"))
1005 conf->level = ACCESS_LVL_OPER;
1006 else if (!strcmp(args[cur_arg+1], "admin"))
1007 conf->level = ACCESS_LVL_ADMIN;
1008 else {
1009 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
1010 args[cur_arg], args[cur_arg+1]);
1011 return ERR_ALERT | ERR_FATAL;
1012 }
1013
1014 return 0;
1015}
1016
1017static struct applet cli_applet = {
1018 .obj_type = OBJ_TYPE_APPLET,
1019 .name = "<CLI>", /* used for logging */
1020 .fct = cli_io_handler,
1021 .release = cli_release_handler,
1022};
1023
Willy Tarreau0a739292016-11-22 20:21:23 +01001024/* register cli keywords */
1025static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau2af99412016-11-23 11:10:59 +01001026 { { "set", "maxconn", "global", NULL }, "set maxconn global : change the per-process maxconn setting", cli_parse_set_maxconn_global, NULL },
Willy Tarreau45c742b2016-11-24 14:51:17 +01001027 { { "set", "rate-limit", NULL }, "set rate-limit : change a rate limiting value", cli_parse_set_ratelimit, NULL },
Willy Tarreau599852e2016-11-22 20:33:32 +01001028 { { "set", "timeout", NULL }, "set timeout : change a timeout setting", cli_parse_set_timeout, NULL, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001029 { { "show", "env", NULL }, "show env [var] : dump environment variables known to the process", cli_parse_show_env, cli_io_handler_show_env, NULL },
William Lallemandeceddf72016-12-15 18:06:44 +01001030 { { "show", "cli", "sockets", NULL }, "show cli sockets : dump list of cli sockets", cli_parse_default, cli_io_handler_show_cli_sock, NULL },
Willy Tarreau0a739292016-11-22 20:21:23 +01001031 {{},}
1032}};
1033
William Lallemand74c24fb2016-11-21 17:18:36 +01001034static struct cfg_kw_list cfg_kws = {ILH, {
1035 { CFG_GLOBAL, "stats", stats_parse_global },
1036 { 0, NULL, NULL },
1037}};
1038
1039static struct bind_kw_list bind_kws = { "STAT", { }, {
1040 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
1041 { NULL, NULL, 0 },
1042}};
1043
1044__attribute__((constructor))
1045static void __dumpstats_module_init(void)
1046{
1047 cfg_register_keywords(&cfg_kws);
Willy Tarreau0a739292016-11-22 20:21:23 +01001048 cli_register_kw(&cli_kws);
William Lallemand74c24fb2016-11-21 17:18:36 +01001049 bind_register_keywords(&bind_kws);
1050}
1051
1052/*
1053 * Local variables:
1054 * c-indent-level: 8
1055 * c-basic-offset: 8
1056 * End:
1057 */