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
24 #include <lib/version.h>
25 #include <sys/types.h>
32 #include "sockunion.h"
43 #include <arpa/telnet.h>
46 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
47 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
48 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
63 static void vty_event(enum event
, int, struct vty
*);
65 /* Extern host structure from command.c */
66 extern struct host host
;
68 /* Vector which store each vty structure. */
71 /* Vty timeout value. */
72 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
74 /* Vty access-class command */
75 static char *vty_accesslist_name
= NULL
;
77 /* Vty access-calss for IPv6. */
78 static char *vty_ipv6_accesslist_name
= NULL
;
80 /* VTY server thread. */
81 static vector Vvty_serv_thread
;
83 /* Current directory. */
87 static int vty_config
;
88 static int vty_config_is_lockless
= 0;
90 /* Login password check. */
91 static int no_password_check
= 0;
93 /* Integrated configuration file path */
94 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
96 static int do_log_commands
= 0;
98 void vty_frame(struct vty
*vty
, const char *format
, ...)
102 va_start(args
, format
);
103 vsnprintf(vty
->frame
+ vty
->frame_pos
,
104 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
105 vty
->frame_pos
= strlen(vty
->frame
);
109 void vty_endframe(struct vty
*vty
, const char *endtext
)
111 if (vty
->frame_pos
== 0 && endtext
)
112 vty_out(vty
, "%s", endtext
);
116 bool vty_set_include(struct vty
*vty
, const char *regexp
)
123 memset(&vty
->include
, 0x00, sizeof(vty
->include
));
128 errcode
= regcomp(&vty
->include
, regexp
,
129 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
132 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
133 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
141 /* VTY standard output function. */
142 int vty_out(struct vty
*vty
, const char *format
, ...)
151 if (vty
->frame_pos
) {
153 vty_out(vty
, "%s", vty
->frame
);
156 /* Try to write to initial buffer. */
157 va_start(args
, format
);
158 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
161 /* Initial buffer is not enough. */
162 if (len
< 0 || len
>= size
) {
169 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
173 va_start(args
, format
);
174 len
= vsnprintf(p
, size
, format
, args
);
177 if (len
> -1 && len
< size
)
182 /* When initial buffer is enough to store all output. */
188 vector lines
= frrstr_split_vec(buf
, "\n");
190 frrstr_filter_vec(lines
, &vty
->include
);
191 if (buf
[strlen(buf
) - 1] == '\n' && vector_active(lines
) > 0)
192 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
193 filtered
= frrstr_join_vec(lines
, "\n");
194 frrstr_strvec_free(lines
);
201 /* print with crlf replacement */
202 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
206 fprintf(vty
->of
, "%s", filtered
);
212 /* print without crlf replacement */
213 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
218 XFREE(MTYPE_TMP
, filtered
);
220 /* If p is not different with buf, it is allocated buffer. */
222 XFREE(MTYPE_VTY_OUT_BUF
, p
);
227 static int vty_log_out(struct vty
*vty
, const char *level
,
228 const char *proto_str
, const char *format
,
229 struct timestamp_control
*ctl
, va_list va
)
235 if (!ctl
->already_rendered
) {
236 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
238 ctl
->already_rendered
= 1;
240 if (ctl
->len
+ 1 >= sizeof(buf
))
242 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
247 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
250 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
251 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
254 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
255 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
261 if (write(vty
->wfd
, buf
, len
) < 0) {
262 if (ERRNO_IO_RETRY(errno
))
263 /* Kernel buffer is full, probably too much debugging
265 drop the data and ignore. */
267 /* Fatal I/O error. */
269 0; /* disable monitoring to avoid infinite recursion */
270 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
271 __func__
, vty
->fd
, safe_strerror(errno
));
272 buffer_reset(vty
->obuf
);
273 /* cannot call vty_close, because a parent routine may still try
274 to access the vty struct */
275 vty
->status
= VTY_CLOSE
;
276 shutdown(vty
->fd
, SHUT_RDWR
);
282 /* Output current time to the vty. */
283 void vty_time_print(struct vty
*vty
, int cr
)
285 char buf
[QUAGGA_TIMESTAMP_LEN
];
287 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
288 zlog_info("quagga_timestamp error");
292 vty_out(vty
, "%s\n", buf
);
294 vty_out(vty
, "%s ", buf
);
299 /* Say hello to vty interface. */
300 void vty_hello(struct vty
*vty
)
306 f
= fopen(host
.motdfile
, "r");
308 while (fgets(buf
, sizeof(buf
), f
)) {
310 /* work backwards to ignore trailling isspace()
312 for (s
= buf
+ strlen(buf
);
313 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
316 vty_out(vty
, "%s\n", buf
);
320 vty_out(vty
, "MOTD file not found\n");
321 } else if (host
.motd
)
322 vty_out(vty
, "%s", host
.motd
);
325 /* Put out prompt and wait input from user. */
326 static void vty_prompt(struct vty
*vty
)
328 if (vty
->type
== VTY_TERM
) {
329 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
333 /* Send WILL TELOPT_ECHO to remote server. */
334 static void vty_will_echo(struct vty
*vty
)
336 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
337 vty_out(vty
, "%s", cmd
);
340 /* Make suppress Go-Ahead telnet option. */
341 static void vty_will_suppress_go_ahead(struct vty
*vty
)
343 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
344 vty_out(vty
, "%s", cmd
);
347 /* Make don't use linemode over telnet. */
348 static void vty_dont_linemode(struct vty
*vty
)
350 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
351 vty_out(vty
, "%s", cmd
);
354 /* Use window size. */
355 static void vty_do_window_size(struct vty
*vty
)
357 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
358 vty_out(vty
, "%s", cmd
);
361 #if 0 /* Currently not used. */
362 /* Make don't use lflow vty interface. */
364 vty_dont_lflow_ahead (struct vty
*vty
)
366 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
367 vty_out (vty
, "%s", cmd
);
371 /* Authentication of vty */
372 static void vty_auth(struct vty
*vty
, char *buf
)
375 enum node_type next_node
= 0;
377 char *crypt(const char *, const char *);
382 passwd
= host
.password_encrypt
;
384 passwd
= host
.password
;
386 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
388 next_node
= VIEW_NODE
;
390 case AUTH_ENABLE_NODE
:
392 passwd
= host
.enable_encrypt
;
394 passwd
= host
.enable
;
395 next_node
= ENABLE_NODE
;
401 fail
= strcmp(crypt(buf
, passwd
), passwd
);
403 fail
= strcmp(buf
, passwd
);
409 vty
->node
= next_node
; /* Success ! */
412 if (vty
->fail
>= 3) {
413 if (vty
->node
== AUTH_NODE
) {
415 "%% Bad passwords, too many failures!\n");
416 vty
->status
= VTY_CLOSE
;
418 /* AUTH_ENABLE_NODE */
421 "%% Bad enable passwords, too many failures!\n");
422 vty
->status
= VTY_CLOSE
;
428 /* Command execution over the vty interface. */
429 static int vty_command(struct vty
*vty
, char *buf
)
432 const char *protocolname
;
436 * Log non empty command lines
441 /* Skip white spaces. */
442 while (isspace((int)*cp
) && *cp
!= '\0')
445 if (cp
!= NULL
&& *cp
!= '\0') {
447 char vty_str
[VTY_BUFSIZ
];
448 char prompt_str
[VTY_BUFSIZ
];
450 /* format the base vty info */
451 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
453 for (i
= 0; i
< vector_active(vtyvec
); i
++)
454 if (vty
== vector_slot(vtyvec
, i
)) {
455 snprintf(vty_str
, sizeof(vty_str
),
456 "vty[%d]@%s", i
, vty
->address
);
460 /* format the prompt */
461 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
464 /* now log the command */
465 zlog_err("%s%s", prompt_str
, buf
);
468 #ifdef CONSUMED_TIME_CHECK
472 unsigned long realtime
, cputime
;
475 #endif /* CONSUMED_TIME_CHECK */
477 ret
= cmd_execute(vty
, buf
, NULL
, 0);
479 /* Get the name of the protocol if any */
480 protocolname
= frr_protoname
;
482 #ifdef CONSUMED_TIME_CHECK
484 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
485 > CONSUMED_TIME_CHECK
)
486 /* Warn about CPU hog that must be fixed. */
488 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
489 realtime
/ 1000, cputime
/ 1000, buf
);
491 #endif /* CONSUMED_TIME_CHECK */
493 if (ret
!= CMD_SUCCESS
)
496 if (vty
->type
== VTY_FILE
)
497 vty_out(vty
, "Warning...\n");
499 case CMD_ERR_AMBIGUOUS
:
500 vty_out(vty
, "%% Ambiguous command.\n");
502 case CMD_ERR_NO_MATCH
:
503 vty_out(vty
, "%% [%s] Unknown command: %s\n",
506 case CMD_ERR_INCOMPLETE
:
507 vty_out(vty
, "%% Command incomplete.\n");
514 static const char telnet_backward_char
= 0x08;
515 static const char telnet_space_char
= ' ';
517 /* Basic function to write buffer to vty. */
518 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
520 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
523 /* Should we do buffering here ? And make vty_flush (vty) ? */
524 buffer_put(vty
->obuf
, buf
, nbytes
);
527 /* Basic function to insert character into vty. */
528 static void vty_self_insert(struct vty
*vty
, char c
)
533 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
536 length
= vty
->length
- vty
->cp
;
537 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
538 vty
->buf
[vty
->cp
] = c
;
540 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
541 for (i
= 0; i
< length
; i
++)
542 vty_write(vty
, &telnet_backward_char
, 1);
547 vty
->buf
[vty
->length
] = '\0';
550 /* Self insert character 'c' in overwrite mode. */
551 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
553 if (vty
->cp
== vty
->length
) {
554 vty_self_insert(vty
, c
);
558 vty
->buf
[vty
->cp
++] = c
;
559 vty_write(vty
, &c
, 1);
563 * Insert a string into vty->buf at the current cursor position.
565 * If the resultant string would be larger than VTY_BUFSIZ it is
568 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
570 if (vty
->cp
== VTY_BUFSIZ
)
573 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
574 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
576 vty
->length
= MAX(vty
->cp
, vty
->length
);
577 vty
->buf
[vty
->length
] = '\0';
578 vty_write(vty
, str
, nwrite
);
581 /* Forward character. */
582 static void vty_forward_char(struct vty
*vty
)
584 if (vty
->cp
< vty
->length
) {
585 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
590 /* Backward character. */
591 static void vty_backward_char(struct vty
*vty
)
595 vty_write(vty
, &telnet_backward_char
, 1);
599 /* Move to the beginning of the line. */
600 static void vty_beginning_of_line(struct vty
*vty
)
603 vty_backward_char(vty
);
606 /* Move to the end of the line. */
607 static void vty_end_of_line(struct vty
*vty
)
609 while (vty
->cp
< vty
->length
)
610 vty_forward_char(vty
);
613 static void vty_kill_line_from_beginning(struct vty
*);
614 static void vty_redraw_line(struct vty
*);
616 /* Print command line history. This function is called from
617 vty_next_line and vty_previous_line. */
618 static void vty_history_print(struct vty
*vty
)
622 vty_kill_line_from_beginning(vty
);
624 /* Get previous line from history buffer */
625 length
= strlen(vty
->hist
[vty
->hp
]);
626 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
627 vty
->cp
= vty
->length
= length
;
628 vty
->buf
[vty
->length
] = '\0';
630 /* Redraw current line */
631 vty_redraw_line(vty
);
634 /* Show next command line history. */
635 static void vty_next_line(struct vty
*vty
)
639 if (vty
->hp
== vty
->hindex
)
642 /* Try is there history exist or not. */
644 if (try_index
== (VTY_MAXHIST
- 1))
649 /* If there is not history return. */
650 if (vty
->hist
[try_index
] == NULL
)
655 vty_history_print(vty
);
658 /* Show previous command line history. */
659 static void vty_previous_line(struct vty
*vty
)
665 try_index
= VTY_MAXHIST
- 1;
669 if (vty
->hist
[try_index
] == NULL
)
674 vty_history_print(vty
);
677 /* This function redraw all of the command line character. */
678 static void vty_redraw_line(struct vty
*vty
)
680 vty_write(vty
, vty
->buf
, vty
->length
);
681 vty
->cp
= vty
->length
;
685 static void 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. */
695 static void vty_backward_pure_word(struct vty
*vty
)
697 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
698 vty_backward_char(vty
);
702 static void vty_backward_word(struct vty
*vty
)
704 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
705 vty_backward_char(vty
);
707 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
708 vty_backward_char(vty
);
711 /* When '^D' is typed at the beginning of the line we move to the down
713 static void vty_down_level(struct vty
*vty
)
721 /* When '^Z' is received from vty, move down to the enable mode. */
722 static void vty_end_config(struct vty
*vty
)
741 case BGP_VRF_POLICY_NODE
:
742 case BGP_VNC_DEFAULTS_NODE
:
743 case BGP_VNC_NVE_GROUP_NODE
:
744 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
:
767 case BGP_EVPN_VNI_NODE
:
768 vty_config_unlock(vty
);
769 vty
->node
= ENABLE_NODE
;
772 /* Unknown node, we have to ignore it. */
780 /* Delete a charcter at the current point. */
781 static void vty_delete_char(struct vty
*vty
)
786 if (vty
->length
== 0) {
791 if (vty
->cp
== vty
->length
)
792 return; /* completion need here? */
794 size
= vty
->length
- vty
->cp
;
797 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
798 vty
->buf
[vty
->length
] = '\0';
800 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
803 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
804 vty_write(vty
, &telnet_space_char
, 1);
806 for (i
= 0; i
< size
; i
++)
807 vty_write(vty
, &telnet_backward_char
, 1);
810 /* Delete a character before the point. */
811 static void vty_delete_backward_char(struct vty
*vty
)
816 vty_backward_char(vty
);
817 vty_delete_char(vty
);
820 /* Kill rest of line from current point. */
821 static void vty_kill_line(struct vty
*vty
)
826 size
= vty
->length
- vty
->cp
;
831 for (i
= 0; i
< size
; i
++)
832 vty_write(vty
, &telnet_space_char
, 1);
833 for (i
= 0; i
< size
; i
++)
834 vty_write(vty
, &telnet_backward_char
, 1);
836 memset(&vty
->buf
[vty
->cp
], 0, size
);
837 vty
->length
= vty
->cp
;
840 /* Kill line from the beginning. */
841 static void vty_kill_line_from_beginning(struct vty
*vty
)
843 vty_beginning_of_line(vty
);
847 /* Delete a word before the point. */
848 static void vty_forward_kill_word(struct vty
*vty
)
850 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
851 vty_delete_char(vty
);
852 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
853 vty_delete_char(vty
);
856 /* Delete a word before the point. */
857 static void vty_backward_kill_word(struct vty
*vty
)
859 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
860 vty_delete_backward_char(vty
);
861 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
862 vty_delete_backward_char(vty
);
865 /* Transpose chars before or at the point. */
866 static void vty_transpose_chars(struct vty
*vty
)
870 /* If length is short or point is near by the beginning of line then
872 if (vty
->length
< 2 || vty
->cp
< 1)
875 /* In case of point is located at the end of the line. */
876 if (vty
->cp
== vty
->length
) {
877 c1
= vty
->buf
[vty
->cp
- 1];
878 c2
= vty
->buf
[vty
->cp
- 2];
880 vty_backward_char(vty
);
881 vty_backward_char(vty
);
882 vty_self_insert_overwrite(vty
, c1
);
883 vty_self_insert_overwrite(vty
, c2
);
885 c1
= vty
->buf
[vty
->cp
];
886 c2
= vty
->buf
[vty
->cp
- 1];
888 vty_backward_char(vty
);
889 vty_self_insert_overwrite(vty
, c1
);
890 vty_self_insert_overwrite(vty
, c2
);
894 /* Do completion at vty interface. */
895 static void vty_complete_command(struct vty
*vty
)
899 char **matched
= NULL
;
902 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
905 vline
= cmd_make_strvec(vty
->buf
);
909 /* In case of 'help \t'. */
910 if (isspace((int)vty
->buf
[vty
->length
- 1]))
911 vector_set(vline
, NULL
);
913 matched
= cmd_complete_command(vline
, vty
, &ret
);
915 cmd_free_strvec(vline
);
919 case CMD_ERR_AMBIGUOUS
:
920 vty_out(vty
, "%% Ambiguous command.\n");
922 vty_redraw_line(vty
);
924 case CMD_ERR_NO_MATCH
:
925 /* vty_out (vty, "%% There is no matched command.\n"); */
927 vty_redraw_line(vty
);
929 case CMD_COMPLETE_FULL_MATCH
:
931 /* 2016-11-28 equinox -- need to debug, SEGV here */
932 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
934 vty_redraw_line(vty
);
938 vty_redraw_line(vty
);
939 vty_backward_pure_word(vty
);
940 vty_insert_word_overwrite(vty
, matched
[0]);
941 vty_self_insert(vty
, ' ');
942 XFREE(MTYPE_COMPLETION
, matched
[0]);
944 case CMD_COMPLETE_MATCH
:
946 vty_redraw_line(vty
);
947 vty_backward_pure_word(vty
);
948 vty_insert_word_overwrite(vty
, matched
[0]);
949 XFREE(MTYPE_COMPLETION
, matched
[0]);
951 case CMD_COMPLETE_LIST_MATCH
:
952 for (i
= 0; matched
[i
] != NULL
; i
++) {
953 if (i
!= 0 && ((i
% 6) == 0))
955 vty_out(vty
, "%-10s ", matched
[i
]);
956 XFREE(MTYPE_COMPLETION
, matched
[i
]);
961 vty_redraw_line(vty
);
963 case CMD_ERR_NOTHING_TODO
:
965 vty_redraw_line(vty
);
971 XFREE(MTYPE_TMP
, matched
);
974 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
975 unsigned int desc_width
, struct cmd_token
*token
)
983 if (desc_width
<= 0) {
984 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
988 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
990 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
991 for (pos
= desc_width
; pos
> 0; pos
--)
992 if (*(p
+ pos
) == ' ')
998 strncpy(buf
, p
, pos
);
1000 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1005 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1007 XFREE(MTYPE_TMP
, buf
);
1010 /* Describe matched command function. */
1011 static void vty_describe_command(struct vty
*vty
)
1016 unsigned int i
, width
, desc_width
;
1017 struct cmd_token
*token
, *token_cr
= NULL
;
1019 vline
= cmd_make_strvec(vty
->buf
);
1021 /* In case of '> ?'. */
1022 if (vline
== NULL
) {
1023 vline
= vector_init(1);
1024 vector_set(vline
, NULL
);
1025 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1026 vector_set(vline
, NULL
);
1028 describe
= cmd_describe_command(vline
, vty
, &ret
);
1032 /* Ambiguous error. */
1034 case CMD_ERR_AMBIGUOUS
:
1035 vty_out(vty
, "%% Ambiguous command.\n");
1038 case CMD_ERR_NO_MATCH
:
1039 vty_out(vty
, "%% There is no matched command.\n");
1044 /* Get width of command string. */
1046 for (i
= 0; i
< vector_active(describe
); i
++)
1047 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1050 if (token
->text
[0] == '\0')
1053 len
= strlen(token
->text
);
1059 /* Get width of description string. */
1060 desc_width
= vty
->width
- (width
+ 6);
1062 /* Print out description. */
1063 for (i
= 0; i
< vector_active(describe
); i
++)
1064 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1065 if (token
->text
[0] == '\0')
1068 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1074 vty_out(vty
, " %-s\n", token
->text
);
1075 else if (desc_width
>= strlen(token
->desc
))
1076 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1079 vty_describe_fold(vty
, width
, desc_width
,
1082 if (IS_VARYING_TOKEN(token
->type
)) {
1083 const char *ref
= vector_slot(
1084 vline
, vector_active(vline
) - 1);
1086 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1087 cmd_variable_complete(token
, ref
, varcomps
);
1089 if (vector_active(varcomps
) > 0) {
1090 char *ac
= cmd_variable_comp2str(
1091 varcomps
, vty
->width
);
1092 vty_out(vty
, "%s\n", ac
);
1093 XFREE(MTYPE_TMP
, ac
);
1096 vector_free(varcomps
);
1099 vty_out (vty
, " %-*s %s\n", width
1100 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1101 desc
->str
? desc
->str
: "");
1105 if ((token
= token_cr
)) {
1107 vty_out(vty
, " %-s\n", token
->text
);
1108 else if (desc_width
>= strlen(token
->desc
))
1109 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1112 vty_describe_fold(vty
, width
, desc_width
, token
);
1116 cmd_free_strvec(vline
);
1118 vector_free(describe
);
1121 vty_redraw_line(vty
);
1124 static void vty_clear_buf(struct vty
*vty
)
1126 memset(vty
->buf
, 0, vty
->max
);
1129 /* ^C stop current input and do not add command line to the history. */
1130 static void vty_stop_input(struct vty
*vty
)
1132 vty
->cp
= vty
->length
= 0;
1136 switch (vty
->node
) {
1139 /* Nothing to do. */
1142 case INTERFACE_NODE
:
1156 case LDP_IPV4_IFACE_NODE
:
1157 case LDP_IPV6_IFACE_NODE
:
1158 case LDP_L2VPN_NODE
:
1159 case LDP_PSEUDOWIRE_NODE
:
1162 case KEYCHAIN_KEY_NODE
:
1164 vty_config_unlock(vty
);
1165 vty
->node
= ENABLE_NODE
;
1168 /* Unknown node, we have to ignore it. */
1173 /* Set history pointer to the latest one. */
1174 vty
->hp
= vty
->hindex
;
1177 /* Add current command line to the history buffer. */
1178 static void vty_hist_add(struct vty
*vty
)
1182 if (vty
->length
== 0)
1185 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1187 /* Ignore the same string as previous one. */
1188 if (vty
->hist
[index
])
1189 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1190 vty
->hp
= vty
->hindex
;
1194 /* Insert history entry. */
1195 if (vty
->hist
[vty
->hindex
])
1196 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1197 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1199 /* History index rotation. */
1201 if (vty
->hindex
== VTY_MAXHIST
)
1204 vty
->hp
= vty
->hindex
;
1207 /* #define TELNET_OPTION_DEBUG */
1209 /* Get telnet window size. */
1210 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1212 #ifdef TELNET_OPTION_DEBUG
1215 for (i
= 0; i
< nbytes
; i
++) {
1218 vty_out(vty
, "IAC ");
1221 vty_out(vty
, "WILL ");
1224 vty_out(vty
, "WONT ");
1227 vty_out(vty
, "DO ");
1230 vty_out(vty
, "DONT ");
1233 vty_out(vty
, "SB ");
1236 vty_out(vty
, "SE ");
1239 vty_out(vty
, "TELOPT_ECHO \n");
1242 vty_out(vty
, "TELOPT_SGA \n");
1245 vty_out(vty
, "TELOPT_NAWS \n");
1248 vty_out(vty
, "%x ", buf
[i
]);
1254 #endif /* TELNET_OPTION_DEBUG */
1259 vty
->iac_sb_in_progress
= 1;
1263 if (!vty
->iac_sb_in_progress
)
1266 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1267 vty
->iac_sb_in_progress
= 0;
1270 switch (vty
->sb_buf
[0]) {
1272 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1274 "RFC 1073 violation detected: telnet NAWS option "
1275 "should send %d characters, but we received %lu",
1277 (unsigned long)vty
->sb_len
);
1278 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1280 "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1281 "too small to handle the telnet NAWS option",
1282 (unsigned long)sizeof(vty
->sb_buf
),
1283 TELNET_NAWS_SB_LEN
);
1285 vty
->width
= ((vty
->sb_buf
[1] << 8)
1287 vty
->height
= ((vty
->sb_buf
[3] << 8)
1289 #ifdef TELNET_OPTION_DEBUG
1291 "TELNET NAWS window size negotiation completed: "
1292 "width %d, height %d\n",
1293 vty
->width
, vty
->height
);
1298 vty
->iac_sb_in_progress
= 0;
1308 /* Execute current command line. */
1309 static int vty_execute(struct vty
*vty
)
1315 switch (vty
->node
) {
1317 case AUTH_ENABLE_NODE
:
1318 vty_auth(vty
, vty
->buf
);
1321 ret
= vty_command(vty
, vty
->buf
);
1322 if (vty
->type
== VTY_TERM
)
1327 /* Clear command line buffer. */
1328 vty
->cp
= vty
->length
= 0;
1331 if (vty
->status
!= VTY_CLOSE
)
1337 #define CONTROL(X) ((X) - '@')
1338 #define VTY_NORMAL 0
1339 #define VTY_PRE_ESCAPE 1
1340 #define VTY_ESCAPE 2
1342 /* Escape character command map. */
1343 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1347 vty_previous_line(vty
);
1353 vty_forward_char(vty
);
1356 vty_backward_char(vty
);
1362 /* Go back to normal mode. */
1363 vty
->escape
= VTY_NORMAL
;
1366 /* Quit print out to the buffer. */
1367 static void vty_buffer_reset(struct vty
*vty
)
1369 buffer_reset(vty
->obuf
);
1371 vty_redraw_line(vty
);
1374 /* Read data via vty socket. */
1375 static int vty_read(struct thread
*thread
)
1379 unsigned char buf
[VTY_READ_BUFSIZ
];
1381 int vty_sock
= THREAD_FD(thread
);
1382 struct vty
*vty
= THREAD_ARG(thread
);
1385 /* Read raw data from socket */
1386 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1388 if (ERRNO_IO_RETRY(errno
)) {
1389 vty_event(VTY_READ
, vty_sock
, vty
);
1392 vty
->monitor
= 0; /* disable monitoring to avoid
1393 infinite recursion */
1395 "%s: read error on vty client fd %d, closing: %s",
1396 __func__
, vty
->fd
, safe_strerror(errno
));
1397 buffer_reset(vty
->obuf
);
1399 vty
->status
= VTY_CLOSE
;
1402 for (i
= 0; i
< nbytes
; i
++) {
1403 if (buf
[i
] == IAC
) {
1412 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1413 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1414 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1420 /* In case of telnet command */
1422 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1429 if (vty
->status
== VTY_MORE
) {
1434 vty_buffer_reset(vty
);
1436 #if 0 /* More line does not work for "show ip bgp". */
1439 vty
->status
= VTY_MORELINE
;
1448 /* Escape character. */
1449 if (vty
->escape
== VTY_ESCAPE
) {
1450 vty_escape_map(buf
[i
], vty
);
1454 /* Pre-escape status. */
1455 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1458 vty
->escape
= VTY_ESCAPE
;
1461 vty_backward_word(vty
);
1462 vty
->escape
= VTY_NORMAL
;
1465 vty_forward_word(vty
);
1466 vty
->escape
= VTY_NORMAL
;
1469 vty_forward_kill_word(vty
);
1470 vty
->escape
= VTY_NORMAL
;
1474 vty_backward_kill_word(vty
);
1475 vty
->escape
= VTY_NORMAL
;
1478 vty
->escape
= VTY_NORMAL
;
1486 vty_beginning_of_line(vty
);
1489 vty_backward_char(vty
);
1492 vty_stop_input(vty
);
1495 vty_delete_char(vty
);
1498 vty_end_of_line(vty
);
1501 vty_forward_char(vty
);
1505 vty_delete_backward_char(vty
);
1514 vty_previous_line(vty
);
1517 vty_transpose_chars(vty
);
1520 vty_kill_line_from_beginning(vty
);
1523 vty_backward_kill_word(vty
);
1526 vty_end_config(vty
);
1534 vty_complete_command(vty
);
1537 if (vty
->node
== AUTH_NODE
1538 || vty
->node
== AUTH_ENABLE_NODE
)
1539 vty_self_insert(vty
, buf
[i
]);
1541 vty_describe_command(vty
);
1544 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1545 vty
->escape
= VTY_ESCAPE
;
1548 vty
->escape
= VTY_PRE_ESCAPE
;
1551 if (buf
[i
] > 31 && buf
[i
] < 127)
1552 vty_self_insert(vty
, buf
[i
]);
1558 if (vty
->status
== VTY_CLOSE
)
1561 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1562 vty_event(VTY_READ
, vty_sock
, vty
);
1567 /* Flush buffer to the vty. */
1568 static int vty_flush(struct thread
*thread
)
1571 buffer_status_t flushrc
;
1572 int vty_sock
= THREAD_FD(thread
);
1573 struct vty
*vty
= THREAD_ARG(thread
);
1575 vty
->t_write
= NULL
;
1577 /* Tempolary disable read thread. */
1578 if ((vty
->lines
== 0) && vty
->t_read
) {
1579 thread_cancel(vty
->t_read
);
1583 /* Function execution continue. */
1584 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1586 /* N.B. if width is 0, that means we don't know the window size. */
1587 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1588 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1589 else if (vty
->status
== VTY_MORELINE
)
1590 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1593 flushrc
= buffer_flush_window(
1594 vty
->obuf
, vty_sock
, vty
->width
,
1595 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1599 0; /* disable monitoring to avoid infinite recursion */
1600 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1602 buffer_reset(vty
->obuf
);
1606 if (vty
->status
== VTY_CLOSE
)
1609 vty
->status
= VTY_NORMAL
;
1610 if (vty
->lines
== 0)
1611 vty_event(VTY_READ
, vty_sock
, vty
);
1614 case BUFFER_PENDING
:
1615 /* There is more data waiting to be written. */
1616 vty
->status
= VTY_MORE
;
1617 if (vty
->lines
== 0)
1618 vty_event(VTY_WRITE
, vty_sock
, vty
);
1625 /* Allocate new vty struct. */
1626 struct vty
*vty_new()
1628 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1630 new->fd
= new->wfd
= -1;
1631 new->obuf
= buffer_new(0); /* Use default buffer size. */
1632 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1633 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1634 new->max
= VTY_BUFSIZ
;
1640 /* allocate and initialise vty */
1641 static struct vty
*vty_new_init(int vty_sock
)
1647 vty
->wfd
= vty_sock
;
1648 vty
->type
= VTY_TERM
;
1649 vty
->node
= AUTH_NODE
;
1654 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1657 vector_set_index(vtyvec
, vty_sock
, vty
);
1658 vty
->status
= VTY_NORMAL
;
1661 vty
->iac_sb_in_progress
= 0;
1667 /* Create new vty structure. */
1668 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1670 char buf
[SU_ADDRSTRLEN
];
1673 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1675 /* Allocate new vty structure and set up default values. */
1676 vty
= vty_new_init(vty_sock
);
1678 /* configurable parameters not part of basic init */
1679 vty
->v_timeout
= vty_timeout_val
;
1680 strcpy(vty
->address
, buf
);
1681 if (no_password_check
) {
1683 vty
->node
= ENABLE_NODE
;
1685 vty
->node
= VIEW_NODE
;
1687 if (host
.lines
>= 0)
1688 vty
->lines
= host
.lines
;
1690 if (!no_password_check
) {
1691 /* Vty is not available if password isn't set. */
1692 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1693 vty_out(vty
, "Vty password is not set.\n");
1694 vty
->status
= VTY_CLOSE
;
1700 /* Say hello to the world. */
1702 if (!no_password_check
)
1703 vty_out(vty
, "\nUser Access Verification\n\n");
1705 /* Setting up terminal. */
1707 vty_will_suppress_go_ahead(vty
);
1709 vty_dont_linemode(vty
);
1710 vty_do_window_size(vty
);
1711 /* vty_dont_lflow_ahead (vty); */
1715 /* Add read/write thread. */
1716 vty_event(VTY_WRITE
, vty_sock
, vty
);
1717 vty_event(VTY_READ
, vty_sock
, vty
);
1722 /* create vty for stdio */
1723 static struct termios stdio_orig_termios
;
1724 static struct vty
*stdio_vty
= NULL
;
1725 static bool stdio_termios
= false;
1726 static void (*stdio_vty_atclose
)(int isexit
);
1728 static void vty_stdio_reset(int isexit
)
1732 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1733 stdio_termios
= false;
1737 if (stdio_vty_atclose
)
1738 stdio_vty_atclose(isexit
);
1739 stdio_vty_atclose
= NULL
;
1743 static void vty_stdio_atexit(void)
1748 void vty_stdio_suspend(void)
1753 if (stdio_vty
->t_write
)
1754 thread_cancel(stdio_vty
->t_write
);
1755 if (stdio_vty
->t_read
)
1756 thread_cancel(stdio_vty
->t_read
);
1757 if (stdio_vty
->t_timeout
)
1758 thread_cancel(stdio_vty
->t_timeout
);
1761 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1762 stdio_termios
= false;
1765 void vty_stdio_resume(void)
1770 if (!tcgetattr(0, &stdio_orig_termios
)) {
1771 struct termios termios
;
1773 termios
= stdio_orig_termios
;
1774 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1775 | IGNCR
| ICRNL
| IXON
);
1776 termios
.c_oflag
&= ~OPOST
;
1777 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1778 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1779 termios
.c_cflag
|= CS8
;
1780 tcsetattr(0, TCSANOW
, &termios
);
1781 stdio_termios
= true;
1784 vty_prompt(stdio_vty
);
1786 /* Add read/write thread. */
1787 vty_event(VTY_WRITE
, 1, stdio_vty
);
1788 vty_event(VTY_READ
, 0, stdio_vty
);
1791 void vty_stdio_close(void)
1795 vty_close(stdio_vty
);
1798 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1802 /* refuse creating two vtys on stdio */
1806 vty
= stdio_vty
= vty_new_init(0);
1807 stdio_vty_atclose
= atclose
;
1810 /* always have stdio vty in a known _unchangeable_ state, don't want
1812 * to have any effect here to make sure scripting this works as intended
1814 vty
->node
= ENABLE_NODE
;
1816 strcpy(vty
->address
, "console");
1822 /* Accept connection from the network. */
1823 static int vty_accept(struct thread
*thread
)
1831 struct access_list
*acl
= NULL
;
1832 char buf
[SU_ADDRSTRLEN
];
1834 accept_sock
= THREAD_FD(thread
);
1836 /* We continue hearing vty socket. */
1837 vty_event(VTY_SERV
, accept_sock
, NULL
);
1839 memset(&su
, 0, sizeof(union sockunion
));
1841 /* We can handle IPv4 or IPv6 socket. */
1842 vty_sock
= sockunion_accept(accept_sock
, &su
);
1844 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1847 set_nonblocking(vty_sock
);
1848 set_cloexec(vty_sock
);
1850 sockunion2hostprefix(&su
, &p
);
1852 /* VTY's accesslist apply. */
1853 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1854 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1855 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1856 zlog_info("Vty connection refused from %s",
1857 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1860 /* continue accepting connections */
1861 vty_event(VTY_SERV
, accept_sock
, NULL
);
1867 /* VTY's ipv6 accesslist apply. */
1868 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1869 if ((acl
= access_list_lookup(AFI_IP6
,
1870 vty_ipv6_accesslist_name
))
1871 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1872 zlog_info("Vty connection refused from %s",
1873 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1876 /* continue accepting connections */
1877 vty_event(VTY_SERV
, accept_sock
, NULL
);
1884 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1887 zlog_info("can't set sockopt to vty_sock : %s",
1888 safe_strerror(errno
));
1890 zlog_info("Vty connection from %s",
1891 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1893 vty_create(vty_sock
, &su
);
1898 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1901 struct addrinfo req
;
1902 struct addrinfo
*ainfo
;
1903 struct addrinfo
*ainfo_save
;
1905 char port_str
[BUFSIZ
];
1907 memset(&req
, 0, sizeof(struct addrinfo
));
1908 req
.ai_flags
= AI_PASSIVE
;
1909 req
.ai_family
= AF_UNSPEC
;
1910 req
.ai_socktype
= SOCK_STREAM
;
1911 sprintf(port_str
, "%d", port
);
1912 port_str
[sizeof(port_str
) - 1] = '\0';
1914 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1917 zlog_err("getaddrinfo failed: %s", gai_strerror(ret
));
1924 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1927 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1928 ainfo
->ai_protocol
);
1932 sockopt_v6only(ainfo
->ai_family
, sock
);
1933 sockopt_reuseaddr(sock
);
1934 sockopt_reuseport(sock
);
1937 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1939 close(sock
); /* Avoid sd leak. */
1943 ret
= listen(sock
, 3);
1945 close(sock
); /* Avoid sd leak. */
1949 vty_event(VTY_SERV
, sock
, NULL
);
1950 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1952 freeaddrinfo(ainfo_save
);
1956 /* For sockaddr_un. */
1959 /* VTY shell UNIX domain socket. */
1960 static void vty_serv_un(const char *path
)
1964 struct sockaddr_un serv
;
1966 struct zprivs_ids_t ids
;
1968 /* First of all, unlink existing socket */
1972 old_mask
= umask(0007);
1974 /* Make UNIX domain socket. */
1975 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1977 zlog_err("Cannot create unix stream socket: %s",
1978 safe_strerror(errno
));
1982 /* Make server socket. */
1983 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1984 serv
.sun_family
= AF_UNIX
;
1985 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1986 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1987 len
= serv
.sun_len
= SUN_LEN(&serv
);
1989 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1990 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1994 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1996 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1997 close(sock
); /* Avoid sd leak. */
2001 ret
= listen(sock
, 5);
2003 zlog_err("listen(fd %d) failed: %s", sock
,
2004 safe_strerror(errno
));
2005 close(sock
); /* Avoid sd leak. */
2011 zprivs_get_ids(&ids
);
2013 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2014 earlier for the case when we don't need to chown the file
2015 type casting it here to make a compare */
2016 if ((int)ids
.gid_vty
> 0) {
2017 /* set group of socket */
2018 if (chown(path
, -1, ids
.gid_vty
)) {
2019 zlog_err("vty_serv_un: could chown socket, %s",
2020 safe_strerror(errno
));
2024 vty_event(VTYSH_SERV
, sock
, NULL
);
2027 /* #define VTYSH_DEBUG 1 */
2029 static int vtysh_accept(struct thread
*thread
)
2034 struct sockaddr_un client
;
2037 accept_sock
= THREAD_FD(thread
);
2039 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2041 memset(&client
, 0, sizeof(struct sockaddr_un
));
2042 client_len
= sizeof(struct sockaddr_un
);
2044 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2045 (socklen_t
*)&client_len
);
2048 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2052 if (set_nonblocking(sock
) < 0) {
2054 "vtysh_accept: could not set vty socket %d to non-blocking,"
2056 sock
, safe_strerror(errno
));
2063 printf("VTY shell accept\n");
2064 #endif /* VTYSH_DEBUG */
2069 vty
->type
= VTY_SHELL_SERV
;
2070 vty
->node
= VIEW_NODE
;
2072 vty_event(VTYSH_READ
, sock
, vty
);
2077 static int vtysh_flush(struct vty
*vty
)
2079 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2080 case BUFFER_PENDING
:
2081 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2085 0; /* disable monitoring to avoid infinite recursion */
2086 zlog_warn("%s: write error to fd %d, closing", __func__
,
2088 buffer_reset(vty
->obuf
);
2098 static int vtysh_read(struct thread
*thread
)
2104 unsigned char buf
[VTY_READ_BUFSIZ
];
2106 uint8_t header
[4] = {0, 0, 0, 0};
2108 sock
= THREAD_FD(thread
);
2109 vty
= THREAD_ARG(thread
);
2112 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2114 if (ERRNO_IO_RETRY(errno
)) {
2115 vty_event(VTYSH_READ
, sock
, vty
);
2118 vty
->monitor
= 0; /* disable monitoring to avoid
2119 infinite recursion */
2121 "%s: read failed on vtysh client fd %d, closing: %s",
2122 __func__
, sock
, safe_strerror(errno
));
2124 buffer_reset(vty
->obuf
);
2127 printf("close vtysh\n");
2128 #endif /* VTYSH_DEBUG */
2133 printf("line: %.*s\n", nbytes
, buf
);
2134 #endif /* VTYSH_DEBUG */
2136 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2137 /* Clear command line buffer. */
2138 vty
->cp
= vty
->length
= 0;
2140 vty_out(vty
, "%% Command is too long.\n");
2142 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2143 vty
->buf
[vty
->length
++] = *p
;
2145 /* Pass this line to parser. */
2146 ret
= vty_execute(vty
);
2147 /* Note that vty_execute clears the command buffer and resets
2148 vty->length to 0. */
2150 /* Return result. */
2152 printf("result: %d\n", ret
);
2153 printf("vtysh node: %d\n", vty
->node
);
2154 #endif /* VTYSH_DEBUG */
2156 /* hack for asynchronous "write integrated"
2157 * - other commands in "buf" will be ditched
2158 * - input during pending config-write is
2160 if (ret
== CMD_SUSPEND
)
2163 /* warning: watchfrr hardcodes this result write
2166 buffer_put(vty
->obuf
, header
, 4);
2168 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2169 /* Try to flush results; exit if a write
2176 if (vty
->status
== VTY_CLOSE
)
2179 vty_event(VTYSH_READ
, sock
, vty
);
2184 static int vtysh_write(struct thread
*thread
)
2186 struct vty
*vty
= THREAD_ARG(thread
);
2188 vty
->t_write
= NULL
;
2195 /* Determine address family to bind. */
2196 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2198 /* If port is set to 0, do not listen on TCP/IP at all! */
2200 vty_serv_sock_addrinfo(addr
, port
);
2207 /* Close vty interface. Warning: call this only from functions that
2208 will be careful not to access the vty afterwards (since it has
2209 now been freed). This is safest from top-level functions (called
2210 directly by the thread dispatcher). */
2211 void vty_close(struct vty
*vty
)
2214 bool was_stdio
= false;
2216 /* Cancel threads.*/
2218 thread_cancel(vty
->t_read
);
2220 thread_cancel(vty
->t_write
);
2222 thread_cancel(vty
->t_timeout
);
2225 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2227 /* Free input buffer. */
2228 buffer_free(vty
->obuf
);
2230 /* Free command history. */
2231 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2233 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2237 vector_unset(vtyvec
, vty
->fd
);
2239 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2243 * note check is for fd > STDERR_FILENO, not fd != -1.
2244 * We never close stdin/stdout/stderr here, because we may be
2245 * running in foreground mode with logging to stdout. Also,
2246 * additionally, we'd need to replace these fds with /dev/null. */
2247 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2249 if (vty
->fd
> STDERR_FILENO
) {
2255 XFREE(MTYPE_VTY
, vty
->buf
);
2258 XFREE(MTYPE_VTY
, vty
->error_buf
);
2260 /* Check configure. */
2261 vty_config_unlock(vty
);
2264 XFREE(MTYPE_VTY
, vty
);
2270 /* When time out occur output message then close connection. */
2271 static int vty_timeout(struct thread
*thread
)
2275 vty
= THREAD_ARG(thread
);
2276 vty
->t_timeout
= NULL
;
2280 buffer_reset(vty
->obuf
);
2281 vty_out(vty
, "\nVty connection is timed out.\n");
2283 /* Close connection. */
2284 vty
->status
= VTY_CLOSE
;
2290 /* Read up configuration file from file_name. */
2291 static void vty_read_file(FILE *confp
)
2295 unsigned int line_num
= 0;
2298 /* vty_close won't close stderr; if some config command prints
2299 * something it'll end up there. (not ideal; it'd be beter if output
2300 * from a file-load went to logging instead. Also note that if this
2301 * function is called after daemonizing, stderr will be /dev/null.)
2303 * vty->fd will be -1 from vty_new()
2305 vty
->wfd
= STDERR_FILENO
;
2306 vty
->type
= VTY_FILE
;
2307 vty
->node
= CONFIG_NODE
;
2309 /* Execute configuration file */
2310 ret
= config_from_file(vty
, confp
, &line_num
);
2312 /* Flush any previous errors before printing messages below */
2313 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2315 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2316 const char *message
= NULL
;
2320 case CMD_ERR_AMBIGUOUS
:
2321 message
= "Ambiguous command";
2323 case CMD_ERR_NO_MATCH
:
2324 message
= "No such command";
2327 message
= "Command returned Warning";
2329 case CMD_WARNING_CONFIG_FAILED
:
2330 message
= "Command returned Warning Config Failed";
2332 case CMD_ERR_INCOMPLETE
:
2333 message
= "Command returned Incomplete";
2335 case CMD_ERR_EXEED_ARGC_MAX
:
2337 "Command exceeded maximum number of Arguments";
2340 message
= "Command returned unhandled error message";
2344 nl
= strchr(vty
->error_buf
, '\n');
2347 zlog_err("ERROR: %s on config line %u: %s", message
, line_num
,
2354 static FILE *vty_use_backup_config(const char *fullpath
)
2356 char *fullpath_sav
, *fullpath_tmp
;
2362 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2363 strcpy(fullpath_sav
, fullpath
);
2364 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2366 sav
= open(fullpath_sav
, O_RDONLY
);
2372 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2373 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2375 /* Open file to configuration write. */
2376 tmp
= mkstemp(fullpath_tmp
);
2380 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2383 while ((c
= read(sav
, buffer
, 512)) > 0) {
2384 if (write(tmp
, buffer
, c
) <= 0)
2390 if (rename(fullpath_tmp
, fullpath
) == 0)
2391 ret
= fopen(fullpath
, "r");
2393 unlink(fullpath_tmp
);
2398 unlink(fullpath_tmp
);
2408 /* Read up configuration file from file_name. */
2409 void vty_read_config(const char *config_file
, char *config_default_dir
)
2411 char cwd
[MAXPATHLEN
];
2413 const char *fullpath
;
2416 /* If -f flag specified. */
2417 if (config_file
!= NULL
) {
2418 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2419 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2421 "Failure to determine Current Working Directory %d!",
2425 tmp
= XMALLOC(MTYPE_TMP
,
2426 strlen(cwd
) + strlen(config_file
) + 2);
2427 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2430 fullpath
= config_file
;
2432 confp
= fopen(fullpath
, "r");
2434 if (confp
== NULL
) {
2435 zlog_err("%s: failed to open configuration file %s: %s",
2436 __func__
, fullpath
, safe_strerror(errno
));
2438 confp
= vty_use_backup_config(fullpath
);
2441 "WARNING: using backup configuration file!");
2443 zlog_err("can't open configuration file [%s]",
2450 host_config_set(config_default_dir
);
2454 struct stat conf_stat
;
2456 /* !!!!PLEASE LEAVE!!!!
2457 * This is NEEDED for use with vtysh -b, or else you can get
2458 * a real configuration food fight with a lot garbage in the
2459 * merged configuration file it creates coming from the per
2460 * daemon configuration files. This also allows the daemons
2461 * to start if there default configuration file is not
2462 * present or ignore them, as needed when using vtysh -b to
2463 * configure the daemons at boot - MAG
2466 /* Stat for vtysh Zebra.conf, if found startup and wait for
2467 * boot configuration
2470 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2471 ret
= stat(integrate_default
, &conf_stat
);
2473 goto tmp_free_and_out
;
2476 confp
= fopen(config_default_dir
, "r");
2477 if (confp
== NULL
) {
2478 zlog_err("%s: failed to open configuration file %s: %s",
2479 __func__
, config_default_dir
,
2480 safe_strerror(errno
));
2482 confp
= vty_use_backup_config(config_default_dir
);
2485 "WARNING: using backup configuration file!");
2486 fullpath
= config_default_dir
;
2488 zlog_err("can't open configuration file [%s]",
2489 config_default_dir
);
2490 goto tmp_free_and_out
;
2493 fullpath
= config_default_dir
;
2496 vty_read_file(confp
);
2500 host_config_set(fullpath
);
2504 XFREE(MTYPE_TMP
, tmp
);
2507 /* Small utility function which output log to the VTY. */
2508 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2509 struct timestamp_control
*ctl
, va_list va
)
2517 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2518 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2522 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2528 /* Async-signal-safe version of vty_log for fixed strings. */
2529 void vty_log_fixed(char *buf
, size_t len
)
2532 struct iovec iov
[2];
2533 char crlf
[4] = "\r\n";
2535 /* vty may not have been initialised */
2539 iov
[0].iov_base
= buf
;
2540 iov
[0].iov_len
= len
;
2541 iov
[1].iov_base
= crlf
;
2544 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2546 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2547 /* N.B. We don't care about the return code, since
2549 most likely just about to die anyway. */
2550 if (writev(vty
->wfd
, iov
, 2) == -1) {
2551 fprintf(stderr
, "Failure to writev: %d\n",
2558 int vty_config_lock(struct vty
*vty
)
2560 if (vty_config_is_lockless
)
2562 if (vty_config
== 0) {
2569 int vty_config_unlock(struct vty
*vty
)
2571 if (vty_config_is_lockless
)
2573 if (vty_config
== 1 && vty
->config
== 1) {
2580 void vty_config_lockless(void)
2582 vty_config_is_lockless
= 1;
2585 /* Master of the threads. */
2586 static struct thread_master
*vty_master
;
2588 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2590 struct thread
*vty_serv_thread
= NULL
;
2594 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2596 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2600 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2602 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2606 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2610 vty
->t_write
= NULL
;
2611 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2617 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2619 /* Time out treatment. */
2620 if (vty
->v_timeout
) {
2622 thread_cancel(vty
->t_timeout
);
2623 vty
->t_timeout
= NULL
;
2624 thread_add_timer(vty_master
, vty_timeout
, vty
,
2625 vty
->v_timeout
, &vty
->t_timeout
);
2629 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2632 case VTY_TIMEOUT_RESET
:
2633 if (vty
->t_timeout
) {
2634 thread_cancel(vty
->t_timeout
);
2635 vty
->t_timeout
= NULL
;
2637 if (vty
->v_timeout
) {
2638 vty
->t_timeout
= NULL
;
2639 thread_add_timer(vty_master
, vty_timeout
, vty
,
2640 vty
->v_timeout
, &vty
->t_timeout
);
2646 DEFUN_NOSH (config_who
,
2649 "Display who is on vty\n")
2654 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2655 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2656 vty_out(vty
, "%svty[%d] connected from %s.\n",
2657 v
->config
? "*" : " ", i
, v
->address
);
2661 /* Move to vty configuration mode. */
2662 DEFUN_NOSH (line_vty
,
2665 "Configure a terminal line\n"
2666 "Virtual terminal\n")
2668 vty
->node
= VTY_NODE
;
2672 /* Set time out value. */
2673 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2674 const char *sec_str
)
2676 unsigned long timeout
= 0;
2678 /* min_str and sec_str are already checked by parser. So it must be
2679 all digit string. */
2681 timeout
= strtol(min_str
, NULL
, 10);
2685 timeout
+= strtol(sec_str
, NULL
, 10);
2687 vty_timeout_val
= timeout
;
2688 vty
->v_timeout
= timeout
;
2689 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2695 DEFUN (exec_timeout_min
,
2696 exec_timeout_min_cmd
,
2697 "exec-timeout (0-35791)",
2698 "Set timeout value\n"
2699 "Timeout value in minutes\n")
2702 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2705 DEFUN (exec_timeout_sec
,
2706 exec_timeout_sec_cmd
,
2707 "exec-timeout (0-35791) (0-2147483)",
2708 "Set the EXEC timeout\n"
2709 "Timeout in minutes\n"
2710 "Timeout in seconds\n")
2713 int idx_number_2
= 2;
2714 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2715 argv
[idx_number_2
]->arg
);
2718 DEFUN (no_exec_timeout
,
2719 no_exec_timeout_cmd
,
2722 "Set the EXEC timeout\n")
2724 return exec_timeout(vty
, NULL
, NULL
);
2727 /* Set vty access class. */
2728 DEFUN (vty_access_class
,
2729 vty_access_class_cmd
,
2730 "access-class WORD",
2731 "Filter connections based on an IP access list\n"
2735 if (vty_accesslist_name
)
2736 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2738 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2743 /* Clear vty access class. */
2744 DEFUN (no_vty_access_class
,
2745 no_vty_access_class_cmd
,
2746 "no access-class [WORD]",
2748 "Filter connections based on an IP access list\n"
2752 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2753 if (!vty_accesslist_name
2754 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2755 vty_out(vty
, "Access-class is not currently applied to vty\n");
2756 return CMD_WARNING_CONFIG_FAILED
;
2759 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2761 vty_accesslist_name
= NULL
;
2766 /* Set vty access class. */
2767 DEFUN (vty_ipv6_access_class
,
2768 vty_ipv6_access_class_cmd
,
2769 "ipv6 access-class WORD",
2771 "Filter connections based on an IP access list\n"
2772 "IPv6 access list\n")
2775 if (vty_ipv6_accesslist_name
)
2776 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2778 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2783 /* Clear vty access class. */
2784 DEFUN (no_vty_ipv6_access_class
,
2785 no_vty_ipv6_access_class_cmd
,
2786 "no ipv6 access-class [WORD]",
2789 "Filter connections based on an IP access list\n"
2790 "IPv6 access list\n")
2793 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2795 if (!vty_ipv6_accesslist_name
2796 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2798 "IPv6 access-class is not currently applied to vty\n");
2799 return CMD_WARNING_CONFIG_FAILED
;
2802 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2804 vty_ipv6_accesslist_name
= NULL
;
2813 "Enable password checking\n")
2815 no_password_check
= 0;
2819 DEFUN (no_vty_login
,
2823 "Enable password checking\n")
2825 no_password_check
= 1;
2829 DEFUN (service_advanced_vty
,
2830 service_advanced_vty_cmd
,
2831 "service advanced-vty",
2832 "Set up miscellaneous service\n"
2833 "Enable advanced mode vty interface\n")
2839 DEFUN (no_service_advanced_vty
,
2840 no_service_advanced_vty_cmd
,
2841 "no service advanced-vty",
2843 "Set up miscellaneous service\n"
2844 "Enable advanced mode vty interface\n")
2850 DEFUN_NOSH (terminal_monitor
,
2851 terminal_monitor_cmd
,
2853 "Set terminal line parameters\n"
2854 "Copy debug output to the current terminal line\n")
2860 DEFUN_NOSH (terminal_no_monitor
,
2861 terminal_no_monitor_cmd
,
2862 "terminal no monitor",
2863 "Set terminal line parameters\n"
2865 "Copy debug output to the current terminal line\n")
2871 DEFUN_NOSH (no_terminal_monitor
,
2872 no_terminal_monitor_cmd
,
2873 "no terminal monitor",
2875 "Set terminal line parameters\n"
2876 "Copy debug output to the current terminal line\n")
2878 return terminal_no_monitor(self
, vty
, argc
, argv
);
2882 DEFUN_NOSH (show_history
,
2886 "Display the session command history\n")
2890 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2891 if (index
== VTY_MAXHIST
) {
2896 if (vty
->hist
[index
] != NULL
)
2897 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2906 DEFUN (log_commands
,
2910 "Log all commands (can't be unset without restart)\n")
2912 do_log_commands
= 1;
2916 /* Display current configuration. */
2917 static int vty_config_write(struct vty
*vty
)
2919 vty_out(vty
, "line vty\n");
2921 if (vty_accesslist_name
)
2922 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2924 if (vty_ipv6_accesslist_name
)
2925 vty_out(vty
, " ipv6 access-class %s\n",
2926 vty_ipv6_accesslist_name
);
2929 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2930 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2931 vty_timeout_val
% 60);
2934 if (no_password_check
)
2935 vty_out(vty
, " no login\n");
2937 if (do_log_commands
)
2938 vty_out(vty
, "log commands\n");
2940 vty_out(vty
, "!\n");
2945 struct cmd_node vty_node
= {
2946 VTY_NODE
, "%s(config-line)# ", 1,
2949 /* Reset all VTY status. */
2954 struct thread
*vty_serv_thread
;
2956 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2957 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2958 buffer_reset(vty
->obuf
);
2959 vty
->status
= VTY_CLOSE
;
2963 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
2964 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
2966 thread_cancel(vty_serv_thread
);
2967 vector_slot(Vvty_serv_thread
, i
) = NULL
;
2971 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2973 if (vty_accesslist_name
) {
2974 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2975 vty_accesslist_name
= NULL
;
2978 if (vty_ipv6_accesslist_name
) {
2979 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2980 vty_ipv6_accesslist_name
= NULL
;
2984 static void vty_save_cwd(void)
2986 char cwd
[MAXPATHLEN
];
2989 c
= getcwd(cwd
, MAXPATHLEN
);
2993 * At this point if these go wrong, more than likely
2994 * the whole world is coming down around us
2995 * Hence not worrying about it too much.
2997 if (!chdir(SYSCONFDIR
)) {
2998 zlog_err("Failure to chdir to %s, errno: %d",
3002 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3003 zlog_err("Failure to getcwd, errno: %d", errno
);
3008 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3009 strcpy(vty_cwd
, cwd
);
3017 int vty_shell(struct vty
*vty
)
3019 return vty
->type
== VTY_SHELL
? 1 : 0;
3022 int vty_shell_serv(struct vty
*vty
)
3024 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3027 void vty_init_vtysh()
3029 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3032 /* Install vty's own commands like `who' command. */
3033 void vty_init(struct thread_master
*master_thread
)
3035 /* For further configuration read, preserve current directory. */
3038 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3040 vty_master
= master_thread
;
3042 atexit(vty_stdio_atexit
);
3044 /* Initilize server thread vector. */
3045 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3047 /* Install bgp top node. */
3048 install_node(&vty_node
, vty_config_write
);
3050 install_element(VIEW_NODE
, &config_who_cmd
);
3051 install_element(VIEW_NODE
, &show_history_cmd
);
3052 install_element(CONFIG_NODE
, &line_vty_cmd
);
3053 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3054 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3055 install_element(CONFIG_NODE
, &show_history_cmd
);
3056 install_element(CONFIG_NODE
, &log_commands_cmd
);
3057 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3058 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3059 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3061 install_default(VTY_NODE
);
3062 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3063 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3064 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3065 install_element(VTY_NODE
, &vty_access_class_cmd
);
3066 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3067 install_element(VTY_NODE
, &vty_login_cmd
);
3068 install_element(VTY_NODE
, &no_vty_login_cmd
);
3069 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3070 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3073 void vty_terminate(void)
3076 XFREE(MTYPE_TMP
, vty_cwd
);
3078 if (vtyvec
&& Vvty_serv_thread
) {
3080 vector_free(vtyvec
);
3081 vector_free(Vvty_serv_thread
);
3083 Vvty_serv_thread
= NULL
;