forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtysh.c
3885 lines (3388 loc) · 102 KB
/
vtysh.c
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
/* Virtual terminal interface shell.
* Copyright (C) 2000 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra 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, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include <sys/un.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include "linklist.h"
#include "command.h"
#include "memory.h"
#include "filter.h"
#include "vtysh/vtysh.h"
#include "log.h"
#include "bgpd/bgp_vty.h"
#include "ns.h"
#include "vrf.h"
#include "libfrr.h"
#include "command_graph.h"
#include "frrstr.h"
#include "json.h"
DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CMD, "Vtysh cmd copy")
/* Struct VTY. */
struct vty *vty;
/* VTY shell pager name. */
char *vtysh_pager_name = NULL;
/* VTY shell client structure */
struct vtysh_client {
int fd;
const char *name;
int flag;
char path[MAXPATHLEN];
struct vtysh_client *next;
};
/* Some utility functions for working on vtysh-specific vty tasks */
static FILE *vty_open_pager(struct vty *vty)
{
if (vty->is_paged)
return vty->of;
if (!vtysh_pager_name)
return NULL;
vty->of_saved = vty->of;
vty->of = popen(vtysh_pager_name, "w");
if (vty->of == NULL) {
vty->of = vty->of_saved;
perror("popen");
exit(1);
}
vty->is_paged = true;
return vty->of;
}
static int vty_close_pager(struct vty *vty)
{
if (!vty->is_paged)
return 0;
fflush(vty->of);
if (pclose(vty->of) == -1) {
perror("pclose");
exit(1);
}
vty->of = vty->of_saved;
vty->is_paged = false;
return 0;
}
static void vtysh_pager_envdef(bool fallback)
{
char *pager_defined;
pager_defined = getenv("VTYSH_PAGER");
if (pager_defined)
vtysh_pager_name = strdup(pager_defined);
else if (fallback)
vtysh_pager_name = strdup(VTYSH_PAGER);
}
/* --- */
struct vtysh_client vtysh_client[] = {
{.fd = -1, .name = "zebra", .flag = VTYSH_ZEBRA, .next = NULL},
{.fd = -1, .name = "ripd", .flag = VTYSH_RIPD, .next = NULL},
{.fd = -1, .name = "ripngd", .flag = VTYSH_RIPNGD, .next = NULL},
{.fd = -1, .name = "ospfd", .flag = VTYSH_OSPFD, .next = NULL},
{.fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .next = NULL},
{.fd = -1, .name = "ldpd", .flag = VTYSH_LDPD, .next = NULL},
{.fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .next = NULL},
{.fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .next = NULL},
{.fd = -1, .name = "pimd", .flag = VTYSH_PIMD, .next = NULL},
{.fd = -1, .name = "nhrpd", .flag = VTYSH_NHRPD, .next = NULL},
{.fd = -1, .name = "eigrpd", .flag = VTYSH_EIGRPD, .next = NULL},
{.fd = -1, .name = "babeld", .flag = VTYSH_BABELD, .next = NULL},
{.fd = -1, .name = "sharpd", .flag = VTYSH_SHARPD, .next = NULL},
{.fd = -1, .name = "fabricd", .flag = VTYSH_FABRICD, .next = NULL},
{.fd = -1, .name = "watchfrr", .flag = VTYSH_WATCHFRR, .next = NULL},
{.fd = -1, .name = "pbrd", .flag = VTYSH_PBRD, .next = NULL},
{.fd = -1, .name = "staticd", .flag = VTYSH_STATICD, .next = NULL},
{.fd = -1, .name = "bfdd", .flag = VTYSH_BFDD, .next = NULL},
};
enum vtysh_write_integrated vtysh_write_integrated =
WRITE_INTEGRATED_UNSPECIFIED;
static int vtysh_reconnect(struct vtysh_client *vclient);
static void vclient_close(struct vtysh_client *vclient)
{
if (vclient->fd >= 0) {
vty_out(vty,
"Warning: closing connection to %s because of an I/O error!\n",
vclient->name);
close(vclient->fd);
/* indicate as candidate for reconnect */
vclient->fd = VTYSH_WAS_ACTIVE;
}
}
/*
* Send a CLI command to a client and read the response.
*
* Output will be printed to vty->of. If you want to suppress output, set that
* to NULL.
*
* vclient
* the client to send the command to
*
* line
* the command to send
*
* callback
* if non-null, this will be called with each line of output received from
* the client passed in the second parameter
*
* cbarg
* optional first argument to pass to callback
*
* Returns:
* a status code
*/
static int vtysh_client_run(struct vtysh_client *vclient, const char *line,
void (*callback)(void *, const char *), void *cbarg)
{
int ret;
char stackbuf[4096];
char *buf = stackbuf;
size_t bufsz = sizeof(stackbuf);
char *bufvalid, *end = NULL;
char terminator[3] = {0, 0, 0};
/* vclinet was previously active, try to reconnect */
if (vclient->fd == VTYSH_WAS_ACTIVE) {
ret = vtysh_reconnect(vclient);
if (ret < 0)
goto out_err;
}
if (vclient->fd < 0)
return CMD_SUCCESS;
ret = write(vclient->fd, line, strlen(line) + 1);
if (ret <= 0) {
/* close connection and try to reconnect */
vclient_close(vclient);
ret = vtysh_reconnect(vclient);
if (ret < 0)
goto out_err;
/* retry line */
ret = write(vclient->fd, line, strlen(line) + 1);
if (ret <= 0)
goto out_err;
}
bufvalid = buf;
do {
ssize_t nread =
read(vclient->fd, bufvalid, buf + bufsz - bufvalid - 1);
if (nread < 0 && (errno == EINTR || errno == EAGAIN))
continue;
if (nread <= 0) {
vty_out(vty, "vtysh: error reading from %s: %s (%d)",
vclient->name, safe_strerror(errno), errno);
goto out_err;
}
bufvalid += nread;
/* Null terminate so we may pass this to *printf later. */
bufvalid[0] = '\0';
/*
* We expect string output from daemons, so instead of looking
* for the full 3 null bytes of the terminator, we check for
* just one instead and assume it is the first byte of the
* terminator. The presence of the full terminator is checked
* later.
*/
if (bufvalid - buf >= 4)
end = memmem(bufvalid - 4, 4, "\0", 1);
/*
* calculate # bytes we have, up to & not including the
* terminator if present
*/
size_t textlen = (end ? end : bufvalid) - buf;
bool b = false;
/* feed line processing callback if present */
while (callback && bufvalid > buf && (end > buf || !end)) {
textlen = (end ? end : bufvalid) - buf;
char *eol = memchr(buf, '\n', textlen);
if (eol)
/* line break */
*eol++ = '\0';
else if (end == buf)
/*
* no line break, end of input, no text left
* before end; nothing to write
*/
b = true;
else if (end)
/* no nl, end of input, but some text left */
eol = end;
else if (bufvalid == buf + bufsz - 1) {
/*
* no nl, no end of input, no buffer space;
* realloc
*/
char *new;
bufsz *= 2;
if (buf == stackbuf) {
new = XMALLOC(MTYPE_TMP, bufsz);
memcpy(new, stackbuf, sizeof(stackbuf));
} else
new = XREALLOC(MTYPE_TMP, buf, bufsz);
bufvalid = bufvalid - buf + new;
buf = new;
/* if end != NULL, we won't be reading more
* data... */
assert(end == NULL);
b = true;
} else
b = true;
if (b)
break;
/* eol is at line end now, either \n => \0 or \0\0\0 */
assert(eol && eol <= bufvalid);
if (vty->of)
vty_out(vty, "%s\n", buf);
callback(cbarg, buf);
/* shift back data and adjust bufvalid */
memmove(buf, eol, bufvalid - eol);
bufvalid -= eol - buf;
if (end)
end -= eol - buf;
}
/* else if no callback, dump raw */
if (!callback) {
if (vty->of)
vty_out(vty, "%s", buf);
memmove(buf, buf + textlen, bufvalid - buf - textlen);
bufvalid -= textlen;
if (end)
end -= textlen;
/*
* ----------------------------------------------------
* At this point `buf` should be in one of two states:
* - Empty (i.e. buf == bufvalid)
* - Contains up to 4 bytes of the terminator
* ----------------------------------------------------
*/
assert(((buf == bufvalid)
|| (bufvalid - buf <= 4 && buf[0] == 0x00)));
}
/* if we have the terminator, break */
if (end && bufvalid - buf == 4) {
assert(!memcmp(buf, terminator, 3));
ret = buf[3];
break;
}
} while (true);
goto out;
out_err:
vclient_close(vclient);
ret = CMD_SUCCESS;
out:
if (buf != stackbuf)
XFREE(MTYPE_TMP, buf);
return ret;
}
static int vtysh_client_run_all(struct vtysh_client *head_client,
const char *line, int continue_on_err,
void (*callback)(void *, const char *),
void *cbarg)
{
struct vtysh_client *client;
int rc, rc_all = CMD_SUCCESS;
int correct_instance = 0, wrong_instance = 0;
for (client = head_client; client; client = client->next) {
rc = vtysh_client_run(client, line, callback, cbarg);
if (rc == CMD_NOT_MY_INSTANCE) {
wrong_instance++;
continue;
}
if (client->fd > 0)
correct_instance++;
if (rc != CMD_SUCCESS) {
if (!continue_on_err)
return rc;
rc_all = rc;
}
}
if (wrong_instance && !correct_instance) {
vty_out(vty,
"%% [%s]: command ignored as it targets an instance that is not running\n",
head_client->name);
rc_all = CMD_WARNING_CONFIG_FAILED;
}
return rc_all;
}
/*
* Execute command against all daemons.
*
* head_client
* where to start walking in the daemon list
*
* line
* the specific command to execute
*
* Returns:
* a status code
*/
static int vtysh_client_execute(struct vtysh_client *head_client,
const char *line)
{
return vtysh_client_run_all(head_client, line, 0, NULL, NULL);
}
/*
* Retrieve all running config from daemons and parse it with the vtysh config
* parser. Returned output is not displayed to the user.
*
* head_client
* where to start walking in the daemon list
*
* line
* the specific command to execute
*/
static void vtysh_client_config(struct vtysh_client *head_client, char *line)
{
/* watchfrr currently doesn't load any config, and has some hardcoded
* settings that show up in "show run". skip it here (for now at
* least) so we don't get that mangled up in config-write.
*/
if (head_client->flag == VTYSH_WATCHFRR)
return;
/* suppress output to user */
vty->of_saved = vty->of;
vty->of = NULL;
vtysh_client_run_all(head_client, line, 1, vtysh_config_parse_line,
NULL);
vty->of = vty->of_saved;
}
/* Command execution over the vty interface. */
static int vtysh_execute_func(const char *line, int pager)
{
int ret, cmd_stat;
unsigned int i;
vector vline;
const struct cmd_element *cmd;
int tried = 0;
int saved_ret, saved_node;
/* Split readline string up into the vector. */
vline = cmd_make_strvec(line);
if (vline == NULL)
return CMD_SUCCESS;
if (user_mode) {
if (strncmp("en", vector_slot(vline, 0), 2) == 0) {
cmd_free_strvec(vline);
vty_out(vty, "%% Command not allowed: enable\n");
return CMD_WARNING;
}
}
saved_ret = ret = cmd_execute(vty, line, &cmd, 1);
saved_node = vty->node;
/*
* If command doesn't succeeded in current node, try to walk up in node
* tree. Changing vty->node is enough to try it just out without actual
* walkup in the vtysh.
*/
while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
&& ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
&& vty->node > CONFIG_NODE) {
vty->node = node_parent(vty->node);
ret = cmd_execute(vty, line, &cmd, 1);
tried++;
}
vty->node = saved_node;
/*
* If command succeeded in any other node than current (tried > 0) we
* have to move into node in the vtysh where it succeeded.
*/
if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
|| ret == CMD_WARNING) {
if ((saved_node == BGP_VPNV4_NODE
|| saved_node == BGP_VPNV6_NODE
|| saved_node == BGP_IPV4_NODE
|| saved_node == BGP_IPV6_NODE
|| saved_node == BGP_FLOWSPECV4_NODE
|| saved_node == BGP_FLOWSPECV6_NODE
|| saved_node == BGP_IPV4M_NODE
|| saved_node == BGP_IPV4L_NODE
|| saved_node == BGP_IPV6L_NODE
|| saved_node == BGP_IPV6M_NODE
|| saved_node == BGP_EVPN_NODE
|| saved_node == LDP_IPV4_NODE
|| saved_node == LDP_IPV6_NODE)
&& (tried == 1)) {
vtysh_execute("exit-address-family");
} else if ((saved_node == BGP_EVPN_VNI_NODE) && (tried == 1)) {
vtysh_execute("exit-vni");
} else if (saved_node == BGP_VRF_POLICY_NODE && (tried == 1)) {
vtysh_execute("exit-vrf-policy");
} else if ((saved_node == BGP_VNC_DEFAULTS_NODE
|| saved_node == BGP_VNC_NVE_GROUP_NODE
|| saved_node == BGP_VNC_L2_GROUP_NODE)
&& (tried == 1)) {
vtysh_execute("exit-vnc");
} else if (saved_node == VRF_NODE && (tried == 1)) {
vtysh_execute("exit-vrf");
} else if ((saved_node == KEYCHAIN_KEY_NODE
|| saved_node == LDP_PSEUDOWIRE_NODE
|| saved_node == LDP_IPV4_IFACE_NODE
|| saved_node == LDP_IPV6_IFACE_NODE)
&& (tried == 1)) {
vtysh_execute("exit");
} else if (tried) {
vtysh_execute("end");
vtysh_execute("configure terminal");
}
}
/*
* If command didn't succeed in any node, continue with return value
* from first try.
*/
else if (tried) {
ret = saved_ret;
}
cmd_free_strvec(vline);
cmd_stat = ret;
switch (ret) {
case CMD_WARNING:
case CMD_WARNING_CONFIG_FAILED:
if (vty->type == VTY_FILE)
vty_out(vty, "Warning...\n");
break;
case CMD_ERR_AMBIGUOUS:
vty_out(vty, "%% Ambiguous command: %s\n", line);
break;
case CMD_ERR_NO_MATCH:
vty_out(vty, "%% Unknown command: %s\n", line);
break;
case CMD_ERR_INCOMPLETE:
vty_out(vty, "%% Command incomplete: %s\n", line);
break;
case CMD_SUCCESS_DAEMON: {
/*
* FIXME: Don't open pager for exit commands. popen() causes
* problems if exited from vtysh at all. This hack shouldn't
* cause any problem but is really ugly.
*/
if (pager && strncmp(line, "exit", 4))
vty_open_pager(vty);
if (!strcmp(cmd->string, "configure terminal")) {
for (i = 0; i < array_size(vtysh_client); i++) {
cmd_stat = vtysh_client_execute(
&vtysh_client[i], line);
if (cmd_stat == CMD_WARNING)
break;
}
if (cmd_stat) {
line = "end";
vline = cmd_make_strvec(line);
if (vline == NULL) {
if (vty->is_paged)
vty_close_pager(vty);
return CMD_SUCCESS;
}
ret = cmd_execute_command(vline, vty, &cmd, 1);
cmd_free_strvec(vline);
if (ret != CMD_SUCCESS_DAEMON)
break;
} else if (cmd->func) {
(*cmd->func)(cmd, vty, 0, NULL);
break;
}
}
cmd_stat = CMD_SUCCESS;
struct vtysh_client *vc;
for (i = 0; i < array_size(vtysh_client); i++) {
if (cmd->daemon & vtysh_client[i].flag) {
if (vtysh_client[i].fd < 0
&& (cmd->daemon == vtysh_client[i].flag)) {
for (vc = &vtysh_client[i]; vc;
vc = vc->next)
if (vc->fd == VTYSH_WAS_ACTIVE)
vtysh_reconnect(vc);
}
if (vtysh_client[i].fd < 0
&& (cmd->daemon == vtysh_client[i].flag)) {
bool any_inst = false;
for (vc = &vtysh_client[i]; vc;
vc = vc->next)
any_inst = any_inst
|| (vc->fd > 0);
if (!any_inst) {
fprintf(stderr,
"%s is not running\n",
vtysh_client[i].name);
continue;
}
}
cmd_stat = vtysh_client_execute(
&vtysh_client[i], line);
if (cmd_stat != CMD_SUCCESS)
break;
}
}
if (cmd_stat != CMD_SUCCESS)
break;
if (cmd->func)
(*cmd->func)(cmd, vty, 0, NULL);
}
}
if (vty->is_paged)
vty_close_pager(vty);
return cmd_stat;
}
int vtysh_execute_no_pager(const char *line)
{
return vtysh_execute_func(line, 0);
}
int vtysh_execute(const char *line)
{
return vtysh_execute_func(line, 1);
}
static char *trim(char *s)
{
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace((int)*end))
end--;
*(end + 1) = '\0';
while (*s && isspace((int)*s))
s++;
return s;
}
int vtysh_mark_file(const char *filename)
{
struct vty *vty;
FILE *confp = NULL;
int ret;
vector vline;
int tried = 0;
const struct cmd_element *cmd;
int saved_ret, prev_node;
int lineno = 0;
char *vty_buf_copy = NULL;
char *vty_buf_trimmed = NULL;
if (strncmp("-", filename, 1) == 0)
confp = stdin;
else
confp = fopen(filename, "r");
if (confp == NULL) {
fprintf(stderr, "%% Can't open config file %s due to '%s'.\n",
filename, safe_strerror(errno));
return (CMD_ERR_NO_FILE);
}
vty = vty_new();
vty->wfd = STDERR_FILENO;
vty->type = VTY_TERM;
vty->node = CONFIG_NODE;
vtysh_execute_no_pager("enable");
vtysh_execute_no_pager("configure terminal");
vty_buf_copy = XCALLOC(MTYPE_VTYSH_CMD, VTY_BUFSIZ);
while (fgets(vty->buf, VTY_BUFSIZ, confp)) {
lineno++;
tried = 0;
strcpy(vty_buf_copy, vty->buf);
vty_buf_trimmed = trim(vty_buf_copy);
switch (vty->node) {
case LDP_IPV4_IFACE_NODE:
if (strncmp(vty_buf_copy, " ", 3)) {
vty_out(vty, " end\n");
vty->node = LDP_IPV4_NODE;
}
break;
case LDP_IPV6_IFACE_NODE:
if (strncmp(vty_buf_copy, " ", 3)) {
vty_out(vty, " end\n");
vty->node = LDP_IPV6_NODE;
}
break;
case LDP_PSEUDOWIRE_NODE:
if (strncmp(vty_buf_copy, " ", 2)) {
vty_out(vty, " end\n");
vty->node = LDP_L2VPN_NODE;
}
break;
default:
break;
}
if (vty_buf_trimmed[0] == '!' || vty_buf_trimmed[0] == '#') {
vty_out(vty, "%s", vty->buf);
continue;
}
/* Split readline string up into the vector. */
vline = cmd_make_strvec(vty->buf);
if (vline == NULL) {
vty_out(vty, "%s", vty->buf);
continue;
}
/*
* Ignore the "end" lines, we will generate these where
* appropriate
*/
if (strlen(vty_buf_trimmed) == 3
&& strncmp("end", vty_buf_trimmed, 3) == 0) {
cmd_free_strvec(vline);
continue;
}
prev_node = vty->node;
saved_ret = ret = cmd_execute_command_strict(vline, vty, &cmd);
/*
* If command doesn't succeeded in current node, try to walk up
* in node tree. Changing vty->node is enough to try it just
* out without actual walkup in the vtysh.
*/
while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
&& ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
&& vty->node > CONFIG_NODE) {
vty->node = node_parent(vty->node);
ret = cmd_execute_command_strict(vline, vty, &cmd);
tried++;
}
/*
* If command succeeded in any other node than current (tried >
* 0) we have to move into node in the vtysh where it
* succeeded.
*/
if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
|| ret == CMD_WARNING) {
if ((prev_node == BGP_VPNV4_NODE
|| prev_node == BGP_VPNV6_NODE
|| prev_node == BGP_IPV4_NODE
|| prev_node == BGP_IPV6_NODE
|| prev_node == BGP_FLOWSPECV4_NODE
|| prev_node == BGP_FLOWSPECV6_NODE
|| prev_node == BGP_IPV4L_NODE
|| prev_node == BGP_IPV6L_NODE
|| prev_node == BGP_IPV4M_NODE
|| prev_node == BGP_IPV6M_NODE
|| prev_node == BGP_EVPN_NODE)
&& (tried == 1)) {
vty_out(vty, "exit-address-family\n");
} else if ((prev_node == BGP_EVPN_VNI_NODE)
&& (tried == 1)) {
vty_out(vty, "exit-vni\n");
} else if ((prev_node == KEYCHAIN_KEY_NODE)
&& (tried == 1)) {
vty_out(vty, "exit\n");
} else if (tried) {
vty_out(vty, "end\n");
}
}
/*
* If command didn't succeed in any node, continue with return
* value from first try.
*/
else if (tried) {
ret = saved_ret;
vty->node = prev_node;
}
cmd_free_strvec(vline);
switch (ret) {
case CMD_WARNING:
case CMD_WARNING_CONFIG_FAILED:
if (vty->type == VTY_FILE)
fprintf(stderr, "line %d: Warning...: %s\n",
lineno, vty->buf);
fclose(confp);
vty_close(vty);
XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
return ret;
case CMD_ERR_AMBIGUOUS:
fprintf(stderr, "line %d: %% Ambiguous command: %s\n",
lineno, vty->buf);
fclose(confp);
vty_close(vty);
XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
return CMD_ERR_AMBIGUOUS;
case CMD_ERR_NO_MATCH:
fprintf(stderr, "line %d: %% Unknown command: %s\n",
lineno, vty->buf);
fclose(confp);
vty_close(vty);
XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
return CMD_ERR_NO_MATCH;
case CMD_ERR_INCOMPLETE:
fprintf(stderr, "line %d: %% Command incomplete: %s\n",
lineno, vty->buf);
fclose(confp);
vty_close(vty);
XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
return CMD_ERR_INCOMPLETE;
case CMD_SUCCESS:
vty_out(vty, "%s", vty->buf);
break;
case CMD_SUCCESS_DAEMON: {
int cmd_stat;
vty_out(vty, "%s", vty->buf);
cmd_stat = vtysh_client_execute(&vtysh_client[0],
vty->buf);
if (cmd_stat != CMD_SUCCESS)
break;
if (cmd->func)
(*cmd->func)(cmd, vty, 0, NULL);
}
}
}
/* This is the end */
vty_out(vty, "\nend\n");
vty_close(vty);
XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
if (confp != stdin)
fclose(confp);
return (0);
}
/* Configration make from file. */
int vtysh_config_from_file(struct vty *vty, FILE *fp)
{
int ret;
const struct cmd_element *cmd;
int lineno = 0;
/* once we have an error, we remember & return that */
int retcode = CMD_SUCCESS;
while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
lineno++;
ret = command_config_read_one_line(vty, &cmd, lineno, 1);
switch (ret) {
case CMD_WARNING:
case CMD_WARNING_CONFIG_FAILED:
if (vty->type == VTY_FILE)
fprintf(stderr, "line %d: Warning[%d]...: %s\n",
lineno, vty->node, vty->buf);
retcode = ret;
break;
case CMD_ERR_AMBIGUOUS:
fprintf(stderr,
"line %d: %% Ambiguous command[%d]: %s\n",
lineno, vty->node, vty->buf);
retcode = CMD_ERR_AMBIGUOUS;
break;
case CMD_ERR_NO_MATCH:
fprintf(stderr, "line %d: %% Unknown command[%d]: %s",
lineno, vty->node, vty->buf);
retcode = CMD_ERR_NO_MATCH;
break;
case CMD_ERR_INCOMPLETE:
fprintf(stderr,
"line %d: %% Command incomplete[%d]: %s\n",
lineno, vty->node, vty->buf);
retcode = CMD_ERR_INCOMPLETE;
break;
case CMD_SUCCESS_DAEMON: {
unsigned int i;
int cmd_stat = CMD_SUCCESS;
for (i = 0; i < array_size(vtysh_client); i++) {
if (cmd->daemon & vtysh_client[i].flag) {
cmd_stat = vtysh_client_execute(
&vtysh_client[i], vty->buf);
/*
* CMD_WARNING - Can mean that the
* command was parsed successfully but
* it was already entered in a few
* spots. As such if we receive a
* CMD_WARNING from a daemon we
* shouldn't stop talking to the other
* daemons for the particular command.
*/
if (cmd_stat != CMD_SUCCESS
&& cmd_stat != CMD_WARNING) {
fprintf(stderr,
"line %d: Failure to communicate[%d] to %s, line: %s\n",
lineno, cmd_stat,
vtysh_client[i].name,
vty->buf);
retcode = cmd_stat;
break;
}
}
}
if (cmd_stat != CMD_SUCCESS)
break;
if (cmd->func)
(*cmd->func)(cmd, vty, 0, NULL);
}
}
}
return (retcode);
}
/*
* Function processes cli commands terminated with '?' character when entered
* through either 'vtysh' or 'vtysh -c' interfaces.
*/
static int vtysh_process_questionmark(const char *input, int input_len)
{
int ret, width = 0;
unsigned int i;
vector vline, describe;
struct cmd_token *token;
if (!input)
return 1;
vline = cmd_make_strvec(input);
/* In case of '> ?'. */
if (vline == NULL) {
vline = vector_init(1);
vector_set(vline, NULL);
} else if (input_len && isspace((int)input[input_len - 1]))
vector_set(vline, NULL);
describe = cmd_describe_command(vline, vty, &ret);
/* Ambiguous and no match error. */
switch (ret) {
case CMD_ERR_AMBIGUOUS:
cmd_free_strvec(vline);
vector_free(describe);
vty_out(vty, "%% Ambiguous command.\n");
rl_on_new_line();
return 0;
break;
case CMD_ERR_NO_MATCH:
cmd_free_strvec(vline);
if (describe)
vector_free(describe);
vty_out(vty, "%% There is no matched command.\n");
rl_on_new_line();
return 0;
break;
}
/* Get width of command string. */
width = 0;
for (i = 0; i < vector_active(describe); i++)
if ((token = vector_slot(describe, i)) != NULL) {
if (token->text[0] == '\0')
continue;
int len = strlen(token->text);
if (width < len)
width = len;
}
for (i = 0; i < vector_active(describe); i++)
if ((token = vector_slot(describe, i)) != NULL) {
if (!token->desc)
vty_out(vty, " %-s\n", token->text);
else
vty_out(vty, " %-*s %s\n", width, token->text,
token->desc);
if (IS_VARYING_TOKEN(token->type)) {
const char *ref = vector_slot(
vline, vector_active(vline) - 1);
vector varcomps = vector_init(VECTOR_MIN_SIZE);
cmd_variable_complete(token, ref, varcomps);
if (vector_active(varcomps) > 0) {