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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <lib/version.h>
29 #include "sockunion.h"
39 #include <arpa/telnet.h>
42 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
43 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
44 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
59 static void vty_event(enum event
, int, struct vty
*);
61 /* Extern host structure from command.c */
62 extern struct host host
;
64 /* Vector which store each vty structure. */
67 /* Vty timeout value. */
68 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
70 /* Vty access-class command */
71 static char *vty_accesslist_name
= NULL
;
73 /* Vty access-calss for IPv6. */
74 static char *vty_ipv6_accesslist_name
= NULL
;
76 /* VTY server thread. */
77 static vector Vvty_serv_thread
;
79 /* Current directory. */
83 static int vty_config
;
84 static int vty_config_is_lockless
= 0;
86 /* Login password check. */
87 static int no_password_check
= 0;
89 /* Integrated configuration file path */
90 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
92 static int do_log_commands
= 0;
94 void vty_frame(struct vty
*vty
, const char *format
, ...)
98 va_start(args
, format
);
99 vsnprintf(vty
->frame
+ vty
->frame_pos
,
100 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
101 vty
->frame_pos
= strlen(vty
->frame
);
105 void vty_endframe(struct vty
*vty
, const char *endtext
)
107 if (vty
->frame_pos
== 0 && endtext
)
108 vty_out(vty
, "%s", endtext
);
112 /* VTY standard output function. */
113 int vty_out(struct vty
*vty
, const char *format
, ...)
121 if (vty
->frame_pos
) {
123 vty_out(vty
, "%s", vty
->frame
);
126 if (vty_shell(vty
)) {
127 va_start(args
, format
);
128 vprintf(format
, args
);
131 /* Try to write to initial buffer. */
132 va_start(args
, format
);
133 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
136 /* Initial buffer is not enough. */
137 if (len
< 0 || len
>= size
) {
144 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
148 va_start(args
, format
);
149 len
= vsnprintf(p
, size
, format
, args
);
152 if (len
> -1 && len
< size
)
157 /* When initial buffer is enough to store all output. */
161 /* Pointer p must point out buffer. */
162 if (vty
->type
!= VTY_TERM
)
163 buffer_put(vty
->obuf
, (uint8_t *)p
, len
);
165 buffer_put_crlf(vty
->obuf
, (uint8_t *)p
, len
);
167 /* If p is not different with buf, it is allocated buffer. */
169 XFREE(MTYPE_VTY_OUT_BUF
, p
);
175 static int vty_log_out(struct vty
*vty
, const char *level
,
176 const char *proto_str
, const char *format
,
177 struct timestamp_control
*ctl
, va_list va
)
183 if (!ctl
->already_rendered
) {
184 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
186 ctl
->already_rendered
= 1;
188 if (ctl
->len
+ 1 >= sizeof(buf
))
190 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
195 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
198 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
199 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
202 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
203 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
209 if (write(vty
->wfd
, buf
, len
) < 0) {
210 if (ERRNO_IO_RETRY(errno
))
211 /* Kernel buffer is full, probably too much debugging
213 drop the data and ignore. */
215 /* Fatal I/O error. */
217 0; /* disable monitoring to avoid infinite recursion */
218 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
219 __func__
, vty
->fd
, safe_strerror(errno
));
220 buffer_reset(vty
->obuf
);
221 /* cannot call vty_close, because a parent routine may still try
222 to access the vty struct */
223 vty
->status
= VTY_CLOSE
;
224 shutdown(vty
->fd
, SHUT_RDWR
);
230 /* Output current time to the vty. */
231 void vty_time_print(struct vty
*vty
, int cr
)
233 char buf
[QUAGGA_TIMESTAMP_LEN
];
235 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
236 zlog_info("quagga_timestamp error");
240 vty_out(vty
, "%s\n", buf
);
242 vty_out(vty
, "%s ", buf
);
247 /* Say hello to vty interface. */
248 void vty_hello(struct vty
*vty
)
254 f
= fopen(host
.motdfile
, "r");
256 while (fgets(buf
, sizeof(buf
), f
)) {
258 /* work backwards to ignore trailling isspace()
260 for (s
= buf
+ strlen(buf
);
261 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
264 vty_out(vty
, "%s\n", buf
);
268 vty_out(vty
, "MOTD file not found\n");
269 } else if (host
.motd
)
270 vty_out(vty
, "%s", host
.motd
);
273 /* Put out prompt and wait input from user. */
274 static void vty_prompt(struct vty
*vty
)
276 if (vty
->type
== VTY_TERM
) {
277 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
281 /* Send WILL TELOPT_ECHO to remote server. */
282 static void vty_will_echo(struct vty
*vty
)
284 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
285 vty_out(vty
, "%s", cmd
);
288 /* Make suppress Go-Ahead telnet option. */
289 static void vty_will_suppress_go_ahead(struct vty
*vty
)
291 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
292 vty_out(vty
, "%s", cmd
);
295 /* Make don't use linemode over telnet. */
296 static void 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. */
303 static void vty_do_window_size(struct vty
*vty
)
305 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
306 vty_out(vty
, "%s", cmd
);
309 #if 0 /* Currently not used. */
310 /* Make don't use lflow vty interface. */
312 vty_dont_lflow_ahead (struct vty
*vty
)
314 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
315 vty_out (vty
, "%s", cmd
);
319 /* Allocate new vty struct. */
320 struct vty
*vty_new()
322 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
324 new->fd
= new->wfd
= -1;
325 new->obuf
= buffer_new(0); /* Use default buffer size. */
326 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
327 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
328 new->max
= VTY_BUFSIZ
;
333 /* Authentication of vty */
334 static void vty_auth(struct vty
*vty
, char *buf
)
337 enum node_type next_node
= 0;
339 char *crypt(const char *, const char *);
344 passwd
= host
.password_encrypt
;
346 passwd
= host
.password
;
348 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
350 next_node
= VIEW_NODE
;
352 case AUTH_ENABLE_NODE
:
354 passwd
= host
.enable_encrypt
;
356 passwd
= host
.enable
;
357 next_node
= ENABLE_NODE
;
363 fail
= strcmp(crypt(buf
, passwd
), passwd
);
365 fail
= strcmp(buf
, passwd
);
371 vty
->node
= next_node
; /* Success ! */
374 if (vty
->fail
>= 3) {
375 if (vty
->node
== AUTH_NODE
) {
377 "%% Bad passwords, too many failures!\n");
378 vty
->status
= VTY_CLOSE
;
380 /* AUTH_ENABLE_NODE */
383 "%% Bad enable passwords, too many failures!\n");
384 vty
->status
= VTY_CLOSE
;
390 /* Command execution over the vty interface. */
391 static int vty_command(struct vty
*vty
, char *buf
)
395 const char *protocolname
;
399 * Log non empty command lines
404 /* Skip white spaces. */
405 while (isspace((int)*cp
) && *cp
!= '\0')
408 if (cp
!= NULL
&& *cp
!= '\0') {
410 char vty_str
[VTY_BUFSIZ
];
411 char prompt_str
[VTY_BUFSIZ
];
413 /* format the base vty info */
414 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
416 for (i
= 0; i
< vector_active(vtyvec
); i
++)
417 if (vty
== vector_slot(vtyvec
, i
)) {
418 snprintf(vty_str
, sizeof(vty_str
),
419 "vty[%d]@%s", i
, vty
->address
);
423 /* format the prompt */
424 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
427 /* now log the command */
428 zlog_err("%s%s", prompt_str
, buf
);
430 /* Split readline string up into the vector */
431 vline
= cmd_make_strvec(buf
);
436 #ifdef CONSUMED_TIME_CHECK
440 unsigned long realtime
, cputime
;
443 #endif /* CONSUMED_TIME_CHECK */
445 ret
= cmd_execute_command(vline
, vty
, NULL
, 0);
447 /* Get the name of the protocol if any */
448 protocolname
= frr_protoname
;
450 #ifdef CONSUMED_TIME_CHECK
452 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
453 > CONSUMED_TIME_CHECK
)
454 /* Warn about CPU hog that must be fixed. */
456 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
457 realtime
/ 1000, cputime
/ 1000, buf
);
459 #endif /* CONSUMED_TIME_CHECK */
461 if (ret
!= CMD_SUCCESS
)
464 if (vty
->type
== VTY_FILE
)
465 vty_out(vty
, "Warning...\n");
467 case CMD_ERR_AMBIGUOUS
:
468 vty_out(vty
, "%% Ambiguous command.\n");
470 case CMD_ERR_NO_MATCH
:
471 vty_out(vty
, "%% [%s] Unknown command: %s\n",
474 case CMD_ERR_INCOMPLETE
:
475 vty_out(vty
, "%% Command incomplete.\n");
478 cmd_free_strvec(vline
);
483 static const char telnet_backward_char
= 0x08;
484 static const char telnet_space_char
= ' ';
486 /* Basic function to write buffer to vty. */
487 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
489 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
492 /* Should we do buffering here ? And make vty_flush (vty) ? */
493 buffer_put(vty
->obuf
, buf
, nbytes
);
496 /* Basic function to insert character into vty. */
497 static void vty_self_insert(struct vty
*vty
, char c
)
502 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
505 length
= vty
->length
- vty
->cp
;
506 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
507 vty
->buf
[vty
->cp
] = c
;
509 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
510 for (i
= 0; i
< length
; i
++)
511 vty_write(vty
, &telnet_backward_char
, 1);
516 vty
->buf
[vty
->length
] = '\0';
519 /* Self insert character 'c' in overwrite mode. */
520 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
522 if (vty
->cp
== vty
->length
) {
523 vty_self_insert(vty
, c
);
527 vty
->buf
[vty
->cp
++] = c
;
528 vty_write(vty
, &c
, 1);
532 * Insert a string into vty->buf at the current cursor position.
534 * If the resultant string would be larger than VTY_BUFSIZ it is
537 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
539 if (vty
->cp
== VTY_BUFSIZ
)
542 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
543 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
545 vty
->length
= MAX(vty
->cp
, vty
->length
);
546 vty
->buf
[vty
->length
] = '\0';
547 vty_write(vty
, str
, nwrite
);
550 /* Forward character. */
551 static void vty_forward_char(struct vty
*vty
)
553 if (vty
->cp
< vty
->length
) {
554 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
559 /* Backward character. */
560 static void vty_backward_char(struct vty
*vty
)
564 vty_write(vty
, &telnet_backward_char
, 1);
568 /* Move to the beginning of the line. */
569 static void vty_beginning_of_line(struct vty
*vty
)
572 vty_backward_char(vty
);
575 /* Move to the end of the line. */
576 static void vty_end_of_line(struct vty
*vty
)
578 while (vty
->cp
< vty
->length
)
579 vty_forward_char(vty
);
582 static void vty_kill_line_from_beginning(struct vty
*);
583 static void vty_redraw_line(struct vty
*);
585 /* Print command line history. This function is called from
586 vty_next_line and vty_previous_line. */
587 static void vty_history_print(struct vty
*vty
)
591 vty_kill_line_from_beginning(vty
);
593 /* Get previous line from history buffer */
594 length
= strlen(vty
->hist
[vty
->hp
]);
595 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
596 vty
->cp
= vty
->length
= length
;
597 vty
->buf
[vty
->length
] = '\0';
599 /* Redraw current line */
600 vty_redraw_line(vty
);
603 /* Show next command line history. */
604 static void vty_next_line(struct vty
*vty
)
608 if (vty
->hp
== vty
->hindex
)
611 /* Try is there history exist or not. */
613 if (try_index
== (VTY_MAXHIST
- 1))
618 /* If there is not history return. */
619 if (vty
->hist
[try_index
] == NULL
)
624 vty_history_print(vty
);
627 /* Show previous command line history. */
628 static void vty_previous_line(struct vty
*vty
)
634 try_index
= VTY_MAXHIST
- 1;
638 if (vty
->hist
[try_index
] == NULL
)
643 vty_history_print(vty
);
646 /* This function redraw all of the command line character. */
647 static void vty_redraw_line(struct vty
*vty
)
649 vty_write(vty
, vty
->buf
, vty
->length
);
650 vty
->cp
= vty
->length
;
654 static void vty_forward_word(struct vty
*vty
)
656 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
657 vty_forward_char(vty
);
659 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
660 vty_forward_char(vty
);
663 /* Backward word without skipping training space. */
664 static void vty_backward_pure_word(struct vty
*vty
)
666 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
667 vty_backward_char(vty
);
671 static void vty_backward_word(struct vty
*vty
)
673 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
674 vty_backward_char(vty
);
676 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
677 vty_backward_char(vty
);
680 /* When '^D' is typed at the beginning of the line we move to the down
682 static void vty_down_level(struct vty
*vty
)
690 /* When '^Z' is received from vty, move down to the enable mode. */
691 static void vty_end_config(struct vty
*vty
)
710 case BGP_VRF_POLICY_NODE
:
711 case BGP_VNC_DEFAULTS_NODE
:
712 case BGP_VNC_NVE_GROUP_NODE
:
713 case BGP_VNC_L2_GROUP_NODE
:
728 case LDP_IPV4_IFACE_NODE
:
729 case LDP_IPV6_IFACE_NODE
:
731 case LDP_PSEUDOWIRE_NODE
:
734 case KEYCHAIN_KEY_NODE
:
736 case BGP_EVPN_VNI_NODE
:
737 vty_config_unlock(vty
);
738 vty
->node
= ENABLE_NODE
;
741 /* Unknown node, we have to ignore it. */
749 /* Delete a charcter at the current point. */
750 static void vty_delete_char(struct vty
*vty
)
755 if (vty
->length
== 0) {
760 if (vty
->cp
== vty
->length
)
761 return; /* completion need here? */
763 size
= vty
->length
- vty
->cp
;
766 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
767 vty
->buf
[vty
->length
] = '\0';
769 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
772 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
773 vty_write(vty
, &telnet_space_char
, 1);
775 for (i
= 0; i
< size
; i
++)
776 vty_write(vty
, &telnet_backward_char
, 1);
779 /* Delete a character before the point. */
780 static void vty_delete_backward_char(struct vty
*vty
)
785 vty_backward_char(vty
);
786 vty_delete_char(vty
);
789 /* Kill rest of line from current point. */
790 static void vty_kill_line(struct vty
*vty
)
795 size
= vty
->length
- vty
->cp
;
800 for (i
= 0; i
< size
; i
++)
801 vty_write(vty
, &telnet_space_char
, 1);
802 for (i
= 0; i
< size
; i
++)
803 vty_write(vty
, &telnet_backward_char
, 1);
805 memset(&vty
->buf
[vty
->cp
], 0, size
);
806 vty
->length
= vty
->cp
;
809 /* Kill line from the beginning. */
810 static void vty_kill_line_from_beginning(struct vty
*vty
)
812 vty_beginning_of_line(vty
);
816 /* Delete a word before the point. */
817 static void vty_forward_kill_word(struct vty
*vty
)
819 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
820 vty_delete_char(vty
);
821 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
822 vty_delete_char(vty
);
825 /* Delete a word before the point. */
826 static void vty_backward_kill_word(struct vty
*vty
)
828 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
829 vty_delete_backward_char(vty
);
830 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
831 vty_delete_backward_char(vty
);
834 /* Transpose chars before or at the point. */
835 static void vty_transpose_chars(struct vty
*vty
)
839 /* If length is short or point is near by the beginning of line then
841 if (vty
->length
< 2 || vty
->cp
< 1)
844 /* In case of point is located at the end of the line. */
845 if (vty
->cp
== vty
->length
) {
846 c1
= vty
->buf
[vty
->cp
- 1];
847 c2
= vty
->buf
[vty
->cp
- 2];
849 vty_backward_char(vty
);
850 vty_backward_char(vty
);
851 vty_self_insert_overwrite(vty
, c1
);
852 vty_self_insert_overwrite(vty
, c2
);
854 c1
= vty
->buf
[vty
->cp
];
855 c2
= vty
->buf
[vty
->cp
- 1];
857 vty_backward_char(vty
);
858 vty_self_insert_overwrite(vty
, c1
);
859 vty_self_insert_overwrite(vty
, c2
);
863 /* Do completion at vty interface. */
864 static void vty_complete_command(struct vty
*vty
)
868 char **matched
= NULL
;
871 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
874 vline
= cmd_make_strvec(vty
->buf
);
878 /* In case of 'help \t'. */
879 if (isspace((int)vty
->buf
[vty
->length
- 1]))
880 vector_set(vline
, NULL
);
882 matched
= cmd_complete_command(vline
, vty
, &ret
);
884 cmd_free_strvec(vline
);
888 case CMD_ERR_AMBIGUOUS
:
889 vty_out(vty
, "%% Ambiguous command.\n");
891 vty_redraw_line(vty
);
893 case CMD_ERR_NO_MATCH
:
894 /* vty_out (vty, "%% There is no matched command.\n"); */
896 vty_redraw_line(vty
);
898 case CMD_COMPLETE_FULL_MATCH
:
900 /* 2016-11-28 equinox -- need to debug, SEGV here */
901 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
903 vty_redraw_line(vty
);
907 vty_redraw_line(vty
);
908 vty_backward_pure_word(vty
);
909 vty_insert_word_overwrite(vty
, matched
[0]);
910 vty_self_insert(vty
, ' ');
911 XFREE(MTYPE_COMPLETION
, matched
[0]);
913 case CMD_COMPLETE_MATCH
:
915 vty_redraw_line(vty
);
916 vty_backward_pure_word(vty
);
917 vty_insert_word_overwrite(vty
, matched
[0]);
918 XFREE(MTYPE_COMPLETION
, matched
[0]);
920 case CMD_COMPLETE_LIST_MATCH
:
921 for (i
= 0; matched
[i
] != NULL
; i
++) {
922 if (i
!= 0 && ((i
% 6) == 0))
924 vty_out(vty
, "%-10s ", matched
[i
]);
925 XFREE(MTYPE_COMPLETION
, matched
[i
]);
930 vty_redraw_line(vty
);
932 case CMD_ERR_NOTHING_TODO
:
934 vty_redraw_line(vty
);
940 XFREE(MTYPE_TMP
, matched
);
943 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
944 unsigned int desc_width
, struct cmd_token
*token
)
952 if (desc_width
<= 0) {
953 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
957 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
959 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
960 for (pos
= desc_width
; pos
> 0; pos
--)
961 if (*(p
+ pos
) == ' ')
967 strncpy(buf
, p
, pos
);
969 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
974 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
976 XFREE(MTYPE_TMP
, buf
);
979 /* Describe matched command function. */
980 static void vty_describe_command(struct vty
*vty
)
985 unsigned int i
, width
, desc_width
;
986 struct cmd_token
*token
, *token_cr
= NULL
;
988 vline
= cmd_make_strvec(vty
->buf
);
990 /* In case of '> ?'. */
992 vline
= vector_init(1);
993 vector_set(vline
, NULL
);
994 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
995 vector_set(vline
, NULL
);
997 describe
= cmd_describe_command(vline
, vty
, &ret
);
1001 /* Ambiguous error. */
1003 case CMD_ERR_AMBIGUOUS
:
1004 vty_out(vty
, "%% Ambiguous command.\n");
1007 case CMD_ERR_NO_MATCH
:
1008 vty_out(vty
, "%% There is no matched command.\n");
1013 /* Get width of command string. */
1015 for (i
= 0; i
< vector_active(describe
); i
++)
1016 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1019 if (token
->text
[0] == '\0')
1022 len
= strlen(token
->text
);
1028 /* Get width of description string. */
1029 desc_width
= vty
->width
- (width
+ 6);
1031 /* Print out description. */
1032 for (i
= 0; i
< vector_active(describe
); i
++)
1033 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1034 if (token
->text
[0] == '\0')
1037 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1043 vty_out(vty
, " %-s\n", token
->text
);
1044 else if (desc_width
>= strlen(token
->desc
))
1045 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1048 vty_describe_fold(vty
, width
, desc_width
,
1051 if (IS_VARYING_TOKEN(token
->type
)) {
1052 const char *ref
= vector_slot(
1053 vline
, vector_active(vline
) - 1);
1055 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1056 cmd_variable_complete(token
, ref
, varcomps
);
1058 if (vector_active(varcomps
) > 0) {
1059 char *ac
= cmd_variable_comp2str(
1060 varcomps
, vty
->width
);
1061 vty_out(vty
, "%s\n", ac
);
1062 XFREE(MTYPE_TMP
, ac
);
1065 vector_free(varcomps
);
1068 vty_out (vty
, " %-*s %s\n", width
1069 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1070 desc
->str
? desc
->str
: "");
1074 if ((token
= token_cr
)) {
1076 vty_out(vty
, " %-s\n", token
->text
);
1077 else if (desc_width
>= strlen(token
->desc
))
1078 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1081 vty_describe_fold(vty
, width
, desc_width
, token
);
1085 cmd_free_strvec(vline
);
1087 vector_free(describe
);
1090 vty_redraw_line(vty
);
1093 static void vty_clear_buf(struct vty
*vty
)
1095 memset(vty
->buf
, 0, vty
->max
);
1098 /* ^C stop current input and do not add command line to the history. */
1099 static void vty_stop_input(struct vty
*vty
)
1101 vty
->cp
= vty
->length
= 0;
1105 switch (vty
->node
) {
1108 /* Nothing to do. */
1111 case INTERFACE_NODE
:
1125 case LDP_IPV4_IFACE_NODE
:
1126 case LDP_IPV6_IFACE_NODE
:
1127 case LDP_L2VPN_NODE
:
1128 case LDP_PSEUDOWIRE_NODE
:
1131 case KEYCHAIN_KEY_NODE
:
1133 vty_config_unlock(vty
);
1134 vty
->node
= ENABLE_NODE
;
1137 /* Unknown node, we have to ignore it. */
1142 /* Set history pointer to the latest one. */
1143 vty
->hp
= vty
->hindex
;
1146 /* Add current command line to the history buffer. */
1147 static void vty_hist_add(struct vty
*vty
)
1151 if (vty
->length
== 0)
1154 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1156 /* Ignore the same string as previous one. */
1157 if (vty
->hist
[index
])
1158 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1159 vty
->hp
= vty
->hindex
;
1163 /* Insert history entry. */
1164 if (vty
->hist
[vty
->hindex
])
1165 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1166 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1168 /* History index rotation. */
1170 if (vty
->hindex
== VTY_MAXHIST
)
1173 vty
->hp
= vty
->hindex
;
1176 /* #define TELNET_OPTION_DEBUG */
1178 /* Get telnet window size. */
1179 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1181 #ifdef TELNET_OPTION_DEBUG
1184 for (i
= 0; i
< nbytes
; i
++) {
1187 vty_out(vty
, "IAC ");
1190 vty_out(vty
, "WILL ");
1193 vty_out(vty
, "WONT ");
1196 vty_out(vty
, "DO ");
1199 vty_out(vty
, "DONT ");
1202 vty_out(vty
, "SB ");
1205 vty_out(vty
, "SE ");
1208 vty_out(vty
, "TELOPT_ECHO \n");
1211 vty_out(vty
, "TELOPT_SGA \n");
1214 vty_out(vty
, "TELOPT_NAWS \n");
1217 vty_out(vty
, "%x ", buf
[i
]);
1223 #endif /* TELNET_OPTION_DEBUG */
1228 vty
->iac_sb_in_progress
= 1;
1232 if (!vty
->iac_sb_in_progress
)
1235 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1236 vty
->iac_sb_in_progress
= 0;
1239 switch (vty
->sb_buf
[0]) {
1241 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1243 "RFC 1073 violation detected: telnet NAWS option "
1244 "should send %d characters, but we received %lu",
1246 (unsigned long)vty
->sb_len
);
1247 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1249 "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1250 "too small to handle the telnet NAWS option",
1251 (unsigned long)sizeof(vty
->sb_buf
),
1252 TELNET_NAWS_SB_LEN
);
1254 vty
->width
= ((vty
->sb_buf
[1] << 8)
1256 vty
->height
= ((vty
->sb_buf
[3] << 8)
1258 #ifdef TELNET_OPTION_DEBUG
1260 "TELNET NAWS window size negotiation completed: "
1261 "width %d, height %d\n",
1262 vty
->width
, vty
->height
);
1267 vty
->iac_sb_in_progress
= 0;
1277 /* Execute current command line. */
1278 static int vty_execute(struct vty
*vty
)
1284 switch (vty
->node
) {
1286 case AUTH_ENABLE_NODE
:
1287 vty_auth(vty
, vty
->buf
);
1290 ret
= vty_command(vty
, vty
->buf
);
1291 if (vty
->type
== VTY_TERM
)
1296 /* Clear command line buffer. */
1297 vty
->cp
= vty
->length
= 0;
1300 if (vty
->status
!= VTY_CLOSE
)
1306 #define CONTROL(X) ((X) - '@')
1307 #define VTY_NORMAL 0
1308 #define VTY_PRE_ESCAPE 1
1309 #define VTY_ESCAPE 2
1311 /* Escape character command map. */
1312 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1316 vty_previous_line(vty
);
1322 vty_forward_char(vty
);
1325 vty_backward_char(vty
);
1331 /* Go back to normal mode. */
1332 vty
->escape
= VTY_NORMAL
;
1335 /* Quit print out to the buffer. */
1336 static void vty_buffer_reset(struct vty
*vty
)
1338 buffer_reset(vty
->obuf
);
1340 vty_redraw_line(vty
);
1343 /* Read data via vty socket. */
1344 static int vty_read(struct thread
*thread
)
1348 unsigned char buf
[VTY_READ_BUFSIZ
];
1350 int vty_sock
= THREAD_FD(thread
);
1351 struct vty
*vty
= THREAD_ARG(thread
);
1354 /* Read raw data from socket */
1355 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1357 if (ERRNO_IO_RETRY(errno
)) {
1358 vty_event(VTY_READ
, vty_sock
, vty
);
1361 vty
->monitor
= 0; /* disable monitoring to avoid
1362 infinite recursion */
1364 "%s: read error on vty client fd %d, closing: %s",
1365 __func__
, vty
->fd
, safe_strerror(errno
));
1366 buffer_reset(vty
->obuf
);
1368 vty
->status
= VTY_CLOSE
;
1371 for (i
= 0; i
< nbytes
; i
++) {
1372 if (buf
[i
] == IAC
) {
1381 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1382 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1383 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1389 /* In case of telnet command */
1391 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1398 if (vty
->status
== VTY_MORE
) {
1403 vty_buffer_reset(vty
);
1405 #if 0 /* More line does not work for "show ip bgp". */
1408 vty
->status
= VTY_MORELINE
;
1417 /* Escape character. */
1418 if (vty
->escape
== VTY_ESCAPE
) {
1419 vty_escape_map(buf
[i
], vty
);
1423 /* Pre-escape status. */
1424 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1427 vty
->escape
= VTY_ESCAPE
;
1430 vty_backward_word(vty
);
1431 vty
->escape
= VTY_NORMAL
;
1434 vty_forward_word(vty
);
1435 vty
->escape
= VTY_NORMAL
;
1438 vty_forward_kill_word(vty
);
1439 vty
->escape
= VTY_NORMAL
;
1443 vty_backward_kill_word(vty
);
1444 vty
->escape
= VTY_NORMAL
;
1447 vty
->escape
= VTY_NORMAL
;
1455 vty_beginning_of_line(vty
);
1458 vty_backward_char(vty
);
1461 vty_stop_input(vty
);
1464 vty_delete_char(vty
);
1467 vty_end_of_line(vty
);
1470 vty_forward_char(vty
);
1474 vty_delete_backward_char(vty
);
1483 vty_previous_line(vty
);
1486 vty_transpose_chars(vty
);
1489 vty_kill_line_from_beginning(vty
);
1492 vty_backward_kill_word(vty
);
1495 vty_end_config(vty
);
1503 vty_complete_command(vty
);
1506 if (vty
->node
== AUTH_NODE
1507 || vty
->node
== AUTH_ENABLE_NODE
)
1508 vty_self_insert(vty
, buf
[i
]);
1510 vty_describe_command(vty
);
1513 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1514 vty
->escape
= VTY_ESCAPE
;
1517 vty
->escape
= VTY_PRE_ESCAPE
;
1520 if (buf
[i
] > 31 && buf
[i
] < 127)
1521 vty_self_insert(vty
, buf
[i
]);
1527 if (vty
->status
== VTY_CLOSE
)
1530 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1531 vty_event(VTY_READ
, vty_sock
, vty
);
1536 /* Flush buffer to the vty. */
1537 static int vty_flush(struct thread
*thread
)
1540 buffer_status_t flushrc
;
1541 int vty_sock
= THREAD_FD(thread
);
1542 struct vty
*vty
= THREAD_ARG(thread
);
1544 vty
->t_write
= NULL
;
1546 /* Tempolary disable read thread. */
1547 if ((vty
->lines
== 0) && vty
->t_read
) {
1548 thread_cancel(vty
->t_read
);
1552 /* Function execution continue. */
1553 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1555 /* N.B. if width is 0, that means we don't know the window size. */
1556 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1557 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1558 else if (vty
->status
== VTY_MORELINE
)
1559 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1562 flushrc
= buffer_flush_window(
1563 vty
->obuf
, vty_sock
, vty
->width
,
1564 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1568 0; /* disable monitoring to avoid infinite recursion */
1569 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1571 buffer_reset(vty
->obuf
);
1575 if (vty
->status
== VTY_CLOSE
)
1578 vty
->status
= VTY_NORMAL
;
1579 if (vty
->lines
== 0)
1580 vty_event(VTY_READ
, vty_sock
, vty
);
1583 case BUFFER_PENDING
:
1584 /* There is more data waiting to be written. */
1585 vty
->status
= VTY_MORE
;
1586 if (vty
->lines
== 0)
1587 vty_event(VTY_WRITE
, vty_sock
, vty
);
1594 /* allocate and initialise vty */
1595 static struct vty
*vty_new_init(int vty_sock
)
1601 vty
->wfd
= vty_sock
;
1602 vty
->type
= VTY_TERM
;
1603 vty
->node
= AUTH_NODE
;
1608 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1611 vector_set_index(vtyvec
, vty_sock
, vty
);
1612 vty
->status
= VTY_NORMAL
;
1615 vty
->iac_sb_in_progress
= 0;
1621 /* Create new vty structure. */
1622 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1624 char buf
[SU_ADDRSTRLEN
];
1627 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1629 /* Allocate new vty structure and set up default values. */
1630 vty
= vty_new_init(vty_sock
);
1632 /* configurable parameters not part of basic init */
1633 vty
->v_timeout
= vty_timeout_val
;
1634 strcpy(vty
->address
, buf
);
1635 if (no_password_check
) {
1637 vty
->node
= ENABLE_NODE
;
1639 vty
->node
= VIEW_NODE
;
1641 if (host
.lines
>= 0)
1642 vty
->lines
= host
.lines
;
1644 if (!no_password_check
) {
1645 /* Vty is not available if password isn't set. */
1646 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1647 vty_out(vty
, "Vty password is not set.\n");
1648 vty
->status
= VTY_CLOSE
;
1654 /* Say hello to the world. */
1656 if (!no_password_check
)
1657 vty_out(vty
, "\nUser Access Verification\n\n");
1659 /* Setting up terminal. */
1661 vty_will_suppress_go_ahead(vty
);
1663 vty_dont_linemode(vty
);
1664 vty_do_window_size(vty
);
1665 /* vty_dont_lflow_ahead (vty); */
1669 /* Add read/write thread. */
1670 vty_event(VTY_WRITE
, vty_sock
, vty
);
1671 vty_event(VTY_READ
, vty_sock
, vty
);
1676 /* create vty for stdio */
1677 static struct termios stdio_orig_termios
;
1678 static struct vty
*stdio_vty
= NULL
;
1679 static bool stdio_termios
= false;
1680 static void (*stdio_vty_atclose
)(int isexit
);
1682 static void vty_stdio_reset(int isexit
)
1686 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1687 stdio_termios
= false;
1691 if (stdio_vty_atclose
)
1692 stdio_vty_atclose(isexit
);
1693 stdio_vty_atclose
= NULL
;
1697 static void vty_stdio_atexit(void)
1702 void vty_stdio_suspend(void)
1707 if (stdio_vty
->t_write
)
1708 thread_cancel(stdio_vty
->t_write
);
1709 if (stdio_vty
->t_read
)
1710 thread_cancel(stdio_vty
->t_read
);
1711 if (stdio_vty
->t_timeout
)
1712 thread_cancel(stdio_vty
->t_timeout
);
1715 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1716 stdio_termios
= false;
1719 void vty_stdio_resume(void)
1724 if (!tcgetattr(0, &stdio_orig_termios
)) {
1725 struct termios termios
;
1727 termios
= stdio_orig_termios
;
1728 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1729 | IGNCR
| ICRNL
| IXON
);
1730 termios
.c_oflag
&= ~OPOST
;
1731 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1732 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1733 termios
.c_cflag
|= CS8
;
1734 tcsetattr(0, TCSANOW
, &termios
);
1735 stdio_termios
= true;
1738 vty_prompt(stdio_vty
);
1740 /* Add read/write thread. */
1741 vty_event(VTY_WRITE
, 1, stdio_vty
);
1742 vty_event(VTY_READ
, 0, stdio_vty
);
1745 void vty_stdio_close(void)
1749 vty_close(stdio_vty
);
1752 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1756 /* refuse creating two vtys on stdio */
1760 vty
= stdio_vty
= vty_new_init(0);
1761 stdio_vty_atclose
= atclose
;
1764 /* always have stdio vty in a known _unchangeable_ state, don't want
1766 * to have any effect here to make sure scripting this works as intended
1768 vty
->node
= ENABLE_NODE
;
1770 strcpy(vty
->address
, "console");
1776 /* Accept connection from the network. */
1777 static int vty_accept(struct thread
*thread
)
1785 struct access_list
*acl
= NULL
;
1786 char buf
[SU_ADDRSTRLEN
];
1788 accept_sock
= THREAD_FD(thread
);
1790 /* We continue hearing vty socket. */
1791 vty_event(VTY_SERV
, accept_sock
, NULL
);
1793 memset(&su
, 0, sizeof(union sockunion
));
1795 /* We can handle IPv4 or IPv6 socket. */
1796 vty_sock
= sockunion_accept(accept_sock
, &su
);
1798 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1801 set_nonblocking(vty_sock
);
1802 set_cloexec(vty_sock
);
1804 sockunion2hostprefix(&su
, &p
);
1806 /* VTY's accesslist apply. */
1807 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1808 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1809 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1810 zlog_info("Vty connection refused from %s",
1811 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1814 /* continue accepting connections */
1815 vty_event(VTY_SERV
, accept_sock
, NULL
);
1821 /* VTY's ipv6 accesslist apply. */
1822 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1823 if ((acl
= access_list_lookup(AFI_IP6
,
1824 vty_ipv6_accesslist_name
))
1825 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1826 zlog_info("Vty connection refused from %s",
1827 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1830 /* continue accepting connections */
1831 vty_event(VTY_SERV
, accept_sock
, NULL
);
1838 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1841 zlog_info("can't set sockopt to vty_sock : %s",
1842 safe_strerror(errno
));
1844 zlog_info("Vty connection from %s",
1845 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1847 vty_create(vty_sock
, &su
);
1852 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1855 struct addrinfo req
;
1856 struct addrinfo
*ainfo
;
1857 struct addrinfo
*ainfo_save
;
1859 char port_str
[BUFSIZ
];
1861 memset(&req
, 0, sizeof(struct addrinfo
));
1862 req
.ai_flags
= AI_PASSIVE
;
1863 req
.ai_family
= AF_UNSPEC
;
1864 req
.ai_socktype
= SOCK_STREAM
;
1865 sprintf(port_str
, "%d", port
);
1866 port_str
[sizeof(port_str
) - 1] = '\0';
1868 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1871 zlog_err("getaddrinfo failed: %s", gai_strerror(ret
));
1878 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1881 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1882 ainfo
->ai_protocol
);
1886 sockopt_v6only(ainfo
->ai_family
, sock
);
1887 sockopt_reuseaddr(sock
);
1888 sockopt_reuseport(sock
);
1891 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1893 close(sock
); /* Avoid sd leak. */
1897 ret
= listen(sock
, 3);
1899 close(sock
); /* Avoid sd leak. */
1903 vty_event(VTY_SERV
, sock
, NULL
);
1904 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1906 freeaddrinfo(ainfo_save
);
1910 /* For sockaddr_un. */
1913 /* VTY shell UNIX domain socket. */
1914 static void vty_serv_un(const char *path
)
1918 struct sockaddr_un serv
;
1920 struct zprivs_ids_t ids
;
1922 /* First of all, unlink existing socket */
1926 old_mask
= umask(0007);
1928 /* Make UNIX domain socket. */
1929 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1931 zlog_err("Cannot create unix stream socket: %s",
1932 safe_strerror(errno
));
1936 /* Make server socket. */
1937 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1938 serv
.sun_family
= AF_UNIX
;
1939 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1940 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1941 len
= serv
.sun_len
= SUN_LEN(&serv
);
1943 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1944 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1948 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1950 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1951 close(sock
); /* Avoid sd leak. */
1955 ret
= listen(sock
, 5);
1957 zlog_err("listen(fd %d) failed: %s", sock
,
1958 safe_strerror(errno
));
1959 close(sock
); /* Avoid sd leak. */
1965 zprivs_get_ids(&ids
);
1967 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1968 earlier for the case when we don't need to chown the file
1969 type casting it here to make a compare */
1970 if ((int)ids
.gid_vty
> 0) {
1971 /* set group of socket */
1972 if (chown(path
, -1, ids
.gid_vty
)) {
1973 zlog_err("vty_serv_un: could chown socket, %s",
1974 safe_strerror(errno
));
1978 vty_event(VTYSH_SERV
, sock
, NULL
);
1981 /* #define VTYSH_DEBUG 1 */
1983 static int vtysh_accept(struct thread
*thread
)
1988 struct sockaddr_un client
;
1991 accept_sock
= THREAD_FD(thread
);
1993 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
1995 memset(&client
, 0, sizeof(struct sockaddr_un
));
1996 client_len
= sizeof(struct sockaddr_un
);
1998 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
1999 (socklen_t
*)&client_len
);
2002 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2006 if (set_nonblocking(sock
) < 0) {
2008 "vtysh_accept: could not set vty socket %d to non-blocking,"
2010 sock
, safe_strerror(errno
));
2017 printf("VTY shell accept\n");
2018 #endif /* VTYSH_DEBUG */
2023 vty
->type
= VTY_SHELL_SERV
;
2024 vty
->node
= VIEW_NODE
;
2026 vty_event(VTYSH_READ
, sock
, vty
);
2031 static int vtysh_flush(struct vty
*vty
)
2033 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2034 case BUFFER_PENDING
:
2035 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2039 0; /* disable monitoring to avoid infinite recursion */
2040 zlog_warn("%s: write error to fd %d, closing", __func__
,
2042 buffer_reset(vty
->obuf
);
2052 static int vtysh_read(struct thread
*thread
)
2058 unsigned char buf
[VTY_READ_BUFSIZ
];
2060 uint8_t header
[4] = {0, 0, 0, 0};
2062 sock
= THREAD_FD(thread
);
2063 vty
= THREAD_ARG(thread
);
2066 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2068 if (ERRNO_IO_RETRY(errno
)) {
2069 vty_event(VTYSH_READ
, sock
, vty
);
2072 vty
->monitor
= 0; /* disable monitoring to avoid
2073 infinite recursion */
2075 "%s: read failed on vtysh client fd %d, closing: %s",
2076 __func__
, sock
, safe_strerror(errno
));
2078 buffer_reset(vty
->obuf
);
2081 printf("close vtysh\n");
2082 #endif /* VTYSH_DEBUG */
2087 printf("line: %.*s\n", nbytes
, buf
);
2088 #endif /* VTYSH_DEBUG */
2090 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2091 /* Clear command line buffer. */
2092 vty
->cp
= vty
->length
= 0;
2094 vty_out(vty
, "%% Command is too long.\n");
2096 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2097 vty
->buf
[vty
->length
++] = *p
;
2099 /* Pass this line to parser. */
2100 ret
= vty_execute(vty
);
2101 /* Note that vty_execute clears the command buffer and resets
2102 vty->length to 0. */
2104 /* Return result. */
2106 printf("result: %d\n", ret
);
2107 printf("vtysh node: %d\n", vty
->node
);
2108 #endif /* VTYSH_DEBUG */
2110 /* hack for asynchronous "write integrated"
2111 * - other commands in "buf" will be ditched
2112 * - input during pending config-write is
2114 if (ret
== CMD_SUSPEND
)
2117 /* warning: watchfrr hardcodes this result write
2120 buffer_put(vty
->obuf
, header
, 4);
2122 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2123 /* Try to flush results; exit if a write
2130 if (vty
->status
== VTY_CLOSE
)
2133 vty_event(VTYSH_READ
, sock
, vty
);
2138 static int vtysh_write(struct thread
*thread
)
2140 struct vty
*vty
= THREAD_ARG(thread
);
2142 vty
->t_write
= NULL
;
2149 /* Determine address family to bind. */
2150 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2152 /* If port is set to 0, do not listen on TCP/IP at all! */
2154 vty_serv_sock_addrinfo(addr
, port
);
2161 /* Close vty interface. Warning: call this only from functions that
2162 will be careful not to access the vty afterwards (since it has
2163 now been freed). This is safest from top-level functions (called
2164 directly by the thread dispatcher). */
2165 void vty_close(struct vty
*vty
)
2168 bool was_stdio
= false;
2170 /* Cancel threads.*/
2172 thread_cancel(vty
->t_read
);
2174 thread_cancel(vty
->t_write
);
2176 thread_cancel(vty
->t_timeout
);
2179 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2181 /* Free input buffer. */
2182 buffer_free(vty
->obuf
);
2184 /* Free command history. */
2185 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2187 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2191 vector_unset(vtyvec
, vty
->fd
);
2193 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2197 * note check is for fd > STDERR_FILENO, not fd != -1.
2198 * We never close stdin/stdout/stderr here, because we may be
2199 * running in foreground mode with logging to stdout. Also,
2200 * additionally, we'd need to replace these fds with /dev/null. */
2201 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2203 if (vty
->fd
> STDERR_FILENO
) {
2209 XFREE(MTYPE_VTY
, vty
->buf
);
2212 XFREE(MTYPE_VTY
, vty
->error_buf
);
2214 /* Check configure. */
2215 vty_config_unlock(vty
);
2218 XFREE(MTYPE_VTY
, vty
);
2224 /* When time out occur output message then close connection. */
2225 static int vty_timeout(struct thread
*thread
)
2229 vty
= THREAD_ARG(thread
);
2230 vty
->t_timeout
= NULL
;
2234 buffer_reset(vty
->obuf
);
2235 vty_out(vty
, "\nVty connection is timed out.\n");
2237 /* Close connection. */
2238 vty
->status
= VTY_CLOSE
;
2244 /* Read up configuration file from file_name. */
2245 static void vty_read_file(FILE *confp
)
2249 unsigned int line_num
= 0;
2252 /* vty_close won't close stderr; if some config command prints
2253 * something it'll end up there. (not ideal; it'd be beter if output
2254 * from a file-load went to logging instead. Also note that if this
2255 * function is called after daemonizing, stderr will be /dev/null.)
2257 * vty->fd will be -1 from vty_new()
2259 vty
->wfd
= STDERR_FILENO
;
2260 vty
->type
= VTY_FILE
;
2261 vty
->node
= CONFIG_NODE
;
2263 /* Execute configuration file */
2264 ret
= config_from_file(vty
, confp
, &line_num
);
2266 /* Flush any previous errors before printing messages below */
2267 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2269 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2270 const char *message
= NULL
;
2274 case CMD_ERR_AMBIGUOUS
:
2275 message
= "Ambiguous command";
2277 case CMD_ERR_NO_MATCH
:
2278 message
= "No such command";
2281 message
= "Command returned Warning";
2283 case CMD_WARNING_CONFIG_FAILED
:
2284 message
= "Command returned Warning Config Failed";
2286 case CMD_ERR_INCOMPLETE
:
2287 message
= "Command returned Incomplete";
2289 case CMD_ERR_EXEED_ARGC_MAX
:
2291 "Command exceeded maximum number of Arguments";
2294 message
= "Command returned unhandled error message";
2298 nl
= strchr(vty
->error_buf
, '\n');
2301 zlog_err("ERROR: %s on config line %u: %s", message
, line_num
,
2308 static FILE *vty_use_backup_config(const char *fullpath
)
2310 char *fullpath_sav
, *fullpath_tmp
;
2316 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2317 strcpy(fullpath_sav
, fullpath
);
2318 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2320 sav
= open(fullpath_sav
, O_RDONLY
);
2326 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2327 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2329 /* Open file to configuration write. */
2330 tmp
= mkstemp(fullpath_tmp
);
2334 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2337 while ((c
= read(sav
, buffer
, 512)) > 0) {
2338 if (write(tmp
, buffer
, c
) <= 0)
2344 if (rename(fullpath_tmp
, fullpath
) == 0)
2345 ret
= fopen(fullpath
, "r");
2347 unlink(fullpath_tmp
);
2352 unlink(fullpath_tmp
);
2362 /* Read up configuration file from file_name. */
2363 void vty_read_config(const char *config_file
, char *config_default_dir
)
2365 char cwd
[MAXPATHLEN
];
2367 const char *fullpath
;
2370 /* If -f flag specified. */
2371 if (config_file
!= NULL
) {
2372 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2373 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2375 "Failure to determine Current Working Directory %d!",
2379 tmp
= XMALLOC(MTYPE_TMP
,
2380 strlen(cwd
) + strlen(config_file
) + 2);
2381 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2384 fullpath
= config_file
;
2386 confp
= fopen(fullpath
, "r");
2388 if (confp
== NULL
) {
2389 zlog_err("%s: failed to open configuration file %s: %s",
2390 __func__
, fullpath
, safe_strerror(errno
));
2392 confp
= vty_use_backup_config(fullpath
);
2395 "WARNING: using backup configuration file!");
2397 zlog_err("can't open configuration file [%s]",
2404 host_config_set(config_default_dir
);
2408 struct stat conf_stat
;
2410 /* !!!!PLEASE LEAVE!!!!
2411 * This is NEEDED for use with vtysh -b, or else you can get
2412 * a real configuration food fight with a lot garbage in the
2413 * merged configuration file it creates coming from the per
2414 * daemon configuration files. This also allows the daemons
2415 * to start if there default configuration file is not
2416 * present or ignore them, as needed when using vtysh -b to
2417 * configure the daemons at boot - MAG
2420 /* Stat for vtysh Zebra.conf, if found startup and wait for
2421 * boot configuration
2424 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2425 ret
= stat(integrate_default
, &conf_stat
);
2427 goto tmp_free_and_out
;
2430 confp
= fopen(config_default_dir
, "r");
2431 if (confp
== NULL
) {
2432 zlog_err("%s: failed to open configuration file %s: %s",
2433 __func__
, config_default_dir
,
2434 safe_strerror(errno
));
2436 confp
= vty_use_backup_config(config_default_dir
);
2439 "WARNING: using backup configuration file!");
2440 fullpath
= config_default_dir
;
2442 zlog_err("can't open configuration file [%s]",
2443 config_default_dir
);
2444 goto tmp_free_and_out
;
2447 fullpath
= config_default_dir
;
2450 vty_read_file(confp
);
2454 host_config_set(fullpath
);
2458 XFREE(MTYPE_TMP
, tmp
);
2461 /* Small utility function which output log to the VTY. */
2462 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2463 struct timestamp_control
*ctl
, va_list va
)
2471 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2472 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2476 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2482 /* Async-signal-safe version of vty_log for fixed strings. */
2483 void vty_log_fixed(char *buf
, size_t len
)
2486 struct iovec iov
[2];
2487 char crlf
[4] = "\r\n";
2489 /* vty may not have been initialised */
2493 iov
[0].iov_base
= buf
;
2494 iov
[0].iov_len
= len
;
2495 iov
[1].iov_base
= crlf
;
2498 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2500 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2501 /* N.B. We don't care about the return code, since
2503 most likely just about to die anyway. */
2504 if (writev(vty
->wfd
, iov
, 2) == -1) {
2505 fprintf(stderr
, "Failure to writev: %d\n",
2512 int vty_config_lock(struct vty
*vty
)
2514 if (vty_config_is_lockless
)
2516 if (vty_config
== 0) {
2523 int vty_config_unlock(struct vty
*vty
)
2525 if (vty_config_is_lockless
)
2527 if (vty_config
== 1 && vty
->config
== 1) {
2534 void vty_config_lockless(void)
2536 vty_config_is_lockless
= 1;
2539 /* Master of the threads. */
2540 static struct thread_master
*vty_master
;
2542 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2544 struct thread
*vty_serv_thread
= NULL
;
2548 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2550 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2554 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2556 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2560 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2564 vty
->t_write
= NULL
;
2565 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2571 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2573 /* Time out treatment. */
2574 if (vty
->v_timeout
) {
2576 thread_cancel(vty
->t_timeout
);
2577 vty
->t_timeout
= NULL
;
2578 thread_add_timer(vty_master
, vty_timeout
, vty
,
2579 vty
->v_timeout
, &vty
->t_timeout
);
2583 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2586 case VTY_TIMEOUT_RESET
:
2587 if (vty
->t_timeout
) {
2588 thread_cancel(vty
->t_timeout
);
2589 vty
->t_timeout
= NULL
;
2591 if (vty
->v_timeout
) {
2592 vty
->t_timeout
= NULL
;
2593 thread_add_timer(vty_master
, vty_timeout
, vty
,
2594 vty
->v_timeout
, &vty
->t_timeout
);
2600 DEFUN_NOSH (config_who
,
2603 "Display who is on vty\n")
2608 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2609 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2610 vty_out(vty
, "%svty[%d] connected from %s.\n",
2611 v
->config
? "*" : " ", i
, v
->address
);
2615 /* Move to vty configuration mode. */
2616 DEFUN_NOSH (line_vty
,
2619 "Configure a terminal line\n"
2620 "Virtual terminal\n")
2622 vty
->node
= VTY_NODE
;
2626 /* Set time out value. */
2627 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2628 const char *sec_str
)
2630 unsigned long timeout
= 0;
2632 /* min_str and sec_str are already checked by parser. So it must be
2633 all digit string. */
2635 timeout
= strtol(min_str
, NULL
, 10);
2639 timeout
+= strtol(sec_str
, NULL
, 10);
2641 vty_timeout_val
= timeout
;
2642 vty
->v_timeout
= timeout
;
2643 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2649 DEFUN (exec_timeout_min
,
2650 exec_timeout_min_cmd
,
2651 "exec-timeout (0-35791)",
2652 "Set timeout value\n"
2653 "Timeout value in minutes\n")
2656 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2659 DEFUN (exec_timeout_sec
,
2660 exec_timeout_sec_cmd
,
2661 "exec-timeout (0-35791) (0-2147483)",
2662 "Set the EXEC timeout\n"
2663 "Timeout in minutes\n"
2664 "Timeout in seconds\n")
2667 int idx_number_2
= 2;
2668 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2669 argv
[idx_number_2
]->arg
);
2672 DEFUN (no_exec_timeout
,
2673 no_exec_timeout_cmd
,
2676 "Set the EXEC timeout\n")
2678 return exec_timeout(vty
, NULL
, NULL
);
2681 /* Set vty access class. */
2682 DEFUN (vty_access_class
,
2683 vty_access_class_cmd
,
2684 "access-class WORD",
2685 "Filter connections based on an IP access list\n"
2689 if (vty_accesslist_name
)
2690 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2692 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2697 /* Clear vty access class. */
2698 DEFUN (no_vty_access_class
,
2699 no_vty_access_class_cmd
,
2700 "no access-class [WORD]",
2702 "Filter connections based on an IP access list\n"
2706 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2707 if (!vty_accesslist_name
2708 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2709 vty_out(vty
, "Access-class is not currently applied to vty\n");
2710 return CMD_WARNING_CONFIG_FAILED
;
2713 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2715 vty_accesslist_name
= NULL
;
2720 /* Set vty access class. */
2721 DEFUN (vty_ipv6_access_class
,
2722 vty_ipv6_access_class_cmd
,
2723 "ipv6 access-class WORD",
2725 "Filter connections based on an IP access list\n"
2726 "IPv6 access list\n")
2729 if (vty_ipv6_accesslist_name
)
2730 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2732 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2737 /* Clear vty access class. */
2738 DEFUN (no_vty_ipv6_access_class
,
2739 no_vty_ipv6_access_class_cmd
,
2740 "no ipv6 access-class [WORD]",
2743 "Filter connections based on an IP access list\n"
2744 "IPv6 access list\n")
2747 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2749 if (!vty_ipv6_accesslist_name
2750 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2752 "IPv6 access-class is not currently applied to vty\n");
2753 return CMD_WARNING_CONFIG_FAILED
;
2756 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2758 vty_ipv6_accesslist_name
= NULL
;
2767 "Enable password checking\n")
2769 no_password_check
= 0;
2773 DEFUN (no_vty_login
,
2777 "Enable password checking\n")
2779 no_password_check
= 1;
2783 DEFUN (service_advanced_vty
,
2784 service_advanced_vty_cmd
,
2785 "service advanced-vty",
2786 "Set up miscellaneous service\n"
2787 "Enable advanced mode vty interface\n")
2793 DEFUN (no_service_advanced_vty
,
2794 no_service_advanced_vty_cmd
,
2795 "no service advanced-vty",
2797 "Set up miscellaneous service\n"
2798 "Enable advanced mode vty interface\n")
2804 DEFUN_NOSH (terminal_monitor
,
2805 terminal_monitor_cmd
,
2807 "Set terminal line parameters\n"
2808 "Copy debug output to the current terminal line\n")
2814 DEFUN_NOSH (terminal_no_monitor
,
2815 terminal_no_monitor_cmd
,
2816 "terminal no monitor",
2817 "Set terminal line parameters\n"
2819 "Copy debug output to the current terminal line\n")
2825 DEFUN_NOSH (no_terminal_monitor
,
2826 no_terminal_monitor_cmd
,
2827 "no terminal monitor",
2829 "Set terminal line parameters\n"
2830 "Copy debug output to the current terminal line\n")
2832 return terminal_no_monitor(self
, vty
, argc
, argv
);
2836 DEFUN_NOSH (show_history
,
2840 "Display the session command history\n")
2844 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2845 if (index
== VTY_MAXHIST
) {
2850 if (vty
->hist
[index
] != NULL
)
2851 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2860 DEFUN (log_commands
,
2864 "Log all commands (can't be unset without restart)\n")
2866 do_log_commands
= 1;
2870 /* Display current configuration. */
2871 static int vty_config_write(struct vty
*vty
)
2873 vty_out(vty
, "line vty\n");
2875 if (vty_accesslist_name
)
2876 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2878 if (vty_ipv6_accesslist_name
)
2879 vty_out(vty
, " ipv6 access-class %s\n",
2880 vty_ipv6_accesslist_name
);
2883 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2884 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2885 vty_timeout_val
% 60);
2888 if (no_password_check
)
2889 vty_out(vty
, " no login\n");
2891 if (do_log_commands
)
2892 vty_out(vty
, "log commands\n");
2894 vty_out(vty
, "!\n");
2899 struct cmd_node vty_node
= {
2900 VTY_NODE
, "%s(config-line)# ", 1,
2903 /* Reset all VTY status. */
2908 struct thread
*vty_serv_thread
;
2910 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2911 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2912 buffer_reset(vty
->obuf
);
2913 vty
->status
= VTY_CLOSE
;
2917 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
2918 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
2920 thread_cancel(vty_serv_thread
);
2921 vector_slot(Vvty_serv_thread
, i
) = NULL
;
2925 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2927 if (vty_accesslist_name
) {
2928 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2929 vty_accesslist_name
= NULL
;
2932 if (vty_ipv6_accesslist_name
) {
2933 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2934 vty_ipv6_accesslist_name
= NULL
;
2938 static void vty_save_cwd(void)
2940 char cwd
[MAXPATHLEN
];
2943 c
= getcwd(cwd
, MAXPATHLEN
);
2947 * At this point if these go wrong, more than likely
2948 * the whole world is coming down around us
2949 * Hence not worrying about it too much.
2951 if (!chdir(SYSCONFDIR
)) {
2952 zlog_err("Failure to chdir to %s, errno: %d",
2956 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2957 zlog_err("Failure to getcwd, errno: %d", errno
);
2962 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
2963 strcpy(vty_cwd
, cwd
);
2971 int vty_shell(struct vty
*vty
)
2973 return vty
->type
== VTY_SHELL
? 1 : 0;
2976 int vty_shell_serv(struct vty
*vty
)
2978 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2981 void vty_init_vtysh()
2983 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
2986 /* Install vty's own commands like `who' command. */
2987 void vty_init(struct thread_master
*master_thread
)
2989 /* For further configuration read, preserve current directory. */
2992 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
2994 vty_master
= master_thread
;
2996 atexit(vty_stdio_atexit
);
2998 /* Initilize server thread vector. */
2999 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3001 /* Install bgp top node. */
3002 install_node(&vty_node
, vty_config_write
);
3004 install_element(VIEW_NODE
, &config_who_cmd
);
3005 install_element(VIEW_NODE
, &show_history_cmd
);
3006 install_element(CONFIG_NODE
, &line_vty_cmd
);
3007 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3008 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3009 install_element(CONFIG_NODE
, &show_history_cmd
);
3010 install_element(CONFIG_NODE
, &log_commands_cmd
);
3011 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3012 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3013 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3015 install_default(VTY_NODE
);
3016 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3017 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3018 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3019 install_element(VTY_NODE
, &vty_access_class_cmd
);
3020 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3021 install_element(VTY_NODE
, &vty_login_cmd
);
3022 install_element(VTY_NODE
, &no_vty_login_cmd
);
3023 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3024 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3027 void vty_terminate(void)
3030 XFREE(MTYPE_TMP
, vty_cwd
);
3032 if (vtyvec
&& Vvty_serv_thread
) {
3034 vector_free(vtyvec
);
3035 vector_free(Vvty_serv_thread
);
3037 Vvty_serv_thread
= NULL
;