blob: 0df5ddbbb66ef2efcd4df77e71a25eb023dc2445 [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
hassodda09522004-10-07 21:40:25 +0000164vtysh_client_execute (struct vtysh_client *vclient, const char *line, FILE *fp)
paul718e3742002-12-13 20:15:29 +0000165{
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
hassodda09522004-10-07 21:40:25 +0000248vtysh_execute_func (const char *line, int pager)
paul718e3742002-12-13 20:15:29 +0000249{
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
hassodda09522004-10-07 21:40:25 +0000393vtysh_execute_no_pager (const char *line)
paul718e3742002-12-13 20:15:29 +0000394{
395 vtysh_execute_func (line, 0);
396}
397
398void
hassodda09522004-10-07 21:40:25 +0000399vtysh_execute (const char *line)
paul718e3742002-12-13 20:15:29 +0000400{
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;
hassodda09522004-10-07 21:40:25 +0000515 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000516 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";
1579
hassoe7168df2004-10-03 20:11:32 +00001580 /* If integrated Quagga.conf explicitely set. */
1581 if (vtysh_writeconfig_integrated)
1582 return write_config_integrated();
paul4fc01e62002-12-13 20:49:00 +00001583
1584 fprintf (stdout,"Building Configuration...\n");
1585
1586 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1587 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1588 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1589 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1590 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1591 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
hassoc25e4582003-12-23 10:39:08 +00001592 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, stdout);
hassoe7168df2004-10-03 20:11:32 +00001593
paul4fc01e62002-12-13 20:49:00 +00001594 fprintf (stdout,"[OK]\n");
1595
pauldfc0d9b2003-04-18 23:55:29 +00001596 return ret;
paul4fc01e62002-12-13 20:49:00 +00001597}
1598
paul718e3742002-12-13 20:15:29 +00001599ALIAS (vtysh_write_memory,
1600 vtysh_copy_runningconfig_startupconfig_cmd,
1601 "copy running-config startup-config",
1602 "Copy from one file to another\n"
1603 "Copy from current system configuration\n"
1604 "Copy to startup configuration\n")
1605
1606ALIAS (vtysh_write_memory,
1607 vtysh_write_file_cmd,
1608 "write file",
1609 "Write running configuration to memory, network, or terminal\n"
1610 "Write configuration to the file (same as write memory)\n")
1611
hasso4a6e2252003-05-25 11:51:29 +00001612ALIAS (vtysh_write_memory,
1613 vtysh_write_cmd,
1614 "write",
1615 "Write running configuration to memory, network, or terminal\n")
1616
paul718e3742002-12-13 20:15:29 +00001617ALIAS (vtysh_write_terminal,
1618 vtysh_show_running_config_cmd,
1619 "show running-config",
1620 SHOW_STR
1621 "Current operating configuration\n")
hassob094d262004-08-25 12:22:00 +00001622
hasso34553cc2004-08-27 13:56:39 +00001623DEFUN (vtysh_terminal_length,
1624 vtysh_terminal_length_cmd,
1625 "terminal length <0-512>",
1626 "Set terminal line parameters\n"
1627 "Set number of lines on a screen\n"
1628 "Number of lines on screen (0 for no pausing)\n")
1629{
1630 int lines;
1631 char *endptr = NULL;
1632 char default_pager[10];
1633
1634 lines = strtol (argv[0], &endptr, 10);
1635 if (lines < 0 || lines > 512 || *endptr != '\0')
1636 {
1637 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
1638 return CMD_WARNING;
1639 }
1640
1641 if (vtysh_pager_name)
1642 {
1643 free (vtysh_pager_name);
1644 vtysh_pager_name = NULL;
1645 }
1646
1647 if (lines != 0)
1648 {
1649 snprintf(default_pager, 10, "more -%i", lines);
1650 vtysh_pager_name = strdup (default_pager);
1651 }
1652
1653 return CMD_SUCCESS;
1654}
1655
1656DEFUN (vtysh_terminal_no_length,
1657 vtysh_terminal_no_length_cmd,
1658 "terminal no length",
1659 "Set terminal line parameters\n"
1660 NO_STR
1661 "Set number of lines on a screen\n")
1662{
1663 if (vtysh_pager_name)
1664 {
1665 free (vtysh_pager_name);
1666 vtysh_pager_name = NULL;
1667 }
1668
1669 vtysh_pager_init();
1670 return CMD_SUCCESS;
1671}
1672
hassoe7168df2004-10-03 20:11:32 +00001673DEFUN (vtysh_show_running_daemons,
1674 vtysh_show_running_daemons_cmd,
1675 "show running-daemons",
1676 SHOW_STR
1677 "Show list of running daemons\n")
1678{
1679 if ( vtysh_client[VTYSH_INDEX_ZEBRA].fd > 0 )
1680 vty_out(vty, " zebra");
1681 if ( vtysh_client[VTYSH_INDEX_RIP].fd > 0 )
1682 vty_out(vty, " ripd");
1683 if ( vtysh_client[VTYSH_INDEX_RIPNG].fd > 0 )
1684 vty_out(vty, " ripngd");
1685 if ( vtysh_client[VTYSH_INDEX_OSPF].fd > 0 )
1686 vty_out(vty, " ospfd");
1687 if ( vtysh_client[VTYSH_INDEX_OSPF6].fd > 0 )
1688 vty_out(vty, " ospf6d");
1689 if ( vtysh_client[VTYSH_INDEX_BGP].fd > 0 )
1690 vty_out(vty, " bgpd");
1691 if ( vtysh_client[VTYSH_INDEX_ISIS].fd > 0 )
1692 vty_out(vty, " isisd");
1693 vty_out(vty, "%s", VTY_NEWLINE);
1694
1695 return CMD_SUCCESS;
1696}
1697
paul718e3742002-12-13 20:15:29 +00001698/* Execute command in child process. */
1699int
hasso5862ff52004-10-11 13:20:40 +00001700execute_command (const char *command, int argc, const char *arg1,
1701 const char *arg2)
paul718e3742002-12-13 20:15:29 +00001702{
1703 int ret;
1704 pid_t pid;
1705 int status;
1706
1707 /* Call fork(). */
1708 pid = fork ();
1709
1710 if (pid < 0)
1711 {
1712 /* Failure of fork(). */
1713 fprintf (stderr, "Can't fork: %s\n", strerror (errno));
1714 exit (1);
1715 }
1716 else if (pid == 0)
1717 {
1718 /* This is child process. */
1719 switch (argc)
1720 {
1721 case 0:
hassofa2b17e2004-03-04 17:45:00 +00001722 ret = execlp (command, command, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001723 break;
1724 case 1:
hassofa2b17e2004-03-04 17:45:00 +00001725 ret = execlp (command, command, arg1, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001726 break;
1727 case 2:
hassofa2b17e2004-03-04 17:45:00 +00001728 ret = execlp (command, command, arg1, arg2, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001729 break;
1730 }
1731
1732 /* When execlp suceed, this part is not executed. */
1733 fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
1734 exit (1);
1735 }
1736 else
1737 {
1738 /* This is parent. */
1739 execute_flag = 1;
1740 ret = wait4 (pid, &status, 0, NULL);
1741 execute_flag = 0;
1742 }
1743 return 0;
1744}
1745
1746DEFUN (vtysh_ping,
1747 vtysh_ping_cmd,
1748 "ping WORD",
hasso4eeccf12003-06-25 10:49:55 +00001749 "Send echo messages\n"
paul718e3742002-12-13 20:15:29 +00001750 "Ping destination address or hostname\n")
1751{
1752 execute_command ("ping", 1, argv[0], NULL);
1753 return CMD_SUCCESS;
1754}
1755
hasso4eeccf12003-06-25 10:49:55 +00001756ALIAS (vtysh_ping,
1757 vtysh_ping_ip_cmd,
1758 "ping ip WORD",
1759 "Send echo messages\n"
1760 "IP echo\n"
1761 "Ping destination address or hostname\n")
1762
paul718e3742002-12-13 20:15:29 +00001763DEFUN (vtysh_traceroute,
1764 vtysh_traceroute_cmd,
1765 "traceroute WORD",
1766 "Trace route to destination\n"
1767 "Trace route to destination address or hostname\n")
1768{
1769 execute_command ("traceroute", 1, argv[0], NULL);
1770 return CMD_SUCCESS;
1771}
1772
hasso4eeccf12003-06-25 10:49:55 +00001773ALIAS (vtysh_traceroute,
1774 vtysh_traceroute_ip_cmd,
1775 "traceroute ip WORD",
1776 "Trace route to destination\n"
1777 "IP trace\n"
1778 "Trace route to destination address or hostname\n")
1779
1780#ifdef HAVE_IPV6
1781DEFUN (vtysh_ping6,
1782 vtysh_ping6_cmd,
1783 "ping ipv6 WORD",
1784 "Send echo messages\n"
1785 "IPv6 echo\n"
1786 "Ping destination address or hostname\n")
1787{
1788 execute_command ("ping6", 1, argv[0], NULL);
1789 return CMD_SUCCESS;
1790}
1791
1792DEFUN (vtysh_traceroute6,
1793 vtysh_traceroute6_cmd,
1794 "traceroute ipv6 WORD",
1795 "Trace route to destination\n"
1796 "IPv6 trace\n"
1797 "Trace route to destination address or hostname\n")
1798{
1799 execute_command ("traceroute6", 1, argv[0], NULL);
1800 return CMD_SUCCESS;
1801}
1802#endif
1803
paul718e3742002-12-13 20:15:29 +00001804DEFUN (vtysh_telnet,
1805 vtysh_telnet_cmd,
1806 "telnet WORD",
1807 "Open a telnet connection\n"
1808 "IP address or hostname of a remote system\n")
1809{
1810 execute_command ("telnet", 1, argv[0], NULL);
1811 return CMD_SUCCESS;
1812}
1813
1814DEFUN (vtysh_telnet_port,
1815 vtysh_telnet_port_cmd,
1816 "telnet WORD PORT",
1817 "Open a telnet connection\n"
1818 "IP address or hostname of a remote system\n"
1819 "TCP Port number\n")
1820{
1821 execute_command ("telnet", 2, argv[0], argv[1]);
1822 return CMD_SUCCESS;
1823}
1824
paul5087df52003-01-25 06:56:09 +00001825DEFUN (vtysh_ssh,
1826 vtysh_ssh_cmd,
1827 "ssh WORD",
1828 "Open an ssh connection\n"
1829 "[user@]host\n")
1830{
1831 execute_command ("ssh", 1, argv[0], NULL);
1832 return CMD_SUCCESS;
1833}
1834
paul718e3742002-12-13 20:15:29 +00001835DEFUN (vtysh_start_shell,
1836 vtysh_start_shell_cmd,
1837 "start-shell",
1838 "Start UNIX shell\n")
1839{
1840 execute_command ("sh", 0, NULL, NULL);
1841 return CMD_SUCCESS;
1842}
1843
1844DEFUN (vtysh_start_bash,
1845 vtysh_start_bash_cmd,
1846 "start-shell bash",
1847 "Start UNIX shell\n"
1848 "Start bash\n")
1849{
1850 execute_command ("bash", 0, NULL, NULL);
1851 return CMD_SUCCESS;
1852}
1853
1854DEFUN (vtysh_start_zsh,
1855 vtysh_start_zsh_cmd,
1856 "start-shell zsh",
1857 "Start UNIX shell\n"
1858 "Start Z shell\n")
1859{
1860 execute_command ("zsh", 0, NULL, NULL);
1861 return CMD_SUCCESS;
1862}
hassob094d262004-08-25 12:22:00 +00001863
paul718e3742002-12-13 20:15:29 +00001864void
1865vtysh_install_default (enum node_type node)
1866{
1867 install_element (node, &config_list_cmd);
1868}
1869
1870/* Making connection to protocol daemon. */
1871int
hassodda09522004-10-07 21:40:25 +00001872vtysh_connect (struct vtysh_client *vclient, const char *path)
paul718e3742002-12-13 20:15:29 +00001873{
1874 int ret;
1875 int sock, len;
1876 struct sockaddr_un addr;
1877 struct stat s_stat;
1878 uid_t euid;
1879 gid_t egid;
1880
1881 memset (vclient, 0, sizeof (struct vtysh_client));
1882 vclient->fd = -1;
1883
1884 /* Stat socket to see if we have permission to access it. */
1885 euid = geteuid();
1886 egid = getegid();
1887 ret = stat (path, &s_stat);
1888 if (ret < 0 && errno != ENOENT)
1889 {
1890 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
1891 path, strerror(errno));
1892 exit(1);
1893 }
1894
1895 if (ret >= 0)
1896 {
1897 if (! S_ISSOCK(s_stat.st_mode))
1898 {
1899 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1900 path);
1901 exit (1);
1902 }
1903
paul718e3742002-12-13 20:15:29 +00001904 }
1905
1906 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1907 if (sock < 0)
1908 {
1909#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001910 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
1911 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001912#endif /* DEBUG */
1913 return -1;
1914 }
1915
1916 memset (&addr, 0, sizeof (struct sockaddr_un));
1917 addr.sun_family = AF_UNIX;
1918 strncpy (addr.sun_path, path, strlen (path));
1919#ifdef HAVE_SUN_LEN
1920 len = addr.sun_len = SUN_LEN(&addr);
1921#else
1922 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1923#endif /* HAVE_SUN_LEN */
1924
1925 ret = connect (sock, (struct sockaddr *) &addr, len);
1926 if (ret < 0)
1927 {
1928#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001929 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
1930 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001931#endif /* DEBUG */
1932 close (sock);
1933 return -1;
1934 }
1935 vclient->fd = sock;
1936
1937 return 0;
1938}
1939
1940void
1941vtysh_connect_all()
1942{
1943 /* Clear each daemons client structure. */
paulfe067782003-04-07 16:10:05 +00001944 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1945 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1946 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1947 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1948 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1949 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
hassoc25e4582003-12-23 10:39:08 +00001950 vtysh_connect (&vtysh_client[VTYSH_INDEX_ISIS], ISIS_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +00001951}
1952
hasso95e735b2004-08-26 13:08:30 +00001953/* To disable readline's filename completion. */
pauldfc0d9b2003-04-18 23:55:29 +00001954char *
1955vtysh_completion_entry_function (const char *ignore, int invoking_key)
paul718e3742002-12-13 20:15:29 +00001956{
pauldfc0d9b2003-04-18 23:55:29 +00001957 return NULL;
paul718e3742002-12-13 20:15:29 +00001958}
1959
1960void
1961vtysh_readline_init ()
1962{
1963 /* readline related settings. */
1964 rl_bind_key ('?', vtysh_rl_describe);
paul68980082003-03-25 05:07:42 +00001965 rl_completion_entry_function = vtysh_completion_entry_function;
paul718e3742002-12-13 20:15:29 +00001966 rl_attempted_completion_function = (CPPFunction *)new_completion;
1967 /* do not append space after completion. It will be appended
hasso95e735b2004-08-26 13:08:30 +00001968 * in new_completion() function explicitly. */
paul718e3742002-12-13 20:15:29 +00001969 rl_completion_append_character = '\0';
1970}
1971
1972char *
1973vtysh_prompt ()
1974{
1975 struct utsname names;
1976 static char buf[100];
1977 const char*hostname;
1978 extern struct host host;
1979
1980 hostname = host.name;
1981
1982 if (!hostname)
1983 {
1984 uname (&names);
1985 hostname = names.nodename;
1986 }
1987
1988 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1989
1990 return buf;
1991}
1992
1993void
1994vtysh_init_vty ()
1995{
1996 /* Make vty structure. */
1997 vty = vty_new ();
1998 vty->type = VTY_SHELL;
1999 vty->node = VIEW_NODE;
2000
2001 /* Initialize commands. */
2002 cmd_init (0);
2003
2004 /* Install nodes. */
2005 install_node (&bgp_node, NULL);
2006 install_node (&rip_node, NULL);
2007 install_node (&interface_node, NULL);
2008 install_node (&rmap_node, NULL);
2009 install_node (&zebra_node, NULL);
2010 install_node (&bgp_vpnv4_node, NULL);
2011 install_node (&bgp_ipv4_node, NULL);
2012 install_node (&bgp_ipv4m_node, NULL);
2013/* #ifdef HAVE_IPV6 */
2014 install_node (&bgp_ipv6_node, NULL);
2015/* #endif */
2016 install_node (&ospf_node, NULL);
2017/* #ifdef HAVE_IPV6 */
2018 install_node (&ripng_node, NULL);
2019 install_node (&ospf6_node, NULL);
2020/* #endif */
2021 install_node (&keychain_node, NULL);
2022 install_node (&keychain_key_node, NULL);
hassoc25e4582003-12-23 10:39:08 +00002023 install_node (&isis_node, NULL);
hassoe7168df2004-10-03 20:11:32 +00002024 install_node (&vty_node, NULL);
paul718e3742002-12-13 20:15:29 +00002025
2026 vtysh_install_default (VIEW_NODE);
2027 vtysh_install_default (ENABLE_NODE);
2028 vtysh_install_default (CONFIG_NODE);
2029 vtysh_install_default (BGP_NODE);
2030 vtysh_install_default (RIP_NODE);
2031 vtysh_install_default (INTERFACE_NODE);
2032 vtysh_install_default (RMAP_NODE);
2033 vtysh_install_default (ZEBRA_NODE);
2034 vtysh_install_default (BGP_VPNV4_NODE);
2035 vtysh_install_default (BGP_IPV4_NODE);
2036 vtysh_install_default (BGP_IPV4M_NODE);
2037 vtysh_install_default (BGP_IPV6_NODE);
2038 vtysh_install_default (OSPF_NODE);
2039 vtysh_install_default (RIPNG_NODE);
2040 vtysh_install_default (OSPF6_NODE);
hassoc25e4582003-12-23 10:39:08 +00002041 vtysh_install_default (ISIS_NODE);
paul718e3742002-12-13 20:15:29 +00002042 vtysh_install_default (KEYCHAIN_NODE);
2043 vtysh_install_default (KEYCHAIN_KEY_NODE);
hassoe7168df2004-10-03 20:11:32 +00002044 vtysh_install_default (VTY_NODE);
paul718e3742002-12-13 20:15:29 +00002045
2046 install_element (VIEW_NODE, &vtysh_enable_cmd);
2047 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
2048 install_element (ENABLE_NODE, &vtysh_disable_cmd);
2049
2050 /* "exit" command. */
2051 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
2052 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
2053 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
2054 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
2055 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
2056 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
2057 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
2058 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
paul68980082003-03-25 05:07:42 +00002059 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
2060 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
paul718e3742002-12-13 20:15:29 +00002061 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
2062 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
paul68980082003-03-25 05:07:42 +00002063 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
2064 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
paul718e3742002-12-13 20:15:29 +00002065 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
2066 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
2067 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
2068 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
2069 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
2070 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
2071 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
2072 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
2073 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
2074 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
hassoc25e4582003-12-23 10:39:08 +00002075 install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
2076 install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
paul718e3742002-12-13 20:15:29 +00002077 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
2078 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
2079 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
2080 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
2081 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
2082 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
hassoe7168df2004-10-03 20:11:32 +00002083 install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
2084 install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002085
2086 /* "end" command. */
2087 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
2088 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
2089 install_element (RIP_NODE, &vtysh_end_all_cmd);
2090 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
2091 install_element (OSPF_NODE, &vtysh_end_all_cmd);
2092 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
2093 install_element (BGP_NODE, &vtysh_end_all_cmd);
2094 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
2095 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
2096 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
2097 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
hassoc25e4582003-12-23 10:39:08 +00002098 install_element (ISIS_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002099 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
2100 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
2101 install_element (RMAP_NODE, &vtysh_end_all_cmd);
hassoe7168df2004-10-03 20:11:32 +00002102 install_element (VTY_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002103
paul338a9912003-03-01 15:44:10 +00002104 install_element (INTERFACE_NODE, &interface_desc_cmd);
paul464dc8d2003-03-28 02:25:45 +00002105 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
paul718e3742002-12-13 20:15:29 +00002106 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
2107 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
2108 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
2109 install_element (CONFIG_NODE, &router_rip_cmd);
2110#ifdef HAVE_IPV6
2111 install_element (CONFIG_NODE, &router_ripng_cmd);
2112#endif
2113 install_element (CONFIG_NODE, &router_ospf_cmd);
2114#ifdef HAVE_IPV6
2115 install_element (CONFIG_NODE, &router_ospf6_cmd);
2116#endif
hassoc25e4582003-12-23 10:39:08 +00002117 install_element (CONFIG_NODE, &router_isis_cmd);
paul718e3742002-12-13 20:15:29 +00002118 install_element (CONFIG_NODE, &router_bgp_cmd);
2119 install_element (BGP_NODE, &address_family_vpnv4_cmd);
2120 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
2121 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
2122 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
2123#ifdef HAVE_IPV6
2124 install_element (BGP_NODE, &address_family_ipv6_cmd);
2125 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
2126#endif
2127 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
2128 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
2129 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
2130 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
2131 install_element (CONFIG_NODE, &key_chain_cmd);
2132 install_element (CONFIG_NODE, &route_map_cmd);
hassoe7168df2004-10-03 20:11:32 +00002133 install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002134 install_element (KEYCHAIN_NODE, &key_cmd);
2135 install_element (KEYCHAIN_NODE, &key_chain_cmd);
2136 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
2137 install_element (CONFIG_NODE, &vtysh_interface_cmd);
paul32d24632003-05-23 09:25:20 +00002138 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
paul718e3742002-12-13 20:15:29 +00002139 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
2140 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
2141 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
hasso4a6e2252003-05-25 11:51:29 +00002142 install_element (ENABLE_NODE, &vtysh_write_cmd);
paul718e3742002-12-13 20:15:29 +00002143
hasso95e735b2004-08-26 13:08:30 +00002144 /* "write terminal" command. */
paul718e3742002-12-13 20:15:29 +00002145 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
hassoe7168df2004-10-03 20:11:32 +00002146
2147 install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
2148 install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
paul718e3742002-12-13 20:15:29 +00002149
hasso95e735b2004-08-26 13:08:30 +00002150 /* "write memory" command. */
paul718e3742002-12-13 20:15:29 +00002151 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
paul718e3742002-12-13 20:15:29 +00002152
hasso34553cc2004-08-27 13:56:39 +00002153 install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
2154 install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
2155 install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
2156 install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
hassoe7168df2004-10-03 20:11:32 +00002157 install_element (VIEW_NODE, &vtysh_show_running_daemons_cmd);
2158 install_element (ENABLE_NODE, &vtysh_show_running_daemons_cmd);
hasso34553cc2004-08-27 13:56:39 +00002159
paul718e3742002-12-13 20:15:29 +00002160 install_element (VIEW_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002161 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002162 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002163 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
2164#ifdef HAVE_IPV6
2165 install_element (VIEW_NODE, &vtysh_ping6_cmd);
2166 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
2167#endif
paul718e3742002-12-13 20:15:29 +00002168 install_element (VIEW_NODE, &vtysh_telnet_cmd);
2169 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
paul5087df52003-01-25 06:56:09 +00002170 install_element (VIEW_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002171 install_element (ENABLE_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002172 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002173 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002174 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
2175#ifdef HAVE_IPV6
2176 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
2177 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
2178#endif
paul718e3742002-12-13 20:15:29 +00002179 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
2180 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
hasso67e29ab2004-08-26 22:21:31 +00002181 install_element (ENABLE_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002182 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
2183 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
2184 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
2185
paul718e3742002-12-13 20:15:29 +00002186 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
2187 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
2188 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
2189 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
2190 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
2191 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
2192 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
2193 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
2194 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
2195 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
hassoe7168df2004-10-03 20:11:32 +00002196
2197 install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
2198 install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
2199
2200 install_element (CONFIG_NODE, &vtysh_password_cmd);
2201 install_element (CONFIG_NODE, &vtysh_password_text_cmd);
2202 install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
2203 install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
2204 install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
2205
paul718e3742002-12-13 20:15:29 +00002206}