blob: c7271ff02d75cabf1167ac7b3642f63687acb9a5 [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 {
ajs85fb1e62004-11-11 14:03:39 +0000194 if ((numnulls == 3) && (nbytes == 1))
195 return buf[0];
196
paul718e3742002-12-13 20:15:29 +0000197 buf[nbytes] = '\0';
ajs85fb1e62004-11-11 14:03:39 +0000198 fputs (buf, fp);
paul718e3742002-12-13 20:15:29 +0000199 fflush (fp);
paul2852de12004-09-17 06:52:16 +0000200
paul0921d482004-10-11 18:21:55 +0000201 /* check for trailling \0\0\0<ret code>,
202 * even if split across reads
203 * (see lib/vty.c::vtysh_read)
204 */
paul2852de12004-09-17 06:52:16 +0000205 if (nbytes >= 4)
206 {
207 i = nbytes-4;
208 numnulls = 0;
209 }
210 else
211 i = 0;
212
paul0921d482004-10-11 18:21:55 +0000213 while (i < nbytes && numnulls < 3)
paul2852de12004-09-17 06:52:16 +0000214 {
215 if (buf[i++] == '\0')
216 numnulls++;
217 else
ajs85fb1e62004-11-11 14:03:39 +0000218 numnulls = 0;
paul2852de12004-09-17 06:52:16 +0000219 }
paul718e3742002-12-13 20:15:29 +0000220
ajs85fb1e62004-11-11 14:03:39 +0000221 /* got 3 or more trailing NULs? */
222 if ((numnulls >= 3) && (i < nbytes))
paul0921d482004-10-11 18:21:55 +0000223 return (buf[nbytes-1]);
paul718e3742002-12-13 20:15:29 +0000224 }
225 }
paul718e3742002-12-13 20:15:29 +0000226}
227
228void
229vtysh_exit_ripd_only ()
230{
231 vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], "exit", stdout);
232}
233
234
235void
236vtysh_pager_init ()
237{
hasso5a9c53d2004-08-27 14:23:28 +0000238 char *pager_defined;
239
240 pager_defined = getenv ("VTYSH_PAGER");
241
242 if (pager_defined)
243 vtysh_pager_name = strdup (pager_defined);
244 else
hasso34553cc2004-08-27 13:56:39 +0000245 vtysh_pager_name = strdup ("more");
paul718e3742002-12-13 20:15:29 +0000246}
247
248/* Command execution over the vty interface. */
249void
hassodda09522004-10-07 21:40:25 +0000250vtysh_execute_func (const char *line, int pager)
paul718e3742002-12-13 20:15:29 +0000251{
252 int ret, cmd_stat;
253 vector vline;
254 struct cmd_element *cmd;
255 FILE *fp = NULL;
paula805cc22003-05-01 14:29:48 +0000256 int closepager=0;
paul718e3742002-12-13 20:15:29 +0000257
hasso95e735b2004-08-26 13:08:30 +0000258 /* Split readline string up into the vector. */
paul718e3742002-12-13 20:15:29 +0000259 vline = cmd_make_strvec (line);
260
261 if (vline == NULL)
262 return;
263
264 ret = cmd_execute_command (vline, vty, &cmd);
265
266 cmd_free_strvec (vline);
267
268 switch (ret)
269 {
270 case CMD_WARNING:
271 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000272 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000273 break;
274 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000275 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000276 break;
277 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000278 fprintf (stdout,"%% Unknown command.\n");
paul718e3742002-12-13 20:15:29 +0000279 break;
280 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000281 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000282 break;
283 case CMD_SUCCESS_DAEMON:
284 {
hasso97b7db22004-10-20 19:07:48 +0000285 /* FIXME: Don't open pager for exit commands. popen() causes problems
286 * if exited from vtysh at all. This hack shouldn't cause any problem
287 * but is really ugly. */
288 if (pager && vtysh_pager_name && (strncmp(line, "exit", 4) != 0))
paul718e3742002-12-13 20:15:29 +0000289 {
paul4fc01e62002-12-13 20:49:00 +0000290 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +0000291 if (fp == NULL)
292 {
paula805cc22003-05-01 14:29:48 +0000293 perror ("popen failed for pager");
294 fp = stdout;
paul718e3742002-12-13 20:15:29 +0000295 }
paula805cc22003-05-01 14:29:48 +0000296 else
297 closepager=1;
paul718e3742002-12-13 20:15:29 +0000298 }
299 else
300 fp = stdout;
301
302 if (! strcmp(cmd->string,"configure terminal"))
303 {
304 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
305 line, fp);
306 if (cmd_stat != CMD_WARNING)
307 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
308 line, fp);
309 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000310 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
311 line, fp);
paul718e3742002-12-13 20:15:29 +0000312 if (cmd_stat != CMD_WARNING)
313 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
314 line, fp);
315 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000316 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
317 line, fp);
paul718e3742002-12-13 20:15:29 +0000318 if (cmd_stat != CMD_WARNING)
319 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
320 line, fp);
hassob094d262004-08-25 12:22:00 +0000321 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000322 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
323 line, fp);
paul718e3742002-12-13 20:15:29 +0000324 if (cmd_stat)
325 {
hassob094d262004-08-25 12:22:00 +0000326 line = "end";
327 vline = cmd_make_strvec (line);
paul718e3742002-12-13 20:15:29 +0000328
hassob094d262004-08-25 12:22:00 +0000329 if (vline == NULL)
paul718e3742002-12-13 20:15:29 +0000330 {
paula805cc22003-05-01 14:29:48 +0000331 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000332 {
333 if (pclose (fp) == -1)
334 {
paula805cc22003-05-01 14:29:48 +0000335 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000336 }
337 fp = NULL;
338 }
339 return;
340 }
341
hassob094d262004-08-25 12:22:00 +0000342 ret = cmd_execute_command (vline, vty, &cmd);
343 cmd_free_strvec (vline);
344 if (ret != CMD_SUCCESS_DAEMON)
345 break;
paul718e3742002-12-13 20:15:29 +0000346 }
347 else
348 if (cmd->func)
349 {
350 (*cmd->func) (cmd, vty, 0, NULL);
351 break;
hassob094d262004-08-25 12:22:00 +0000352 }
paul718e3742002-12-13 20:15:29 +0000353 }
354
355 if (cmd->daemon & VTYSH_ZEBRA)
356 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, fp)
357 != CMD_SUCCESS)
358 break;
359 if (cmd->daemon & VTYSH_RIPD)
360 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, fp)
361 != CMD_SUCCESS)
362 break;
363 if (cmd->daemon & VTYSH_RIPNGD)
364 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp)
365 != CMD_SUCCESS)
366 break;
367 if (cmd->daemon & VTYSH_OSPFD)
368 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, fp)
369 != CMD_SUCCESS)
370 break;
371 if (cmd->daemon & VTYSH_OSPF6D)
372 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp)
373 != CMD_SUCCESS)
374 break;
375 if (cmd->daemon & VTYSH_BGPD)
376 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, fp)
377 != CMD_SUCCESS)
378 break;
hassob094d262004-08-25 12:22:00 +0000379 if (cmd->daemon & VTYSH_ISISD)
380 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, fp)
381 != CMD_SUCCESS)
382 break;
paul718e3742002-12-13 20:15:29 +0000383 if (cmd->func)
384 (*cmd->func) (cmd, vty, 0, NULL);
385 }
386 }
paula805cc22003-05-01 14:29:48 +0000387 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000388 {
389 if (pclose (fp) == -1)
390 {
paula805cc22003-05-01 14:29:48 +0000391 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000392 }
393 fp = NULL;
394 }
395}
396
397void
hassodda09522004-10-07 21:40:25 +0000398vtysh_execute_no_pager (const char *line)
paul718e3742002-12-13 20:15:29 +0000399{
400 vtysh_execute_func (line, 0);
401}
402
403void
hassodda09522004-10-07 21:40:25 +0000404vtysh_execute (const char *line)
paul718e3742002-12-13 20:15:29 +0000405{
406 vtysh_execute_func (line, 1);
407}
408
409/* Configration make from file. */
410int
411vtysh_config_from_file (struct vty *vty, FILE *fp)
412{
413 int ret;
414 vector vline;
415 struct cmd_element *cmd;
416
417 while (fgets (vty->buf, VTY_BUFSIZ, fp))
418 {
419 if (vty->buf[0] == '!' || vty->buf[1] == '#')
420 continue;
421
422 vline = cmd_make_strvec (vty->buf);
423
hasso95e735b2004-08-26 13:08:30 +0000424 /* In case of comment line. */
paul718e3742002-12-13 20:15:29 +0000425 if (vline == NULL)
426 continue;
427
hasso95e735b2004-08-26 13:08:30 +0000428 /* Execute configuration command : this is strict match. */
paul718e3742002-12-13 20:15:29 +0000429 ret = cmd_execute_command_strict (vline, vty, &cmd);
430
hasso95e735b2004-08-26 13:08:30 +0000431 /* Try again with setting node to CONFIG_NODE. */
paul718e3742002-12-13 20:15:29 +0000432 if (ret != CMD_SUCCESS
433 && ret != CMD_SUCCESS_DAEMON
434 && ret != CMD_WARNING)
435 {
436 if (vty->node == KEYCHAIN_KEY_NODE)
437 {
438 vty->node = KEYCHAIN_NODE;
439 vtysh_exit_ripd_only ();
440 ret = cmd_execute_command_strict (vline, vty, &cmd);
441
442 if (ret != CMD_SUCCESS
443 && ret != CMD_SUCCESS_DAEMON
444 && ret != CMD_WARNING)
445 {
446 vtysh_exit_ripd_only ();
447 vty->node = CONFIG_NODE;
448 ret = cmd_execute_command_strict (vline, vty, &cmd);
449 }
450 }
451 else
452 {
453 vtysh_execute ("end");
454 vtysh_execute ("configure terminal");
455 vty->node = CONFIG_NODE;
456 ret = cmd_execute_command_strict (vline, vty, &cmd);
457 }
458 }
459
460 cmd_free_strvec (vline);
461
462 switch (ret)
463 {
464 case CMD_WARNING:
465 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000466 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000467 break;
468 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000469 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000470 break;
471 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000472 fprintf (stdout,"%% Unknown command: %s", vty->buf);
paul718e3742002-12-13 20:15:29 +0000473 break;
474 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000475 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000476 break;
477 case CMD_SUCCESS_DAEMON:
478 {
479 if (cmd->daemon & VTYSH_ZEBRA)
480 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
481 vty->buf, stdout) != CMD_SUCCESS)
482 break;
483 if (cmd->daemon & VTYSH_RIPD)
484 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
485 vty->buf, stdout) != CMD_SUCCESS)
486 break;
487 if (cmd->daemon & VTYSH_RIPNGD)
488 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
489 vty->buf, stdout) != CMD_SUCCESS)
490 break;
491 if (cmd->daemon & VTYSH_OSPFD)
492 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
493 vty->buf, stdout) != CMD_SUCCESS)
494 break;
495 if (cmd->daemon & VTYSH_OSPF6D)
496 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
497 vty->buf, stdout) != CMD_SUCCESS)
498 break;
499 if (cmd->daemon & VTYSH_BGPD)
500 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
501 vty->buf, stdout) != CMD_SUCCESS)
502 break;
hassob094d262004-08-25 12:22:00 +0000503 if (cmd->daemon & VTYSH_ISISD)
504 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
505 vty->buf, stdout) != CMD_SUCCESS)
506 break;
paul718e3742002-12-13 20:15:29 +0000507 if (cmd->func)
508 (*cmd->func) (cmd, vty, 0, NULL);
509 }
510 }
511 }
512 return CMD_SUCCESS;
513}
514
515/* We don't care about the point of the cursor when '?' is typed. */
516int
517vtysh_rl_describe ()
518{
519 int ret;
hassodda09522004-10-07 21:40:25 +0000520 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000521 vector vline;
522 vector describe;
523 int width;
524 struct desc *desc;
525
526 vline = cmd_make_strvec (rl_line_buffer);
527
528 /* In case of '> ?'. */
529 if (vline == NULL)
530 {
531 vline = vector_init (1);
532 vector_set (vline, '\0');
533 }
534 else
535 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
536 vector_set (vline, '\0');
537
538 describe = cmd_describe_command (vline, vty, &ret);
539
paul4fc01e62002-12-13 20:49:00 +0000540 fprintf (stdout,"\n");
paul718e3742002-12-13 20:15:29 +0000541
542 /* Ambiguous and no match error. */
543 switch (ret)
544 {
545 case CMD_ERR_AMBIGUOUS:
546 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000547 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000548 rl_on_new_line ();
549 return 0;
550 break;
551 case CMD_ERR_NO_MATCH:
552 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000553 fprintf (stdout,"%% There is no matched command.\n");
paul718e3742002-12-13 20:15:29 +0000554 rl_on_new_line ();
555 return 0;
556 break;
557 }
558
559 /* Get width of command string. */
560 width = 0;
561 for (i = 0; i < vector_max (describe); i++)
562 if ((desc = vector_slot (describe, i)) != NULL)
563 {
564 int len;
565
566 if (desc->cmd[0] == '\0')
567 continue;
568
569 len = strlen (desc->cmd);
570 if (desc->cmd[0] == '.')
571 len--;
572
573 if (width < len)
574 width = len;
575 }
576
577 for (i = 0; i < vector_max (describe); i++)
578 if ((desc = vector_slot (describe, i)) != NULL)
579 {
580 if (desc->cmd[0] == '\0')
581 continue;
582
583 if (! desc->str)
paul4fc01e62002-12-13 20:49:00 +0000584 fprintf (stdout," %-s\n",
hassob094d262004-08-25 12:22:00 +0000585 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
paul718e3742002-12-13 20:15:29 +0000586 else
paul4fc01e62002-12-13 20:49:00 +0000587 fprintf (stdout," %-*s %s\n",
hassob094d262004-08-25 12:22:00 +0000588 width,
589 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
590 desc->str);
paul718e3742002-12-13 20:15:29 +0000591 }
592
593 cmd_free_strvec (vline);
594 vector_free (describe);
595
596 rl_on_new_line();
597
598 return 0;
599}
600
hasso95e735b2004-08-26 13:08:30 +0000601/* Result of cmd_complete_command() call will be stored here
602 * and used in new_completion() in order to put the space in
603 * correct places only. */
paul718e3742002-12-13 20:15:29 +0000604int complete_status;
605
606char *
pauldfc0d9b2003-04-18 23:55:29 +0000607command_generator (const char *text, int state)
paul718e3742002-12-13 20:15:29 +0000608{
609 vector vline;
610 static char **matched = NULL;
611 static int index = 0;
612
613 /* First call. */
614 if (! state)
615 {
616 index = 0;
617
618 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
619 return NULL;
620
621 vline = cmd_make_strvec (rl_line_buffer);
622 if (vline == NULL)
623 return NULL;
624
625 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
626 vector_set (vline, '\0');
627
628 matched = cmd_complete_command (vline, vty, &complete_status);
629 }
630
631 if (matched && matched[index])
632 return matched[index++];
633
634 return NULL;
635}
636
637char **
638new_completion (char *text, int start, int end)
639{
640 char **matches;
641
pauldfc0d9b2003-04-18 23:55:29 +0000642 matches = rl_completion_matches (text, command_generator);
paul718e3742002-12-13 20:15:29 +0000643
644 if (matches)
645 {
646 rl_point = rl_end;
647 if (complete_status == CMD_COMPLETE_FULL_MATCH)
648 rl_pending_input = ' ';
649 }
650
651 return matches;
652}
653
654char **
655vtysh_completion (char *text, int start, int end)
656{
657 int ret;
658 vector vline;
659 char **matched = NULL;
660
661 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
662 return NULL;
663
664 vline = cmd_make_strvec (rl_line_buffer);
665 if (vline == NULL)
666 return NULL;
667
668 /* In case of 'help \t'. */
669 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
670 vector_set (vline, '\0');
671
672 matched = cmd_complete_command (vline, vty, &ret);
673
674 cmd_free_strvec (vline);
675
676 return (char **) matched;
677}
678
hasso95e735b2004-08-26 13:08:30 +0000679/* Vty node structures. */
paul718e3742002-12-13 20:15:29 +0000680struct cmd_node bgp_node =
681{
682 BGP_NODE,
683 "%s(config-router)# ",
684};
685
paul718e3742002-12-13 20:15:29 +0000686struct cmd_node rip_node =
687{
688 RIP_NODE,
689 "%s(config-router)# ",
690};
691
hassoc25e4582003-12-23 10:39:08 +0000692struct cmd_node isis_node =
693{
694 ISIS_NODE,
695 "%s(config-router)# ",
hassoc25e4582003-12-23 10:39:08 +0000696};
697
paul718e3742002-12-13 20:15:29 +0000698struct cmd_node interface_node =
699{
700 INTERFACE_NODE,
701 "%s(config-if)# ",
702};
703
hasso95e735b2004-08-26 13:08:30 +0000704struct cmd_node rmap_node =
705{
706 RMAP_NODE,
707 "%s(config-route-map)# "
708};
709
710struct cmd_node zebra_node =
711{
712 ZEBRA_NODE,
713 "%s(config-router)# "
714};
715
716struct cmd_node bgp_vpnv4_node =
717{
718 BGP_VPNV4_NODE,
719 "%s(config-router-af)# "
720};
721
722struct cmd_node bgp_ipv4_node =
723{
724 BGP_IPV4_NODE,
725 "%s(config-router-af)# "
726};
727
728struct cmd_node bgp_ipv4m_node =
729{
730 BGP_IPV4M_NODE,
731 "%s(config-router-af)# "
732};
733
734struct cmd_node bgp_ipv6_node =
735{
736 BGP_IPV6_NODE,
737 "%s(config-router-af)# "
738};
739
740struct cmd_node ospf_node =
741{
742 OSPF_NODE,
743 "%s(config-router)# "
744};
745
746struct cmd_node ripng_node =
747{
748 RIPNG_NODE,
749 "%s(config-router)# "
750};
751
752struct cmd_node ospf6_node =
753{
754 OSPF6_NODE,
755 "%s(config-ospf6)# "
756};
757
758struct cmd_node keychain_node =
759{
760 KEYCHAIN_NODE,
761 "%s(config-keychain)# "
762};
763
764struct cmd_node keychain_key_node =
765{
766 KEYCHAIN_KEY_NODE,
767 "%s(config-keychain-key)# "
768};
769
hassoe7168df2004-10-03 20:11:32 +0000770/* Defined in lib/vty.c */
771extern struct cmd_node vty_node;
772
hasso95e735b2004-08-26 13:08:30 +0000773/* When '^Z' is received from vty, move down to the enable mode. */
774int
775vtysh_end ()
776{
777 switch (vty->node)
778 {
779 case VIEW_NODE:
780 case ENABLE_NODE:
781 /* Nothing to do. */
782 break;
783 default:
784 vty->node = ENABLE_NODE;
785 break;
786 }
787 return CMD_SUCCESS;
788}
789
790DEFUNSH (VTYSH_ALL,
791 vtysh_end_all,
792 vtysh_end_all_cmd,
793 "end",
hassoe7168df2004-10-03 20:11:32 +0000794 "End current mode and change to enable mode\n")
hasso95e735b2004-08-26 13:08:30 +0000795{
hasso42895462004-09-26 16:25:07 +0000796 return vtysh_end ();
hasso95e735b2004-08-26 13:08:30 +0000797}
798
paul718e3742002-12-13 20:15:29 +0000799DEFUNSH (VTYSH_BGPD,
800 router_bgp,
801 router_bgp_cmd,
802 "router bgp <1-65535>",
803 ROUTER_STR
804 BGP_STR
805 AS_STR)
806{
807 vty->node = BGP_NODE;
808 return CMD_SUCCESS;
809}
810
811DEFUNSH (VTYSH_BGPD,
812 address_family_vpnv4,
813 address_family_vpnv4_cmd,
814 "address-family vpnv4",
815 "Enter Address Family command mode\n"
816 "Address family\n")
817{
818 vty->node = BGP_VPNV4_NODE;
819 return CMD_SUCCESS;
820}
821
822DEFUNSH (VTYSH_BGPD,
823 address_family_vpnv4_unicast,
824 address_family_vpnv4_unicast_cmd,
825 "address-family vpnv4 unicast",
826 "Enter Address Family command mode\n"
827 "Address family\n"
828 "Address Family Modifier\n")
829{
830 vty->node = BGP_VPNV4_NODE;
831 return CMD_SUCCESS;
832}
833
834DEFUNSH (VTYSH_BGPD,
835 address_family_ipv4_unicast,
836 address_family_ipv4_unicast_cmd,
837 "address-family ipv4 unicast",
838 "Enter Address Family command mode\n"
839 "Address family\n"
840 "Address Family Modifier\n")
841{
842 vty->node = BGP_IPV4_NODE;
843 return CMD_SUCCESS;
844}
845
846DEFUNSH (VTYSH_BGPD,
847 address_family_ipv4_multicast,
848 address_family_ipv4_multicast_cmd,
849 "address-family ipv4 multicast",
850 "Enter Address Family command mode\n"
851 "Address family\n"
852 "Address Family Modifier\n")
853{
854 vty->node = BGP_IPV4M_NODE;
855 return CMD_SUCCESS;
856}
857
858DEFUNSH (VTYSH_BGPD,
859 address_family_ipv6,
860 address_family_ipv6_cmd,
861 "address-family ipv6",
862 "Enter Address Family command mode\n"
863 "Address family\n")
864{
865 vty->node = BGP_IPV6_NODE;
866 return CMD_SUCCESS;
867}
868
869DEFUNSH (VTYSH_BGPD,
870 address_family_ipv6_unicast,
871 address_family_ipv6_unicast_cmd,
872 "address-family ipv6 unicast",
873 "Enter Address Family command mode\n"
874 "Address family\n"
875 "Address Family Modifier\n")
876{
877 vty->node = BGP_IPV6_NODE;
878 return CMD_SUCCESS;
879}
880
881DEFUNSH (VTYSH_RIPD,
882 key_chain,
883 key_chain_cmd,
884 "key chain WORD",
885 "Authentication key management\n"
886 "Key-chain management\n"
887 "Key-chain name\n")
888{
889 vty->node = KEYCHAIN_NODE;
890 return CMD_SUCCESS;
891}
892
893DEFUNSH (VTYSH_RIPD,
894 key,
895 key_cmd,
896 "key <0-2147483647>",
897 "Configure a key\n"
898 "Key identifier number\n")
899{
900 vty->node = KEYCHAIN_KEY_NODE;
901 return CMD_SUCCESS;
902}
903
904DEFUNSH (VTYSH_RIPD,
905 router_rip,
906 router_rip_cmd,
907 "router rip",
908 ROUTER_STR
909 "RIP")
910{
911 vty->node = RIP_NODE;
912 return CMD_SUCCESS;
913}
914
915DEFUNSH (VTYSH_RIPNGD,
916 router_ripng,
917 router_ripng_cmd,
918 "router ripng",
919 ROUTER_STR
920 "RIPng")
921{
922 vty->node = RIPNG_NODE;
923 return CMD_SUCCESS;
924}
925
926DEFUNSH (VTYSH_OSPFD,
927 router_ospf,
928 router_ospf_cmd,
929 "router ospf",
930 "Enable a routing process\n"
931 "Start OSPF configuration\n")
932{
933 vty->node = OSPF_NODE;
934 return CMD_SUCCESS;
935}
936
937DEFUNSH (VTYSH_OSPF6D,
938 router_ospf6,
939 router_ospf6_cmd,
940 "router ospf6",
941 OSPF6_ROUTER_STR
942 OSPF6_STR)
943{
944 vty->node = OSPF6_NODE;
945 return CMD_SUCCESS;
946}
947
hassoc25e4582003-12-23 10:39:08 +0000948DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +0000949 router_isis,
950 router_isis_cmd,
951 "router isis WORD",
952 ROUTER_STR
953 "ISO IS-IS\n"
954 "ISO Routing area tag")
hassoc25e4582003-12-23 10:39:08 +0000955{
956 vty->node = ISIS_NODE;
957 return CMD_SUCCESS;
958}
959
paul718e3742002-12-13 20:15:29 +0000960DEFUNSH (VTYSH_RMAP,
961 route_map,
962 route_map_cmd,
963 "route-map WORD (deny|permit) <1-65535>",
964 "Create route-map or enter route-map command mode\n"
965 "Route map tag\n"
966 "Route map denies set operations\n"
967 "Route map permits set operations\n"
968 "Sequence to insert to/delete from existing route-map entry\n")
969{
970 vty->node = RMAP_NODE;
971 return CMD_SUCCESS;
972}
973
paul718e3742002-12-13 20:15:29 +0000974DEFUNSH (VTYSH_ALL,
hassoe7168df2004-10-03 20:11:32 +0000975 vtysh_line_vty,
976 vtysh_line_vty_cmd,
977 "line vty",
978 "Configure a terminal line\n"
979 "Virtual terminal\n")
980{
981 vty->node = VTY_NODE;
982 return CMD_SUCCESS;
983}
984
985DEFUNSH (VTYSH_ALL,
paul718e3742002-12-13 20:15:29 +0000986 vtysh_enable,
987 vtysh_enable_cmd,
988 "enable",
989 "Turn on privileged mode command\n")
990{
991 vty->node = ENABLE_NODE;
992 return CMD_SUCCESS;
993}
994
paul718e3742002-12-13 20:15:29 +0000995DEFUNSH (VTYSH_ALL,
996 vtysh_disable,
997 vtysh_disable_cmd,
998 "disable",
999 "Turn off privileged mode command\n")
1000{
1001 if (vty->node == ENABLE_NODE)
1002 vty->node = VIEW_NODE;
1003 return CMD_SUCCESS;
1004}
1005
paul718e3742002-12-13 20:15:29 +00001006DEFUNSH (VTYSH_ALL,
1007 vtysh_config_terminal,
1008 vtysh_config_terminal_cmd,
1009 "configure terminal",
1010 "Configuration from vty interface\n"
1011 "Configuration terminal\n")
1012{
1013 vty->node = CONFIG_NODE;
1014 return CMD_SUCCESS;
1015}
1016
1017int
1018vtysh_exit (struct vty *vty)
1019{
1020 switch (vty->node)
1021 {
1022 case VIEW_NODE:
1023 case ENABLE_NODE:
1024 exit (0);
1025 break;
1026 case CONFIG_NODE:
1027 vty->node = ENABLE_NODE;
1028 break;
1029 case INTERFACE_NODE:
1030 case ZEBRA_NODE:
1031 case BGP_NODE:
1032 case RIP_NODE:
1033 case RIPNG_NODE:
1034 case OSPF_NODE:
1035 case OSPF6_NODE:
hassoc25e4582003-12-23 10:39:08 +00001036 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001037 case MASC_NODE:
1038 case RMAP_NODE:
1039 case VTY_NODE:
1040 case KEYCHAIN_NODE:
hasso2a56df92004-05-09 23:16:40 +00001041 vtysh_execute("end");
1042 vtysh_execute("configure terminal");
paul718e3742002-12-13 20:15:29 +00001043 vty->node = CONFIG_NODE;
1044 break;
1045 case BGP_VPNV4_NODE:
1046 case BGP_IPV4_NODE:
1047 case BGP_IPV4M_NODE:
1048 case BGP_IPV6_NODE:
1049 vty->node = BGP_NODE;
1050 break;
1051 case KEYCHAIN_KEY_NODE:
1052 vty->node = KEYCHAIN_NODE;
1053 break;
1054 default:
1055 break;
1056 }
1057 return CMD_SUCCESS;
1058}
1059
1060DEFUNSH (VTYSH_ALL,
1061 vtysh_exit_all,
1062 vtysh_exit_all_cmd,
1063 "exit",
1064 "Exit current mode and down to previous mode\n")
1065{
1066 return vtysh_exit (vty);
1067}
1068
1069ALIAS (vtysh_exit_all,
1070 vtysh_quit_all_cmd,
1071 "quit",
1072 "Exit current mode and down to previous mode\n")
1073
1074DEFUNSH (VTYSH_BGPD,
1075 exit_address_family,
1076 exit_address_family_cmd,
1077 "exit-address-family",
1078 "Exit from Address Family configuration mode\n")
1079{
1080 if (vty->node == BGP_IPV4_NODE
1081 || vty->node == BGP_IPV4M_NODE
1082 || vty->node == BGP_VPNV4_NODE
1083 || vty->node == BGP_IPV6_NODE)
1084 vty->node = BGP_NODE;
1085 return CMD_SUCCESS;
1086}
1087
1088DEFUNSH (VTYSH_ZEBRA,
1089 vtysh_exit_zebra,
1090 vtysh_exit_zebra_cmd,
1091 "exit",
1092 "Exit current mode and down to previous mode\n")
1093{
1094 return vtysh_exit (vty);
1095}
1096
1097ALIAS (vtysh_exit_zebra,
1098 vtysh_quit_zebra_cmd,
1099 "quit",
1100 "Exit current mode and down to previous mode\n")
1101
1102DEFUNSH (VTYSH_RIPD,
1103 vtysh_exit_ripd,
1104 vtysh_exit_ripd_cmd,
1105 "exit",
1106 "Exit current mode and down to previous mode\n")
1107{
1108 return vtysh_exit (vty);
1109}
1110
1111ALIAS (vtysh_exit_ripd,
1112 vtysh_quit_ripd_cmd,
1113 "quit",
1114 "Exit current mode and down to previous mode\n")
1115
paul68980082003-03-25 05:07:42 +00001116DEFUNSH (VTYSH_RIPNGD,
hassob094d262004-08-25 12:22:00 +00001117 vtysh_exit_ripngd,
1118 vtysh_exit_ripngd_cmd,
1119 "exit",
1120 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001121{
1122 return vtysh_exit (vty);
1123}
1124
1125ALIAS (vtysh_exit_ripngd,
1126 vtysh_quit_ripngd_cmd,
1127 "quit",
1128 "Exit current mode and down to previous mode\n")
1129
paul718e3742002-12-13 20:15:29 +00001130DEFUNSH (VTYSH_RMAP,
1131 vtysh_exit_rmap,
1132 vtysh_exit_rmap_cmd,
1133 "exit",
1134 "Exit current mode and down to previous mode\n")
1135{
1136 return vtysh_exit (vty);
1137}
1138
1139ALIAS (vtysh_exit_rmap,
1140 vtysh_quit_rmap_cmd,
1141 "quit",
1142 "Exit current mode and down to previous mode\n")
1143
1144DEFUNSH (VTYSH_BGPD,
1145 vtysh_exit_bgpd,
1146 vtysh_exit_bgpd_cmd,
1147 "exit",
1148 "Exit current mode and down to previous mode\n")
1149{
1150 return vtysh_exit (vty);
1151}
1152
1153ALIAS (vtysh_exit_bgpd,
1154 vtysh_quit_bgpd_cmd,
1155 "quit",
1156 "Exit current mode and down to previous mode\n")
1157
1158DEFUNSH (VTYSH_OSPFD,
1159 vtysh_exit_ospfd,
1160 vtysh_exit_ospfd_cmd,
1161 "exit",
1162 "Exit current mode and down to previous mode\n")
1163{
1164 return vtysh_exit (vty);
1165}
1166
1167ALIAS (vtysh_exit_ospfd,
1168 vtysh_quit_ospfd_cmd,
1169 "quit",
1170 "Exit current mode and down to previous mode\n")
1171
paul68980082003-03-25 05:07:42 +00001172DEFUNSH (VTYSH_OSPF6D,
hassob094d262004-08-25 12:22:00 +00001173 vtysh_exit_ospf6d,
1174 vtysh_exit_ospf6d_cmd,
1175 "exit",
1176 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001177{
1178 return vtysh_exit (vty);
1179}
1180
1181ALIAS (vtysh_exit_ospf6d,
1182 vtysh_quit_ospf6d_cmd,
1183 "quit",
1184 "Exit current mode and down to previous mode\n")
1185
hassoc25e4582003-12-23 10:39:08 +00001186DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +00001187 vtysh_exit_isisd,
1188 vtysh_exit_isisd_cmd,
1189 "exit",
1190 "Exit current mode and down to previous mode\n")
hassoc25e4582003-12-23 10:39:08 +00001191{
1192 return vtysh_exit (vty);
1193}
1194
1195ALIAS (vtysh_exit_isisd,
1196 vtysh_quit_isisd_cmd,
1197 "quit",
1198 "Exit current mode and down to previous mode\n")
1199
hassoe7168df2004-10-03 20:11:32 +00001200DEFUNSH (VTYSH_ALL,
1201 vtysh_exit_line_vty,
1202 vtysh_exit_line_vty_cmd,
1203 "exit",
1204 "Exit current mode and down to previous mode\n")
1205{
1206 return vtysh_exit (vty);
1207}
1208
1209ALIAS (vtysh_exit_line_vty,
1210 vtysh_quit_line_vty_cmd,
1211 "quit",
1212 "Exit current mode and down to previous mode\n")
1213
hasso95e735b2004-08-26 13:08:30 +00001214DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001215 vtysh_interface,
1216 vtysh_interface_cmd,
1217 "interface IFNAME",
1218 "Select an interface to configure\n"
1219 "Interface's name\n")
1220{
1221 vty->node = INTERFACE_NODE;
1222 return CMD_SUCCESS;
1223}
1224
hasso95e735b2004-08-26 13:08:30 +00001225/* TODO Implement "no interface command in isisd. */
paul32d24632003-05-23 09:25:20 +00001226DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
1227 vtysh_no_interface_cmd,
1228 "no interface IFNAME",
1229 NO_STR
1230 "Delete a pseudo interface's configuration\n"
1231 "Interface's name\n")
1232
hasso95e735b2004-08-26 13:08:30 +00001233/* TODO Implement interface description commands in ripngd, ospf6d
1234 * and isisd. */
paul338a9912003-03-01 15:44:10 +00001235DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1236 interface_desc_cmd,
1237 "description .LINE",
1238 "Interface specific description\n"
1239 "Characters describing this interface\n")
paul464dc8d2003-03-28 02:25:45 +00001240
1241DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1242 no_interface_desc_cmd,
1243 "no description",
1244 NO_STR
1245 "Interface specific description\n")
paul338a9912003-03-01 15:44:10 +00001246
hasso95e735b2004-08-26 13:08:30 +00001247DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001248 vtysh_exit_interface,
1249 vtysh_exit_interface_cmd,
1250 "exit",
1251 "Exit current mode and down to previous mode\n")
1252{
1253 return vtysh_exit (vty);
1254}
1255
1256ALIAS (vtysh_exit_interface,
1257 vtysh_quit_interface_cmd,
1258 "quit",
1259 "Exit current mode and down to previous mode\n")
1260
hasso95e735b2004-08-26 13:08:30 +00001261/* Logging commands. */
1262DEFUNSH (VTYSH_ALL,
1263 vtysh_log_stdout,
1264 vtysh_log_stdout_cmd,
1265 "log stdout",
1266 "Logging control\n"
1267 "Logging goes to stdout\n")
1268{
1269 return CMD_SUCCESS;
1270}
1271
1272DEFUNSH (VTYSH_ALL,
1273 no_vtysh_log_stdout,
1274 no_vtysh_log_stdout_cmd,
1275 "no log stdout",
1276 NO_STR
1277 "Logging control\n"
1278 "Logging goes to stdout\n")
1279{
1280 return CMD_SUCCESS;
1281}
1282
1283DEFUNSH (VTYSH_ALL,
1284 vtysh_log_file,
1285 vtysh_log_file_cmd,
1286 "log file FILENAME",
1287 "Logging control\n"
1288 "Logging to file\n"
1289 "Logging filename\n")
1290{
1291 return CMD_SUCCESS;
1292}
1293
1294DEFUNSH (VTYSH_ALL,
1295 no_vtysh_log_file,
1296 no_vtysh_log_file_cmd,
1297 "no log file [FILENAME]",
1298 NO_STR
1299 "Logging control\n"
1300 "Cancel logging to file\n"
1301 "Logging file name\n")
1302{
1303 return CMD_SUCCESS;
1304}
1305
1306DEFUNSH (VTYSH_ALL,
1307 vtysh_log_syslog,
1308 vtysh_log_syslog_cmd,
1309 "log syslog",
1310 "Logging control\n"
1311 "Logging goes to syslog\n")
1312{
1313 return CMD_SUCCESS;
1314}
1315
1316DEFUNSH (VTYSH_ALL,
1317 no_vtysh_log_syslog,
1318 no_vtysh_log_syslog_cmd,
1319 "no log syslog",
1320 NO_STR
1321 "Logging control\n"
1322 "Cancel logging to syslog\n")
1323{
1324 return CMD_SUCCESS;
1325}
1326
1327DEFUNSH (VTYSH_ALL,
1328 vtysh_log_trap,
1329 vtysh_log_trap_cmd,
1330 "log trap (emergencies|alerts|critical|errors|warnings|\
1331 notifications|informational|debugging)",
1332 "Logging control\n"
1333 "Limit logging to specifed level\n")
1334{
1335 return CMD_SUCCESS;
1336}
1337
1338DEFUNSH (VTYSH_ALL,
1339 no_vtysh_log_trap,
1340 no_vtysh_log_trap_cmd,
1341 "no log trap",
1342 NO_STR
1343 "Logging control\n"
1344 "Permit all logging information\n")
1345{
1346 return CMD_SUCCESS;
1347}
1348
1349DEFUNSH (VTYSH_ALL,
1350 vtysh_log_record_priority,
1351 vtysh_log_record_priority_cmd,
1352 "log record-priority",
1353 "Logging control\n"
1354 "Log the priority of the message within the message\n")
1355{
1356 return CMD_SUCCESS;
1357}
1358
1359DEFUNSH (VTYSH_ALL,
1360 no_vtysh_log_record_priority,
1361 no_vtysh_log_record_priority_cmd,
1362 "no log record-priority",
1363 NO_STR
1364 "Logging control\n"
1365 "Do not log the priority of the message within the message\n")
1366{
1367 return CMD_SUCCESS;
1368}
1369
hassoe7168df2004-10-03 20:11:32 +00001370DEFUNSH (VTYSH_ALL,
1371 vtysh_service_password_encrypt,
1372 vtysh_service_password_encrypt_cmd,
1373 "service password-encryption",
1374 "Set up miscellaneous service\n"
1375 "Enable encrypted passwords\n")
1376{
1377 return CMD_SUCCESS;
1378}
1379
1380DEFUNSH (VTYSH_ALL,
1381 no_vtysh_service_password_encrypt,
1382 no_vtysh_service_password_encrypt_cmd,
1383 "no service password-encryption",
1384 NO_STR
1385 "Set up miscellaneous service\n"
1386 "Enable encrypted passwords\n")
1387{
1388 return CMD_SUCCESS;
1389}
1390
1391DEFUNSH (VTYSH_ALL,
1392 vtysh_config_password,
1393 vtysh_password_cmd,
1394 "password (8|) WORD",
1395 "Assign the terminal connection password\n"
1396 "Specifies a HIDDEN password will follow\n"
1397 "dummy string \n"
1398 "The HIDDEN line password string\n")
1399{
1400 return CMD_SUCCESS;
1401}
1402
1403DEFUNSH (VTYSH_ALL,
1404 vtysh_password_text,
1405 vtysh_password_text_cmd,
1406 "password LINE",
1407 "Assign the terminal connection password\n"
1408 "The UNENCRYPTED (cleartext) line password\n")
1409{
1410 return CMD_SUCCESS;
1411}
1412
1413DEFUNSH (VTYSH_ALL,
1414 vtysh_config_enable_password,
1415 vtysh_enable_password_cmd,
1416 "enable password (8|) WORD",
1417 "Modify enable password parameters\n"
1418 "Assign the privileged level password\n"
1419 "Specifies a HIDDEN password will follow\n"
1420 "dummy string \n"
1421 "The HIDDEN 'enable' password string\n")
1422{
1423 return CMD_SUCCESS;
1424}
1425
1426DEFUNSH (VTYSH_ALL,
1427 vtysh_enable_password_text,
1428 vtysh_enable_password_text_cmd,
1429 "enable password LINE",
1430 "Modify enable password parameters\n"
1431 "Assign the privileged level password\n"
1432 "The UNENCRYPTED (cleartext) 'enable' password\n")
1433{
1434 return CMD_SUCCESS;
1435}
1436
1437DEFUNSH (VTYSH_ALL,
1438 no_vtysh_config_enable_password,
1439 no_vtysh_enable_password_cmd,
1440 "no enable password",
1441 NO_STR
1442 "Modify enable password parameters\n"
1443 "Assign the privileged level password\n")
1444{
1445 return CMD_SUCCESS;
1446}
1447
paul718e3742002-12-13 20:15:29 +00001448DEFUN (vtysh_write_terminal,
1449 vtysh_write_terminal_cmd,
1450 "write terminal",
1451 "Write running configuration to memory, network, or terminal\n"
1452 "Write to terminal\n")
1453{
1454 int ret;
1455 char line[] = "write terminal\n";
1456 FILE *fp = NULL;
1457
1458 if (vtysh_pager_name)
1459 {
paul4fc01e62002-12-13 20:49:00 +00001460 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +00001461 if (fp == NULL)
1462 {
1463 perror ("popen");
1464 exit (1);
1465 }
1466 }
1467 else
1468 fp = stdout;
1469
1470 vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
1471 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
1472 VTY_NEWLINE);
hassoe7168df2004-10-03 20:11:32 +00001473 vty_out (vty, "!%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001474
1475 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1476 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1477 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1478 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1479 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1480 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001481 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001482
hassoe7168df2004-10-03 20:11:32 +00001483 /* Integrate vtysh specific configuration. */
1484 vtysh_config_write ();
1485
paul718e3742002-12-13 20:15:29 +00001486 vtysh_config_dump (fp);
1487
1488 if (vtysh_pager_name && fp)
1489 {
1490 fflush (fp);
1491 if (pclose (fp) == -1)
1492 {
1493 perror ("pclose");
1494 exit (1);
1495 }
1496 fp = NULL;
1497 }
1498
1499 return CMD_SUCCESS;
1500}
1501
hassoe7168df2004-10-03 20:11:32 +00001502DEFUN (vtysh_integrated_config,
1503 vtysh_integrated_config_cmd,
1504 "service integrated-vtysh-config",
1505 "Set up miscellaneous service\n"
1506 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001507{
hassoe7168df2004-10-03 20:11:32 +00001508 vtysh_writeconfig_integrated = 1;
1509 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001510}
1511
hassoe7168df2004-10-03 20:11:32 +00001512DEFUN (no_vtysh_integrated_config,
1513 no_vtysh_integrated_config_cmd,
1514 "no service integrated-vtysh-config",
1515 NO_STR
1516 "Set up miscellaneous service\n"
1517 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001518{
hassoe7168df2004-10-03 20:11:32 +00001519 vtysh_writeconfig_integrated = 0;
1520 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001521}
1522
1523int write_config_integrated(void)
paul718e3742002-12-13 20:15:29 +00001524{
1525 int ret;
paul718e3742002-12-13 20:15:29 +00001526 char line[] = "write terminal\n";
1527 FILE *fp;
1528 char *integrate_sav = NULL;
1529
hasso95e735b2004-08-26 13:08:30 +00001530 integrate_sav = malloc (strlen (integrate_default) +
1531 strlen (CONF_BACKUP_EXT) + 1);
paul718e3742002-12-13 20:15:29 +00001532 strcpy (integrate_sav, integrate_default);
1533 strcat (integrate_sav, CONF_BACKUP_EXT);
1534
paul4fc01e62002-12-13 20:49:00 +00001535 fprintf (stdout,"Building Configuration...\n");
paul718e3742002-12-13 20:15:29 +00001536
hasso95e735b2004-08-26 13:08:30 +00001537 /* Move current configuration file to backup config file. */
paul718e3742002-12-13 20:15:29 +00001538 unlink (integrate_sav);
1539 rename (integrate_default, integrate_sav);
hasso95e735b2004-08-26 13:08:30 +00001540 free (integrate_sav);
paul4fc01e62002-12-13 20:49:00 +00001541
paul718e3742002-12-13 20:15:29 +00001542 fp = fopen (integrate_default, "w");
1543 if (fp == NULL)
1544 {
hasso95e735b2004-08-26 13:08:30 +00001545 fprintf (stdout,"%% Can't open configuration file %s.\n",
1546 integrate_default);
paul718e3742002-12-13 20:15:29 +00001547 return CMD_SUCCESS;
1548 }
paul718e3742002-12-13 20:15:29 +00001549
paul718e3742002-12-13 20:15:29 +00001550 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1551 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1552 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1553 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1554 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1555 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001556 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001557
1558 vtysh_config_dump (fp);
1559
1560 fclose (fp);
1561
gdtaa593d52003-12-22 20:15:53 +00001562 if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
1563 {
1564 fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
1565 integrate_default, strerror(errno), errno);
1566 return CMD_WARNING;
1567 }
1568
paul4fc01e62002-12-13 20:49:00 +00001569 fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
1570
1571 fprintf (stdout,"[OK]\n");
1572
paul718e3742002-12-13 20:15:29 +00001573 return CMD_SUCCESS;
1574}
1575
paul4fc01e62002-12-13 20:49:00 +00001576DEFUN (vtysh_write_memory,
1577 vtysh_write_memory_cmd,
1578 "write memory",
1579 "Write running configuration to memory, network, or terminal\n"
1580 "Write configuration to the file (same as write file)\n")
1581{
pauldfc0d9b2003-04-18 23:55:29 +00001582 int ret = CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001583 char line[] = "write memory\n";
1584
hassoe7168df2004-10-03 20:11:32 +00001585 /* If integrated Quagga.conf explicitely set. */
1586 if (vtysh_writeconfig_integrated)
1587 return write_config_integrated();
paul4fc01e62002-12-13 20:49:00 +00001588
1589 fprintf (stdout,"Building Configuration...\n");
1590
1591 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1592 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1593 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1594 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1595 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1596 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
hassoc25e4582003-12-23 10:39:08 +00001597 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, stdout);
hassoe7168df2004-10-03 20:11:32 +00001598
paul4fc01e62002-12-13 20:49:00 +00001599 fprintf (stdout,"[OK]\n");
1600
pauldfc0d9b2003-04-18 23:55:29 +00001601 return ret;
paul4fc01e62002-12-13 20:49:00 +00001602}
1603
paul718e3742002-12-13 20:15:29 +00001604ALIAS (vtysh_write_memory,
1605 vtysh_copy_runningconfig_startupconfig_cmd,
1606 "copy running-config startup-config",
1607 "Copy from one file to another\n"
1608 "Copy from current system configuration\n"
1609 "Copy to startup configuration\n")
1610
1611ALIAS (vtysh_write_memory,
1612 vtysh_write_file_cmd,
1613 "write file",
1614 "Write running configuration to memory, network, or terminal\n"
1615 "Write configuration to the file (same as write memory)\n")
1616
hasso4a6e2252003-05-25 11:51:29 +00001617ALIAS (vtysh_write_memory,
1618 vtysh_write_cmd,
1619 "write",
1620 "Write running configuration to memory, network, or terminal\n")
1621
paul718e3742002-12-13 20:15:29 +00001622ALIAS (vtysh_write_terminal,
1623 vtysh_show_running_config_cmd,
1624 "show running-config",
1625 SHOW_STR
1626 "Current operating configuration\n")
hassob094d262004-08-25 12:22:00 +00001627
hasso34553cc2004-08-27 13:56:39 +00001628DEFUN (vtysh_terminal_length,
1629 vtysh_terminal_length_cmd,
1630 "terminal length <0-512>",
1631 "Set terminal line parameters\n"
1632 "Set number of lines on a screen\n"
1633 "Number of lines on screen (0 for no pausing)\n")
1634{
1635 int lines;
1636 char *endptr = NULL;
1637 char default_pager[10];
1638
1639 lines = strtol (argv[0], &endptr, 10);
1640 if (lines < 0 || lines > 512 || *endptr != '\0')
1641 {
1642 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
1643 return CMD_WARNING;
1644 }
1645
1646 if (vtysh_pager_name)
1647 {
1648 free (vtysh_pager_name);
1649 vtysh_pager_name = NULL;
1650 }
1651
1652 if (lines != 0)
1653 {
1654 snprintf(default_pager, 10, "more -%i", lines);
1655 vtysh_pager_name = strdup (default_pager);
1656 }
1657
1658 return CMD_SUCCESS;
1659}
1660
1661DEFUN (vtysh_terminal_no_length,
1662 vtysh_terminal_no_length_cmd,
1663 "terminal no length",
1664 "Set terminal line parameters\n"
1665 NO_STR
1666 "Set number of lines on a screen\n")
1667{
1668 if (vtysh_pager_name)
1669 {
1670 free (vtysh_pager_name);
1671 vtysh_pager_name = NULL;
1672 }
1673
1674 vtysh_pager_init();
1675 return CMD_SUCCESS;
1676}
1677
hassof2799e62004-10-28 17:43:11 +00001678DEFUN (vtysh_show_daemons,
1679 vtysh_show_daemons_cmd,
1680 "show daemons",
hassoe7168df2004-10-03 20:11:32 +00001681 SHOW_STR
1682 "Show list of running daemons\n")
1683{
1684 if ( vtysh_client[VTYSH_INDEX_ZEBRA].fd > 0 )
1685 vty_out(vty, " zebra");
1686 if ( vtysh_client[VTYSH_INDEX_RIP].fd > 0 )
1687 vty_out(vty, " ripd");
1688 if ( vtysh_client[VTYSH_INDEX_RIPNG].fd > 0 )
1689 vty_out(vty, " ripngd");
1690 if ( vtysh_client[VTYSH_INDEX_OSPF].fd > 0 )
1691 vty_out(vty, " ospfd");
1692 if ( vtysh_client[VTYSH_INDEX_OSPF6].fd > 0 )
1693 vty_out(vty, " ospf6d");
1694 if ( vtysh_client[VTYSH_INDEX_BGP].fd > 0 )
1695 vty_out(vty, " bgpd");
1696 if ( vtysh_client[VTYSH_INDEX_ISIS].fd > 0 )
1697 vty_out(vty, " isisd");
1698 vty_out(vty, "%s", VTY_NEWLINE);
1699
1700 return CMD_SUCCESS;
1701}
1702
paul718e3742002-12-13 20:15:29 +00001703/* Execute command in child process. */
1704int
hasso5862ff52004-10-11 13:20:40 +00001705execute_command (const char *command, int argc, const char *arg1,
1706 const char *arg2)
paul718e3742002-12-13 20:15:29 +00001707{
1708 int ret;
1709 pid_t pid;
1710 int status;
1711
1712 /* Call fork(). */
1713 pid = fork ();
1714
1715 if (pid < 0)
1716 {
1717 /* Failure of fork(). */
1718 fprintf (stderr, "Can't fork: %s\n", strerror (errno));
1719 exit (1);
1720 }
1721 else if (pid == 0)
1722 {
1723 /* This is child process. */
1724 switch (argc)
1725 {
1726 case 0:
hassofa2b17e2004-03-04 17:45:00 +00001727 ret = execlp (command, command, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001728 break;
1729 case 1:
hassofa2b17e2004-03-04 17:45:00 +00001730 ret = execlp (command, command, arg1, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001731 break;
1732 case 2:
hassofa2b17e2004-03-04 17:45:00 +00001733 ret = execlp (command, command, arg1, arg2, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001734 break;
1735 }
1736
1737 /* When execlp suceed, this part is not executed. */
1738 fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
1739 exit (1);
1740 }
1741 else
1742 {
1743 /* This is parent. */
1744 execute_flag = 1;
1745 ret = wait4 (pid, &status, 0, NULL);
1746 execute_flag = 0;
1747 }
1748 return 0;
1749}
1750
1751DEFUN (vtysh_ping,
1752 vtysh_ping_cmd,
1753 "ping WORD",
hasso4eeccf12003-06-25 10:49:55 +00001754 "Send echo messages\n"
paul718e3742002-12-13 20:15:29 +00001755 "Ping destination address or hostname\n")
1756{
1757 execute_command ("ping", 1, argv[0], NULL);
1758 return CMD_SUCCESS;
1759}
1760
hasso4eeccf12003-06-25 10:49:55 +00001761ALIAS (vtysh_ping,
1762 vtysh_ping_ip_cmd,
1763 "ping ip WORD",
1764 "Send echo messages\n"
1765 "IP echo\n"
1766 "Ping destination address or hostname\n")
1767
paul718e3742002-12-13 20:15:29 +00001768DEFUN (vtysh_traceroute,
1769 vtysh_traceroute_cmd,
1770 "traceroute WORD",
1771 "Trace route to destination\n"
1772 "Trace route to destination address or hostname\n")
1773{
1774 execute_command ("traceroute", 1, argv[0], NULL);
1775 return CMD_SUCCESS;
1776}
1777
hasso4eeccf12003-06-25 10:49:55 +00001778ALIAS (vtysh_traceroute,
1779 vtysh_traceroute_ip_cmd,
1780 "traceroute ip WORD",
1781 "Trace route to destination\n"
1782 "IP trace\n"
1783 "Trace route to destination address or hostname\n")
1784
1785#ifdef HAVE_IPV6
1786DEFUN (vtysh_ping6,
1787 vtysh_ping6_cmd,
1788 "ping ipv6 WORD",
1789 "Send echo messages\n"
1790 "IPv6 echo\n"
1791 "Ping destination address or hostname\n")
1792{
1793 execute_command ("ping6", 1, argv[0], NULL);
1794 return CMD_SUCCESS;
1795}
1796
1797DEFUN (vtysh_traceroute6,
1798 vtysh_traceroute6_cmd,
1799 "traceroute ipv6 WORD",
1800 "Trace route to destination\n"
1801 "IPv6 trace\n"
1802 "Trace route to destination address or hostname\n")
1803{
1804 execute_command ("traceroute6", 1, argv[0], NULL);
1805 return CMD_SUCCESS;
1806}
1807#endif
1808
paul718e3742002-12-13 20:15:29 +00001809DEFUN (vtysh_telnet,
1810 vtysh_telnet_cmd,
1811 "telnet WORD",
1812 "Open a telnet connection\n"
1813 "IP address or hostname of a remote system\n")
1814{
1815 execute_command ("telnet", 1, argv[0], NULL);
1816 return CMD_SUCCESS;
1817}
1818
1819DEFUN (vtysh_telnet_port,
1820 vtysh_telnet_port_cmd,
1821 "telnet WORD PORT",
1822 "Open a telnet connection\n"
1823 "IP address or hostname of a remote system\n"
1824 "TCP Port number\n")
1825{
1826 execute_command ("telnet", 2, argv[0], argv[1]);
1827 return CMD_SUCCESS;
1828}
1829
paul5087df52003-01-25 06:56:09 +00001830DEFUN (vtysh_ssh,
1831 vtysh_ssh_cmd,
1832 "ssh WORD",
1833 "Open an ssh connection\n"
1834 "[user@]host\n")
1835{
1836 execute_command ("ssh", 1, argv[0], NULL);
1837 return CMD_SUCCESS;
1838}
1839
paul718e3742002-12-13 20:15:29 +00001840DEFUN (vtysh_start_shell,
1841 vtysh_start_shell_cmd,
1842 "start-shell",
1843 "Start UNIX shell\n")
1844{
1845 execute_command ("sh", 0, NULL, NULL);
1846 return CMD_SUCCESS;
1847}
1848
1849DEFUN (vtysh_start_bash,
1850 vtysh_start_bash_cmd,
1851 "start-shell bash",
1852 "Start UNIX shell\n"
1853 "Start bash\n")
1854{
1855 execute_command ("bash", 0, NULL, NULL);
1856 return CMD_SUCCESS;
1857}
1858
1859DEFUN (vtysh_start_zsh,
1860 vtysh_start_zsh_cmd,
1861 "start-shell zsh",
1862 "Start UNIX shell\n"
1863 "Start Z shell\n")
1864{
1865 execute_command ("zsh", 0, NULL, NULL);
1866 return CMD_SUCCESS;
1867}
hassob094d262004-08-25 12:22:00 +00001868
paul718e3742002-12-13 20:15:29 +00001869void
1870vtysh_install_default (enum node_type node)
1871{
1872 install_element (node, &config_list_cmd);
1873}
1874
1875/* Making connection to protocol daemon. */
1876int
hassodda09522004-10-07 21:40:25 +00001877vtysh_connect (struct vtysh_client *vclient, const char *path)
paul718e3742002-12-13 20:15:29 +00001878{
1879 int ret;
1880 int sock, len;
1881 struct sockaddr_un addr;
1882 struct stat s_stat;
1883 uid_t euid;
1884 gid_t egid;
1885
1886 memset (vclient, 0, sizeof (struct vtysh_client));
1887 vclient->fd = -1;
1888
1889 /* Stat socket to see if we have permission to access it. */
1890 euid = geteuid();
1891 egid = getegid();
1892 ret = stat (path, &s_stat);
1893 if (ret < 0 && errno != ENOENT)
1894 {
1895 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
1896 path, strerror(errno));
1897 exit(1);
1898 }
1899
1900 if (ret >= 0)
1901 {
1902 if (! S_ISSOCK(s_stat.st_mode))
1903 {
1904 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1905 path);
1906 exit (1);
1907 }
1908
paul718e3742002-12-13 20:15:29 +00001909 }
1910
1911 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1912 if (sock < 0)
1913 {
1914#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001915 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
1916 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001917#endif /* DEBUG */
1918 return -1;
1919 }
1920
1921 memset (&addr, 0, sizeof (struct sockaddr_un));
1922 addr.sun_family = AF_UNIX;
1923 strncpy (addr.sun_path, path, strlen (path));
1924#ifdef HAVE_SUN_LEN
1925 len = addr.sun_len = SUN_LEN(&addr);
1926#else
1927 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1928#endif /* HAVE_SUN_LEN */
1929
1930 ret = connect (sock, (struct sockaddr *) &addr, len);
1931 if (ret < 0)
1932 {
1933#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001934 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
1935 strerror(errno));
paul718e3742002-12-13 20:15:29 +00001936#endif /* DEBUG */
1937 close (sock);
1938 return -1;
1939 }
1940 vclient->fd = sock;
1941
1942 return 0;
1943}
1944
1945void
1946vtysh_connect_all()
1947{
1948 /* Clear each daemons client structure. */
paulfe067782003-04-07 16:10:05 +00001949 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1950 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1951 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1952 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1953 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1954 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
hassoc25e4582003-12-23 10:39:08 +00001955 vtysh_connect (&vtysh_client[VTYSH_INDEX_ISIS], ISIS_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +00001956}
1957
hasso95e735b2004-08-26 13:08:30 +00001958/* To disable readline's filename completion. */
pauldfc0d9b2003-04-18 23:55:29 +00001959char *
1960vtysh_completion_entry_function (const char *ignore, int invoking_key)
paul718e3742002-12-13 20:15:29 +00001961{
pauldfc0d9b2003-04-18 23:55:29 +00001962 return NULL;
paul718e3742002-12-13 20:15:29 +00001963}
1964
1965void
1966vtysh_readline_init ()
1967{
1968 /* readline related settings. */
1969 rl_bind_key ('?', vtysh_rl_describe);
paul68980082003-03-25 05:07:42 +00001970 rl_completion_entry_function = vtysh_completion_entry_function;
paul718e3742002-12-13 20:15:29 +00001971 rl_attempted_completion_function = (CPPFunction *)new_completion;
1972 /* do not append space after completion. It will be appended
hasso95e735b2004-08-26 13:08:30 +00001973 * in new_completion() function explicitly. */
paul718e3742002-12-13 20:15:29 +00001974 rl_completion_append_character = '\0';
1975}
1976
1977char *
1978vtysh_prompt ()
1979{
1980 struct utsname names;
1981 static char buf[100];
1982 const char*hostname;
1983 extern struct host host;
1984
1985 hostname = host.name;
1986
1987 if (!hostname)
1988 {
1989 uname (&names);
1990 hostname = names.nodename;
1991 }
1992
1993 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1994
1995 return buf;
1996}
1997
1998void
1999vtysh_init_vty ()
2000{
2001 /* Make vty structure. */
2002 vty = vty_new ();
2003 vty->type = VTY_SHELL;
2004 vty->node = VIEW_NODE;
2005
2006 /* Initialize commands. */
2007 cmd_init (0);
2008
2009 /* Install nodes. */
2010 install_node (&bgp_node, NULL);
2011 install_node (&rip_node, NULL);
2012 install_node (&interface_node, NULL);
2013 install_node (&rmap_node, NULL);
2014 install_node (&zebra_node, NULL);
2015 install_node (&bgp_vpnv4_node, NULL);
2016 install_node (&bgp_ipv4_node, NULL);
2017 install_node (&bgp_ipv4m_node, NULL);
2018/* #ifdef HAVE_IPV6 */
2019 install_node (&bgp_ipv6_node, NULL);
2020/* #endif */
2021 install_node (&ospf_node, NULL);
2022/* #ifdef HAVE_IPV6 */
2023 install_node (&ripng_node, NULL);
2024 install_node (&ospf6_node, NULL);
2025/* #endif */
2026 install_node (&keychain_node, NULL);
2027 install_node (&keychain_key_node, NULL);
hassoc25e4582003-12-23 10:39:08 +00002028 install_node (&isis_node, NULL);
hassoe7168df2004-10-03 20:11:32 +00002029 install_node (&vty_node, NULL);
paul718e3742002-12-13 20:15:29 +00002030
2031 vtysh_install_default (VIEW_NODE);
2032 vtysh_install_default (ENABLE_NODE);
2033 vtysh_install_default (CONFIG_NODE);
2034 vtysh_install_default (BGP_NODE);
2035 vtysh_install_default (RIP_NODE);
2036 vtysh_install_default (INTERFACE_NODE);
2037 vtysh_install_default (RMAP_NODE);
2038 vtysh_install_default (ZEBRA_NODE);
2039 vtysh_install_default (BGP_VPNV4_NODE);
2040 vtysh_install_default (BGP_IPV4_NODE);
2041 vtysh_install_default (BGP_IPV4M_NODE);
2042 vtysh_install_default (BGP_IPV6_NODE);
2043 vtysh_install_default (OSPF_NODE);
2044 vtysh_install_default (RIPNG_NODE);
2045 vtysh_install_default (OSPF6_NODE);
hassoc25e4582003-12-23 10:39:08 +00002046 vtysh_install_default (ISIS_NODE);
paul718e3742002-12-13 20:15:29 +00002047 vtysh_install_default (KEYCHAIN_NODE);
2048 vtysh_install_default (KEYCHAIN_KEY_NODE);
hassoe7168df2004-10-03 20:11:32 +00002049 vtysh_install_default (VTY_NODE);
paul718e3742002-12-13 20:15:29 +00002050
2051 install_element (VIEW_NODE, &vtysh_enable_cmd);
2052 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
2053 install_element (ENABLE_NODE, &vtysh_disable_cmd);
2054
2055 /* "exit" command. */
2056 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
2057 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
2058 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
2059 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
2060 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
2061 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
2062 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
2063 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
paul68980082003-03-25 05:07:42 +00002064 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
2065 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
paul718e3742002-12-13 20:15:29 +00002066 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
2067 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
paul68980082003-03-25 05:07:42 +00002068 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
2069 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
paul718e3742002-12-13 20:15:29 +00002070 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
2071 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
2072 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
2073 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
2074 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
2075 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
2076 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
2077 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
2078 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
2079 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
hassoc25e4582003-12-23 10:39:08 +00002080 install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
2081 install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
paul718e3742002-12-13 20:15:29 +00002082 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
2083 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
2084 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
2085 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
2086 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
2087 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
hassoe7168df2004-10-03 20:11:32 +00002088 install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
2089 install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002090
2091 /* "end" command. */
2092 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
2093 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
2094 install_element (RIP_NODE, &vtysh_end_all_cmd);
2095 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
2096 install_element (OSPF_NODE, &vtysh_end_all_cmd);
2097 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
2098 install_element (BGP_NODE, &vtysh_end_all_cmd);
2099 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
2100 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
2101 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
2102 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
hassoc25e4582003-12-23 10:39:08 +00002103 install_element (ISIS_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002104 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
2105 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
2106 install_element (RMAP_NODE, &vtysh_end_all_cmd);
hassoe7168df2004-10-03 20:11:32 +00002107 install_element (VTY_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002108
paul338a9912003-03-01 15:44:10 +00002109 install_element (INTERFACE_NODE, &interface_desc_cmd);
paul464dc8d2003-03-28 02:25:45 +00002110 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
paul718e3742002-12-13 20:15:29 +00002111 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
2112 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
2113 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
2114 install_element (CONFIG_NODE, &router_rip_cmd);
2115#ifdef HAVE_IPV6
2116 install_element (CONFIG_NODE, &router_ripng_cmd);
2117#endif
2118 install_element (CONFIG_NODE, &router_ospf_cmd);
2119#ifdef HAVE_IPV6
2120 install_element (CONFIG_NODE, &router_ospf6_cmd);
2121#endif
hassoc25e4582003-12-23 10:39:08 +00002122 install_element (CONFIG_NODE, &router_isis_cmd);
paul718e3742002-12-13 20:15:29 +00002123 install_element (CONFIG_NODE, &router_bgp_cmd);
2124 install_element (BGP_NODE, &address_family_vpnv4_cmd);
2125 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
2126 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
2127 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
2128#ifdef HAVE_IPV6
2129 install_element (BGP_NODE, &address_family_ipv6_cmd);
2130 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
2131#endif
2132 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
2133 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
2134 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
2135 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
2136 install_element (CONFIG_NODE, &key_chain_cmd);
2137 install_element (CONFIG_NODE, &route_map_cmd);
hassoe7168df2004-10-03 20:11:32 +00002138 install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002139 install_element (KEYCHAIN_NODE, &key_cmd);
2140 install_element (KEYCHAIN_NODE, &key_chain_cmd);
2141 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
2142 install_element (CONFIG_NODE, &vtysh_interface_cmd);
paul32d24632003-05-23 09:25:20 +00002143 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
paul718e3742002-12-13 20:15:29 +00002144 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
2145 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
2146 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
hasso4a6e2252003-05-25 11:51:29 +00002147 install_element (ENABLE_NODE, &vtysh_write_cmd);
paul718e3742002-12-13 20:15:29 +00002148
hasso95e735b2004-08-26 13:08:30 +00002149 /* "write terminal" command. */
paul718e3742002-12-13 20:15:29 +00002150 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
hassoe7168df2004-10-03 20:11:32 +00002151
2152 install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
2153 install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
paul718e3742002-12-13 20:15:29 +00002154
hasso95e735b2004-08-26 13:08:30 +00002155 /* "write memory" command. */
paul718e3742002-12-13 20:15:29 +00002156 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
paul718e3742002-12-13 20:15:29 +00002157
hasso34553cc2004-08-27 13:56:39 +00002158 install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
2159 install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
2160 install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
2161 install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
hassof2799e62004-10-28 17:43:11 +00002162 install_element (VIEW_NODE, &vtysh_show_daemons_cmd);
2163 install_element (ENABLE_NODE, &vtysh_show_daemons_cmd);
hasso34553cc2004-08-27 13:56:39 +00002164
paul718e3742002-12-13 20:15:29 +00002165 install_element (VIEW_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002166 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002167 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002168 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
2169#ifdef HAVE_IPV6
2170 install_element (VIEW_NODE, &vtysh_ping6_cmd);
2171 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
2172#endif
paul718e3742002-12-13 20:15:29 +00002173 install_element (VIEW_NODE, &vtysh_telnet_cmd);
2174 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
paul5087df52003-01-25 06:56:09 +00002175 install_element (VIEW_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002176 install_element (ENABLE_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002177 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002178 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002179 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
2180#ifdef HAVE_IPV6
2181 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
2182 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
2183#endif
paul718e3742002-12-13 20:15:29 +00002184 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
2185 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
hasso67e29ab2004-08-26 22:21:31 +00002186 install_element (ENABLE_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002187 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
2188 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
2189 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
2190
paul718e3742002-12-13 20:15:29 +00002191 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
2192 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
2193 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
2194 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
2195 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
2196 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
2197 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
2198 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
2199 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
2200 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
hassoe7168df2004-10-03 20:11:32 +00002201
2202 install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
2203 install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
2204
2205 install_element (CONFIG_NODE, &vtysh_password_cmd);
2206 install_element (CONFIG_NODE, &vtysh_password_text_cmd);
2207 install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
2208 install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
2209 install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
2210
paul718e3742002-12-13 20:15:29 +00002211}