blob: 79e07420d5d5327608f0fc2ff356fdad0515500b [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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];
hassob094d262004-08-25 12:22:00 +000048
hassoe7168df2004-10-03 20:11:32 +000049/* Using integrated config from Quagga.conf. Default is no. */
50int vtysh_writeconfig_integrated = 0;
51
52extern char config_default[];
53
paul718e3742002-12-13 20:15:29 +000054void
55vclient_close (struct vtysh_client *vclient)
56{
57 if (vclient->fd > 0)
58 close (vclient->fd);
59 vclient->fd = -1;
60}
61
paul718e3742002-12-13 20:15:29 +000062/* Following filled with debug code to trace a problematic condition
hasso95e735b2004-08-26 13:08:30 +000063 * under load - it SHOULD handle it. */
paul718e3742002-12-13 20:15:29 +000064#define ERR_WHERE_STRING "vtysh(): vtysh_client_config(): "
65int
66vtysh_client_config (struct vtysh_client *vclient, char *line)
67{
68 int ret;
69 char *buf;
70 size_t bufsz;
71 char *pbuf;
72 size_t left;
73 char *eoln;
74 int nbytes;
75 int i;
76 int readln;
77
78 if (vclient->fd < 0)
79 return CMD_SUCCESS;
80
81 ret = write (vclient->fd, line, strlen (line) + 1);
82 if (ret <= 0)
83 {
84 vclient_close (vclient);
85 return CMD_SUCCESS;
86 }
87
hasso95e735b2004-08-26 13:08:30 +000088 /* Allow enough room for buffer to read more than a few pages from socket. */
paule3d29b52003-01-23 18:05:42 +000089 bufsz = 5 * getpagesize() + 1;
paul718e3742002-12-13 20:15:29 +000090 buf = XMALLOC(MTYPE_TMP, bufsz);
91 memset(buf, 0, bufsz);
92 pbuf = buf;
93
94 while (1)
95 {
96 if (pbuf >= ((buf + bufsz) -1))
97 {
98 fprintf (stderr, ERR_WHERE_STRING \
99 "warning - pbuf beyond buffer end.\n");
100 return CMD_WARNING;
101 }
102
103 readln = (buf + bufsz) - pbuf - 1;
104 nbytes = read (vclient->fd, pbuf, readln);
105
106 if (nbytes <= 0)
107 {
108
109 if (errno == EINTR)
110 continue;
111
112 fprintf(stderr, ERR_WHERE_STRING "(%u)", errno);
113 perror("");
114
115 if (errno == EAGAIN || errno == EIO)
116 continue;
117
118 vclient_close (vclient);
119 XFREE(MTYPE_TMP, buf);
120 return CMD_SUCCESS;
121 }
122
123 pbuf[nbytes] = '\0';
124
125 if (nbytes >= 4)
126 {
127 i = nbytes - 4;
128 if (pbuf[i] == '\0' && pbuf[i + 1] == '\0' && pbuf[i + 2] == '\0')
129 {
130 ret = pbuf[i + 3];
131 break;
132 }
133 }
134 pbuf += nbytes;
135
136 /* See if a line exists in buffer, if so parse and consume it, and
hasso95e735b2004-08-26 13:08:30 +0000137 * reset read position. */
paul718e3742002-12-13 20:15:29 +0000138 if ((eoln = strrchr(buf, '\n')) == NULL)
139 continue;
140
141 if (eoln >= ((buf + bufsz) - 1))
142 {
143 fprintf (stderr, ERR_WHERE_STRING \
144 "warning - eoln beyond buffer end.\n");
145 }
146 vtysh_config_parse(buf);
147
148 eoln++;
149 left = (size_t)(buf + bufsz - eoln);
150 memmove(buf, eoln, left);
151 buf[bufsz-1] = '\0';
152 pbuf = buf + strlen(buf);
153 }
154
hasso95e735b2004-08-26 13:08:30 +0000155 /* Parse anything left in the buffer. */
hassoe7168df2004-10-03 20:11:32 +0000156
paul718e3742002-12-13 20:15:29 +0000157 vtysh_config_parse (buf);
158
159 XFREE(MTYPE_TMP, buf);
160 return ret;
161}
162
163int
164vtysh_client_execute (struct vtysh_client *vclient, char *line, FILE *fp)
165{
166 int ret;
167 char buf[1001];
168 int nbytes;
paul2852de12004-09-17 06:52:16 +0000169 int i;
170 int numnulls = 0;
paul718e3742002-12-13 20:15:29 +0000171
172 if (vclient->fd < 0)
173 return CMD_SUCCESS;
174
175 ret = write (vclient->fd, line, strlen (line) + 1);
176 if (ret <= 0)
177 {
178 vclient_close (vclient);
179 return CMD_SUCCESS;
180 }
181
182 while (1)
183 {
184 nbytes = read (vclient->fd, buf, sizeof(buf)-1);
185
186 if (nbytes <= 0 && errno != EINTR)
187 {
188 vclient_close (vclient);
189 return CMD_SUCCESS;
190 }
191
192 if (nbytes > 0)
193 {
194 buf[nbytes] = '\0';
195 fprintf (fp, "%s", buf);
196 fflush (fp);
paul2852de12004-09-17 06:52:16 +0000197
198 /* check for trailling \0\0\0\0, even if split across reads */
199 if (nbytes >= 4)
200 {
201 i = nbytes-4;
202 numnulls = 0;
203 }
204 else
205 i = 0;
206
207 while (i < nbytes)
208 {
209 if (buf[i++] == '\0')
210 numnulls++;
211 else
212 {
213 numnulls = 0;
214 break;
215 }
216 }
paul718e3742002-12-13 20:15:29 +0000217
paul2852de12004-09-17 06:52:16 +0000218 /* got 3 or more trailling nulls? */
219 if (numnulls >= 3)
220 return CMD_SUCCESS;
paul718e3742002-12-13 20:15:29 +0000221 }
222 }
223 return ret;
224}
225
226void
227vtysh_exit_ripd_only ()
228{
229 vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], "exit", stdout);
230}
231
232
233void
234vtysh_pager_init ()
235{
hasso5a9c53d2004-08-27 14:23:28 +0000236 char *pager_defined;
237
238 pager_defined = getenv ("VTYSH_PAGER");
239
240 if (pager_defined)
241 vtysh_pager_name = strdup (pager_defined);
242 else
hasso34553cc2004-08-27 13:56:39 +0000243 vtysh_pager_name = strdup ("more");
paul718e3742002-12-13 20:15:29 +0000244}
245
246/* Command execution over the vty interface. */
247void
248vtysh_execute_func (char *line, int pager)
249{
250 int ret, cmd_stat;
251 vector vline;
252 struct cmd_element *cmd;
253 FILE *fp = NULL;
paula805cc22003-05-01 14:29:48 +0000254 int closepager=0;
paul718e3742002-12-13 20:15:29 +0000255
hasso95e735b2004-08-26 13:08:30 +0000256 /* Split readline string up into the vector. */
paul718e3742002-12-13 20:15:29 +0000257 vline = cmd_make_strvec (line);
258
259 if (vline == NULL)
260 return;
261
262 ret = cmd_execute_command (vline, vty, &cmd);
263
264 cmd_free_strvec (vline);
265
266 switch (ret)
267 {
268 case CMD_WARNING:
269 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000270 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000271 break;
272 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000273 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000274 break;
275 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000276 fprintf (stdout,"%% Unknown command.\n");
paul718e3742002-12-13 20:15:29 +0000277 break;
278 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000279 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000280 break;
281 case CMD_SUCCESS_DAEMON:
282 {
283 if (pager && vtysh_pager_name)
284 {
paul4fc01e62002-12-13 20:49:00 +0000285 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +0000286 if (fp == NULL)
287 {
paula805cc22003-05-01 14:29:48 +0000288 perror ("popen failed for pager");
289 fp = stdout;
paul718e3742002-12-13 20:15:29 +0000290 }
paula805cc22003-05-01 14:29:48 +0000291 else
292 closepager=1;
paul718e3742002-12-13 20:15:29 +0000293 }
294 else
295 fp = stdout;
296
297 if (! strcmp(cmd->string,"configure terminal"))
298 {
299 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
300 line, fp);
301 if (cmd_stat != CMD_WARNING)
302 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
303 line, fp);
304 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000305 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
306 line, fp);
paul718e3742002-12-13 20:15:29 +0000307 if (cmd_stat != CMD_WARNING)
308 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
309 line, fp);
310 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000311 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
312 line, fp);
paul718e3742002-12-13 20:15:29 +0000313 if (cmd_stat != CMD_WARNING)
314 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
315 line, fp);
hassob094d262004-08-25 12:22:00 +0000316 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000317 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
318 line, fp);
paul718e3742002-12-13 20:15:29 +0000319 if (cmd_stat)
320 {
hassob094d262004-08-25 12:22:00 +0000321 line = "end";
322 vline = cmd_make_strvec (line);
paul718e3742002-12-13 20:15:29 +0000323
hassob094d262004-08-25 12:22:00 +0000324 if (vline == NULL)
paul718e3742002-12-13 20:15:29 +0000325 {
paula805cc22003-05-01 14:29:48 +0000326 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000327 {
328 if (pclose (fp) == -1)
329 {
paula805cc22003-05-01 14:29:48 +0000330 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000331 }
332 fp = NULL;
333 }
334 return;
335 }
336
hassob094d262004-08-25 12:22:00 +0000337 ret = cmd_execute_command (vline, vty, &cmd);
338 cmd_free_strvec (vline);
339 if (ret != CMD_SUCCESS_DAEMON)
340 break;
paul718e3742002-12-13 20:15:29 +0000341 }
342 else
343 if (cmd->func)
344 {
345 (*cmd->func) (cmd, vty, 0, NULL);
346 break;
hassob094d262004-08-25 12:22:00 +0000347 }
paul718e3742002-12-13 20:15:29 +0000348 }
349
350 if (cmd->daemon & VTYSH_ZEBRA)
351 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, fp)
352 != CMD_SUCCESS)
353 break;
354 if (cmd->daemon & VTYSH_RIPD)
355 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, fp)
356 != CMD_SUCCESS)
357 break;
358 if (cmd->daemon & VTYSH_RIPNGD)
359 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp)
360 != CMD_SUCCESS)
361 break;
362 if (cmd->daemon & VTYSH_OSPFD)
363 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, fp)
364 != CMD_SUCCESS)
365 break;
366 if (cmd->daemon & VTYSH_OSPF6D)
367 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp)
368 != CMD_SUCCESS)
369 break;
370 if (cmd->daemon & VTYSH_BGPD)
371 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, fp)
372 != CMD_SUCCESS)
373 break;
hassob094d262004-08-25 12:22:00 +0000374 if (cmd->daemon & VTYSH_ISISD)
375 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, fp)
376 != CMD_SUCCESS)
377 break;
paul718e3742002-12-13 20:15:29 +0000378 if (cmd->func)
379 (*cmd->func) (cmd, vty, 0, NULL);
380 }
381 }
paula805cc22003-05-01 14:29:48 +0000382 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000383 {
384 if (pclose (fp) == -1)
385 {
paula805cc22003-05-01 14:29:48 +0000386 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000387 }
388 fp = NULL;
389 }
390}
391
392void
393vtysh_execute_no_pager (char *line)
394{
395 vtysh_execute_func (line, 0);
396}
397
398void
399vtysh_execute (char *line)
400{
401 vtysh_execute_func (line, 1);
402}
403
404/* Configration make from file. */
405int
406vtysh_config_from_file (struct vty *vty, FILE *fp)
407{
408 int ret;
409 vector vline;
410 struct cmd_element *cmd;
411
412 while (fgets (vty->buf, VTY_BUFSIZ, fp))
413 {
414 if (vty->buf[0] == '!' || vty->buf[1] == '#')
415 continue;
416
417 vline = cmd_make_strvec (vty->buf);
418
hasso95e735b2004-08-26 13:08:30 +0000419 /* In case of comment line. */
paul718e3742002-12-13 20:15:29 +0000420 if (vline == NULL)
421 continue;
422
hasso95e735b2004-08-26 13:08:30 +0000423 /* Execute configuration command : this is strict match. */
paul718e3742002-12-13 20:15:29 +0000424 ret = cmd_execute_command_strict (vline, vty, &cmd);
425
hasso95e735b2004-08-26 13:08:30 +0000426 /* Try again with setting node to CONFIG_NODE. */
paul718e3742002-12-13 20:15:29 +0000427 if (ret != CMD_SUCCESS
428 && ret != CMD_SUCCESS_DAEMON
429 && ret != CMD_WARNING)
430 {
431 if (vty->node == KEYCHAIN_KEY_NODE)
432 {
433 vty->node = KEYCHAIN_NODE;
434 vtysh_exit_ripd_only ();
435 ret = cmd_execute_command_strict (vline, vty, &cmd);
436
437 if (ret != CMD_SUCCESS
438 && ret != CMD_SUCCESS_DAEMON
439 && ret != CMD_WARNING)
440 {
441 vtysh_exit_ripd_only ();
442 vty->node = CONFIG_NODE;
443 ret = cmd_execute_command_strict (vline, vty, &cmd);
444 }
445 }
446 else
447 {
448 vtysh_execute ("end");
449 vtysh_execute ("configure terminal");
450 vty->node = CONFIG_NODE;
451 ret = cmd_execute_command_strict (vline, vty, &cmd);
452 }
453 }
454
455 cmd_free_strvec (vline);
456
457 switch (ret)
458 {
459 case CMD_WARNING:
460 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000461 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000462 break;
463 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000464 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000465 break;
466 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000467 fprintf (stdout,"%% Unknown command: %s", vty->buf);
paul718e3742002-12-13 20:15:29 +0000468 break;
469 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000470 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000471 break;
472 case CMD_SUCCESS_DAEMON:
473 {
474 if (cmd->daemon & VTYSH_ZEBRA)
475 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
476 vty->buf, stdout) != CMD_SUCCESS)
477 break;
478 if (cmd->daemon & VTYSH_RIPD)
479 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
480 vty->buf, stdout) != CMD_SUCCESS)
481 break;
482 if (cmd->daemon & VTYSH_RIPNGD)
483 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
484 vty->buf, stdout) != CMD_SUCCESS)
485 break;
486 if (cmd->daemon & VTYSH_OSPFD)
487 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
488 vty->buf, stdout) != CMD_SUCCESS)
489 break;
490 if (cmd->daemon & VTYSH_OSPF6D)
491 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
492 vty->buf, stdout) != CMD_SUCCESS)
493 break;
494 if (cmd->daemon & VTYSH_BGPD)
495 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
496 vty->buf, stdout) != CMD_SUCCESS)
497 break;
hassob094d262004-08-25 12:22:00 +0000498 if (cmd->daemon & VTYSH_ISISD)
499 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
500 vty->buf, stdout) != CMD_SUCCESS)
501 break;
paul718e3742002-12-13 20:15:29 +0000502 if (cmd->func)
503 (*cmd->func) (cmd, vty, 0, NULL);
504 }
505 }
506 }
507 return CMD_SUCCESS;
508}
509
510/* We don't care about the point of the cursor when '?' is typed. */
511int
512vtysh_rl_describe ()
513{
514 int ret;
515 int i;
516 vector vline;
517 vector describe;
518 int width;
519 struct desc *desc;
520
521 vline = cmd_make_strvec (rl_line_buffer);
522
523 /* In case of '> ?'. */
524 if (vline == NULL)
525 {
526 vline = vector_init (1);
527 vector_set (vline, '\0');
528 }
529 else
530 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
531 vector_set (vline, '\0');
532
533 describe = cmd_describe_command (vline, vty, &ret);
534
paul4fc01e62002-12-13 20:49:00 +0000535 fprintf (stdout,"\n");
paul718e3742002-12-13 20:15:29 +0000536
537 /* Ambiguous and no match error. */
538 switch (ret)
539 {
540 case CMD_ERR_AMBIGUOUS:
541 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000542 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000543 rl_on_new_line ();
544 return 0;
545 break;
546 case CMD_ERR_NO_MATCH:
547 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000548 fprintf (stdout,"%% There is no matched command.\n");
paul718e3742002-12-13 20:15:29 +0000549 rl_on_new_line ();
550 return 0;
551 break;
552 }
553
554 /* Get width of command string. */
555 width = 0;
556 for (i = 0; i < vector_max (describe); i++)
557 if ((desc = vector_slot (describe, i)) != NULL)
558 {
559 int len;
560
561 if (desc->cmd[0] == '\0')
562 continue;
563
564 len = strlen (desc->cmd);
565 if (desc->cmd[0] == '.')
566 len--;
567
568 if (width < len)
569 width = len;
570 }
571
572 for (i = 0; i < vector_max (describe); i++)
573 if ((desc = vector_slot (describe, i)) != NULL)
574 {
575 if (desc->cmd[0] == '\0')
576 continue;
577
578 if (! desc->str)
paul4fc01e62002-12-13 20:49:00 +0000579 fprintf (stdout," %-s\n",
hassob094d262004-08-25 12:22:00 +0000580 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
paul718e3742002-12-13 20:15:29 +0000581 else
paul4fc01e62002-12-13 20:49:00 +0000582 fprintf (stdout," %-*s %s\n",
hassob094d262004-08-25 12:22:00 +0000583 width,
584 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
585 desc->str);
paul718e3742002-12-13 20:15:29 +0000586 }
587
588 cmd_free_strvec (vline);
589 vector_free (describe);
590
591 rl_on_new_line();
592
593 return 0;
594}
595
hasso95e735b2004-08-26 13:08:30 +0000596/* Result of cmd_complete_command() call will be stored here
597 * and used in new_completion() in order to put the space in
598 * correct places only. */
paul718e3742002-12-13 20:15:29 +0000599int complete_status;
600
601char *
pauldfc0d9b2003-04-18 23:55:29 +0000602command_generator (const char *text, int state)
paul718e3742002-12-13 20:15:29 +0000603{
604 vector vline;
605 static char **matched = NULL;
606 static int index = 0;
607
608 /* First call. */
609 if (! state)
610 {
611 index = 0;
612
613 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
614 return NULL;
615
616 vline = cmd_make_strvec (rl_line_buffer);
617 if (vline == NULL)
618 return NULL;
619
620 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
621 vector_set (vline, '\0');
622
623 matched = cmd_complete_command (vline, vty, &complete_status);
624 }
625
626 if (matched && matched[index])
627 return matched[index++];
628
629 return NULL;
630}
631
632char **
633new_completion (char *text, int start, int end)
634{
635 char **matches;
636
pauldfc0d9b2003-04-18 23:55:29 +0000637 matches = rl_completion_matches (text, command_generator);
paul718e3742002-12-13 20:15:29 +0000638
639 if (matches)
640 {
641 rl_point = rl_end;
642 if (complete_status == CMD_COMPLETE_FULL_MATCH)
643 rl_pending_input = ' ';
644 }
645
646 return matches;
647}
648
649char **
650vtysh_completion (char *text, int start, int end)
651{
652 int ret;
653 vector vline;
654 char **matched = NULL;
655
656 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
657 return NULL;
658
659 vline = cmd_make_strvec (rl_line_buffer);
660 if (vline == NULL)
661 return NULL;
662
663 /* In case of 'help \t'. */
664 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
665 vector_set (vline, '\0');
666
667 matched = cmd_complete_command (vline, vty, &ret);
668
669 cmd_free_strvec (vline);
670
671 return (char **) matched;
672}
673
hasso95e735b2004-08-26 13:08:30 +0000674/* Vty node structures. */
paul718e3742002-12-13 20:15:29 +0000675struct cmd_node bgp_node =
676{
677 BGP_NODE,
678 "%s(config-router)# ",
679};
680
paul718e3742002-12-13 20:15:29 +0000681struct cmd_node rip_node =
682{
683 RIP_NODE,
684 "%s(config-router)# ",
685};
686
hassoc25e4582003-12-23 10:39:08 +0000687struct cmd_node isis_node =
688{
689 ISIS_NODE,
690 "%s(config-router)# ",
hassoc25e4582003-12-23 10:39:08 +0000691};
692
paul718e3742002-12-13 20:15:29 +0000693struct cmd_node interface_node =
694{
695 INTERFACE_NODE,
696 "%s(config-if)# ",
697};
698
hasso95e735b2004-08-26 13:08:30 +0000699struct cmd_node rmap_node =
700{
701 RMAP_NODE,
702 "%s(config-route-map)# "
703};
704
705struct cmd_node zebra_node =
706{
707 ZEBRA_NODE,
708 "%s(config-router)# "
709};
710
711struct cmd_node bgp_vpnv4_node =
712{
713 BGP_VPNV4_NODE,
714 "%s(config-router-af)# "
715};
716
717struct cmd_node bgp_ipv4_node =
718{
719 BGP_IPV4_NODE,
720 "%s(config-router-af)# "
721};
722
723struct cmd_node bgp_ipv4m_node =
724{
725 BGP_IPV4M_NODE,
726 "%s(config-router-af)# "
727};
728
729struct cmd_node bgp_ipv6_node =
730{
731 BGP_IPV6_NODE,
732 "%s(config-router-af)# "
733};
734
735struct cmd_node ospf_node =
736{
737 OSPF_NODE,
738 "%s(config-router)# "
739};
740
741struct cmd_node ripng_node =
742{
743 RIPNG_NODE,
744 "%s(config-router)# "
745};
746
747struct cmd_node ospf6_node =
748{
749 OSPF6_NODE,
750 "%s(config-ospf6)# "
751};
752
753struct cmd_node keychain_node =
754{
755 KEYCHAIN_NODE,
756 "%s(config-keychain)# "
757};
758
759struct cmd_node keychain_key_node =
760{
761 KEYCHAIN_KEY_NODE,
762 "%s(config-keychain-key)# "
763};
764
hassoe7168df2004-10-03 20:11:32 +0000765/* Defined in lib/vty.c */
766extern struct cmd_node vty_node;
767
hasso95e735b2004-08-26 13:08:30 +0000768/* When '^Z' is received from vty, move down to the enable mode. */
769int
770vtysh_end ()
771{
772 switch (vty->node)
773 {
774 case VIEW_NODE:
775 case ENABLE_NODE:
776 /* Nothing to do. */
777 break;
778 default:
779 vty->node = ENABLE_NODE;
780 break;
781 }
782 return CMD_SUCCESS;
783}
784
785DEFUNSH (VTYSH_ALL,
786 vtysh_end_all,
787 vtysh_end_all_cmd,
788 "end",
hassoe7168df2004-10-03 20:11:32 +0000789 "End current mode and change to enable mode\n")
hasso95e735b2004-08-26 13:08:30 +0000790{
hasso42895462004-09-26 16:25:07 +0000791 return vtysh_end ();
hasso95e735b2004-08-26 13:08:30 +0000792}
793
paul718e3742002-12-13 20:15:29 +0000794DEFUNSH (VTYSH_BGPD,
795 router_bgp,
796 router_bgp_cmd,
797 "router bgp <1-65535>",
798 ROUTER_STR
799 BGP_STR
800 AS_STR)
801{
802 vty->node = BGP_NODE;
803 return CMD_SUCCESS;
804}
805
806DEFUNSH (VTYSH_BGPD,
807 address_family_vpnv4,
808 address_family_vpnv4_cmd,
809 "address-family vpnv4",
810 "Enter Address Family command mode\n"
811 "Address family\n")
812{
813 vty->node = BGP_VPNV4_NODE;
814 return CMD_SUCCESS;
815}
816
817DEFUNSH (VTYSH_BGPD,
818 address_family_vpnv4_unicast,
819 address_family_vpnv4_unicast_cmd,
820 "address-family vpnv4 unicast",
821 "Enter Address Family command mode\n"
822 "Address family\n"
823 "Address Family Modifier\n")
824{
825 vty->node = BGP_VPNV4_NODE;
826 return CMD_SUCCESS;
827}
828
829DEFUNSH (VTYSH_BGPD,
830 address_family_ipv4_unicast,
831 address_family_ipv4_unicast_cmd,
832 "address-family ipv4 unicast",
833 "Enter Address Family command mode\n"
834 "Address family\n"
835 "Address Family Modifier\n")
836{
837 vty->node = BGP_IPV4_NODE;
838 return CMD_SUCCESS;
839}
840
841DEFUNSH (VTYSH_BGPD,
842 address_family_ipv4_multicast,
843 address_family_ipv4_multicast_cmd,
844 "address-family ipv4 multicast",
845 "Enter Address Family command mode\n"
846 "Address family\n"
847 "Address Family Modifier\n")
848{
849 vty->node = BGP_IPV4M_NODE;
850 return CMD_SUCCESS;
851}
852
853DEFUNSH (VTYSH_BGPD,
854 address_family_ipv6,
855 address_family_ipv6_cmd,
856 "address-family ipv6",
857 "Enter Address Family command mode\n"
858 "Address family\n")
859{
860 vty->node = BGP_IPV6_NODE;
861 return CMD_SUCCESS;
862}
863
864DEFUNSH (VTYSH_BGPD,
865 address_family_ipv6_unicast,
866 address_family_ipv6_unicast_cmd,
867 "address-family ipv6 unicast",
868 "Enter Address Family command mode\n"
869 "Address family\n"
870 "Address Family Modifier\n")
871{
872 vty->node = BGP_IPV6_NODE;
873 return CMD_SUCCESS;
874}
875
876DEFUNSH (VTYSH_RIPD,
877 key_chain,
878 key_chain_cmd,
879 "key chain WORD",
880 "Authentication key management\n"
881 "Key-chain management\n"
882 "Key-chain name\n")
883{
884 vty->node = KEYCHAIN_NODE;
885 return CMD_SUCCESS;
886}
887
888DEFUNSH (VTYSH_RIPD,
889 key,
890 key_cmd,
891 "key <0-2147483647>",
892 "Configure a key\n"
893 "Key identifier number\n")
894{
895 vty->node = KEYCHAIN_KEY_NODE;
896 return CMD_SUCCESS;
897}
898
899DEFUNSH (VTYSH_RIPD,
900 router_rip,
901 router_rip_cmd,
902 "router rip",
903 ROUTER_STR
904 "RIP")
905{
906 vty->node = RIP_NODE;
907 return CMD_SUCCESS;
908}
909
910DEFUNSH (VTYSH_RIPNGD,
911 router_ripng,
912 router_ripng_cmd,
913 "router ripng",
914 ROUTER_STR
915 "RIPng")
916{
917 vty->node = RIPNG_NODE;
918 return CMD_SUCCESS;
919}
920
921DEFUNSH (VTYSH_OSPFD,
922 router_ospf,
923 router_ospf_cmd,
924 "router ospf",
925 "Enable a routing process\n"
926 "Start OSPF configuration\n")
927{
928 vty->node = OSPF_NODE;
929 return CMD_SUCCESS;
930}
931
932DEFUNSH (VTYSH_OSPF6D,
933 router_ospf6,
934 router_ospf6_cmd,
935 "router ospf6",
936 OSPF6_ROUTER_STR
937 OSPF6_STR)
938{
939 vty->node = OSPF6_NODE;
940 return CMD_SUCCESS;
941}
942
hassoc25e4582003-12-23 10:39:08 +0000943DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +0000944 router_isis,
945 router_isis_cmd,
946 "router isis WORD",
947 ROUTER_STR
948 "ISO IS-IS\n"
949 "ISO Routing area tag")
hassoc25e4582003-12-23 10:39:08 +0000950{
951 vty->node = ISIS_NODE;
952 return CMD_SUCCESS;
953}
954
paul718e3742002-12-13 20:15:29 +0000955DEFUNSH (VTYSH_RMAP,
956 route_map,
957 route_map_cmd,
958 "route-map WORD (deny|permit) <1-65535>",
959 "Create route-map or enter route-map command mode\n"
960 "Route map tag\n"
961 "Route map denies set operations\n"
962 "Route map permits set operations\n"
963 "Sequence to insert to/delete from existing route-map entry\n")
964{
965 vty->node = RMAP_NODE;
966 return CMD_SUCCESS;
967}
968
paul718e3742002-12-13 20:15:29 +0000969DEFUNSH (VTYSH_ALL,
hassoe7168df2004-10-03 20:11:32 +0000970 vtysh_line_vty,
971 vtysh_line_vty_cmd,
972 "line vty",
973 "Configure a terminal line\n"
974 "Virtual terminal\n")
975{
976 vty->node = VTY_NODE;
977 return CMD_SUCCESS;
978}
979
980DEFUNSH (VTYSH_ALL,
paul718e3742002-12-13 20:15:29 +0000981 vtysh_enable,
982 vtysh_enable_cmd,
983 "enable",
984 "Turn on privileged mode command\n")
985{
986 vty->node = ENABLE_NODE;
987 return CMD_SUCCESS;
988}
989
paul718e3742002-12-13 20:15:29 +0000990DEFUNSH (VTYSH_ALL,
991 vtysh_disable,
992 vtysh_disable_cmd,
993 "disable",
994 "Turn off privileged mode command\n")
995{
996 if (vty->node == ENABLE_NODE)
997 vty->node = VIEW_NODE;
998 return CMD_SUCCESS;
999}
1000
paul718e3742002-12-13 20:15:29 +00001001DEFUNSH (VTYSH_ALL,
1002 vtysh_config_terminal,
1003 vtysh_config_terminal_cmd,
1004 "configure terminal",
1005 "Configuration from vty interface\n"
1006 "Configuration terminal\n")
1007{
1008 vty->node = CONFIG_NODE;
1009 return CMD_SUCCESS;
1010}
1011
1012int
1013vtysh_exit (struct vty *vty)
1014{
1015 switch (vty->node)
1016 {
1017 case VIEW_NODE:
1018 case ENABLE_NODE:
1019 exit (0);
1020 break;
1021 case CONFIG_NODE:
1022 vty->node = ENABLE_NODE;
1023 break;
1024 case INTERFACE_NODE:
1025 case ZEBRA_NODE:
1026 case BGP_NODE:
1027 case RIP_NODE:
1028 case RIPNG_NODE:
1029 case OSPF_NODE:
1030 case OSPF6_NODE:
hassoc25e4582003-12-23 10:39:08 +00001031 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001032 case MASC_NODE:
1033 case RMAP_NODE:
1034 case VTY_NODE:
1035 case KEYCHAIN_NODE:
hasso2a56df92004-05-09 23:16:40 +00001036 vtysh_execute("end");
1037 vtysh_execute("configure terminal");
paul718e3742002-12-13 20:15:29 +00001038 vty->node = CONFIG_NODE;
1039 break;
1040 case BGP_VPNV4_NODE:
1041 case BGP_IPV4_NODE:
1042 case BGP_IPV4M_NODE:
1043 case BGP_IPV6_NODE:
1044 vty->node = BGP_NODE;
1045 break;
1046 case KEYCHAIN_KEY_NODE:
1047 vty->node = KEYCHAIN_NODE;
1048 break;
1049 default:
1050 break;
1051 }
1052 return CMD_SUCCESS;
1053}
1054
1055DEFUNSH (VTYSH_ALL,
1056 vtysh_exit_all,
1057 vtysh_exit_all_cmd,
1058 "exit",
1059 "Exit current mode and down to previous mode\n")
1060{
1061 return vtysh_exit (vty);
1062}
1063
1064ALIAS (vtysh_exit_all,
1065 vtysh_quit_all_cmd,
1066 "quit",
1067 "Exit current mode and down to previous mode\n")
1068
1069DEFUNSH (VTYSH_BGPD,
1070 exit_address_family,
1071 exit_address_family_cmd,
1072 "exit-address-family",
1073 "Exit from Address Family configuration mode\n")
1074{
1075 if (vty->node == BGP_IPV4_NODE
1076 || vty->node == BGP_IPV4M_NODE
1077 || vty->node == BGP_VPNV4_NODE
1078 || vty->node == BGP_IPV6_NODE)
1079 vty->node = BGP_NODE;
1080 return CMD_SUCCESS;
1081}
1082
1083DEFUNSH (VTYSH_ZEBRA,
1084 vtysh_exit_zebra,
1085 vtysh_exit_zebra_cmd,
1086 "exit",
1087 "Exit current mode and down to previous mode\n")
1088{
1089 return vtysh_exit (vty);
1090}
1091
1092ALIAS (vtysh_exit_zebra,
1093 vtysh_quit_zebra_cmd,
1094 "quit",
1095 "Exit current mode and down to previous mode\n")
1096
1097DEFUNSH (VTYSH_RIPD,
1098 vtysh_exit_ripd,
1099 vtysh_exit_ripd_cmd,
1100 "exit",
1101 "Exit current mode and down to previous mode\n")
1102{
1103 return vtysh_exit (vty);
1104}
1105
1106ALIAS (vtysh_exit_ripd,
1107 vtysh_quit_ripd_cmd,
1108 "quit",
1109 "Exit current mode and down to previous mode\n")
1110
paul68980082003-03-25 05:07:42 +00001111DEFUNSH (VTYSH_RIPNGD,
hassob094d262004-08-25 12:22:00 +00001112 vtysh_exit_ripngd,
1113 vtysh_exit_ripngd_cmd,
1114 "exit",
1115 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001116{
1117 return vtysh_exit (vty);
1118}
1119
1120ALIAS (vtysh_exit_ripngd,
1121 vtysh_quit_ripngd_cmd,
1122 "quit",
1123 "Exit current mode and down to previous mode\n")
1124
paul718e3742002-12-13 20:15:29 +00001125DEFUNSH (VTYSH_RMAP,
1126 vtysh_exit_rmap,
1127 vtysh_exit_rmap_cmd,
1128 "exit",
1129 "Exit current mode and down to previous mode\n")
1130{
1131 return vtysh_exit (vty);
1132}
1133
1134ALIAS (vtysh_exit_rmap,
1135 vtysh_quit_rmap_cmd,
1136 "quit",
1137 "Exit current mode and down to previous mode\n")
1138
1139DEFUNSH (VTYSH_BGPD,
1140 vtysh_exit_bgpd,
1141 vtysh_exit_bgpd_cmd,
1142 "exit",
1143 "Exit current mode and down to previous mode\n")
1144{
1145 return vtysh_exit (vty);
1146}
1147
1148ALIAS (vtysh_exit_bgpd,
1149 vtysh_quit_bgpd_cmd,
1150 "quit",
1151 "Exit current mode and down to previous mode\n")
1152
1153DEFUNSH (VTYSH_OSPFD,
1154 vtysh_exit_ospfd,
1155 vtysh_exit_ospfd_cmd,
1156 "exit",
1157 "Exit current mode and down to previous mode\n")
1158{
1159 return vtysh_exit (vty);
1160}
1161
1162ALIAS (vtysh_exit_ospfd,
1163 vtysh_quit_ospfd_cmd,
1164 "quit",
1165 "Exit current mode and down to previous mode\n")
1166
paul68980082003-03-25 05:07:42 +00001167DEFUNSH (VTYSH_OSPF6D,
hassob094d262004-08-25 12:22:00 +00001168 vtysh_exit_ospf6d,
1169 vtysh_exit_ospf6d_cmd,
1170 "exit",
1171 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001172{
1173 return vtysh_exit (vty);
1174}
1175
1176ALIAS (vtysh_exit_ospf6d,
1177 vtysh_quit_ospf6d_cmd,
1178 "quit",
1179 "Exit current mode and down to previous mode\n")
1180
hassoc25e4582003-12-23 10:39:08 +00001181DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +00001182 vtysh_exit_isisd,
1183 vtysh_exit_isisd_cmd,
1184 "exit",
1185 "Exit current mode and down to previous mode\n")
hassoc25e4582003-12-23 10:39:08 +00001186{
1187 return vtysh_exit (vty);
1188}
1189
1190ALIAS (vtysh_exit_isisd,
1191 vtysh_quit_isisd_cmd,
1192 "quit",
1193 "Exit current mode and down to previous mode\n")
1194
hassoe7168df2004-10-03 20:11:32 +00001195DEFUNSH (VTYSH_ALL,
1196 vtysh_exit_line_vty,
1197 vtysh_exit_line_vty_cmd,
1198 "exit",
1199 "Exit current mode and down to previous mode\n")
1200{
1201 return vtysh_exit (vty);
1202}
1203
1204ALIAS (vtysh_exit_line_vty,
1205 vtysh_quit_line_vty_cmd,
1206 "quit",
1207 "Exit current mode and down to previous mode\n")
1208
hasso95e735b2004-08-26 13:08:30 +00001209DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001210 vtysh_interface,
1211 vtysh_interface_cmd,
1212 "interface IFNAME",
1213 "Select an interface to configure\n"
1214 "Interface's name\n")
1215{
1216 vty->node = INTERFACE_NODE;
1217 return CMD_SUCCESS;
1218}
1219
hasso95e735b2004-08-26 13:08:30 +00001220/* TODO Implement "no interface command in isisd. */
paul32d24632003-05-23 09:25:20 +00001221DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
1222 vtysh_no_interface_cmd,
1223 "no interface IFNAME",
1224 NO_STR
1225 "Delete a pseudo interface's configuration\n"
1226 "Interface's name\n")
1227
hasso95e735b2004-08-26 13:08:30 +00001228/* TODO Implement interface description commands in ripngd, ospf6d
1229 * and isisd. */
paul338a9912003-03-01 15:44:10 +00001230DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1231 interface_desc_cmd,
1232 "description .LINE",
1233 "Interface specific description\n"
1234 "Characters describing this interface\n")
paul464dc8d2003-03-28 02:25:45 +00001235
1236DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1237 no_interface_desc_cmd,
1238 "no description",
1239 NO_STR
1240 "Interface specific description\n")
paul338a9912003-03-01 15:44:10 +00001241
hasso95e735b2004-08-26 13:08:30 +00001242DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001243 vtysh_exit_interface,
1244 vtysh_exit_interface_cmd,
1245 "exit",
1246 "Exit current mode and down to previous mode\n")
1247{
1248 return vtysh_exit (vty);
1249}
1250
1251ALIAS (vtysh_exit_interface,
1252 vtysh_quit_interface_cmd,
1253 "quit",
1254 "Exit current mode and down to previous mode\n")
1255
hasso95e735b2004-08-26 13:08:30 +00001256/* Logging commands. */
1257DEFUNSH (VTYSH_ALL,
1258 vtysh_log_stdout,
1259 vtysh_log_stdout_cmd,
1260 "log stdout",
1261 "Logging control\n"
1262 "Logging goes to stdout\n")
1263{
1264 return CMD_SUCCESS;
1265}
1266
1267DEFUNSH (VTYSH_ALL,
1268 no_vtysh_log_stdout,
1269 no_vtysh_log_stdout_cmd,
1270 "no log stdout",
1271 NO_STR
1272 "Logging control\n"
1273 "Logging goes to stdout\n")
1274{
1275 return CMD_SUCCESS;
1276}
1277
1278DEFUNSH (VTYSH_ALL,
1279 vtysh_log_file,
1280 vtysh_log_file_cmd,
1281 "log file FILENAME",
1282 "Logging control\n"
1283 "Logging to file\n"
1284 "Logging filename\n")
1285{
1286 return CMD_SUCCESS;
1287}
1288
1289DEFUNSH (VTYSH_ALL,
1290 no_vtysh_log_file,
1291 no_vtysh_log_file_cmd,
1292 "no log file [FILENAME]",
1293 NO_STR
1294 "Logging control\n"
1295 "Cancel logging to file\n"
1296 "Logging file name\n")
1297{
1298 return CMD_SUCCESS;
1299}
1300
1301DEFUNSH (VTYSH_ALL,
1302 vtysh_log_syslog,
1303 vtysh_log_syslog_cmd,
1304 "log syslog",
1305 "Logging control\n"
1306 "Logging goes to syslog\n")
1307{
1308 return CMD_SUCCESS;
1309}
1310
1311DEFUNSH (VTYSH_ALL,
1312 no_vtysh_log_syslog,
1313 no_vtysh_log_syslog_cmd,
1314 "no log syslog",
1315 NO_STR
1316 "Logging control\n"
1317 "Cancel logging to syslog\n")
1318{
1319 return CMD_SUCCESS;
1320}
1321
1322DEFUNSH (VTYSH_ALL,
1323 vtysh_log_trap,
1324 vtysh_log_trap_cmd,
1325 "log trap (emergencies|alerts|critical|errors|warnings|\
1326 notifications|informational|debugging)",
1327 "Logging control\n"
1328 "Limit logging to specifed level\n")
1329{
1330 return CMD_SUCCESS;
1331}
1332
1333DEFUNSH (VTYSH_ALL,
1334 no_vtysh_log_trap,
1335 no_vtysh_log_trap_cmd,
1336 "no log trap",
1337 NO_STR
1338 "Logging control\n"
1339 "Permit all logging information\n")
1340{
1341 return CMD_SUCCESS;
1342}
1343
1344DEFUNSH (VTYSH_ALL,
1345 vtysh_log_record_priority,
1346 vtysh_log_record_priority_cmd,
1347 "log record-priority",
1348 "Logging control\n"
1349 "Log the priority of the message within the message\n")
1350{
1351 return CMD_SUCCESS;
1352}
1353
1354DEFUNSH (VTYSH_ALL,
1355 no_vtysh_log_record_priority,
1356 no_vtysh_log_record_priority_cmd,
1357 "no log record-priority",
1358 NO_STR
1359 "Logging control\n"
1360 "Do not log the priority of the message within the message\n")
1361{
1362 return CMD_SUCCESS;
1363}
1364
hassoe7168df2004-10-03 20:11:32 +00001365DEFUNSH (VTYSH_ALL,
1366 vtysh_service_password_encrypt,
1367 vtysh_service_password_encrypt_cmd,
1368 "service password-encryption",
1369 "Set up miscellaneous service\n"
1370 "Enable encrypted passwords\n")
1371{
1372 return CMD_SUCCESS;
1373}
1374
1375DEFUNSH (VTYSH_ALL,
1376 no_vtysh_service_password_encrypt,
1377 no_vtysh_service_password_encrypt_cmd,
1378 "no service password-encryption",
1379 NO_STR
1380 "Set up miscellaneous service\n"
1381 "Enable encrypted passwords\n")
1382{
1383 return CMD_SUCCESS;
1384}
1385
1386DEFUNSH (VTYSH_ALL,
1387 vtysh_config_password,
1388 vtysh_password_cmd,
1389 "password (8|) WORD",
1390 "Assign the terminal connection password\n"
1391 "Specifies a HIDDEN password will follow\n"
1392 "dummy string \n"
1393 "The HIDDEN line password string\n")
1394{
1395 return CMD_SUCCESS;
1396}
1397
1398DEFUNSH (VTYSH_ALL,
1399 vtysh_password_text,
1400 vtysh_password_text_cmd,
1401 "password LINE",
1402 "Assign the terminal connection password\n"
1403 "The UNENCRYPTED (cleartext) line password\n")
1404{
1405 return CMD_SUCCESS;
1406}
1407
1408DEFUNSH (VTYSH_ALL,
1409 vtysh_config_enable_password,
1410 vtysh_enable_password_cmd,
1411 "enable password (8|) WORD",
1412 "Modify enable password parameters\n"
1413 "Assign the privileged level password\n"
1414 "Specifies a HIDDEN password will follow\n"
1415 "dummy string \n"
1416 "The HIDDEN 'enable' password string\n")
1417{
1418 return CMD_SUCCESS;
1419}
1420
1421DEFUNSH (VTYSH_ALL,
1422 vtysh_enable_password_text,
1423 vtysh_enable_password_text_cmd,
1424 "enable password LINE",
1425 "Modify enable password parameters\n"
1426 "Assign the privileged level password\n"
1427 "The UNENCRYPTED (cleartext) 'enable' password\n")
1428{
1429 return CMD_SUCCESS;
1430}
1431
1432DEFUNSH (VTYSH_ALL,
1433 no_vtysh_config_enable_password,
1434 no_vtysh_enable_password_cmd,
1435 "no enable password",
1436 NO_STR
1437 "Modify enable password parameters\n"
1438 "Assign the privileged level password\n")
1439{
1440 return CMD_SUCCESS;
1441}
1442
paul718e3742002-12-13 20:15:29 +00001443DEFUN (vtysh_write_terminal,
1444 vtysh_write_terminal_cmd,
1445 "write terminal",
1446 "Write running configuration to memory, network, or terminal\n"
1447 "Write to terminal\n")
1448{
1449 int ret;
1450 char line[] = "write terminal\n";
1451 FILE *fp = NULL;
1452
1453 if (vtysh_pager_name)
1454 {
paul4fc01e62002-12-13 20:49:00 +00001455 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +00001456 if (fp == NULL)
1457 {
1458 perror ("popen");
1459 exit (1);
1460 }
1461 }
1462 else
1463 fp = stdout;
1464
1465 vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
1466 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
1467 VTY_NEWLINE);
hassoe7168df2004-10-03 20:11:32 +00001468 vty_out (vty, "!%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001469
1470 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1471 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1472 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1473 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1474 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1475 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001476 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001477
hassoe7168df2004-10-03 20:11:32 +00001478 /* Integrate vtysh specific configuration. */
1479 vtysh_config_write ();
1480
paul718e3742002-12-13 20:15:29 +00001481 vtysh_config_dump (fp);
1482
1483 if (vtysh_pager_name && fp)
1484 {
1485 fflush (fp);
1486 if (pclose (fp) == -1)
1487 {
1488 perror ("pclose");
1489 exit (1);
1490 }
1491 fp = NULL;
1492 }
1493
1494 return CMD_SUCCESS;
1495}
1496
hassoe7168df2004-10-03 20:11:32 +00001497DEFUN (vtysh_integrated_config,
1498 vtysh_integrated_config_cmd,
1499 "service integrated-vtysh-config",
1500 "Set up miscellaneous service\n"
1501 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001502{
hassoe7168df2004-10-03 20:11:32 +00001503 vtysh_writeconfig_integrated = 1;
1504 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001505}
1506
hassoe7168df2004-10-03 20:11:32 +00001507DEFUN (no_vtysh_integrated_config,
1508 no_vtysh_integrated_config_cmd,
1509 "no service integrated-vtysh-config",
1510 NO_STR
1511 "Set up miscellaneous service\n"
1512 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001513{
hassoe7168df2004-10-03 20:11:32 +00001514 vtysh_writeconfig_integrated = 0;
1515 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001516}
1517
1518int write_config_integrated(void)
paul718e3742002-12-13 20:15:29 +00001519{
1520 int ret;
paul718e3742002-12-13 20:15:29 +00001521 char line[] = "write terminal\n";
1522 FILE *fp;
1523 char *integrate_sav = NULL;
1524
hasso95e735b2004-08-26 13:08:30 +00001525 integrate_sav = malloc (strlen (integrate_default) +
1526 strlen (CONF_BACKUP_EXT) + 1);
paul718e3742002-12-13 20:15:29 +00001527 strcpy (integrate_sav, integrate_default);
1528 strcat (integrate_sav, CONF_BACKUP_EXT);
1529
paul4fc01e62002-12-13 20:49:00 +00001530 fprintf (stdout,"Building Configuration...\n");
paul718e3742002-12-13 20:15:29 +00001531
hasso95e735b2004-08-26 13:08:30 +00001532 /* Move current configuration file to backup config file. */
paul718e3742002-12-13 20:15:29 +00001533 unlink (integrate_sav);
1534 rename (integrate_default, integrate_sav);
hasso95e735b2004-08-26 13:08:30 +00001535 free (integrate_sav);
paul4fc01e62002-12-13 20:49:00 +00001536
paul718e3742002-12-13 20:15:29 +00001537 fp = fopen (integrate_default, "w");
1538 if (fp == NULL)
1539 {
hasso95e735b2004-08-26 13:08:30 +00001540 fprintf (stdout,"%% Can't open configuration file %s.\n",
1541 integrate_default);
paul718e3742002-12-13 20:15:29 +00001542 return CMD_SUCCESS;
1543 }
paul718e3742002-12-13 20:15:29 +00001544
paul718e3742002-12-13 20:15:29 +00001545 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1546 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1547 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1548 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1549 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1550 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001551 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001552
1553 vtysh_config_dump (fp);
1554
1555 fclose (fp);
1556
gdtaa593d52003-12-22 20:15:53 +00001557 if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
1558 {
1559 fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
1560 integrate_default, strerror(errno), errno);
1561 return CMD_WARNING;
1562 }
1563
paul4fc01e62002-12-13 20:49:00 +00001564 fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
1565
1566 fprintf (stdout,"[OK]\n");
1567
paul718e3742002-12-13 20:15:29 +00001568 return CMD_SUCCESS;
1569}
1570
paul4fc01e62002-12-13 20:49:00 +00001571DEFUN (vtysh_write_memory,
1572 vtysh_write_memory_cmd,
1573 "write memory",
1574 "Write running configuration to memory, network, or terminal\n"
1575 "Write configuration to the file (same as write file)\n")
1576{
pauldfc0d9b2003-04-18 23:55:29 +00001577 int ret = CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001578 char line[] = "write memory\n";
hassoe7168df2004-10-03 20:11:32 +00001579 char *vtysh_conf;
1580 extern struct host host;
1581 FILE *fp;
paul4fc01e62002-12-13 20:49:00 +00001582
hassoe7168df2004-10-03 20:11:32 +00001583 /* If integrated Quagga.conf explicitely set. */
1584 if (vtysh_writeconfig_integrated)
1585 return write_config_integrated();
paul4fc01e62002-12-13 20:49:00 +00001586
1587 fprintf (stdout,"Building Configuration...\n");
1588
1589 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1590 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1591 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1592 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1593 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1594 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
hassoc25e4582003-12-23 10:39:08 +00001595 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, stdout);
hassoe7168df2004-10-03 20:11:32 +00001596
paul4fc01e62002-12-13 20:49:00 +00001597 fprintf (stdout,"[OK]\n");
1598
pauldfc0d9b2003-04-18 23:55:29 +00001599 return ret;
paul4fc01e62002-12-13 20:49:00 +00001600}
1601
paul718e3742002-12-13 20:15:29 +00001602ALIAS (vtysh_write_memory,
1603 vtysh_copy_runningconfig_startupconfig_cmd,
1604 "copy running-config startup-config",
1605 "Copy from one file to another\n"
1606 "Copy from current system configuration\n"
1607 "Copy to startup configuration\n")
1608
1609ALIAS (vtysh_write_memory,
1610 vtysh_write_file_cmd,
1611 "write file",
1612 "Write running configuration to memory, network, or terminal\n"
1613 "Write configuration to the file (same as write memory)\n")
1614
hasso4a6e2252003-05-25 11:51:29 +00001615ALIAS (vtysh_write_memory,
1616 vtysh_write_cmd,
1617 "write",
1618 "Write running configuration to memory, network, or terminal\n")
1619
paul718e3742002-12-13 20:15:29 +00001620ALIAS (vtysh_write_terminal,
1621 vtysh_show_running_config_cmd,
1622 "show running-config",
1623 SHOW_STR
1624 "Current operating configuration\n")
hassob094d262004-08-25 12:22:00 +00001625
hasso34553cc2004-08-27 13:56:39 +00001626DEFUN (vtysh_terminal_length,
1627 vtysh_terminal_length_cmd,
1628 "terminal length <0-512>",
1629 "Set terminal line parameters\n"
1630 "Set number of lines on a screen\n"
1631 "Number of lines on screen (0 for no pausing)\n")
1632{
1633 int lines;
1634 char *endptr = NULL;
1635 char default_pager[10];
1636
1637 lines = strtol (argv[0], &endptr, 10);
1638 if (lines < 0 || lines > 512 || *endptr != '\0')
1639 {
1640 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
1641 return CMD_WARNING;
1642 }
1643
1644 if (vtysh_pager_name)
1645 {
1646 free (vtysh_pager_name);
1647 vtysh_pager_name = NULL;
1648 }
1649
1650 if (lines != 0)
1651 {
1652 snprintf(default_pager, 10, "more -%i", lines);
1653 vtysh_pager_name = strdup (default_pager);
1654 }
1655
1656 return CMD_SUCCESS;
1657}
1658
1659DEFUN (vtysh_terminal_no_length,
1660 vtysh_terminal_no_length_cmd,
1661 "terminal no length",
1662 "Set terminal line parameters\n"
1663 NO_STR
1664 "Set number of lines on a screen\n")
1665{
1666 if (vtysh_pager_name)
1667 {
1668 free (vtysh_pager_name);
1669 vtysh_pager_name = NULL;
1670 }
1671
1672 vtysh_pager_init();
1673 return CMD_SUCCESS;
1674}
1675
hassoe7168df2004-10-03 20:11:32 +00001676DEFUN (vtysh_show_running_daemons,
1677 vtysh_show_running_daemons_cmd,
1678 "show running-daemons",
1679 SHOW_STR
1680 "Show list of running daemons\n")
1681{
1682 if ( vtysh_client[VTYSH_INDEX_ZEBRA].fd > 0 )
1683 vty_out(vty, " zebra");
1684 if ( vtysh_client[VTYSH_INDEX_RIP].fd > 0 )
1685 vty_out(vty, " ripd");
1686 if ( vtysh_client[VTYSH_INDEX_RIPNG].fd > 0 )
1687 vty_out(vty, " ripngd");
1688 if ( vtysh_client[VTYSH_INDEX_OSPF].fd > 0 )
1689 vty_out(vty, " ospfd");
1690 if ( vtysh_client[VTYSH_INDEX_OSPF6].fd > 0 )
1691 vty_out(vty, " ospf6d");
1692 if ( vtysh_client[VTYSH_INDEX_BGP].fd > 0 )
1693 vty_out(vty, " bgpd");
1694 if ( vtysh_client[VTYSH_INDEX_ISIS].fd > 0 )
1695 vty_out(vty, " isisd");
1696 vty_out(vty, "%s", VTY_NEWLINE);
1697
1698 return CMD_SUCCESS;
1699}
1700
paul718e3742002-12-13 20:15:29 +00001701/* Execute command in child process. */
1702int
1703execute_command (char *command, int argc, char *arg1, char *arg2)
1704{
1705 int ret;
1706 pid_t pid;
1707 int status;
1708
1709 /* Call fork(). */
1710 pid = fork ();
1711
1712 if (pid < 0)
1713 {
1714 /* Failure of fork(). */
1715 fprintf (stderr, "Can't fork: %s\n", strerror (errno));
1716 exit (1);
1717 }
1718 else if (pid == 0)
1719 {
1720 /* This is child process. */
1721 switch (argc)
1722 {
1723 case 0:
hassofa2b17e2004-03-04 17:45:00 +00001724 ret = execlp (command, command, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001725 break;
1726 case 1:
hassofa2b17e2004-03-04 17:45:00 +00001727 ret = execlp (command, command, arg1, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001728 break;
1729 case 2:
hassofa2b17e2004-03-04 17:45:00 +00001730 ret = execlp (command, command, arg1, arg2, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001731 break;
1732 }
1733
1734 /* When execlp suceed, this part is not executed. */
1735 fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
1736 exit (1);
1737 }
1738 else
1739 {
1740 /* This is parent. */
1741 execute_flag = 1;
1742 ret = wait4 (pid, &status, 0, NULL);
1743 execute_flag = 0;
1744 }
1745 return 0;
1746}
1747
1748DEFUN (vtysh_ping,
1749 vtysh_ping_cmd,
1750 "ping WORD",
hasso4eeccf12003-06-25 10:49:55 +00001751 "Send echo messages\n"
paul718e3742002-12-13 20:15:29 +00001752 "Ping destination address or hostname\n")
1753{
1754 execute_command ("ping", 1, argv[0], NULL);
1755 return CMD_SUCCESS;
1756}
1757
hasso4eeccf12003-06-25 10:49:55 +00001758ALIAS (vtysh_ping,
1759 vtysh_ping_ip_cmd,
1760 "ping ip WORD",
1761 "Send echo messages\n"
1762 "IP echo\n"
1763 "Ping destination address or hostname\n")
1764
paul718e3742002-12-13 20:15:29 +00001765DEFUN (vtysh_traceroute,
1766 vtysh_traceroute_cmd,
1767 "traceroute WORD",
1768 "Trace route to destination\n"
1769 "Trace route to destination address or hostname\n")
1770{
1771 execute_command ("traceroute", 1, argv[0], NULL);
1772 return CMD_SUCCESS;
1773}
1774
hasso4eeccf12003-06-25 10:49:55 +00001775ALIAS (vtysh_traceroute,
1776 vtysh_traceroute_ip_cmd,
1777 "traceroute ip WORD",
1778 "Trace route to destination\n"
1779 "IP trace\n"
1780 "Trace route to destination address or hostname\n")
1781
1782#ifdef HAVE_IPV6
1783DEFUN (vtysh_ping6,
1784 vtysh_ping6_cmd,
1785 "ping ipv6 WORD",
1786 "Send echo messages\n"
1787 "IPv6 echo\n"
1788 "Ping destination address or hostname\n")
1789{
1790 execute_command ("ping6", 1, argv[0], NULL);
1791 return CMD_SUCCESS;
1792}
1793
1794DEFUN (vtysh_traceroute6,
1795 vtysh_traceroute6_cmd,
1796 "traceroute ipv6 WORD",
1797 "Trace route to destination\n"
1798 "IPv6 trace\n"
1799 "Trace route to destination address or hostname\n")
1800{
1801 execute_command ("traceroute6", 1, argv[0], NULL);
1802 return CMD_SUCCESS;
1803}
1804#endif
1805
paul718e3742002-12-13 20:15:29 +00001806DEFUN (vtysh_telnet,
1807 vtysh_telnet_cmd,
1808 "telnet WORD",
1809 "Open a telnet connection\n"
1810 "IP address or hostname of a remote system\n")
1811{
1812 execute_command ("telnet", 1, argv[0], NULL);
1813 return CMD_SUCCESS;
1814}
1815
1816DEFUN (vtysh_telnet_port,
1817 vtysh_telnet_port_cmd,
1818 "telnet WORD PORT",
1819 "Open a telnet connection\n"
1820 "IP address or hostname of a remote system\n"
1821 "TCP Port number\n")
1822{
1823 execute_command ("telnet", 2, argv[0], argv[1]);
1824 return CMD_SUCCESS;
1825}
1826
paul5087df52003-01-25 06:56:09 +00001827DEFUN (vtysh_ssh,
1828 vtysh_ssh_cmd,
1829 "ssh WORD",
1830 "Open an ssh connection\n"
1831 "[user@]host\n")
1832{
1833 execute_command ("ssh", 1, argv[0], NULL);
1834 return CMD_SUCCESS;
1835}
1836
paul718e3742002-12-13 20:15:29 +00001837DEFUN (vtysh_start_shell,
1838 vtysh_start_shell_cmd,
1839 "start-shell",
1840 "Start UNIX shell\n")
1841{
1842 execute_command ("sh", 0, NULL, NULL);
1843 return CMD_SUCCESS;
1844}
1845
1846DEFUN (vtysh_start_bash,
1847 vtysh_start_bash_cmd,
1848 "start-shell bash",
1849 "Start UNIX shell\n"
1850 "Start bash\n")
1851{
1852 execute_command ("bash", 0, NULL, NULL);
1853 return CMD_SUCCESS;
1854}
1855
1856DEFUN (vtysh_start_zsh,
1857 vtysh_start_zsh_cmd,
1858 "start-shell zsh",
1859 "Start UNIX shell\n"
1860 "Start Z shell\n")
1861{
1862 execute_command ("zsh", 0, NULL, NULL);
1863 return CMD_SUCCESS;
1864}
hassob094d262004-08-25 12:22:00 +00001865
paul718e3742002-12-13 20:15:29 +00001866void
1867vtysh_install_default (enum node_type node)
1868{
1869 install_element (node, &config_list_cmd);
1870}
1871
1872/* Making connection to protocol daemon. */
1873int
1874vtysh_connect (struct vtysh_client *vclient, char *path)
1875{
1876 int ret;
1877 int sock, len;
1878 struct sockaddr_un addr;
1879 struct stat s_stat;
1880 uid_t euid;
1881 gid_t egid;
1882
1883 memset (vclient, 0, sizeof (struct vtysh_client));
1884 vclient->fd = -1;
1885
1886 /* Stat socket to see if we have permission to access it. */
1887 euid = geteuid();
1888 egid = getegid();
1889 ret = stat (path, &s_stat);
1890 if (ret < 0 && errno != ENOENT)
1891 {
1892 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
1893 path, strerror(errno));
1894 exit(1);
1895 }
1896
1897 if (ret >= 0)
1898 {
1899 if (! S_ISSOCK(s_stat.st_mode))
1900 {
1901 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1902 path);
1903 exit (1);
1904 }
1905
paul718e3742002-12-13 20:15:29 +00001906 }
1907
1908 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1909 if (sock < 0)
1910 {
1911#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001912 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
1913 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001914#endif /* DEBUG */
1915 return -1;
1916 }
1917
1918 memset (&addr, 0, sizeof (struct sockaddr_un));
1919 addr.sun_family = AF_UNIX;
1920 strncpy (addr.sun_path, path, strlen (path));
1921#ifdef HAVE_SUN_LEN
1922 len = addr.sun_len = SUN_LEN(&addr);
1923#else
1924 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1925#endif /* HAVE_SUN_LEN */
1926
1927 ret = connect (sock, (struct sockaddr *) &addr, len);
1928 if (ret < 0)
1929 {
1930#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001931 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
1932 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001933#endif /* DEBUG */
1934 close (sock);
1935 return -1;
1936 }
1937 vclient->fd = sock;
1938
1939 return 0;
1940}
1941
1942void
1943vtysh_connect_all()
1944{
1945 /* Clear each daemons client structure. */
paulfe067782003-04-07 16:10:05 +00001946 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1947 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1948 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1949 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1950 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1951 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
hassoc25e4582003-12-23 10:39:08 +00001952 vtysh_connect (&vtysh_client[VTYSH_INDEX_ISIS], ISIS_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +00001953}
1954
hasso95e735b2004-08-26 13:08:30 +00001955/* To disable readline's filename completion. */
pauldfc0d9b2003-04-18 23:55:29 +00001956char *
1957vtysh_completion_entry_function (const char *ignore, int invoking_key)
paul718e3742002-12-13 20:15:29 +00001958{
pauldfc0d9b2003-04-18 23:55:29 +00001959 return NULL;
paul718e3742002-12-13 20:15:29 +00001960}
1961
1962void
1963vtysh_readline_init ()
1964{
1965 /* readline related settings. */
1966 rl_bind_key ('?', vtysh_rl_describe);
paul68980082003-03-25 05:07:42 +00001967 rl_completion_entry_function = vtysh_completion_entry_function;
paul718e3742002-12-13 20:15:29 +00001968 rl_attempted_completion_function = (CPPFunction *)new_completion;
1969 /* do not append space after completion. It will be appended
hasso95e735b2004-08-26 13:08:30 +00001970 * in new_completion() function explicitly. */
paul718e3742002-12-13 20:15:29 +00001971 rl_completion_append_character = '\0';
1972}
1973
1974char *
1975vtysh_prompt ()
1976{
1977 struct utsname names;
1978 static char buf[100];
1979 const char*hostname;
1980 extern struct host host;
1981
1982 hostname = host.name;
1983
1984 if (!hostname)
1985 {
1986 uname (&names);
1987 hostname = names.nodename;
1988 }
1989
1990 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1991
1992 return buf;
1993}
1994
1995void
1996vtysh_init_vty ()
1997{
1998 /* Make vty structure. */
1999 vty = vty_new ();
2000 vty->type = VTY_SHELL;
2001 vty->node = VIEW_NODE;
2002
2003 /* Initialize commands. */
2004 cmd_init (0);
2005
2006 /* Install nodes. */
2007 install_node (&bgp_node, NULL);
2008 install_node (&rip_node, NULL);
2009 install_node (&interface_node, NULL);
2010 install_node (&rmap_node, NULL);
2011 install_node (&zebra_node, NULL);
2012 install_node (&bgp_vpnv4_node, NULL);
2013 install_node (&bgp_ipv4_node, NULL);
2014 install_node (&bgp_ipv4m_node, NULL);
2015/* #ifdef HAVE_IPV6 */
2016 install_node (&bgp_ipv6_node, NULL);
2017/* #endif */
2018 install_node (&ospf_node, NULL);
2019/* #ifdef HAVE_IPV6 */
2020 install_node (&ripng_node, NULL);
2021 install_node (&ospf6_node, NULL);
2022/* #endif */
2023 install_node (&keychain_node, NULL);
2024 install_node (&keychain_key_node, NULL);
hassoc25e4582003-12-23 10:39:08 +00002025 install_node (&isis_node, NULL);
hassoe7168df2004-10-03 20:11:32 +00002026 install_node (&vty_node, NULL);
paul718e3742002-12-13 20:15:29 +00002027
2028 vtysh_install_default (VIEW_NODE);
2029 vtysh_install_default (ENABLE_NODE);
2030 vtysh_install_default (CONFIG_NODE);
2031 vtysh_install_default (BGP_NODE);
2032 vtysh_install_default (RIP_NODE);
2033 vtysh_install_default (INTERFACE_NODE);
2034 vtysh_install_default (RMAP_NODE);
2035 vtysh_install_default (ZEBRA_NODE);
2036 vtysh_install_default (BGP_VPNV4_NODE);
2037 vtysh_install_default (BGP_IPV4_NODE);
2038 vtysh_install_default (BGP_IPV4M_NODE);
2039 vtysh_install_default (BGP_IPV6_NODE);
2040 vtysh_install_default (OSPF_NODE);
2041 vtysh_install_default (RIPNG_NODE);
2042 vtysh_install_default (OSPF6_NODE);
hassoc25e4582003-12-23 10:39:08 +00002043 vtysh_install_default (ISIS_NODE);
paul718e3742002-12-13 20:15:29 +00002044 vtysh_install_default (KEYCHAIN_NODE);
2045 vtysh_install_default (KEYCHAIN_KEY_NODE);
hassoe7168df2004-10-03 20:11:32 +00002046 vtysh_install_default (VTY_NODE);
paul718e3742002-12-13 20:15:29 +00002047
2048 install_element (VIEW_NODE, &vtysh_enable_cmd);
2049 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
2050 install_element (ENABLE_NODE, &vtysh_disable_cmd);
2051
2052 /* "exit" command. */
2053 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
2054 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
2055 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
2056 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
2057 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
2058 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
2059 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
2060 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
paul68980082003-03-25 05:07:42 +00002061 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
2062 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
paul718e3742002-12-13 20:15:29 +00002063 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
2064 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
paul68980082003-03-25 05:07:42 +00002065 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
2066 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
paul718e3742002-12-13 20:15:29 +00002067 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
2068 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
2069 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
2070 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
2071 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
2072 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
2073 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
2074 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
2075 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
2076 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
hassoc25e4582003-12-23 10:39:08 +00002077 install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
2078 install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
paul718e3742002-12-13 20:15:29 +00002079 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
2080 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
2081 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
2082 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
2083 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
2084 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
hassoe7168df2004-10-03 20:11:32 +00002085 install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
2086 install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002087
2088 /* "end" command. */
2089 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
2090 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
2091 install_element (RIP_NODE, &vtysh_end_all_cmd);
2092 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
2093 install_element (OSPF_NODE, &vtysh_end_all_cmd);
2094 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
2095 install_element (BGP_NODE, &vtysh_end_all_cmd);
2096 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
2097 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
2098 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
2099 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
hassoc25e4582003-12-23 10:39:08 +00002100 install_element (ISIS_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002101 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
2102 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
2103 install_element (RMAP_NODE, &vtysh_end_all_cmd);
hassoe7168df2004-10-03 20:11:32 +00002104 install_element (VTY_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002105
paul338a9912003-03-01 15:44:10 +00002106 install_element (INTERFACE_NODE, &interface_desc_cmd);
paul464dc8d2003-03-28 02:25:45 +00002107 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
paul718e3742002-12-13 20:15:29 +00002108 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
2109 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
2110 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
2111 install_element (CONFIG_NODE, &router_rip_cmd);
2112#ifdef HAVE_IPV6
2113 install_element (CONFIG_NODE, &router_ripng_cmd);
2114#endif
2115 install_element (CONFIG_NODE, &router_ospf_cmd);
2116#ifdef HAVE_IPV6
2117 install_element (CONFIG_NODE, &router_ospf6_cmd);
2118#endif
hassoc25e4582003-12-23 10:39:08 +00002119 install_element (CONFIG_NODE, &router_isis_cmd);
paul718e3742002-12-13 20:15:29 +00002120 install_element (CONFIG_NODE, &router_bgp_cmd);
2121 install_element (BGP_NODE, &address_family_vpnv4_cmd);
2122 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
2123 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
2124 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
2125#ifdef HAVE_IPV6
2126 install_element (BGP_NODE, &address_family_ipv6_cmd);
2127 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
2128#endif
2129 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
2130 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
2131 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
2132 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
2133 install_element (CONFIG_NODE, &key_chain_cmd);
2134 install_element (CONFIG_NODE, &route_map_cmd);
hassoe7168df2004-10-03 20:11:32 +00002135 install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002136 install_element (KEYCHAIN_NODE, &key_cmd);
2137 install_element (KEYCHAIN_NODE, &key_chain_cmd);
2138 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
2139 install_element (CONFIG_NODE, &vtysh_interface_cmd);
paul32d24632003-05-23 09:25:20 +00002140 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
paul718e3742002-12-13 20:15:29 +00002141 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
2142 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
2143 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
hasso4a6e2252003-05-25 11:51:29 +00002144 install_element (ENABLE_NODE, &vtysh_write_cmd);
paul718e3742002-12-13 20:15:29 +00002145
hasso95e735b2004-08-26 13:08:30 +00002146 /* "write terminal" command. */
paul718e3742002-12-13 20:15:29 +00002147 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
hassoe7168df2004-10-03 20:11:32 +00002148
2149 install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
2150 install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
paul718e3742002-12-13 20:15:29 +00002151
hasso95e735b2004-08-26 13:08:30 +00002152 /* "write memory" command. */
paul718e3742002-12-13 20:15:29 +00002153 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
paul718e3742002-12-13 20:15:29 +00002154
hasso34553cc2004-08-27 13:56:39 +00002155 install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
2156 install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
2157 install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
2158 install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
hassoe7168df2004-10-03 20:11:32 +00002159 install_element (VIEW_NODE, &vtysh_show_running_daemons_cmd);
2160 install_element (ENABLE_NODE, &vtysh_show_running_daemons_cmd);
hasso34553cc2004-08-27 13:56:39 +00002161
paul718e3742002-12-13 20:15:29 +00002162 install_element (VIEW_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002163 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002164 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002165 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
2166#ifdef HAVE_IPV6
2167 install_element (VIEW_NODE, &vtysh_ping6_cmd);
2168 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
2169#endif
paul718e3742002-12-13 20:15:29 +00002170 install_element (VIEW_NODE, &vtysh_telnet_cmd);
2171 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
paul5087df52003-01-25 06:56:09 +00002172 install_element (VIEW_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002173 install_element (ENABLE_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002174 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002175 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002176 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
2177#ifdef HAVE_IPV6
2178 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
2179 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
2180#endif
paul718e3742002-12-13 20:15:29 +00002181 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
2182 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
hasso67e29ab2004-08-26 22:21:31 +00002183 install_element (ENABLE_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002184 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
2185 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
2186 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
2187
paul718e3742002-12-13 20:15:29 +00002188 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
2189 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
2190 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
2191 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
2192 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
2193 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
2194 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
2195 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
2196 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
2197 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
hassoe7168df2004-10-03 20:11:32 +00002198
2199 install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
2200 install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
2201
2202 install_element (CONFIG_NODE, &vtysh_password_cmd);
2203 install_element (CONFIG_NODE, &vtysh_password_text_cmd);
2204 install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
2205 install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
2206 install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
2207
paul718e3742002-12-13 20:15:29 +00002208}