]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh.c
004-07-13 David Wiggins <dwiggins@bbn.com
[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");
352 if (! vtysh_pager_name)
353 vtysh_pager_name = "more";
354}
355
356/* Command execution over the vty interface. */
357void
358vtysh_execute_func (char *line, int pager)
359{
360 int ret, cmd_stat;
361 vector vline;
362 struct cmd_element *cmd;
363 FILE *fp = NULL;
a805cc2d 364 int closepager=0;
718e3744 365
366 /* Split readline string up into the vector */
367 vline = cmd_make_strvec (line);
368
369 if (vline == NULL)
370 return;
371
372 ret = cmd_execute_command (vline, vty, &cmd);
373
374 cmd_free_strvec (vline);
375
376 switch (ret)
377 {
378 case CMD_WARNING:
379 if (vty->type == VTY_FILE)
4fc01e67 380 fprintf (stdout,"Warning...\n");
718e3744 381 break;
382 case CMD_ERR_AMBIGUOUS:
4fc01e67 383 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 384 break;
385 case CMD_ERR_NO_MATCH:
4fc01e67 386 fprintf (stdout,"%% Unknown command.\n");
718e3744 387 break;
388 case CMD_ERR_INCOMPLETE:
4fc01e67 389 fprintf (stdout,"%% Command incomplete.\n");
718e3744 390 break;
391 case CMD_SUCCESS_DAEMON:
392 {
393 if (pager && vtysh_pager_name)
394 {
4fc01e67 395 fp = popen (vtysh_pager_name, "w");
718e3744 396 if (fp == NULL)
397 {
a805cc2d 398 perror ("popen failed for pager");
399 fp = stdout;
718e3744 400 }
a805cc2d 401 else
402 closepager=1;
718e3744 403 }
404 else
405 fp = stdout;
406
407 if (! strcmp(cmd->string,"configure terminal"))
408 {
409 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
410 line, fp);
411 if (cmd_stat != CMD_WARNING)
412 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
413 line, fp);
414 if (cmd_stat != CMD_WARNING)
415 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp);
416 if (cmd_stat != CMD_WARNING)
417 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
418 line, fp);
419 if (cmd_stat != CMD_WARNING)
420 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp);
421 if (cmd_stat != CMD_WARNING)
422 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
423 line, fp);
c25e458a 424 if (cmd_stat != CMD_WARNING)
425 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, fp);
718e3744 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;
c25e458a 481 if (cmd->daemon & VTYSH_ISISD)
482 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, fp)
483 != CMD_SUCCESS)
484 break;
718e3744 485 if (cmd->func)
486 (*cmd->func) (cmd, vty, 0, NULL);
487 }
488 }
a805cc2d 489 if (pager && vtysh_pager_name && fp && closepager)
718e3744 490 {
491 if (pclose (fp) == -1)
492 {
a805cc2d 493 perror ("pclose failed for pager");
718e3744 494 }
495 fp = NULL;
496 }
497}
498
499void
500vtysh_execute_no_pager (char *line)
501{
502 vtysh_execute_func (line, 0);
503}
504
505void
506vtysh_execute (char *line)
507{
508 vtysh_execute_func (line, 1);
509}
510
511/* Configration make from file. */
512int
513vtysh_config_from_file (struct vty *vty, FILE *fp)
514{
515 int ret;
516 vector vline;
517 struct cmd_element *cmd;
518
519 while (fgets (vty->buf, VTY_BUFSIZ, fp))
520 {
521 if (vty->buf[0] == '!' || vty->buf[1] == '#')
522 continue;
523
524 vline = cmd_make_strvec (vty->buf);
525
526 /* In case of comment line */
527 if (vline == NULL)
528 continue;
529
530 /* Execute configuration command : this is strict match */
531 ret = cmd_execute_command_strict (vline, vty, &cmd);
532
533 /* Try again with setting node to CONFIG_NODE */
534 if (ret != CMD_SUCCESS
535 && ret != CMD_SUCCESS_DAEMON
536 && ret != CMD_WARNING)
537 {
538 if (vty->node == KEYCHAIN_KEY_NODE)
539 {
540 vty->node = KEYCHAIN_NODE;
541 vtysh_exit_ripd_only ();
542 ret = cmd_execute_command_strict (vline, vty, &cmd);
543
544 if (ret != CMD_SUCCESS
545 && ret != CMD_SUCCESS_DAEMON
546 && ret != CMD_WARNING)
547 {
548 vtysh_exit_ripd_only ();
549 vty->node = CONFIG_NODE;
550 ret = cmd_execute_command_strict (vline, vty, &cmd);
551 }
552 }
553 else
554 {
555 vtysh_execute ("end");
556 vtysh_execute ("configure terminal");
557 vty->node = CONFIG_NODE;
558 ret = cmd_execute_command_strict (vline, vty, &cmd);
559 }
560 }
561
562 cmd_free_strvec (vline);
563
564 switch (ret)
565 {
566 case CMD_WARNING:
567 if (vty->type == VTY_FILE)
4fc01e67 568 fprintf (stdout,"Warning...\n");
718e3744 569 break;
570 case CMD_ERR_AMBIGUOUS:
4fc01e67 571 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 572 break;
573 case CMD_ERR_NO_MATCH:
4fc01e67 574 fprintf (stdout,"%% Unknown command: %s", vty->buf);
718e3744 575 break;
576 case CMD_ERR_INCOMPLETE:
4fc01e67 577 fprintf (stdout,"%% Command incomplete.\n");
718e3744 578 break;
579 case CMD_SUCCESS_DAEMON:
580 {
581 if (cmd->daemon & VTYSH_ZEBRA)
582 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
583 vty->buf, stdout) != CMD_SUCCESS)
584 break;
585 if (cmd->daemon & VTYSH_RIPD)
586 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
587 vty->buf, stdout) != CMD_SUCCESS)
588 break;
589 if (cmd->daemon & VTYSH_RIPNGD)
590 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
591 vty->buf, stdout) != CMD_SUCCESS)
592 break;
593 if (cmd->daemon & VTYSH_OSPFD)
594 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
595 vty->buf, stdout) != CMD_SUCCESS)
596 break;
597 if (cmd->daemon & VTYSH_OSPF6D)
598 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
599 vty->buf, stdout) != CMD_SUCCESS)
600 break;
601 if (cmd->daemon & VTYSH_BGPD)
602 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
603 vty->buf, stdout) != CMD_SUCCESS)
604 break;
c25e458a 605 if (cmd->daemon & VTYSH_ISISD)
606 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
607 vty->buf, stdout) != CMD_SUCCESS)
608 break;
718e3744 609 if (cmd->func)
610 (*cmd->func) (cmd, vty, 0, NULL);
611 }
612 }
613 }
614 return CMD_SUCCESS;
615}
616
617/* We don't care about the point of the cursor when '?' is typed. */
618int
619vtysh_rl_describe ()
620{
621 int ret;
622 int i;
623 vector vline;
624 vector describe;
625 int width;
626 struct desc *desc;
627
628 vline = cmd_make_strvec (rl_line_buffer);
629
630 /* In case of '> ?'. */
631 if (vline == NULL)
632 {
633 vline = vector_init (1);
634 vector_set (vline, '\0');
635 }
636 else
637 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
638 vector_set (vline, '\0');
639
640 describe = cmd_describe_command (vline, vty, &ret);
641
4fc01e67 642 fprintf (stdout,"\n");
718e3744 643
644 /* Ambiguous and no match error. */
645 switch (ret)
646 {
647 case CMD_ERR_AMBIGUOUS:
648 cmd_free_strvec (vline);
4fc01e67 649 fprintf (stdout,"%% Ambiguous command.\n");
718e3744 650 rl_on_new_line ();
651 return 0;
652 break;
653 case CMD_ERR_NO_MATCH:
654 cmd_free_strvec (vline);
4fc01e67 655 fprintf (stdout,"%% There is no matched command.\n");
718e3744 656 rl_on_new_line ();
657 return 0;
658 break;
659 }
660
661 /* Get width of command string. */
662 width = 0;
663 for (i = 0; i < vector_max (describe); i++)
664 if ((desc = vector_slot (describe, i)) != NULL)
665 {
666 int len;
667
668 if (desc->cmd[0] == '\0')
669 continue;
670
671 len = strlen (desc->cmd);
672 if (desc->cmd[0] == '.')
673 len--;
674
675 if (width < len)
676 width = len;
677 }
678
679 for (i = 0; i < vector_max (describe); i++)
680 if ((desc = vector_slot (describe, i)) != NULL)
681 {
682 if (desc->cmd[0] == '\0')
683 continue;
684
685 if (! desc->str)
4fc01e67 686 fprintf (stdout," %-s\n",
718e3744 687 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
688 else
4fc01e67 689 fprintf (stdout," %-*s %s\n",
718e3744 690 width,
691 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
692 desc->str);
693 }
694
695 cmd_free_strvec (vline);
696 vector_free (describe);
697
698 rl_on_new_line();
699
700 return 0;
701}
702
703/* result of cmd_complete_command() call will be stored here
704 and used in new_completion() in order to put the space in
705 correct places only */
706int complete_status;
707
708char *
dfc0d9ba 709command_generator (const char *text, int state)
718e3744 710{
711 vector vline;
712 static char **matched = NULL;
713 static int index = 0;
714
715 /* First call. */
716 if (! state)
717 {
718 index = 0;
719
720 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
721 return NULL;
722
723 vline = cmd_make_strvec (rl_line_buffer);
724 if (vline == NULL)
725 return NULL;
726
727 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
728 vector_set (vline, '\0');
729
730 matched = cmd_complete_command (vline, vty, &complete_status);
731 }
732
733 if (matched && matched[index])
734 return matched[index++];
735
736 return NULL;
737}
738
739char **
740new_completion (char *text, int start, int end)
741{
742 char **matches;
743
dfc0d9ba 744 matches = rl_completion_matches (text, command_generator);
718e3744 745
746 if (matches)
747 {
748 rl_point = rl_end;
749 if (complete_status == CMD_COMPLETE_FULL_MATCH)
750 rl_pending_input = ' ';
751 }
752
753 return matches;
754}
755
756char **
757vtysh_completion (char *text, int start, int end)
758{
759 int ret;
760 vector vline;
761 char **matched = NULL;
762
763 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
764 return NULL;
765
766 vline = cmd_make_strvec (rl_line_buffer);
767 if (vline == NULL)
768 return NULL;
769
770 /* In case of 'help \t'. */
771 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
772 vector_set (vline, '\0');
773
774 matched = cmd_complete_command (vline, vty, &ret);
775
776 cmd_free_strvec (vline);
777
778 return (char **) matched;
779}
780
781/* BGP node structure. */
782struct cmd_node bgp_node =
783{
784 BGP_NODE,
785 "%s(config-router)# ",
786};
787
788/* BGP node structure. */
789struct cmd_node rip_node =
790{
791 RIP_NODE,
792 "%s(config-router)# ",
793};
794
c25e458a 795/* ISIS node structure. */
796struct cmd_node isis_node =
797{
798 ISIS_NODE,
799 "%s(config-router)# ",
800 1
801};
802
718e3744 803struct cmd_node interface_node =
804{
805 INTERFACE_NODE,
806 "%s(config-if)# ",
807};
808
809DEFUNSH (VTYSH_BGPD,
810 router_bgp,
811 router_bgp_cmd,
812 "router bgp <1-65535>",
813 ROUTER_STR
814 BGP_STR
815 AS_STR)
816{
817 vty->node = BGP_NODE;
818 return CMD_SUCCESS;
819}
820
821DEFUNSH (VTYSH_BGPD,
822 address_family_vpnv4,
823 address_family_vpnv4_cmd,
824 "address-family vpnv4",
825 "Enter Address Family command mode\n"
826 "Address family\n")
827{
828 vty->node = BGP_VPNV4_NODE;
829 return CMD_SUCCESS;
830}
831
832DEFUNSH (VTYSH_BGPD,
833 address_family_vpnv4_unicast,
834 address_family_vpnv4_unicast_cmd,
835 "address-family vpnv4 unicast",
836 "Enter Address Family command mode\n"
837 "Address family\n"
838 "Address Family Modifier\n")
839{
840 vty->node = BGP_VPNV4_NODE;
841 return CMD_SUCCESS;
842}
843
844DEFUNSH (VTYSH_BGPD,
845 address_family_ipv4_unicast,
846 address_family_ipv4_unicast_cmd,
847 "address-family ipv4 unicast",
848 "Enter Address Family command mode\n"
849 "Address family\n"
850 "Address Family Modifier\n")
851{
852 vty->node = BGP_IPV4_NODE;
853 return CMD_SUCCESS;
854}
855
856DEFUNSH (VTYSH_BGPD,
857 address_family_ipv4_multicast,
858 address_family_ipv4_multicast_cmd,
859 "address-family ipv4 multicast",
860 "Enter Address Family command mode\n"
861 "Address family\n"
862 "Address Family Modifier\n")
863{
864 vty->node = BGP_IPV4M_NODE;
865 return CMD_SUCCESS;
866}
867
868DEFUNSH (VTYSH_BGPD,
869 address_family_ipv6,
870 address_family_ipv6_cmd,
871 "address-family ipv6",
872 "Enter Address Family command mode\n"
873 "Address family\n")
874{
875 vty->node = BGP_IPV6_NODE;
876 return CMD_SUCCESS;
877}
878
879DEFUNSH (VTYSH_BGPD,
880 address_family_ipv6_unicast,
881 address_family_ipv6_unicast_cmd,
882 "address-family ipv6 unicast",
883 "Enter Address Family command mode\n"
884 "Address family\n"
885 "Address Family Modifier\n")
886{
887 vty->node = BGP_IPV6_NODE;
888 return CMD_SUCCESS;
889}
890
891DEFUNSH (VTYSH_RIPD,
892 key_chain,
893 key_chain_cmd,
894 "key chain WORD",
895 "Authentication key management\n"
896 "Key-chain management\n"
897 "Key-chain name\n")
898{
899 vty->node = KEYCHAIN_NODE;
900 return CMD_SUCCESS;
901}
902
903DEFUNSH (VTYSH_RIPD,
904 key,
905 key_cmd,
906 "key <0-2147483647>",
907 "Configure a key\n"
908 "Key identifier number\n")
909{
910 vty->node = KEYCHAIN_KEY_NODE;
911 return CMD_SUCCESS;
912}
913
914DEFUNSH (VTYSH_RIPD,
915 router_rip,
916 router_rip_cmd,
917 "router rip",
918 ROUTER_STR
919 "RIP")
920{
921 vty->node = RIP_NODE;
922 return CMD_SUCCESS;
923}
924
925DEFUNSH (VTYSH_RIPNGD,
926 router_ripng,
927 router_ripng_cmd,
928 "router ripng",
929 ROUTER_STR
930 "RIPng")
931{
932 vty->node = RIPNG_NODE;
933 return CMD_SUCCESS;
934}
935
936DEFUNSH (VTYSH_OSPFD,
937 router_ospf,
938 router_ospf_cmd,
939 "router ospf",
940 "Enable a routing process\n"
941 "Start OSPF configuration\n")
942{
943 vty->node = OSPF_NODE;
944 return CMD_SUCCESS;
945}
946
947DEFUNSH (VTYSH_OSPF6D,
948 router_ospf6,
949 router_ospf6_cmd,
950 "router ospf6",
951 OSPF6_ROUTER_STR
952 OSPF6_STR)
953{
954 vty->node = OSPF6_NODE;
955 return CMD_SUCCESS;
956}
957
c25e458a 958DEFUNSH (VTYSH_ISISD,
959 router_isis,
960 router_isis_cmd,
961 "router isis WORD",
962 ROUTER_STR
963 "ISO IS-IS\n"
964 "ISO Routing area tag")
965{
966 vty->node = ISIS_NODE;
967 return CMD_SUCCESS;
968}
969
718e3744 970DEFUNSH (VTYSH_RMAP,
971 route_map,
972 route_map_cmd,
973 "route-map WORD (deny|permit) <1-65535>",
974 "Create route-map or enter route-map command mode\n"
975 "Route map tag\n"
976 "Route map denies set operations\n"
977 "Route map permits set operations\n"
978 "Sequence to insert to/delete from existing route-map entry\n")
979{
980 vty->node = RMAP_NODE;
981 return CMD_SUCCESS;
982}
983
984/* Enable command */
985DEFUNSH (VTYSH_ALL,
986 vtysh_enable,
987 vtysh_enable_cmd,
988 "enable",
989 "Turn on privileged mode command\n")
990{
991 vty->node = ENABLE_NODE;
992 return CMD_SUCCESS;
993}
994
995/* Disable command */
996DEFUNSH (VTYSH_ALL,
997 vtysh_disable,
998 vtysh_disable_cmd,
999 "disable",
1000 "Turn off privileged mode command\n")
1001{
1002 if (vty->node == ENABLE_NODE)
1003 vty->node = VIEW_NODE;
1004 return CMD_SUCCESS;
1005}
1006
1007/* Configration from terminal */
1008DEFUNSH (VTYSH_ALL,
1009 vtysh_config_terminal,
1010 vtysh_config_terminal_cmd,
1011 "configure terminal",
1012 "Configuration from vty interface\n"
1013 "Configuration terminal\n")
1014{
1015 vty->node = CONFIG_NODE;
1016 return CMD_SUCCESS;
1017}
1018
1019int
1020vtysh_exit (struct vty *vty)
1021{
1022 switch (vty->node)
1023 {
1024 case VIEW_NODE:
1025 case ENABLE_NODE:
1026 exit (0);
1027 break;
1028 case CONFIG_NODE:
1029 vty->node = ENABLE_NODE;
1030 break;
1031 case INTERFACE_NODE:
1032 case ZEBRA_NODE:
1033 case BGP_NODE:
1034 case RIP_NODE:
1035 case RIPNG_NODE:
1036 case OSPF_NODE:
1037 case OSPF6_NODE:
c25e458a 1038 case ISIS_NODE:
718e3744 1039 case MASC_NODE:
1040 case RMAP_NODE:
1041 case VTY_NODE:
1042 case KEYCHAIN_NODE:
2a56df97 1043 vtysh_execute("end");
1044 vtysh_execute("configure terminal");
718e3744 1045 vty->node = CONFIG_NODE;
1046 break;
1047 case BGP_VPNV4_NODE:
1048 case BGP_IPV4_NODE:
1049 case BGP_IPV4M_NODE:
1050 case BGP_IPV6_NODE:
1051 vty->node = BGP_NODE;
1052 break;
1053 case KEYCHAIN_KEY_NODE:
1054 vty->node = KEYCHAIN_NODE;
1055 break;
1056 default:
1057 break;
1058 }
1059 return CMD_SUCCESS;
1060}
1061
1062DEFUNSH (VTYSH_ALL,
1063 vtysh_exit_all,
1064 vtysh_exit_all_cmd,
1065 "exit",
1066 "Exit current mode and down to previous mode\n")
1067{
1068 return vtysh_exit (vty);
1069}
1070
1071ALIAS (vtysh_exit_all,
1072 vtysh_quit_all_cmd,
1073 "quit",
1074 "Exit current mode and down to previous mode\n")
1075
1076DEFUNSH (VTYSH_BGPD,
1077 exit_address_family,
1078 exit_address_family_cmd,
1079 "exit-address-family",
1080 "Exit from Address Family configuration mode\n")
1081{
1082 if (vty->node == BGP_IPV4_NODE
1083 || vty->node == BGP_IPV4M_NODE
1084 || vty->node == BGP_VPNV4_NODE
1085 || vty->node == BGP_IPV6_NODE)
1086 vty->node = BGP_NODE;
1087 return CMD_SUCCESS;
1088}
1089
1090DEFUNSH (VTYSH_ZEBRA,
1091 vtysh_exit_zebra,
1092 vtysh_exit_zebra_cmd,
1093 "exit",
1094 "Exit current mode and down to previous mode\n")
1095{
1096 return vtysh_exit (vty);
1097}
1098
1099ALIAS (vtysh_exit_zebra,
1100 vtysh_quit_zebra_cmd,
1101 "quit",
1102 "Exit current mode and down to previous mode\n")
1103
1104DEFUNSH (VTYSH_RIPD,
1105 vtysh_exit_ripd,
1106 vtysh_exit_ripd_cmd,
1107 "exit",
1108 "Exit current mode and down to previous mode\n")
1109{
1110 return vtysh_exit (vty);
1111}
1112
1113ALIAS (vtysh_exit_ripd,
1114 vtysh_quit_ripd_cmd,
1115 "quit",
1116 "Exit current mode and down to previous mode\n")
1117
68980084 1118DEFUNSH (VTYSH_RIPNGD,
1119 vtysh_exit_ripngd,
1120 vtysh_exit_ripngd_cmd,
1121 "exit",
1122 "Exit current mode and down to previous mode\n")
1123{
1124 return vtysh_exit (vty);
1125}
1126
1127ALIAS (vtysh_exit_ripngd,
1128 vtysh_quit_ripngd_cmd,
1129 "quit",
1130 "Exit current mode and down to previous mode\n")
1131
718e3744 1132DEFUNSH (VTYSH_RMAP,
1133 vtysh_exit_rmap,
1134 vtysh_exit_rmap_cmd,
1135 "exit",
1136 "Exit current mode and down to previous mode\n")
1137{
1138 return vtysh_exit (vty);
1139}
1140
1141ALIAS (vtysh_exit_rmap,
1142 vtysh_quit_rmap_cmd,
1143 "quit",
1144 "Exit current mode and down to previous mode\n")
1145
1146DEFUNSH (VTYSH_BGPD,
1147 vtysh_exit_bgpd,
1148 vtysh_exit_bgpd_cmd,
1149 "exit",
1150 "Exit current mode and down to previous mode\n")
1151{
1152 return vtysh_exit (vty);
1153}
1154
1155ALIAS (vtysh_exit_bgpd,
1156 vtysh_quit_bgpd_cmd,
1157 "quit",
1158 "Exit current mode and down to previous mode\n")
1159
1160DEFUNSH (VTYSH_OSPFD,
1161 vtysh_exit_ospfd,
1162 vtysh_exit_ospfd_cmd,
1163 "exit",
1164 "Exit current mode and down to previous mode\n")
1165{
1166 return vtysh_exit (vty);
1167}
1168
1169ALIAS (vtysh_exit_ospfd,
1170 vtysh_quit_ospfd_cmd,
1171 "quit",
1172 "Exit current mode and down to previous mode\n")
1173
68980084 1174DEFUNSH (VTYSH_OSPF6D,
1175 vtysh_exit_ospf6d,
1176 vtysh_exit_ospf6d_cmd,
1177 "exit",
1178 "Exit current mode and down to previous mode\n")
1179{
1180 return vtysh_exit (vty);
1181}
1182
1183ALIAS (vtysh_exit_ospf6d,
1184 vtysh_quit_ospf6d_cmd,
1185 "quit",
1186 "Exit current mode and down to previous mode\n")
1187
c25e458a 1188DEFUNSH (VTYSH_ISISD,
1189 vtysh_exit_isisd,
1190 vtysh_exit_isisd_cmd,
1191 "exit",
1192 "Exit current mode and down to previous mode\n")
1193{
1194 return vtysh_exit (vty);
1195}
1196
1197ALIAS (vtysh_exit_isisd,
1198 vtysh_quit_isisd_cmd,
1199 "quit",
1200 "Exit current mode and down to previous mode\n")
1201
1202DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_ISISD,
718e3744 1203 vtysh_interface,
1204 vtysh_interface_cmd,
1205 "interface IFNAME",
1206 "Select an interface to configure\n"
1207 "Interface's name\n")
1208{
1209 vty->node = INTERFACE_NODE;
1210 return CMD_SUCCESS;
1211}
1212
32d2463c 1213DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
1214 vtysh_no_interface_cmd,
1215 "no interface IFNAME",
1216 NO_STR
1217 "Delete a pseudo interface's configuration\n"
1218 "Interface's name\n")
1219
338a9916 1220DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1221 interface_desc_cmd,
1222 "description .LINE",
1223 "Interface specific description\n"
1224 "Characters describing this interface\n")
464dc8da 1225
1226DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1227 no_interface_desc_cmd,
1228 "no description",
1229 NO_STR
1230 "Interface specific description\n")
338a9916 1231
c25e458a 1232DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D|VTYSH_ISISD,
718e3744 1233 vtysh_exit_interface,
1234 vtysh_exit_interface_cmd,
1235 "exit",
1236 "Exit current mode and down to previous mode\n")
1237{
1238 return vtysh_exit (vty);
1239}
1240
1241ALIAS (vtysh_exit_interface,
1242 vtysh_quit_interface_cmd,
1243 "quit",
1244 "Exit current mode and down to previous mode\n")
1245
1246DEFUN (vtysh_write_terminal,
1247 vtysh_write_terminal_cmd,
1248 "write terminal",
1249 "Write running configuration to memory, network, or terminal\n"
1250 "Write to terminal\n")
1251{
1252 int ret;
1253 char line[] = "write terminal\n";
1254 FILE *fp = NULL;
1255
1256 if (vtysh_pager_name)
1257 {
4fc01e67 1258 fp = popen (vtysh_pager_name, "w");
718e3744 1259 if (fp == NULL)
1260 {
1261 perror ("popen");
1262 exit (1);
1263 }
1264 }
1265 else
1266 fp = stdout;
1267
1268 vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
1269 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
1270 VTY_NEWLINE);
1271
1272 vtysh_config_write (fp);
1273
1274 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1275 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1276 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1277 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1278 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1279 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
c25e458a 1280 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
718e3744 1281
1282 vtysh_config_dump (fp);
1283
1284 if (vtysh_pager_name && fp)
1285 {
1286 fflush (fp);
1287 if (pclose (fp) == -1)
1288 {
1289 perror ("pclose");
1290 exit (1);
1291 }
1292 fp = NULL;
1293 }
1294
1295 return CMD_SUCCESS;
1296}
1297
4fc01e67 1298struct vtysh_writeconfig_t {
1299 int daemon;
1300 int integrated;
1301} vtysh_wc = {-1,0};
1302
1303DEFUN (vtysh_write_config,
1304 vtysh_write_config_cmd,
1305 "write-config (daemon|integrated)",
1306 "Specify config files to write to\n"
1307 "Write per daemon file\n"
1308 "Write integrated file\n"
1309)
1310{
1311 if (!strncmp(argv[0],"d",1)) {
1312 vtysh_wc.daemon = 1;
1313 } else if (!strncmp(argv[0],"i",1)) {
1314 vtysh_wc.integrated = 1;
1315 }
1316 return CMD_SUCCESS;
1317}
1318
1319DEFUN (no_vtysh_write_config,
1320 no_vtysh_write_config_cmd,
1321 "no write-config (daemon|integrated)",
1322 "Negate per daemon and/or integrated config files\n"
1323)
1324{
1325 if (!strncmp(argv[0],"d",1)) {
1326 vtysh_wc.daemon = 0;
1327 } else if (!strncmp(argv[0],"i",1)) {
1328 vtysh_wc.integrated = 0;
1329 }
1330 return CMD_SUCCESS;
1331}
1332
1333int write_config_integrated(void)
718e3744 1334{
1335 int ret;
718e3744 1336 char line[] = "write terminal\n";
1337 FILE *fp;
1338 char *integrate_sav = NULL;
1339
718e3744 1340 integrate_sav = malloc (strlen (integrate_default)
1341 + strlen (CONF_BACKUP_EXT) + 1);
1342 strcpy (integrate_sav, integrate_default);
1343 strcat (integrate_sav, CONF_BACKUP_EXT);
1344
1345
4fc01e67 1346 fprintf (stdout,"Building Configuration...\n");
718e3744 1347
1348 /* Move current configuration file to backup config file */
1349 unlink (integrate_sav);
1350 rename (integrate_default, integrate_sav);
5087df56 1351 free (integrate_sav);
4fc01e67 1352
718e3744 1353 fp = fopen (integrate_default, "w");
1354 if (fp == NULL)
1355 {
4fc01e67 1356 fprintf (stdout,"%% Can't open configuration file %s.\n", integrate_default);
718e3744 1357 return CMD_SUCCESS;
1358 }
718e3744 1359
1360 vtysh_config_write (fp);
1361
1362 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1363 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1364 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1365 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1366 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1367 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
c25e458a 1368 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
718e3744 1369
1370 vtysh_config_dump (fp);
1371
1372 fclose (fp);
1373
aa593d5e 1374 if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
1375 {
1376 fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
1377 integrate_default, strerror(errno), errno);
1378 return CMD_WARNING;
1379 }
1380
4fc01e67 1381 fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
1382
1383 fprintf (stdout,"[OK]\n");
1384
718e3744 1385 return CMD_SUCCESS;
1386}
1387
4fc01e67 1388DEFUN (vtysh_write_memory,
1389 vtysh_write_memory_cmd,
1390 "write memory",
1391 "Write running configuration to memory, network, or terminal\n"
1392 "Write configuration to the file (same as write file)\n")
1393{
dfc0d9ba 1394 int ret = CMD_SUCCESS;
4fc01e67 1395 char line[] = "write memory\n";
1396
1397 /* if integrated Zebra.conf explicitely set */
1398 if (vtysh_wc.integrated == 1) {
1399 ret = write_config_integrated();
1400 }
1401
1402 if (!vtysh_wc.daemon) {
1403 return ret;
1404 }
1405
1406 fprintf (stdout,"Building Configuration...\n");
1407
1408 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1409 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1410 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1411 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1412 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1413 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
c25e458a 1414 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, stdout);
4fc01e67 1415
1416 fprintf (stdout,"[OK]\n");
1417
dfc0d9ba 1418 return ret;
4fc01e67 1419}
1420
718e3744 1421ALIAS (vtysh_write_memory,
1422 vtysh_copy_runningconfig_startupconfig_cmd,
1423 "copy running-config startup-config",
1424 "Copy from one file to another\n"
1425 "Copy from current system configuration\n"
1426 "Copy to startup configuration\n")
1427
1428ALIAS (vtysh_write_memory,
1429 vtysh_write_file_cmd,
1430 "write file",
1431 "Write running configuration to memory, network, or terminal\n"
1432 "Write configuration to the file (same as write memory)\n")
1433
4a6e2257 1434ALIAS (vtysh_write_memory,
1435 vtysh_write_cmd,
1436 "write",
1437 "Write running configuration to memory, network, or terminal\n")
1438
718e3744 1439ALIAS (vtysh_write_terminal,
1440 vtysh_show_running_config_cmd,
1441 "show running-config",
1442 SHOW_STR
1443 "Current operating configuration\n")
1444\f
1445/* Execute command in child process. */
1446int
1447execute_command (char *command, int argc, char *arg1, char *arg2)
1448{
1449 int ret;
1450 pid_t pid;
1451 int status;
1452
1453 /* Call fork(). */
1454 pid = fork ();
1455
1456 if (pid < 0)
1457 {
1458 /* Failure of fork(). */
1459 fprintf (stderr, "Can't fork: %s\n", strerror (errno));
1460 exit (1);
1461 }
1462 else if (pid == 0)
1463 {
1464 /* This is child process. */
1465 switch (argc)
1466 {
1467 case 0:
fa2b17e3 1468 ret = execlp (command, command, (const char *)NULL);
718e3744 1469 break;
1470 case 1:
fa2b17e3 1471 ret = execlp (command, command, arg1, (const char *)NULL);
718e3744 1472 break;
1473 case 2:
fa2b17e3 1474 ret = execlp (command, command, arg1, arg2, (const char *)NULL);
718e3744 1475 break;
1476 }
1477
1478 /* When execlp suceed, this part is not executed. */
1479 fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
1480 exit (1);
1481 }
1482 else
1483 {
1484 /* This is parent. */
1485 execute_flag = 1;
1486 ret = wait4 (pid, &status, 0, NULL);
1487 execute_flag = 0;
1488 }
1489 return 0;
1490}
1491
1492DEFUN (vtysh_ping,
1493 vtysh_ping_cmd,
1494 "ping WORD",
4eeccf18 1495 "Send echo messages\n"
718e3744 1496 "Ping destination address or hostname\n")
1497{
1498 execute_command ("ping", 1, argv[0], NULL);
1499 return CMD_SUCCESS;
1500}
1501
4eeccf18 1502ALIAS (vtysh_ping,
1503 vtysh_ping_ip_cmd,
1504 "ping ip WORD",
1505 "Send echo messages\n"
1506 "IP echo\n"
1507 "Ping destination address or hostname\n")
1508
718e3744 1509DEFUN (vtysh_traceroute,
1510 vtysh_traceroute_cmd,
1511 "traceroute WORD",
1512 "Trace route to destination\n"
1513 "Trace route to destination address or hostname\n")
1514{
1515 execute_command ("traceroute", 1, argv[0], NULL);
1516 return CMD_SUCCESS;
1517}
1518
4eeccf18 1519ALIAS (vtysh_traceroute,
1520 vtysh_traceroute_ip_cmd,
1521 "traceroute ip WORD",
1522 "Trace route to destination\n"
1523 "IP trace\n"
1524 "Trace route to destination address or hostname\n")
1525
1526#ifdef HAVE_IPV6
1527DEFUN (vtysh_ping6,
1528 vtysh_ping6_cmd,
1529 "ping ipv6 WORD",
1530 "Send echo messages\n"
1531 "IPv6 echo\n"
1532 "Ping destination address or hostname\n")
1533{
1534 execute_command ("ping6", 1, argv[0], NULL);
1535 return CMD_SUCCESS;
1536}
1537
1538DEFUN (vtysh_traceroute6,
1539 vtysh_traceroute6_cmd,
1540 "traceroute ipv6 WORD",
1541 "Trace route to destination\n"
1542 "IPv6 trace\n"
1543 "Trace route to destination address or hostname\n")
1544{
1545 execute_command ("traceroute6", 1, argv[0], NULL);
1546 return CMD_SUCCESS;
1547}
1548#endif
1549
718e3744 1550DEFUN (vtysh_telnet,
1551 vtysh_telnet_cmd,
1552 "telnet WORD",
1553 "Open a telnet connection\n"
1554 "IP address or hostname of a remote system\n")
1555{
1556 execute_command ("telnet", 1, argv[0], NULL);
1557 return CMD_SUCCESS;
1558}
1559
1560DEFUN (vtysh_telnet_port,
1561 vtysh_telnet_port_cmd,
1562 "telnet WORD PORT",
1563 "Open a telnet connection\n"
1564 "IP address or hostname of a remote system\n"
1565 "TCP Port number\n")
1566{
1567 execute_command ("telnet", 2, argv[0], argv[1]);
1568 return CMD_SUCCESS;
1569}
1570
5087df56 1571DEFUN (vtysh_ssh,
1572 vtysh_ssh_cmd,
1573 "ssh WORD",
1574 "Open an ssh connection\n"
1575 "[user@]host\n")
1576{
1577 execute_command ("ssh", 1, argv[0], NULL);
1578 return CMD_SUCCESS;
1579}
1580
718e3744 1581DEFUN (vtysh_start_shell,
1582 vtysh_start_shell_cmd,
1583 "start-shell",
1584 "Start UNIX shell\n")
1585{
1586 execute_command ("sh", 0, NULL, NULL);
1587 return CMD_SUCCESS;
1588}
1589
1590DEFUN (vtysh_start_bash,
1591 vtysh_start_bash_cmd,
1592 "start-shell bash",
1593 "Start UNIX shell\n"
1594 "Start bash\n")
1595{
1596 execute_command ("bash", 0, NULL, NULL);
1597 return CMD_SUCCESS;
1598}
1599
1600DEFUN (vtysh_start_zsh,
1601 vtysh_start_zsh_cmd,
1602 "start-shell zsh",
1603 "Start UNIX shell\n"
1604 "Start Z shell\n")
1605{
1606 execute_command ("zsh", 0, NULL, NULL);
1607 return CMD_SUCCESS;
1608}
1609\f
1610/* Route map node structure. */
1611struct cmd_node rmap_node =
1612{
1613 RMAP_NODE,
1614 "%s(config-route-map)# "
1615};
1616
1617/* Zebra node structure. */
1618struct cmd_node zebra_node =
1619{
1620 ZEBRA_NODE,
1621 "%s(config-router)# "
1622};
1623
1624struct cmd_node bgp_vpnv4_node =
1625{
1626 BGP_VPNV4_NODE,
1627 "%s(config-router-af)# "
1628};
1629
1630struct cmd_node bgp_ipv4_node =
1631{
1632 BGP_IPV4_NODE,
1633 "%s(config-router-af)# "
1634};
1635
1636struct cmd_node bgp_ipv4m_node =
1637{
1638 BGP_IPV4M_NODE,
1639 "%s(config-router-af)# "
1640};
1641
1642struct cmd_node bgp_ipv6_node =
1643{
1644 BGP_IPV6_NODE,
1645 "%s(config-router-af)# "
1646};
1647
1648struct cmd_node ospf_node =
1649{
1650 OSPF_NODE,
1651 "%s(config-router)# "
1652};
1653
1654/* RIPng node structure. */
1655struct cmd_node ripng_node =
1656{
1657 RIPNG_NODE,
1658 "%s(config-router)# "
1659};
1660
1661/* OSPF6 node structure. */
1662struct cmd_node ospf6_node =
1663{
1664 OSPF6_NODE,
1665 "%s(config-ospf6)# "
1666};
1667
1668struct cmd_node keychain_node =
1669{
1670 KEYCHAIN_NODE,
1671 "%s(config-keychain)# "
1672};
1673
1674struct cmd_node keychain_key_node =
1675{
1676 KEYCHAIN_KEY_NODE,
1677 "%s(config-keychain-key)# "
1678};
1679\f
1680void
1681vtysh_install_default (enum node_type node)
1682{
1683 install_element (node, &config_list_cmd);
1684}
1685
1686/* Making connection to protocol daemon. */
1687int
1688vtysh_connect (struct vtysh_client *vclient, char *path)
1689{
1690 int ret;
1691 int sock, len;
1692 struct sockaddr_un addr;
1693 struct stat s_stat;
1694 uid_t euid;
1695 gid_t egid;
1696
1697 memset (vclient, 0, sizeof (struct vtysh_client));
1698 vclient->fd = -1;
1699
1700 /* Stat socket to see if we have permission to access it. */
1701 euid = geteuid();
1702 egid = getegid();
1703 ret = stat (path, &s_stat);
1704 if (ret < 0 && errno != ENOENT)
1705 {
1706 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
1707 path, strerror(errno));
1708 exit(1);
1709 }
1710
1711 if (ret >= 0)
1712 {
1713 if (! S_ISSOCK(s_stat.st_mode))
1714 {
1715 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1716 path);
1717 exit (1);
1718 }
1719
718e3744 1720 }
1721
1722 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1723 if (sock < 0)
1724 {
1725#ifdef DEBUG
1726 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path, strerror(errno));
1727#endif /* DEBUG */
1728 return -1;
1729 }
1730
1731 memset (&addr, 0, sizeof (struct sockaddr_un));
1732 addr.sun_family = AF_UNIX;
1733 strncpy (addr.sun_path, path, strlen (path));
1734#ifdef HAVE_SUN_LEN
1735 len = addr.sun_len = SUN_LEN(&addr);
1736#else
1737 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1738#endif /* HAVE_SUN_LEN */
1739
1740 ret = connect (sock, (struct sockaddr *) &addr, len);
1741 if (ret < 0)
1742 {
1743#ifdef DEBUG
1744 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path, strerror(errno));
1745#endif /* DEBUG */
1746 close (sock);
1747 return -1;
1748 }
1749 vclient->fd = sock;
1750
1751 return 0;
1752}
1753
1754void
1755vtysh_connect_all()
1756{
1757 /* Clear each daemons client structure. */
fe067785 1758 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1759 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1760 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1761 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1762 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1763 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
c25e458a 1764 vtysh_connect (&vtysh_client[VTYSH_INDEX_ISIS], ISIS_VTYSH_PATH);
718e3744 1765}
1766
1767
1768/* To disable readline's filename completion */
dfc0d9ba 1769char *
1770vtysh_completion_entry_function (const char *ignore, int invoking_key)
718e3744 1771{
dfc0d9ba 1772 return NULL;
718e3744 1773}
1774
1775void
1776vtysh_readline_init ()
1777{
1778 /* readline related settings. */
1779 rl_bind_key ('?', vtysh_rl_describe);
68980084 1780 rl_completion_entry_function = vtysh_completion_entry_function;
718e3744 1781 rl_attempted_completion_function = (CPPFunction *)new_completion;
1782 /* do not append space after completion. It will be appended
1783 in new_completion() function explicitly */
1784 rl_completion_append_character = '\0';
1785}
1786
1787char *
1788vtysh_prompt ()
1789{
1790 struct utsname names;
1791 static char buf[100];
1792 const char*hostname;
1793 extern struct host host;
1794
1795 hostname = host.name;
1796
1797 if (!hostname)
1798 {
1799 uname (&names);
1800 hostname = names.nodename;
1801 }
1802
1803 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1804
1805 return buf;
1806}
1807
1808void
1809vtysh_init_vty ()
1810{
1811 /* Make vty structure. */
1812 vty = vty_new ();
1813 vty->type = VTY_SHELL;
1814 vty->node = VIEW_NODE;
1815
1816 /* Initialize commands. */
1817 cmd_init (0);
1818
1819 /* Install nodes. */
1820 install_node (&bgp_node, NULL);
1821 install_node (&rip_node, NULL);
1822 install_node (&interface_node, NULL);
1823 install_node (&rmap_node, NULL);
1824 install_node (&zebra_node, NULL);
1825 install_node (&bgp_vpnv4_node, NULL);
1826 install_node (&bgp_ipv4_node, NULL);
1827 install_node (&bgp_ipv4m_node, NULL);
1828/* #ifdef HAVE_IPV6 */
1829 install_node (&bgp_ipv6_node, NULL);
1830/* #endif */
1831 install_node (&ospf_node, NULL);
1832/* #ifdef HAVE_IPV6 */
1833 install_node (&ripng_node, NULL);
1834 install_node (&ospf6_node, NULL);
1835/* #endif */
1836 install_node (&keychain_node, NULL);
1837 install_node (&keychain_key_node, NULL);
c25e458a 1838 install_node (&isis_node, NULL);
718e3744 1839
1840 vtysh_install_default (VIEW_NODE);
1841 vtysh_install_default (ENABLE_NODE);
1842 vtysh_install_default (CONFIG_NODE);
1843 vtysh_install_default (BGP_NODE);
1844 vtysh_install_default (RIP_NODE);
1845 vtysh_install_default (INTERFACE_NODE);
1846 vtysh_install_default (RMAP_NODE);
1847 vtysh_install_default (ZEBRA_NODE);
1848 vtysh_install_default (BGP_VPNV4_NODE);
1849 vtysh_install_default (BGP_IPV4_NODE);
1850 vtysh_install_default (BGP_IPV4M_NODE);
1851 vtysh_install_default (BGP_IPV6_NODE);
1852 vtysh_install_default (OSPF_NODE);
1853 vtysh_install_default (RIPNG_NODE);
1854 vtysh_install_default (OSPF6_NODE);
c25e458a 1855 vtysh_install_default (ISIS_NODE);
718e3744 1856 vtysh_install_default (KEYCHAIN_NODE);
1857 vtysh_install_default (KEYCHAIN_KEY_NODE);
1858
1859 install_element (VIEW_NODE, &vtysh_enable_cmd);
1860 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
1861 install_element (ENABLE_NODE, &vtysh_disable_cmd);
1862
1863 /* "exit" command. */
1864 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
1865 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
1866 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
1867 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
1868 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
1869 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
1870 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
1871 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
68980084 1872 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
1873 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
718e3744 1874 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
1875 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
68980084 1876 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
1877 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
718e3744 1878 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
1879 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
1880 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
1881 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
1882 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
1883 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
1884 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
1885 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
1886 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
1887 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
c25e458a 1888 install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
1889 install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
718e3744 1890 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
1891 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
1892 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
1893 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
1894 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
1895 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
1896
1897 /* "end" command. */
1898 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
1899 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
1900 install_element (RIP_NODE, &vtysh_end_all_cmd);
1901 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
1902 install_element (OSPF_NODE, &vtysh_end_all_cmd);
1903 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
1904 install_element (BGP_NODE, &vtysh_end_all_cmd);
1905 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
1906 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
1907 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
1908 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
c25e458a 1909 install_element (ISIS_NODE, &vtysh_end_all_cmd);
718e3744 1910 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
1911 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
1912 install_element (RMAP_NODE, &vtysh_end_all_cmd);
1913
338a9916 1914 install_element (INTERFACE_NODE, &interface_desc_cmd);
464dc8da 1915 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
718e3744 1916 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
1917 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
1918 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
1919 install_element (CONFIG_NODE, &router_rip_cmd);
1920#ifdef HAVE_IPV6
1921 install_element (CONFIG_NODE, &router_ripng_cmd);
1922#endif
1923 install_element (CONFIG_NODE, &router_ospf_cmd);
1924#ifdef HAVE_IPV6
1925 install_element (CONFIG_NODE, &router_ospf6_cmd);
1926#endif
c25e458a 1927 install_element (CONFIG_NODE, &router_isis_cmd);
718e3744 1928 install_element (CONFIG_NODE, &router_bgp_cmd);
1929 install_element (BGP_NODE, &address_family_vpnv4_cmd);
1930 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
1931 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
1932 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
1933#ifdef HAVE_IPV6
1934 install_element (BGP_NODE, &address_family_ipv6_cmd);
1935 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
1936#endif
1937 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
1938 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
1939 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
1940 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
1941 install_element (CONFIG_NODE, &key_chain_cmd);
1942 install_element (CONFIG_NODE, &route_map_cmd);
1943 install_element (KEYCHAIN_NODE, &key_cmd);
1944 install_element (KEYCHAIN_NODE, &key_chain_cmd);
1945 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
1946 install_element (CONFIG_NODE, &vtysh_interface_cmd);
32d2463c 1947 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
718e3744 1948 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
1949 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
1950 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
4a6e2257 1951 install_element (ENABLE_NODE, &vtysh_write_cmd);
718e3744 1952
1953 /* write terminal command */
1954 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
1955 install_element (CONFIG_NODE, &vtysh_write_terminal_cmd);
1956 install_element (BGP_NODE, &vtysh_write_terminal_cmd);
1957 install_element (BGP_VPNV4_NODE, &vtysh_write_terminal_cmd);
1958 install_element (BGP_IPV4_NODE, &vtysh_write_terminal_cmd);
1959 install_element (BGP_IPV4M_NODE, &vtysh_write_terminal_cmd);
1960 install_element (BGP_IPV6_NODE, &vtysh_write_terminal_cmd);
1961 install_element (RIP_NODE, &vtysh_write_terminal_cmd);
1962 install_element (RIPNG_NODE, &vtysh_write_terminal_cmd);
1963 install_element (OSPF_NODE, &vtysh_write_terminal_cmd);
1964 install_element (OSPF6_NODE, &vtysh_write_terminal_cmd);
c25e458a 1965 install_element (ISIS_NODE, &vtysh_write_terminal_cmd);
718e3744 1966 install_element (INTERFACE_NODE, &vtysh_write_terminal_cmd);
1967 install_element (RMAP_NODE, &vtysh_write_terminal_cmd);
1968 install_element (KEYCHAIN_NODE, &vtysh_write_terminal_cmd);
1969 install_element (KEYCHAIN_KEY_NODE, &vtysh_write_terminal_cmd);
1970
1971 /* write memory command */
1972 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
1973 install_element (CONFIG_NODE, &vtysh_write_memory_cmd);
1974 install_element (BGP_NODE, &vtysh_write_memory_cmd);
1975 install_element (BGP_VPNV4_NODE, &vtysh_write_memory_cmd);
1976 install_element (BGP_IPV4_NODE, &vtysh_write_memory_cmd);
1977 install_element (BGP_IPV4M_NODE, &vtysh_write_memory_cmd);
1978 install_element (BGP_IPV6_NODE, &vtysh_write_memory_cmd);
1979 install_element (RIP_NODE, &vtysh_write_memory_cmd);
1980 install_element (RIPNG_NODE, &vtysh_write_memory_cmd);
1981 install_element (OSPF_NODE, &vtysh_write_memory_cmd);
1982 install_element (OSPF6_NODE, &vtysh_write_memory_cmd);
c25e458a 1983 install_element (ISIS_NODE, &vtysh_write_memory_cmd);
718e3744 1984 install_element (INTERFACE_NODE, &vtysh_write_memory_cmd);
1985 install_element (RMAP_NODE, &vtysh_write_memory_cmd);
1986 install_element (KEYCHAIN_NODE, &vtysh_write_memory_cmd);
1987 install_element (KEYCHAIN_KEY_NODE, &vtysh_write_memory_cmd);
1988
1989 install_element (VIEW_NODE, &vtysh_ping_cmd);
4eeccf18 1990 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
718e3744 1991 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
4eeccf18 1992 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
1993#ifdef HAVE_IPV6
1994 install_element (VIEW_NODE, &vtysh_ping6_cmd);
1995 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
1996#endif
718e3744 1997 install_element (VIEW_NODE, &vtysh_telnet_cmd);
1998 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
5087df56 1999 install_element (VIEW_NODE, &vtysh_ssh_cmd);
718e3744 2000 install_element (ENABLE_NODE, &vtysh_ping_cmd);
4eeccf18 2001 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
718e3744 2002 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
4eeccf18 2003 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
2004#ifdef HAVE_IPV6
2005 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
2006 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
2007#endif
718e3744 2008 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
2009 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
2010 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
2011 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
2012 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
2013
718e3744 2014 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
2015 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
2016 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
2017 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
2018 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
2019 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
2020 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
2021 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
2022 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
2023 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
4fc01e67 2024 install_element (CONFIG_NODE, &vtysh_write_config_cmd);
2025 install_element (CONFIG_NODE, &no_vtysh_write_config_cmd);
718e3744 2026}