forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
3235 lines (2761 loc) · 92.5 KB
/
cli.c
File metadata and controls
3235 lines (2761 loc) · 92.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Functions dedicated to statistics output and the stats socket
*
* Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
* Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <net/if.h>
#include <haproxy/api.h>
#include <haproxy/applet.h>
#include <haproxy/base64.h>
#include <haproxy/cfgparse.h>
#include <haproxy/channel.h>
#include <haproxy/check.h>
#include <haproxy/cli.h>
#include <haproxy/compression.h>
#include <haproxy/dns-t.h>
#include <haproxy/errors.h>
#include <haproxy/fd.h>
#include <haproxy/freq_ctr.h>
#include <haproxy/frontend.h>
#include <haproxy/global.h>
#include <haproxy/list.h>
#include <haproxy/listener.h>
#include <haproxy/log.h>
#include <haproxy/mworker.h>
#include <haproxy/mworker-t.h>
#include <haproxy/pattern-t.h>
#include <haproxy/peers.h>
#include <haproxy/pipe.h>
#include <haproxy/protocol.h>
#include <haproxy/proxy.h>
#include <haproxy/sample-t.h>
#include <haproxy/sc_strm.h>
#include <haproxy/server.h>
#include <haproxy/session.h>
#include <haproxy/sock.h>
#include <haproxy/stats-t.h>
#include <haproxy/stconn.h>
#include <haproxy/stream.h>
#include <haproxy/task.h>
#include <haproxy/ticks.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
#include <haproxy/version.h>
#define PAYLOAD_PATTERN "<<"
static struct applet cli_applet;
static struct applet mcli_applet;
static const char cli_permission_denied_msg[] =
"Permission denied\n"
"";
static THREAD_LOCAL char *dynamic_usage_msg = NULL;
/* List head of cli keywords */
static struct cli_kw_list cli_keywords = {
.list = LIST_HEAD_INIT(cli_keywords.list)
};
extern const char *stat_status_codes[];
struct proxy *mworker_proxy; /* CLI proxy of the master */
struct bind_conf *mcli_reload_bind_conf;
/* CLI context for the "show env" command */
struct show_env_ctx {
char **var; /* first variable to show */
int show_one; /* stop after showing the first one */
};
/* CLI context for the "show fd" command */
/* flags for show_fd_ctx->show_mask */
#define CLI_SHOWFD_F_PI 0x00000001 /* pipes */
#define CLI_SHOWFD_F_LI 0x00000002 /* listeners */
#define CLI_SHOWFD_F_FE 0x00000004 /* frontend conns */
#define CLI_SHOWFD_F_SV 0x00000010 /* server-only conns */
#define CLI_SHOWFD_F_PX 0x00000020 /* proxy-only conns */
#define CLI_SHOWFD_F_BE 0x00000030 /* backend: srv+px */
#define CLI_SHOWFD_F_CO 0x00000034 /* conn: be+fe */
#define CLI_SHOWFD_F_ANY 0x0000003f /* any type */
struct show_fd_ctx {
int fd; /* first FD to show */
int show_one; /* stop after showing one FD */
uint show_mask; /* CLI_SHOWFD_F_xxx */
};
/* CLI context for the "show cli sockets" command */
struct show_sock_ctx {
struct bind_conf *bind_conf;
struct listener *listener;
};
static int cmp_kw_entries(const void *a, const void *b)
{
const struct cli_kw *l = *(const struct cli_kw **)a;
const struct cli_kw *r = *(const struct cli_kw **)b;
return strcmp(l->usage ? l->usage : "", r->usage ? r->usage : "");
}
/* This will show the help message and list the commands supported at the
* current level that match all of the first words of <args> if args is not
* NULL, or all args if none matches or if args is null.
*/
static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args)
{
struct cli_kw *entries[CLI_MAX_HELP_ENTRIES];
struct cli_kw_list *kw_list;
struct cli_kw *kw;
struct buffer *tmp = get_trash_chunk();
struct buffer out;
struct { struct cli_kw *kw; int dist; } matches[CLI_MAX_MATCHES], swp;
int idx;
int ishelp = 0;
int length = 0;
int help_entries = 0;
ha_free(&dynamic_usage_msg);
if (args && *args && strcmp(*args, "help") == 0) {
args++;
ishelp = 1;
}
/* first, let's measure the longest match */
list_for_each_entry(kw_list, &cli_keywords.list, list) {
for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
continue;
if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
(appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
(ACCESS_MASTER_ONLY|ACCESS_MASTER))
continue;
/* OK this command is visible */
for (idx = 0; idx < CLI_PREFIX_KW_NB; idx++) {
if (!kw->str_kw[idx])
break; // end of keyword
if (!args || !args[idx] || !*args[idx])
break; // end of command line
if (strcmp(kw->str_kw[idx], args[idx]) != 0)
break;
if (idx + 1 > length)
length = idx + 1;
}
}
}
/* now <length> equals the number of exactly matching words */
chunk_reset(tmp);
if (ishelp) // this is the help message.
chunk_strcat(tmp, "The following commands are valid at this level:\n");
else {
chunk_strcat(tmp, "Unknown command: '");
if (args && *args)
chunk_strcat(tmp, *args);
chunk_strcat(tmp, "'");
if (!length && (!args || !*args || !**args)) // no match
chunk_strcat(tmp, ". Please enter one of the following commands only:\n");
else // partial match
chunk_strcat(tmp, ", but maybe one of the following ones is a better match:\n");
}
for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
matches[idx].kw = NULL;
matches[idx].dist = INT_MAX;
}
/* In case of partial match we'll look for the best matching entries
* starting from position <length>
*/
if (args && args[length] && *args[length]) {
list_for_each_entry(kw_list, &cli_keywords.list, list) {
for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
continue;
if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
(ACCESS_MASTER_ONLY|ACCESS_MASTER)))
continue;
for (idx = 0; idx < length; idx++) {
if (!kw->str_kw[idx])
break; // end of keyword
if (!args || !args[idx] || !*args[idx])
break; // end of command line
if (strcmp(kw->str_kw[idx], args[idx]) != 0)
break;
}
/* extra non-matching words are fuzzy-matched */
if (kw->usage && idx == length && args[idx] && *args[idx]) {
uint8_t word_sig[1024];
uint8_t list_sig[1024];
int dist = 0;
int totlen = 0;
int i;
/* this one matches, let's compute the distance between the two
* on the remaining words. For this we're computing the signature
* of everything that remains and the cumulated length of the
* strings.
*/
memset(word_sig, 0, sizeof(word_sig));
for (i = idx; i < CLI_PREFIX_KW_NB && args[i] && *args[i]; i++) {
update_word_fingerprint(word_sig, args[i]);
totlen += strlen(args[i]);
}
memset(list_sig, 0, sizeof(list_sig));
for (i = idx; i < CLI_PREFIX_KW_NB && kw->str_kw[i]; i++) {
update_word_fingerprint(list_sig, kw->str_kw[i]);
totlen += strlen(kw->str_kw[i]);
}
dist = word_fingerprint_distance(word_sig, list_sig);
/* insert this one at its place if relevant, in order to keep only
* the best matches.
*/
swp.kw = kw; swp.dist = dist;
if (dist < 5*totlen/2 && dist < matches[CLI_MAX_MATCHES-1].dist) {
matches[CLI_MAX_MATCHES-1] = swp;
for (idx = CLI_MAX_MATCHES - 1; --idx >= 0;) {
if (matches[idx+1].dist >= matches[idx].dist)
break;
matches[idx+1] = matches[idx];
matches[idx] = swp;
}
}
}
}
}
}
if (matches[0].kw) {
/* we have fuzzy matches, let's propose them */
for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
kw = matches[idx].kw;
if (!kw)
break;
/* stop the dump if some words look very unlikely candidates */
if (matches[idx].dist > 5*matches[0].dist/2)
break;
if (help_entries < CLI_MAX_HELP_ENTRIES)
entries[help_entries++] = kw;
}
}
list_for_each_entry(kw_list, &cli_keywords.list, list) {
/* no full dump if we've already found nice candidates */
if (matches[0].kw)
break;
for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
/* in a worker or normal process, don't display master-only commands
* nor expert/experimental mode commands if not in this mode.
*/
if (kw->level & ~appctx->cli_level & (ACCESS_MASTER_ONLY|ACCESS_EXPERT|ACCESS_EXPERIMENTAL))
continue;
/* in master, if the CLI don't have the
* ACCESS_MCLI_DEBUG don't display commands that have
* neither the master bit nor the master-only bit.
*/
if (!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
((appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) ==
(ACCESS_MASTER_ONLY|ACCESS_MASTER)))
continue;
for (idx = 0; idx < length; idx++) {
if (!kw->str_kw[idx])
break; // end of keyword
if (!args || !args[idx] || !*args[idx])
break; // end of command line
if (strcmp(kw->str_kw[idx], args[idx]) != 0)
break;
}
if (kw->usage && idx == length && help_entries < CLI_MAX_HELP_ENTRIES)
entries[help_entries++] = kw;
}
}
qsort(entries, help_entries, sizeof(*entries), cmp_kw_entries);
for (idx = 0; idx < help_entries; idx++)
chunk_appendf(tmp, " %s\n", entries[idx]->usage);
/* always show the prompt/help/quit commands */
chunk_strcat(tmp,
" help [<command>] : list matching or all commands\n"
" prompt : toggle interactive mode with prompt\n"
" quit : disconnect\n");
chunk_init(&out, NULL, 0);
chunk_dup(&out, tmp);
dynamic_usage_msg = out.area;
cli_msg(appctx, LOG_INFO, dynamic_usage_msg);
return dynamic_usage_msg;
}
struct cli_kw* cli_find_kw(char **args)
{
struct cli_kw_list *kw_list;
struct cli_kw *kw;/* current cli_kw */
char **tmp_args;
const char **tmp_str_kw;
int found = 0;
if (LIST_ISEMPTY(&cli_keywords.list))
return NULL;
list_for_each_entry(kw_list, &cli_keywords.list, list) {
kw = &kw_list->kw[0];
while (*kw->str_kw) {
tmp_args = args;
tmp_str_kw = kw->str_kw;
while (*tmp_str_kw) {
if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
found = 1;
} else {
found = 0;
break;
}
tmp_args++;
tmp_str_kw++;
}
if (found)
return (kw);
kw++;
}
}
return NULL;
}
struct cli_kw* cli_find_kw_exact(char **args)
{
struct cli_kw_list *kw_list;
int found = 0;
int i;
int j;
if (LIST_ISEMPTY(&cli_keywords.list))
return NULL;
list_for_each_entry(kw_list, &cli_keywords.list, list) {
for (i = 0; kw_list->kw[i].str_kw[0]; i++) {
found = 1;
for (j = 0; j < CLI_PREFIX_KW_NB; j++) {
if (args[j] == NULL && kw_list->kw[i].str_kw[j] == NULL) {
break;
}
if (args[j] == NULL || kw_list->kw[i].str_kw[j] == NULL) {
found = 0;
break;
}
if (strcmp(args[j], kw_list->kw[i].str_kw[j]) != 0) {
found = 0;
break;
}
}
if (found)
return &kw_list->kw[i];
}
}
return NULL;
}
void cli_register_kw(struct cli_kw_list *kw_list)
{
LIST_APPEND(&cli_keywords.list, &kw_list->list);
}
/* list all known keywords on stdout, one per line */
void cli_list_keywords(void)
{
struct cli_kw_list *kw_list;
struct cli_kw *kwp, *kwn, *kw;
int idx;
for (kwn = kwp = NULL;; kwp = kwn) {
list_for_each_entry(kw_list, &cli_keywords.list, list) {
/* note: we sort based on the usage message when available,
* otherwise we fall back to the first keyword.
*/
for (kw = &kw_list->kw[0]; kw->str_kw[0]; kw++) {
if (strordered(kwp ? kwp->usage ? kwp->usage : kwp->str_kw[0] : NULL,
kw->usage ? kw->usage : kw->str_kw[0],
kwn != kwp ? kwn->usage ? kwn->usage : kwn->str_kw[0] : NULL))
kwn = kw;
}
}
if (kwn == kwp)
break;
for (idx = 0; kwn->str_kw[idx]; idx++) {
printf("%s ", kwn->str_kw[idx]);
}
if (kwn->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER))
printf("[MASTER] ");
if (!(kwn->level & ACCESS_MASTER_ONLY))
printf("[WORKER] ");
if (kwn->level & ACCESS_EXPERT)
printf("[EXPERT] ");
if (kwn->level & ACCESS_EXPERIMENTAL)
printf("[EXPERIM] ");
printf("\n");
}
}
/* allocate a new stats frontend named <name>, and return it
* (or NULL in case of lack of memory).
*/
static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
{
struct proxy *fe;
fe = calloc(1, sizeof(*fe));
if (!fe)
return NULL;
init_new_proxy(fe);
fe->next = proxies_list;
proxies_list = fe;
fe->last_change = now.tv_sec;
fe->id = strdup("GLOBAL");
fe->cap = PR_CAP_FE|PR_CAP_INT;
fe->maxconn = 10; /* default to 10 concurrent connections */
fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
fe->conf.file = strdup(file);
fe->conf.line = line;
fe->accept = frontend_accept;
fe->default_target = &cli_applet.obj_type;
/* the stats frontend is the only one able to assign ID #0 */
fe->conf.id.key = fe->uuid = 0;
eb32_insert(&used_proxy_id, &fe->conf.id);
return fe;
}
/* This function parses a "stats" statement in the "global" section. It returns
* -1 if there is any error, otherwise zero. If it returns -1, it will write an
* error message into the <err> buffer which will be preallocated. The trailing
* '\n' must not be written. The function must be called with <args> pointing to
* the first word after "stats".
*/
static int cli_parse_global(char **args, int section_type, struct proxy *curpx,
const struct proxy *defpx, const char *file, int line,
char **err)
{
struct bind_conf *bind_conf;
struct listener *l;
if (strcmp(args[1], "socket") == 0) {
int cur_arg;
if (*args[2] == 0) {
memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
return -1;
}
if (!global.cli_fe) {
if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
return -1;
}
}
bind_conf = bind_conf_alloc(global.cli_fe, file, line, args[2], xprt_get(XPRT_RAW));
if (!bind_conf) {
memprintf(err, "'%s %s' : out of memory trying to allocate a bind_conf", args[0], args[1]);
return -1;
}
bind_conf->level &= ~ACCESS_LVL_MASK;
bind_conf->level |= ACCESS_LVL_OPER; /* default access level */
if (!str2listener(args[2], global.cli_fe, bind_conf, file, line, err)) {
memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
file, line, args[0], args[1], err && *err ? *err : "error");
return -1;
}
cur_arg = 3;
while (*args[cur_arg]) {
struct bind_kw *kw;
const char *best;
int code;
kw = bind_find_kw(args[cur_arg]);
if (kw) {
if (!kw->parse) {
memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
args[0], args[1], args[cur_arg]);
return -1;
}
code = kw->parse(args, cur_arg, global.cli_fe, bind_conf, err);
/* FIXME: this is ugly, we don't have a way to collect warnings,
* yet some important bind keywords may report warnings that we
* must display.
*/
if (((code & (ERR_WARN|ERR_FATAL|ERR_ALERT)) == ERR_WARN) && err && *err) {
indent_msg(err, 2);
ha_warning("parsing [%s:%d] : '%s %s' : %s\n", file, line, args[0], args[1], *err);
ha_free(err);
}
if (code & ~ERR_WARN) {
if (err && *err)
memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
else
memprintf(err, "'%s %s' : error encountered while processing '%s'",
args[0], args[1], args[cur_arg]);
return -1;
}
cur_arg += 1 + kw->skip;
continue;
}
best = bind_find_best_kw(args[cur_arg]);
if (best)
memprintf(err, "'%s %s' : unknown keyword '%s'. Did you mean '%s' maybe ?",
args[0], args[1], args[cur_arg], best);
else
memprintf(err, "'%s %s' : unknown keyword '%s'.",
args[0], args[1], args[cur_arg]);
return -1;
}
bind_conf->accept = session_accept_fd;
bind_conf->nice = -64; /* we want to boost priority for local stats */
bind_conf->options |= BC_O_UNLIMITED; /* don't make the peers subject to global limits */
list_for_each_entry(l, &bind_conf->listeners, by_bind) {
global.maxsock++; /* for the listening socket */
}
}
else if (strcmp(args[1], "timeout") == 0) {
unsigned timeout;
const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
if (res == PARSE_TIME_OVER) {
memprintf(err, "timer overflow in argument '%s' to '%s %s' (maximum value is 2147483647 ms or ~24.8 days)",
args[2], args[0], args[1]);
return -1;
}
else if (res == PARSE_TIME_UNDER) {
memprintf(err, "timer underflow in argument '%s' to '%s %s' (minimum non-null value is 1 ms)",
args[2], args[0], args[1]);
return -1;
}
else if (res) {
memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
return -1;
}
if (!timeout) {
memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
return -1;
}
if (!global.cli_fe) {
if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
return -1;
}
}
global.cli_fe->timeout.client = MS_TO_TICKS(timeout);
}
else if (strcmp(args[1], "maxconn") == 0) {
int maxconn = atol(args[2]);
if (maxconn <= 0) {
memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
return -1;
}
if (!global.cli_fe) {
if ((global.cli_fe = cli_alloc_fe("GLOBAL", file, line)) == NULL) {
memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
return -1;
}
}
global.cli_fe->maxconn = maxconn;
}
else if (strcmp(args[1], "bind-process") == 0) {
memprintf(err, "'%s' is not supported anymore.", args[0]);
return -1;
}
else {
memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
return -1;
}
return 0;
}
/*
* This function exports the bound addresses of a <frontend> in the environment
* variable <varname>. Those addresses are separated by semicolons and prefixed
* with their type (abns@, unix@, sockpair@ etc)
* Return -1 upon error, 0 otherwise
*/
int listeners_setenv(struct proxy *frontend, const char *varname)
{
struct buffer *trash = get_trash_chunk();
struct bind_conf *bind_conf;
if (frontend) {
list_for_each_entry(bind_conf, &frontend->conf.bind, by_fe) {
struct listener *l;
list_for_each_entry(l, &bind_conf->listeners, by_bind) {
char addr[46];
char port[6];
/* separate listener by semicolons */
if (trash->data)
chunk_appendf(trash, ";");
if (l->rx.addr.ss_family == AF_UNIX) {
const struct sockaddr_un *un;
un = (struct sockaddr_un *)&l->rx.addr;
if (un->sun_path[0] == '\0') {
chunk_appendf(trash, "abns@%s", un->sun_path+1);
} else {
chunk_appendf(trash, "unix@%s", un->sun_path);
}
} else if (l->rx.addr.ss_family == AF_INET) {
addr_to_str(&l->rx.addr, addr, sizeof(addr));
port_to_str(&l->rx.addr, port, sizeof(port));
chunk_appendf(trash, "ipv4@%s:%s", addr, port);
} else if (l->rx.addr.ss_family == AF_INET6) {
addr_to_str(&l->rx.addr, addr, sizeof(addr));
port_to_str(&l->rx.addr, port, sizeof(port));
chunk_appendf(trash, "ipv6@[%s]:%s", addr, port);
} else if (l->rx.addr.ss_family == AF_CUST_SOCKPAIR) {
chunk_appendf(trash, "sockpair@%d", ((struct sockaddr_in *)&l->rx.addr)->sin_addr.s_addr);
}
}
}
trash->area[trash->data++] = '\0';
if (setenv(varname, trash->area, 1) < 0)
return -1;
}
return 0;
}
int cli_socket_setenv()
{
if (listeners_setenv(global.cli_fe, "HAPROXY_CLI") < 0)
return -1;
if (listeners_setenv(mworker_proxy, "HAPROXY_MASTER_CLI") < 0)
return -1;
return 0;
}
REGISTER_CONFIG_POSTPARSER("cli", cli_socket_setenv);
/* Verifies that the CLI at least has a level at least as high as <level>
* (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
* failure, an error message is prepared and the appctx's state is adjusted
* to print it so that a return 1 is enough to abort any processing.
*/
int cli_has_level(struct appctx *appctx, int level)
{
if ((appctx->cli_level & ACCESS_LVL_MASK) < level) {
cli_err(appctx, cli_permission_denied_msg);
return 0;
}
return 1;
}
/* same as cli_has_level but for the CLI proxy and without error message */
int pcli_has_level(struct stream *s, int level)
{
if ((s->pcli_flags & ACCESS_LVL_MASK) < level) {
return 0;
}
return 1;
}
/* Returns severity_output for the current session if set, or default for the socket */
static int cli_get_severity_output(struct appctx *appctx)
{
if (appctx->cli_severity_output)
return appctx->cli_severity_output;
return strm_li(appctx_strm(appctx))->bind_conf->severity_output;
}
/* Processes the CLI interpreter on the stats socket. This function is called
* from the CLI's IO handler running in an appctx context. The function returns
* 1 if the request was understood, otherwise zero (in which case an error
* message will be displayed). It is called with appctx->st0
* set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do
* it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to
* have its own I/O handler called again. Most of the time, parsers will only
* set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg.
* If a keyword parser is NULL and an I/O handler is declared, the I/O handler
* will automatically be used.
*/
static int cli_parse_request(struct appctx *appctx)
{
char *args[MAX_CLI_ARGS + 1], *p, *end, *payload = NULL;
int i = 0;
struct cli_kw *kw;
p = appctx->chunk->area;
end = p + appctx->chunk->data;
/*
* Get pointers on words.
* One extra slot is reserved to store a pointer on a null byte.
*/
while (i < MAX_CLI_ARGS && p < end) {
int j, k;
/* skip leading spaces/tabs */
p += strspn(p, " \t");
if (!*p)
break;
if (strcmp(p, PAYLOAD_PATTERN) == 0) {
/* payload pattern recognized here, this is not an arg anymore,
* the payload starts at the first byte that follows the zero
* after the pattern.
*/
payload = p + strlen(PAYLOAD_PATTERN) + 1;
break;
}
args[i] = p;
while (1) {
p += strcspn(p, " \t\\");
/* escaped chars using backlashes (\) */
if (*p == '\\') {
if (!*++p)
break;
if (!*++p)
break;
} else {
break;
}
}
*p++ = 0;
/* unescape backslashes (\) */
for (j = 0, k = 0; args[i][k]; k++) {
if (args[i][k] == '\\') {
if (args[i][k + 1] == '\\')
k++;
else
continue;
}
args[i][j] = args[i][k];
j++;
}
args[i][j] = 0;
i++;
}
/* fill unused slots */
p = appctx->chunk->area + appctx->chunk->data;
for (; i < MAX_CLI_ARGS + 1; i++)
args[i] = p;
kw = cli_find_kw(args);
if (!kw ||
(kw->level & ~appctx->cli_level & ACCESS_MASTER_ONLY) ||
(!(appctx->cli_level & ACCESS_MCLI_DEBUG) &&
(appctx->cli_level & ~kw->level & (ACCESS_MASTER_ONLY|ACCESS_MASTER)) == (ACCESS_MASTER_ONLY|ACCESS_MASTER))) {
/* keyword not found in this mode */
cli_gen_usage_msg(appctx, args);
return 0;
}
/* don't handle expert mode commands if not in this mode. */
if (kw->level & ~appctx->cli_level & ACCESS_EXPERT) {
cli_err(appctx, "This command is restricted to expert mode only.\n");
return 0;
}
if (kw->level & ~appctx->cli_level & ACCESS_EXPERIMENTAL) {
cli_err(appctx, "This command is restricted to experimental mode only.\n");
return 0;
}
if (kw->level == ACCESS_EXPERT)
mark_tainted(TAINTED_CLI_EXPERT_MODE);
else if (kw->level == ACCESS_EXPERIMENTAL)
mark_tainted(TAINTED_CLI_EXPERIMENTAL_MODE);
appctx->io_handler = kw->io_handler;
appctx->io_release = kw->io_release;
if (kw->parse && kw->parse(args, payload, appctx, kw->private) != 0)
goto fail;
/* kw->parse could set its own io_handler or io_release handler */
if (!appctx->io_handler)
goto fail;
appctx->st0 = CLI_ST_CALLBACK;
return 1;
fail:
appctx->io_handler = NULL;
appctx->io_release = NULL;
return 1;
}
/* prepends then outputs the argument msg with a syslog-type severity depending on severity_output value */
static int cli_output_msg(struct channel *chn, const char *msg, int severity, int severity_output)
{
struct buffer *tmp;
if (likely(severity_output == CLI_SEVERITY_NONE))
return ci_putblk(chn, msg, strlen(msg));
tmp = get_trash_chunk();
chunk_reset(tmp);
if (severity < 0 || severity > 7) {
ha_warning("socket command feedback with invalid severity %d", severity);
chunk_printf(tmp, "[%d]: ", severity);
}
else {
switch (severity_output) {
case CLI_SEVERITY_NUMBER:
chunk_printf(tmp, "[%d]: ", severity);
break;
case CLI_SEVERITY_STRING:
chunk_printf(tmp, "[%s]: ", log_levels[severity]);
break;
default:
ha_warning("Unrecognized severity output %d", severity_output);
}
}
chunk_appendf(tmp, "%s", msg);
return ci_putblk(chn, tmp->area, strlen(tmp->area));
}
/* This I/O handler runs as an applet embedded in a stream connector. It is
* used to processes I/O from/to the stats unix socket. The system relies on a
* state machine handling requests and various responses. We read a request,
* then we process it and send the response, and we possibly display a prompt.
* Then we can read again. The state is stored in appctx->st0 and is one of the
* CLI_ST_* constants. appctx->st1 is used to indicate whether prompt is enabled
* or not.
*/
static void cli_io_handler(struct appctx *appctx)
{
struct stconn *sc = appctx_sc(appctx);
struct channel *req = sc_oc(sc);
struct channel *res = sc_ic(sc);
struct bind_conf *bind_conf = strm_li(__sc_strm(sc))->bind_conf;
int reql;
int len;
if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW))))
goto out;
/* Check if the input buffer is available. */
if (!b_size(&res->buf)) {
sc_need_room(sc);
goto out;
}
while (1) {
if (appctx->st0 == CLI_ST_INIT) {
/* reset severity to default at init */
appctx->cli_severity_output = bind_conf->severity_output;
applet_reset_svcctx(appctx);
appctx->st0 = CLI_ST_GETREQ;
appctx->cli_level = bind_conf->level;
}
else if (appctx->st0 == CLI_ST_END) {
se_fl_set(appctx->sedesc, SE_FL_EOS);
free_trash_chunk(appctx->chunk);
appctx->chunk = NULL;
break;
}
else if (appctx->st0 == CLI_ST_GETREQ) {
char *str;
/* use a trash chunk to store received data */
if (!appctx->chunk) {
appctx->chunk = alloc_trash_chunk();
if (!appctx->chunk) {
se_fl_set(appctx->sedesc, SE_FL_ERROR);
appctx->st0 = CLI_ST_END;
continue;
}
}
str = appctx->chunk->area + appctx->chunk->data;
/* ensure we have some output room left in the event we
* would want to return some info right after parsing.
*/
if (buffer_almost_full(sc_ib(sc))) {
sc_need_room(sc);
break;
}
/* payload doesn't take escapes nor does it end on semi-colons, so
* we use the regular getline. Normal mode however must stop on
* LFs and semi-colons that are not prefixed by a backslash. Note
* that we reserve one byte at the end to insert a trailing nul byte.
*/
if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)
reql = co_getline(sc_oc(sc), str,
appctx->chunk->size - appctx->chunk->data - 1);
else
reql = co_getdelim(sc_oc(sc), str,
appctx->chunk->size - appctx->chunk->data - 1,
"\n;", '\\');
if (reql <= 0) { /* closed or EOL not found */
if (reql == 0)
break;
se_fl_set(appctx->sedesc, SE_FL_ERROR);
appctx->st0 = CLI_ST_END;
continue;
}
if (!(appctx->st1 & APPCTX_CLI_ST1_PAYLOAD)) {
/* seek for a possible unescaped semi-colon. If we find
* one, we replace it with an LF and skip only this part.
*/
for (len = 0; len < reql; len++) {
if (str[len] == '\\') {
len++;
continue;
}
if (str[len] == ';') {
str[len] = '\n';
reql = len + 1;
break;
}
}
}
/* now it is time to check that we have a full line,
* remove the trailing \n and possibly \r, then cut the
* line.
*/
len = reql - 1;
if (str[len] != '\n') {
se_fl_set(appctx->sedesc, SE_FL_ERROR);
appctx->st0 = CLI_ST_END;
continue;
}
if (len && str[len-1] == '\r')
len--;
str[len] = '\0';
appctx->chunk->data += len;
if (appctx->st1 & APPCTX_CLI_ST1_PAYLOAD) {
appctx->chunk->area[appctx->chunk->data] = '\n';
appctx->chunk->area[appctx->chunk->data + 1] = 0;