blob: 08bd1275cc95fda3385d00523081077ecbf71442 [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"
ajs6099b3b2004-11-20 02:06:59 +000036#include "log.h"
paul718e3742002-12-13 20:15:29 +000037
38/* Struct VTY. */
39struct vty *vty;
40
41/* VTY shell pager name. */
42char *vtysh_pager_name = NULL;
43
44/* VTY shell client structure. */
45struct vtysh_client
46{
47 int fd;
48} vtysh_client[VTYSH_INDEX_MAX];
hassob094d262004-08-25 12:22:00 +000049
hassoe7168df2004-10-03 20:11:32 +000050/* Using integrated config from Quagga.conf. Default is no. */
51int vtysh_writeconfig_integrated = 0;
52
53extern char config_default[];
54
paul718e3742002-12-13 20:15:29 +000055void
56vclient_close (struct vtysh_client *vclient)
57{
58 if (vclient->fd > 0)
59 close (vclient->fd);
60 vclient->fd = -1;
61}
62
paul718e3742002-12-13 20:15:29 +000063/* Following filled with debug code to trace a problematic condition
hasso95e735b2004-08-26 13:08:30 +000064 * under load - it SHOULD handle it. */
paul718e3742002-12-13 20:15:29 +000065#define ERR_WHERE_STRING "vtysh(): vtysh_client_config(): "
66int
67vtysh_client_config (struct vtysh_client *vclient, char *line)
68{
69 int ret;
70 char *buf;
71 size_t bufsz;
72 char *pbuf;
73 size_t left;
74 char *eoln;
75 int nbytes;
76 int i;
77 int readln;
78
79 if (vclient->fd < 0)
80 return CMD_SUCCESS;
81
82 ret = write (vclient->fd, line, strlen (line) + 1);
83 if (ret <= 0)
84 {
85 vclient_close (vclient);
86 return CMD_SUCCESS;
87 }
88
hasso95e735b2004-08-26 13:08:30 +000089 /* Allow enough room for buffer to read more than a few pages from socket. */
paule3d29b52003-01-23 18:05:42 +000090 bufsz = 5 * getpagesize() + 1;
paul718e3742002-12-13 20:15:29 +000091 buf = XMALLOC(MTYPE_TMP, bufsz);
92 memset(buf, 0, bufsz);
93 pbuf = buf;
94
95 while (1)
96 {
97 if (pbuf >= ((buf + bufsz) -1))
98 {
99 fprintf (stderr, ERR_WHERE_STRING \
100 "warning - pbuf beyond buffer end.\n");
101 return CMD_WARNING;
102 }
103
104 readln = (buf + bufsz) - pbuf - 1;
105 nbytes = read (vclient->fd, pbuf, readln);
106
107 if (nbytes <= 0)
108 {
109
110 if (errno == EINTR)
111 continue;
112
113 fprintf(stderr, ERR_WHERE_STRING "(%u)", errno);
114 perror("");
115
116 if (errno == EAGAIN || errno == EIO)
117 continue;
118
119 vclient_close (vclient);
120 XFREE(MTYPE_TMP, buf);
121 return CMD_SUCCESS;
122 }
123
124 pbuf[nbytes] = '\0';
125
126 if (nbytes >= 4)
127 {
128 i = nbytes - 4;
129 if (pbuf[i] == '\0' && pbuf[i + 1] == '\0' && pbuf[i + 2] == '\0')
130 {
131 ret = pbuf[i + 3];
132 break;
133 }
134 }
135 pbuf += nbytes;
136
137 /* See if a line exists in buffer, if so parse and consume it, and
hasso95e735b2004-08-26 13:08:30 +0000138 * reset read position. */
paul718e3742002-12-13 20:15:29 +0000139 if ((eoln = strrchr(buf, '\n')) == NULL)
140 continue;
141
142 if (eoln >= ((buf + bufsz) - 1))
143 {
144 fprintf (stderr, ERR_WHERE_STRING \
145 "warning - eoln beyond buffer end.\n");
146 }
147 vtysh_config_parse(buf);
148
149 eoln++;
150 left = (size_t)(buf + bufsz - eoln);
151 memmove(buf, eoln, left);
152 buf[bufsz-1] = '\0';
153 pbuf = buf + strlen(buf);
154 }
155
hasso95e735b2004-08-26 13:08:30 +0000156 /* Parse anything left in the buffer. */
hassoe7168df2004-10-03 20:11:32 +0000157
paul718e3742002-12-13 20:15:29 +0000158 vtysh_config_parse (buf);
159
160 XFREE(MTYPE_TMP, buf);
161 return ret;
162}
163
164int
hassodda09522004-10-07 21:40:25 +0000165vtysh_client_execute (struct vtysh_client *vclient, const char *line, FILE *fp)
paul718e3742002-12-13 20:15:29 +0000166{
167 int ret;
168 char buf[1001];
169 int nbytes;
paul2852de12004-09-17 06:52:16 +0000170 int i;
171 int numnulls = 0;
paul718e3742002-12-13 20:15:29 +0000172
173 if (vclient->fd < 0)
174 return CMD_SUCCESS;
175
176 ret = write (vclient->fd, line, strlen (line) + 1);
177 if (ret <= 0)
178 {
179 vclient_close (vclient);
180 return CMD_SUCCESS;
181 }
182
183 while (1)
184 {
185 nbytes = read (vclient->fd, buf, sizeof(buf)-1);
186
187 if (nbytes <= 0 && errno != EINTR)
188 {
189 vclient_close (vclient);
190 return CMD_SUCCESS;
191 }
192
193 if (nbytes > 0)
194 {
ajs85fb1e62004-11-11 14:03:39 +0000195 if ((numnulls == 3) && (nbytes == 1))
196 return buf[0];
197
paul718e3742002-12-13 20:15:29 +0000198 buf[nbytes] = '\0';
ajs85fb1e62004-11-11 14:03:39 +0000199 fputs (buf, fp);
paul718e3742002-12-13 20:15:29 +0000200 fflush (fp);
paul2852de12004-09-17 06:52:16 +0000201
paul0921d482004-10-11 18:21:55 +0000202 /* check for trailling \0\0\0<ret code>,
203 * even if split across reads
204 * (see lib/vty.c::vtysh_read)
205 */
paul2852de12004-09-17 06:52:16 +0000206 if (nbytes >= 4)
207 {
208 i = nbytes-4;
209 numnulls = 0;
210 }
211 else
212 i = 0;
213
paul0921d482004-10-11 18:21:55 +0000214 while (i < nbytes && numnulls < 3)
paul2852de12004-09-17 06:52:16 +0000215 {
216 if (buf[i++] == '\0')
217 numnulls++;
218 else
ajs85fb1e62004-11-11 14:03:39 +0000219 numnulls = 0;
paul2852de12004-09-17 06:52:16 +0000220 }
paul718e3742002-12-13 20:15:29 +0000221
ajs85fb1e62004-11-11 14:03:39 +0000222 /* got 3 or more trailing NULs? */
223 if ((numnulls >= 3) && (i < nbytes))
paul0921d482004-10-11 18:21:55 +0000224 return (buf[nbytes-1]);
paul718e3742002-12-13 20:15:29 +0000225 }
226 }
paul718e3742002-12-13 20:15:29 +0000227}
228
229void
230vtysh_exit_ripd_only ()
231{
232 vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], "exit", stdout);
233}
234
235
236void
237vtysh_pager_init ()
238{
hasso5a9c53d2004-08-27 14:23:28 +0000239 char *pager_defined;
240
241 pager_defined = getenv ("VTYSH_PAGER");
242
243 if (pager_defined)
244 vtysh_pager_name = strdup (pager_defined);
245 else
hasso34553cc2004-08-27 13:56:39 +0000246 vtysh_pager_name = strdup ("more");
paul718e3742002-12-13 20:15:29 +0000247}
248
249/* Command execution over the vty interface. */
250void
hassodda09522004-10-07 21:40:25 +0000251vtysh_execute_func (const char *line, int pager)
paul718e3742002-12-13 20:15:29 +0000252{
253 int ret, cmd_stat;
254 vector vline;
255 struct cmd_element *cmd;
256 FILE *fp = NULL;
paula805cc22003-05-01 14:29:48 +0000257 int closepager=0;
paul718e3742002-12-13 20:15:29 +0000258
hasso95e735b2004-08-26 13:08:30 +0000259 /* Split readline string up into the vector. */
paul718e3742002-12-13 20:15:29 +0000260 vline = cmd_make_strvec (line);
261
262 if (vline == NULL)
263 return;
264
265 ret = cmd_execute_command (vline, vty, &cmd);
266
267 cmd_free_strvec (vline);
268
269 switch (ret)
270 {
271 case CMD_WARNING:
272 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000273 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000274 break;
275 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000276 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000277 break;
278 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000279 fprintf (stdout,"%% Unknown command.\n");
paul718e3742002-12-13 20:15:29 +0000280 break;
281 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000282 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000283 break;
284 case CMD_SUCCESS_DAEMON:
285 {
hasso97b7db22004-10-20 19:07:48 +0000286 /* FIXME: Don't open pager for exit commands. popen() causes problems
287 * if exited from vtysh at all. This hack shouldn't cause any problem
288 * but is really ugly. */
289 if (pager && vtysh_pager_name && (strncmp(line, "exit", 4) != 0))
paul718e3742002-12-13 20:15:29 +0000290 {
paul4fc01e62002-12-13 20:49:00 +0000291 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +0000292 if (fp == NULL)
293 {
paula805cc22003-05-01 14:29:48 +0000294 perror ("popen failed for pager");
295 fp = stdout;
paul718e3742002-12-13 20:15:29 +0000296 }
paula805cc22003-05-01 14:29:48 +0000297 else
298 closepager=1;
paul718e3742002-12-13 20:15:29 +0000299 }
300 else
301 fp = stdout;
302
303 if (! strcmp(cmd->string,"configure terminal"))
304 {
305 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
306 line, fp);
307 if (cmd_stat != CMD_WARNING)
308 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
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_RIPNG],
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_OSPF],
315 line, fp);
316 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000317 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
318 line, fp);
paul718e3742002-12-13 20:15:29 +0000319 if (cmd_stat != CMD_WARNING)
320 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
321 line, fp);
hassob094d262004-08-25 12:22:00 +0000322 if (cmd_stat != CMD_WARNING)
hasso95e735b2004-08-26 13:08:30 +0000323 cmd_stat = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
324 line, fp);
paul718e3742002-12-13 20:15:29 +0000325 if (cmd_stat)
326 {
hassob094d262004-08-25 12:22:00 +0000327 line = "end";
328 vline = cmd_make_strvec (line);
paul718e3742002-12-13 20:15:29 +0000329
hassob094d262004-08-25 12:22:00 +0000330 if (vline == NULL)
paul718e3742002-12-13 20:15:29 +0000331 {
paula805cc22003-05-01 14:29:48 +0000332 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000333 {
334 if (pclose (fp) == -1)
335 {
paula805cc22003-05-01 14:29:48 +0000336 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000337 }
338 fp = NULL;
339 }
340 return;
341 }
342
hassob094d262004-08-25 12:22:00 +0000343 ret = cmd_execute_command (vline, vty, &cmd);
344 cmd_free_strvec (vline);
345 if (ret != CMD_SUCCESS_DAEMON)
346 break;
paul718e3742002-12-13 20:15:29 +0000347 }
348 else
349 if (cmd->func)
350 {
351 (*cmd->func) (cmd, vty, 0, NULL);
352 break;
hassob094d262004-08-25 12:22:00 +0000353 }
paul718e3742002-12-13 20:15:29 +0000354 }
355
356 if (cmd->daemon & VTYSH_ZEBRA)
357 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, fp)
358 != CMD_SUCCESS)
359 break;
360 if (cmd->daemon & VTYSH_RIPD)
361 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, fp)
362 != CMD_SUCCESS)
363 break;
364 if (cmd->daemon & VTYSH_RIPNGD)
365 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, fp)
366 != CMD_SUCCESS)
367 break;
368 if (cmd->daemon & VTYSH_OSPFD)
369 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, fp)
370 != CMD_SUCCESS)
371 break;
372 if (cmd->daemon & VTYSH_OSPF6D)
373 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, fp)
374 != CMD_SUCCESS)
375 break;
376 if (cmd->daemon & VTYSH_BGPD)
377 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, fp)
378 != CMD_SUCCESS)
379 break;
hassob094d262004-08-25 12:22:00 +0000380 if (cmd->daemon & VTYSH_ISISD)
381 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, fp)
382 != CMD_SUCCESS)
383 break;
paul718e3742002-12-13 20:15:29 +0000384 if (cmd->func)
385 (*cmd->func) (cmd, vty, 0, NULL);
386 }
387 }
paula805cc22003-05-01 14:29:48 +0000388 if (pager && vtysh_pager_name && fp && closepager)
paul718e3742002-12-13 20:15:29 +0000389 {
390 if (pclose (fp) == -1)
391 {
paula805cc22003-05-01 14:29:48 +0000392 perror ("pclose failed for pager");
paul718e3742002-12-13 20:15:29 +0000393 }
394 fp = NULL;
395 }
396}
397
398void
hassodda09522004-10-07 21:40:25 +0000399vtysh_execute_no_pager (const char *line)
paul718e3742002-12-13 20:15:29 +0000400{
401 vtysh_execute_func (line, 0);
402}
403
404void
hassodda09522004-10-07 21:40:25 +0000405vtysh_execute (const char *line)
paul718e3742002-12-13 20:15:29 +0000406{
407 vtysh_execute_func (line, 1);
408}
409
410/* Configration make from file. */
411int
412vtysh_config_from_file (struct vty *vty, FILE *fp)
413{
414 int ret;
415 vector vline;
416 struct cmd_element *cmd;
417
418 while (fgets (vty->buf, VTY_BUFSIZ, fp))
419 {
420 if (vty->buf[0] == '!' || vty->buf[1] == '#')
421 continue;
422
423 vline = cmd_make_strvec (vty->buf);
424
hasso95e735b2004-08-26 13:08:30 +0000425 /* In case of comment line. */
paul718e3742002-12-13 20:15:29 +0000426 if (vline == NULL)
427 continue;
428
hasso95e735b2004-08-26 13:08:30 +0000429 /* Execute configuration command : this is strict match. */
paul718e3742002-12-13 20:15:29 +0000430 ret = cmd_execute_command_strict (vline, vty, &cmd);
431
hasso95e735b2004-08-26 13:08:30 +0000432 /* Try again with setting node to CONFIG_NODE. */
paul718e3742002-12-13 20:15:29 +0000433 if (ret != CMD_SUCCESS
434 && ret != CMD_SUCCESS_DAEMON
435 && ret != CMD_WARNING)
436 {
437 if (vty->node == KEYCHAIN_KEY_NODE)
438 {
439 vty->node = KEYCHAIN_NODE;
440 vtysh_exit_ripd_only ();
441 ret = cmd_execute_command_strict (vline, vty, &cmd);
442
443 if (ret != CMD_SUCCESS
444 && ret != CMD_SUCCESS_DAEMON
445 && ret != CMD_WARNING)
446 {
447 vtysh_exit_ripd_only ();
448 vty->node = CONFIG_NODE;
449 ret = cmd_execute_command_strict (vline, vty, &cmd);
450 }
451 }
452 else
453 {
454 vtysh_execute ("end");
455 vtysh_execute ("configure terminal");
456 vty->node = CONFIG_NODE;
457 ret = cmd_execute_command_strict (vline, vty, &cmd);
458 }
459 }
460
461 cmd_free_strvec (vline);
462
463 switch (ret)
464 {
465 case CMD_WARNING:
466 if (vty->type == VTY_FILE)
paul4fc01e62002-12-13 20:49:00 +0000467 fprintf (stdout,"Warning...\n");
paul718e3742002-12-13 20:15:29 +0000468 break;
469 case CMD_ERR_AMBIGUOUS:
paul4fc01e62002-12-13 20:49:00 +0000470 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000471 break;
472 case CMD_ERR_NO_MATCH:
paul4fc01e62002-12-13 20:49:00 +0000473 fprintf (stdout,"%% Unknown command: %s", vty->buf);
paul718e3742002-12-13 20:15:29 +0000474 break;
475 case CMD_ERR_INCOMPLETE:
paul4fc01e62002-12-13 20:49:00 +0000476 fprintf (stdout,"%% Command incomplete.\n");
paul718e3742002-12-13 20:15:29 +0000477 break;
478 case CMD_SUCCESS_DAEMON:
479 {
480 if (cmd->daemon & VTYSH_ZEBRA)
481 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA],
482 vty->buf, stdout) != CMD_SUCCESS)
483 break;
484 if (cmd->daemon & VTYSH_RIPD)
485 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP],
486 vty->buf, stdout) != CMD_SUCCESS)
487 break;
488 if (cmd->daemon & VTYSH_RIPNGD)
489 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG],
490 vty->buf, stdout) != CMD_SUCCESS)
491 break;
492 if (cmd->daemon & VTYSH_OSPFD)
493 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF],
494 vty->buf, stdout) != CMD_SUCCESS)
495 break;
496 if (cmd->daemon & VTYSH_OSPF6D)
497 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6],
498 vty->buf, stdout) != CMD_SUCCESS)
499 break;
500 if (cmd->daemon & VTYSH_BGPD)
501 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP],
502 vty->buf, stdout) != CMD_SUCCESS)
503 break;
hassob094d262004-08-25 12:22:00 +0000504 if (cmd->daemon & VTYSH_ISISD)
505 if (vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS],
506 vty->buf, stdout) != CMD_SUCCESS)
507 break;
paul718e3742002-12-13 20:15:29 +0000508 if (cmd->func)
509 (*cmd->func) (cmd, vty, 0, NULL);
510 }
511 }
512 }
513 return CMD_SUCCESS;
514}
515
516/* We don't care about the point of the cursor when '?' is typed. */
517int
518vtysh_rl_describe ()
519{
520 int ret;
hassodda09522004-10-07 21:40:25 +0000521 unsigned int i;
paul718e3742002-12-13 20:15:29 +0000522 vector vline;
523 vector describe;
524 int width;
525 struct desc *desc;
526
527 vline = cmd_make_strvec (rl_line_buffer);
528
529 /* In case of '> ?'. */
530 if (vline == NULL)
531 {
532 vline = vector_init (1);
533 vector_set (vline, '\0');
534 }
535 else
536 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
537 vector_set (vline, '\0');
538
539 describe = cmd_describe_command (vline, vty, &ret);
540
paul4fc01e62002-12-13 20:49:00 +0000541 fprintf (stdout,"\n");
paul718e3742002-12-13 20:15:29 +0000542
543 /* Ambiguous and no match error. */
544 switch (ret)
545 {
546 case CMD_ERR_AMBIGUOUS:
547 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000548 fprintf (stdout,"%% Ambiguous command.\n");
paul718e3742002-12-13 20:15:29 +0000549 rl_on_new_line ();
550 return 0;
551 break;
552 case CMD_ERR_NO_MATCH:
553 cmd_free_strvec (vline);
paul4fc01e62002-12-13 20:49:00 +0000554 fprintf (stdout,"%% There is no matched command.\n");
paul718e3742002-12-13 20:15:29 +0000555 rl_on_new_line ();
556 return 0;
557 break;
558 }
559
560 /* Get width of command string. */
561 width = 0;
562 for (i = 0; i < vector_max (describe); i++)
563 if ((desc = vector_slot (describe, i)) != NULL)
564 {
565 int len;
566
567 if (desc->cmd[0] == '\0')
568 continue;
569
570 len = strlen (desc->cmd);
571 if (desc->cmd[0] == '.')
572 len--;
573
574 if (width < len)
575 width = len;
576 }
577
578 for (i = 0; i < vector_max (describe); i++)
579 if ((desc = vector_slot (describe, i)) != NULL)
580 {
581 if (desc->cmd[0] == '\0')
582 continue;
583
584 if (! desc->str)
paul4fc01e62002-12-13 20:49:00 +0000585 fprintf (stdout," %-s\n",
hassob094d262004-08-25 12:22:00 +0000586 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd);
paul718e3742002-12-13 20:15:29 +0000587 else
paul4fc01e62002-12-13 20:49:00 +0000588 fprintf (stdout," %-*s %s\n",
hassob094d262004-08-25 12:22:00 +0000589 width,
590 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
591 desc->str);
paul718e3742002-12-13 20:15:29 +0000592 }
593
594 cmd_free_strvec (vline);
595 vector_free (describe);
596
597 rl_on_new_line();
598
599 return 0;
600}
601
hasso95e735b2004-08-26 13:08:30 +0000602/* Result of cmd_complete_command() call will be stored here
603 * and used in new_completion() in order to put the space in
604 * correct places only. */
paul718e3742002-12-13 20:15:29 +0000605int complete_status;
606
607char *
pauldfc0d9b2003-04-18 23:55:29 +0000608command_generator (const char *text, int state)
paul718e3742002-12-13 20:15:29 +0000609{
610 vector vline;
611 static char **matched = NULL;
612 static int index = 0;
613
614 /* First call. */
615 if (! state)
616 {
617 index = 0;
618
619 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
620 return NULL;
621
622 vline = cmd_make_strvec (rl_line_buffer);
623 if (vline == NULL)
624 return NULL;
625
626 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
627 vector_set (vline, '\0');
628
629 matched = cmd_complete_command (vline, vty, &complete_status);
630 }
631
632 if (matched && matched[index])
633 return matched[index++];
634
635 return NULL;
636}
637
638char **
639new_completion (char *text, int start, int end)
640{
641 char **matches;
642
pauldfc0d9b2003-04-18 23:55:29 +0000643 matches = rl_completion_matches (text, command_generator);
paul718e3742002-12-13 20:15:29 +0000644
645 if (matches)
646 {
647 rl_point = rl_end;
648 if (complete_status == CMD_COMPLETE_FULL_MATCH)
649 rl_pending_input = ' ';
650 }
651
652 return matches;
653}
654
655char **
656vtysh_completion (char *text, int start, int end)
657{
658 int ret;
659 vector vline;
660 char **matched = NULL;
661
662 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
663 return NULL;
664
665 vline = cmd_make_strvec (rl_line_buffer);
666 if (vline == NULL)
667 return NULL;
668
669 /* In case of 'help \t'. */
670 if (rl_end && isspace ((int) rl_line_buffer[rl_end - 1]))
671 vector_set (vline, '\0');
672
673 matched = cmd_complete_command (vline, vty, &ret);
674
675 cmd_free_strvec (vline);
676
677 return (char **) matched;
678}
679
hasso95e735b2004-08-26 13:08:30 +0000680/* Vty node structures. */
paul718e3742002-12-13 20:15:29 +0000681struct cmd_node bgp_node =
682{
683 BGP_NODE,
684 "%s(config-router)# ",
685};
686
paul718e3742002-12-13 20:15:29 +0000687struct cmd_node rip_node =
688{
689 RIP_NODE,
690 "%s(config-router)# ",
691};
692
hassoc25e4582003-12-23 10:39:08 +0000693struct cmd_node isis_node =
694{
695 ISIS_NODE,
696 "%s(config-router)# ",
hassoc25e4582003-12-23 10:39:08 +0000697};
698
paul718e3742002-12-13 20:15:29 +0000699struct cmd_node interface_node =
700{
701 INTERFACE_NODE,
702 "%s(config-if)# ",
703};
704
hasso95e735b2004-08-26 13:08:30 +0000705struct cmd_node rmap_node =
706{
707 RMAP_NODE,
708 "%s(config-route-map)# "
709};
710
711struct cmd_node zebra_node =
712{
713 ZEBRA_NODE,
714 "%s(config-router)# "
715};
716
717struct cmd_node bgp_vpnv4_node =
718{
719 BGP_VPNV4_NODE,
720 "%s(config-router-af)# "
721};
722
723struct cmd_node bgp_ipv4_node =
724{
725 BGP_IPV4_NODE,
726 "%s(config-router-af)# "
727};
728
729struct cmd_node bgp_ipv4m_node =
730{
731 BGP_IPV4M_NODE,
732 "%s(config-router-af)# "
733};
734
735struct cmd_node bgp_ipv6_node =
736{
737 BGP_IPV6_NODE,
738 "%s(config-router-af)# "
739};
740
741struct cmd_node ospf_node =
742{
743 OSPF_NODE,
744 "%s(config-router)# "
745};
746
747struct cmd_node ripng_node =
748{
749 RIPNG_NODE,
750 "%s(config-router)# "
751};
752
753struct cmd_node ospf6_node =
754{
755 OSPF6_NODE,
756 "%s(config-ospf6)# "
757};
758
759struct cmd_node keychain_node =
760{
761 KEYCHAIN_NODE,
762 "%s(config-keychain)# "
763};
764
765struct cmd_node keychain_key_node =
766{
767 KEYCHAIN_KEY_NODE,
768 "%s(config-keychain-key)# "
769};
770
hassoe7168df2004-10-03 20:11:32 +0000771/* Defined in lib/vty.c */
772extern struct cmd_node vty_node;
773
hasso95e735b2004-08-26 13:08:30 +0000774/* When '^Z' is received from vty, move down to the enable mode. */
775int
776vtysh_end ()
777{
778 switch (vty->node)
779 {
780 case VIEW_NODE:
781 case ENABLE_NODE:
782 /* Nothing to do. */
783 break;
784 default:
785 vty->node = ENABLE_NODE;
786 break;
787 }
788 return CMD_SUCCESS;
789}
790
791DEFUNSH (VTYSH_ALL,
792 vtysh_end_all,
793 vtysh_end_all_cmd,
794 "end",
hassoe7168df2004-10-03 20:11:32 +0000795 "End current mode and change to enable mode\n")
hasso95e735b2004-08-26 13:08:30 +0000796{
hasso42895462004-09-26 16:25:07 +0000797 return vtysh_end ();
hasso95e735b2004-08-26 13:08:30 +0000798}
799
paul718e3742002-12-13 20:15:29 +0000800DEFUNSH (VTYSH_BGPD,
801 router_bgp,
802 router_bgp_cmd,
803 "router bgp <1-65535>",
804 ROUTER_STR
805 BGP_STR
806 AS_STR)
807{
808 vty->node = BGP_NODE;
809 return CMD_SUCCESS;
810}
811
812DEFUNSH (VTYSH_BGPD,
813 address_family_vpnv4,
814 address_family_vpnv4_cmd,
815 "address-family vpnv4",
816 "Enter Address Family command mode\n"
817 "Address family\n")
818{
819 vty->node = BGP_VPNV4_NODE;
820 return CMD_SUCCESS;
821}
822
823DEFUNSH (VTYSH_BGPD,
824 address_family_vpnv4_unicast,
825 address_family_vpnv4_unicast_cmd,
826 "address-family vpnv4 unicast",
827 "Enter Address Family command mode\n"
828 "Address family\n"
829 "Address Family Modifier\n")
830{
831 vty->node = BGP_VPNV4_NODE;
832 return CMD_SUCCESS;
833}
834
835DEFUNSH (VTYSH_BGPD,
836 address_family_ipv4_unicast,
837 address_family_ipv4_unicast_cmd,
838 "address-family ipv4 unicast",
839 "Enter Address Family command mode\n"
840 "Address family\n"
841 "Address Family Modifier\n")
842{
843 vty->node = BGP_IPV4_NODE;
844 return CMD_SUCCESS;
845}
846
847DEFUNSH (VTYSH_BGPD,
848 address_family_ipv4_multicast,
849 address_family_ipv4_multicast_cmd,
850 "address-family ipv4 multicast",
851 "Enter Address Family command mode\n"
852 "Address family\n"
853 "Address Family Modifier\n")
854{
855 vty->node = BGP_IPV4M_NODE;
856 return CMD_SUCCESS;
857}
858
859DEFUNSH (VTYSH_BGPD,
860 address_family_ipv6,
861 address_family_ipv6_cmd,
862 "address-family ipv6",
863 "Enter Address Family command mode\n"
864 "Address family\n")
865{
866 vty->node = BGP_IPV6_NODE;
867 return CMD_SUCCESS;
868}
869
870DEFUNSH (VTYSH_BGPD,
871 address_family_ipv6_unicast,
872 address_family_ipv6_unicast_cmd,
873 "address-family ipv6 unicast",
874 "Enter Address Family command mode\n"
875 "Address family\n"
876 "Address Family Modifier\n")
877{
878 vty->node = BGP_IPV6_NODE;
879 return CMD_SUCCESS;
880}
881
882DEFUNSH (VTYSH_RIPD,
883 key_chain,
884 key_chain_cmd,
885 "key chain WORD",
886 "Authentication key management\n"
887 "Key-chain management\n"
888 "Key-chain name\n")
889{
890 vty->node = KEYCHAIN_NODE;
891 return CMD_SUCCESS;
892}
893
894DEFUNSH (VTYSH_RIPD,
895 key,
896 key_cmd,
897 "key <0-2147483647>",
898 "Configure a key\n"
899 "Key identifier number\n")
900{
901 vty->node = KEYCHAIN_KEY_NODE;
902 return CMD_SUCCESS;
903}
904
905DEFUNSH (VTYSH_RIPD,
906 router_rip,
907 router_rip_cmd,
908 "router rip",
909 ROUTER_STR
910 "RIP")
911{
912 vty->node = RIP_NODE;
913 return CMD_SUCCESS;
914}
915
916DEFUNSH (VTYSH_RIPNGD,
917 router_ripng,
918 router_ripng_cmd,
919 "router ripng",
920 ROUTER_STR
921 "RIPng")
922{
923 vty->node = RIPNG_NODE;
924 return CMD_SUCCESS;
925}
926
927DEFUNSH (VTYSH_OSPFD,
928 router_ospf,
929 router_ospf_cmd,
930 "router ospf",
931 "Enable a routing process\n"
932 "Start OSPF configuration\n")
933{
934 vty->node = OSPF_NODE;
935 return CMD_SUCCESS;
936}
937
938DEFUNSH (VTYSH_OSPF6D,
939 router_ospf6,
940 router_ospf6_cmd,
941 "router ospf6",
942 OSPF6_ROUTER_STR
943 OSPF6_STR)
944{
945 vty->node = OSPF6_NODE;
946 return CMD_SUCCESS;
947}
948
hassoc25e4582003-12-23 10:39:08 +0000949DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +0000950 router_isis,
951 router_isis_cmd,
952 "router isis WORD",
953 ROUTER_STR
954 "ISO IS-IS\n"
955 "ISO Routing area tag")
hassoc25e4582003-12-23 10:39:08 +0000956{
957 vty->node = ISIS_NODE;
958 return CMD_SUCCESS;
959}
960
paul718e3742002-12-13 20:15:29 +0000961DEFUNSH (VTYSH_RMAP,
962 route_map,
963 route_map_cmd,
964 "route-map WORD (deny|permit) <1-65535>",
965 "Create route-map or enter route-map command mode\n"
966 "Route map tag\n"
967 "Route map denies set operations\n"
968 "Route map permits set operations\n"
969 "Sequence to insert to/delete from existing route-map entry\n")
970{
971 vty->node = RMAP_NODE;
972 return CMD_SUCCESS;
973}
974
paul718e3742002-12-13 20:15:29 +0000975DEFUNSH (VTYSH_ALL,
hassoe7168df2004-10-03 20:11:32 +0000976 vtysh_line_vty,
977 vtysh_line_vty_cmd,
978 "line vty",
979 "Configure a terminal line\n"
980 "Virtual terminal\n")
981{
982 vty->node = VTY_NODE;
983 return CMD_SUCCESS;
984}
985
986DEFUNSH (VTYSH_ALL,
paul718e3742002-12-13 20:15:29 +0000987 vtysh_enable,
988 vtysh_enable_cmd,
989 "enable",
990 "Turn on privileged mode command\n")
991{
992 vty->node = ENABLE_NODE;
993 return CMD_SUCCESS;
994}
995
paul718e3742002-12-13 20:15:29 +0000996DEFUNSH (VTYSH_ALL,
997 vtysh_disable,
998 vtysh_disable_cmd,
999 "disable",
1000 "Turn off privileged mode command\n")
1001{
1002 if (vty->node == ENABLE_NODE)
1003 vty->node = VIEW_NODE;
1004 return CMD_SUCCESS;
1005}
1006
paul718e3742002-12-13 20:15:29 +00001007DEFUNSH (VTYSH_ALL,
1008 vtysh_config_terminal,
1009 vtysh_config_terminal_cmd,
1010 "configure terminal",
1011 "Configuration from vty interface\n"
1012 "Configuration terminal\n")
1013{
1014 vty->node = CONFIG_NODE;
1015 return CMD_SUCCESS;
1016}
1017
1018int
1019vtysh_exit (struct vty *vty)
1020{
1021 switch (vty->node)
1022 {
1023 case VIEW_NODE:
1024 case ENABLE_NODE:
1025 exit (0);
1026 break;
1027 case CONFIG_NODE:
1028 vty->node = ENABLE_NODE;
1029 break;
1030 case INTERFACE_NODE:
1031 case ZEBRA_NODE:
1032 case BGP_NODE:
1033 case RIP_NODE:
1034 case RIPNG_NODE:
1035 case OSPF_NODE:
1036 case OSPF6_NODE:
hassoc25e4582003-12-23 10:39:08 +00001037 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001038 case MASC_NODE:
1039 case RMAP_NODE:
1040 case VTY_NODE:
1041 case KEYCHAIN_NODE:
hasso2a56df92004-05-09 23:16:40 +00001042 vtysh_execute("end");
1043 vtysh_execute("configure terminal");
paul718e3742002-12-13 20:15:29 +00001044 vty->node = CONFIG_NODE;
1045 break;
1046 case BGP_VPNV4_NODE:
1047 case BGP_IPV4_NODE:
1048 case BGP_IPV4M_NODE:
1049 case BGP_IPV6_NODE:
1050 vty->node = BGP_NODE;
1051 break;
1052 case KEYCHAIN_KEY_NODE:
1053 vty->node = KEYCHAIN_NODE;
1054 break;
1055 default:
1056 break;
1057 }
1058 return CMD_SUCCESS;
1059}
1060
1061DEFUNSH (VTYSH_ALL,
1062 vtysh_exit_all,
1063 vtysh_exit_all_cmd,
1064 "exit",
1065 "Exit current mode and down to previous mode\n")
1066{
1067 return vtysh_exit (vty);
1068}
1069
1070ALIAS (vtysh_exit_all,
1071 vtysh_quit_all_cmd,
1072 "quit",
1073 "Exit current mode and down to previous mode\n")
1074
1075DEFUNSH (VTYSH_BGPD,
1076 exit_address_family,
1077 exit_address_family_cmd,
1078 "exit-address-family",
1079 "Exit from Address Family configuration mode\n")
1080{
1081 if (vty->node == BGP_IPV4_NODE
1082 || vty->node == BGP_IPV4M_NODE
1083 || vty->node == BGP_VPNV4_NODE
1084 || vty->node == BGP_IPV6_NODE)
1085 vty->node = BGP_NODE;
1086 return CMD_SUCCESS;
1087}
1088
1089DEFUNSH (VTYSH_ZEBRA,
1090 vtysh_exit_zebra,
1091 vtysh_exit_zebra_cmd,
1092 "exit",
1093 "Exit current mode and down to previous mode\n")
1094{
1095 return vtysh_exit (vty);
1096}
1097
1098ALIAS (vtysh_exit_zebra,
1099 vtysh_quit_zebra_cmd,
1100 "quit",
1101 "Exit current mode and down to previous mode\n")
1102
1103DEFUNSH (VTYSH_RIPD,
1104 vtysh_exit_ripd,
1105 vtysh_exit_ripd_cmd,
1106 "exit",
1107 "Exit current mode and down to previous mode\n")
1108{
1109 return vtysh_exit (vty);
1110}
1111
1112ALIAS (vtysh_exit_ripd,
1113 vtysh_quit_ripd_cmd,
1114 "quit",
1115 "Exit current mode and down to previous mode\n")
1116
paul68980082003-03-25 05:07:42 +00001117DEFUNSH (VTYSH_RIPNGD,
hassob094d262004-08-25 12:22:00 +00001118 vtysh_exit_ripngd,
1119 vtysh_exit_ripngd_cmd,
1120 "exit",
1121 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001122{
1123 return vtysh_exit (vty);
1124}
1125
1126ALIAS (vtysh_exit_ripngd,
1127 vtysh_quit_ripngd_cmd,
1128 "quit",
1129 "Exit current mode and down to previous mode\n")
1130
paul718e3742002-12-13 20:15:29 +00001131DEFUNSH (VTYSH_RMAP,
1132 vtysh_exit_rmap,
1133 vtysh_exit_rmap_cmd,
1134 "exit",
1135 "Exit current mode and down to previous mode\n")
1136{
1137 return vtysh_exit (vty);
1138}
1139
1140ALIAS (vtysh_exit_rmap,
1141 vtysh_quit_rmap_cmd,
1142 "quit",
1143 "Exit current mode and down to previous mode\n")
1144
1145DEFUNSH (VTYSH_BGPD,
1146 vtysh_exit_bgpd,
1147 vtysh_exit_bgpd_cmd,
1148 "exit",
1149 "Exit current mode and down to previous mode\n")
1150{
1151 return vtysh_exit (vty);
1152}
1153
1154ALIAS (vtysh_exit_bgpd,
1155 vtysh_quit_bgpd_cmd,
1156 "quit",
1157 "Exit current mode and down to previous mode\n")
1158
1159DEFUNSH (VTYSH_OSPFD,
1160 vtysh_exit_ospfd,
1161 vtysh_exit_ospfd_cmd,
1162 "exit",
1163 "Exit current mode and down to previous mode\n")
1164{
1165 return vtysh_exit (vty);
1166}
1167
1168ALIAS (vtysh_exit_ospfd,
1169 vtysh_quit_ospfd_cmd,
1170 "quit",
1171 "Exit current mode and down to previous mode\n")
1172
paul68980082003-03-25 05:07:42 +00001173DEFUNSH (VTYSH_OSPF6D,
hassob094d262004-08-25 12:22:00 +00001174 vtysh_exit_ospf6d,
1175 vtysh_exit_ospf6d_cmd,
1176 "exit",
1177 "Exit current mode and down to previous mode\n")
paul68980082003-03-25 05:07:42 +00001178{
1179 return vtysh_exit (vty);
1180}
1181
1182ALIAS (vtysh_exit_ospf6d,
1183 vtysh_quit_ospf6d_cmd,
1184 "quit",
1185 "Exit current mode and down to previous mode\n")
1186
hassoc25e4582003-12-23 10:39:08 +00001187DEFUNSH (VTYSH_ISISD,
hassob094d262004-08-25 12:22:00 +00001188 vtysh_exit_isisd,
1189 vtysh_exit_isisd_cmd,
1190 "exit",
1191 "Exit current mode and down to previous mode\n")
hassoc25e4582003-12-23 10:39:08 +00001192{
1193 return vtysh_exit (vty);
1194}
1195
1196ALIAS (vtysh_exit_isisd,
1197 vtysh_quit_isisd_cmd,
1198 "quit",
1199 "Exit current mode and down to previous mode\n")
1200
hassoe7168df2004-10-03 20:11:32 +00001201DEFUNSH (VTYSH_ALL,
1202 vtysh_exit_line_vty,
1203 vtysh_exit_line_vty_cmd,
1204 "exit",
1205 "Exit current mode and down to previous mode\n")
1206{
1207 return vtysh_exit (vty);
1208}
1209
1210ALIAS (vtysh_exit_line_vty,
1211 vtysh_quit_line_vty_cmd,
1212 "quit",
1213 "Exit current mode and down to previous mode\n")
1214
hasso95e735b2004-08-26 13:08:30 +00001215DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001216 vtysh_interface,
1217 vtysh_interface_cmd,
1218 "interface IFNAME",
1219 "Select an interface to configure\n"
1220 "Interface's name\n")
1221{
1222 vty->node = INTERFACE_NODE;
1223 return CMD_SUCCESS;
1224}
1225
hasso95e735b2004-08-26 13:08:30 +00001226/* TODO Implement "no interface command in isisd. */
paul32d24632003-05-23 09:25:20 +00001227DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_RIPNGD|VTYSH_OSPFD|VTYSH_OSPF6D,
1228 vtysh_no_interface_cmd,
1229 "no interface IFNAME",
1230 NO_STR
1231 "Delete a pseudo interface's configuration\n"
1232 "Interface's name\n")
1233
hasso95e735b2004-08-26 13:08:30 +00001234/* TODO Implement interface description commands in ripngd, ospf6d
1235 * and isisd. */
paul338a9912003-03-01 15:44:10 +00001236DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1237 interface_desc_cmd,
1238 "description .LINE",
1239 "Interface specific description\n"
1240 "Characters describing this interface\n")
paul464dc8d2003-03-28 02:25:45 +00001241
1242DEFSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD,
1243 no_interface_desc_cmd,
1244 "no description",
1245 NO_STR
1246 "Interface specific description\n")
paul338a9912003-03-01 15:44:10 +00001247
hasso95e735b2004-08-26 13:08:30 +00001248DEFUNSH (VTYSH_INTERFACE,
paul718e3742002-12-13 20:15:29 +00001249 vtysh_exit_interface,
1250 vtysh_exit_interface_cmd,
1251 "exit",
1252 "Exit current mode and down to previous mode\n")
1253{
1254 return vtysh_exit (vty);
1255}
1256
1257ALIAS (vtysh_exit_interface,
1258 vtysh_quit_interface_cmd,
1259 "quit",
1260 "Exit current mode and down to previous mode\n")
1261
hasso95e735b2004-08-26 13:08:30 +00001262/* Logging commands. */
1263DEFUNSH (VTYSH_ALL,
1264 vtysh_log_stdout,
1265 vtysh_log_stdout_cmd,
1266 "log stdout",
1267 "Logging control\n"
1268 "Logging goes to stdout\n")
1269{
1270 return CMD_SUCCESS;
1271}
1272
1273DEFUNSH (VTYSH_ALL,
1274 no_vtysh_log_stdout,
1275 no_vtysh_log_stdout_cmd,
1276 "no log stdout",
1277 NO_STR
1278 "Logging control\n"
1279 "Logging goes to stdout\n")
1280{
1281 return CMD_SUCCESS;
1282}
1283
1284DEFUNSH (VTYSH_ALL,
1285 vtysh_log_file,
1286 vtysh_log_file_cmd,
1287 "log file FILENAME",
1288 "Logging control\n"
1289 "Logging to file\n"
1290 "Logging filename\n")
1291{
1292 return CMD_SUCCESS;
1293}
1294
1295DEFUNSH (VTYSH_ALL,
1296 no_vtysh_log_file,
1297 no_vtysh_log_file_cmd,
1298 "no log file [FILENAME]",
1299 NO_STR
1300 "Logging control\n"
1301 "Cancel logging to file\n"
1302 "Logging file name\n")
1303{
1304 return CMD_SUCCESS;
1305}
1306
1307DEFUNSH (VTYSH_ALL,
1308 vtysh_log_syslog,
1309 vtysh_log_syslog_cmd,
1310 "log syslog",
1311 "Logging control\n"
1312 "Logging goes to syslog\n")
1313{
1314 return CMD_SUCCESS;
1315}
1316
1317DEFUNSH (VTYSH_ALL,
1318 no_vtysh_log_syslog,
1319 no_vtysh_log_syslog_cmd,
1320 "no log syslog",
1321 NO_STR
1322 "Logging control\n"
1323 "Cancel logging to syslog\n")
1324{
1325 return CMD_SUCCESS;
1326}
1327
1328DEFUNSH (VTYSH_ALL,
1329 vtysh_log_trap,
1330 vtysh_log_trap_cmd,
1331 "log trap (emergencies|alerts|critical|errors|warnings|\
1332 notifications|informational|debugging)",
1333 "Logging control\n"
1334 "Limit logging to specifed level\n")
1335{
1336 return CMD_SUCCESS;
1337}
1338
1339DEFUNSH (VTYSH_ALL,
1340 no_vtysh_log_trap,
1341 no_vtysh_log_trap_cmd,
1342 "no log trap",
1343 NO_STR
1344 "Logging control\n"
1345 "Permit all logging information\n")
1346{
1347 return CMD_SUCCESS;
1348}
1349
1350DEFUNSH (VTYSH_ALL,
1351 vtysh_log_record_priority,
1352 vtysh_log_record_priority_cmd,
1353 "log record-priority",
1354 "Logging control\n"
1355 "Log the priority of the message within the message\n")
1356{
1357 return CMD_SUCCESS;
1358}
1359
1360DEFUNSH (VTYSH_ALL,
1361 no_vtysh_log_record_priority,
1362 no_vtysh_log_record_priority_cmd,
1363 "no log record-priority",
1364 NO_STR
1365 "Logging control\n"
1366 "Do not log the priority of the message within the message\n")
1367{
1368 return CMD_SUCCESS;
1369}
1370
hassoe7168df2004-10-03 20:11:32 +00001371DEFUNSH (VTYSH_ALL,
1372 vtysh_service_password_encrypt,
1373 vtysh_service_password_encrypt_cmd,
1374 "service password-encryption",
1375 "Set up miscellaneous service\n"
1376 "Enable encrypted passwords\n")
1377{
1378 return CMD_SUCCESS;
1379}
1380
1381DEFUNSH (VTYSH_ALL,
1382 no_vtysh_service_password_encrypt,
1383 no_vtysh_service_password_encrypt_cmd,
1384 "no service password-encryption",
1385 NO_STR
1386 "Set up miscellaneous service\n"
1387 "Enable encrypted passwords\n")
1388{
1389 return CMD_SUCCESS;
1390}
1391
1392DEFUNSH (VTYSH_ALL,
1393 vtysh_config_password,
1394 vtysh_password_cmd,
1395 "password (8|) WORD",
1396 "Assign the terminal connection password\n"
1397 "Specifies a HIDDEN password will follow\n"
1398 "dummy string \n"
1399 "The HIDDEN line password string\n")
1400{
1401 return CMD_SUCCESS;
1402}
1403
1404DEFUNSH (VTYSH_ALL,
1405 vtysh_password_text,
1406 vtysh_password_text_cmd,
1407 "password LINE",
1408 "Assign the terminal connection password\n"
1409 "The UNENCRYPTED (cleartext) line password\n")
1410{
1411 return CMD_SUCCESS;
1412}
1413
1414DEFUNSH (VTYSH_ALL,
1415 vtysh_config_enable_password,
1416 vtysh_enable_password_cmd,
1417 "enable password (8|) WORD",
1418 "Modify enable password parameters\n"
1419 "Assign the privileged level password\n"
1420 "Specifies a HIDDEN password will follow\n"
1421 "dummy string \n"
1422 "The HIDDEN 'enable' password string\n")
1423{
1424 return CMD_SUCCESS;
1425}
1426
1427DEFUNSH (VTYSH_ALL,
1428 vtysh_enable_password_text,
1429 vtysh_enable_password_text_cmd,
1430 "enable password LINE",
1431 "Modify enable password parameters\n"
1432 "Assign the privileged level password\n"
1433 "The UNENCRYPTED (cleartext) 'enable' password\n")
1434{
1435 return CMD_SUCCESS;
1436}
1437
1438DEFUNSH (VTYSH_ALL,
1439 no_vtysh_config_enable_password,
1440 no_vtysh_enable_password_cmd,
1441 "no enable password",
1442 NO_STR
1443 "Modify enable password parameters\n"
1444 "Assign the privileged level password\n")
1445{
1446 return CMD_SUCCESS;
1447}
1448
paul718e3742002-12-13 20:15:29 +00001449DEFUN (vtysh_write_terminal,
1450 vtysh_write_terminal_cmd,
1451 "write terminal",
1452 "Write running configuration to memory, network, or terminal\n"
1453 "Write to terminal\n")
1454{
1455 int ret;
1456 char line[] = "write terminal\n";
1457 FILE *fp = NULL;
1458
1459 if (vtysh_pager_name)
1460 {
paul4fc01e62002-12-13 20:49:00 +00001461 fp = popen (vtysh_pager_name, "w");
paul718e3742002-12-13 20:15:29 +00001462 if (fp == NULL)
1463 {
1464 perror ("popen");
1465 exit (1);
1466 }
1467 }
1468 else
1469 fp = stdout;
1470
1471 vty_out (vty, "Building configuration...%s", VTY_NEWLINE);
1472 vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE,
1473 VTY_NEWLINE);
hassoe7168df2004-10-03 20:11:32 +00001474 vty_out (vty, "!%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001475
1476 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1477 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1478 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1479 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1480 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1481 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001482 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001483
hassoe7168df2004-10-03 20:11:32 +00001484 /* Integrate vtysh specific configuration. */
1485 vtysh_config_write ();
1486
paul718e3742002-12-13 20:15:29 +00001487 vtysh_config_dump (fp);
1488
1489 if (vtysh_pager_name && fp)
1490 {
1491 fflush (fp);
1492 if (pclose (fp) == -1)
1493 {
1494 perror ("pclose");
1495 exit (1);
1496 }
1497 fp = NULL;
1498 }
1499
1500 return CMD_SUCCESS;
1501}
1502
hassoe7168df2004-10-03 20:11:32 +00001503DEFUN (vtysh_integrated_config,
1504 vtysh_integrated_config_cmd,
1505 "service integrated-vtysh-config",
1506 "Set up miscellaneous service\n"
1507 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001508{
hassoe7168df2004-10-03 20:11:32 +00001509 vtysh_writeconfig_integrated = 1;
1510 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001511}
1512
hassoe7168df2004-10-03 20:11:32 +00001513DEFUN (no_vtysh_integrated_config,
1514 no_vtysh_integrated_config_cmd,
1515 "no service integrated-vtysh-config",
1516 NO_STR
1517 "Set up miscellaneous service\n"
1518 "Write configuration into integrated file\n")
paul4fc01e62002-12-13 20:49:00 +00001519{
hassoe7168df2004-10-03 20:11:32 +00001520 vtysh_writeconfig_integrated = 0;
1521 return CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001522}
1523
1524int write_config_integrated(void)
paul718e3742002-12-13 20:15:29 +00001525{
1526 int ret;
paul718e3742002-12-13 20:15:29 +00001527 char line[] = "write terminal\n";
1528 FILE *fp;
1529 char *integrate_sav = NULL;
1530
hasso95e735b2004-08-26 13:08:30 +00001531 integrate_sav = malloc (strlen (integrate_default) +
1532 strlen (CONF_BACKUP_EXT) + 1);
paul718e3742002-12-13 20:15:29 +00001533 strcpy (integrate_sav, integrate_default);
1534 strcat (integrate_sav, CONF_BACKUP_EXT);
1535
paul4fc01e62002-12-13 20:49:00 +00001536 fprintf (stdout,"Building Configuration...\n");
paul718e3742002-12-13 20:15:29 +00001537
hasso95e735b2004-08-26 13:08:30 +00001538 /* Move current configuration file to backup config file. */
paul718e3742002-12-13 20:15:29 +00001539 unlink (integrate_sav);
1540 rename (integrate_default, integrate_sav);
hasso95e735b2004-08-26 13:08:30 +00001541 free (integrate_sav);
paul4fc01e62002-12-13 20:49:00 +00001542
paul718e3742002-12-13 20:15:29 +00001543 fp = fopen (integrate_default, "w");
1544 if (fp == NULL)
1545 {
hasso95e735b2004-08-26 13:08:30 +00001546 fprintf (stdout,"%% Can't open configuration file %s.\n",
1547 integrate_default);
paul718e3742002-12-13 20:15:29 +00001548 return CMD_SUCCESS;
1549 }
paul718e3742002-12-13 20:15:29 +00001550
paul718e3742002-12-13 20:15:29 +00001551 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);
1552 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);
1553 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);
1554 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);
1555 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);
1556 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);
hassoc25e4582003-12-23 10:39:08 +00001557 ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ISIS], line);
paul718e3742002-12-13 20:15:29 +00001558
1559 vtysh_config_dump (fp);
1560
1561 fclose (fp);
1562
gdtaa593d52003-12-22 20:15:53 +00001563 if (chmod (integrate_default, CONFIGFILE_MASK) != 0)
1564 {
1565 fprintf (stdout,"%% Can't chmod configuration file %s: %s (%d)\n",
ajs6099b3b2004-11-20 02:06:59 +00001566 integrate_default, safe_strerror(errno), errno);
gdtaa593d52003-12-22 20:15:53 +00001567 return CMD_WARNING;
1568 }
1569
paul4fc01e62002-12-13 20:49:00 +00001570 fprintf(stdout,"Integrated configuration saved to %s\n",integrate_default);
1571
1572 fprintf (stdout,"[OK]\n");
1573
paul718e3742002-12-13 20:15:29 +00001574 return CMD_SUCCESS;
1575}
1576
paul4fc01e62002-12-13 20:49:00 +00001577DEFUN (vtysh_write_memory,
1578 vtysh_write_memory_cmd,
1579 "write memory",
1580 "Write running configuration to memory, network, or terminal\n"
1581 "Write configuration to the file (same as write file)\n")
1582{
pauldfc0d9b2003-04-18 23:55:29 +00001583 int ret = CMD_SUCCESS;
paul4fc01e62002-12-13 20:49:00 +00001584 char line[] = "write memory\n";
1585
hassoe7168df2004-10-03 20:11:32 +00001586 /* If integrated Quagga.conf explicitely set. */
1587 if (vtysh_writeconfig_integrated)
1588 return write_config_integrated();
paul4fc01e62002-12-13 20:49:00 +00001589
1590 fprintf (stdout,"Building Configuration...\n");
1591
1592 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ZEBRA], line, stdout);
1593 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIP], line, stdout);
1594 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_RIPNG], line, stdout);
1595 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF], line, stdout);
1596 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_OSPF6], line, stdout);
1597 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_BGP], line, stdout);
hassoc25e4582003-12-23 10:39:08 +00001598 ret = vtysh_client_execute (&vtysh_client[VTYSH_INDEX_ISIS], line, stdout);
hassoe7168df2004-10-03 20:11:32 +00001599
paul4fc01e62002-12-13 20:49:00 +00001600 fprintf (stdout,"[OK]\n");
1601
pauldfc0d9b2003-04-18 23:55:29 +00001602 return ret;
paul4fc01e62002-12-13 20:49:00 +00001603}
1604
paul718e3742002-12-13 20:15:29 +00001605ALIAS (vtysh_write_memory,
1606 vtysh_copy_runningconfig_startupconfig_cmd,
1607 "copy running-config startup-config",
1608 "Copy from one file to another\n"
1609 "Copy from current system configuration\n"
1610 "Copy to startup configuration\n")
1611
1612ALIAS (vtysh_write_memory,
1613 vtysh_write_file_cmd,
1614 "write file",
1615 "Write running configuration to memory, network, or terminal\n"
1616 "Write configuration to the file (same as write memory)\n")
1617
hasso4a6e2252003-05-25 11:51:29 +00001618ALIAS (vtysh_write_memory,
1619 vtysh_write_cmd,
1620 "write",
1621 "Write running configuration to memory, network, or terminal\n")
1622
paul718e3742002-12-13 20:15:29 +00001623ALIAS (vtysh_write_terminal,
1624 vtysh_show_running_config_cmd,
1625 "show running-config",
1626 SHOW_STR
1627 "Current operating configuration\n")
hassob094d262004-08-25 12:22:00 +00001628
hasso34553cc2004-08-27 13:56:39 +00001629DEFUN (vtysh_terminal_length,
1630 vtysh_terminal_length_cmd,
1631 "terminal length <0-512>",
1632 "Set terminal line parameters\n"
1633 "Set number of lines on a screen\n"
1634 "Number of lines on screen (0 for no pausing)\n")
1635{
1636 int lines;
1637 char *endptr = NULL;
1638 char default_pager[10];
1639
1640 lines = strtol (argv[0], &endptr, 10);
1641 if (lines < 0 || lines > 512 || *endptr != '\0')
1642 {
1643 vty_out (vty, "length is malformed%s", VTY_NEWLINE);
1644 return CMD_WARNING;
1645 }
1646
1647 if (vtysh_pager_name)
1648 {
1649 free (vtysh_pager_name);
1650 vtysh_pager_name = NULL;
1651 }
1652
1653 if (lines != 0)
1654 {
1655 snprintf(default_pager, 10, "more -%i", lines);
1656 vtysh_pager_name = strdup (default_pager);
1657 }
1658
1659 return CMD_SUCCESS;
1660}
1661
1662DEFUN (vtysh_terminal_no_length,
1663 vtysh_terminal_no_length_cmd,
1664 "terminal no length",
1665 "Set terminal line parameters\n"
1666 NO_STR
1667 "Set number of lines on a screen\n")
1668{
1669 if (vtysh_pager_name)
1670 {
1671 free (vtysh_pager_name);
1672 vtysh_pager_name = NULL;
1673 }
1674
1675 vtysh_pager_init();
1676 return CMD_SUCCESS;
1677}
1678
hassof2799e62004-10-28 17:43:11 +00001679DEFUN (vtysh_show_daemons,
1680 vtysh_show_daemons_cmd,
1681 "show daemons",
hassoe7168df2004-10-03 20:11:32 +00001682 SHOW_STR
1683 "Show list of running daemons\n")
1684{
1685 if ( vtysh_client[VTYSH_INDEX_ZEBRA].fd > 0 )
1686 vty_out(vty, " zebra");
1687 if ( vtysh_client[VTYSH_INDEX_RIP].fd > 0 )
1688 vty_out(vty, " ripd");
1689 if ( vtysh_client[VTYSH_INDEX_RIPNG].fd > 0 )
1690 vty_out(vty, " ripngd");
1691 if ( vtysh_client[VTYSH_INDEX_OSPF].fd > 0 )
1692 vty_out(vty, " ospfd");
1693 if ( vtysh_client[VTYSH_INDEX_OSPF6].fd > 0 )
1694 vty_out(vty, " ospf6d");
1695 if ( vtysh_client[VTYSH_INDEX_BGP].fd > 0 )
1696 vty_out(vty, " bgpd");
1697 if ( vtysh_client[VTYSH_INDEX_ISIS].fd > 0 )
1698 vty_out(vty, " isisd");
1699 vty_out(vty, "%s", VTY_NEWLINE);
1700
1701 return CMD_SUCCESS;
1702}
1703
paul718e3742002-12-13 20:15:29 +00001704/* Execute command in child process. */
1705int
hasso5862ff52004-10-11 13:20:40 +00001706execute_command (const char *command, int argc, const char *arg1,
1707 const char *arg2)
paul718e3742002-12-13 20:15:29 +00001708{
1709 int ret;
1710 pid_t pid;
1711 int status;
1712
1713 /* Call fork(). */
1714 pid = fork ();
1715
1716 if (pid < 0)
1717 {
1718 /* Failure of fork(). */
ajs6099b3b2004-11-20 02:06:59 +00001719 fprintf (stderr, "Can't fork: %s\n", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001720 exit (1);
1721 }
1722 else if (pid == 0)
1723 {
1724 /* This is child process. */
1725 switch (argc)
1726 {
1727 case 0:
hassofa2b17e2004-03-04 17:45:00 +00001728 ret = execlp (command, command, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001729 break;
1730 case 1:
hassofa2b17e2004-03-04 17:45:00 +00001731 ret = execlp (command, command, arg1, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001732 break;
1733 case 2:
hassofa2b17e2004-03-04 17:45:00 +00001734 ret = execlp (command, command, arg1, arg2, (const char *)NULL);
paul718e3742002-12-13 20:15:29 +00001735 break;
1736 }
1737
1738 /* When execlp suceed, this part is not executed. */
ajs6099b3b2004-11-20 02:06:59 +00001739 fprintf (stderr, "Can't execute %s: %s\n", command, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001740 exit (1);
1741 }
1742 else
1743 {
1744 /* This is parent. */
1745 execute_flag = 1;
1746 ret = wait4 (pid, &status, 0, NULL);
1747 execute_flag = 0;
1748 }
1749 return 0;
1750}
1751
1752DEFUN (vtysh_ping,
1753 vtysh_ping_cmd,
1754 "ping WORD",
hasso4eeccf12003-06-25 10:49:55 +00001755 "Send echo messages\n"
paul718e3742002-12-13 20:15:29 +00001756 "Ping destination address or hostname\n")
1757{
1758 execute_command ("ping", 1, argv[0], NULL);
1759 return CMD_SUCCESS;
1760}
1761
hasso4eeccf12003-06-25 10:49:55 +00001762ALIAS (vtysh_ping,
1763 vtysh_ping_ip_cmd,
1764 "ping ip WORD",
1765 "Send echo messages\n"
1766 "IP echo\n"
1767 "Ping destination address or hostname\n")
1768
paul718e3742002-12-13 20:15:29 +00001769DEFUN (vtysh_traceroute,
1770 vtysh_traceroute_cmd,
1771 "traceroute WORD",
1772 "Trace route to destination\n"
1773 "Trace route to destination address or hostname\n")
1774{
1775 execute_command ("traceroute", 1, argv[0], NULL);
1776 return CMD_SUCCESS;
1777}
1778
hasso4eeccf12003-06-25 10:49:55 +00001779ALIAS (vtysh_traceroute,
1780 vtysh_traceroute_ip_cmd,
1781 "traceroute ip WORD",
1782 "Trace route to destination\n"
1783 "IP trace\n"
1784 "Trace route to destination address or hostname\n")
1785
1786#ifdef HAVE_IPV6
1787DEFUN (vtysh_ping6,
1788 vtysh_ping6_cmd,
1789 "ping ipv6 WORD",
1790 "Send echo messages\n"
1791 "IPv6 echo\n"
1792 "Ping destination address or hostname\n")
1793{
1794 execute_command ("ping6", 1, argv[0], NULL);
1795 return CMD_SUCCESS;
1796}
1797
1798DEFUN (vtysh_traceroute6,
1799 vtysh_traceroute6_cmd,
1800 "traceroute ipv6 WORD",
1801 "Trace route to destination\n"
1802 "IPv6 trace\n"
1803 "Trace route to destination address or hostname\n")
1804{
1805 execute_command ("traceroute6", 1, argv[0], NULL);
1806 return CMD_SUCCESS;
1807}
1808#endif
1809
paul718e3742002-12-13 20:15:29 +00001810DEFUN (vtysh_telnet,
1811 vtysh_telnet_cmd,
1812 "telnet WORD",
1813 "Open a telnet connection\n"
1814 "IP address or hostname of a remote system\n")
1815{
1816 execute_command ("telnet", 1, argv[0], NULL);
1817 return CMD_SUCCESS;
1818}
1819
1820DEFUN (vtysh_telnet_port,
1821 vtysh_telnet_port_cmd,
1822 "telnet WORD PORT",
1823 "Open a telnet connection\n"
1824 "IP address or hostname of a remote system\n"
1825 "TCP Port number\n")
1826{
1827 execute_command ("telnet", 2, argv[0], argv[1]);
1828 return CMD_SUCCESS;
1829}
1830
paul5087df52003-01-25 06:56:09 +00001831DEFUN (vtysh_ssh,
1832 vtysh_ssh_cmd,
1833 "ssh WORD",
1834 "Open an ssh connection\n"
1835 "[user@]host\n")
1836{
1837 execute_command ("ssh", 1, argv[0], NULL);
1838 return CMD_SUCCESS;
1839}
1840
paul718e3742002-12-13 20:15:29 +00001841DEFUN (vtysh_start_shell,
1842 vtysh_start_shell_cmd,
1843 "start-shell",
1844 "Start UNIX shell\n")
1845{
1846 execute_command ("sh", 0, NULL, NULL);
1847 return CMD_SUCCESS;
1848}
1849
1850DEFUN (vtysh_start_bash,
1851 vtysh_start_bash_cmd,
1852 "start-shell bash",
1853 "Start UNIX shell\n"
1854 "Start bash\n")
1855{
1856 execute_command ("bash", 0, NULL, NULL);
1857 return CMD_SUCCESS;
1858}
1859
1860DEFUN (vtysh_start_zsh,
1861 vtysh_start_zsh_cmd,
1862 "start-shell zsh",
1863 "Start UNIX shell\n"
1864 "Start Z shell\n")
1865{
1866 execute_command ("zsh", 0, NULL, NULL);
1867 return CMD_SUCCESS;
1868}
hassob094d262004-08-25 12:22:00 +00001869
paul718e3742002-12-13 20:15:29 +00001870void
1871vtysh_install_default (enum node_type node)
1872{
1873 install_element (node, &config_list_cmd);
1874}
1875
1876/* Making connection to protocol daemon. */
1877int
hassodda09522004-10-07 21:40:25 +00001878vtysh_connect (struct vtysh_client *vclient, const char *path)
paul718e3742002-12-13 20:15:29 +00001879{
1880 int ret;
1881 int sock, len;
1882 struct sockaddr_un addr;
1883 struct stat s_stat;
1884 uid_t euid;
1885 gid_t egid;
1886
1887 memset (vclient, 0, sizeof (struct vtysh_client));
1888 vclient->fd = -1;
1889
1890 /* Stat socket to see if we have permission to access it. */
1891 euid = geteuid();
1892 egid = getegid();
1893 ret = stat (path, &s_stat);
1894 if (ret < 0 && errno != ENOENT)
1895 {
1896 fprintf (stderr, "vtysh_connect(%s): stat = %s\n",
ajs6099b3b2004-11-20 02:06:59 +00001897 path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001898 exit(1);
1899 }
1900
1901 if (ret >= 0)
1902 {
1903 if (! S_ISSOCK(s_stat.st_mode))
1904 {
1905 fprintf (stderr, "vtysh_connect(%s): Not a socket\n",
1906 path);
1907 exit (1);
1908 }
1909
paul718e3742002-12-13 20:15:29 +00001910 }
1911
1912 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1913 if (sock < 0)
1914 {
1915#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001916 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
ajs6099b3b2004-11-20 02:06:59 +00001917 safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001918#endif /* DEBUG */
1919 return -1;
1920 }
1921
1922 memset (&addr, 0, sizeof (struct sockaddr_un));
1923 addr.sun_family = AF_UNIX;
1924 strncpy (addr.sun_path, path, strlen (path));
1925#ifdef HAVE_SUN_LEN
1926 len = addr.sun_len = SUN_LEN(&addr);
1927#else
1928 len = sizeof (addr.sun_family) + strlen (addr.sun_path);
1929#endif /* HAVE_SUN_LEN */
1930
1931 ret = connect (sock, (struct sockaddr *) &addr, len);
1932 if (ret < 0)
1933 {
1934#ifdef DEBUG
hasso95e735b2004-08-26 13:08:30 +00001935 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
ajs6099b3b2004-11-20 02:06:59 +00001936 safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001937#endif /* DEBUG */
1938 close (sock);
1939 return -1;
1940 }
1941 vclient->fd = sock;
1942
1943 return 0;
1944}
1945
1946void
1947vtysh_connect_all()
1948{
1949 /* Clear each daemons client structure. */
paulfe067782003-04-07 16:10:05 +00001950 vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_VTYSH_PATH);
1951 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_VTYSH_PATH);
1952 vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_VTYSH_PATH);
1953 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_VTYSH_PATH);
1954 vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_VTYSH_PATH);
1955 vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_VTYSH_PATH);
hassoc25e4582003-12-23 10:39:08 +00001956 vtysh_connect (&vtysh_client[VTYSH_INDEX_ISIS], ISIS_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +00001957}
1958
hasso95e735b2004-08-26 13:08:30 +00001959/* To disable readline's filename completion. */
pauldfc0d9b2003-04-18 23:55:29 +00001960char *
1961vtysh_completion_entry_function (const char *ignore, int invoking_key)
paul718e3742002-12-13 20:15:29 +00001962{
pauldfc0d9b2003-04-18 23:55:29 +00001963 return NULL;
paul718e3742002-12-13 20:15:29 +00001964}
1965
1966void
1967vtysh_readline_init ()
1968{
1969 /* readline related settings. */
1970 rl_bind_key ('?', vtysh_rl_describe);
paul68980082003-03-25 05:07:42 +00001971 rl_completion_entry_function = vtysh_completion_entry_function;
paul718e3742002-12-13 20:15:29 +00001972 rl_attempted_completion_function = (CPPFunction *)new_completion;
1973 /* do not append space after completion. It will be appended
hasso95e735b2004-08-26 13:08:30 +00001974 * in new_completion() function explicitly. */
paul718e3742002-12-13 20:15:29 +00001975 rl_completion_append_character = '\0';
1976}
1977
1978char *
1979vtysh_prompt ()
1980{
1981 struct utsname names;
1982 static char buf[100];
1983 const char*hostname;
1984 extern struct host host;
1985
1986 hostname = host.name;
1987
1988 if (!hostname)
1989 {
1990 uname (&names);
1991 hostname = names.nodename;
1992 }
1993
1994 snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);
1995
1996 return buf;
1997}
1998
1999void
2000vtysh_init_vty ()
2001{
2002 /* Make vty structure. */
2003 vty = vty_new ();
2004 vty->type = VTY_SHELL;
2005 vty->node = VIEW_NODE;
2006
2007 /* Initialize commands. */
2008 cmd_init (0);
2009
2010 /* Install nodes. */
2011 install_node (&bgp_node, NULL);
2012 install_node (&rip_node, NULL);
2013 install_node (&interface_node, NULL);
2014 install_node (&rmap_node, NULL);
2015 install_node (&zebra_node, NULL);
2016 install_node (&bgp_vpnv4_node, NULL);
2017 install_node (&bgp_ipv4_node, NULL);
2018 install_node (&bgp_ipv4m_node, NULL);
2019/* #ifdef HAVE_IPV6 */
2020 install_node (&bgp_ipv6_node, NULL);
2021/* #endif */
2022 install_node (&ospf_node, NULL);
2023/* #ifdef HAVE_IPV6 */
2024 install_node (&ripng_node, NULL);
2025 install_node (&ospf6_node, NULL);
2026/* #endif */
2027 install_node (&keychain_node, NULL);
2028 install_node (&keychain_key_node, NULL);
hassoc25e4582003-12-23 10:39:08 +00002029 install_node (&isis_node, NULL);
hassoe7168df2004-10-03 20:11:32 +00002030 install_node (&vty_node, NULL);
paul718e3742002-12-13 20:15:29 +00002031
2032 vtysh_install_default (VIEW_NODE);
2033 vtysh_install_default (ENABLE_NODE);
2034 vtysh_install_default (CONFIG_NODE);
2035 vtysh_install_default (BGP_NODE);
2036 vtysh_install_default (RIP_NODE);
2037 vtysh_install_default (INTERFACE_NODE);
2038 vtysh_install_default (RMAP_NODE);
2039 vtysh_install_default (ZEBRA_NODE);
2040 vtysh_install_default (BGP_VPNV4_NODE);
2041 vtysh_install_default (BGP_IPV4_NODE);
2042 vtysh_install_default (BGP_IPV4M_NODE);
2043 vtysh_install_default (BGP_IPV6_NODE);
2044 vtysh_install_default (OSPF_NODE);
2045 vtysh_install_default (RIPNG_NODE);
2046 vtysh_install_default (OSPF6_NODE);
hassoc25e4582003-12-23 10:39:08 +00002047 vtysh_install_default (ISIS_NODE);
paul718e3742002-12-13 20:15:29 +00002048 vtysh_install_default (KEYCHAIN_NODE);
2049 vtysh_install_default (KEYCHAIN_KEY_NODE);
hassoe7168df2004-10-03 20:11:32 +00002050 vtysh_install_default (VTY_NODE);
paul718e3742002-12-13 20:15:29 +00002051
2052 install_element (VIEW_NODE, &vtysh_enable_cmd);
2053 install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);
2054 install_element (ENABLE_NODE, &vtysh_disable_cmd);
2055
2056 /* "exit" command. */
2057 install_element (VIEW_NODE, &vtysh_exit_all_cmd);
2058 install_element (VIEW_NODE, &vtysh_quit_all_cmd);
2059 install_element (CONFIG_NODE, &vtysh_exit_all_cmd);
2060 /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */
2061 install_element (ENABLE_NODE, &vtysh_exit_all_cmd);
2062 install_element (ENABLE_NODE, &vtysh_quit_all_cmd);
2063 install_element (RIP_NODE, &vtysh_exit_ripd_cmd);
2064 install_element (RIP_NODE, &vtysh_quit_ripd_cmd);
paul68980082003-03-25 05:07:42 +00002065 install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);
2066 install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);
paul718e3742002-12-13 20:15:29 +00002067 install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);
2068 install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);
paul68980082003-03-25 05:07:42 +00002069 install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
2070 install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
paul718e3742002-12-13 20:15:29 +00002071 install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);
2072 install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);
2073 install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
2074 install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
2075 install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
2076 install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
2077 install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
2078 install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
2079 install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
2080 install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
hassoc25e4582003-12-23 10:39:08 +00002081 install_element (ISIS_NODE, &vtysh_exit_isisd_cmd);
2082 install_element (ISIS_NODE, &vtysh_quit_isisd_cmd);
paul718e3742002-12-13 20:15:29 +00002083 install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
2084 install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
2085 install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
2086 install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
2087 install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);
2088 install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);
hassoe7168df2004-10-03 20:11:32 +00002089 install_element (VTY_NODE, &vtysh_exit_line_vty_cmd);
2090 install_element (VTY_NODE, &vtysh_quit_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002091
2092 /* "end" command. */
2093 install_element (CONFIG_NODE, &vtysh_end_all_cmd);
2094 install_element (ENABLE_NODE, &vtysh_end_all_cmd);
2095 install_element (RIP_NODE, &vtysh_end_all_cmd);
2096 install_element (RIPNG_NODE, &vtysh_end_all_cmd);
2097 install_element (OSPF_NODE, &vtysh_end_all_cmd);
2098 install_element (OSPF6_NODE, &vtysh_end_all_cmd);
2099 install_element (BGP_NODE, &vtysh_end_all_cmd);
2100 install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);
2101 install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);
2102 install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);
2103 install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);
hassoc25e4582003-12-23 10:39:08 +00002104 install_element (ISIS_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002105 install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);
2106 install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
2107 install_element (RMAP_NODE, &vtysh_end_all_cmd);
hassoe7168df2004-10-03 20:11:32 +00002108 install_element (VTY_NODE, &vtysh_end_all_cmd);
paul718e3742002-12-13 20:15:29 +00002109
paul338a9912003-03-01 15:44:10 +00002110 install_element (INTERFACE_NODE, &interface_desc_cmd);
paul464dc8d2003-03-28 02:25:45 +00002111 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
paul718e3742002-12-13 20:15:29 +00002112 install_element (INTERFACE_NODE, &vtysh_end_all_cmd);
2113 install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);
2114 install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);
2115 install_element (CONFIG_NODE, &router_rip_cmd);
2116#ifdef HAVE_IPV6
2117 install_element (CONFIG_NODE, &router_ripng_cmd);
2118#endif
2119 install_element (CONFIG_NODE, &router_ospf_cmd);
2120#ifdef HAVE_IPV6
2121 install_element (CONFIG_NODE, &router_ospf6_cmd);
2122#endif
hassoc25e4582003-12-23 10:39:08 +00002123 install_element (CONFIG_NODE, &router_isis_cmd);
paul718e3742002-12-13 20:15:29 +00002124 install_element (CONFIG_NODE, &router_bgp_cmd);
2125 install_element (BGP_NODE, &address_family_vpnv4_cmd);
2126 install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);
2127 install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);
2128 install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);
2129#ifdef HAVE_IPV6
2130 install_element (BGP_NODE, &address_family_ipv6_cmd);
2131 install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);
2132#endif
2133 install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);
2134 install_element (BGP_IPV4_NODE, &exit_address_family_cmd);
2135 install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);
2136 install_element (BGP_IPV6_NODE, &exit_address_family_cmd);
2137 install_element (CONFIG_NODE, &key_chain_cmd);
2138 install_element (CONFIG_NODE, &route_map_cmd);
hassoe7168df2004-10-03 20:11:32 +00002139 install_element (CONFIG_NODE, &vtysh_line_vty_cmd);
paul718e3742002-12-13 20:15:29 +00002140 install_element (KEYCHAIN_NODE, &key_cmd);
2141 install_element (KEYCHAIN_NODE, &key_chain_cmd);
2142 install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);
2143 install_element (CONFIG_NODE, &vtysh_interface_cmd);
paul32d24632003-05-23 09:25:20 +00002144 install_element (CONFIG_NODE, &vtysh_no_interface_cmd);
paul718e3742002-12-13 20:15:29 +00002145 install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);
2146 install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);
2147 install_element (ENABLE_NODE, &vtysh_write_file_cmd);
hasso4a6e2252003-05-25 11:51:29 +00002148 install_element (ENABLE_NODE, &vtysh_write_cmd);
paul718e3742002-12-13 20:15:29 +00002149
hasso95e735b2004-08-26 13:08:30 +00002150 /* "write terminal" command. */
paul718e3742002-12-13 20:15:29 +00002151 install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);
hassoe7168df2004-10-03 20:11:32 +00002152
2153 install_element (CONFIG_NODE, &vtysh_integrated_config_cmd);
2154 install_element (CONFIG_NODE, &no_vtysh_integrated_config_cmd);
paul718e3742002-12-13 20:15:29 +00002155
hasso95e735b2004-08-26 13:08:30 +00002156 /* "write memory" command. */
paul718e3742002-12-13 20:15:29 +00002157 install_element (ENABLE_NODE, &vtysh_write_memory_cmd);
paul718e3742002-12-13 20:15:29 +00002158
hasso34553cc2004-08-27 13:56:39 +00002159 install_element (VIEW_NODE, &vtysh_terminal_length_cmd);
2160 install_element (ENABLE_NODE, &vtysh_terminal_length_cmd);
2161 install_element (VIEW_NODE, &vtysh_terminal_no_length_cmd);
2162 install_element (ENABLE_NODE, &vtysh_terminal_no_length_cmd);
hassof2799e62004-10-28 17:43:11 +00002163 install_element (VIEW_NODE, &vtysh_show_daemons_cmd);
2164 install_element (ENABLE_NODE, &vtysh_show_daemons_cmd);
hasso34553cc2004-08-27 13:56:39 +00002165
paul718e3742002-12-13 20:15:29 +00002166 install_element (VIEW_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002167 install_element (VIEW_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002168 install_element (VIEW_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002169 install_element (VIEW_NODE, &vtysh_traceroute_ip_cmd);
2170#ifdef HAVE_IPV6
2171 install_element (VIEW_NODE, &vtysh_ping6_cmd);
2172 install_element (VIEW_NODE, &vtysh_traceroute6_cmd);
2173#endif
paul718e3742002-12-13 20:15:29 +00002174 install_element (VIEW_NODE, &vtysh_telnet_cmd);
2175 install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
paul5087df52003-01-25 06:56:09 +00002176 install_element (VIEW_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002177 install_element (ENABLE_NODE, &vtysh_ping_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002178 install_element (ENABLE_NODE, &vtysh_ping_ip_cmd);
paul718e3742002-12-13 20:15:29 +00002179 install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
hasso4eeccf12003-06-25 10:49:55 +00002180 install_element (ENABLE_NODE, &vtysh_traceroute_ip_cmd);
2181#ifdef HAVE_IPV6
2182 install_element (ENABLE_NODE, &vtysh_ping6_cmd);
2183 install_element (ENABLE_NODE, &vtysh_traceroute6_cmd);
2184#endif
paul718e3742002-12-13 20:15:29 +00002185 install_element (ENABLE_NODE, &vtysh_telnet_cmd);
2186 install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
hasso67e29ab2004-08-26 22:21:31 +00002187 install_element (ENABLE_NODE, &vtysh_ssh_cmd);
paul718e3742002-12-13 20:15:29 +00002188 install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
2189 install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
2190 install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
2191
paul718e3742002-12-13 20:15:29 +00002192 install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);
2193 install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);
2194 install_element (CONFIG_NODE, &vtysh_log_file_cmd);
2195 install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);
2196 install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);
2197 install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);
2198 install_element (CONFIG_NODE, &vtysh_log_trap_cmd);
2199 install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);
2200 install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);
2201 install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
hassoe7168df2004-10-03 20:11:32 +00002202
2203 install_element (CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
2204 install_element (CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
2205
2206 install_element (CONFIG_NODE, &vtysh_password_cmd);
2207 install_element (CONFIG_NODE, &vtysh_password_text_cmd);
2208 install_element (CONFIG_NODE, &vtysh_enable_password_cmd);
2209 install_element (CONFIG_NODE, &vtysh_enable_password_text_cmd);
2210 install_element (CONFIG_NODE, &no_vtysh_enable_password_cmd);
2211
paul718e3742002-12-13 20:15:29 +00002212}