2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <lib/version.h>
30 #include "sockunion.h"
40 #include <arpa/telnet.h>
43 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
44 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
45 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
61 static void vty_event (enum event
, int, struct vty
*);
63 /* Extern host structure from command.c */
64 extern struct host host
;
66 /* Vector which store each vty structure. */
69 /* Vty timeout value. */
70 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
72 /* Vty access-class command */
73 static char *vty_accesslist_name
= NULL
;
75 /* Vty access-calss for IPv6. */
76 static char *vty_ipv6_accesslist_name
= NULL
;
78 /* VTY server thread. */
79 static vector Vvty_serv_thread
;
81 /* Current directory. */
85 static int vty_config
;
86 static int vty_config_is_lockless
= 0;
88 /* Login password check. */
89 static int no_password_check
= 0;
91 /* Integrated configuration file path */
92 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
94 static int do_log_commands
= 0;
96 /* VTY standard output function. */
98 vty_out (struct vty
*vty
, const char *format
, ...)
108 va_start (args
, format
);
109 vprintf (format
, args
);
114 /* Try to write to initial buffer. */
115 va_start (args
, format
);
116 len
= vsnprintf (buf
, sizeof(buf
), format
, args
);
119 /* Initial buffer is not enough. */
120 if (len
< 0 || len
>= size
)
129 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
133 va_start (args
, format
);
134 len
= vsnprintf (p
, size
, format
, args
);
137 if (len
> -1 && len
< size
)
142 /* When initial buffer is enough to store all output. */
146 /* Pointer p must point out buffer. */
147 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
149 /* If p is not different with buf, it is allocated buffer. */
151 XFREE (MTYPE_VTY_OUT_BUF
, p
);
158 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
159 const char *format
, struct timestamp_control
*ctl
, va_list va
)
165 if (!ctl
->already_rendered
)
167 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
168 ctl
->already_rendered
= 1;
170 if (ctl
->len
+1 >= sizeof(buf
))
172 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
177 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
179 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
180 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
183 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
184 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
190 if (write(vty
->wfd
, buf
, len
) < 0)
192 if (ERRNO_IO_RETRY(errno
))
193 /* Kernel buffer is full, probably too much debugging output, so just
194 drop the data and ignore. */
196 /* Fatal I/O error. */
197 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
198 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
199 __func__
, vty
->fd
, safe_strerror(errno
));
200 buffer_reset(vty
->obuf
);
201 /* cannot call vty_close, because a parent routine may still try
202 to access the vty struct */
203 vty
->status
= VTY_CLOSE
;
204 shutdown(vty
->fd
, SHUT_RDWR
);
210 /* Output current time to the vty. */
212 vty_time_print (struct vty
*vty
, int cr
)
214 char buf
[QUAGGA_TIMESTAMP_LEN
];
216 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
218 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
222 vty_out (vty
, "%s\n", buf
);
224 vty_out (vty
, "%s ", buf
);
229 /* Say hello to vty interface. */
231 vty_hello (struct vty
*vty
)
238 f
= fopen (host
.motdfile
, "r");
241 while (fgets (buf
, sizeof (buf
), f
))
244 /* work backwards to ignore trailling isspace() */
245 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
248 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
253 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
256 vty_out (vty
, "%s", host
.motd
);
259 /* Put out prompt and wait input from user. */
261 vty_prompt (struct vty
*vty
)
263 struct utsname names
;
266 if (vty
->type
== VTY_TERM
)
268 hostname
= host
.name
;
272 hostname
= names
.nodename
;
274 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
278 /* Send WILL TELOPT_ECHO to remote server. */
280 vty_will_echo (struct vty
*vty
)
282 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
283 vty_out (vty
, "%s", cmd
);
286 /* Make suppress Go-Ahead telnet option. */
288 vty_will_suppress_go_ahead (struct vty
*vty
)
290 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
291 vty_out (vty
, "%s", cmd
);
294 /* Make don't use linemode over telnet. */
296 vty_dont_linemode (struct vty
*vty
)
298 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
299 vty_out (vty
, "%s", cmd
);
302 /* Use window size. */
304 vty_do_window_size (struct vty
*vty
)
306 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
307 vty_out (vty
, "%s", cmd
);
310 #if 0 /* Currently not used. */
311 /* Make don't use lflow vty interface. */
313 vty_dont_lflow_ahead (struct vty
*vty
)
315 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
316 vty_out (vty
, "%s", cmd
);
320 /* Allocate new vty struct. */
324 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
326 new->obuf
= buffer_new(0); /* Use default buffer size. */
327 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
328 new->error_buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
329 new->max
= VTY_BUFSIZ
;
334 /* Authentication of vty */
336 vty_auth (struct vty
*vty
, char *buf
)
339 enum node_type next_node
= 0;
341 char *crypt (const char *, const char *);
347 passwd
= host
.password_encrypt
;
349 passwd
= host
.password
;
351 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
353 next_node
= VIEW_NODE
;
355 case AUTH_ENABLE_NODE
:
357 passwd
= host
.enable_encrypt
;
359 passwd
= host
.enable
;
360 next_node
= ENABLE_NODE
;
367 fail
= strcmp (crypt(buf
, passwd
), passwd
);
369 fail
= strcmp (buf
, passwd
);
377 vty
->node
= next_node
; /* Success ! */
384 if (vty
->node
== AUTH_NODE
)
386 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
387 vty
->status
= VTY_CLOSE
;
391 /* AUTH_ENABLE_NODE */
393 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
394 vty
->status
= VTY_CLOSE
;
400 /* Command execution over the vty interface. */
402 vty_command (struct vty
*vty
, char *buf
)
406 const char *protocolname
;
410 * Log non empty command lines
416 /* Skip white spaces. */
417 while (isspace ((int) *cp
) && *cp
!= '\0')
420 if (cp
!= NULL
&& *cp
!= '\0')
423 char vty_str
[VTY_BUFSIZ
];
424 char prompt_str
[VTY_BUFSIZ
];
426 /* format the base vty info */
427 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
429 for (i
= 0; i
< vector_active (vtyvec
); i
++)
430 if (vty
== vector_slot (vtyvec
, i
))
432 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
437 /* format the prompt */
438 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt (vty
->node
), vty_str
);
440 /* now log the command */
441 zlog(NULL
, LOG_ERR
, "%s%s", prompt_str
, buf
);
443 /* Split readline string up into the vector */
444 vline
= cmd_make_strvec (buf
);
449 #ifdef CONSUMED_TIME_CHECK
453 unsigned long realtime
, cputime
;
456 #endif /* CONSUMED_TIME_CHECK */
458 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
460 /* Get the name of the protocol if any */
462 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
464 protocolname
= zlog_proto_names
[ZLOG_NONE
];
466 #ifdef CONSUMED_TIME_CHECK
468 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
470 /* Warn about CPU hog that must be fixed. */
471 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
472 realtime
/1000, cputime
/1000, buf
);
474 #endif /* CONSUMED_TIME_CHECK */
476 if (ret
!= CMD_SUCCESS
)
480 if (vty
->type
== VTY_FILE
)
481 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
483 case CMD_ERR_AMBIGUOUS
:
484 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
486 case CMD_ERR_NO_MATCH
:
487 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
489 case CMD_ERR_INCOMPLETE
:
490 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
493 cmd_free_strvec (vline
);
498 static const char telnet_backward_char
= 0x08;
499 static const char telnet_space_char
= ' ';
501 /* Basic function to write buffer to vty. */
503 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
505 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
508 /* Should we do buffering here ? And make vty_flush (vty) ? */
509 buffer_put (vty
->obuf
, buf
, nbytes
);
512 /* Ensure length of input buffer. Is buffer is short, double it. */
514 vty_ensure (struct vty
*vty
, int length
)
516 if (vty
->max
<= length
)
519 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
520 vty
->error_buf
= XREALLOC (MTYPE_VTY
, vty
->error_buf
, vty
->max
);
524 /* Basic function to insert character into vty. */
526 vty_self_insert (struct vty
*vty
, char c
)
531 vty_ensure (vty
, vty
->length
+ 1);
532 length
= vty
->length
- vty
->cp
;
533 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
534 vty
->buf
[vty
->cp
] = c
;
536 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
537 for (i
= 0; i
< length
; i
++)
538 vty_write (vty
, &telnet_backward_char
, 1);
544 /* Self insert character 'c' in overwrite mode. */
546 vty_self_insert_overwrite (struct vty
*vty
, char c
)
548 vty_ensure (vty
, vty
->length
+ 1);
549 vty
->buf
[vty
->cp
++] = c
;
551 if (vty
->cp
> vty
->length
)
554 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
557 vty_write (vty
, &c
, 1);
560 /* Insert a word into vty interface with overwrite mode. */
562 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
564 int len
= strlen (str
);
565 vty_write (vty
, str
, len
);
566 strcpy (&vty
->buf
[vty
->cp
], str
);
568 vty
->length
= vty
->cp
;
571 /* Forward character. */
573 vty_forward_char (struct vty
*vty
)
575 if (vty
->cp
< vty
->length
)
577 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
582 /* Backward character. */
584 vty_backward_char (struct vty
*vty
)
589 vty_write (vty
, &telnet_backward_char
, 1);
593 /* Move to the beginning of the line. */
595 vty_beginning_of_line (struct vty
*vty
)
598 vty_backward_char (vty
);
601 /* Move to the end of the line. */
603 vty_end_of_line (struct vty
*vty
)
605 while (vty
->cp
< vty
->length
)
606 vty_forward_char (vty
);
609 static void vty_kill_line_from_beginning (struct vty
*);
610 static void vty_redraw_line (struct vty
*);
612 /* Print command line history. This function is called from
613 vty_next_line and vty_previous_line. */
615 vty_history_print (struct vty
*vty
)
619 vty_kill_line_from_beginning (vty
);
621 /* Get previous line from history buffer */
622 length
= strlen (vty
->hist
[vty
->hp
]);
623 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
624 vty
->cp
= vty
->length
= length
;
626 /* Redraw current line */
627 vty_redraw_line (vty
);
630 /* Show next command line history. */
632 vty_next_line (struct vty
*vty
)
636 if (vty
->hp
== vty
->hindex
)
639 /* Try is there history exist or not. */
641 if (try_index
== (VTY_MAXHIST
- 1))
646 /* If there is not history return. */
647 if (vty
->hist
[try_index
] == NULL
)
652 vty_history_print (vty
);
655 /* Show previous command line history. */
657 vty_previous_line (struct vty
*vty
)
663 try_index
= VTY_MAXHIST
- 1;
667 if (vty
->hist
[try_index
] == NULL
)
672 vty_history_print (vty
);
675 /* This function redraw all of the command line character. */
677 vty_redraw_line (struct vty
*vty
)
679 vty_write (vty
, vty
->buf
, vty
->length
);
680 vty
->cp
= vty
->length
;
685 vty_forward_word (struct vty
*vty
)
687 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
688 vty_forward_char (vty
);
690 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
691 vty_forward_char (vty
);
694 /* Backward word without skipping training space. */
696 vty_backward_pure_word (struct vty
*vty
)
698 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
699 vty_backward_char (vty
);
704 vty_backward_word (struct vty
*vty
)
706 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
707 vty_backward_char (vty
);
709 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
710 vty_backward_char (vty
);
713 /* When '^D' is typed at the beginning of the line we move to the down
716 vty_down_level (struct vty
*vty
)
718 vty_out (vty
, "%s", VTY_NEWLINE
);
719 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
724 /* When '^Z' is received from vty, move down to the enable mode. */
726 vty_end_config (struct vty
*vty
)
728 vty_out (vty
, "%s", VTY_NEWLINE
);
745 case BGP_ENCAPV6_NODE
:
746 case BGP_VNC_DEFAULTS_NODE
:
747 case BGP_VNC_NVE_GROUP_NODE
:
748 case BGP_VNC_L2_GROUP_NODE
:
759 case LDP_IPV4_IFACE_NODE
:
760 case LDP_IPV6_IFACE_NODE
:
762 case LDP_PSEUDOWIRE_NODE
:
765 case KEYCHAIN_KEY_NODE
:
769 vty_config_unlock (vty
);
770 vty
->node
= ENABLE_NODE
;
773 /* Unknown node, we have to ignore it. */
781 /* Delete a charcter at the current point. */
783 vty_delete_char (struct vty
*vty
)
788 if (vty
->length
== 0)
790 vty_down_level (vty
);
794 if (vty
->cp
== vty
->length
)
795 return; /* completion need here? */
797 size
= vty
->length
- vty
->cp
;
800 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
801 vty
->buf
[vty
->length
] = '\0';
803 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
806 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
807 vty_write (vty
, &telnet_space_char
, 1);
809 for (i
= 0; i
< size
; i
++)
810 vty_write (vty
, &telnet_backward_char
, 1);
813 /* Delete a character before the point. */
815 vty_delete_backward_char (struct vty
*vty
)
820 vty_backward_char (vty
);
821 vty_delete_char (vty
);
824 /* Kill rest of line from current point. */
826 vty_kill_line (struct vty
*vty
)
831 size
= vty
->length
- vty
->cp
;
836 for (i
= 0; i
< size
; i
++)
837 vty_write (vty
, &telnet_space_char
, 1);
838 for (i
= 0; i
< size
; i
++)
839 vty_write (vty
, &telnet_backward_char
, 1);
841 memset (&vty
->buf
[vty
->cp
], 0, size
);
842 vty
->length
= vty
->cp
;
845 /* Kill line from the beginning. */
847 vty_kill_line_from_beginning (struct vty
*vty
)
849 vty_beginning_of_line (vty
);
853 /* Delete a word before the point. */
855 vty_forward_kill_word (struct vty
*vty
)
857 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
858 vty_delete_char (vty
);
859 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
860 vty_delete_char (vty
);
863 /* Delete a word before the point. */
865 vty_backward_kill_word (struct vty
*vty
)
867 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
868 vty_delete_backward_char (vty
);
869 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
870 vty_delete_backward_char (vty
);
873 /* Transpose chars before or at the point. */
875 vty_transpose_chars (struct vty
*vty
)
879 /* If length is short or point is near by the beginning of line then
881 if (vty
->length
< 2 || vty
->cp
< 1)
884 /* In case of point is located at the end of the line. */
885 if (vty
->cp
== vty
->length
)
887 c1
= vty
->buf
[vty
->cp
- 1];
888 c2
= vty
->buf
[vty
->cp
- 2];
890 vty_backward_char (vty
);
891 vty_backward_char (vty
);
892 vty_self_insert_overwrite (vty
, c1
);
893 vty_self_insert_overwrite (vty
, c2
);
897 c1
= vty
->buf
[vty
->cp
];
898 c2
= vty
->buf
[vty
->cp
- 1];
900 vty_backward_char (vty
);
901 vty_self_insert_overwrite (vty
, c1
);
902 vty_self_insert_overwrite (vty
, c2
);
906 /* Do completion at vty interface. */
908 vty_complete_command (struct vty
*vty
)
912 char **matched
= NULL
;
915 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
918 vline
= cmd_make_strvec (vty
->buf
);
922 /* In case of 'help \t'. */
923 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
924 vector_set (vline
, NULL
);
926 matched
= cmd_complete_command_lib (vline
, vty
, &ret
, 1);
928 cmd_free_strvec (vline
);
930 vty_out (vty
, "%s", VTY_NEWLINE
);
933 case CMD_ERR_AMBIGUOUS
:
934 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
936 vty_redraw_line (vty
);
938 case CMD_ERR_NO_MATCH
:
939 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
941 vty_redraw_line (vty
);
943 case CMD_COMPLETE_FULL_MATCH
:
945 vty_redraw_line (vty
);
946 vty_backward_pure_word (vty
);
947 vty_insert_word_overwrite (vty
, matched
[0]);
948 vty_self_insert (vty
, ' ');
949 XFREE (MTYPE_TMP
, matched
[0]);
951 case CMD_COMPLETE_MATCH
:
953 vty_redraw_line (vty
);
954 vty_backward_pure_word (vty
);
955 vty_insert_word_overwrite (vty
, matched
[0]);
956 XFREE (MTYPE_TMP
, matched
[0]);
957 vector_only_index_free (matched
);
960 case CMD_COMPLETE_LIST_MATCH
:
961 for (i
= 0; matched
[i
] != NULL
; i
++)
963 if (i
!= 0 && ((i
% 6) == 0))
964 vty_out (vty
, "%s", VTY_NEWLINE
);
965 vty_out (vty
, "%-10s ", matched
[i
]);
966 XFREE (MTYPE_TMP
, matched
[i
]);
968 vty_out (vty
, "%s", VTY_NEWLINE
);
971 vty_redraw_line (vty
);
973 case CMD_ERR_NOTHING_TODO
:
975 vty_redraw_line (vty
);
981 vector_only_index_free (matched
);
985 vty_describe_fold (struct vty
*vty
, int cmd_width
,
986 unsigned int desc_width
, struct cmd_token
*token
)
992 cmd
= token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
;
996 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
1000 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
1002 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
1004 for (pos
= desc_width
; pos
> 0; pos
--)
1005 if (*(p
+ pos
) == ' ')
1011 strncpy (buf
, p
, pos
);
1013 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
1018 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
1020 XFREE (MTYPE_TMP
, buf
);
1023 /* Describe matched command function. */
1025 vty_describe_command (struct vty
*vty
)
1030 unsigned int i
, width
, desc_width
;
1031 struct cmd_token
*token
, *token_cr
= NULL
;
1033 vline
= cmd_make_strvec (vty
->buf
);
1035 /* In case of '> ?'. */
1038 vline
= vector_init (1);
1039 vector_set (vline
, NULL
);
1042 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
1043 vector_set (vline
, NULL
);
1045 describe
= cmd_describe_command (vline
, vty
, &ret
);
1047 vty_out (vty
, "%s", VTY_NEWLINE
);
1049 /* Ambiguous error. */
1052 case CMD_ERR_AMBIGUOUS
:
1053 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1056 case CMD_ERR_NO_MATCH
:
1057 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1062 /* Get width of command string. */
1064 for (i
= 0; i
< vector_active (describe
); i
++)
1065 if ((token
= vector_slot (describe
, i
)) != NULL
)
1069 if (token
->cmd
[0] == '\0')
1072 len
= strlen (token
->cmd
);
1073 if (token
->cmd
[0] == '.')
1080 /* Get width of description string. */
1081 desc_width
= vty
->width
- (width
+ 6);
1083 /* Print out description. */
1084 for (i
= 0; i
< vector_active (describe
); i
++)
1085 if ((token
= vector_slot (describe
, i
)) != NULL
)
1087 if (token
->cmd
[0] == '\0')
1090 if (strcmp (token
->cmd
, command_cr
) == 0)
1097 vty_out (vty
, " %-s%s",
1098 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1100 else if (desc_width
>= strlen (token
->desc
))
1101 vty_out (vty
, " %-*s %s%s", width
,
1102 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1103 token
->desc
, VTY_NEWLINE
);
1105 vty_describe_fold (vty
, width
, desc_width
, token
);
1108 vty_out (vty
, " %-*s %s%s", width
1109 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1110 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1114 if ((token
= token_cr
))
1117 vty_out (vty
, " %-s%s",
1118 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1120 else if (desc_width
>= strlen (token
->desc
))
1121 vty_out (vty
, " %-*s %s%s", width
,
1122 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1123 token
->desc
, VTY_NEWLINE
);
1125 vty_describe_fold (vty
, width
, desc_width
, token
);
1129 cmd_free_strvec (vline
);
1131 vector_free (describe
);
1134 vty_redraw_line (vty
);
1138 vty_clear_buf (struct vty
*vty
)
1140 memset (vty
->buf
, 0, vty
->max
);
1143 /* ^C stop current input and do not add command line to the history. */
1145 vty_stop_input (struct vty
*vty
)
1147 vty
->cp
= vty
->length
= 0;
1148 vty_clear_buf (vty
);
1149 vty_out (vty
, "%s", VTY_NEWLINE
);
1155 /* Nothing to do. */
1158 case INTERFACE_NODE
:
1169 case LDP_IPV4_IFACE_NODE
:
1170 case LDP_IPV6_IFACE_NODE
:
1171 case LDP_L2VPN_NODE
:
1172 case LDP_PSEUDOWIRE_NODE
:
1175 case KEYCHAIN_KEY_NODE
:
1179 vty_config_unlock (vty
);
1180 vty
->node
= ENABLE_NODE
;
1183 /* Unknown node, we have to ignore it. */
1188 /* Set history pointer to the latest one. */
1189 vty
->hp
= vty
->hindex
;
1192 /* Add current command line to the history buffer. */
1194 vty_hist_add (struct vty
*vty
)
1198 if (vty
->length
== 0)
1201 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1203 /* Ignore the same string as previous one. */
1204 if (vty
->hist
[index
])
1205 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1207 vty
->hp
= vty
->hindex
;
1211 /* Insert history entry. */
1212 if (vty
->hist
[vty
->hindex
])
1213 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1214 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1216 /* History index rotation. */
1218 if (vty
->hindex
== VTY_MAXHIST
)
1221 vty
->hp
= vty
->hindex
;
1224 /* #define TELNET_OPTION_DEBUG */
1226 /* Get telnet window size. */
1228 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1230 #ifdef TELNET_OPTION_DEBUG
1233 for (i
= 0; i
< nbytes
; i
++)
1238 vty_out (vty
, "IAC ");
1241 vty_out (vty
, "WILL ");
1244 vty_out (vty
, "WONT ");
1247 vty_out (vty
, "DO ");
1250 vty_out (vty
, "DONT ");
1253 vty_out (vty
, "SB ");
1256 vty_out (vty
, "SE ");
1259 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1262 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1265 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1268 vty_out (vty
, "%x ", buf
[i
]);
1272 vty_out (vty
, "%s", VTY_NEWLINE
);
1274 #endif /* TELNET_OPTION_DEBUG */
1280 vty
->iac_sb_in_progress
= 1;
1285 if (!vty
->iac_sb_in_progress
)
1288 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1290 vty
->iac_sb_in_progress
= 0;
1293 switch (vty
->sb_buf
[0])
1296 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1297 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1298 "should send %d characters, but we received %lu",
1299 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1300 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1301 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1302 "too small to handle the telnet NAWS option",
1303 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1306 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1307 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1308 #ifdef TELNET_OPTION_DEBUG
1309 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1310 "width %d, height %d%s",
1311 vty
->width
, vty
->height
, VTY_NEWLINE
);
1316 vty
->iac_sb_in_progress
= 0;
1326 /* Execute current command line. */
1328 vty_execute (struct vty
*vty
)
1337 case AUTH_ENABLE_NODE
:
1338 vty_auth (vty
, vty
->buf
);
1341 ret
= vty_command (vty
, vty
->buf
);
1342 if (vty
->type
== VTY_TERM
)
1347 /* Clear command line buffer. */
1348 vty
->cp
= vty
->length
= 0;
1349 vty_clear_buf (vty
);
1351 if (vty
->status
!= VTY_CLOSE
)
1357 #define CONTROL(X) ((X) - '@')
1358 #define VTY_NORMAL 0
1359 #define VTY_PRE_ESCAPE 1
1360 #define VTY_ESCAPE 2
1362 /* Escape character command map. */
1364 vty_escape_map (unsigned char c
, struct vty
*vty
)
1369 vty_previous_line (vty
);
1372 vty_next_line (vty
);
1375 vty_forward_char (vty
);
1378 vty_backward_char (vty
);
1384 /* Go back to normal mode. */
1385 vty
->escape
= VTY_NORMAL
;
1388 /* Quit print out to the buffer. */
1390 vty_buffer_reset (struct vty
*vty
)
1392 buffer_reset (vty
->obuf
);
1394 vty_redraw_line (vty
);
1397 /* Read data via vty socket. */
1399 vty_read (struct thread
*thread
)
1403 unsigned char buf
[VTY_READ_BUFSIZ
];
1405 int vty_sock
= THREAD_FD (thread
);
1406 struct vty
*vty
= THREAD_ARG (thread
);
1409 /* Read raw data from socket */
1410 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1414 if (ERRNO_IO_RETRY(errno
))
1416 vty_event (VTY_READ
, vty_sock
, vty
);
1419 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1420 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1421 __func__
, vty
->fd
, safe_strerror(errno
));
1422 buffer_reset(vty
->obuf
);
1424 vty
->status
= VTY_CLOSE
;
1427 for (i
= 0; i
< nbytes
; i
++)
1442 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1444 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1445 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1452 /* In case of telnet command */
1454 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1461 if (vty
->status
== VTY_MORE
)
1468 vty_buffer_reset (vty
);
1470 #if 0 /* More line does not work for "show ip bgp". */
1473 vty
->status
= VTY_MORELINE
;
1482 /* Escape character. */
1483 if (vty
->escape
== VTY_ESCAPE
)
1485 vty_escape_map (buf
[i
], vty
);
1489 /* Pre-escape status. */
1490 if (vty
->escape
== VTY_PRE_ESCAPE
)
1495 vty
->escape
= VTY_ESCAPE
;
1498 vty_backward_word (vty
);
1499 vty
->escape
= VTY_NORMAL
;
1502 vty_forward_word (vty
);
1503 vty
->escape
= VTY_NORMAL
;
1506 vty_forward_kill_word (vty
);
1507 vty
->escape
= VTY_NORMAL
;
1511 vty_backward_kill_word (vty
);
1512 vty
->escape
= VTY_NORMAL
;
1515 vty
->escape
= VTY_NORMAL
;
1524 vty_beginning_of_line (vty
);
1527 vty_backward_char (vty
);
1530 vty_stop_input (vty
);
1533 vty_delete_char (vty
);
1536 vty_end_of_line (vty
);
1539 vty_forward_char (vty
);
1543 vty_delete_backward_char (vty
);
1546 vty_kill_line (vty
);
1549 vty_next_line (vty
);
1552 vty_previous_line (vty
);
1555 vty_transpose_chars (vty
);
1558 vty_kill_line_from_beginning (vty
);
1561 vty_backward_kill_word (vty
);
1564 vty_end_config (vty
);
1568 vty_out (vty
, "%s", VTY_NEWLINE
);
1572 vty_complete_command (vty
);
1575 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1576 vty_self_insert (vty
, buf
[i
]);
1578 vty_describe_command (vty
);
1581 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1583 vty
->escape
= VTY_ESCAPE
;
1587 vty
->escape
= VTY_PRE_ESCAPE
;
1590 if (buf
[i
] > 31 && buf
[i
] < 127)
1591 vty_self_insert (vty
, buf
[i
]);
1597 if (vty
->status
== VTY_CLOSE
)
1601 vty_event (VTY_WRITE
, vty
->wfd
, vty
);
1602 vty_event (VTY_READ
, vty_sock
, vty
);
1607 /* Flush buffer to the vty. */
1609 vty_flush (struct thread
*thread
)
1612 buffer_status_t flushrc
;
1613 int vty_sock
= THREAD_FD (thread
);
1614 struct vty
*vty
= THREAD_ARG (thread
);
1616 vty
->t_write
= NULL
;
1618 /* Tempolary disable read thread. */
1619 if ((vty
->lines
== 0) && vty
->t_read
)
1621 thread_cancel (vty
->t_read
);
1625 /* Function execution continue. */
1626 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1628 /* N.B. if width is 0, that means we don't know the window size. */
1629 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1630 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1631 else if (vty
->status
== VTY_MORELINE
)
1632 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1635 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1636 vty
->lines
>= 0 ? vty
->lines
:
1642 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1643 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1645 buffer_reset(vty
->obuf
);
1649 if (vty
->status
== VTY_CLOSE
)
1653 vty
->status
= VTY_NORMAL
;
1654 if (vty
->lines
== 0)
1655 vty_event (VTY_READ
, vty_sock
, vty
);
1658 case BUFFER_PENDING
:
1659 /* There is more data waiting to be written. */
1660 vty
->status
= VTY_MORE
;
1661 if (vty
->lines
== 0)
1662 vty_event (VTY_WRITE
, vty_sock
, vty
);
1669 /* allocate and initialise vty */
1671 vty_new_init (int vty_sock
)
1677 vty
->wfd
= vty_sock
;
1678 vty
->type
= VTY_TERM
;
1679 vty
->node
= AUTH_NODE
;
1682 vty_clear_buf (vty
);
1684 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1687 vector_set_index (vtyvec
, vty_sock
, vty
);
1688 vty
->status
= VTY_NORMAL
;
1691 vty
->iac_sb_in_progress
= 0;
1697 /* Create new vty structure. */
1699 vty_create (int vty_sock
, union sockunion
*su
)
1701 char buf
[SU_ADDRSTRLEN
];
1704 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1706 /* Allocate new vty structure and set up default values. */
1707 vty
= vty_new_init (vty_sock
);
1709 /* configurable parameters not part of basic init */
1710 vty
->v_timeout
= vty_timeout_val
;
1711 strcpy (vty
->address
, buf
);
1712 if (no_password_check
)
1715 vty
->node
= ENABLE_NODE
;
1717 vty
->node
= VIEW_NODE
;
1719 if (host
.lines
>= 0)
1720 vty
->lines
= host
.lines
;
1722 if (! no_password_check
)
1724 /* Vty is not available if password isn't set. */
1725 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1727 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1728 vty
->status
= VTY_CLOSE
;
1734 /* Say hello to the world. */
1736 if (! no_password_check
)
1737 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1739 /* Setting up terminal. */
1740 vty_will_echo (vty
);
1741 vty_will_suppress_go_ahead (vty
);
1743 vty_dont_linemode (vty
);
1744 vty_do_window_size (vty
);
1745 /* vty_dont_lflow_ahead (vty); */
1749 /* Add read/write thread. */
1750 vty_event (VTY_WRITE
, vty_sock
, vty
);
1751 vty_event (VTY_READ
, vty_sock
, vty
);
1756 /* create vty for stdio */
1757 static struct termios stdio_orig_termios
;
1758 static struct vty
*stdio_vty
= NULL
;
1759 static void (*stdio_vty_atclose
)(void);
1762 vty_stdio_reset (void)
1766 tcsetattr (0, TCSANOW
, &stdio_orig_termios
);
1769 if (stdio_vty_atclose
)
1770 stdio_vty_atclose ();
1771 stdio_vty_atclose
= NULL
;
1776 vty_stdio (void (*atclose
)())
1779 struct termios termios
;
1781 /* refuse creating two vtys on stdio */
1785 vty
= stdio_vty
= vty_new_init (0);
1786 stdio_vty_atclose
= atclose
;
1789 /* always have stdio vty in a known _unchangeable_ state, don't want config
1790 * to have any effect here to make sure scripting this works as intended */
1791 vty
->node
= ENABLE_NODE
;
1793 strcpy (vty
->address
, "console");
1795 if (!tcgetattr (0, &stdio_orig_termios
))
1797 termios
= stdio_orig_termios
;
1798 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
1799 | INLCR
| IGNCR
| ICRNL
| IXON
);
1800 termios
.c_oflag
&= ~OPOST
;
1801 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
1802 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1803 termios
.c_cflag
|= CS8
;
1804 tcsetattr (0, TCSANOW
, &termios
);
1809 /* Add read/write thread. */
1810 vty_event (VTY_WRITE
, 1, vty
);
1811 vty_event (VTY_READ
, 0, vty
);
1816 /* Accept connection from the network. */
1818 vty_accept (struct thread
*thread
)
1826 struct access_list
*acl
= NULL
;
1827 char buf
[SU_ADDRSTRLEN
];
1829 accept_sock
= THREAD_FD (thread
);
1831 /* We continue hearing vty socket. */
1832 vty_event (VTY_SERV
, accept_sock
, NULL
);
1834 memset (&su
, 0, sizeof (union sockunion
));
1836 /* We can handle IPv4 or IPv6 socket. */
1837 vty_sock
= sockunion_accept (accept_sock
, &su
);
1840 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1843 set_nonblocking(vty_sock
);
1844 set_cloexec(vty_sock
);
1846 sockunion2hostprefix (&su
, &p
);
1848 /* VTY's accesslist apply. */
1849 if (p
.family
== AF_INET
&& vty_accesslist_name
)
1851 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1852 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1854 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1855 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1858 /* continue accepting connections */
1859 vty_event (VTY_SERV
, accept_sock
, NULL
);
1866 /* VTY's ipv6 accesslist apply. */
1867 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1869 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1870 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1872 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1873 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1876 /* continue accepting connections */
1877 vty_event (VTY_SERV
, accept_sock
, NULL
);
1882 #endif /* HAVE_IPV6 */
1885 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1886 (char *) &on
, sizeof (on
));
1888 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1889 safe_strerror (errno
));
1891 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1892 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1894 vty_create (vty_sock
, &su
);
1901 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1904 struct addrinfo req
;
1905 struct addrinfo
*ainfo
;
1906 struct addrinfo
*ainfo_save
;
1908 char port_str
[BUFSIZ
];
1910 memset (&req
, 0, sizeof (struct addrinfo
));
1911 req
.ai_flags
= AI_PASSIVE
;
1912 req
.ai_family
= AF_UNSPEC
;
1913 req
.ai_socktype
= SOCK_STREAM
;
1914 sprintf (port_str
, "%d", port
);
1915 port_str
[sizeof (port_str
) - 1] = '\0';
1917 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1921 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1929 if (ainfo
->ai_family
!= AF_INET
1931 && ainfo
->ai_family
!= AF_INET6
1932 #endif /* HAVE_IPV6 */
1936 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1940 sockopt_v6only (ainfo
->ai_family
, sock
);
1941 sockopt_reuseaddr (sock
);
1942 sockopt_reuseport (sock
);
1945 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1948 close (sock
); /* Avoid sd leak. */
1952 ret
= listen (sock
, 3);
1955 close (sock
); /* Avoid sd leak. */
1959 vty_event (VTY_SERV
, sock
, NULL
);
1961 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1963 freeaddrinfo (ainfo_save
);
1965 #else /* HAVE_IPV6 */
1967 /* Make vty server socket. */
1969 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1976 memset (&su
, 0, sizeof (union sockunion
));
1977 su
.sa
.sa_family
= family
;
1982 naddr
=&su
.sin
.sin_addr
;
1986 naddr
=&su
.sin6
.sin6_addr
;
1992 switch(inet_pton(family
,addr
,naddr
))
1995 zlog_err("bad address %s",addr
);
1999 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
2003 /* Make new socket. */
2004 accept_sock
= sockunion_stream_socket (&su
);
2005 if (accept_sock
< 0)
2008 /* This is server, so reuse address. */
2009 sockopt_reuseaddr (accept_sock
);
2010 sockopt_reuseport (accept_sock
);
2011 set_cloexec (accept_sock
);
2013 /* Bind socket to universal address and given port. */
2014 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
2017 zlog_warn("can't bind socket");
2018 close (accept_sock
); /* Avoid sd leak. */
2022 /* Listen socket under queue 3. */
2023 ret
= listen (accept_sock
, 3);
2026 zlog (NULL
, LOG_WARNING
, "can't listen socket");
2027 close (accept_sock
); /* Avoid sd leak. */
2031 /* Add vty server event. */
2032 vty_event (VTY_SERV
, accept_sock
, NULL
);
2034 #endif /* HAVE_IPV6 */
2037 /* For sockaddr_un. */
2040 /* VTY shell UNIX domain socket. */
2042 vty_serv_un (const char *path
)
2046 struct sockaddr_un serv
;
2048 struct zprivs_ids_t ids
;
2050 /* First of all, unlink existing socket */
2054 old_mask
= umask (0007);
2056 /* Make UNIX domain socket. */
2057 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2060 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
2064 /* Make server socket. */
2065 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2066 serv
.sun_family
= AF_UNIX
;
2067 strncpy (serv
.sun_path
, path
, strlen (path
));
2068 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2069 len
= serv
.sun_len
= SUN_LEN(&serv
);
2071 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2072 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2076 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2079 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2080 close (sock
); /* Avoid sd leak. */
2084 ret
= listen (sock
, 5);
2087 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
2088 close (sock
); /* Avoid sd leak. */
2094 zprivs_get_ids(&ids
);
2096 if (ids
.gid_vty
> 0)
2098 /* set group of socket */
2099 if ( chown (path
, -1, ids
.gid_vty
) )
2101 zlog_err ("vty_serv_un: could chown socket, %s",
2102 safe_strerror (errno
) );
2106 vty_event (VTYSH_SERV
, sock
, NULL
);
2109 /* #define VTYSH_DEBUG 1 */
2112 vtysh_accept (struct thread
*thread
)
2117 struct sockaddr_un client
;
2120 accept_sock
= THREAD_FD (thread
);
2122 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
2124 memset (&client
, 0, sizeof (struct sockaddr_un
));
2125 client_len
= sizeof (struct sockaddr_un
);
2127 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
2128 (socklen_t
*) &client_len
);
2132 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
2136 if (set_nonblocking(sock
) < 0)
2138 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2139 " %s, closing", sock
, safe_strerror (errno
));
2146 printf ("VTY shell accept\n");
2147 #endif /* VTYSH_DEBUG */
2152 vty
->type
= VTY_SHELL_SERV
;
2153 vty
->node
= VIEW_NODE
;
2155 vty_event (VTYSH_READ
, sock
, vty
);
2161 vtysh_flush(struct vty
*vty
)
2163 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
))
2165 case BUFFER_PENDING
:
2166 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2169 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2170 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2171 buffer_reset(vty
->obuf
);
2182 vtysh_read (struct thread
*thread
)
2188 unsigned char buf
[VTY_READ_BUFSIZ
];
2190 u_char header
[4] = {0, 0, 0, 0};
2192 sock
= THREAD_FD (thread
);
2193 vty
= THREAD_ARG (thread
);
2196 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2200 if (ERRNO_IO_RETRY(errno
))
2202 vty_event (VTYSH_READ
, sock
, vty
);
2205 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2206 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2207 __func__
, sock
, safe_strerror(errno
));
2209 buffer_reset(vty
->obuf
);
2212 printf ("close vtysh\n");
2213 #endif /* VTYSH_DEBUG */
2218 printf ("line: %.*s\n", nbytes
, buf
);
2219 #endif /* VTYSH_DEBUG */
2221 for (p
= buf
; p
< buf
+nbytes
; p
++)
2223 vty_ensure(vty
, vty
->length
+1);
2224 vty
->buf
[vty
->length
++] = *p
;
2227 /* Pass this line to parser. */
2228 ret
= vty_execute (vty
);
2229 /* Note that vty_execute clears the command buffer and resets
2230 vty->length to 0. */
2232 /* Return result. */
2234 printf ("result: %d\n", ret
);
2235 printf ("vtysh node: %d\n", vty
->node
);
2236 #endif /* VTYSH_DEBUG */
2239 buffer_put(vty
->obuf
, header
, 4);
2241 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2242 /* Try to flush results; exit if a write error occurs. */
2247 vty_event (VTYSH_READ
, sock
, vty
);
2253 vtysh_write (struct thread
*thread
)
2255 struct vty
*vty
= THREAD_ARG (thread
);
2257 vty
->t_write
= NULL
;
2264 /* Determine address family to bind. */
2266 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2268 /* If port is set to 0, do not listen on TCP/IP at all! */
2273 vty_serv_sock_addrinfo (addr
, port
);
2274 #else /* ! HAVE_IPV6 */
2275 vty_serv_sock_family (addr
,port
, AF_INET
);
2276 #endif /* HAVE_IPV6 */
2284 /* Close vty interface. Warning: call this only from functions that
2285 will be careful not to access the vty afterwards (since it has
2286 now been freed). This is safest from top-level functions (called
2287 directly by the thread dispatcher). */
2289 vty_close (struct vty
*vty
)
2293 /* Cancel threads.*/
2295 thread_cancel (vty
->t_read
);
2297 thread_cancel (vty
->t_write
);
2299 thread_cancel (vty
->t_timeout
);
2302 buffer_flush_all (vty
->obuf
, vty
->wfd
);
2304 /* Free input buffer. */
2305 buffer_free (vty
->obuf
);
2307 /* Free command history. */
2308 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2310 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2313 vector_unset (vtyvec
, vty
->fd
);
2322 XFREE (MTYPE_VTY
, vty
->buf
);
2325 XFREE (MTYPE_VTY
, vty
->error_buf
);
2327 /* Check configure. */
2328 vty_config_unlock (vty
);
2331 XFREE (MTYPE_VTY
, vty
);
2334 /* When time out occur output message then close connection. */
2336 vty_timeout (struct thread
*thread
)
2340 vty
= THREAD_ARG (thread
);
2341 vty
->t_timeout
= NULL
;
2345 buffer_reset (vty
->obuf
);
2346 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2348 /* Close connection. */
2349 vty
->status
= VTY_CLOSE
;
2355 /* Read up configuration file from file_name. */
2357 vty_read_file (FILE *confp
)
2361 unsigned int line_num
= 0;
2364 vty
->wfd
= dup(STDERR_FILENO
); /* vty_close() will close this */
2367 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2368 vty
->wfd
= STDOUT_FILENO
;
2370 vty
->fd
= STDIN_FILENO
;
2371 vty
->type
= VTY_FILE
;
2372 vty
->node
= CONFIG_NODE
;
2374 /* Execute configuration file */
2375 ret
= config_from_file (vty
, confp
, &line_num
);
2377 /* Flush any previous errors before printing messages below */
2378 buffer_flush_all (vty
->obuf
, vty
->fd
);
2380 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2384 case CMD_ERR_AMBIGUOUS
:
2385 fprintf (stderr
, "*** Error reading config: Ambiguous command.\n");
2387 case CMD_ERR_NO_MATCH
:
2388 fprintf (stderr
, "*** Error reading config: There is no such command.\n");
2391 fprintf (stderr
, "*** Error occured processing line %u, below:\n%s\n",
2392 line_num
, vty
->error_buf
);
2399 vty_use_backup_config (char *fullpath
)
2401 char *fullpath_sav
, *fullpath_tmp
;
2408 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2409 strcpy (fullpath_sav
, fullpath
);
2410 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2411 if (stat (fullpath_sav
, &buf
) == -1)
2413 free (fullpath_sav
);
2417 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2418 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2420 /* Open file to configuration write. */
2421 tmp
= mkstemp (fullpath_tmp
);
2424 free (fullpath_sav
);
2425 free (fullpath_tmp
);
2429 sav
= open (fullpath_sav
, O_RDONLY
);
2432 unlink (fullpath_tmp
);
2433 free (fullpath_sav
);
2434 free (fullpath_tmp
);
2438 while((c
= read (sav
, buffer
, 512)) > 0)
2440 if (write (tmp
, buffer
, c
) <= 0)
2442 free (fullpath_sav
);
2443 free (fullpath_tmp
);
2452 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2454 unlink (fullpath_tmp
);
2455 free (fullpath_sav
);
2456 free (fullpath_tmp
);
2460 if (link (fullpath_tmp
, fullpath
) == 0)
2461 ret
= fopen (fullpath
, "r");
2463 unlink (fullpath_tmp
);
2465 free (fullpath_sav
);
2466 free (fullpath_tmp
);
2470 /* Read up configuration file from file_name. */
2472 vty_read_config (char *config_file
,
2473 char *config_default_dir
)
2475 char cwd
[MAXPATHLEN
];
2480 /* If -f flag specified. */
2481 if (config_file
!= NULL
)
2483 if (! IS_DIRECTORY_SEP (config_file
[0]))
2485 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2487 fprintf (stderr
, "Failure to determine Current Working Directory %d!\n", errno
);
2490 tmp
= XMALLOC (MTYPE_TMP
,
2491 strlen (cwd
) + strlen (config_file
) + 2);
2492 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2496 fullpath
= config_file
;
2498 confp
= fopen (fullpath
, "r");
2502 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2503 __func__
, fullpath
, safe_strerror (errno
));
2505 confp
= vty_use_backup_config (fullpath
);
2507 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2510 fprintf (stderr
, "can't open configuration file [%s]\n",
2519 host_config_set (config_default_dir
);
2523 struct stat conf_stat
;
2525 /* !!!!PLEASE LEAVE!!!!
2526 * This is NEEDED for use with vtysh -b, or else you can get
2527 * a real configuration food fight with a lot garbage in the
2528 * merged configuration file it creates coming from the per
2529 * daemon configuration files. This also allows the daemons
2530 * to start if there default configuration file is not
2531 * present or ignore them, as needed when using vtysh -b to
2532 * configure the daemons at boot - MAG
2535 /* Stat for vtysh Zebra.conf, if found startup and wait for
2536 * boot configuration
2539 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2541 ret
= stat (integrate_default
, &conf_stat
);
2543 goto tmp_free_and_out
;
2546 confp
= fopen (config_default_dir
, "r");
2549 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2550 __func__
, config_default_dir
, safe_strerror (errno
));
2552 confp
= vty_use_backup_config (config_default_dir
);
2555 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2556 fullpath
= config_default_dir
;
2560 fprintf (stderr
, "can't open configuration file [%s]\n",
2561 config_default_dir
);
2562 goto tmp_free_and_out
;
2566 fullpath
= config_default_dir
;
2569 vty_read_file (confp
);
2573 host_config_set (fullpath
);
2577 XFREE (MTYPE_TMP
, fullpath
);
2580 /* Small utility function which output log to the VTY. */
2582 vty_log (const char *level
, const char *proto_str
,
2583 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2591 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2592 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2597 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2602 /* Async-signal-safe version of vty_log for fixed strings. */
2604 vty_log_fixed (char *buf
, size_t len
)
2607 struct iovec iov
[2];
2608 char crlf
[4] = "\r\n";
2610 /* vty may not have been initialised */
2614 iov
[0].iov_base
= buf
;
2615 iov
[0].iov_len
= len
;
2616 iov
[1].iov_base
= crlf
;
2619 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2622 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2623 /* N.B. We don't care about the return code, since process is
2624 most likely just about to die anyway. */
2625 if (writev(vty
->wfd
, iov
, 2) == -1)
2627 fprintf(stderr
, "Failure to writev: %d\n", errno
);
2634 vty_config_lock (struct vty
*vty
)
2636 if (vty_config_is_lockless
)
2638 if (vty_config
== 0)
2647 vty_config_unlock (struct vty
*vty
)
2649 if (vty_config_is_lockless
)
2651 if (vty_config
== 1 && vty
->config
== 1)
2660 vty_config_lockless (void)
2662 vty_config_is_lockless
= 1;
2665 /* Master of the threads. */
2666 static struct thread_master
*vty_master
;
2669 vty_event (enum event event
, int sock
, struct vty
*vty
)
2671 struct thread
*vty_serv_thread
;
2676 vty_serv_thread
= thread_add_read (vty_master
, vty_accept
, vty
, sock
);
2677 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2681 vty_serv_thread
= thread_add_read (vty_master
, vtysh_accept
, vty
, sock
);
2682 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2685 vty
->t_read
= thread_add_read (vty_master
, vtysh_read
, vty
, sock
);
2688 vty
->t_write
= thread_add_write (vty_master
, vtysh_write
, vty
, sock
);
2692 vty
->t_read
= thread_add_read (vty_master
, vty_read
, vty
, sock
);
2694 /* Time out treatment. */
2698 thread_cancel (vty
->t_timeout
);
2700 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2705 vty
->t_write
= thread_add_write (vty_master
, vty_flush
, vty
, sock
);
2707 case VTY_TIMEOUT_RESET
:
2710 thread_cancel (vty
->t_timeout
);
2711 vty
->t_timeout
= NULL
;
2716 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2725 "Display who is on vty\n")
2730 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2731 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2732 vty_out (vty
, "%svty[%d] connected from %s.%s",
2733 v
->config
? "*" : " ",
2734 i
, v
->address
, VTY_NEWLINE
);
2738 /* Move to vty configuration mode. */
2742 "Configure a terminal line\n"
2743 "Virtual terminal\n")
2745 vty
->node
= VTY_NODE
;
2749 /* Set time out value. */
2751 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2753 unsigned long timeout
= 0;
2755 /* min_str and sec_str are already checked by parser. So it must be
2756 all digit string. */
2759 timeout
= strtol (min_str
, NULL
, 10);
2763 timeout
+= strtol (sec_str
, NULL
, 10);
2765 vty_timeout_val
= timeout
;
2766 vty
->v_timeout
= timeout
;
2767 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2773 DEFUN (exec_timeout_min
,
2774 exec_timeout_min_cmd
,
2775 "exec-timeout <0-35791>",
2776 "Set timeout value\n"
2777 "Timeout value in minutes\n")
2779 return exec_timeout (vty
, argv
[0], NULL
);
2782 DEFUN (exec_timeout_sec
,
2783 exec_timeout_sec_cmd
,
2784 "exec-timeout <0-35791> <0-2147483>",
2785 "Set the EXEC timeout\n"
2786 "Timeout in minutes\n"
2787 "Timeout in seconds\n")
2789 return exec_timeout (vty
, argv
[0], argv
[1]);
2792 DEFUN (no_exec_timeout
,
2793 no_exec_timeout_cmd
,
2796 "Set the EXEC timeout\n")
2798 return exec_timeout (vty
, NULL
, NULL
);
2801 /* Set vty access class. */
2802 DEFUN (vty_access_class
,
2803 vty_access_class_cmd
,
2804 "access-class WORD",
2805 "Filter connections based on an IP access list\n"
2808 if (vty_accesslist_name
)
2809 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2811 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2816 /* Clear vty access class. */
2817 DEFUN (no_vty_access_class
,
2818 no_vty_access_class_cmd
,
2819 "no access-class [WORD]",
2821 "Filter connections based on an IP access list\n"
2824 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2826 vty_out (vty
, "Access-class is not currently applied to vty%s",
2831 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2833 vty_accesslist_name
= NULL
;
2839 /* Set vty access class. */
2840 DEFUN (vty_ipv6_access_class
,
2841 vty_ipv6_access_class_cmd
,
2842 "ipv6 access-class WORD",
2844 "Filter connections based on an IP access list\n"
2845 "IPv6 access list\n")
2847 if (vty_ipv6_accesslist_name
)
2848 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2850 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2855 /* Clear vty access class. */
2856 DEFUN (no_vty_ipv6_access_class
,
2857 no_vty_ipv6_access_class_cmd
,
2858 "no ipv6 access-class [WORD]",
2861 "Filter connections based on an IP access list\n"
2862 "IPv6 access list\n")
2864 if (! vty_ipv6_accesslist_name
||
2865 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2867 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2872 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2874 vty_ipv6_accesslist_name
= NULL
;
2878 #endif /* HAVE_IPV6 */
2884 "Enable password checking\n")
2886 no_password_check
= 0;
2890 DEFUN (no_vty_login
,
2894 "Enable password checking\n")
2896 no_password_check
= 1;
2900 DEFUN (service_advanced_vty
,
2901 service_advanced_vty_cmd
,
2902 "service advanced-vty",
2903 "Set up miscellaneous service\n"
2904 "Enable advanced mode vty interface\n")
2910 DEFUN (no_service_advanced_vty
,
2911 no_service_advanced_vty_cmd
,
2912 "no service advanced-vty",
2914 "Set up miscellaneous service\n"
2915 "Enable advanced mode vty interface\n")
2921 DEFUN (terminal_monitor
,
2922 terminal_monitor_cmd
,
2924 "Set terminal line parameters\n"
2925 "Copy debug output to the current terminal line\n")
2931 DEFUN (terminal_no_monitor
,
2932 terminal_no_monitor_cmd
,
2933 "terminal no monitor",
2934 "Set terminal line parameters\n"
2936 "Copy debug output to the current terminal line\n")
2942 ALIAS (terminal_no_monitor
,
2943 no_terminal_monitor_cmd
,
2944 "no terminal monitor",
2946 "Set terminal line parameters\n"
2947 "Copy debug output to the current terminal line\n")
2949 DEFUN (show_history
,
2953 "Display the session command history\n")
2957 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2959 if (index
== VTY_MAXHIST
)
2965 if (vty
->hist
[index
] != NULL
)
2966 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2975 DEFUN (log_commands
,
2979 "Log all commands (can't be unset without restart)\n")
2981 do_log_commands
= 1;
2985 /* Display current configuration. */
2987 vty_config_write (struct vty
*vty
)
2989 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2991 if (vty_accesslist_name
)
2992 vty_out (vty
, " access-class %s%s",
2993 vty_accesslist_name
, VTY_NEWLINE
);
2995 if (vty_ipv6_accesslist_name
)
2996 vty_out (vty
, " ipv6 access-class %s%s",
2997 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
3000 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3001 vty_out (vty
, " exec-timeout %ld %ld%s",
3002 vty_timeout_val
/ 60,
3003 vty_timeout_val
% 60, VTY_NEWLINE
);
3006 if (no_password_check
)
3007 vty_out (vty
, " no login%s", VTY_NEWLINE
);
3009 if (do_log_commands
)
3010 vty_out (vty
, "log commands%s", VTY_NEWLINE
);
3012 vty_out (vty
, "!%s", VTY_NEWLINE
);
3017 struct cmd_node vty_node
=
3020 "%s(config-line)# ",
3024 /* Reset all VTY status. */
3030 struct thread
*vty_serv_thread
;
3032 for (i
= 0; i
< vector_active (vtyvec
); i
++)
3033 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
3035 buffer_reset (vty
->obuf
);
3036 vty
->status
= VTY_CLOSE
;
3040 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
3041 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
3043 thread_cancel (vty_serv_thread
);
3044 vector_slot (Vvty_serv_thread
, i
) = NULL
;
3048 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3050 if (vty_accesslist_name
)
3052 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3053 vty_accesslist_name
= NULL
;
3056 if (vty_ipv6_accesslist_name
)
3058 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3059 vty_ipv6_accesslist_name
= NULL
;
3066 char cwd
[MAXPATHLEN
];
3069 c
= getcwd (cwd
, MAXPATHLEN
);
3074 * At this point if these go wrong, more than likely
3075 * the whole world is coming down around us
3076 * Hence not worrying about it too much.
3078 if (!chdir (SYSCONFDIR
))
3080 fprintf(stderr
, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR
, errno
);
3083 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
3085 fprintf(stderr
, "Failure to getcwd, errno: %d\n", errno
);
3090 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
3091 strcpy (vty_cwd
, cwd
);
3101 vty_shell (struct vty
*vty
)
3103 return vty
->type
== VTY_SHELL
? 1 : 0;
3107 vty_shell_serv (struct vty
*vty
)
3109 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3115 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3118 /* Install vty's own commands like `who' command. */
3120 vty_init (struct thread_master
*master_thread
)
3122 /* For further configuration read, preserve current directory. */
3125 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3127 vty_master
= master_thread
;
3129 atexit (vty_stdio_reset
);
3131 /* Initilize server thread vector. */
3132 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
3134 /* Install bgp top node. */
3135 install_node (&vty_node
, vty_config_write
);
3137 install_element (VIEW_NODE
, &config_who_cmd
);
3138 install_element (VIEW_NODE
, &show_history_cmd
);
3139 install_element (CONFIG_NODE
, &line_vty_cmd
);
3140 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
3141 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3142 install_element (CONFIG_NODE
, &show_history_cmd
);
3143 install_element (CONFIG_NODE
, &log_commands_cmd
);
3144 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
3145 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
3146 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
3148 install_default (VTY_NODE
);
3149 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
3150 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
3151 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
3152 install_element (VTY_NODE
, &vty_access_class_cmd
);
3153 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
3154 install_element (VTY_NODE
, &vty_login_cmd
);
3155 install_element (VTY_NODE
, &no_vty_login_cmd
);
3157 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
3158 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3159 #endif /* HAVE_IPV6 */
3163 vty_terminate (void)
3166 XFREE (MTYPE_TMP
, vty_cwd
);
3168 if (vtyvec
&& Vvty_serv_thread
)
3171 vector_free (vtyvec
);
3172 vector_free (Vvty_serv_thread
);
3176 /* Utility functions to get arguments from commands generated
3177 by the xml2cli.pl script. */
3179 vty_get_arg_value (struct vty_arg
*args
[], const char *arg
)
3183 if (strcmp ((*args
)->name
, arg
) == 0)
3184 return (*args
)->value
;
3191 vty_get_arg (struct vty_arg
*args
[], const char *arg
)
3195 if (strcmp ((*args
)->name
, arg
) == 0)