]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh.c
2003-08-11 Taisuke Sasaki <sasaki@soft.net.fujitsu.co.jp>
[mirror_frr.git] / vtysh / vtysh.c
CommitLineData
718e3744 1/* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include <sys/un.h>
25#include <setjmp.h>
26#include <sys/wait.h>
27#include <sys/resource.h>
28#include <sys/stat.h>
29
30#include <readline/readline.h>
31#include <readline/history.h>
32
33#include "command.h"
34#include "memory.h"
35#include "vtysh/vtysh.h"
36
37/* Struct VTY. */
38struct vty *vty;
39
40/* VTY shell pager name. */
41char *vtysh_pager_name = NULL;
42
43/* VTY shell client structure. */
44struct vtysh_client
45{
46 int fd;
47} vtysh_client[VTYSH_INDEX_MAX];
48\f
49/* When '^Z' is received from vty, move down to the enable mode. */
50int
51vtysh_end ()
52{
53 switch (vty->node)
54 {
55 case VIEW_NODE:
56 case ENABLE_NODE:
57 /* Nothing to do. */
58 break;
59 default:
60 vty->node = ENABLE_NODE;
61 break;
62 }
63 return CMD_SUCCESS;
64}
65
66DEFUNSH (VTYSH_ALL,
67 vtysh_end_all,
68 vtysh_end_all_cmd,
69 "end",
70 "End current mode and down to previous mode\n")
71{
72 return vtysh_end (vty);
73}
74
75DEFUNSH (VTYSH_ALL,
76 vtysh_log_stdout,
77 vtysh_log_stdout_cmd,
78 "log stdout",
79 "Logging control\n"
80 "Logging goes to stdout\n")
81{
82 return CMD_SUCCESS;
83}
84
85DEFUNSH (VTYSH_ALL,
86 no_vtysh_log_stdout,
87 no_vtysh_log_stdout_cmd,
88 "no log stdout",
89 NO_STR
90 "Logging control\n"
91 "Logging goes to stdout\n")
92{
93 return CMD_SUCCESS;
94}
95
96DEFUNSH (VTYSH_ALL,
97 vtysh_log_file,
98 vtysh_log_file_cmd,
99 "log file FILENAME",
100 "Logging control\n"
101 "Logging to file\n"
102 "Logging filename\n")
103{
104 return CMD_SUCCESS;
105}
106
107DEFUNSH (VTYSH_ALL,
108 no_vtysh_log_file,
109 no_vtysh_log_file_cmd,
110 "no log file [FILENAME]",
111 NO_STR
112 "Logging control\n"
113 "Cancel logging to file\n"
114 "Logging file name\n")
115{
116 return CMD_SUCCESS;
117}
118
119DEFUNSH (VTYSH_ALL,
120 vtysh_log_syslog,
121 vtysh_log_syslog_cmd,
122 "log syslog",
123 "Logging control\n"
124 "Logging goes to syslog\n")
125{
126 return CMD_SUCCESS;
127}
128
129DEFUNSH (VTYSH_ALL,
130 no_vtysh_log_syslog,
131 no_vtysh_log_syslog_cmd,
132 "no log syslog",
133 NO_STR
134 "Logging control\n"
135 "Cancel logging to syslog\n")
136{
137 return CMD_SUCCESS;
138}
139
140DEFUNSH (VTYSH_ALL,
141 vtysh_log_trap,
142 vtysh_log_trap_cmd,
143 "log trap (emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)",
144 "Logging control\n"
145 "Limit logging to specifed level\n")
146{
147 return CMD_SUCCESS;
148}
149
150DEFUNSH (VTYSH_ALL,
151 no_vtysh_log_trap,
152 no_vtysh_log_trap_cmd,
153 "no log trap",
154 NO_STR
155 "Logging control\n"
156 "Permit all logging information\n")
157{
158 return CMD_SUCCESS;
159}
160
161DEFUNSH (VTYSH_ALL,
162 vtysh_log_record_priority,
163 vtysh_log_record_priority_cmd,
164 "log record-priority",
165 "Logging control\n"
166 "Log the priority of the message within the message\n")
167{
168 return CMD_SUCCESS;
169}
170
171DEFUNSH (VTYSH_ALL,
172 no_vtysh_log_record_priority,
173 no_vtysh_log_record_priority_cmd,
174 "no log record-priority",
175 NO_STR
176 "Logging control\n"
177 "Do not log the priority of the message within the message\n")
178{
179 return CMD_SUCCESS;
180}
181
182void
183vclient_close (struct vtysh_client *vclient)
184{
185 if (vclient->fd > 0)
186 close (vclient->fd);
187 vclient->fd = -1;
188}
189
190
191/* Following filled with debug code to trace a problematic condition
192 under load - it SHOULD handle it.
193*/
194#define ERR_WHERE_STRING "vtysh(): vtysh_client_config(): "
195int
196vtysh_client_config (struct vtysh_client *vclient, char *line)
197{
198 int ret;
199 char *buf;
200 size_t bufsz;
201 char *pbuf;
202 size_t left;
203 char *eoln;
204 int nbytes;
205 int i;
206 int readln;
207
208 if (vclient->fd < 0)
209 return CMD_SUCCESS;
210
211 ret = write (vclient->fd, line, strlen (line) + 1);
212 if (ret <= 0)
213 {
214 vclient_close (vclient);
215 return CMD_SUCCESS;
216 }
217
218 /* Allow enough room for buffer to read more than a few pages from socket
219 */
e3d29b5f 220 bufsz = 5 * getpagesize() + 1;
718e3744 221 buf = XMALLOC(MTYPE_TMP, bufsz);
222 memset(buf, 0, bufsz);
223 pbuf = buf;
224
225 while (1)
226 {
227 if (pbuf >= ((buf + bufsz) -1))
228 {
229 fprintf (stderr, ERR_WHERE_STRING \
230 "warning - pbuf beyond buffer end.\n");
231 return CMD_WARNING;
232 }
233
234 readln = (buf + bufsz) - pbuf - 1;
235 nbytes = read (vclient->fd, pbuf, readln);
236
237 if (nbytes <= 0)
238 {
239
240 if (errno == EINTR)
241 continue;
242
243 fprintf(stderr, ERR_WHERE_STRING "(%u)", errno);
244 perror("");
245
246 if (errno == EAGAIN || errno == EIO)
247 continue;
248
249 vclient_close (vclient);
250 XFREE(MTYPE_TMP, buf);
251 return CMD_SUCCESS;
252 }
253
254 pbuf[nbytes] = '\0';
255
256 if (nbytes >= 4)
257 {
258 i = nbytes - 4;
259 if (pbuf[i] == '\0' && pbuf[i + 1] == '\0' && pbuf[i + 2] == '\0')
260 {
261 ret = pbuf[i + 3];
262 break;
263 }
264 }
265 pbuf += nbytes;
266
267 /* See if a line exists in buffer, if so parse and consume it, and
268 reset read position */
269 if ((eoln = strrchr(buf, '\n')) == NULL)
270 continue;
271
272 if (eoln >= ((buf + bufsz) - 1))
273 {
274 fprintf (stderr, ERR_WHERE_STRING \
275 "warning - eoln beyond buffer end.\n");
276 }
277 vtysh_config_parse(buf);
278
279 eoln++;
280 left = (size_t)(buf + bufsz - eoln);
281 memmove(buf, eoln, left);
282 buf[bufsz-1] = '\0';
283 pbuf = buf + strlen(buf);
284 }
285
286 /* parse anything left in the buffer */
287 vtysh_config_parse (buf);
288
289 XFREE(MTYPE_TMP, buf);
290 return ret;
291}
292
293int
294vtysh_client_execute (struct vtysh_client *vclient, char *line, FILE *fp)
295{
296 int ret;
297 char buf[1001];
298 int nbytes;
299 int i;
300
301 if (vclient->fd < 0)
302 return CMD_SUCCESS;
303
304 ret = write (vclient->fd, line, strlen (line) + 1);
305 if (ret <= 0)
306 {
307 vclient_close (vclient);
308 return CMD_SUCCESS;
309 }
310
311 while (1)
312 {
313 nbytes = read (vclient->fd, buf, sizeof(buf)-1);
314
315 if (nbytes <= 0 && errno != EINTR)
316 {
317 vclient_close (vclient);
318 return CMD_SUCCESS;
319 }
320
321 if (nbytes > 0)
322 {
323 buf[nbytes] = '\0';
324 fprintf (fp, "%s", buf);
325 fflush (fp);
326
327 if (nbytes >= 4)
328 {
329 i = nbytes - 4;
330 if (buf[i] == '\0' && buf[i + 1] == '\0' && buf[i + 2] == '\0')
331 {
332 ret = buf[i + 3];
333 break;
334 }
335 }
336 }
337 }
338 return ret;
339}
340
341void
342vtysh_exit_ripd_only ()
343{
344 vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], "exit", stdout);
345}
346
347
348void
349vtysh_pager_init ()
350{
351 vtysh_pager_name = getenv ("VTYSH_PAGER");
a805cc2d 352 if (! vtysh_pager_name)
353 vtysh_pager_name = getenv ("PAGER");
718e3744 354 if (! vtysh_pager_name)
355 vtysh_pager_name = "more";
356}
357
358/* Command execution over the vty interface. */
359void
360vtysh_execute_func (char *line, int pager)
361{
362 int ret, cmd_stat;
363 vector vline;
364 struct cmd_element *cmd;
365 FILE *fp = NULL;
a805cc2d 366 int closepager=0;
718e3744 367
368 /* Split readline string up into the vector */
369 vline = cmd_make_strvec (line);
370
371 if (vline == NULL)
372 return;
373
374 ret = cmd_execute_command (vline, vty, &cmd);
375
376 cmd_free_strvec (vline);
377
378 switch (ret)
379 {
380 case CMD_WARNING:
381 if (vty->type == VTY_FILE)
4fc01e67 382 fprintf (stdout,"Warning...\n");
718e3744 383 break;
384 case CMD_ERR_AMBIGUOUS:
4fc01e67 385 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 386 break;
387 case CMD_ERR_NO_MATCH:
4fc01e67 388 fprintf (stdout,"%% Unknown command.\n");
718e3744 389 break;
390 case CMD_ERR_INCOMPLETE:
4fc01e67 391 fprintf (stdout,"%% Command incomplete.\n");
718e3744 392 break;
393 case CMD_SUCCESS_DAEMON:
394 {
395 if (pager && vtysh_pager_name)
396 {
4fc01e67 397 fp = popen (vtysh_pager_name, "w");
718e3744 398 if (fp == NULL)
399 {
a805cc2d 400 perror ("popen failed for pager");
401 fp = stdout;
718e3744 402 }
a805cc2d 403 else
404 closepager=1;
718e3744 405 }
406 else
407 fp = stdout;
408
409 if (! strcmp(cmd->string,"configure terminal"))
410 {
411 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
412 line, fp);
413 if (cmd_stat != CMD_WARNING)
414 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
415 line, fp);
416 if (cmd_stat != CMD_WARNING)
417 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp);
418 if (cmd_stat != CMD_WARNING)
419 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
420 line, fp);
421 if (cmd_stat != CMD_WARNING)
422 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp);
423 if (cmd_stat != CMD_WARNING)
424 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
425 line, fp);
426 if (cmd_stat)
427 {
428 line = "end";
429 vline = cmd_make_strvec (line);
430
431 if (vline == NULL)
432 {
a805cc2d 433 if (pager && vtysh_pager_name && fp && closepager)
718e3744 434 {
435 if (pclose (fp) == -1)
436 {
a805cc2d 437 perror ("pclose failed for pager");
718e3744 438 }
439 fp = NULL;
440 }
441 return;
442 }
443
444 ret = cmd_execute_command (vline, vty, &cmd);
445 cmd_free_strvec (vline);
446 if (ret != CMD_SUCCESS_DAEMON)
447 break;
448 }
449 else
450 if (cmd->func)
451 {
452 (*cmd->func) (cmd, vty, 0, NULL);
453 break;
454 }
455 }
456
457 if (cmd->daemon & VTYSH_ZEBRA)
458 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, fp)
459 != CMD_SUCCESS)
460 break;
461 if (cmd->daemon & VTYSH_RIPD)
462 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, fp)
463 != CMD_SUCCESS)
464 break;
465 if (cmd->daemon & VTYSH_RIPNGD)
466 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp)
467 != CMD_SUCCESS)
468 break;
469 if (cmd->daemon & VTYSH_OSPFD)
470 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, fp)
471 != CMD_SUCCESS)
472 break;
473 if (cmd->daemon & VTYSH_OSPF6D)
474 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp)
475 != CMD_SUCCESS)
476 break;
477 if (cmd->daemon & VTYSH_BGPD)
478 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, fp)
479 != CMD_SUCCESS)
480 break;
481 if (cmd->func)
482 (*cmd->func) (cmd, vty, 0, NULL);
483 }
484 }
a805cc2d 485 if (pager && vtysh_pager_name && fp && closepager)
718e3744 486 {
487 if (pclose (fp) == -1)
488 {
a805cc2d 489 perror ("pclose failed for pager");
718e3744 490 }
491 fp = NULL;
492 }
493}
494
495void
496vtysh_execute_no_pager (char *line)
497{
498 vtysh_execute_func (line, 0);
499}
500
501void
502vtysh_execute (char *line)
503{
504 vtysh_execute_func (line, 1);
505}
506
507/* Configration make from file. */
508int
509vtysh_config_from_file (struct vty *vty, FILE *fp)
510{
511 int ret;
512 vector vline;
513 struct cmd_element *cmd;
514
515 while (fgets (vty->buf, VTY_BUFSIZ, fp))
516 {
517 if (vty->buf[0] == '!' || vty->buf[1] == '#')
518 continue;
519
520 vline = cmd_make_strvec (vty->buf);
521
522 /* In case of comment line */
523 if (vline == NULL)
524 continue;
525
526 /* Execute configuration command : this is strict match */
527 ret = cmd_execute_command_strict (vline, vty, &cmd);
528
529 /* Try again with setting node to CONFIG_NODE */
530 if (ret != CMD_SUCCESS
531 && ret != CMD_SUCCESS_DAEMON
532 && ret != CMD_WARNING)
533 {
534 if (vty->node == KEYCHAIN_KEY_NODE)
535 {
536 vty->node = KEYCHAIN_NODE;
537 vtysh_exit_ripd_only ();
538 ret = cmd_execute_command_strict (vline, vty, &cmd);
539
540 if (ret != CMD_SUCCESS
541 && ret != CMD_SUCCESS_DAEMON
542 && ret != CMD_WARNING)
543 {
544 vtysh_exit_ripd_only ();
545 vty->node = CONFIG_NODE;
546 ret = cmd_execute_command_strict (vline, vty, &cmd);
547 }
548 }
549 else
550 {
551 vtysh_execute ("end");
552 vtysh_execute ("configure terminal");
553 vty->node = CONFIG_NODE;
554 ret = cmd_execute_command_strict (vline, vty, &cmd);
555 }
556 }
557
558 cmd_free_strvec (vline);
559
560 switch (ret)
561 {
562 case CMD_WARNING:
563 if (vty->type == VTY_FILE)
4fc01e67 564 fprintf (stdout,"Warning...\n");
718e3744 565 break;
566 case CMD_ERR_AMBIGUOUS:
4fc01e67 567 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 568 break;
569 case CMD_ERR_NO_MATCH:
4fc01e67 570 fprintf (stdout,"%% Unknown command: %s", vty->buf);
718e3744 571 break;
572 case CMD_ERR_INCOMPLETE:
4fc01e67 573 fprintf (stdout,"%% Command incomplete.\n");
718e3744 574 break;
575 case CMD_SUCCESS_DAEMON:
576 {
577 if (cmd->daemon & VTYSH_ZEBRA)
578 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
579 vty->buf, stdout) != CMD_SUCCESS)
580 break;
581 if (cmd->daemon & VTYSH_RIPD)
582 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
583 vty->buf, stdout) != CMD_SUCCESS)
584 break;
585 if (cmd->daemon & VTYSH_RIPNGD)
586 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
587 vty->buf, stdout) != CMD_SUCCESS)
588 break;
589 if (cmd->daemon & VTYSH_OSPFD)
590 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
591 vty->buf, stdout) != CMD_SUCCESS)
592 break;
593 if (cmd->daemon & VTYSH_OSPF6D)
594 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
595 vty->buf, stdout) != CMD_SUCCESS)
596 break;
597 if (cmd->daemon & VTYSH_BGPD)
598 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
599 vty->buf, stdout) != CMD_SUCCESS)
600 break;
601 if (cmd->func)
602 (*cmd->func) (cmd, vty, 0, NULL);
603 }
604 }
605 }
606 return CMD_SUCCESS;
607}
608
609/* We don't care about the point of the cursor when '?' is typed. */
610int
611vtysh_rl_describe ()
612{
613 int ret;
614 int i;
615 vector vline;
616 vector describe;
617 int width;
618 struct desc *desc;
619
620 vline = cmd_make_strvec (rl_line_buffer);
621
622 /* In case of '> ?'. */
623 if (vline == NULL)
624 {
625 vline = vector_init (1);
626 vector_set (vline, '\0');
627 }
628 else
629 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
630 vector_set (vline, '\0');
631
632 describe = cmd_describe_command (vline, vty, &ret);
633
4fc01e67 634 fprintf (stdout,"\n");
718e3744 635
636 /* Ambiguous and no match error. */
637 switch (ret)
638 {
639 case CMD_ERR_AMBIGUOUS:
640 cmd_free_strvec (vline);
4fc01e67 641 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 642 rl_on_new_line ();
643 return 0;
644 break;
645 case CMD_ERR_NO_MATCH:
646 cmd_free_strvec (vline);
4fc01e67 647 fprintf (stdout,"%% There is no matched command.\n");
718e3744 648 rl_on_new_line ();
649 return 0;
650 break;
651 }
652
653 /* Get width of command string. */
654 width = 0;
655 for (i = 0; i < vector_max (describe); i++)
656 if ((desc = vector_slot (describe, i)) != NULL)
657 {
658 int len;
659
660 if (desc->cmd[0] == '\0')
661 continue;
662
663 len = strlen (desc->cmd);
664 if (desc->cmd[0] == '.')
665 len--;
666
667 if (width < len)
668 width = len;
669 }
670
671 for (i = 0; i < vector_max (describe); i++)
672 if ((desc = vector_slot (describe, i)) != NULL)
673 {
674 if (desc->cmd[0] == '\0')
675 continue;
676
677 if (! desc->str)
4fc01e67 678 fprintf (stdout," %-s\n",
718e3744 679 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
680 else
4fc01e67 681 fprintf (stdout," %-*s %s\n",
718e3744 682 width,
683 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
684 desc->str);
685 }
686
687 cmd_free_strvec (vline);
688 vector_free (describe);
689
690 rl_on_new_line();
691
692 return 0;
693}
694
695/* result of cmd_complete_command() call will be stored here
696 and used in new_completion() in order to put the space in
697 correct places only */
698int complete_status;
699
700char *
dfc0d9ba 701command_generator (const char *text, int state)
718e3744 702{
703 vector vline;
704 static char **matched = NULL;
705 static int index = 0;
706
707 /* First call. */
708 if (! state)
709 {
710 index = 0;
711
712 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
713 return NULL;
714
715 vline = cmd_make_strvec (rl_line_buffer);
716 if (vline == NULL)
717 return NULL;
718
719 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
720 vector_set (vline, '\0');
721
722 matched = cmd_complete_command (vline, vty, &complete_status);
723 }
724
725 if (matched && matched[index])
726 return matched[index++];
727
728 return NULL;
729}
730
731char **
732new_completion (char *text, int start, int end)
733{
734 char **matches;
735
dfc0d9ba 736 matches = rl_completion_matches (text, command_generator);
718e3744 737
738 if (matches)
739 {
740 rl_point = rl_end;
741 if (complete_status == CMD_COMPLETE_FULL_MATCH)
742 rl_pending_input = ' ';
743 }
744
745 return matches;
746}
747
748char **
749vtysh_completion (char *text, int start, int end)
750{
751 int ret;
752 vector vline;
753 char **matched = NULL;
754
755 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
756 return NULL;
757
758 vline = cmd_make_strvec (rl_line_buffer);
759 if (vline == NULL)
760 return NULL;
761
762 /* In case of 'help \t'. */
763 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
764 vector_set (vline, '\0');
765
766 matched = cmd_complete_command (vline, vty, &ret);
767
768 cmd_free_strvec (vline);
769
770 return (char **) matched;
771}
772
773/* BGP node structure. */
774struct cmd_node bgp_node =
775{
776 BGP_NODE,
777 "%s(config-router)# ",
778};
779
780/* BGP node structure. */
781struct cmd_node rip_node =
782{
783 RIP_NODE,
784 "%s(config-router)# ",
785};
786
787struct cmd_node interface_node =
788{
789 INTERFACE_NODE,
790 "%s(config-if)# ",
791};
792
793DEFUNSH (VTYSH_BGPD,
794 router_bgp,
795 router_bgp_cmd,
796 "router bgp <1-65535>",
797 ROUTER_STR
798 BGP_STR
799 AS_STR)
800{
801 vty->node = BGP_NODE;
802 return CMD_SUCCESS;
803}
804
805DEFUNSH (VTYSH_BGPD,
806 address_family_vpnv4,
807 address_family_vpnv4_cmd,
808 "address-family vpnv4",
809 "Enter Address Family command mode\n"
810 "Address family\n")
811{
812 vty->node = BGP_VPNV4_NODE;
813 return CMD_SUCCESS;
814}
815
816DEFUNSH (VTYSH_BGPD,
817 address_family_vpnv4_unicast,
818 address_family_vpnv4_unicast_cmd,
819 "address-family vpnv4 unicast",
820 "Enter Address Family command mode\n"
821 "Address family\n"
822 "Address Family Modifier\n")
823{
824 vty->node = BGP_VPNV4_NODE;
825 return CMD_SUCCESS;
826}
827
828DEFUNSH (VTYSH_BGPD,
829 address_family_ipv4_unicast,
830 address_family_ipv4_unicast_cmd,
831 "address-family ipv4 unicast",
832 "Enter Address Family command mode\n"
833 "Address family\n"
834 "Address Family Modifier\n")
835{
836 vty->node = BGP_IPV4_NODE;
837 return CMD_SUCCESS;
838}
839
840DEFUNSH (VTYSH_BGPD,
841 address_family_ipv4_multicast,
842 address_family_ipv4_multicast_cmd,
843 "address-family ipv4 multicast",
844 "Enter Address Family command mode\n"
845 "Address family\n"
846 "Address Family Modifier\n")
847{
848 vty->node = BGP_IPV4M_NODE;
849 return CMD_SUCCESS;
850}
851
852DEFUNSH (VTYSH_BGPD,
853 address_family_ipv6,
854 address_family_ipv6_cmd,
855 "address-family ipv6",
856 "Enter Address Family command mode\n"
857 "Address family\n")
858{
859 vty->node = BGP_IPV6_NODE;
860 return CMD_SUCCESS;
861}
862
863DEFUNSH (VTYSH_BGPD,
864 address_family_ipv6_unicast,
865 address_family_ipv6_unicast_cmd,
866 "address-family ipv6 unicast",
867 "Enter Address Family command mode\n"
868 "Address family\n"
869 "Address Family Modifier\n")
870{
871 vty->node = BGP_IPV6_NODE;
872 return CMD_SUCCESS;
873}
874
875DEFUNSH (VTYSH_RIPD,
876 key_chain,
877 key_chain_cmd,
878 "key chain WORD",
879 "Authentication key management\n"
880 "Key-chain management\n"
881 "Key-chain name\n")
882{
883 vty->node = KEYCHAIN_NODE;
884 return CMD_SUCCESS;
885}
886
887DEFUNSH (VTYSH_RIPD,
888 key,
889 key_cmd,
890 "key <0-2147483647>",
891 "Configure a key\n"
892 "Key identifier number\n")
893{
894 vty->node = KEYCHAIN_KEY_NODE;
895 return CMD_SUCCESS;
896}
897
898DEFUNSH (VTYSH_RIPD,
899 router_rip,
900 router_rip_cmd,
901 "router rip",
902 ROUTER_STR
903 "RIP")
904{
905 vty->node = RIP_NODE;
906 return CMD_SUCCESS;
907}
908
909DEFUNSH (VTYSH_RIPNGD,
910 router_ripng,
911 router_ripng_cmd,
912 "router ripng",
913 ROUTER_STR
914 "RIPng")
915{
916 vty->node = RIPNG_NODE;
917 return CMD_SUCCESS;
918}
919
920DEFUNSH (VTYSH_OSPFD,
921 router_ospf,
922 router_ospf_cmd,
923 "router ospf",
924 "Enable a routing process\n"
925 "Start OSPF configuration\n")
926{
927 vty->node = OSPF_NODE;
928 return CMD_SUCCESS;
929}
930
931DEFUNSH (VTYSH_OSPF6D,
932 router_ospf6,
933 router_ospf6_cmd,
934 "router ospf6",
935 OSPF6_ROUTER_STR
936 OSPF6_STR)
937{
938 vty->node = OSPF6_NODE;
939 return CMD_SUCCESS;
940}
941
942DEFUNSH (VTYSH_RMAP,
943 route_map,
944 route_map_cmd,
945 "route-map WORD (deny|permit) <1-65535>",
946 "Create route-map or enter route-map command mode\n"
947 "Route map tag\n"
948 "Route map denies set operations\n"
949 "Route map permits set operations\n"
950 "Sequence to insert to/delete from existing route-map entry\n")
951{
952 vty->node = RMAP_NODE;
953 return CMD_SUCCESS;
954}
955
956/* Enable command */
957DEFUNSH (VTYSH_ALL,
958 vtysh_enable,
959 vtysh_enable_cmd,
960 "enable",
961 "Turn on privileged mode command\n")
962{
963 vty->node = ENABLE_NODE;
964 return CMD_SUCCESS;
965}
966
967/* Disable command */
968DEFUNSH (VTYSH_ALL,
969 vtysh_disable,
970 vtysh_disable_cmd,
971 "disable",
972 "Turn off privileged mode command\n")
973{
974 if (vty->node == ENABLE_NODE)
975 vty->node = VIEW_NODE;
976 return CMD_SUCCESS;
977}
978
979/* Configration from terminal */
980DEFUNSH (VTYSH_ALL,
981 vtysh_config_terminal,
982 vtysh_config_terminal_cmd,
983 "configure terminal",
984 "Configuration from vty interface\n"
985 "Configuration terminal\n")
986{
987 vty->node = CONFIG_NODE;
988 return CMD_SUCCESS;
989}
990
991int
992vtysh_exit (struct vty *vty)
993{
994 switch (vty->node)
995 {
996 case VIEW_NODE:
997 case ENABLE_NODE:
998 exit (0);
999 break;
1000 case CONFIG_NODE:
1001 vty->node = ENABLE_NODE;
1002 break;
1003 case INTERFACE_NODE:
1004 case ZEBRA_NODE:
1005 case BGP_NODE:
1006 case RIP_NODE:
1007 case RIPNG_NODE:
1008 case OSPF_NODE:
1009 case OSPF6_NODE:
1010 case MASC_NODE:
1011 case RMAP_NODE:
1012 case VTY_NODE:
1013 case KEYCHAIN_NODE:
1014 vty->node = CONFIG_NODE;
1015 break;
1016 case BGP_VPNV4_NODE:
1017 case BGP_IPV4_NODE:
1018 case BGP_IPV4M_NODE:
1019 case BGP_IPV6_NODE:
1020 vty->node = BGP_NODE;
1021 break;
1022 case KEYCHAIN_KEY_NODE:
1023 vty->node = KEYCHAIN_NODE;
1024 break;
1025 default:
1026 break;
1027 }
1028 return CMD_SUCCESS;
1029}
1030
1031DEFUNSH (VTYSH_ALL,
1032 vtysh_exit_all,
1033 vtysh_exit_all_cmd,
1034 "exit",
1035 "Exit current mode and down to previous mode\n")
1036{
1037 return vtysh_exit (vty);
1038}
1039
1040ALIAS (vtysh_exit_all,
1041 vtysh_quit_all_cmd,
1042 "quit",
1043 "Exit current mode and down to previous mode\n")
1044
1045DEFUNSH (VTYSH_BGPD,
1046 exit_address_family,
1047 exit_address_family_cmd,
1048 "exit-address-family",
1049 "Exit from Address Family configuration mode\n")
1050{
1051 if (vty->node == BGP_IPV4_NODE
1052 || vty->node == BGP_IPV4M_NODE
1053 || vty->node == BGP_VPNV4_NODE
1054 || vty->node == BGP_IPV6_NODE)
1055 vty->node = BGP_NODE;
1056 return CMD_SUCCESS;
1057}
1058
1059DEFUNSH (VTYSH_ZEBRA,
1060 vtysh_exit_zebra,
1061 vtysh_exit_zebra_cmd,
1062 "exit",
1063 "Exit current mode and down to previous mode\n")
1064{
1065 return vtysh_exit (vty);
1066}
1067
1068ALIAS (vtysh_exit_zebra,
1069 vtysh_quit_zebra_cmd,
1070 "quit",
1071 "Exit current mode and down to previous mode\n")
1072
1073DEFUNSH (VTYSH_RIPD,
1074 vtysh_exit_ripd,
1075 vtysh_exit_ripd_cmd,
1076 "exit",
1077 "Exit current mode and down to previous mode\n")
1078{
1079 return vtysh_exit (vty);
1080}
1081
1082ALIAS (vtysh_exit_ripd,
1083 vtysh_quit_ripd_cmd,
1084 "quit",
1085 "Exit current mode and down to previous mode\n")
1086
68980084 1087DEFUNSH (VTYSH_RIPNGD,
1088 vtysh_exit_ripngd,
1089 vtysh_exit_ripngd_cmd,
1090 "exit",
1091 "Exit current mode and down to previous mode\n")
1092{
1093 return vtysh_exit (vty);
1094}
1095
1096ALIAS (vtysh_exit_ripngd,
1097 vtysh_quit_ripngd_cmd,
1098 "quit",
1099 "Exit current mode and down to previous mode\n")
1100
718e3744 1101DEFUNSH (VTYSH_RMAP,
1102 vtysh_exit_rmap,
1103 vtysh_exit_rmap_cmd,
1104 "exit",
1105 "Exit current mode and down to previous mode\n")
1106{
1107 return vtysh_exit (vty);
1108}
1109
1110ALIAS (vtysh_exit_rmap,
1111 vtysh_quit_rmap_cmd,
1112 "quit",
1113 "Exit current mode and down to previous mode\n")
1114
1115DEFUNSH (VTYSH_BGPD,
1116 vtysh_exit_bgpd,
1117 vtysh_exit_bgpd_cmd,
1118 "exit",
1119 "Exit current mode and down to previous mode\n")
1120{
1121 return vtysh_exit (vty);
1122}
1123
1124ALIAS (vtysh_exit_bgpd,
1125 vtysh_quit_bgpd_cmd,
1126 "quit",
1127 "Exit current mode and down to previous mode\n")
1128
1129DEFUNSH (VTYSH_OSPFD,
1130 vtysh_exit_ospfd,
1131 vtysh_exit_ospfd_cmd,
1132 "exit",
1133 "Exit current mode and down to previous mode\n")
1134{
1135 return vtysh_exit (vty);
1136}
1137
1138ALIAS (vtysh_exit_ospfd,
1139 vtysh_quit_ospfd_cmd,
1140 "quit",
1141 "Exit current mode and down to previous mode\n")
1142
68980084 1143DEFUNSH (VTYSH_OSPF6D,
1144 vtysh_exit_ospf6d,
1145 vtysh_exit_ospf6d_cmd,
1146 "exit",
1147 "Exit current mode and down to previous mode\n")
1148{
1149 return vtysh_exit (vty);
1150}
1151
1152ALIAS (vtysh_exit_ospf6d,
1153 vtysh_quit_ospf6d_cmd,
1154 "quit",
1155 "Exit current mode and down to previous mode\n")
1156
111bd7a5 1157DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
718e3744 1158 vtysh_interface,
1159 vtysh_interface_cmd,
1160 "interface IFNAME",
1161 "Select an interface to configure\n"
1162 "Interface's name\n")
1163{
1164 vty->node = INTERFACE_NODE;
1165 return CMD_SUCCESS;
1166}
1167
32d2463c 1168DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
1169 vtysh_no_interface_cmd,
1170 "no interface IFNAME",
1171 NO_STR
1172 "Delete a pseudo interface's configuration\n"
1173 "Interface's name\n")
1174
338a9916 1175DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1176 interface_desc_cmd,
1177 "description .LINE",
1178 "Interface specific description\n"
1179 "Characters describing this interface\n")
464dc8da 1180
1181DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1182 no_interface_desc_cmd,
1183 "no description",
1184 NO_STR
1185 "Interface specific description\n")
338a9916 1186
111bd7a5 1187DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
718e3744 1188 vtysh_exit_interface,
1189 vtysh_exit_interface_cmd,
1190 "exit",
1191 "Exit current mode and down to previous mode\n")
1192{
1193 return vtysh_exit (vty);
1194}
1195
1196ALIAS (vtysh_exit_interface,
1197 vtysh_quit_interface_cmd,
1198 "quit",
1199 "Exit current mode and down to previous mode\n")
1200
1201DEFUN (vtysh_write_terminal,
1202 vtysh_write_terminal_cmd,
1203 "write terminal",
1204 "Write running configuration to memory, network, or terminal\n"
1205 "Write to terminal\n")
1206{
1207 int ret;
1208 char line[] = "write terminal\n";
1209 FILE *fp = NULL;
1210
1211 if (vtysh_pager_name)
1212 {
4fc01e67 1213 fp = popen (vtysh_pager_name, "w");
718e3744 1214 if (fp == NULL)
1215 {
1216 perror ("popen");
1217 exit (1);
1218 }
1219 }
1220 else
1221 fp = stdout;
1222
1223 vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
1224 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
1225 VTY_NEWLINE);
1226
1227 vtysh_config_write (fp);
1228
1229 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1230 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1231 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1232 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1233 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1234 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
1235
1236 vtysh_config_dump (fp);
1237
1238 if (vtysh_pager_name && fp)
1239 {
1240 fflush (fp);
1241 if (pclose (fp) == -1)
1242 {
1243 perror ("pclose");
1244 exit (1);
1245 }
1246 fp = NULL;
1247 }
1248
1249 return CMD_SUCCESS;
1250}
1251
4fc01e67 1252struct vtysh_writeconfig_t {
1253 int daemon;
1254 int integrated;
1255} vtysh_wc = {-1,0};
1256
1257DEFUN (vtysh_write_config,
1258 vtysh_write_config_cmd,
1259 "write-config (daemon|integrated)",
1260 "Specify config files to write to\n"
1261 "Write per daemon file\n"
1262 "Write integrated file\n"
1263)
1264{
1265 if (!strncmp(argv[0],"d",1)) {
1266 vtysh_wc.daemon = 1;
1267 } else if (!strncmp(argv[0],"i",1)) {
1268 vtysh_wc.integrated = 1;
1269 }
1270 return CMD_SUCCESS;
1271}
1272
1273DEFUN (no_vtysh_write_config,
1274 no_vtysh_write_config_cmd,
1275 "no write-config (daemon|integrated)",
1276 "Negate per daemon and/or integrated config files\n"
1277)
1278{
1279 if (!strncmp(argv[0],"d",1)) {
1280 vtysh_wc.daemon = 0;
1281 } else if (!strncmp(argv[0],"i",1)) {
1282 vtysh_wc.integrated = 0;
1283 }
1284 return CMD_SUCCESS;
1285}
1286
1287int write_config_integrated(void)
718e3744 1288{
1289 int ret;
1290 mode_t old_umask;
1291 char line[] = "write terminal\n";
1292 FILE *fp;
1293 char *integrate_sav = NULL;
1294
1295 /* config files have 0600 perms... */
1296 old_umask = umask (0077);
1297
1298 integrate_sav = malloc (strlen (integrate_default)
1299 + strlen (CONF_BACKUP_EXT) + 1);
1300 strcpy (integrate_sav, integrate_default);
1301 strcat (integrate_sav, CONF_BACKUP_EXT);
1302
1303
4fc01e67 1304 fprintf (stdout,"Building Configuration...\n");
718e3744 1305
1306 /* Move current configuration file to backup config file */
1307 unlink (integrate_sav);
1308 rename (integrate_default, integrate_sav);
5087df56 1309 free (integrate_sav);
4fc01e67 1310
718e3744 1311 fp = fopen (integrate_default, "w");
1312 if (fp == NULL)
1313 {
4fc01e67 1314 fprintf (stdout,"%% Can't open configuration file %s.\n", integrate_default);
718e3744 1315 umask (old_umask);
1316 return CMD_SUCCESS;
1317 }
718e3744 1318
1319 vtysh_config_write (fp);
1320
1321 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1322 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1323 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1324 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1325 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1326 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
1327
1328 vtysh_config_dump (fp);
1329
1330 fclose (fp);
1331
4fc01e67 1332 fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
1333
1334 fprintf (stdout,"[OK]\n");
1335
718e3744 1336 umask (old_umask);
1337 return CMD_SUCCESS;
1338}
1339
4fc01e67 1340DEFUN (vtysh_write_memory,
1341 vtysh_write_memory_cmd,
1342 "write memory",
1343 "Write running configuration to memory, network, or terminal\n"
1344 "Write configuration to the file (same as write file)\n")
1345{
dfc0d9ba 1346 int ret = CMD_SUCCESS;
4fc01e67 1347 char line[] = "write memory\n";
1348
1349 /* if integrated Zebra.conf explicitely set */
1350 if (vtysh_wc.integrated == 1) {
1351 ret = write_config_integrated();
1352 }
1353
1354 if (!vtysh_wc.daemon) {
1355 return ret;
1356 }
1357
1358 fprintf (stdout,"Building Configuration...\n");
1359
1360 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1361 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1362 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1363 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1364 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1365 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
1366
1367 fprintf (stdout,"[OK]\n");
1368
dfc0d9ba 1369 return ret;
4fc01e67 1370}
1371
718e3744 1372ALIAS (vtysh_write_memory,
1373 vtysh_copy_runningconfig_startupconfig_cmd,
1374 "copy running-config startup-config",
1375 "Copy from one file to another\n"
1376 "Copy from current system configuration\n"
1377 "Copy to startup configuration\n")
1378
1379ALIAS (vtysh_write_memory,
1380 vtysh_write_file_cmd,
1381 "write file",
1382 "Write running configuration to memory, network, or terminal\n"
1383 "Write configuration to the file (same as write memory)\n")
1384
4a6e2257 1385ALIAS (vtysh_write_memory,
1386 vtysh_write_cmd,
1387 "write",
1388 "Write running configuration to memory, network, or terminal\n")
1389
718e3744 1390ALIAS (vtysh_write_terminal,
1391 vtysh_show_running_config_cmd,
1392 "show running-config",
1393 SHOW_STR
1394 "Current operating configuration\n")
1395\f
1396/* Execute command in child process. */
1397int
1398execute_command (char *command, int argc, char *arg1, char *arg2)
1399{
1400 int ret;
1401 pid_t pid;
1402 int status;
1403
1404 /* Call fork(). */
1405 pid = fork ();
1406
1407 if (pid < 0)
1408 {
1409 /* Failure of fork(). */
1410 fprintf (stderr, "Can't fork: %s\n", strerror (errno));
1411 exit (1);
1412 }
1413 else if (pid == 0)
1414 {
1415 /* This is child process. */
1416 switch (argc)
1417 {
1418 case 0:
1419 ret = execlp (command, command, NULL);
1420 break;
1421 case 1:
1422 ret = execlp (command, command, arg1, NULL);
1423 break;
1424 case 2:
1425 ret = execlp (command, command, arg1, arg2, NULL);
1426 break;
1427 }
1428
1429 /* When execlp suceed, this part is not executed. */
1430 fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
1431 exit (1);
1432 }
1433 else
1434 {
1435 /* This is parent. */
1436 execute_flag = 1;
1437 ret = wait4 (pid, &status, 0, NULL);
1438 execute_flag = 0;
1439 }
1440 return 0;
1441}
1442
1443DEFUN (vtysh_ping,
1444 vtysh_ping_cmd,
1445 "ping WORD",
4eeccf18 1446 "Send echo messages\n"
718e3744 1447 "Ping destination address or hostname\n")
1448{
1449 execute_command ("ping", 1, argv[0], NULL);
1450 return CMD_SUCCESS;
1451}
1452
4eeccf18 1453ALIAS (vtysh_ping,
1454 vtysh_ping_ip_cmd,
1455 "ping ip WORD",
1456 "Send echo messages\n"
1457 "IP echo\n"
1458 "Ping destination address or hostname\n")
1459
718e3744 1460DEFUN (vtysh_traceroute,
1461 vtysh_traceroute_cmd,
1462 "traceroute WORD",
1463 "Trace route to destination\n"
1464 "Trace route to destination address or hostname\n")
1465{
1466 execute_command ("traceroute", 1, argv[0], NULL);
1467 return CMD_SUCCESS;
1468}
1469
4eeccf18 1470ALIAS (vtysh_traceroute,
1471 vtysh_traceroute_ip_cmd,
1472 "traceroute ip WORD",
1473 "Trace route to destination\n"
1474 "IP trace\n"
1475 "Trace route to destination address or hostname\n")
1476
1477#ifdef HAVE_IPV6
1478DEFUN (vtysh_ping6,
1479 vtysh_ping6_cmd,
1480 "ping ipv6 WORD",
1481 "Send echo messages\n"
1482 "IPv6 echo\n"
1483 "Ping destination address or hostname\n")
1484{
1485 execute_command ("ping6", 1, argv[0], NULL);
1486 return CMD_SUCCESS;
1487}
1488
1489DEFUN (vtysh_traceroute6,
1490 vtysh_traceroute6_cmd,
1491 "traceroute ipv6 WORD",
1492 "Trace route to destination\n"
1493 "IPv6 trace\n"
1494 "Trace route to destination address or hostname\n")
1495{
1496 execute_command ("traceroute6", 1, argv[0], NULL);
1497 return CMD_SUCCESS;
1498}
1499#endif
1500
718e3744 1501DEFUN (vtysh_telnet,
1502 vtysh_telnet_cmd,
1503 "telnet WORD",
1504 "Open a telnet connection\n"
1505 "IP address or hostname of a remote system\n")
1506{
1507 execute_command ("telnet", 1, argv[0], NULL);
1508 return CMD_SUCCESS;
1509}
1510
1511DEFUN (vtysh_telnet_port,
1512 vtysh_telnet_port_cmd,
1513 "telnet WORD PORT",
1514 "Open a telnet connection\n"
1515 "IP address or hostname of a remote system\n"
1516 "TCP Port number\n")
1517{
1518 execute_command ("telnet", 2, argv[0], argv[1]);
1519 return CMD_SUCCESS;
1520}
1521
5087df56 1522DEFUN (vtysh_ssh,
1523 vtysh_ssh_cmd,
1524 "ssh WORD",
1525 "Open an ssh connection\n"
1526 "[user@]host\n")
1527{
1528 execute_command ("ssh", 1, argv[0], NULL);
1529 return CMD_SUCCESS;
1530}
1531
718e3744 1532DEFUN (vtysh_start_shell,
1533 vtysh_start_shell_cmd,
1534 "start-shell",
1535 "Start UNIX shell\n")
1536{
1537 execute_command ("sh", 0, NULL, NULL);
1538 return CMD_SUCCESS;
1539}
1540
1541DEFUN (vtysh_start_bash,
1542 vtysh_start_bash_cmd,
1543 "start-shell bash",
1544 "Start UNIX shell\n"
1545 "Start bash\n")
1546{
1547 execute_command ("bash", 0, NULL, NULL);
1548 return CMD_SUCCESS;
1549}
1550
1551DEFUN (vtysh_start_zsh,
1552 vtysh_start_zsh_cmd,
1553 "start-shell zsh",
1554 "Start UNIX shell\n"
1555 "Start Z shell\n")
1556{
1557 execute_command ("zsh", 0, NULL, NULL);
1558 return CMD_SUCCESS;
1559}
1560\f
1561/* Route map node structure. */
1562struct cmd_node rmap_node =
1563{
1564 RMAP_NODE,
1565 "%s(config-route-map)# "
1566};
1567
1568/* Zebra node structure. */
1569struct cmd_node zebra_node =
1570{
1571 ZEBRA_NODE,
1572 "%s(config-router)# "
1573};
1574
1575struct cmd_node bgp_vpnv4_node =
1576{
1577 BGP_VPNV4_NODE,
1578 "%s(config-router-af)# "
1579};
1580
1581struct cmd_node bgp_ipv4_node =
1582{
1583 BGP_IPV4_NODE,
1584 "%s(config-router-af)# "
1585};
1586
1587struct cmd_node bgp_ipv4m_node =
1588{
1589 BGP_IPV4M_NODE,
1590 "%s(config-router-af)# "
1591};
1592
1593struct cmd_node bgp_ipv6_node =
1594{
1595 BGP_IPV6_NODE,
1596 "%s(config-router-af)# "
1597};
1598
1599struct cmd_node ospf_node =
1600{
1601 OSPF_NODE,
1602 "%s(config-router)# "
1603};
1604
1605/* RIPng node structure. */
1606struct cmd_node ripng_node =
1607{
1608 RIPNG_NODE,
1609 "%s(config-router)# "
1610};
1611
1612/* OSPF6 node structure. */
1613struct cmd_node ospf6_node =
1614{
1615 OSPF6_NODE,
1616 "%s(config-ospf6)# "
1617};
1618
1619struct cmd_node keychain_node =
1620{
1621 KEYCHAIN_NODE,
1622 "%s(config-keychain)# "
1623};
1624
1625struct cmd_node keychain_key_node =
1626{
1627 KEYCHAIN_KEY_NODE,
1628 "%s(config-keychain-key)# "
1629};
1630\f
1631void
1632vtysh_install_default (enum node_type node)
1633{
1634 install_element (node, &config_list_cmd);
1635}
1636
1637/* Making connection to protocol daemon. */
1638int
1639vtysh_connect (struct vtysh_client *vclient, char *path)
1640{
1641 int ret;
1642 int sock, len;
1643 struct sockaddr_un addr;
1644 struct stat s_stat;
1645 uid_t euid;
1646 gid_t egid;
1647
1648 memset (vclient, 0, sizeof (struct vtysh_client));
1649 vclient->fd = -1;
1650
1651 /* Stat socket to see if we have permission to access it. */
1652 euid = geteuid();
1653 egid = getegid();
1654 ret = stat (path, &s_stat);
1655 if (ret < 0 && errno != ENOENT)
1656 {
1657 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
1658 path, strerror(errno));
1659 exit(1);
1660 }
1661
1662 if (ret >= 0)
1663 {
1664 if (! S_ISSOCK(s_stat.st_mode))
1665 {
1666 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1667 path);
1668 exit (1);
1669 }
1670
718e3744 1671 }
1672
1673 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1674 if (sock < 0)
1675 {
1676#ifdef DEBUG
1677 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path, strerror(errno));
1678#endif /* DEBUG */
1679 return -1;
1680 }
1681
1682 memset (&addr, 0, sizeof (struct sockaddr_un));
1683 addr.sun_family = AF_UNIX;
1684 strncpy (addr.sun_path, path, strlen (path));
1685#ifdef HAVE_SUN_LEN
1686 len = addr.sun_len = SUN_LEN(&addr);
1687#else
1688 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1689#endif /* HAVE_SUN_LEN */
1690
1691 ret = connect (sock, (struct sockaddr *) &addr, len);
1692 if (ret < 0)
1693 {
1694#ifdef DEBUG
1695 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path, strerror(errno));
1696#endif /* DEBUG */
1697 close (sock);
1698 return -1;
1699 }
1700 vclient->fd = sock;
1701
1702 return 0;
1703}
1704
1705void
1706vtysh_connect_all()
1707{
1708 /* Clear each daemons client structure. */
fe067785 1709 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1710 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1711 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1712 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1713 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1714 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
718e3744 1715}
1716
1717
1718/* To disable readline's filename completion */
dfc0d9ba 1719char *
1720vtysh_completion_entry_function (const char *ignore, int invoking_key)
718e3744 1721{
dfc0d9ba 1722 return NULL;
718e3744 1723}
1724
1725void
1726vtysh_readline_init ()
1727{
1728 /* readline related settings. */
1729 rl_bind_key ('?', vtysh_rl_describe);
68980084 1730 rl_completion_entry_function = vtysh_completion_entry_function;
718e3744 1731 rl_attempted_completion_function = (CPPFunction *)new_completion;
1732 /* do not append space after completion. It will be appended
1733 in new_completion() function explicitly */
1734 rl_completion_append_character = '\0';
1735}
1736
1737char *
1738vtysh_prompt ()
1739{
1740 struct utsname names;
1741 static char buf[100];
1742 const char*hostname;
1743 extern struct host host;
1744
1745 hostname = host.name;
1746
1747 if (!hostname)
1748 {
1749 uname (&names);
1750 hostname = names.nodename;
1751 }
1752
1753 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1754
1755 return buf;
1756}
1757
1758void
1759vtysh_init_vty ()
1760{
1761 /* Make vty structure. */
1762 vty = vty_new ();
1763 vty->type = VTY_SHELL;
1764 vty->node = VIEW_NODE;
1765
1766 /* Initialize commands. */
1767 cmd_init (0);
1768
1769 /* Install nodes. */
1770 install_node (&bgp_node, NULL);
1771 install_node (&rip_node, NULL);
1772 install_node (&interface_node, NULL);
1773 install_node (&rmap_node, NULL);
1774 install_node (&zebra_node, NULL);
1775 install_node (&bgp_vpnv4_node, NULL);
1776 install_node (&bgp_ipv4_node, NULL);
1777 install_node (&bgp_ipv4m_node, NULL);
1778/* #ifdef HAVE_IPV6 */
1779 install_node (&bgp_ipv6_node, NULL);
1780/* #endif */
1781 install_node (&ospf_node, NULL);
1782/* #ifdef HAVE_IPV6 */
1783 install_node (&ripng_node, NULL);
1784 install_node (&ospf6_node, NULL);
1785/* #endif */
1786 install_node (&keychain_node, NULL);
1787 install_node (&keychain_key_node, NULL);
1788
1789 vtysh_install_default (VIEW_NODE);
1790 vtysh_install_default (ENABLE_NODE);
1791 vtysh_install_default (CONFIG_NODE);
1792 vtysh_install_default (BGP_NODE);
1793 vtysh_install_default (RIP_NODE);
1794 vtysh_install_default (INTERFACE_NODE);
1795 vtysh_install_default (RMAP_NODE);
1796 vtysh_install_default (ZEBRA_NODE);
1797 vtysh_install_default (BGP_VPNV4_NODE);
1798 vtysh_install_default (BGP_IPV4_NODE);
1799 vtysh_install_default (BGP_IPV4M_NODE);
1800 vtysh_install_default (BGP_IPV6_NODE);
1801 vtysh_install_default (OSPF_NODE);
1802 vtysh_install_default (RIPNG_NODE);
1803 vtysh_install_default (OSPF6_NODE);
1804 vtysh_install_default (KEYCHAIN_NODE);
1805 vtysh_install_default (KEYCHAIN_KEY_NODE);
1806
1807 install_element (VIEW_NODE, &vtysh_enable_cmd);
1808 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
1809 install_element (ENABLE_NODE, &vtysh_disable_cmd);
1810
1811 /* "exit" command. */
1812 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
1813 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
1814 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
1815 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
1816 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
1817 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
1818 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
1819 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
68980084 1820 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
1821 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
718e3744 1822 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
1823 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
68980084 1824 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
1825 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
718e3744 1826 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
1827 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
1828 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
1829 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
1830 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
1831 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
1832 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
1833 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
1834 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
1835 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
1836 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
1837 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
1838 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
1839 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
1840 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
1841 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
1842
1843 /* "end" command. */
1844 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
1845 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
1846 install_element (RIP_NODE, &vtysh_end_all_cmd);
1847 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
1848 install_element (OSPF_NODE, &vtysh_end_all_cmd);
1849 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
1850 install_element (BGP_NODE, &vtysh_end_all_cmd);
1851 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
1852 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
1853 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
1854 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
1855 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
1856 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
1857 install_element (RMAP_NODE, &vtysh_end_all_cmd);
1858
338a9916 1859 install_element (INTERFACE_NODE, &interface_desc_cmd);
464dc8da 1860 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
718e3744 1861 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
1862 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
1863 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
1864 install_element (CONFIG_NODE, &router_rip_cmd);
1865#ifdef HAVE_IPV6
1866 install_element (CONFIG_NODE, &router_ripng_cmd);
1867#endif
1868 install_element (CONFIG_NODE, &router_ospf_cmd);
1869#ifdef HAVE_IPV6
1870 install_element (CONFIG_NODE, &router_ospf6_cmd);
1871#endif
1872 install_element (CONFIG_NODE, &router_bgp_cmd);
1873 install_element (BGP_NODE, &address_family_vpnv4_cmd);
1874 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
1875 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
1876 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
1877#ifdef HAVE_IPV6
1878 install_element (BGP_NODE, &address_family_ipv6_cmd);
1879 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
1880#endif
1881 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
1882 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
1883 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
1884 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
1885 install_element (CONFIG_NODE, &key_chain_cmd);
1886 install_element (CONFIG_NODE, &route_map_cmd);
1887 install_element (KEYCHAIN_NODE, &key_cmd);
1888 install_element (KEYCHAIN_NODE, &key_chain_cmd);
1889 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
1890 install_element (CONFIG_NODE, &vtysh_interface_cmd);
32d2463c 1891 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
718e3744 1892 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
1893 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
1894 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
4a6e2257 1895 install_element (ENABLE_NODE, &vtysh_write_cmd);
718e3744 1896
1897 /* write terminal command */
1898 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
1899 install_element (CONFIG_NODE, &vtysh_write_terminal_cmd);
1900 install_element (BGP_NODE, &vtysh_write_terminal_cmd);
1901 install_element (BGP_VPNV4_NODE, &vtysh_write_terminal_cmd);
1902 install_element (BGP_IPV4_NODE, &vtysh_write_terminal_cmd);
1903 install_element (BGP_IPV4M_NODE, &vtysh_write_terminal_cmd);
1904 install_element (BGP_IPV6_NODE, &vtysh_write_terminal_cmd);
1905 install_element (RIP_NODE, &vtysh_write_terminal_cmd);
1906 install_element (RIPNG_NODE, &vtysh_write_terminal_cmd);
1907 install_element (OSPF_NODE, &vtysh_write_terminal_cmd);
1908 install_element (OSPF6_NODE, &vtysh_write_terminal_cmd);
1909 install_element (INTERFACE_NODE, &vtysh_write_terminal_cmd);
1910 install_element (RMAP_NODE, &vtysh_write_terminal_cmd);
1911 install_element (KEYCHAIN_NODE, &vtysh_write_terminal_cmd);
1912 install_element (KEYCHAIN_KEY_NODE, &vtysh_write_terminal_cmd);
1913
1914 /* write memory command */
1915 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
1916 install_element (CONFIG_NODE, &vtysh_write_memory_cmd);
1917 install_element (BGP_NODE, &vtysh_write_memory_cmd);
1918 install_element (BGP_VPNV4_NODE, &vtysh_write_memory_cmd);
1919 install_element (BGP_IPV4_NODE, &vtysh_write_memory_cmd);
1920 install_element (BGP_IPV4M_NODE, &vtysh_write_memory_cmd);
1921 install_element (BGP_IPV6_NODE, &vtysh_write_memory_cmd);
1922 install_element (RIP_NODE, &vtysh_write_memory_cmd);
1923 install_element (RIPNG_NODE, &vtysh_write_memory_cmd);
1924 install_element (OSPF_NODE, &vtysh_write_memory_cmd);
1925 install_element (OSPF6_NODE, &vtysh_write_memory_cmd);
1926 install_element (INTERFACE_NODE, &vtysh_write_memory_cmd);
1927 install_element (RMAP_NODE, &vtysh_write_memory_cmd);
1928 install_element (KEYCHAIN_NODE, &vtysh_write_memory_cmd);
1929 install_element (KEYCHAIN_KEY_NODE, &vtysh_write_memory_cmd);
1930
1931 install_element (VIEW_NODE, &vtysh_ping_cmd);
4eeccf18 1932 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
718e3744 1933 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
4eeccf18 1934 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
1935#ifdef HAVE_IPV6
1936 install_element (VIEW_NODE, &vtysh_ping6_cmd);
1937 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
1938#endif
718e3744 1939 install_element (VIEW_NODE, &vtysh_telnet_cmd);
1940 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
5087df56 1941 install_element (VIEW_NODE, &vtysh_ssh_cmd);
718e3744 1942 install_element (ENABLE_NODE, &vtysh_ping_cmd);
4eeccf18 1943 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
718e3744 1944 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
4eeccf18 1945 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
1946#ifdef HAVE_IPV6
1947 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
1948 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
1949#endif
718e3744 1950 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
1951 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
1952 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
1953 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
1954 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
1955
718e3744 1956 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
1957 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
1958 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
1959 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
1960 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
1961 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
1962 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
1963 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
1964 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
1965 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
4fc01e67 1966 install_element (CONFIG_NODE, &vtysh_write_config_cmd);
1967 install_element (CONFIG_NODE, &no_vtysh_write_config_cmd);
718e3744 1968}