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>
26 #include <sys/types.h>
27 #ifdef HAVE_LIBPCRE2_POSIX
28 #ifndef _FRR_PCRE2_POSIX
29 #define _FRR_PCRE2_POSIX
30 #include <pcre2posix.h>
31 #endif /* _FRR_PCRE2_POSIX */
32 #elif defined(HAVE_LIBPCREPOSIX)
33 #include <pcreposix.h>
36 #endif /* HAVE_LIBPCRE2_POSIX */
43 #include "sockunion.h"
53 #include "lib_errors.h"
54 #include "northbound_cli.h"
58 #include <arpa/telnet.h>
61 #include "lib/vty_clippy.c"
63 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY");
64 DEFINE_MTYPE_STATIC(LIB
, VTY_SERV
, "VTY server");
65 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer");
66 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history");
68 DECLARE_DLIST(vtys
, struct vty
, itm
);
83 PREDECL_DLIST(vtyservs
);
86 struct vtyservs_item itm
;
91 struct thread
*t_accept
;
94 DECLARE_DLIST(vtyservs
, struct vty_serv
, itm
);
96 static void vty_event_serv(enum vty_event event
, struct vty_serv
*);
97 static void vty_event(enum vty_event
, struct vty
*);
99 /* Extern host structure from command.c */
100 extern struct host host
;
102 /* active listeners */
103 static struct vtyservs_head vty_servs
[1] = {INIT_DLIST(vty_servs
[0])};
105 /* active connections */
106 static struct vtys_head vty_sessions
[1] = {INIT_DLIST(vty_sessions
[0])};
107 static struct vtys_head vtysh_sessions
[1] = {INIT_DLIST(vtysh_sessions
[0])};
109 /* Vty timeout value. */
110 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
112 /* Vty access-class command */
113 static char *vty_accesslist_name
= NULL
;
115 /* Vty access-calss for IPv6. */
116 static char *vty_ipv6_accesslist_name
= NULL
;
118 /* Current directory. */
119 static char vty_cwd
[MAXPATHLEN
];
121 /* Login password check. */
122 static int no_password_check
= 0;
124 /* Integrated configuration file path */
125 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
127 static bool do_log_commands
;
128 static bool do_log_commands_perm
;
130 void vty_frame(struct vty
*vty
, const char *format
, ...)
134 va_start(args
, format
);
135 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
136 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
137 vty
->frame_pos
= strlen(vty
->frame
);
141 void vty_endframe(struct vty
*vty
, const char *endtext
)
143 if (vty
->frame_pos
== 0 && endtext
)
144 vty_out(vty
, "%s", endtext
);
148 bool vty_set_include(struct vty
*vty
, const char *regexp
)
156 regfree(&vty
->include
);
162 errcode
= regcomp(&vty
->include
, regexp
,
163 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
166 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
167 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
175 /* VTY standard output function. */
176 int vty_out(struct vty
*vty
, const char *format
, ...)
183 /* format string may contain %m, keep errno intact for printfrr */
184 int saved_errno
= errno
;
186 if (vty
->frame_pos
) {
188 vty_out(vty
, "%s", vty
->frame
);
191 va_start(args
, format
);
193 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
200 vector lines
= frrstr_split_vec(p
, "\n");
202 /* Place first value in the cache */
203 char *firstline
= vector_slot(lines
, 0);
204 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
206 /* If our split returned more than one entry, time to filter */
207 if (vector_active(lines
) > 1) {
209 * returned string is MTYPE_TMP so it matches the MTYPE
210 * of everything else in the vector
212 char *bstr
= buffer_getstr(vty
->lbuf
);
213 buffer_reset(vty
->lbuf
);
214 XFREE(MTYPE_TMP
, lines
->index
[0]);
215 vector_set_index(lines
, 0, bstr
);
216 frrstr_filter_vec(lines
, &vty
->include
);
217 vector_compact(lines
);
219 * Consider the string "foo\n". If the regex is an empty string
220 * and the line ended with a newline, then the vector will look
226 * If the regex isn't empty, the vector will look like:
230 * In this case we'd like to preserve the newline, so we add
231 * the empty string [1] as in the first example.
233 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
234 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
235 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
237 filtered
= frrstr_join_vec(lines
, "\n");
243 frrstr_strvec_free(lines
);
254 /* print with crlf replacement */
255 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
260 fprintf(vty
->of
, "%s", filtered
);
262 } else if (vty
->of_saved
) {
263 fprintf(vty
->of_saved
, "%s", filtered
);
264 fflush(vty
->of_saved
);
270 /* print without crlf replacement */
271 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
277 if (vty
->filter
&& filtered
)
278 XFREE(MTYPE_TMP
, filtered
);
280 /* If p is not different with buf, it is allocated buffer. */
282 XFREE(MTYPE_VTY_OUT_BUF
, p
);
287 int vty_json(struct vty
*vty
, struct json_object
*json
)
294 text
= json_object_to_json_string_ext(
295 json
, JSON_C_TO_STRING_PRETTY
| JSON_C_TO_STRING_NOSLASHESCAPE
);
296 vty_out(vty
, "%s\n", text
);
297 json_object_free(json
);
302 /* Output current time to the vty. */
303 void vty_time_print(struct vty
*vty
, int cr
)
305 char buf
[FRR_TIMESTAMP_LEN
];
307 if (frr_timestamp(0, buf
, sizeof(buf
)) == 0) {
308 zlog_info("frr_timestamp error");
312 vty_out(vty
, "%s\n", buf
);
314 vty_out(vty
, "%s ", buf
);
319 /* Say hello to vty interface. */
320 void vty_hello(struct vty
*vty
)
326 f
= fopen(host
.motdfile
, "r");
328 while (fgets(buf
, sizeof(buf
), f
)) {
330 /* work backwards to ignore trailling isspace()
332 for (s
= buf
+ strlen(buf
);
333 (s
> buf
) && isspace((unsigned char)s
[-1]);
337 vty_out(vty
, "%s\n", buf
);
341 vty_out(vty
, "MOTD file not found\n");
342 } else if (host
.motd
)
343 vty_out(vty
, "%s", host
.motd
);
346 #pragma GCC diagnostic push
347 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
348 /* prompt formatting has a %s in the cmd_node prompt string.
350 * Also for some reason GCC emits the warning on the end of the function
351 * (optimization maybe?) rather than on the vty_out line, so this pragma
352 * wraps the entire function rather than just the vty_out line.
355 /* Put out prompt and wait input from user. */
356 static void vty_prompt(struct vty
*vty
)
358 if (vty
->type
== VTY_TERM
) {
359 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
362 #pragma GCC diagnostic pop
364 /* Send WILL TELOPT_ECHO to remote server. */
365 static void vty_will_echo(struct vty
*vty
)
367 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
368 vty_out(vty
, "%s", cmd
);
371 /* Make suppress Go-Ahead telnet option. */
372 static void vty_will_suppress_go_ahead(struct vty
*vty
)
374 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
375 vty_out(vty
, "%s", cmd
);
378 /* Make don't use linemode over telnet. */
379 static void vty_dont_linemode(struct vty
*vty
)
381 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
382 vty_out(vty
, "%s", cmd
);
385 /* Use window size. */
386 static void vty_do_window_size(struct vty
*vty
)
388 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
389 vty_out(vty
, "%s", cmd
);
392 /* Authentication of vty */
393 static void vty_auth(struct vty
*vty
, char *buf
)
396 enum node_type next_node
= 0;
402 passwd
= host
.password_encrypt
;
404 passwd
= host
.password
;
406 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
408 next_node
= VIEW_NODE
;
410 case AUTH_ENABLE_NODE
:
412 passwd
= host
.enable_encrypt
;
414 passwd
= host
.enable
;
415 next_node
= ENABLE_NODE
;
421 fail
= strcmp(crypt(buf
, passwd
), passwd
);
423 fail
= strcmp(buf
, passwd
);
429 vty
->node
= next_node
; /* Success ! */
432 if (vty
->fail
>= 3) {
433 if (vty
->node
== AUTH_NODE
) {
435 "%% Bad passwords, too many failures!\n");
436 vty
->status
= VTY_CLOSE
;
438 /* AUTH_ENABLE_NODE */
441 "%% Bad enable passwords, too many failures!\n");
442 vty
->status
= VTY_CLOSE
;
448 /* Command execution over the vty interface. */
449 static int vty_command(struct vty
*vty
, char *buf
)
452 const char *protocolname
;
458 * Log non empty command lines
460 if (do_log_commands
&&
461 strncmp(buf
, "echo PING", strlen("echo PING")) != 0)
464 /* Skip white spaces. */
465 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
468 if (cp
!= NULL
&& *cp
!= '\0') {
469 char vty_str
[VTY_BUFSIZ
];
470 char prompt_str
[VTY_BUFSIZ
];
472 /* format the base vty info */
473 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
476 /* format the prompt */
477 #pragma GCC diagnostic push
478 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
479 /* prompt formatting has a %s in the cmd_node prompt string */
480 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
482 #pragma GCC diagnostic pop
484 /* now log the command */
485 zlog_notice("%s%s", prompt_str
, buf
);
490 unsigned long walltime
, cputime
;
492 /* cmd_execute() may change cputime_enabled if we're executing the
493 * "service cputime-stats" command, which can result in nonsensical
494 * and very confusing warnings
496 bool cputime_enabled_here
= cputime_enabled
;
500 ret
= cmd_execute(vty
, buf
, NULL
, 0);
504 walltime
= thread_consumed_time(&after
, &before
, &cputime
);
506 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
507 && cputime
> cputime_threshold
)
508 /* Warn about CPU hog that must be fixed. */
509 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
510 "CPU HOG: command took %lums (cpu time %lums): %s",
511 walltime
/ 1000, cputime
/ 1000, buf
);
512 else if (walltime_threshold
&& walltime
> walltime_threshold
)
513 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
514 "STARVATION: command took %lums (cpu time %lums): %s",
515 walltime
/ 1000, cputime
/ 1000, buf
);
517 /* Get the name of the protocol if any */
518 protocolname
= frr_protoname
;
520 if (ret
!= CMD_SUCCESS
)
523 if (vty
->type
== VTY_FILE
)
524 vty_out(vty
, "Warning...\n");
526 case CMD_ERR_AMBIGUOUS
:
527 vty_out(vty
, "%% Ambiguous command.\n");
529 case CMD_ERR_NO_MATCH
:
530 vty_out(vty
, "%% [%s] Unknown command: %s\n",
533 case CMD_ERR_INCOMPLETE
:
534 vty_out(vty
, "%% Command incomplete.\n");
541 static const char telnet_backward_char
= 0x08;
542 static const char telnet_space_char
= ' ';
544 /* Basic function to write buffer to vty. */
545 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
547 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
550 /* Should we do buffering here ? And make vty_flush (vty) ? */
551 buffer_put(vty
->obuf
, buf
, nbytes
);
554 /* Basic function to insert character into vty. */
555 static void vty_self_insert(struct vty
*vty
, char c
)
560 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
563 length
= vty
->length
- vty
->cp
;
564 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
565 vty
->buf
[vty
->cp
] = c
;
567 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
568 for (i
= 0; i
< length
; i
++)
569 vty_write(vty
, &telnet_backward_char
, 1);
574 vty
->buf
[vty
->length
] = '\0';
577 /* Self insert character 'c' in overwrite mode. */
578 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
580 if (vty
->cp
== vty
->length
) {
581 vty_self_insert(vty
, c
);
585 vty
->buf
[vty
->cp
++] = c
;
586 vty_write(vty
, &c
, 1);
590 * Insert a string into vty->buf at the current cursor position.
592 * If the resultant string would be larger than VTY_BUFSIZ it is
595 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
597 if (vty
->cp
== VTY_BUFSIZ
)
600 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
601 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
603 vty
->length
= MAX(vty
->cp
, vty
->length
);
604 vty
->buf
[vty
->length
] = '\0';
605 vty_write(vty
, str
, nwrite
);
608 /* Forward character. */
609 static void vty_forward_char(struct vty
*vty
)
611 if (vty
->cp
< vty
->length
) {
612 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
617 /* Backward character. */
618 static void vty_backward_char(struct vty
*vty
)
622 vty_write(vty
, &telnet_backward_char
, 1);
626 /* Move to the beginning of the line. */
627 static void vty_beginning_of_line(struct vty
*vty
)
630 vty_backward_char(vty
);
633 /* Move to the end of the line. */
634 static void vty_end_of_line(struct vty
*vty
)
636 while (vty
->cp
< vty
->length
)
637 vty_forward_char(vty
);
640 static void vty_kill_line_from_beginning(struct vty
*);
641 static void vty_redraw_line(struct vty
*);
643 /* Print command line history. This function is called from
644 vty_next_line and vty_previous_line. */
645 static void vty_history_print(struct vty
*vty
)
649 vty_kill_line_from_beginning(vty
);
651 /* Get previous line from history buffer */
652 length
= strlen(vty
->hist
[vty
->hp
]);
653 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
654 vty
->cp
= vty
->length
= length
;
655 vty
->buf
[vty
->length
] = '\0';
657 /* Redraw current line */
658 vty_redraw_line(vty
);
661 /* Show next command line history. */
662 static void vty_next_line(struct vty
*vty
)
666 if (vty
->hp
== vty
->hindex
)
669 /* Try is there history exist or not. */
671 if (try_index
== (VTY_MAXHIST
- 1))
676 /* If there is not history return. */
677 if (vty
->hist
[try_index
] == NULL
)
682 vty_history_print(vty
);
685 /* Show previous command line history. */
686 static void vty_previous_line(struct vty
*vty
)
692 try_index
= VTY_MAXHIST
- 1;
696 if (vty
->hist
[try_index
] == NULL
)
701 vty_history_print(vty
);
704 /* This function redraw all of the command line character. */
705 static void vty_redraw_line(struct vty
*vty
)
707 vty_write(vty
, vty
->buf
, vty
->length
);
708 vty
->cp
= vty
->length
;
712 static void vty_forward_word(struct vty
*vty
)
714 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
715 vty_forward_char(vty
);
717 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
718 vty_forward_char(vty
);
721 /* Backward word without skipping training space. */
722 static void vty_backward_pure_word(struct vty
*vty
)
724 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
725 vty_backward_char(vty
);
729 static void vty_backward_word(struct vty
*vty
)
731 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
732 vty_backward_char(vty
);
734 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
735 vty_backward_char(vty
);
738 /* When '^D' is typed at the beginning of the line we move to the down
740 static void vty_down_level(struct vty
*vty
)
748 /* When '^Z' is received from vty, move down to the enable mode. */
749 static void vty_end_config(struct vty
*vty
)
754 vty_config_exit(vty
);
755 vty
->node
= ENABLE_NODE
;
762 /* Delete a character at the current point. */
763 static void vty_delete_char(struct vty
*vty
)
768 if (vty
->length
== 0) {
773 if (vty
->cp
== vty
->length
)
774 return; /* completion need here? */
776 size
= vty
->length
- vty
->cp
;
779 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
780 vty
->buf
[vty
->length
] = '\0';
782 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
785 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
786 vty_write(vty
, &telnet_space_char
, 1);
788 for (i
= 0; i
< size
; i
++)
789 vty_write(vty
, &telnet_backward_char
, 1);
792 /* Delete a character before the point. */
793 static void vty_delete_backward_char(struct vty
*vty
)
798 vty_backward_char(vty
);
799 vty_delete_char(vty
);
802 /* Kill rest of line from current point. */
803 static void vty_kill_line(struct vty
*vty
)
808 size
= vty
->length
- vty
->cp
;
813 for (i
= 0; i
< size
; i
++)
814 vty_write(vty
, &telnet_space_char
, 1);
815 for (i
= 0; i
< size
; i
++)
816 vty_write(vty
, &telnet_backward_char
, 1);
818 memset(&vty
->buf
[vty
->cp
], 0, size
);
819 vty
->length
= vty
->cp
;
822 /* Kill line from the beginning. */
823 static void vty_kill_line_from_beginning(struct vty
*vty
)
825 vty_beginning_of_line(vty
);
829 /* Delete a word before the point. */
830 static void vty_forward_kill_word(struct vty
*vty
)
832 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
833 vty_delete_char(vty
);
834 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
835 vty_delete_char(vty
);
838 /* Delete a word before the point. */
839 static void vty_backward_kill_word(struct vty
*vty
)
841 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
842 vty_delete_backward_char(vty
);
843 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
844 vty_delete_backward_char(vty
);
847 /* Transpose chars before or at the point. */
848 static void vty_transpose_chars(struct vty
*vty
)
852 /* If length is short or point is near by the beginning of line then
854 if (vty
->length
< 2 || vty
->cp
< 1)
857 /* In case of point is located at the end of the line. */
858 if (vty
->cp
== vty
->length
) {
859 c1
= vty
->buf
[vty
->cp
- 1];
860 c2
= vty
->buf
[vty
->cp
- 2];
862 vty_backward_char(vty
);
863 vty_backward_char(vty
);
864 vty_self_insert_overwrite(vty
, c1
);
865 vty_self_insert_overwrite(vty
, c2
);
867 c1
= vty
->buf
[vty
->cp
];
868 c2
= vty
->buf
[vty
->cp
- 1];
870 vty_backward_char(vty
);
871 vty_self_insert_overwrite(vty
, c1
);
872 vty_self_insert_overwrite(vty
, c2
);
876 /* Do completion at vty interface. */
877 static void vty_complete_command(struct vty
*vty
)
881 char **matched
= NULL
;
884 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
887 vline
= cmd_make_strvec(vty
->buf
);
891 /* In case of 'help \t'. */
892 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
893 vector_set(vline
, NULL
);
895 matched
= cmd_complete_command(vline
, vty
, &ret
);
897 cmd_free_strvec(vline
);
901 case CMD_ERR_AMBIGUOUS
:
902 vty_out(vty
, "%% Ambiguous command.\n");
904 vty_redraw_line(vty
);
906 case CMD_ERR_NO_MATCH
:
907 /* vty_out (vty, "%% There is no matched command.\n"); */
909 vty_redraw_line(vty
);
911 case CMD_COMPLETE_FULL_MATCH
:
913 /* 2016-11-28 equinox -- need to debug, SEGV here */
914 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
916 vty_redraw_line(vty
);
920 vty_redraw_line(vty
);
921 vty_backward_pure_word(vty
);
922 vty_insert_word_overwrite(vty
, matched
[0]);
923 vty_self_insert(vty
, ' ');
924 XFREE(MTYPE_COMPLETION
, matched
[0]);
926 case CMD_COMPLETE_MATCH
:
928 vty_redraw_line(vty
);
929 vty_backward_pure_word(vty
);
930 vty_insert_word_overwrite(vty
, matched
[0]);
931 XFREE(MTYPE_COMPLETION
, matched
[0]);
933 case CMD_COMPLETE_LIST_MATCH
:
934 for (i
= 0; matched
[i
] != NULL
; i
++) {
935 if (i
!= 0 && ((i
% 6) == 0))
937 vty_out(vty
, "%-10s ", matched
[i
]);
938 XFREE(MTYPE_COMPLETION
, matched
[i
]);
943 vty_redraw_line(vty
);
945 case CMD_ERR_NOTHING_TODO
:
947 vty_redraw_line(vty
);
952 XFREE(MTYPE_TMP
, matched
);
955 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
956 unsigned int desc_width
, struct cmd_token
*token
)
964 if (desc_width
<= 0) {
965 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
969 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
971 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
972 for (pos
= desc_width
; pos
> 0; pos
--)
973 if (*(p
+ pos
) == ' ')
981 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
986 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
988 XFREE(MTYPE_TMP
, buf
);
991 /* Describe matched command function. */
992 static void vty_describe_command(struct vty
*vty
)
997 unsigned int i
, width
, desc_width
;
998 struct cmd_token
*token
, *token_cr
= NULL
;
1000 vline
= cmd_make_strvec(vty
->buf
);
1002 /* In case of '> ?'. */
1003 if (vline
== NULL
) {
1004 vline
= vector_init(1);
1005 vector_set(vline
, NULL
);
1006 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1007 vector_set(vline
, NULL
);
1009 describe
= cmd_describe_command(vline
, vty
, &ret
);
1013 /* Ambiguous error. */
1015 case CMD_ERR_AMBIGUOUS
:
1016 vty_out(vty
, "%% Ambiguous command.\n");
1019 case CMD_ERR_NO_MATCH
:
1020 vty_out(vty
, "%% There is no matched command.\n");
1025 /* Get width of command string. */
1027 for (i
= 0; i
< vector_active(describe
); i
++)
1028 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1031 if (token
->text
[0] == '\0')
1034 len
= strlen(token
->text
);
1040 /* Get width of description string. */
1041 desc_width
= vty
->width
- (width
+ 6);
1043 /* Print out description. */
1044 for (i
= 0; i
< vector_active(describe
); i
++)
1045 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1046 if (token
->text
[0] == '\0')
1049 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1055 vty_out(vty
, " %-s\n", token
->text
);
1056 else if (desc_width
>= strlen(token
->desc
))
1057 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1060 vty_describe_fold(vty
, width
, desc_width
,
1063 if (IS_VARYING_TOKEN(token
->type
)) {
1064 const char *ref
= vector_slot(
1065 vline
, vector_active(vline
) - 1);
1067 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1068 cmd_variable_complete(token
, ref
, varcomps
);
1070 if (vector_active(varcomps
) > 0) {
1071 char *ac
= cmd_variable_comp2str(
1072 varcomps
, vty
->width
);
1073 vty_out(vty
, "%s\n", ac
);
1074 XFREE(MTYPE_TMP
, ac
);
1077 vector_free(varcomps
);
1081 if ((token
= token_cr
)) {
1083 vty_out(vty
, " %-s\n", token
->text
);
1084 else if (desc_width
>= strlen(token
->desc
))
1085 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1088 vty_describe_fold(vty
, width
, desc_width
, token
);
1092 cmd_free_strvec(vline
);
1094 vector_free(describe
);
1097 vty_redraw_line(vty
);
1100 static void vty_clear_buf(struct vty
*vty
)
1102 memset(vty
->buf
, 0, vty
->max
);
1105 /* ^C stop current input and do not add command line to the history. */
1106 static void vty_stop_input(struct vty
*vty
)
1108 vty
->cp
= vty
->length
= 0;
1113 vty_config_exit(vty
);
1114 vty
->node
= ENABLE_NODE
;
1119 /* Set history pointer to the latest one. */
1120 vty
->hp
= vty
->hindex
;
1123 /* Add current command line to the history buffer. */
1124 static void vty_hist_add(struct vty
*vty
)
1128 if (vty
->length
== 0)
1131 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1133 /* Ignore the same string as previous one. */
1134 if (vty
->hist
[index
])
1135 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1136 vty
->hp
= vty
->hindex
;
1140 /* Insert history entry. */
1141 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1142 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1144 /* History index rotation. */
1146 if (vty
->hindex
== VTY_MAXHIST
)
1149 vty
->hp
= vty
->hindex
;
1152 /* #define TELNET_OPTION_DEBUG */
1154 /* Get telnet window size. */
1155 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1157 #ifdef TELNET_OPTION_DEBUG
1160 for (i
= 0; i
< nbytes
; i
++) {
1163 vty_out(vty
, "IAC ");
1166 vty_out(vty
, "WILL ");
1169 vty_out(vty
, "WONT ");
1172 vty_out(vty
, "DO ");
1175 vty_out(vty
, "DONT ");
1178 vty_out(vty
, "SB ");
1181 vty_out(vty
, "SE ");
1184 vty_out(vty
, "TELOPT_ECHO \n");
1187 vty_out(vty
, "TELOPT_SGA \n");
1190 vty_out(vty
, "TELOPT_NAWS \n");
1193 vty_out(vty
, "%x ", buf
[i
]);
1199 #endif /* TELNET_OPTION_DEBUG */
1204 vty
->iac_sb_in_progress
= 1;
1207 if (!vty
->iac_sb_in_progress
)
1210 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1211 vty
->iac_sb_in_progress
= 0;
1214 switch (vty
->sb_buf
[0]) {
1216 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1219 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1221 (unsigned long)vty
->sb_len
);
1222 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1225 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1226 (unsigned long)sizeof(vty
->sb_buf
),
1227 TELNET_NAWS_SB_LEN
);
1229 vty
->width
= ((vty
->sb_buf
[1] << 8)
1231 vty
->height
= ((vty
->sb_buf
[3] << 8)
1233 #ifdef TELNET_OPTION_DEBUG
1235 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1236 vty
->width
, vty
->height
);
1241 vty
->iac_sb_in_progress
= 0;
1250 /* Execute current command line. */
1251 static int vty_execute(struct vty
*vty
)
1257 switch (vty
->node
) {
1259 case AUTH_ENABLE_NODE
:
1260 vty_auth(vty
, vty
->buf
);
1263 ret
= vty_command(vty
, vty
->buf
);
1264 if (vty
->type
== VTY_TERM
)
1269 /* Clear command line buffer. */
1270 vty
->cp
= vty
->length
= 0;
1273 if (vty
->status
!= VTY_CLOSE
)
1279 #define CONTROL(X) ((X) - '@')
1280 #define VTY_NORMAL 0
1281 #define VTY_PRE_ESCAPE 1
1282 #define VTY_ESCAPE 2
1285 /* Escape character command map. */
1286 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1290 vty_previous_line(vty
);
1296 vty_forward_char(vty
);
1299 vty_backward_char(vty
);
1305 /* Go back to normal mode. */
1306 vty
->escape
= VTY_NORMAL
;
1309 /* Quit print out to the buffer. */
1310 static void vty_buffer_reset(struct vty
*vty
)
1312 buffer_reset(vty
->obuf
);
1313 buffer_reset(vty
->lbuf
);
1315 vty_redraw_line(vty
);
1318 /* Read data via vty socket. */
1319 static void vty_read(struct thread
*thread
)
1323 unsigned char buf
[VTY_READ_BUFSIZ
];
1325 struct vty
*vty
= THREAD_ARG(thread
);
1327 /* Read raw data from socket */
1328 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1330 if (ERRNO_IO_RETRY(errno
)) {
1331 vty_event(VTY_READ
, vty
);
1336 "%s: read error on vty client fd %d, closing: %s",
1337 __func__
, vty
->fd
, safe_strerror(errno
));
1338 buffer_reset(vty
->obuf
);
1339 buffer_reset(vty
->lbuf
);
1341 vty
->status
= VTY_CLOSE
;
1344 for (i
= 0; i
< nbytes
; i
++) {
1345 if (buf
[i
] == IAC
) {
1354 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1355 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1356 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1362 /* In case of telnet command */
1364 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1371 if (vty
->status
== VTY_MORE
) {
1376 vty_buffer_reset(vty
);
1384 /* Escape character. */
1385 if (vty
->escape
== VTY_ESCAPE
) {
1386 vty_escape_map(buf
[i
], vty
);
1390 /* Pre-escape status. */
1391 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1394 vty
->escape
= VTY_ESCAPE
;
1397 vty_backward_word(vty
);
1398 vty
->escape
= VTY_NORMAL
;
1401 vty_forward_word(vty
);
1402 vty
->escape
= VTY_NORMAL
;
1405 vty_forward_kill_word(vty
);
1406 vty
->escape
= VTY_NORMAL
;
1410 vty_backward_kill_word(vty
);
1411 vty
->escape
= VTY_NORMAL
;
1414 vty
->escape
= VTY_NORMAL
;
1420 if (vty
->escape
== VTY_CR
) {
1421 /* if we get CR+NL, the NL results in an extra empty
1422 * prompt line being printed without this; just drop
1423 * the NL if it immediately follows CR.
1425 vty
->escape
= VTY_NORMAL
;
1433 vty_beginning_of_line(vty
);
1436 vty_backward_char(vty
);
1439 vty_stop_input(vty
);
1442 vty_delete_char(vty
);
1445 vty_end_of_line(vty
);
1448 vty_forward_char(vty
);
1452 vty_delete_backward_char(vty
);
1461 vty_previous_line(vty
);
1464 vty_transpose_chars(vty
);
1467 vty_kill_line_from_beginning(vty
);
1470 vty_backward_kill_word(vty
);
1473 vty_end_config(vty
);
1476 vty
->escape
= VTY_CR
;
1480 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1483 if (vty
->pass_fd
!= -1) {
1484 close(vty
->pass_fd
);
1489 vty_complete_command(vty
);
1492 if (vty
->node
== AUTH_NODE
1493 || vty
->node
== AUTH_ENABLE_NODE
)
1494 vty_self_insert(vty
, buf
[i
]);
1496 vty_describe_command(vty
);
1499 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1500 vty
->escape
= VTY_ESCAPE
;
1503 vty
->escape
= VTY_PRE_ESCAPE
;
1506 if (buf
[i
] > 31 && buf
[i
] < 127)
1507 vty_self_insert(vty
, buf
[i
]);
1513 if (vty
->status
== VTY_CLOSE
)
1516 vty_event(VTY_WRITE
, vty
);
1517 vty_event(VTY_READ
, vty
);
1521 /* Flush buffer to the vty. */
1522 static void vty_flush(struct thread
*thread
)
1525 buffer_status_t flushrc
;
1526 struct vty
*vty
= THREAD_ARG(thread
);
1528 /* Tempolary disable read thread. */
1529 if (vty
->lines
== 0)
1530 THREAD_OFF(vty
->t_read
);
1532 /* Function execution continue. */
1533 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1535 /* N.B. if width is 0, that means we don't know the window size. */
1536 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1537 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1538 else if (vty
->status
== VTY_MORELINE
)
1539 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1542 flushrc
= buffer_flush_window(
1543 vty
->obuf
, vty
->wfd
, vty
->width
,
1544 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1547 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1549 buffer_reset(vty
->lbuf
);
1550 buffer_reset(vty
->obuf
);
1554 if (vty
->status
== VTY_CLOSE
)
1557 vty
->status
= VTY_NORMAL
;
1558 if (vty
->lines
== 0)
1559 vty_event(VTY_READ
, vty
);
1562 case BUFFER_PENDING
:
1563 /* There is more data waiting to be written. */
1564 vty
->status
= VTY_MORE
;
1565 if (vty
->lines
== 0)
1566 vty_event(VTY_WRITE
, vty
);
1571 /* Allocate new vty struct. */
1572 struct vty
*vty_new(void)
1574 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1576 new->fd
= new->wfd
= -1;
1578 new->lbuf
= buffer_new(0);
1579 new->obuf
= buffer_new(0); /* Use default buffer size. */
1580 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1581 new->max
= VTY_BUFSIZ
;
1588 /* allocate and initialise vty */
1589 static struct vty
*vty_new_init(int vty_sock
)
1595 vty
->wfd
= vty_sock
;
1596 vty
->type
= VTY_TERM
;
1597 vty
->node
= AUTH_NODE
;
1602 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1605 vty
->xpath_index
= 0;
1606 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1607 vty
->private_config
= false;
1608 vty
->candidate_config
= vty_shared_candidate_config
;
1609 vty
->status
= VTY_NORMAL
;
1612 vty
->iac_sb_in_progress
= 0;
1615 vtys_add_tail(vty_sessions
, vty
);
1620 /* Create new vty structure. */
1621 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1623 char buf
[SU_ADDRSTRLEN
];
1626 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1628 /* Allocate new vty structure and set up default values. */
1629 vty
= vty_new_init(vty_sock
);
1631 /* configurable parameters not part of basic init */
1632 vty
->v_timeout
= vty_timeout_val
;
1633 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1634 if (no_password_check
) {
1636 vty
->node
= ENABLE_NODE
;
1638 vty
->node
= VIEW_NODE
;
1640 if (host
.lines
>= 0)
1641 vty
->lines
= host
.lines
;
1643 if (!no_password_check
) {
1644 /* Vty is not available if password isn't set. */
1645 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1646 vty_out(vty
, "Vty password is not set.\n");
1647 vty
->status
= VTY_CLOSE
;
1653 /* Say hello to the world. */
1655 if (!no_password_check
)
1656 vty_out(vty
, "\nUser Access Verification\n\n");
1658 /* Setting up terminal. */
1660 vty_will_suppress_go_ahead(vty
);
1662 vty_dont_linemode(vty
);
1663 vty_do_window_size(vty
);
1664 /* vty_dont_lflow_ahead (vty); */
1668 /* Add read/write thread. */
1669 vty_event(VTY_WRITE
, vty
);
1670 vty_event(VTY_READ
, vty
);
1675 /* create vty for stdio */
1676 static struct termios stdio_orig_termios
;
1677 static struct vty
*stdio_vty
= NULL
;
1678 static bool stdio_termios
= false;
1679 static void (*stdio_vty_atclose
)(int isexit
);
1681 static void vty_stdio_reset(int isexit
)
1685 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1686 stdio_termios
= false;
1690 if (stdio_vty_atclose
)
1691 stdio_vty_atclose(isexit
);
1692 stdio_vty_atclose
= NULL
;
1696 static void vty_stdio_atexit(void)
1701 void vty_stdio_suspend(void)
1706 THREAD_OFF(stdio_vty
->t_write
);
1707 THREAD_OFF(stdio_vty
->t_read
);
1708 THREAD_OFF(stdio_vty
->t_timeout
);
1711 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1712 stdio_termios
= false;
1715 void vty_stdio_resume(void)
1720 if (!tcgetattr(0, &stdio_orig_termios
)) {
1721 struct termios termios
;
1723 termios
= stdio_orig_termios
;
1724 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1725 | IGNCR
| ICRNL
| IXON
);
1726 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1727 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1728 termios
.c_cflag
|= CS8
;
1729 tcsetattr(0, TCSANOW
, &termios
);
1730 stdio_termios
= true;
1733 vty_prompt(stdio_vty
);
1735 /* Add read/write thread. */
1736 vty_event(VTY_WRITE
, stdio_vty
);
1737 vty_event(VTY_READ
, stdio_vty
);
1740 void vty_stdio_close(void)
1744 vty_close(stdio_vty
);
1747 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1751 /* refuse creating two vtys on stdio */
1755 vty
= stdio_vty
= vty_new_init(0);
1756 stdio_vty_atclose
= atclose
;
1759 /* always have stdio vty in a known _unchangeable_ state, don't want
1761 * to have any effect here to make sure scripting this works as intended
1763 vty
->node
= ENABLE_NODE
;
1765 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1771 /* Accept connection from the network. */
1772 static void vty_accept(struct thread
*thread
)
1774 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1779 int accept_sock
= vtyserv
->sock
;
1781 struct access_list
*acl
= NULL
;
1783 /* We continue hearing vty socket. */
1784 vty_event_serv(VTY_SERV
, vtyserv
);
1786 memset(&su
, 0, sizeof(union sockunion
));
1788 /* We can handle IPv4 or IPv6 socket. */
1789 vty_sock
= sockunion_accept(accept_sock
, &su
);
1791 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1792 safe_strerror(errno
));
1795 set_nonblocking(vty_sock
);
1796 set_cloexec(vty_sock
);
1798 if (!sockunion2hostprefix(&su
, &p
)) {
1800 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1805 /* VTY's accesslist apply. */
1806 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1807 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1808 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1809 zlog_info("Vty connection refused from %pSU", &su
);
1815 /* VTY's ipv6 accesslist apply. */
1816 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1817 if ((acl
= access_list_lookup(AFI_IP6
,
1818 vty_ipv6_accesslist_name
))
1819 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1820 zlog_info("Vty connection refused from %pSU", &su
);
1827 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1830 zlog_info("can't set sockopt to vty_sock : %s",
1831 safe_strerror(errno
));
1833 zlog_info("Vty connection from %pSU", &su
);
1835 vty_create(vty_sock
, &su
);
1838 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1841 struct addrinfo req
;
1842 struct addrinfo
*ainfo
;
1843 struct addrinfo
*ainfo_save
;
1845 char port_str
[BUFSIZ
];
1847 memset(&req
, 0, sizeof(req
));
1848 req
.ai_flags
= AI_PASSIVE
;
1849 req
.ai_family
= AF_UNSPEC
;
1850 req
.ai_socktype
= SOCK_STREAM
;
1851 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1852 port_str
[sizeof(port_str
) - 1] = '\0';
1854 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1857 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1865 struct vty_serv
*vtyserv
;
1867 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1870 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1871 ainfo
->ai_protocol
);
1875 sockopt_v6only(ainfo
->ai_family
, sock
);
1876 sockopt_reuseaddr(sock
);
1877 sockopt_reuseport(sock
);
1880 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1882 close(sock
); /* Avoid sd leak. */
1886 ret
= listen(sock
, 3);
1888 close(sock
); /* Avoid sd leak. */
1892 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1893 vtyserv
->sock
= sock
;
1894 vtyservs_add_tail(vty_servs
, vtyserv
);
1896 vty_event_serv(VTY_SERV
, vtyserv
);
1897 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1899 freeaddrinfo(ainfo_save
);
1903 /* For sockaddr_un. */
1906 /* VTY shell UNIX domain socket. */
1907 static void vty_serv_un(const char *path
)
1909 struct vty_serv
*vtyserv
;
1912 struct sockaddr_un serv
;
1914 struct zprivs_ids_t ids
;
1916 /* First of all, unlink existing socket */
1920 old_mask
= umask(0007);
1922 /* Make UNIX domain socket. */
1923 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1925 flog_err_sys(EC_LIB_SOCKET
,
1926 "Cannot create unix stream socket: %s",
1927 safe_strerror(errno
));
1931 /* Make server socket. */
1932 memset(&serv
, 0, sizeof(serv
));
1933 serv
.sun_family
= AF_UNIX
;
1934 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1935 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1936 len
= serv
.sun_len
= SUN_LEN(&serv
);
1938 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1939 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1943 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1945 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1946 safe_strerror(errno
));
1947 close(sock
); /* Avoid sd leak. */
1951 ret
= listen(sock
, 5);
1953 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1954 safe_strerror(errno
));
1955 close(sock
); /* Avoid sd leak. */
1961 zprivs_get_ids(&ids
);
1963 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1964 earlier for the case when we don't need to chown the file
1965 type casting it here to make a compare */
1966 if ((int)ids
.gid_vty
> 0) {
1967 /* set group of socket */
1968 if (chown(path
, -1, ids
.gid_vty
)) {
1969 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1970 "vty_serv_un: could chown socket, %s",
1971 safe_strerror(errno
));
1975 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1976 vtyserv
->sock
= sock
;
1977 vtyserv
->vtysh
= true;
1978 vtyservs_add_tail(vty_servs
, vtyserv
);
1980 vty_event_serv(VTYSH_SERV
, vtyserv
);
1983 /* #define VTYSH_DEBUG 1 */
1985 static void vtysh_accept(struct thread
*thread
)
1987 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1988 int accept_sock
= vtyserv
->sock
;
1991 struct sockaddr_un client
;
1994 vty_event_serv(VTYSH_SERV
, vtyserv
);
1996 memset(&client
, 0, sizeof(client
));
1997 client_len
= sizeof(struct sockaddr_un
);
1999 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2000 (socklen_t
*)&client_len
);
2003 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2004 safe_strerror(errno
));
2008 if (set_nonblocking(sock
) < 0) {
2011 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2012 sock
, safe_strerror(errno
));
2019 printf("VTY shell accept\n");
2020 #endif /* VTYSH_DEBUG */
2025 vty
->type
= VTY_SHELL_SERV
;
2026 vty
->node
= VIEW_NODE
;
2027 vtys_add_tail(vtysh_sessions
, vty
);
2029 vty_event(VTYSH_READ
, vty
);
2032 static int vtysh_do_pass_fd(struct vty
*vty
)
2034 struct iovec iov
[1] = {
2036 .iov_base
= vty
->pass_fd_status
,
2037 .iov_len
= sizeof(vty
->pass_fd_status
),
2041 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2042 struct cmsghdr align
;
2044 struct msghdr mh
= {
2046 .msg_iovlen
= array_size(iov
),
2047 .msg_control
= u
.buf
,
2048 .msg_controllen
= sizeof(u
.buf
),
2050 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2053 memset(&u
.buf
, 0, sizeof(u
.buf
));
2054 cmh
->cmsg_level
= SOL_SOCKET
;
2055 cmh
->cmsg_type
= SCM_RIGHTS
;
2056 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2057 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2059 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2060 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2061 return BUFFER_PENDING
;
2063 close(vty
->pass_fd
);
2065 vty
->status
= VTY_NORMAL
;
2068 return BUFFER_ERROR
;
2070 /* resume accepting commands (suspended in vtysh_read) */
2071 vty_event(VTYSH_READ
, vty
);
2073 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2074 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2076 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2077 return BUFFER_PENDING
;
2079 return BUFFER_EMPTY
;
2082 static int vtysh_flush(struct vty
*vty
)
2086 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2087 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2088 ret
= vtysh_do_pass_fd(vty
);
2091 case BUFFER_PENDING
:
2092 vty_event(VTYSH_WRITE
, vty
);
2095 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2097 buffer_reset(vty
->lbuf
);
2098 buffer_reset(vty
->obuf
);
2107 void vty_pass_fd(struct vty
*vty
, int fd
)
2109 if (vty
->pass_fd
!= -1)
2110 close(vty
->pass_fd
);
2115 static void vtysh_read(struct thread
*thread
)
2121 unsigned char buf
[VTY_READ_BUFSIZ
];
2123 uint8_t header
[4] = {0, 0, 0, 0};
2125 sock
= THREAD_FD(thread
);
2126 vty
= THREAD_ARG(thread
);
2128 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2130 if (ERRNO_IO_RETRY(errno
)) {
2131 vty_event(VTYSH_READ
, vty
);
2136 "%s: read failed on vtysh client fd %d, closing: %s",
2137 __func__
, sock
, safe_strerror(errno
));
2139 buffer_reset(vty
->lbuf
);
2140 buffer_reset(vty
->obuf
);
2143 printf("close vtysh\n");
2144 #endif /* VTYSH_DEBUG */
2149 printf("line: %.*s\n", nbytes
, buf
);
2150 #endif /* VTYSH_DEBUG */
2152 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2153 /* Clear command line buffer. */
2154 vty
->cp
= vty
->length
= 0;
2156 vty_out(vty
, "%% Command is too long.\n");
2158 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2159 vty
->buf
[vty
->length
++] = *p
;
2161 /* Pass this line to parser. */
2162 ret
= vty_execute(vty
);
2163 /* Note that vty_execute clears the command buffer and resets
2164 vty->length to 0. */
2166 /* Return result. */
2168 printf("result: %d\n", ret
);
2169 printf("vtysh node: %d\n", vty
->node
);
2170 #endif /* VTYSH_DEBUG */
2172 if (vty
->pass_fd
!= -1) {
2173 memset(vty
->pass_fd_status
, 0, 4);
2174 vty
->pass_fd_status
[3] = ret
;
2175 vty
->status
= VTY_PASSFD
;
2178 vty_event(VTYSH_WRITE
, vty
);
2180 /* this introduces a "sequence point"
2181 * command output is written normally,
2182 * read processing is suspended until
2184 * then retcode + FD is written
2185 * then normal processing resumes
2187 * => skip vty_event(VTYSH_READ, vty)!
2192 /* hack for asynchronous "write integrated"
2193 * - other commands in "buf" will be ditched
2194 * - input during pending config-write is
2196 if (ret
== CMD_SUSPEND
)
2199 /* warning: watchfrr hardcodes this result write
2202 buffer_put(vty
->obuf
, header
, 4);
2204 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2205 /* Try to flush results; exit if a write
2212 if (vty
->status
== VTY_CLOSE
)
2215 vty_event(VTYSH_READ
, vty
);
2218 static void vtysh_write(struct thread
*thread
)
2220 struct vty
*vty
= THREAD_ARG(thread
);
2227 /* Determine address family to bind. */
2228 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2230 /* If port is set to 0, do not listen on TCP/IP at all! */
2232 vty_serv_sock_addrinfo(addr
, port
);
2239 static void vty_error_delete(void *arg
)
2241 struct vty_error
*ve
= arg
;
2243 XFREE(MTYPE_TMP
, ve
);
2246 /* Close vty interface. Warning: call this only from functions that
2247 will be careful not to access the vty afterwards (since it has
2248 now been freed). This is safest from top-level functions (called
2249 directly by the thread dispatcher). */
2250 void vty_close(struct vty
*vty
)
2253 bool was_stdio
= false;
2255 /* Drop out of configure / transaction if needed. */
2256 vty_config_exit(vty
);
2258 /* Cancel threads.*/
2259 THREAD_OFF(vty
->t_read
);
2260 THREAD_OFF(vty
->t_write
);
2261 THREAD_OFF(vty
->t_timeout
);
2263 if (vty
->pass_fd
!= -1) {
2264 close(vty
->pass_fd
);
2267 zlog_live_close(&vty
->live_log
);
2270 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2272 /* Free input buffer. */
2273 buffer_free(vty
->obuf
);
2274 buffer_free(vty
->lbuf
);
2276 /* Free command history. */
2277 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2278 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2282 if (vty
->fd
!= -1) {
2283 if (vty
->type
== VTY_SHELL_SERV
)
2284 vtys_del(vtysh_sessions
, vty
);
2286 vtys_del(vty_sessions
, vty
);
2289 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2293 * note check is for fd > STDERR_FILENO, not fd != -1.
2294 * We never close stdin/stdout/stderr here, because we may be
2295 * running in foreground mode with logging to stdout. Also,
2296 * additionally, we'd need to replace these fds with /dev/null. */
2297 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2299 if (vty
->fd
> STDERR_FILENO
)
2301 if (vty
->fd
== STDIN_FILENO
)
2304 XFREE(MTYPE_VTY
, vty
->buf
);
2307 vty
->error
->del
= vty_error_delete
;
2308 list_delete(&vty
->error
);
2312 XFREE(MTYPE_VTY
, vty
);
2318 /* When time out occur output message then close connection. */
2319 static void vty_timeout(struct thread
*thread
)
2323 vty
= THREAD_ARG(thread
);
2327 buffer_reset(vty
->lbuf
);
2328 buffer_reset(vty
->obuf
);
2329 vty_out(vty
, "\nVty connection is timed out.\n");
2331 /* Close connection. */
2332 vty
->status
= VTY_CLOSE
;
2336 /* Read up configuration file from file_name. */
2337 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2341 struct vty_error
*ve
;
2342 struct listnode
*node
;
2343 unsigned int line_num
= 0;
2346 /* vty_close won't close stderr; if some config command prints
2347 * something it'll end up there. (not ideal; it'd be better if output
2348 * from a file-load went to logging instead. Also note that if this
2349 * function is called after daemonizing, stderr will be /dev/null.)
2351 * vty->fd will be -1 from vty_new()
2353 vty
->wfd
= STDERR_FILENO
;
2354 vty
->type
= VTY_FILE
;
2355 vty
->node
= CONFIG_NODE
;
2358 vty
->candidate_config
= config
;
2360 vty
->private_config
= true;
2361 vty
->candidate_config
= nb_config_new(NULL
);
2364 /* Execute configuration file */
2365 ret
= config_from_file(vty
, confp
, &line_num
);
2367 /* Flush any previous errors before printing messages below */
2368 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2370 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2371 const char *message
= NULL
;
2375 case CMD_ERR_AMBIGUOUS
:
2376 message
= "Ambiguous command";
2378 case CMD_ERR_NO_MATCH
:
2379 message
= "No such command";
2382 message
= "Command returned Warning";
2384 case CMD_WARNING_CONFIG_FAILED
:
2385 message
= "Command returned Warning Config Failed";
2387 case CMD_ERR_INCOMPLETE
:
2388 message
= "Command returned Incomplete";
2390 case CMD_ERR_EXEED_ARGC_MAX
:
2392 "Command exceeded maximum number of Arguments";
2395 message
= "Command returned unhandled error message";
2399 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2400 nl
= strchr(ve
->error_buf
, '\n');
2403 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2404 message
, ve
->line_num
, ve
->error_buf
);
2409 * Automatically commit the candidate configuration after
2410 * reading the configuration file.
2412 if (config
== NULL
) {
2413 struct nb_context context
= {};
2414 char errmsg
[BUFSIZ
] = {0};
2416 context
.client
= NB_CLIENT_CLI
;
2418 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2419 "Read configuration file", NULL
,
2420 errmsg
, sizeof(errmsg
));
2421 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2423 "%s: failed to read configuration file: %s (%s)",
2424 __func__
, nb_err_name(ret
), errmsg
);
2430 static FILE *vty_use_backup_config(const char *fullpath
)
2432 char *fullpath_sav
, *fullpath_tmp
;
2438 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2439 fullpath_sav
= malloc(fullpath_sav_sz
);
2440 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2441 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2443 sav
= open(fullpath_sav
, O_RDONLY
);
2449 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2450 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2452 /* Open file to configuration write. */
2453 tmp
= mkstemp(fullpath_tmp
);
2457 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2460 while ((c
= read(sav
, buffer
, 512)) > 0) {
2461 if (write(tmp
, buffer
, c
) <= 0)
2467 if (rename(fullpath_tmp
, fullpath
) == 0)
2468 ret
= fopen(fullpath
, "r");
2470 unlink(fullpath_tmp
);
2475 unlink(fullpath_tmp
);
2485 /* Read up configuration file from file_name. */
2486 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2487 char *config_default_dir
)
2489 char cwd
[MAXPATHLEN
];
2491 const char *fullpath
;
2493 bool read_success
= false;
2495 /* If -f flag specified. */
2496 if (config_file
!= NULL
) {
2497 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2498 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2501 "%s: failure to determine Current Working Directory %d!",
2503 goto tmp_free_and_out
;
2505 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2506 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2507 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2510 fullpath
= config_file
;
2512 confp
= fopen(fullpath
, "r");
2514 if (confp
== NULL
) {
2516 EC_LIB_BACKUP_CONFIG
,
2517 "%s: failed to open configuration file %s: %s, checking backup",
2518 __func__
, fullpath
, safe_strerror(errno
));
2520 confp
= vty_use_backup_config(fullpath
);
2522 flog_warn(EC_LIB_BACKUP_CONFIG
,
2523 "using backup configuration file!");
2527 "%s: can't open configuration file [%s]",
2528 __func__
, config_file
);
2529 goto tmp_free_and_out
;
2534 host_config_set(config_default_dir
);
2538 struct stat conf_stat
;
2540 /* !!!!PLEASE LEAVE!!!!
2541 * This is NEEDED for use with vtysh -b, or else you can get
2542 * a real configuration food fight with a lot garbage in the
2543 * merged configuration file it creates coming from the per
2544 * daemon configuration files. This also allows the daemons
2545 * to start if there default configuration file is not
2546 * present or ignore them, as needed when using vtysh -b to
2547 * configure the daemons at boot - MAG
2550 /* Stat for vtysh Zebra.conf, if found startup and wait for
2551 * boot configuration
2554 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2555 ret
= stat(integrate_default
, &conf_stat
);
2557 read_success
= true;
2558 goto tmp_free_and_out
;
2562 confp
= fopen(config_default_dir
, "r");
2563 if (confp
== NULL
) {
2566 "%s: failed to open configuration file %s: %s, checking backup",
2567 __func__
, config_default_dir
,
2568 safe_strerror(errno
));
2570 confp
= vty_use_backup_config(config_default_dir
);
2572 flog_warn(EC_LIB_BACKUP_CONFIG
,
2573 "using backup configuration file!");
2574 fullpath
= config_default_dir
;
2576 flog_err(EC_LIB_VTY
,
2577 "can't open configuration file [%s]",
2578 config_default_dir
);
2579 goto tmp_free_and_out
;
2582 fullpath
= config_default_dir
;
2585 vty_read_file(config
, confp
);
2586 read_success
= true;
2590 host_config_set(fullpath
);
2593 XFREE(MTYPE_TMP
, tmp
);
2595 return read_success
;
2598 static void update_xpath(struct vty
*vty
, const char *oldpath
,
2599 const char *newpath
)
2603 for (i
= 0; i
< vty
->xpath_index
; i
++) {
2604 if (!frrstr_startswith(vty
->xpath
[i
], oldpath
))
2607 char *tmp
= frrstr_replace(vty
->xpath
[i
], oldpath
, newpath
);
2608 strlcpy(vty
->xpath
[i
], tmp
, sizeof(vty
->xpath
[0]));
2609 XFREE(MTYPE_TMP
, tmp
);
2613 void vty_update_xpath(const char *oldpath
, const char *newpath
)
2617 frr_each (vtys
, vtysh_sessions
, vty
)
2618 update_xpath(vty
, oldpath
, newpath
);
2619 frr_each (vtys
, vty_sessions
, vty
)
2620 update_xpath(vty
, oldpath
, newpath
);
2623 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2625 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2626 vty_out(vty
, "%% Configuration is locked by other client\n");
2630 vty
->node
= CONFIG_NODE
;
2632 vty
->private_config
= private_config
;
2633 vty
->xpath_index
= 0;
2635 if (private_config
) {
2636 vty
->candidate_config
= nb_config_dup(running_config
);
2637 vty
->candidate_config_base
= nb_config_dup(running_config
);
2639 "Warning: uncommitted changes will be discarded on exit.\n\n");
2641 vty
->candidate_config
= vty_shared_candidate_config
;
2642 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2643 vty
->candidate_config_base
=
2644 nb_config_dup(running_config
);
2650 void vty_config_exit(struct vty
*vty
)
2652 enum node_type node
= vty
->node
;
2653 struct cmd_node
*cnode
;
2655 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2656 * somewhere below CONFIG_NODE */
2657 while (node
&& node
!= CONFIG_NODE
) {
2658 cnode
= vector_lookup(cmdvec
, node
);
2659 node
= cnode
->parent_node
;
2661 if (node
!= CONFIG_NODE
)
2662 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2665 while (vty
->node
!= ENABLE_NODE
)
2666 /* will call vty_config_node_exit() below */
2670 int vty_config_node_exit(struct vty
*vty
)
2672 vty
->xpath_index
= 0;
2674 /* Perform any pending commits. */
2675 (void)nb_cli_pending_commit_check(vty
);
2677 /* Check if there's a pending confirmed commit. */
2678 if (vty
->t_confirmed_commit_timeout
) {
2680 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2681 nb_cli_confirmed_commit_rollback(vty
);
2682 nb_cli_confirmed_commit_clean(vty
);
2685 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2687 if (vty
->candidate_config
) {
2688 if (vty
->private_config
)
2689 nb_config_free(vty
->candidate_config
);
2690 vty
->candidate_config
= NULL
;
2692 if (vty
->candidate_config_base
) {
2693 nb_config_free(vty
->candidate_config_base
);
2694 vty
->candidate_config_base
= NULL
;
2697 vty
->config
= false;
2701 /* Master of the threads. */
2702 static struct thread_master
*vty_master
;
2704 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2708 thread_add_read(vty_master
, vty_accept
, vty_serv
,
2709 vty_serv
->sock
, &vty_serv
->t_accept
);
2713 thread_add_read(vty_master
, vtysh_accept
, vty_serv
,
2714 vty_serv
->sock
, &vty_serv
->t_accept
);
2718 assert(!"vty_event_serv() called incorrectly");
2722 static void vty_event(enum vty_event event
, struct vty
*vty
)
2727 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2731 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2736 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2739 /* Time out treatment. */
2740 if (vty
->v_timeout
) {
2741 THREAD_OFF(vty
->t_timeout
);
2742 thread_add_timer(vty_master
, vty_timeout
, vty
,
2743 vty
->v_timeout
, &vty
->t_timeout
);
2747 thread_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2750 case VTY_TIMEOUT_RESET
:
2751 THREAD_OFF(vty
->t_timeout
);
2753 thread_add_timer(vty_master
, vty_timeout
, vty
,
2754 vty
->v_timeout
, &vty
->t_timeout
);
2757 assert(!"vty_event() called incorrectly");
2761 DEFUN_NOSH (config_who
,
2764 "Display who is on vty\n")
2768 frr_each (vtys
, vty_sessions
, v
)
2769 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2770 v
->config
? "*" : " ", v
->fd
, v
->address
,
2771 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2775 /* Move to vty configuration mode. */
2776 DEFUN_NOSH (line_vty
,
2779 "Configure a terminal line\n"
2780 "Virtual terminal\n")
2782 vty
->node
= VTY_NODE
;
2786 /* Set time out value. */
2787 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2788 const char *sec_str
)
2790 unsigned long timeout
= 0;
2792 /* min_str and sec_str are already checked by parser. So it must be
2793 all digit string. */
2795 timeout
= strtol(min_str
, NULL
, 10);
2799 timeout
+= strtol(sec_str
, NULL
, 10);
2801 vty_timeout_val
= timeout
;
2802 vty
->v_timeout
= timeout
;
2803 vty_event(VTY_TIMEOUT_RESET
, vty
);
2809 DEFUN (exec_timeout_min
,
2810 exec_timeout_min_cmd
,
2811 "exec-timeout (0-35791)",
2812 "Set timeout value\n"
2813 "Timeout value in minutes\n")
2816 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2819 DEFUN (exec_timeout_sec
,
2820 exec_timeout_sec_cmd
,
2821 "exec-timeout (0-35791) (0-2147483)",
2822 "Set the EXEC timeout\n"
2823 "Timeout in minutes\n"
2824 "Timeout in seconds\n")
2827 int idx_number_2
= 2;
2828 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2829 argv
[idx_number_2
]->arg
);
2832 DEFUN (no_exec_timeout
,
2833 no_exec_timeout_cmd
,
2836 "Set the EXEC timeout\n")
2838 return exec_timeout(vty
, NULL
, NULL
);
2841 /* Set vty access class. */
2842 DEFUN (vty_access_class
,
2843 vty_access_class_cmd
,
2844 "access-class WORD",
2845 "Filter connections based on an IP access list\n"
2849 if (vty_accesslist_name
)
2850 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2852 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2857 /* Clear vty access class. */
2858 DEFUN (no_vty_access_class
,
2859 no_vty_access_class_cmd
,
2860 "no access-class [WORD]",
2862 "Filter connections based on an IP access list\n"
2866 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2867 if (!vty_accesslist_name
2868 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2869 vty_out(vty
, "Access-class is not currently applied to vty\n");
2870 return CMD_WARNING_CONFIG_FAILED
;
2873 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2875 vty_accesslist_name
= NULL
;
2880 /* Set vty access class. */
2881 DEFUN (vty_ipv6_access_class
,
2882 vty_ipv6_access_class_cmd
,
2883 "ipv6 access-class WORD",
2885 "Filter connections based on an IP access list\n"
2886 "IPv6 access list\n")
2889 if (vty_ipv6_accesslist_name
)
2890 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2892 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2897 /* Clear vty access class. */
2898 DEFUN (no_vty_ipv6_access_class
,
2899 no_vty_ipv6_access_class_cmd
,
2900 "no ipv6 access-class [WORD]",
2903 "Filter connections based on an IP access list\n"
2904 "IPv6 access list\n")
2907 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2909 if (!vty_ipv6_accesslist_name
2910 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2912 "IPv6 access-class is not currently applied to vty\n");
2913 return CMD_WARNING_CONFIG_FAILED
;
2916 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2918 vty_ipv6_accesslist_name
= NULL
;
2927 "Enable password checking\n")
2929 no_password_check
= 0;
2933 DEFUN (no_vty_login
,
2937 "Enable password checking\n")
2939 no_password_check
= 1;
2943 DEFUN (service_advanced_vty
,
2944 service_advanced_vty_cmd
,
2945 "service advanced-vty",
2946 "Set up miscellaneous service\n"
2947 "Enable advanced mode vty interface\n")
2953 DEFUN (no_service_advanced_vty
,
2954 no_service_advanced_vty_cmd
,
2955 "no service advanced-vty",
2957 "Set up miscellaneous service\n"
2958 "Enable advanced mode vty interface\n")
2964 DEFUN_NOSH(terminal_monitor
,
2965 terminal_monitor_cmd
,
2966 "terminal monitor [detach]",
2967 "Set terminal line parameters\n"
2968 "Copy debug output to the current terminal line\n"
2969 "Keep logging feed open independent of VTY session\n")
2973 if (vty
->type
!= VTY_SHELL_SERV
) {
2974 vty_out(vty
, "%% not supported\n");
2979 struct zlog_live_cfg detach_log
= {};
2981 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
2982 zlog_live_disown(&detach_log
);
2984 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
2987 vty_out(vty
, "%% error opening live log: %m\n");
2991 vty_pass_fd(vty
, fd_ret
);
2995 DEFUN_NOSH(no_terminal_monitor
,
2996 no_terminal_monitor_cmd
,
2997 "no terminal monitor",
2999 "Set terminal line parameters\n"
3000 "Copy debug output to the current terminal line\n")
3002 zlog_live_close(&vty
->live_log
);
3006 DEFUN_NOSH(terminal_no_monitor
,
3007 terminal_no_monitor_cmd
,
3008 "terminal no monitor",
3009 "Set terminal line parameters\n"
3011 "Copy debug output to the current terminal line\n")
3013 return no_terminal_monitor(self
, vty
, argc
, argv
);
3017 DEFUN_NOSH (show_history
,
3021 "Display the session command history\n")
3025 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3026 if (index
== VTY_MAXHIST
) {
3031 if (vty
->hist
[index
] != NULL
)
3032 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3041 DEFPY (log_commands
,
3043 "[no] log commands",
3046 "Log all commands\n")
3049 if (do_log_commands_perm
) {
3051 "Daemon started with permanent logging turned on for commands, ignoring\n");
3055 do_log_commands
= false;
3057 do_log_commands
= true;
3062 /* Display current configuration. */
3063 static int vty_config_write(struct vty
*vty
)
3065 vty_frame(vty
, "line vty\n");
3067 if (vty_accesslist_name
)
3068 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3070 if (vty_ipv6_accesslist_name
)
3071 vty_out(vty
, " ipv6 access-class %s\n",
3072 vty_ipv6_accesslist_name
);
3075 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3076 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3077 vty_timeout_val
% 60);
3080 if (no_password_check
)
3081 vty_out(vty
, " no login\n");
3083 vty_endframe(vty
, "exit\n");
3085 if (do_log_commands
)
3086 vty_out(vty
, "log commands\n");
3088 vty_out(vty
, "!\n");
3093 static int vty_config_write(struct vty
*vty
);
3094 struct cmd_node vty_node
= {
3097 .parent_node
= CONFIG_NODE
,
3098 .prompt
= "%s(config-line)# ",
3099 .config_write
= vty_config_write
,
3102 /* Reset all VTY status. */
3103 void vty_reset(void)
3107 frr_each_safe (vtys
, vty_sessions
, vty
) {
3108 buffer_reset(vty
->lbuf
);
3109 buffer_reset(vty
->obuf
);
3110 vty
->status
= VTY_CLOSE
;
3114 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3116 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3117 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3120 static void vty_save_cwd(void)
3124 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3128 * At this point if these go wrong, more than likely
3129 * the whole world is coming down around us
3130 * Hence not worrying about it too much.
3132 if (chdir(SYSCONFDIR
)) {
3133 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3134 "Failure to chdir to %s, errno: %d",
3138 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3139 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3140 "Failure to getcwd, errno: %d", errno
);
3146 char *vty_get_cwd(void)
3151 int vty_shell(struct vty
*vty
)
3153 return vty
->type
== VTY_SHELL
? 1 : 0;
3156 int vty_shell_serv(struct vty
*vty
)
3158 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3161 void vty_init_vtysh(void)
3163 /* currently nothing to do, but likely to have future use */
3166 /* Install vty's own commands like `who' command. */
3167 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3169 /* For further configuration read, preserve current directory. */
3172 vty_master
= master_thread
;
3174 atexit(vty_stdio_atexit
);
3176 /* Install bgp top node. */
3177 install_node(&vty_node
);
3179 install_element(VIEW_NODE
, &config_who_cmd
);
3180 install_element(VIEW_NODE
, &show_history_cmd
);
3181 install_element(CONFIG_NODE
, &line_vty_cmd
);
3182 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3183 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3184 install_element(CONFIG_NODE
, &show_history_cmd
);
3185 install_element(CONFIG_NODE
, &log_commands_cmd
);
3187 if (do_command_logging
) {
3188 do_log_commands
= true;
3189 do_log_commands_perm
= true;
3192 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3193 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3194 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3196 install_default(VTY_NODE
);
3197 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3198 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3199 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3200 install_element(VTY_NODE
, &vty_access_class_cmd
);
3201 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3202 install_element(VTY_NODE
, &vty_login_cmd
);
3203 install_element(VTY_NODE
, &no_vty_login_cmd
);
3204 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3205 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3208 void vty_terminate(void)
3211 struct vty_serv
*vtyserv
;
3213 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3217 /* default state of vty_sessions is initialized & empty. */
3218 vtys_fini(vty_sessions
);
3219 vtys_init(vty_sessions
);
3221 /* vty_reset() doesn't close vtysh sessions */
3222 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3223 buffer_reset(vty
->lbuf
);
3224 buffer_reset(vty
->obuf
);
3225 vty
->status
= VTY_CLOSE
;
3229 vtys_fini(vtysh_sessions
);
3230 vtys_init(vtysh_sessions
);
3232 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
3233 THREAD_OFF(vtyserv
->t_accept
);
3234 close(vtyserv
->sock
);
3235 XFREE(MTYPE_VTY_SERV
, vtyserv
);
3238 vtyservs_fini(vty_servs
);
3239 vtyservs_init(vty_servs
);