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>
33 #include "sockunion.h"
43 #include "lib_errors.h"
45 #include <arpa/telnet.h>
48 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
49 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
50 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
65 static void vty_event(enum event
, int, struct vty
*);
67 /* Extern host structure from command.c */
68 extern struct host host
;
70 /* Vector which store each vty structure. */
73 /* Vty timeout value. */
74 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
76 /* Vty access-class command */
77 static char *vty_accesslist_name
= NULL
;
79 /* Vty access-calss for IPv6. */
80 static char *vty_ipv6_accesslist_name
= NULL
;
82 /* VTY server thread. */
83 static vector Vvty_serv_thread
;
85 /* Current directory. */
89 static int vty_config
;
90 static int vty_config_is_lockless
= 0;
92 /* Login password check. */
93 static int no_password_check
= 0;
95 /* Integrated configuration file path */
96 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
98 static int do_log_commands
= 0;
100 void vty_frame(struct vty
*vty
, const char *format
, ...)
104 va_start(args
, format
);
105 vsnprintf(vty
->frame
+ vty
->frame_pos
,
106 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
107 vty
->frame_pos
= strlen(vty
->frame
);
111 void vty_endframe(struct vty
*vty
, const char *endtext
)
113 if (vty
->frame_pos
== 0 && endtext
)
114 vty_out(vty
, "%s", endtext
);
118 bool vty_set_include(struct vty
*vty
, const char *regexp
)
126 regfree(&vty
->include
);
132 errcode
= regcomp(&vty
->include
, regexp
,
133 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
136 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
137 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
145 /* VTY standard output function. */
146 int vty_out(struct vty
*vty
, const char *format
, ...)
155 if (vty
->frame_pos
) {
157 vty_out(vty
, "%s", vty
->frame
);
160 /* Try to write to initial buffer. */
161 va_start(args
, format
);
162 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
165 /* Initial buffer is not enough. */
166 if (len
< 0 || len
>= size
) {
173 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
177 va_start(args
, format
);
178 len
= vsnprintf(p
, size
, format
, args
);
181 if (len
> -1 && len
< size
)
186 /* When initial buffer is enough to store all output. */
192 vector lines
= frrstr_split_vec(p
, "\n");
194 /* Place first value in the cache */
195 char *firstline
= vector_slot(lines
, 0);
196 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
198 /* If our split returned more than one entry, time to filter */
199 if (vector_active(lines
) > 1) {
201 * returned string is MTYPE_TMP so it matches the MTYPE
202 * of everything else in the vector
204 char *bstr
= buffer_getstr(vty
->lbuf
);
205 buffer_reset(vty
->lbuf
);
206 XFREE(MTYPE_TMP
, lines
->index
[0]);
207 vector_set_index(lines
, 0, bstr
);
208 frrstr_filter_vec(lines
, &vty
->include
);
209 vector_compact(lines
);
211 * Consider the string "foo\n". If the regex is an empty string
212 * and the line ended with a newline, then the vector will look
218 * If the regex isn't empty, the vector will look like:
222 * In this case we'd like to preserve the newline, so we add
223 * the empty string [1] as in the first example.
225 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
226 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
227 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
229 filtered
= frrstr_join_vec(lines
, "\n");
235 frrstr_strvec_free(lines
);
246 /* print with crlf replacement */
247 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
251 fprintf(vty
->of
, "%s", filtered
);
257 /* print without crlf replacement */
258 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
264 if (vty
->filter
&& filtered
)
265 XFREE(MTYPE_TMP
, filtered
);
267 /* If p is not different with buf, it is allocated buffer. */
269 XFREE(MTYPE_VTY_OUT_BUF
, p
);
274 static int vty_log_out(struct vty
*vty
, const char *level
,
275 const char *proto_str
, const char *format
,
276 struct timestamp_control
*ctl
, va_list va
)
282 if (!ctl
->already_rendered
) {
283 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
285 ctl
->already_rendered
= 1;
287 if (ctl
->len
+ 1 >= sizeof(buf
))
289 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
294 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
297 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
298 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
301 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
302 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
308 if (write(vty
->wfd
, buf
, len
) < 0) {
309 if (ERRNO_IO_RETRY(errno
))
310 /* Kernel buffer is full, probably too much debugging
312 drop the data and ignore. */
314 /* Fatal I/O error. */
316 0; /* disable monitoring to avoid infinite recursion */
317 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
318 __func__
, vty
->fd
, safe_strerror(errno
));
319 buffer_reset(vty
->obuf
);
320 buffer_reset(vty
->lbuf
);
321 /* cannot call vty_close, because a parent routine may still try
322 to access the vty struct */
323 vty
->status
= VTY_CLOSE
;
324 shutdown(vty
->fd
, SHUT_RDWR
);
330 /* Output current time to the vty. */
331 void vty_time_print(struct vty
*vty
, int cr
)
333 char buf
[QUAGGA_TIMESTAMP_LEN
];
335 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
336 zlog_info("quagga_timestamp error");
340 vty_out(vty
, "%s\n", buf
);
342 vty_out(vty
, "%s ", buf
);
347 /* Say hello to vty interface. */
348 void vty_hello(struct vty
*vty
)
354 f
= fopen(host
.motdfile
, "r");
356 while (fgets(buf
, sizeof(buf
), f
)) {
358 /* work backwards to ignore trailling isspace()
360 for (s
= buf
+ strlen(buf
);
361 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
364 vty_out(vty
, "%s\n", buf
);
368 vty_out(vty
, "MOTD file not found\n");
369 } else if (host
.motd
)
370 vty_out(vty
, "%s", host
.motd
);
373 /* Put out prompt and wait input from user. */
374 static void vty_prompt(struct vty
*vty
)
376 if (vty
->type
== VTY_TERM
) {
377 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
381 /* Send WILL TELOPT_ECHO to remote server. */
382 static void vty_will_echo(struct vty
*vty
)
384 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
385 vty_out(vty
, "%s", cmd
);
388 /* Make suppress Go-Ahead telnet option. */
389 static void vty_will_suppress_go_ahead(struct vty
*vty
)
391 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
392 vty_out(vty
, "%s", cmd
);
395 /* Make don't use linemode over telnet. */
396 static void vty_dont_linemode(struct vty
*vty
)
398 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
399 vty_out(vty
, "%s", cmd
);
402 /* Use window size. */
403 static void vty_do_window_size(struct vty
*vty
)
405 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
406 vty_out(vty
, "%s", cmd
);
409 #if 0 /* Currently not used. */
410 /* Make don't use lflow vty interface. */
412 vty_dont_lflow_ahead (struct vty
*vty
)
414 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
415 vty_out (vty
, "%s", cmd
);
419 /* Authentication of vty */
420 static void vty_auth(struct vty
*vty
, char *buf
)
423 enum node_type next_node
= 0;
425 char *crypt(const char *, const char *);
430 passwd
= host
.password_encrypt
;
432 passwd
= host
.password
;
434 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
436 next_node
= VIEW_NODE
;
438 case AUTH_ENABLE_NODE
:
440 passwd
= host
.enable_encrypt
;
442 passwd
= host
.enable
;
443 next_node
= ENABLE_NODE
;
449 fail
= strcmp(crypt(buf
, passwd
), passwd
);
451 fail
= strcmp(buf
, passwd
);
457 vty
->node
= next_node
; /* Success ! */
460 if (vty
->fail
>= 3) {
461 if (vty
->node
== AUTH_NODE
) {
463 "%% Bad passwords, too many failures!\n");
464 vty
->status
= VTY_CLOSE
;
466 /* AUTH_ENABLE_NODE */
469 "%% Bad enable passwords, too many failures!\n");
470 vty
->status
= VTY_CLOSE
;
476 /* Command execution over the vty interface. */
477 static int vty_command(struct vty
*vty
, char *buf
)
480 const char *protocolname
;
486 * Log non empty command lines
491 /* Skip white spaces. */
492 while (isspace((int)*cp
) && *cp
!= '\0')
495 if (cp
!= NULL
&& *cp
!= '\0') {
497 char vty_str
[VTY_BUFSIZ
];
498 char prompt_str
[VTY_BUFSIZ
];
500 /* format the base vty info */
501 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
503 for (i
= 0; i
< vector_active(vtyvec
); i
++)
504 if (vty
== vector_slot(vtyvec
, i
)) {
505 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
510 /* format the prompt */
511 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
514 /* now log the command */
515 zlog_notice("%s%s", prompt_str
, buf
);
518 #ifdef CONSUMED_TIME_CHECK
522 unsigned long realtime
, cputime
;
525 #endif /* CONSUMED_TIME_CHECK */
527 ret
= cmd_execute(vty
, buf
, NULL
, 0);
529 /* Get the name of the protocol if any */
530 protocolname
= frr_protoname
;
532 #ifdef CONSUMED_TIME_CHECK
534 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
535 > CONSUMED_TIME_CHECK
)
536 /* Warn about CPU hog that must be fixed. */
538 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
539 realtime
/ 1000, cputime
/ 1000, buf
);
541 #endif /* CONSUMED_TIME_CHECK */
543 if (ret
!= CMD_SUCCESS
)
546 if (vty
->type
== VTY_FILE
)
547 vty_out(vty
, "Warning...\n");
549 case CMD_ERR_AMBIGUOUS
:
550 vty_out(vty
, "%% Ambiguous command.\n");
552 case CMD_ERR_NO_MATCH
:
553 vty_out(vty
, "%% [%s] Unknown command: %s\n",
556 case CMD_ERR_INCOMPLETE
:
557 vty_out(vty
, "%% Command incomplete.\n");
564 static const char telnet_backward_char
= 0x08;
565 static const char telnet_space_char
= ' ';
567 /* Basic function to write buffer to vty. */
568 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
570 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
573 /* Should we do buffering here ? And make vty_flush (vty) ? */
574 buffer_put(vty
->obuf
, buf
, nbytes
);
577 /* Basic function to insert character into vty. */
578 static void vty_self_insert(struct vty
*vty
, char c
)
583 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
586 length
= vty
->length
- vty
->cp
;
587 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
588 vty
->buf
[vty
->cp
] = c
;
590 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
591 for (i
= 0; i
< length
; i
++)
592 vty_write(vty
, &telnet_backward_char
, 1);
597 vty
->buf
[vty
->length
] = '\0';
600 /* Self insert character 'c' in overwrite mode. */
601 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
603 if (vty
->cp
== vty
->length
) {
604 vty_self_insert(vty
, c
);
608 vty
->buf
[vty
->cp
++] = c
;
609 vty_write(vty
, &c
, 1);
613 * Insert a string into vty->buf at the current cursor position.
615 * If the resultant string would be larger than VTY_BUFSIZ it is
618 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
620 if (vty
->cp
== VTY_BUFSIZ
)
623 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
624 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
626 vty
->length
= MAX(vty
->cp
, vty
->length
);
627 vty
->buf
[vty
->length
] = '\0';
628 vty_write(vty
, str
, nwrite
);
631 /* Forward character. */
632 static void vty_forward_char(struct vty
*vty
)
634 if (vty
->cp
< vty
->length
) {
635 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
640 /* Backward character. */
641 static void vty_backward_char(struct vty
*vty
)
645 vty_write(vty
, &telnet_backward_char
, 1);
649 /* Move to the beginning of the line. */
650 static void vty_beginning_of_line(struct vty
*vty
)
653 vty_backward_char(vty
);
656 /* Move to the end of the line. */
657 static void vty_end_of_line(struct vty
*vty
)
659 while (vty
->cp
< vty
->length
)
660 vty_forward_char(vty
);
663 static void vty_kill_line_from_beginning(struct vty
*);
664 static void vty_redraw_line(struct vty
*);
666 /* Print command line history. This function is called from
667 vty_next_line and vty_previous_line. */
668 static void vty_history_print(struct vty
*vty
)
672 vty_kill_line_from_beginning(vty
);
674 /* Get previous line from history buffer */
675 length
= strlen(vty
->hist
[vty
->hp
]);
676 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
677 vty
->cp
= vty
->length
= length
;
678 vty
->buf
[vty
->length
] = '\0';
680 /* Redraw current line */
681 vty_redraw_line(vty
);
684 /* Show next command line history. */
685 static void vty_next_line(struct vty
*vty
)
689 if (vty
->hp
== vty
->hindex
)
692 /* Try is there history exist or not. */
694 if (try_index
== (VTY_MAXHIST
- 1))
699 /* If there is not history return. */
700 if (vty
->hist
[try_index
] == NULL
)
705 vty_history_print(vty
);
708 /* Show previous command line history. */
709 static void vty_previous_line(struct vty
*vty
)
715 try_index
= VTY_MAXHIST
- 1;
719 if (vty
->hist
[try_index
] == NULL
)
724 vty_history_print(vty
);
727 /* This function redraw all of the command line character. */
728 static void vty_redraw_line(struct vty
*vty
)
730 vty_write(vty
, vty
->buf
, vty
->length
);
731 vty
->cp
= vty
->length
;
735 static void vty_forward_word(struct vty
*vty
)
737 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
738 vty_forward_char(vty
);
740 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
741 vty_forward_char(vty
);
744 /* Backward word without skipping training space. */
745 static void vty_backward_pure_word(struct vty
*vty
)
747 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
748 vty_backward_char(vty
);
752 static void vty_backward_word(struct vty
*vty
)
754 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
755 vty_backward_char(vty
);
757 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
758 vty_backward_char(vty
);
761 /* When '^D' is typed at the beginning of the line we move to the down
763 static void vty_down_level(struct vty
*vty
)
771 /* When '^Z' is received from vty, move down to the enable mode. */
772 static void vty_end_config(struct vty
*vty
)
791 case BGP_VRF_POLICY_NODE
:
792 case BGP_VNC_DEFAULTS_NODE
:
793 case BGP_VNC_NVE_GROUP_NODE
:
794 case BGP_VNC_L2_GROUP_NODE
:
809 case LDP_IPV4_IFACE_NODE
:
810 case LDP_IPV6_IFACE_NODE
:
812 case LDP_PSEUDOWIRE_NODE
:
815 case KEYCHAIN_KEY_NODE
:
817 case BGP_EVPN_VNI_NODE
:
820 vty_config_unlock(vty
);
821 vty
->node
= ENABLE_NODE
;
824 /* Unknown node, we have to ignore it. */
832 /* Delete a charcter at the current point. */
833 static void vty_delete_char(struct vty
*vty
)
838 if (vty
->length
== 0) {
843 if (vty
->cp
== vty
->length
)
844 return; /* completion need here? */
846 size
= vty
->length
- vty
->cp
;
849 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
850 vty
->buf
[vty
->length
] = '\0';
852 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
855 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
856 vty_write(vty
, &telnet_space_char
, 1);
858 for (i
= 0; i
< size
; i
++)
859 vty_write(vty
, &telnet_backward_char
, 1);
862 /* Delete a character before the point. */
863 static void vty_delete_backward_char(struct vty
*vty
)
868 vty_backward_char(vty
);
869 vty_delete_char(vty
);
872 /* Kill rest of line from current point. */
873 static void vty_kill_line(struct vty
*vty
)
878 size
= vty
->length
- vty
->cp
;
883 for (i
= 0; i
< size
; i
++)
884 vty_write(vty
, &telnet_space_char
, 1);
885 for (i
= 0; i
< size
; i
++)
886 vty_write(vty
, &telnet_backward_char
, 1);
888 memset(&vty
->buf
[vty
->cp
], 0, size
);
889 vty
->length
= vty
->cp
;
892 /* Kill line from the beginning. */
893 static void vty_kill_line_from_beginning(struct vty
*vty
)
895 vty_beginning_of_line(vty
);
899 /* Delete a word before the point. */
900 static void vty_forward_kill_word(struct vty
*vty
)
902 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
903 vty_delete_char(vty
);
904 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
905 vty_delete_char(vty
);
908 /* Delete a word before the point. */
909 static void vty_backward_kill_word(struct vty
*vty
)
911 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
912 vty_delete_backward_char(vty
);
913 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
914 vty_delete_backward_char(vty
);
917 /* Transpose chars before or at the point. */
918 static void vty_transpose_chars(struct vty
*vty
)
922 /* If length is short or point is near by the beginning of line then
924 if (vty
->length
< 2 || vty
->cp
< 1)
927 /* In case of point is located at the end of the line. */
928 if (vty
->cp
== vty
->length
) {
929 c1
= vty
->buf
[vty
->cp
- 1];
930 c2
= vty
->buf
[vty
->cp
- 2];
932 vty_backward_char(vty
);
933 vty_backward_char(vty
);
934 vty_self_insert_overwrite(vty
, c1
);
935 vty_self_insert_overwrite(vty
, c2
);
937 c1
= vty
->buf
[vty
->cp
];
938 c2
= vty
->buf
[vty
->cp
- 1];
940 vty_backward_char(vty
);
941 vty_self_insert_overwrite(vty
, c1
);
942 vty_self_insert_overwrite(vty
, c2
);
946 /* Do completion at vty interface. */
947 static void vty_complete_command(struct vty
*vty
)
951 char **matched
= NULL
;
954 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
957 vline
= cmd_make_strvec(vty
->buf
);
961 /* In case of 'help \t'. */
962 if (isspace((int)vty
->buf
[vty
->length
- 1]))
963 vector_set(vline
, NULL
);
965 matched
= cmd_complete_command(vline
, vty
, &ret
);
967 cmd_free_strvec(vline
);
971 case CMD_ERR_AMBIGUOUS
:
972 vty_out(vty
, "%% Ambiguous command.\n");
974 vty_redraw_line(vty
);
976 case CMD_ERR_NO_MATCH
:
977 /* vty_out (vty, "%% There is no matched command.\n"); */
979 vty_redraw_line(vty
);
981 case CMD_COMPLETE_FULL_MATCH
:
983 /* 2016-11-28 equinox -- need to debug, SEGV here */
984 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
986 vty_redraw_line(vty
);
990 vty_redraw_line(vty
);
991 vty_backward_pure_word(vty
);
992 vty_insert_word_overwrite(vty
, matched
[0]);
993 vty_self_insert(vty
, ' ');
994 XFREE(MTYPE_COMPLETION
, matched
[0]);
996 case CMD_COMPLETE_MATCH
:
998 vty_redraw_line(vty
);
999 vty_backward_pure_word(vty
);
1000 vty_insert_word_overwrite(vty
, matched
[0]);
1001 XFREE(MTYPE_COMPLETION
, matched
[0]);
1003 case CMD_COMPLETE_LIST_MATCH
:
1004 for (i
= 0; matched
[i
] != NULL
; i
++) {
1005 if (i
!= 0 && ((i
% 6) == 0))
1007 vty_out(vty
, "%-10s ", matched
[i
]);
1008 XFREE(MTYPE_COMPLETION
, matched
[i
]);
1013 vty_redraw_line(vty
);
1015 case CMD_ERR_NOTHING_TODO
:
1017 vty_redraw_line(vty
);
1023 XFREE(MTYPE_TMP
, matched
);
1026 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
1027 unsigned int desc_width
, struct cmd_token
*token
)
1030 const char *cmd
, *p
;
1035 if (desc_width
<= 0) {
1036 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1040 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1042 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1043 for (pos
= desc_width
; pos
> 0; pos
--)
1044 if (*(p
+ pos
) == ' ')
1050 strncpy(buf
, p
, pos
);
1052 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1057 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1059 XFREE(MTYPE_TMP
, buf
);
1062 /* Describe matched command function. */
1063 static void vty_describe_command(struct vty
*vty
)
1068 unsigned int i
, width
, desc_width
;
1069 struct cmd_token
*token
, *token_cr
= NULL
;
1071 vline
= cmd_make_strvec(vty
->buf
);
1073 /* In case of '> ?'. */
1074 if (vline
== NULL
) {
1075 vline
= vector_init(1);
1076 vector_set(vline
, NULL
);
1077 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1078 vector_set(vline
, NULL
);
1080 describe
= cmd_describe_command(vline
, vty
, &ret
);
1084 /* Ambiguous error. */
1086 case CMD_ERR_AMBIGUOUS
:
1087 vty_out(vty
, "%% Ambiguous command.\n");
1090 case CMD_ERR_NO_MATCH
:
1091 vty_out(vty
, "%% There is no matched command.\n");
1096 /* Get width of command string. */
1098 for (i
= 0; i
< vector_active(describe
); i
++)
1099 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1102 if (token
->text
[0] == '\0')
1105 len
= strlen(token
->text
);
1111 /* Get width of description string. */
1112 desc_width
= vty
->width
- (width
+ 6);
1114 /* Print out description. */
1115 for (i
= 0; i
< vector_active(describe
); i
++)
1116 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1117 if (token
->text
[0] == '\0')
1120 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1126 vty_out(vty
, " %-s\n", token
->text
);
1127 else if (desc_width
>= strlen(token
->desc
))
1128 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1131 vty_describe_fold(vty
, width
, desc_width
,
1134 if (IS_VARYING_TOKEN(token
->type
)) {
1135 const char *ref
= vector_slot(
1136 vline
, vector_active(vline
) - 1);
1138 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1139 cmd_variable_complete(token
, ref
, varcomps
);
1141 if (vector_active(varcomps
) > 0) {
1142 char *ac
= cmd_variable_comp2str(
1143 varcomps
, vty
->width
);
1144 vty_out(vty
, "%s\n", ac
);
1145 XFREE(MTYPE_TMP
, ac
);
1148 vector_free(varcomps
);
1151 vty_out (vty
, " %-*s %s\n", width
1152 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1153 desc
->str
? desc
->str
: "");
1157 if ((token
= token_cr
)) {
1159 vty_out(vty
, " %-s\n", token
->text
);
1160 else if (desc_width
>= strlen(token
->desc
))
1161 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1164 vty_describe_fold(vty
, width
, desc_width
, token
);
1168 cmd_free_strvec(vline
);
1170 vector_free(describe
);
1173 vty_redraw_line(vty
);
1176 static void vty_clear_buf(struct vty
*vty
)
1178 memset(vty
->buf
, 0, vty
->max
);
1181 /* ^C stop current input and do not add command line to the history. */
1182 static void vty_stop_input(struct vty
*vty
)
1184 vty
->cp
= vty
->length
= 0;
1188 switch (vty
->node
) {
1191 /* Nothing to do. */
1194 case INTERFACE_NODE
:
1208 case LDP_IPV4_IFACE_NODE
:
1209 case LDP_IPV6_IFACE_NODE
:
1210 case LDP_L2VPN_NODE
:
1211 case LDP_PSEUDOWIRE_NODE
:
1214 case KEYCHAIN_KEY_NODE
:
1218 vty_config_unlock(vty
);
1219 vty
->node
= ENABLE_NODE
;
1222 /* Unknown node, we have to ignore it. */
1227 /* Set history pointer to the latest one. */
1228 vty
->hp
= vty
->hindex
;
1231 /* Add current command line to the history buffer. */
1232 static void vty_hist_add(struct vty
*vty
)
1236 if (vty
->length
== 0)
1239 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1241 /* Ignore the same string as previous one. */
1242 if (vty
->hist
[index
])
1243 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1244 vty
->hp
= vty
->hindex
;
1248 /* Insert history entry. */
1249 if (vty
->hist
[vty
->hindex
])
1250 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1251 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1253 /* History index rotation. */
1255 if (vty
->hindex
== VTY_MAXHIST
)
1258 vty
->hp
= vty
->hindex
;
1261 /* #define TELNET_OPTION_DEBUG */
1263 /* Get telnet window size. */
1264 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1266 #ifdef TELNET_OPTION_DEBUG
1269 for (i
= 0; i
< nbytes
; i
++) {
1272 vty_out(vty
, "IAC ");
1275 vty_out(vty
, "WILL ");
1278 vty_out(vty
, "WONT ");
1281 vty_out(vty
, "DO ");
1284 vty_out(vty
, "DONT ");
1287 vty_out(vty
, "SB ");
1290 vty_out(vty
, "SE ");
1293 vty_out(vty
, "TELOPT_ECHO \n");
1296 vty_out(vty
, "TELOPT_SGA \n");
1299 vty_out(vty
, "TELOPT_NAWS \n");
1302 vty_out(vty
, "%x ", buf
[i
]);
1308 #endif /* TELNET_OPTION_DEBUG */
1313 vty
->iac_sb_in_progress
= 1;
1317 if (!vty
->iac_sb_in_progress
)
1320 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1321 vty
->iac_sb_in_progress
= 0;
1324 switch (vty
->sb_buf
[0]) {
1326 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1328 "RFC 1073 violation detected: telnet NAWS option "
1329 "should send %d characters, but we received %lu",
1331 (unsigned long)vty
->sb_len
);
1332 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1334 LIB_ERR_DEVELOPMENT
,
1335 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1336 (unsigned long)sizeof(vty
->sb_buf
),
1337 TELNET_NAWS_SB_LEN
);
1339 vty
->width
= ((vty
->sb_buf
[1] << 8)
1341 vty
->height
= ((vty
->sb_buf
[3] << 8)
1343 #ifdef TELNET_OPTION_DEBUG
1345 "TELNET NAWS window size negotiation completed: "
1346 "width %d, height %d\n",
1347 vty
->width
, vty
->height
);
1352 vty
->iac_sb_in_progress
= 0;
1362 /* Execute current command line. */
1363 static int vty_execute(struct vty
*vty
)
1369 switch (vty
->node
) {
1371 case AUTH_ENABLE_NODE
:
1372 vty_auth(vty
, vty
->buf
);
1375 ret
= vty_command(vty
, vty
->buf
);
1376 if (vty
->type
== VTY_TERM
)
1381 /* Clear command line buffer. */
1382 vty
->cp
= vty
->length
= 0;
1385 if (vty
->status
!= VTY_CLOSE
)
1391 #define CONTROL(X) ((X) - '@')
1392 #define VTY_NORMAL 0
1393 #define VTY_PRE_ESCAPE 1
1394 #define VTY_ESCAPE 2
1396 /* Escape character command map. */
1397 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1401 vty_previous_line(vty
);
1407 vty_forward_char(vty
);
1410 vty_backward_char(vty
);
1416 /* Go back to normal mode. */
1417 vty
->escape
= VTY_NORMAL
;
1420 /* Quit print out to the buffer. */
1421 static void vty_buffer_reset(struct vty
*vty
)
1423 buffer_reset(vty
->obuf
);
1424 buffer_reset(vty
->lbuf
);
1426 vty_redraw_line(vty
);
1429 /* Read data via vty socket. */
1430 static int vty_read(struct thread
*thread
)
1434 unsigned char buf
[VTY_READ_BUFSIZ
];
1436 int vty_sock
= THREAD_FD(thread
);
1437 struct vty
*vty
= THREAD_ARG(thread
);
1440 /* Read raw data from socket */
1441 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1443 if (ERRNO_IO_RETRY(errno
)) {
1444 vty_event(VTY_READ
, vty_sock
, vty
);
1447 vty
->monitor
= 0; /* disable monitoring to avoid
1448 infinite recursion */
1450 "%s: read error on vty client fd %d, closing: %s",
1451 __func__
, vty
->fd
, safe_strerror(errno
));
1452 buffer_reset(vty
->obuf
);
1453 buffer_reset(vty
->lbuf
);
1455 vty
->status
= VTY_CLOSE
;
1458 for (i
= 0; i
< nbytes
; i
++) {
1459 if (buf
[i
] == IAC
) {
1468 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1469 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1470 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1476 /* In case of telnet command */
1478 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1485 if (vty
->status
== VTY_MORE
) {
1490 vty_buffer_reset(vty
);
1492 #if 0 /* More line does not work for "show ip bgp". */
1495 vty
->status
= VTY_MORELINE
;
1504 /* Escape character. */
1505 if (vty
->escape
== VTY_ESCAPE
) {
1506 vty_escape_map(buf
[i
], vty
);
1510 /* Pre-escape status. */
1511 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1514 vty
->escape
= VTY_ESCAPE
;
1517 vty_backward_word(vty
);
1518 vty
->escape
= VTY_NORMAL
;
1521 vty_forward_word(vty
);
1522 vty
->escape
= VTY_NORMAL
;
1525 vty_forward_kill_word(vty
);
1526 vty
->escape
= VTY_NORMAL
;
1530 vty_backward_kill_word(vty
);
1531 vty
->escape
= VTY_NORMAL
;
1534 vty
->escape
= VTY_NORMAL
;
1542 vty_beginning_of_line(vty
);
1545 vty_backward_char(vty
);
1548 vty_stop_input(vty
);
1551 vty_delete_char(vty
);
1554 vty_end_of_line(vty
);
1557 vty_forward_char(vty
);
1561 vty_delete_backward_char(vty
);
1570 vty_previous_line(vty
);
1573 vty_transpose_chars(vty
);
1576 vty_kill_line_from_beginning(vty
);
1579 vty_backward_kill_word(vty
);
1582 vty_end_config(vty
);
1590 vty_complete_command(vty
);
1593 if (vty
->node
== AUTH_NODE
1594 || vty
->node
== AUTH_ENABLE_NODE
)
1595 vty_self_insert(vty
, buf
[i
]);
1597 vty_describe_command(vty
);
1600 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1601 vty
->escape
= VTY_ESCAPE
;
1604 vty
->escape
= VTY_PRE_ESCAPE
;
1607 if (buf
[i
] > 31 && buf
[i
] < 127)
1608 vty_self_insert(vty
, buf
[i
]);
1614 if (vty
->status
== VTY_CLOSE
)
1617 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1618 vty_event(VTY_READ
, vty_sock
, vty
);
1623 /* Flush buffer to the vty. */
1624 static int vty_flush(struct thread
*thread
)
1627 buffer_status_t flushrc
;
1628 int vty_sock
= THREAD_FD(thread
);
1629 struct vty
*vty
= THREAD_ARG(thread
);
1631 vty
->t_write
= NULL
;
1633 /* Tempolary disable read thread. */
1634 if ((vty
->lines
== 0) && vty
->t_read
) {
1635 thread_cancel(vty
->t_read
);
1639 /* Function execution continue. */
1640 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1642 /* N.B. if width is 0, that means we don't know the window size. */
1643 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1644 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1645 else if (vty
->status
== VTY_MORELINE
)
1646 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1649 flushrc
= buffer_flush_window(
1650 vty
->obuf
, vty_sock
, vty
->width
,
1651 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1655 0; /* disable monitoring to avoid infinite recursion */
1656 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1658 buffer_reset(vty
->lbuf
);
1659 buffer_reset(vty
->obuf
);
1663 if (vty
->status
== VTY_CLOSE
)
1666 vty
->status
= VTY_NORMAL
;
1667 if (vty
->lines
== 0)
1668 vty_event(VTY_READ
, vty_sock
, vty
);
1671 case BUFFER_PENDING
:
1672 /* There is more data waiting to be written. */
1673 vty
->status
= VTY_MORE
;
1674 if (vty
->lines
== 0)
1675 vty_event(VTY_WRITE
, vty_sock
, vty
);
1682 /* Allocate new vty struct. */
1683 struct vty
*vty_new()
1685 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1687 new->fd
= new->wfd
= -1;
1689 new->lbuf
= buffer_new(0);
1690 new->obuf
= buffer_new(0); /* Use default buffer size. */
1691 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1692 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1693 new->max
= VTY_BUFSIZ
;
1699 /* allocate and initialise vty */
1700 static struct vty
*vty_new_init(int vty_sock
)
1706 vty
->wfd
= vty_sock
;
1707 vty
->type
= VTY_TERM
;
1708 vty
->node
= AUTH_NODE
;
1713 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1716 vector_set_index(vtyvec
, vty_sock
, vty
);
1717 vty
->status
= VTY_NORMAL
;
1720 vty
->iac_sb_in_progress
= 0;
1726 /* Create new vty structure. */
1727 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1729 char buf
[SU_ADDRSTRLEN
];
1732 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1734 /* Allocate new vty structure and set up default values. */
1735 vty
= vty_new_init(vty_sock
);
1737 /* configurable parameters not part of basic init */
1738 vty
->v_timeout
= vty_timeout_val
;
1739 strcpy(vty
->address
, buf
);
1740 if (no_password_check
) {
1742 vty
->node
= ENABLE_NODE
;
1744 vty
->node
= VIEW_NODE
;
1746 if (host
.lines
>= 0)
1747 vty
->lines
= host
.lines
;
1749 if (!no_password_check
) {
1750 /* Vty is not available if password isn't set. */
1751 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1752 vty_out(vty
, "Vty password is not set.\n");
1753 vty
->status
= VTY_CLOSE
;
1759 /* Say hello to the world. */
1761 if (!no_password_check
)
1762 vty_out(vty
, "\nUser Access Verification\n\n");
1764 /* Setting up terminal. */
1766 vty_will_suppress_go_ahead(vty
);
1768 vty_dont_linemode(vty
);
1769 vty_do_window_size(vty
);
1770 /* vty_dont_lflow_ahead (vty); */
1774 /* Add read/write thread. */
1775 vty_event(VTY_WRITE
, vty_sock
, vty
);
1776 vty_event(VTY_READ
, vty_sock
, vty
);
1781 /* create vty for stdio */
1782 static struct termios stdio_orig_termios
;
1783 static struct vty
*stdio_vty
= NULL
;
1784 static bool stdio_termios
= false;
1785 static void (*stdio_vty_atclose
)(int isexit
);
1787 static void vty_stdio_reset(int isexit
)
1791 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1792 stdio_termios
= false;
1796 if (stdio_vty_atclose
)
1797 stdio_vty_atclose(isexit
);
1798 stdio_vty_atclose
= NULL
;
1802 static void vty_stdio_atexit(void)
1807 void vty_stdio_suspend(void)
1812 if (stdio_vty
->t_write
)
1813 thread_cancel(stdio_vty
->t_write
);
1814 if (stdio_vty
->t_read
)
1815 thread_cancel(stdio_vty
->t_read
);
1816 if (stdio_vty
->t_timeout
)
1817 thread_cancel(stdio_vty
->t_timeout
);
1820 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1821 stdio_termios
= false;
1824 void vty_stdio_resume(void)
1829 if (!tcgetattr(0, &stdio_orig_termios
)) {
1830 struct termios termios
;
1832 termios
= stdio_orig_termios
;
1833 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1834 | IGNCR
| ICRNL
| IXON
);
1835 termios
.c_oflag
&= ~OPOST
;
1836 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1837 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1838 termios
.c_cflag
|= CS8
;
1839 tcsetattr(0, TCSANOW
, &termios
);
1840 stdio_termios
= true;
1843 vty_prompt(stdio_vty
);
1845 /* Add read/write thread. */
1846 vty_event(VTY_WRITE
, 1, stdio_vty
);
1847 vty_event(VTY_READ
, 0, stdio_vty
);
1850 void vty_stdio_close(void)
1854 vty_close(stdio_vty
);
1857 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1861 /* refuse creating two vtys on stdio */
1865 vty
= stdio_vty
= vty_new_init(0);
1866 stdio_vty_atclose
= atclose
;
1869 /* always have stdio vty in a known _unchangeable_ state, don't want
1871 * to have any effect here to make sure scripting this works as intended
1873 vty
->node
= ENABLE_NODE
;
1875 strcpy(vty
->address
, "console");
1881 /* Accept connection from the network. */
1882 static int vty_accept(struct thread
*thread
)
1890 struct access_list
*acl
= NULL
;
1891 char buf
[SU_ADDRSTRLEN
];
1893 accept_sock
= THREAD_FD(thread
);
1895 /* We continue hearing vty socket. */
1896 vty_event(VTY_SERV
, accept_sock
, NULL
);
1898 memset(&su
, 0, sizeof(union sockunion
));
1900 /* We can handle IPv4 or IPv6 socket. */
1901 vty_sock
= sockunion_accept(accept_sock
, &su
);
1903 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1906 set_nonblocking(vty_sock
);
1907 set_cloexec(vty_sock
);
1909 sockunion2hostprefix(&su
, &p
);
1911 /* VTY's accesslist apply. */
1912 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1913 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1914 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1915 zlog_info("Vty connection refused from %s",
1916 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1919 /* continue accepting connections */
1920 vty_event(VTY_SERV
, accept_sock
, NULL
);
1926 /* VTY's ipv6 accesslist apply. */
1927 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1928 if ((acl
= access_list_lookup(AFI_IP6
,
1929 vty_ipv6_accesslist_name
))
1930 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1931 zlog_info("Vty connection refused from %s",
1932 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1935 /* continue accepting connections */
1936 vty_event(VTY_SERV
, accept_sock
, NULL
);
1943 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1946 zlog_info("can't set sockopt to vty_sock : %s",
1947 safe_strerror(errno
));
1949 zlog_info("Vty connection from %s",
1950 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1952 vty_create(vty_sock
, &su
);
1957 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1960 struct addrinfo req
;
1961 struct addrinfo
*ainfo
;
1962 struct addrinfo
*ainfo_save
;
1964 char port_str
[BUFSIZ
];
1966 memset(&req
, 0, sizeof(struct addrinfo
));
1967 req
.ai_flags
= AI_PASSIVE
;
1968 req
.ai_family
= AF_UNSPEC
;
1969 req
.ai_socktype
= SOCK_STREAM
;
1970 sprintf(port_str
, "%d", port
);
1971 port_str
[sizeof(port_str
) - 1] = '\0';
1973 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1976 flog_err_sys(LIB_ERR_SYSTEM_CALL
, "getaddrinfo failed: %s",
1984 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1987 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1988 ainfo
->ai_protocol
);
1992 sockopt_v6only(ainfo
->ai_family
, sock
);
1993 sockopt_reuseaddr(sock
);
1994 sockopt_reuseport(sock
);
1997 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1999 close(sock
); /* Avoid sd leak. */
2003 ret
= listen(sock
, 3);
2005 close(sock
); /* Avoid sd leak. */
2009 vty_event(VTY_SERV
, sock
, NULL
);
2010 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
2012 freeaddrinfo(ainfo_save
);
2016 /* For sockaddr_un. */
2019 /* VTY shell UNIX domain socket. */
2020 static void vty_serv_un(const char *path
)
2024 struct sockaddr_un serv
;
2026 struct zprivs_ids_t ids
;
2028 /* First of all, unlink existing socket */
2032 old_mask
= umask(0007);
2034 /* Make UNIX domain socket. */
2035 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2037 flog_err_sys(LIB_ERR_SOCKET
,
2038 "Cannot create unix stream socket: %s",
2039 safe_strerror(errno
));
2043 /* Make server socket. */
2044 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2045 serv
.sun_family
= AF_UNIX
;
2046 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2047 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2048 len
= serv
.sun_len
= SUN_LEN(&serv
);
2050 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2051 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2055 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2057 flog_err_sys(LIB_ERR_SOCKET
, "Cannot bind path %s: %s", path
,
2058 safe_strerror(errno
));
2059 close(sock
); /* Avoid sd leak. */
2063 ret
= listen(sock
, 5);
2065 flog_err_sys(LIB_ERR_SOCKET
, "listen(fd %d) failed: %s", sock
,
2066 safe_strerror(errno
));
2067 close(sock
); /* Avoid sd leak. */
2073 zprivs_get_ids(&ids
);
2075 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2076 earlier for the case when we don't need to chown the file
2077 type casting it here to make a compare */
2078 if ((int)ids
.gid_vty
> 0) {
2079 /* set group of socket */
2080 if (chown(path
, -1, ids
.gid_vty
)) {
2081 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
2082 "vty_serv_un: could chown socket, %s",
2083 safe_strerror(errno
));
2087 vty_event(VTYSH_SERV
, sock
, NULL
);
2090 /* #define VTYSH_DEBUG 1 */
2092 static int vtysh_accept(struct thread
*thread
)
2097 struct sockaddr_un client
;
2100 accept_sock
= THREAD_FD(thread
);
2102 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2104 memset(&client
, 0, sizeof(struct sockaddr_un
));
2105 client_len
= sizeof(struct sockaddr_un
);
2107 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2108 (socklen_t
*)&client_len
);
2111 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2115 if (set_nonblocking(sock
) < 0) {
2117 "vtysh_accept: could not set vty socket %d to non-blocking,"
2119 sock
, safe_strerror(errno
));
2126 printf("VTY shell accept\n");
2127 #endif /* VTYSH_DEBUG */
2132 vty
->type
= VTY_SHELL_SERV
;
2133 vty
->node
= VIEW_NODE
;
2135 vty_event(VTYSH_READ
, sock
, vty
);
2140 static int vtysh_flush(struct vty
*vty
)
2142 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2143 case BUFFER_PENDING
:
2144 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2148 0; /* disable monitoring to avoid infinite recursion */
2149 zlog_warn("%s: write error to fd %d, closing", __func__
,
2151 buffer_reset(vty
->lbuf
);
2152 buffer_reset(vty
->obuf
);
2162 static int vtysh_read(struct thread
*thread
)
2168 unsigned char buf
[VTY_READ_BUFSIZ
];
2170 uint8_t header
[4] = {0, 0, 0, 0};
2172 sock
= THREAD_FD(thread
);
2173 vty
= THREAD_ARG(thread
);
2176 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2178 if (ERRNO_IO_RETRY(errno
)) {
2179 vty_event(VTYSH_READ
, sock
, vty
);
2182 vty
->monitor
= 0; /* disable monitoring to avoid
2183 infinite recursion */
2185 "%s: read failed on vtysh client fd %d, closing: %s",
2186 __func__
, sock
, safe_strerror(errno
));
2188 buffer_reset(vty
->lbuf
);
2189 buffer_reset(vty
->obuf
);
2192 printf("close vtysh\n");
2193 #endif /* VTYSH_DEBUG */
2198 printf("line: %.*s\n", nbytes
, buf
);
2199 #endif /* VTYSH_DEBUG */
2201 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2202 /* Clear command line buffer. */
2203 vty
->cp
= vty
->length
= 0;
2205 vty_out(vty
, "%% Command is too long.\n");
2207 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2208 vty
->buf
[vty
->length
++] = *p
;
2210 /* Pass this line to parser. */
2211 ret
= vty_execute(vty
);
2212 /* Note that vty_execute clears the command buffer and resets
2213 vty->length to 0. */
2215 /* Return result. */
2217 printf("result: %d\n", ret
);
2218 printf("vtysh node: %d\n", vty
->node
);
2219 #endif /* VTYSH_DEBUG */
2221 /* hack for asynchronous "write integrated"
2222 * - other commands in "buf" will be ditched
2223 * - input during pending config-write is
2225 if (ret
== CMD_SUSPEND
)
2228 /* warning: watchfrr hardcodes this result write
2231 buffer_put(vty
->obuf
, header
, 4);
2233 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2234 /* Try to flush results; exit if a write
2241 if (vty
->status
== VTY_CLOSE
)
2244 vty_event(VTYSH_READ
, sock
, vty
);
2249 static int vtysh_write(struct thread
*thread
)
2251 struct vty
*vty
= THREAD_ARG(thread
);
2253 vty
->t_write
= NULL
;
2260 /* Determine address family to bind. */
2261 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2263 /* If port is set to 0, do not listen on TCP/IP at all! */
2265 vty_serv_sock_addrinfo(addr
, port
);
2272 /* Close vty interface. Warning: call this only from functions that
2273 will be careful not to access the vty afterwards (since it has
2274 now been freed). This is safest from top-level functions (called
2275 directly by the thread dispatcher). */
2276 void vty_close(struct vty
*vty
)
2279 bool was_stdio
= false;
2281 /* Cancel threads.*/
2283 thread_cancel(vty
->t_read
);
2285 thread_cancel(vty
->t_write
);
2287 thread_cancel(vty
->t_timeout
);
2290 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2292 /* Free input buffer. */
2293 buffer_free(vty
->obuf
);
2294 buffer_free(vty
->lbuf
);
2296 /* Free command history. */
2297 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2299 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2303 vector_unset(vtyvec
, vty
->fd
);
2305 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2309 * note check is for fd > STDERR_FILENO, not fd != -1.
2310 * We never close stdin/stdout/stderr here, because we may be
2311 * running in foreground mode with logging to stdout. Also,
2312 * additionally, we'd need to replace these fds with /dev/null. */
2313 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2315 if (vty
->fd
> STDERR_FILENO
)
2317 if (vty
->fd
== STDIN_FILENO
)
2321 XFREE(MTYPE_VTY
, vty
->buf
);
2324 XFREE(MTYPE_VTY
, vty
->error_buf
);
2326 /* Check configure. */
2327 vty_config_unlock(vty
);
2330 XFREE(MTYPE_VTY
, vty
);
2336 /* When time out occur output message then close connection. */
2337 static int vty_timeout(struct thread
*thread
)
2341 vty
= THREAD_ARG(thread
);
2342 vty
->t_timeout
= NULL
;
2346 buffer_reset(vty
->lbuf
);
2347 buffer_reset(vty
->obuf
);
2348 vty_out(vty
, "\nVty connection is timed out.\n");
2350 /* Close connection. */
2351 vty
->status
= VTY_CLOSE
;
2357 /* Read up configuration file from file_name. */
2358 static void vty_read_file(FILE *confp
)
2362 unsigned int line_num
= 0;
2365 /* vty_close won't close stderr; if some config command prints
2366 * something it'll end up there. (not ideal; it'd be beter if output
2367 * from a file-load went to logging instead. Also note that if this
2368 * function is called after daemonizing, stderr will be /dev/null.)
2370 * vty->fd will be -1 from vty_new()
2372 vty
->wfd
= STDERR_FILENO
;
2373 vty
->type
= VTY_FILE
;
2374 vty
->node
= CONFIG_NODE
;
2376 /* Execute configuration file */
2377 ret
= config_from_file(vty
, confp
, &line_num
);
2379 /* Flush any previous errors before printing messages below */
2380 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2382 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2383 const char *message
= NULL
;
2387 case CMD_ERR_AMBIGUOUS
:
2388 message
= "Ambiguous command";
2390 case CMD_ERR_NO_MATCH
:
2391 message
= "No such command";
2394 message
= "Command returned Warning";
2396 case CMD_WARNING_CONFIG_FAILED
:
2397 message
= "Command returned Warning Config Failed";
2399 case CMD_ERR_INCOMPLETE
:
2400 message
= "Command returned Incomplete";
2402 case CMD_ERR_EXEED_ARGC_MAX
:
2404 "Command exceeded maximum number of Arguments";
2407 message
= "Command returned unhandled error message";
2411 nl
= strchr(vty
->error_buf
, '\n');
2414 flog_err(LIB_ERR_VTY
,
2415 "ERROR: %s on config line %u: %s", message
, line_num
,
2422 static FILE *vty_use_backup_config(const char *fullpath
)
2424 char *fullpath_sav
, *fullpath_tmp
;
2430 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2431 strcpy(fullpath_sav
, fullpath
);
2432 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2434 sav
= open(fullpath_sav
, O_RDONLY
);
2440 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2441 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2443 /* Open file to configuration write. */
2444 tmp
= mkstemp(fullpath_tmp
);
2448 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2451 while ((c
= read(sav
, buffer
, 512)) > 0) {
2452 if (write(tmp
, buffer
, c
) <= 0)
2458 if (rename(fullpath_tmp
, fullpath
) == 0)
2459 ret
= fopen(fullpath
, "r");
2461 unlink(fullpath_tmp
);
2466 unlink(fullpath_tmp
);
2476 /* Read up configuration file from file_name. */
2477 bool vty_read_config(const char *config_file
, char *config_default_dir
)
2479 char cwd
[MAXPATHLEN
];
2481 const char *fullpath
;
2483 bool read_success
= false;
2485 /* If -f flag specified. */
2486 if (config_file
!= NULL
) {
2487 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2488 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2490 LIB_ERR_SYSTEM_CALL
,
2491 "Failure to determine Current Working Directory %d!",
2495 tmp
= XMALLOC(MTYPE_TMP
,
2496 strlen(cwd
) + strlen(config_file
) + 2);
2497 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2500 fullpath
= config_file
;
2502 confp
= fopen(fullpath
, "r");
2504 if (confp
== NULL
) {
2505 zlog_warn("%s: failed to open configuration file %s: %s, checking backup",
2506 __func__
, fullpath
, safe_strerror(errno
));
2508 confp
= vty_use_backup_config(fullpath
);
2511 "WARNING: using backup configuration file!");
2513 flog_err(LIB_ERR_VTY
,
2514 "can't open configuration file [%s]",
2521 host_config_set(config_default_dir
);
2525 struct stat conf_stat
;
2527 /* !!!!PLEASE LEAVE!!!!
2528 * This is NEEDED for use with vtysh -b, or else you can get
2529 * a real configuration food fight with a lot garbage in the
2530 * merged configuration file it creates coming from the per
2531 * daemon configuration files. This also allows the daemons
2532 * to start if there default configuration file is not
2533 * present or ignore them, as needed when using vtysh -b to
2534 * configure the daemons at boot - MAG
2537 /* Stat for vtysh Zebra.conf, if found startup and wait for
2538 * boot configuration
2541 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2542 ret
= stat(integrate_default
, &conf_stat
);
2544 read_success
= true;
2545 goto tmp_free_and_out
;
2549 confp
= fopen(config_default_dir
, "r");
2550 if (confp
== NULL
) {
2551 zlog_warn("%s: failed to open configuration file %s: %s, checking backup",
2552 __func__
, config_default_dir
,
2553 safe_strerror(errno
));
2555 confp
= vty_use_backup_config(config_default_dir
);
2558 "WARNING: using backup configuration file!");
2559 fullpath
= config_default_dir
;
2561 flog_err(LIB_ERR_VTY
,
2562 "can't open configuration file [%s]",
2563 config_default_dir
);
2564 goto tmp_free_and_out
;
2567 fullpath
= config_default_dir
;
2570 vty_read_file(confp
);
2571 read_success
= true;
2575 host_config_set(fullpath
);
2579 XFREE(MTYPE_TMP
, tmp
);
2581 return read_success
;
2584 /* Small utility function which output log to the VTY. */
2585 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2586 struct timestamp_control
*ctl
, va_list va
)
2594 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2595 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2599 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2605 /* Async-signal-safe version of vty_log for fixed strings. */
2606 void vty_log_fixed(char *buf
, size_t len
)
2609 struct iovec iov
[2];
2610 char crlf
[4] = "\r\n";
2612 /* vty may not have been initialised */
2616 iov
[0].iov_base
= buf
;
2617 iov
[0].iov_len
= len
;
2618 iov
[1].iov_base
= crlf
;
2621 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2623 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2624 /* N.B. We don't care about the return code, since
2626 most likely just about to die anyway. */
2627 if (writev(vty
->wfd
, iov
, 2) == -1) {
2628 fprintf(stderr
, "Failure to writev: %d\n",
2635 int vty_config_lock(struct vty
*vty
)
2637 if (vty_config_is_lockless
)
2639 if (vty_config
== 0) {
2646 int vty_config_unlock(struct vty
*vty
)
2648 if (vty_config_is_lockless
)
2650 if (vty_config
== 1 && vty
->config
== 1) {
2657 void vty_config_lockless(void)
2659 vty_config_is_lockless
= 1;
2662 /* Master of the threads. */
2663 static struct thread_master
*vty_master
;
2665 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2667 struct thread
*vty_serv_thread
= NULL
;
2671 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2673 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2677 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2679 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2683 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2687 vty
->t_write
= NULL
;
2688 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2694 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2696 /* Time out treatment. */
2697 if (vty
->v_timeout
) {
2699 thread_cancel(vty
->t_timeout
);
2700 vty
->t_timeout
= NULL
;
2701 thread_add_timer(vty_master
, vty_timeout
, vty
,
2702 vty
->v_timeout
, &vty
->t_timeout
);
2706 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2709 case VTY_TIMEOUT_RESET
:
2710 if (vty
->t_timeout
) {
2711 thread_cancel(vty
->t_timeout
);
2712 vty
->t_timeout
= NULL
;
2714 if (vty
->v_timeout
) {
2715 vty
->t_timeout
= NULL
;
2716 thread_add_timer(vty_master
, vty_timeout
, vty
,
2717 vty
->v_timeout
, &vty
->t_timeout
);
2723 DEFUN_NOSH (config_who
,
2726 "Display who is on vty\n")
2731 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2732 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2733 vty_out(vty
, "%svty[%d] connected from %s.\n",
2734 v
->config
? "*" : " ", i
, v
->address
);
2738 /* Move to vty configuration mode. */
2739 DEFUN_NOSH (line_vty
,
2742 "Configure a terminal line\n"
2743 "Virtual terminal\n")
2745 vty
->node
= VTY_NODE
;
2749 /* Set time out value. */
2750 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2751 const char *sec_str
)
2753 unsigned long timeout
= 0;
2755 /* min_str and sec_str are already checked by parser. So it must be
2756 all digit string. */
2758 timeout
= strtol(min_str
, NULL
, 10);
2762 timeout
+= strtol(sec_str
, NULL
, 10);
2764 vty_timeout_val
= timeout
;
2765 vty
->v_timeout
= timeout
;
2766 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2772 DEFUN (exec_timeout_min
,
2773 exec_timeout_min_cmd
,
2774 "exec-timeout (0-35791)",
2775 "Set timeout value\n"
2776 "Timeout value in minutes\n")
2779 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2782 DEFUN (exec_timeout_sec
,
2783 exec_timeout_sec_cmd
,
2784 "exec-timeout (0-35791) (0-2147483)",
2785 "Set the EXEC timeout\n"
2786 "Timeout in minutes\n"
2787 "Timeout in seconds\n")
2790 int idx_number_2
= 2;
2791 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2792 argv
[idx_number_2
]->arg
);
2795 DEFUN (no_exec_timeout
,
2796 no_exec_timeout_cmd
,
2799 "Set the EXEC timeout\n")
2801 return exec_timeout(vty
, NULL
, NULL
);
2804 /* Set vty access class. */
2805 DEFUN (vty_access_class
,
2806 vty_access_class_cmd
,
2807 "access-class WORD",
2808 "Filter connections based on an IP access list\n"
2812 if (vty_accesslist_name
)
2813 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2815 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2820 /* Clear vty access class. */
2821 DEFUN (no_vty_access_class
,
2822 no_vty_access_class_cmd
,
2823 "no access-class [WORD]",
2825 "Filter connections based on an IP access list\n"
2829 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2830 if (!vty_accesslist_name
2831 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2832 vty_out(vty
, "Access-class is not currently applied to vty\n");
2833 return CMD_WARNING_CONFIG_FAILED
;
2836 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2838 vty_accesslist_name
= NULL
;
2843 /* Set vty access class. */
2844 DEFUN (vty_ipv6_access_class
,
2845 vty_ipv6_access_class_cmd
,
2846 "ipv6 access-class WORD",
2848 "Filter connections based on an IP access list\n"
2849 "IPv6 access list\n")
2852 if (vty_ipv6_accesslist_name
)
2853 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2855 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2860 /* Clear vty access class. */
2861 DEFUN (no_vty_ipv6_access_class
,
2862 no_vty_ipv6_access_class_cmd
,
2863 "no ipv6 access-class [WORD]",
2866 "Filter connections based on an IP access list\n"
2867 "IPv6 access list\n")
2870 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2872 if (!vty_ipv6_accesslist_name
2873 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2875 "IPv6 access-class is not currently applied to vty\n");
2876 return CMD_WARNING_CONFIG_FAILED
;
2879 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2881 vty_ipv6_accesslist_name
= NULL
;
2890 "Enable password checking\n")
2892 no_password_check
= 0;
2896 DEFUN (no_vty_login
,
2900 "Enable password checking\n")
2902 no_password_check
= 1;
2906 DEFUN (service_advanced_vty
,
2907 service_advanced_vty_cmd
,
2908 "service advanced-vty",
2909 "Set up miscellaneous service\n"
2910 "Enable advanced mode vty interface\n")
2916 DEFUN (no_service_advanced_vty
,
2917 no_service_advanced_vty_cmd
,
2918 "no service advanced-vty",
2920 "Set up miscellaneous service\n"
2921 "Enable advanced mode vty interface\n")
2927 DEFUN_NOSH (terminal_monitor
,
2928 terminal_monitor_cmd
,
2930 "Set terminal line parameters\n"
2931 "Copy debug output to the current terminal line\n")
2937 DEFUN_NOSH (terminal_no_monitor
,
2938 terminal_no_monitor_cmd
,
2939 "terminal no monitor",
2940 "Set terminal line parameters\n"
2942 "Copy debug output to the current terminal line\n")
2948 DEFUN_NOSH (no_terminal_monitor
,
2949 no_terminal_monitor_cmd
,
2950 "no terminal monitor",
2952 "Set terminal line parameters\n"
2953 "Copy debug output to the current terminal line\n")
2955 return terminal_no_monitor(self
, vty
, argc
, argv
);
2959 DEFUN_NOSH (show_history
,
2963 "Display the session command history\n")
2967 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2968 if (index
== VTY_MAXHIST
) {
2973 if (vty
->hist
[index
] != NULL
)
2974 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2983 DEFUN (log_commands
,
2987 "Log all commands (can't be unset without restart)\n")
2989 do_log_commands
= 1;
2993 /* Display current configuration. */
2994 static int vty_config_write(struct vty
*vty
)
2996 vty_out(vty
, "line vty\n");
2998 if (vty_accesslist_name
)
2999 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3001 if (vty_ipv6_accesslist_name
)
3002 vty_out(vty
, " ipv6 access-class %s\n",
3003 vty_ipv6_accesslist_name
);
3006 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3007 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3008 vty_timeout_val
% 60);
3011 if (no_password_check
)
3012 vty_out(vty
, " no login\n");
3014 if (do_log_commands
)
3015 vty_out(vty
, "log commands\n");
3017 vty_out(vty
, "!\n");
3022 struct cmd_node vty_node
= {
3023 VTY_NODE
, "%s(config-line)# ", 1,
3026 /* Reset all VTY status. */
3031 struct thread
*vty_serv_thread
;
3033 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3034 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3035 buffer_reset(vty
->lbuf
);
3036 buffer_reset(vty
->obuf
);
3037 vty
->status
= VTY_CLOSE
;
3041 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3042 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3044 thread_cancel(vty_serv_thread
);
3045 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3049 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3051 if (vty_accesslist_name
) {
3052 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3053 vty_accesslist_name
= NULL
;
3056 if (vty_ipv6_accesslist_name
) {
3057 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3058 vty_ipv6_accesslist_name
= NULL
;
3062 static void vty_save_cwd(void)
3064 char cwd
[MAXPATHLEN
];
3067 c
= getcwd(cwd
, MAXPATHLEN
);
3071 * At this point if these go wrong, more than likely
3072 * the whole world is coming down around us
3073 * Hence not worrying about it too much.
3075 if (!chdir(SYSCONFDIR
)) {
3076 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
3077 "Failure to chdir to %s, errno: %d",
3081 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3082 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
3083 "Failure to getcwd, errno: %d", errno
);
3088 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3089 strcpy(vty_cwd
, cwd
);
3097 int vty_shell(struct vty
*vty
)
3099 return vty
->type
== VTY_SHELL
? 1 : 0;
3102 int vty_shell_serv(struct vty
*vty
)
3104 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3107 void vty_init_vtysh()
3109 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3112 /* Install vty's own commands like `who' command. */
3113 void vty_init(struct thread_master
*master_thread
)
3115 /* For further configuration read, preserve current directory. */
3118 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3120 vty_master
= master_thread
;
3122 atexit(vty_stdio_atexit
);
3124 /* Initilize server thread vector. */
3125 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3127 /* Install bgp top node. */
3128 install_node(&vty_node
, vty_config_write
);
3130 install_element(VIEW_NODE
, &config_who_cmd
);
3131 install_element(VIEW_NODE
, &show_history_cmd
);
3132 install_element(CONFIG_NODE
, &line_vty_cmd
);
3133 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3134 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3135 install_element(CONFIG_NODE
, &show_history_cmd
);
3136 install_element(CONFIG_NODE
, &log_commands_cmd
);
3137 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3138 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3139 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3141 install_default(VTY_NODE
);
3142 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3143 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3144 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3145 install_element(VTY_NODE
, &vty_access_class_cmd
);
3146 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3147 install_element(VTY_NODE
, &vty_login_cmd
);
3148 install_element(VTY_NODE
, &no_vty_login_cmd
);
3149 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3150 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3153 void vty_terminate(void)
3156 XFREE(MTYPE_TMP
, vty_cwd
);
3158 if (vtyvec
&& Vvty_serv_thread
) {
3160 vector_free(vtyvec
);
3161 vector_free(Vvty_serv_thread
);
3163 Vvty_serv_thread
= NULL
;