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 /* Put out prompt and wait input from user. */
347 static void vty_prompt(struct vty
*vty
)
349 if (vty
->type
== VTY_TERM
) {
350 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
354 /* Send WILL TELOPT_ECHO to remote server. */
355 static void vty_will_echo(struct vty
*vty
)
357 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
358 vty_out(vty
, "%s", cmd
);
361 /* Make suppress Go-Ahead telnet option. */
362 static void vty_will_suppress_go_ahead(struct vty
*vty
)
364 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
365 vty_out(vty
, "%s", cmd
);
368 /* Make don't use linemode over telnet. */
369 static void vty_dont_linemode(struct vty
*vty
)
371 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
372 vty_out(vty
, "%s", cmd
);
375 /* Use window size. */
376 static void vty_do_window_size(struct vty
*vty
)
378 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
379 vty_out(vty
, "%s", cmd
);
382 /* Authentication of vty */
383 static void vty_auth(struct vty
*vty
, char *buf
)
386 enum node_type next_node
= 0;
392 passwd
= host
.password_encrypt
;
394 passwd
= host
.password
;
396 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
398 next_node
= VIEW_NODE
;
400 case AUTH_ENABLE_NODE
:
402 passwd
= host
.enable_encrypt
;
404 passwd
= host
.enable
;
405 next_node
= ENABLE_NODE
;
411 fail
= strcmp(crypt(buf
, passwd
), passwd
);
413 fail
= strcmp(buf
, passwd
);
419 vty
->node
= next_node
; /* Success ! */
422 if (vty
->fail
>= 3) {
423 if (vty
->node
== AUTH_NODE
) {
425 "%% Bad passwords, too many failures!\n");
426 vty
->status
= VTY_CLOSE
;
428 /* AUTH_ENABLE_NODE */
431 "%% Bad enable passwords, too many failures!\n");
432 vty
->status
= VTY_CLOSE
;
438 /* Command execution over the vty interface. */
439 static int vty_command(struct vty
*vty
, char *buf
)
442 const char *protocolname
;
448 * Log non empty command lines
450 if (do_log_commands
&&
451 strncmp(buf
, "echo PING", strlen("echo PING")) != 0)
454 /* Skip white spaces. */
455 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
458 if (cp
!= NULL
&& *cp
!= '\0') {
459 char vty_str
[VTY_BUFSIZ
];
460 char prompt_str
[VTY_BUFSIZ
];
462 /* format the base vty info */
463 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
466 /* format the prompt */
467 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
470 /* now log the command */
471 zlog_notice("%s%s", prompt_str
, buf
);
476 unsigned long walltime
, cputime
;
478 /* cmd_execute() may change cputime_enabled if we're executing the
479 * "service cputime-stats" command, which can result in nonsensical
480 * and very confusing warnings
482 bool cputime_enabled_here
= cputime_enabled
;
486 ret
= cmd_execute(vty
, buf
, NULL
, 0);
490 walltime
= thread_consumed_time(&after
, &before
, &cputime
);
492 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
493 && cputime
> cputime_threshold
)
494 /* Warn about CPU hog that must be fixed. */
495 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
496 "CPU HOG: command took %lums (cpu time %lums): %s",
497 walltime
/ 1000, cputime
/ 1000, buf
);
498 else if (walltime_threshold
&& walltime
> walltime_threshold
)
499 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
500 "STARVATION: command took %lums (cpu time %lums): %s",
501 walltime
/ 1000, cputime
/ 1000, buf
);
503 /* Get the name of the protocol if any */
504 protocolname
= frr_protoname
;
506 if (ret
!= CMD_SUCCESS
)
509 if (vty
->type
== VTY_FILE
)
510 vty_out(vty
, "Warning...\n");
512 case CMD_ERR_AMBIGUOUS
:
513 vty_out(vty
, "%% Ambiguous command.\n");
515 case CMD_ERR_NO_MATCH
:
516 vty_out(vty
, "%% [%s] Unknown command: %s\n",
519 case CMD_ERR_INCOMPLETE
:
520 vty_out(vty
, "%% Command incomplete.\n");
527 static const char telnet_backward_char
= 0x08;
528 static const char telnet_space_char
= ' ';
530 /* Basic function to write buffer to vty. */
531 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
533 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
536 /* Should we do buffering here ? And make vty_flush (vty) ? */
537 buffer_put(vty
->obuf
, buf
, nbytes
);
540 /* Basic function to insert character into vty. */
541 static void vty_self_insert(struct vty
*vty
, char c
)
546 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
549 length
= vty
->length
- vty
->cp
;
550 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
551 vty
->buf
[vty
->cp
] = c
;
553 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
554 for (i
= 0; i
< length
; i
++)
555 vty_write(vty
, &telnet_backward_char
, 1);
560 vty
->buf
[vty
->length
] = '\0';
563 /* Self insert character 'c' in overwrite mode. */
564 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
566 if (vty
->cp
== vty
->length
) {
567 vty_self_insert(vty
, c
);
571 vty
->buf
[vty
->cp
++] = c
;
572 vty_write(vty
, &c
, 1);
576 * Insert a string into vty->buf at the current cursor position.
578 * If the resultant string would be larger than VTY_BUFSIZ it is
581 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
583 if (vty
->cp
== VTY_BUFSIZ
)
586 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
587 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
589 vty
->length
= MAX(vty
->cp
, vty
->length
);
590 vty
->buf
[vty
->length
] = '\0';
591 vty_write(vty
, str
, nwrite
);
594 /* Forward character. */
595 static void vty_forward_char(struct vty
*vty
)
597 if (vty
->cp
< vty
->length
) {
598 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
603 /* Backward character. */
604 static void vty_backward_char(struct vty
*vty
)
608 vty_write(vty
, &telnet_backward_char
, 1);
612 /* Move to the beginning of the line. */
613 static void vty_beginning_of_line(struct vty
*vty
)
616 vty_backward_char(vty
);
619 /* Move to the end of the line. */
620 static void vty_end_of_line(struct vty
*vty
)
622 while (vty
->cp
< vty
->length
)
623 vty_forward_char(vty
);
626 static void vty_kill_line_from_beginning(struct vty
*);
627 static void vty_redraw_line(struct vty
*);
629 /* Print command line history. This function is called from
630 vty_next_line and vty_previous_line. */
631 static void vty_history_print(struct vty
*vty
)
635 vty_kill_line_from_beginning(vty
);
637 /* Get previous line from history buffer */
638 length
= strlen(vty
->hist
[vty
->hp
]);
639 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
640 vty
->cp
= vty
->length
= length
;
641 vty
->buf
[vty
->length
] = '\0';
643 /* Redraw current line */
644 vty_redraw_line(vty
);
647 /* Show next command line history. */
648 static void vty_next_line(struct vty
*vty
)
652 if (vty
->hp
== vty
->hindex
)
655 /* Try is there history exist or not. */
657 if (try_index
== (VTY_MAXHIST
- 1))
662 /* If there is not history return. */
663 if (vty
->hist
[try_index
] == NULL
)
668 vty_history_print(vty
);
671 /* Show previous command line history. */
672 static void vty_previous_line(struct vty
*vty
)
678 try_index
= VTY_MAXHIST
- 1;
682 if (vty
->hist
[try_index
] == NULL
)
687 vty_history_print(vty
);
690 /* This function redraw all of the command line character. */
691 static void vty_redraw_line(struct vty
*vty
)
693 vty_write(vty
, vty
->buf
, vty
->length
);
694 vty
->cp
= vty
->length
;
698 static void vty_forward_word(struct vty
*vty
)
700 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
701 vty_forward_char(vty
);
703 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
704 vty_forward_char(vty
);
707 /* Backward word without skipping training space. */
708 static void vty_backward_pure_word(struct vty
*vty
)
710 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
711 vty_backward_char(vty
);
715 static void vty_backward_word(struct vty
*vty
)
717 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
718 vty_backward_char(vty
);
720 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
721 vty_backward_char(vty
);
724 /* When '^D' is typed at the beginning of the line we move to the down
726 static void vty_down_level(struct vty
*vty
)
734 /* When '^Z' is received from vty, move down to the enable mode. */
735 static void vty_end_config(struct vty
*vty
)
740 vty_config_exit(vty
);
741 vty
->node
= ENABLE_NODE
;
748 /* Delete a character at the current point. */
749 static void vty_delete_char(struct vty
*vty
)
754 if (vty
->length
== 0) {
759 if (vty
->cp
== vty
->length
)
760 return; /* completion need here? */
762 size
= vty
->length
- vty
->cp
;
765 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
766 vty
->buf
[vty
->length
] = '\0';
768 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
771 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
772 vty_write(vty
, &telnet_space_char
, 1);
774 for (i
= 0; i
< size
; i
++)
775 vty_write(vty
, &telnet_backward_char
, 1);
778 /* Delete a character before the point. */
779 static void vty_delete_backward_char(struct vty
*vty
)
784 vty_backward_char(vty
);
785 vty_delete_char(vty
);
788 /* Kill rest of line from current point. */
789 static void vty_kill_line(struct vty
*vty
)
794 size
= vty
->length
- vty
->cp
;
799 for (i
= 0; i
< size
; i
++)
800 vty_write(vty
, &telnet_space_char
, 1);
801 for (i
= 0; i
< size
; i
++)
802 vty_write(vty
, &telnet_backward_char
, 1);
804 memset(&vty
->buf
[vty
->cp
], 0, size
);
805 vty
->length
= vty
->cp
;
808 /* Kill line from the beginning. */
809 static void vty_kill_line_from_beginning(struct vty
*vty
)
811 vty_beginning_of_line(vty
);
815 /* Delete a word before the point. */
816 static void vty_forward_kill_word(struct vty
*vty
)
818 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
819 vty_delete_char(vty
);
820 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
821 vty_delete_char(vty
);
824 /* Delete a word before the point. */
825 static void vty_backward_kill_word(struct vty
*vty
)
827 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
828 vty_delete_backward_char(vty
);
829 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
830 vty_delete_backward_char(vty
);
833 /* Transpose chars before or at the point. */
834 static void vty_transpose_chars(struct vty
*vty
)
838 /* If length is short or point is near by the beginning of line then
840 if (vty
->length
< 2 || vty
->cp
< 1)
843 /* In case of point is located at the end of the line. */
844 if (vty
->cp
== vty
->length
) {
845 c1
= vty
->buf
[vty
->cp
- 1];
846 c2
= vty
->buf
[vty
->cp
- 2];
848 vty_backward_char(vty
);
849 vty_backward_char(vty
);
850 vty_self_insert_overwrite(vty
, c1
);
851 vty_self_insert_overwrite(vty
, c2
);
853 c1
= vty
->buf
[vty
->cp
];
854 c2
= vty
->buf
[vty
->cp
- 1];
856 vty_backward_char(vty
);
857 vty_self_insert_overwrite(vty
, c1
);
858 vty_self_insert_overwrite(vty
, c2
);
862 /* Do completion at vty interface. */
863 static void vty_complete_command(struct vty
*vty
)
867 char **matched
= NULL
;
870 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
873 vline
= cmd_make_strvec(vty
->buf
);
877 /* In case of 'help \t'. */
878 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
879 vector_set(vline
, NULL
);
881 matched
= cmd_complete_command(vline
, vty
, &ret
);
883 cmd_free_strvec(vline
);
887 case CMD_ERR_AMBIGUOUS
:
888 vty_out(vty
, "%% Ambiguous command.\n");
890 vty_redraw_line(vty
);
892 case CMD_ERR_NO_MATCH
:
893 /* vty_out (vty, "%% There is no matched command.\n"); */
895 vty_redraw_line(vty
);
897 case CMD_COMPLETE_FULL_MATCH
:
899 /* 2016-11-28 equinox -- need to debug, SEGV here */
900 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
902 vty_redraw_line(vty
);
906 vty_redraw_line(vty
);
907 vty_backward_pure_word(vty
);
908 vty_insert_word_overwrite(vty
, matched
[0]);
909 vty_self_insert(vty
, ' ');
910 XFREE(MTYPE_COMPLETION
, matched
[0]);
912 case CMD_COMPLETE_MATCH
:
914 vty_redraw_line(vty
);
915 vty_backward_pure_word(vty
);
916 vty_insert_word_overwrite(vty
, matched
[0]);
917 XFREE(MTYPE_COMPLETION
, matched
[0]);
919 case CMD_COMPLETE_LIST_MATCH
:
920 for (i
= 0; matched
[i
] != NULL
; i
++) {
921 if (i
!= 0 && ((i
% 6) == 0))
923 vty_out(vty
, "%-10s ", matched
[i
]);
924 XFREE(MTYPE_COMPLETION
, matched
[i
]);
929 vty_redraw_line(vty
);
931 case CMD_ERR_NOTHING_TODO
:
933 vty_redraw_line(vty
);
938 XFREE(MTYPE_TMP
, matched
);
941 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
942 unsigned int desc_width
, struct cmd_token
*token
)
950 if (desc_width
<= 0) {
951 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
955 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
957 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
958 for (pos
= desc_width
; pos
> 0; pos
--)
959 if (*(p
+ pos
) == ' ')
967 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
972 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
974 XFREE(MTYPE_TMP
, buf
);
977 /* Describe matched command function. */
978 static void vty_describe_command(struct vty
*vty
)
983 unsigned int i
, width
, desc_width
;
984 struct cmd_token
*token
, *token_cr
= NULL
;
986 vline
= cmd_make_strvec(vty
->buf
);
988 /* In case of '> ?'. */
990 vline
= vector_init(1);
991 vector_set(vline
, NULL
);
992 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
993 vector_set(vline
, NULL
);
995 describe
= cmd_describe_command(vline
, vty
, &ret
);
999 /* Ambiguous error. */
1001 case CMD_ERR_AMBIGUOUS
:
1002 vty_out(vty
, "%% Ambiguous command.\n");
1005 case CMD_ERR_NO_MATCH
:
1006 vty_out(vty
, "%% There is no matched command.\n");
1011 /* Get width of command string. */
1013 for (i
= 0; i
< vector_active(describe
); i
++)
1014 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1017 if (token
->text
[0] == '\0')
1020 len
= strlen(token
->text
);
1026 /* Get width of description string. */
1027 desc_width
= vty
->width
- (width
+ 6);
1029 /* Print out description. */
1030 for (i
= 0; i
< vector_active(describe
); i
++)
1031 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1032 if (token
->text
[0] == '\0')
1035 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1041 vty_out(vty
, " %-s\n", token
->text
);
1042 else if (desc_width
>= strlen(token
->desc
))
1043 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1046 vty_describe_fold(vty
, width
, desc_width
,
1049 if (IS_VARYING_TOKEN(token
->type
)) {
1050 const char *ref
= vector_slot(
1051 vline
, vector_active(vline
) - 1);
1053 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1054 cmd_variable_complete(token
, ref
, varcomps
);
1056 if (vector_active(varcomps
) > 0) {
1057 char *ac
= cmd_variable_comp2str(
1058 varcomps
, vty
->width
);
1059 vty_out(vty
, "%s\n", ac
);
1060 XFREE(MTYPE_TMP
, ac
);
1063 vector_free(varcomps
);
1067 if ((token
= token_cr
)) {
1069 vty_out(vty
, " %-s\n", token
->text
);
1070 else if (desc_width
>= strlen(token
->desc
))
1071 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1074 vty_describe_fold(vty
, width
, desc_width
, token
);
1078 cmd_free_strvec(vline
);
1080 vector_free(describe
);
1083 vty_redraw_line(vty
);
1086 static void vty_clear_buf(struct vty
*vty
)
1088 memset(vty
->buf
, 0, vty
->max
);
1091 /* ^C stop current input and do not add command line to the history. */
1092 static void vty_stop_input(struct vty
*vty
)
1094 vty
->cp
= vty
->length
= 0;
1099 vty_config_exit(vty
);
1100 vty
->node
= ENABLE_NODE
;
1105 /* Set history pointer to the latest one. */
1106 vty
->hp
= vty
->hindex
;
1109 /* Add current command line to the history buffer. */
1110 static void vty_hist_add(struct vty
*vty
)
1114 if (vty
->length
== 0)
1117 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1119 /* Ignore the same string as previous one. */
1120 if (vty
->hist
[index
])
1121 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1122 vty
->hp
= vty
->hindex
;
1126 /* Insert history entry. */
1127 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1128 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1130 /* History index rotation. */
1132 if (vty
->hindex
== VTY_MAXHIST
)
1135 vty
->hp
= vty
->hindex
;
1138 /* #define TELNET_OPTION_DEBUG */
1140 /* Get telnet window size. */
1141 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1143 #ifdef TELNET_OPTION_DEBUG
1146 for (i
= 0; i
< nbytes
; i
++) {
1149 vty_out(vty
, "IAC ");
1152 vty_out(vty
, "WILL ");
1155 vty_out(vty
, "WONT ");
1158 vty_out(vty
, "DO ");
1161 vty_out(vty
, "DONT ");
1164 vty_out(vty
, "SB ");
1167 vty_out(vty
, "SE ");
1170 vty_out(vty
, "TELOPT_ECHO \n");
1173 vty_out(vty
, "TELOPT_SGA \n");
1176 vty_out(vty
, "TELOPT_NAWS \n");
1179 vty_out(vty
, "%x ", buf
[i
]);
1185 #endif /* TELNET_OPTION_DEBUG */
1190 vty
->iac_sb_in_progress
= 1;
1193 if (!vty
->iac_sb_in_progress
)
1196 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1197 vty
->iac_sb_in_progress
= 0;
1200 switch (vty
->sb_buf
[0]) {
1202 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1205 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1207 (unsigned long)vty
->sb_len
);
1208 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1211 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1212 (unsigned long)sizeof(vty
->sb_buf
),
1213 TELNET_NAWS_SB_LEN
);
1215 vty
->width
= ((vty
->sb_buf
[1] << 8)
1217 vty
->height
= ((vty
->sb_buf
[3] << 8)
1219 #ifdef TELNET_OPTION_DEBUG
1221 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1222 vty
->width
, vty
->height
);
1227 vty
->iac_sb_in_progress
= 0;
1236 /* Execute current command line. */
1237 static int vty_execute(struct vty
*vty
)
1243 switch (vty
->node
) {
1245 case AUTH_ENABLE_NODE
:
1246 vty_auth(vty
, vty
->buf
);
1249 ret
= vty_command(vty
, vty
->buf
);
1250 if (vty
->type
== VTY_TERM
)
1255 /* Clear command line buffer. */
1256 vty
->cp
= vty
->length
= 0;
1259 if (vty
->status
!= VTY_CLOSE
)
1265 #define CONTROL(X) ((X) - '@')
1266 #define VTY_NORMAL 0
1267 #define VTY_PRE_ESCAPE 1
1268 #define VTY_ESCAPE 2
1271 /* Escape character command map. */
1272 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1276 vty_previous_line(vty
);
1282 vty_forward_char(vty
);
1285 vty_backward_char(vty
);
1291 /* Go back to normal mode. */
1292 vty
->escape
= VTY_NORMAL
;
1295 /* Quit print out to the buffer. */
1296 static void vty_buffer_reset(struct vty
*vty
)
1298 buffer_reset(vty
->obuf
);
1299 buffer_reset(vty
->lbuf
);
1301 vty_redraw_line(vty
);
1304 /* Read data via vty socket. */
1305 static void vty_read(struct thread
*thread
)
1309 unsigned char buf
[VTY_READ_BUFSIZ
];
1311 struct vty
*vty
= THREAD_ARG(thread
);
1313 /* Read raw data from socket */
1314 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1316 if (ERRNO_IO_RETRY(errno
)) {
1317 vty_event(VTY_READ
, vty
);
1322 "%s: read error on vty client fd %d, closing: %s",
1323 __func__
, vty
->fd
, safe_strerror(errno
));
1324 buffer_reset(vty
->obuf
);
1325 buffer_reset(vty
->lbuf
);
1327 vty
->status
= VTY_CLOSE
;
1330 for (i
= 0; i
< nbytes
; i
++) {
1331 if (buf
[i
] == IAC
) {
1340 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1341 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1342 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1348 /* In case of telnet command */
1350 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1357 if (vty
->status
== VTY_MORE
) {
1362 vty_buffer_reset(vty
);
1370 /* Escape character. */
1371 if (vty
->escape
== VTY_ESCAPE
) {
1372 vty_escape_map(buf
[i
], vty
);
1376 /* Pre-escape status. */
1377 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1380 vty
->escape
= VTY_ESCAPE
;
1383 vty_backward_word(vty
);
1384 vty
->escape
= VTY_NORMAL
;
1387 vty_forward_word(vty
);
1388 vty
->escape
= VTY_NORMAL
;
1391 vty_forward_kill_word(vty
);
1392 vty
->escape
= VTY_NORMAL
;
1396 vty_backward_kill_word(vty
);
1397 vty
->escape
= VTY_NORMAL
;
1400 vty
->escape
= VTY_NORMAL
;
1406 if (vty
->escape
== VTY_CR
) {
1407 /* if we get CR+NL, the NL results in an extra empty
1408 * prompt line being printed without this; just drop
1409 * the NL if it immediately follows CR.
1411 vty
->escape
= VTY_NORMAL
;
1419 vty_beginning_of_line(vty
);
1422 vty_backward_char(vty
);
1425 vty_stop_input(vty
);
1428 vty_delete_char(vty
);
1431 vty_end_of_line(vty
);
1434 vty_forward_char(vty
);
1438 vty_delete_backward_char(vty
);
1447 vty_previous_line(vty
);
1450 vty_transpose_chars(vty
);
1453 vty_kill_line_from_beginning(vty
);
1456 vty_backward_kill_word(vty
);
1459 vty_end_config(vty
);
1462 vty
->escape
= VTY_CR
;
1466 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1469 if (vty
->pass_fd
!= -1) {
1470 close(vty
->pass_fd
);
1475 vty_complete_command(vty
);
1478 if (vty
->node
== AUTH_NODE
1479 || vty
->node
== AUTH_ENABLE_NODE
)
1480 vty_self_insert(vty
, buf
[i
]);
1482 vty_describe_command(vty
);
1485 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1486 vty
->escape
= VTY_ESCAPE
;
1489 vty
->escape
= VTY_PRE_ESCAPE
;
1492 if (buf
[i
] > 31 && buf
[i
] < 127)
1493 vty_self_insert(vty
, buf
[i
]);
1499 if (vty
->status
== VTY_CLOSE
)
1502 vty_event(VTY_WRITE
, vty
);
1503 vty_event(VTY_READ
, vty
);
1507 /* Flush buffer to the vty. */
1508 static void vty_flush(struct thread
*thread
)
1511 buffer_status_t flushrc
;
1512 struct vty
*vty
= THREAD_ARG(thread
);
1514 /* Tempolary disable read thread. */
1515 if (vty
->lines
== 0)
1516 THREAD_OFF(vty
->t_read
);
1518 /* Function execution continue. */
1519 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1521 /* N.B. if width is 0, that means we don't know the window size. */
1522 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1523 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1524 else if (vty
->status
== VTY_MORELINE
)
1525 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1528 flushrc
= buffer_flush_window(
1529 vty
->obuf
, vty
->wfd
, vty
->width
,
1530 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1533 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1535 buffer_reset(vty
->lbuf
);
1536 buffer_reset(vty
->obuf
);
1540 if (vty
->status
== VTY_CLOSE
)
1543 vty
->status
= VTY_NORMAL
;
1544 if (vty
->lines
== 0)
1545 vty_event(VTY_READ
, vty
);
1548 case BUFFER_PENDING
:
1549 /* There is more data waiting to be written. */
1550 vty
->status
= VTY_MORE
;
1551 if (vty
->lines
== 0)
1552 vty_event(VTY_WRITE
, vty
);
1557 /* Allocate new vty struct. */
1558 struct vty
*vty_new(void)
1560 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1562 new->fd
= new->wfd
= -1;
1564 new->lbuf
= buffer_new(0);
1565 new->obuf
= buffer_new(0); /* Use default buffer size. */
1566 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1567 new->max
= VTY_BUFSIZ
;
1574 /* allocate and initialise vty */
1575 static struct vty
*vty_new_init(int vty_sock
)
1581 vty
->wfd
= vty_sock
;
1582 vty
->type
= VTY_TERM
;
1583 vty
->node
= AUTH_NODE
;
1588 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1591 vty
->xpath_index
= 0;
1592 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1593 vty
->private_config
= false;
1594 vty
->candidate_config
= vty_shared_candidate_config
;
1595 vty
->status
= VTY_NORMAL
;
1598 vty
->iac_sb_in_progress
= 0;
1601 vtys_add_tail(vty_sessions
, vty
);
1606 /* Create new vty structure. */
1607 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1609 char buf
[SU_ADDRSTRLEN
];
1612 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1614 /* Allocate new vty structure and set up default values. */
1615 vty
= vty_new_init(vty_sock
);
1617 /* configurable parameters not part of basic init */
1618 vty
->v_timeout
= vty_timeout_val
;
1619 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1620 if (no_password_check
) {
1622 vty
->node
= ENABLE_NODE
;
1624 vty
->node
= VIEW_NODE
;
1626 if (host
.lines
>= 0)
1627 vty
->lines
= host
.lines
;
1629 if (!no_password_check
) {
1630 /* Vty is not available if password isn't set. */
1631 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1632 vty_out(vty
, "Vty password is not set.\n");
1633 vty
->status
= VTY_CLOSE
;
1639 /* Say hello to the world. */
1641 if (!no_password_check
)
1642 vty_out(vty
, "\nUser Access Verification\n\n");
1644 /* Setting up terminal. */
1646 vty_will_suppress_go_ahead(vty
);
1648 vty_dont_linemode(vty
);
1649 vty_do_window_size(vty
);
1650 /* vty_dont_lflow_ahead (vty); */
1654 /* Add read/write thread. */
1655 vty_event(VTY_WRITE
, vty
);
1656 vty_event(VTY_READ
, vty
);
1661 /* create vty for stdio */
1662 static struct termios stdio_orig_termios
;
1663 static struct vty
*stdio_vty
= NULL
;
1664 static bool stdio_termios
= false;
1665 static void (*stdio_vty_atclose
)(int isexit
);
1667 static void vty_stdio_reset(int isexit
)
1671 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1672 stdio_termios
= false;
1676 if (stdio_vty_atclose
)
1677 stdio_vty_atclose(isexit
);
1678 stdio_vty_atclose
= NULL
;
1682 static void vty_stdio_atexit(void)
1687 void vty_stdio_suspend(void)
1692 THREAD_OFF(stdio_vty
->t_write
);
1693 THREAD_OFF(stdio_vty
->t_read
);
1694 THREAD_OFF(stdio_vty
->t_timeout
);
1697 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1698 stdio_termios
= false;
1701 void vty_stdio_resume(void)
1706 if (!tcgetattr(0, &stdio_orig_termios
)) {
1707 struct termios termios
;
1709 termios
= stdio_orig_termios
;
1710 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1711 | IGNCR
| ICRNL
| IXON
);
1712 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1713 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1714 termios
.c_cflag
|= CS8
;
1715 tcsetattr(0, TCSANOW
, &termios
);
1716 stdio_termios
= true;
1719 vty_prompt(stdio_vty
);
1721 /* Add read/write thread. */
1722 vty_event(VTY_WRITE
, stdio_vty
);
1723 vty_event(VTY_READ
, stdio_vty
);
1726 void vty_stdio_close(void)
1730 vty_close(stdio_vty
);
1733 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1737 /* refuse creating two vtys on stdio */
1741 vty
= stdio_vty
= vty_new_init(0);
1742 stdio_vty_atclose
= atclose
;
1745 /* always have stdio vty in a known _unchangeable_ state, don't want
1747 * to have any effect here to make sure scripting this works as intended
1749 vty
->node
= ENABLE_NODE
;
1751 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1757 /* Accept connection from the network. */
1758 static void vty_accept(struct thread
*thread
)
1760 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1765 int accept_sock
= vtyserv
->sock
;
1767 struct access_list
*acl
= NULL
;
1769 /* We continue hearing vty socket. */
1770 vty_event_serv(VTY_SERV
, vtyserv
);
1772 memset(&su
, 0, sizeof(union sockunion
));
1774 /* We can handle IPv4 or IPv6 socket. */
1775 vty_sock
= sockunion_accept(accept_sock
, &su
);
1777 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1778 safe_strerror(errno
));
1781 set_nonblocking(vty_sock
);
1782 set_cloexec(vty_sock
);
1784 if (!sockunion2hostprefix(&su
, &p
)) {
1786 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1791 /* VTY's accesslist apply. */
1792 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1793 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1794 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1795 zlog_info("Vty connection refused from %pSU", &su
);
1801 /* VTY's ipv6 accesslist apply. */
1802 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1803 if ((acl
= access_list_lookup(AFI_IP6
,
1804 vty_ipv6_accesslist_name
))
1805 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1806 zlog_info("Vty connection refused from %pSU", &su
);
1813 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1816 zlog_info("can't set sockopt to vty_sock : %s",
1817 safe_strerror(errno
));
1819 zlog_info("Vty connection from %pSU", &su
);
1821 vty_create(vty_sock
, &su
);
1824 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1827 struct addrinfo req
;
1828 struct addrinfo
*ainfo
;
1829 struct addrinfo
*ainfo_save
;
1831 char port_str
[BUFSIZ
];
1833 memset(&req
, 0, sizeof(req
));
1834 req
.ai_flags
= AI_PASSIVE
;
1835 req
.ai_family
= AF_UNSPEC
;
1836 req
.ai_socktype
= SOCK_STREAM
;
1837 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1838 port_str
[sizeof(port_str
) - 1] = '\0';
1840 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1843 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1851 struct vty_serv
*vtyserv
;
1853 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1856 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1857 ainfo
->ai_protocol
);
1861 sockopt_v6only(ainfo
->ai_family
, sock
);
1862 sockopt_reuseaddr(sock
);
1863 sockopt_reuseport(sock
);
1866 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1868 close(sock
); /* Avoid sd leak. */
1872 ret
= listen(sock
, 3);
1874 close(sock
); /* Avoid sd leak. */
1878 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1879 vtyserv
->sock
= sock
;
1880 vtyservs_add_tail(vty_servs
, vtyserv
);
1882 vty_event_serv(VTY_SERV
, vtyserv
);
1883 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1885 freeaddrinfo(ainfo_save
);
1889 /* For sockaddr_un. */
1892 /* VTY shell UNIX domain socket. */
1893 static void vty_serv_un(const char *path
)
1895 struct vty_serv
*vtyserv
;
1898 struct sockaddr_un serv
;
1900 struct zprivs_ids_t ids
;
1902 /* First of all, unlink existing socket */
1906 old_mask
= umask(0007);
1908 /* Make UNIX domain socket. */
1909 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1911 flog_err_sys(EC_LIB_SOCKET
,
1912 "Cannot create unix stream socket: %s",
1913 safe_strerror(errno
));
1917 /* Make server socket. */
1918 memset(&serv
, 0, sizeof(serv
));
1919 serv
.sun_family
= AF_UNIX
;
1920 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1921 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1922 len
= serv
.sun_len
= SUN_LEN(&serv
);
1924 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1925 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1929 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1931 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1932 safe_strerror(errno
));
1933 close(sock
); /* Avoid sd leak. */
1937 ret
= listen(sock
, 5);
1939 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1940 safe_strerror(errno
));
1941 close(sock
); /* Avoid sd leak. */
1947 zprivs_get_ids(&ids
);
1949 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1950 earlier for the case when we don't need to chown the file
1951 type casting it here to make a compare */
1952 if ((int)ids
.gid_vty
> 0) {
1953 /* set group of socket */
1954 if (chown(path
, -1, ids
.gid_vty
)) {
1955 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1956 "vty_serv_un: could chown socket, %s",
1957 safe_strerror(errno
));
1961 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1962 vtyserv
->sock
= sock
;
1963 vtyserv
->vtysh
= true;
1964 vtyservs_add_tail(vty_servs
, vtyserv
);
1966 vty_event_serv(VTYSH_SERV
, vtyserv
);
1969 /* #define VTYSH_DEBUG 1 */
1971 static void vtysh_accept(struct thread
*thread
)
1973 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1974 int accept_sock
= vtyserv
->sock
;
1977 struct sockaddr_un client
;
1980 vty_event_serv(VTYSH_SERV
, vtyserv
);
1982 memset(&client
, 0, sizeof(client
));
1983 client_len
= sizeof(struct sockaddr_un
);
1985 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
1986 (socklen_t
*)&client_len
);
1989 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1990 safe_strerror(errno
));
1994 if (set_nonblocking(sock
) < 0) {
1997 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
1998 sock
, safe_strerror(errno
));
2005 printf("VTY shell accept\n");
2006 #endif /* VTYSH_DEBUG */
2011 vty
->type
= VTY_SHELL_SERV
;
2012 vty
->node
= VIEW_NODE
;
2013 vtys_add_tail(vtysh_sessions
, vty
);
2015 vty_event(VTYSH_READ
, vty
);
2018 static int vtysh_do_pass_fd(struct vty
*vty
)
2020 struct iovec iov
[1] = {
2022 .iov_base
= vty
->pass_fd_status
,
2023 .iov_len
= sizeof(vty
->pass_fd_status
),
2027 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2028 struct cmsghdr align
;
2030 struct msghdr mh
= {
2032 .msg_iovlen
= array_size(iov
),
2033 .msg_control
= u
.buf
,
2034 .msg_controllen
= sizeof(u
.buf
),
2036 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2039 memset(&u
.buf
, 0, sizeof(u
.buf
));
2040 cmh
->cmsg_level
= SOL_SOCKET
;
2041 cmh
->cmsg_type
= SCM_RIGHTS
;
2042 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2043 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2045 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2046 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2047 return BUFFER_PENDING
;
2049 close(vty
->pass_fd
);
2051 vty
->status
= VTY_NORMAL
;
2054 return BUFFER_ERROR
;
2056 /* resume accepting commands (suspended in vtysh_read) */
2057 vty_event(VTYSH_READ
, vty
);
2059 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2060 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2062 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2063 return BUFFER_PENDING
;
2065 return BUFFER_EMPTY
;
2068 static int vtysh_flush(struct vty
*vty
)
2072 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2073 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2074 ret
= vtysh_do_pass_fd(vty
);
2077 case BUFFER_PENDING
:
2078 vty_event(VTYSH_WRITE
, vty
);
2081 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2083 buffer_reset(vty
->lbuf
);
2084 buffer_reset(vty
->obuf
);
2093 void vty_pass_fd(struct vty
*vty
, int fd
)
2095 if (vty
->pass_fd
!= -1)
2096 close(vty
->pass_fd
);
2101 static void vtysh_read(struct thread
*thread
)
2107 unsigned char buf
[VTY_READ_BUFSIZ
];
2109 uint8_t header
[4] = {0, 0, 0, 0};
2111 sock
= THREAD_FD(thread
);
2112 vty
= THREAD_ARG(thread
);
2114 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2116 if (ERRNO_IO_RETRY(errno
)) {
2117 vty_event(VTYSH_READ
, vty
);
2122 "%s: read failed on vtysh client fd %d, closing: %s",
2123 __func__
, sock
, safe_strerror(errno
));
2125 buffer_reset(vty
->lbuf
);
2126 buffer_reset(vty
->obuf
);
2129 printf("close vtysh\n");
2130 #endif /* VTYSH_DEBUG */
2135 printf("line: %.*s\n", nbytes
, buf
);
2136 #endif /* VTYSH_DEBUG */
2138 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2139 /* Clear command line buffer. */
2140 vty
->cp
= vty
->length
= 0;
2142 vty_out(vty
, "%% Command is too long.\n");
2144 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2145 vty
->buf
[vty
->length
++] = *p
;
2147 /* Pass this line to parser. */
2148 ret
= vty_execute(vty
);
2149 /* Note that vty_execute clears the command buffer and resets
2150 vty->length to 0. */
2152 /* Return result. */
2154 printf("result: %d\n", ret
);
2155 printf("vtysh node: %d\n", vty
->node
);
2156 #endif /* VTYSH_DEBUG */
2158 if (vty
->pass_fd
!= -1) {
2159 memset(vty
->pass_fd_status
, 0, 4);
2160 vty
->pass_fd_status
[3] = ret
;
2161 vty
->status
= VTY_PASSFD
;
2164 vty_event(VTYSH_WRITE
, vty
);
2166 /* this introduces a "sequence point"
2167 * command output is written normally,
2168 * read processing is suspended until
2170 * then retcode + FD is written
2171 * then normal processing resumes
2173 * => skip vty_event(VTYSH_READ, vty)!
2178 /* hack for asynchronous "write integrated"
2179 * - other commands in "buf" will be ditched
2180 * - input during pending config-write is
2182 if (ret
== CMD_SUSPEND
)
2185 /* warning: watchfrr hardcodes this result write
2188 buffer_put(vty
->obuf
, header
, 4);
2190 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2191 /* Try to flush results; exit if a write
2198 if (vty
->status
== VTY_CLOSE
)
2201 vty_event(VTYSH_READ
, vty
);
2204 static void vtysh_write(struct thread
*thread
)
2206 struct vty
*vty
= THREAD_ARG(thread
);
2213 /* Determine address family to bind. */
2214 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2216 /* If port is set to 0, do not listen on TCP/IP at all! */
2218 vty_serv_sock_addrinfo(addr
, port
);
2225 static void vty_error_delete(void *arg
)
2227 struct vty_error
*ve
= arg
;
2229 XFREE(MTYPE_TMP
, ve
);
2232 /* Close vty interface. Warning: call this only from functions that
2233 will be careful not to access the vty afterwards (since it has
2234 now been freed). This is safest from top-level functions (called
2235 directly by the thread dispatcher). */
2236 void vty_close(struct vty
*vty
)
2239 bool was_stdio
= false;
2241 /* Drop out of configure / transaction if needed. */
2242 vty_config_exit(vty
);
2244 /* Cancel threads.*/
2245 THREAD_OFF(vty
->t_read
);
2246 THREAD_OFF(vty
->t_write
);
2247 THREAD_OFF(vty
->t_timeout
);
2249 if (vty
->pass_fd
!= -1) {
2250 close(vty
->pass_fd
);
2253 zlog_live_close(&vty
->live_log
);
2256 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2258 /* Free input buffer. */
2259 buffer_free(vty
->obuf
);
2260 buffer_free(vty
->lbuf
);
2262 /* Free command history. */
2263 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2264 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2268 if (vty
->fd
!= -1) {
2269 if (vty
->type
== VTY_SHELL_SERV
)
2270 vtys_del(vtysh_sessions
, vty
);
2272 vtys_del(vty_sessions
, vty
);
2275 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2279 * note check is for fd > STDERR_FILENO, not fd != -1.
2280 * We never close stdin/stdout/stderr here, because we may be
2281 * running in foreground mode with logging to stdout. Also,
2282 * additionally, we'd need to replace these fds with /dev/null. */
2283 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2285 if (vty
->fd
> STDERR_FILENO
)
2287 if (vty
->fd
== STDIN_FILENO
)
2290 XFREE(MTYPE_VTY
, vty
->buf
);
2293 vty
->error
->del
= vty_error_delete
;
2294 list_delete(&vty
->error
);
2298 XFREE(MTYPE_VTY
, vty
);
2304 /* When time out occur output message then close connection. */
2305 static void vty_timeout(struct thread
*thread
)
2309 vty
= THREAD_ARG(thread
);
2313 buffer_reset(vty
->lbuf
);
2314 buffer_reset(vty
->obuf
);
2315 vty_out(vty
, "\nVty connection is timed out.\n");
2317 /* Close connection. */
2318 vty
->status
= VTY_CLOSE
;
2322 /* Read up configuration file from file_name. */
2323 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2327 struct vty_error
*ve
;
2328 struct listnode
*node
;
2329 unsigned int line_num
= 0;
2332 /* vty_close won't close stderr; if some config command prints
2333 * something it'll end up there. (not ideal; it'd be better if output
2334 * from a file-load went to logging instead. Also note that if this
2335 * function is called after daemonizing, stderr will be /dev/null.)
2337 * vty->fd will be -1 from vty_new()
2339 vty
->wfd
= STDERR_FILENO
;
2340 vty
->type
= VTY_FILE
;
2341 vty
->node
= CONFIG_NODE
;
2344 vty
->candidate_config
= config
;
2346 vty
->private_config
= true;
2347 vty
->candidate_config
= nb_config_new(NULL
);
2350 /* Execute configuration file */
2351 ret
= config_from_file(vty
, confp
, &line_num
);
2353 /* Flush any previous errors before printing messages below */
2354 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2356 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2357 const char *message
= NULL
;
2361 case CMD_ERR_AMBIGUOUS
:
2362 message
= "Ambiguous command";
2364 case CMD_ERR_NO_MATCH
:
2365 message
= "No such command";
2368 message
= "Command returned Warning";
2370 case CMD_WARNING_CONFIG_FAILED
:
2371 message
= "Command returned Warning Config Failed";
2373 case CMD_ERR_INCOMPLETE
:
2374 message
= "Command returned Incomplete";
2376 case CMD_ERR_EXEED_ARGC_MAX
:
2378 "Command exceeded maximum number of Arguments";
2381 message
= "Command returned unhandled error message";
2385 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2386 nl
= strchr(ve
->error_buf
, '\n');
2389 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2390 message
, ve
->line_num
, ve
->error_buf
);
2395 * Automatically commit the candidate configuration after
2396 * reading the configuration file.
2398 if (config
== NULL
) {
2399 struct nb_context context
= {};
2400 char errmsg
[BUFSIZ
] = {0};
2402 context
.client
= NB_CLIENT_CLI
;
2404 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2405 "Read configuration file", NULL
,
2406 errmsg
, sizeof(errmsg
));
2407 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2409 "%s: failed to read configuration file: %s (%s)",
2410 __func__
, nb_err_name(ret
), errmsg
);
2416 static FILE *vty_use_backup_config(const char *fullpath
)
2418 char *fullpath_sav
, *fullpath_tmp
;
2424 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2425 fullpath_sav
= malloc(fullpath_sav_sz
);
2426 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2427 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2429 sav
= open(fullpath_sav
, O_RDONLY
);
2435 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2436 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2438 /* Open file to configuration write. */
2439 tmp
= mkstemp(fullpath_tmp
);
2443 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2446 while ((c
= read(sav
, buffer
, 512)) > 0) {
2447 if (write(tmp
, buffer
, c
) <= 0)
2453 if (rename(fullpath_tmp
, fullpath
) == 0)
2454 ret
= fopen(fullpath
, "r");
2456 unlink(fullpath_tmp
);
2461 unlink(fullpath_tmp
);
2471 /* Read up configuration file from file_name. */
2472 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2473 char *config_default_dir
)
2475 char cwd
[MAXPATHLEN
];
2477 const char *fullpath
;
2479 bool read_success
= false;
2481 /* If -f flag specified. */
2482 if (config_file
!= NULL
) {
2483 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2484 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2487 "%s: failure to determine Current Working Directory %d!",
2489 goto tmp_free_and_out
;
2491 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2492 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2493 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2496 fullpath
= config_file
;
2498 confp
= fopen(fullpath
, "r");
2500 if (confp
== NULL
) {
2502 EC_LIB_BACKUP_CONFIG
,
2503 "%s: failed to open configuration file %s: %s, checking backup",
2504 __func__
, fullpath
, safe_strerror(errno
));
2506 confp
= vty_use_backup_config(fullpath
);
2508 flog_warn(EC_LIB_BACKUP_CONFIG
,
2509 "using backup configuration file!");
2513 "%s: can't open configuration file [%s]",
2514 __func__
, config_file
);
2515 goto tmp_free_and_out
;
2520 host_config_set(config_default_dir
);
2524 struct stat conf_stat
;
2526 /* !!!!PLEASE LEAVE!!!!
2527 * This is NEEDED for use with vtysh -b, or else you can get
2528 * a real configuration food fight with a lot garbage in the
2529 * merged configuration file it creates coming from the per
2530 * daemon configuration files. This also allows the daemons
2531 * to start if there default configuration file is not
2532 * present or ignore them, as needed when using vtysh -b to
2533 * configure the daemons at boot - MAG
2536 /* Stat for vtysh Zebra.conf, if found startup and wait for
2537 * boot configuration
2540 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2541 ret
= stat(integrate_default
, &conf_stat
);
2543 read_success
= true;
2544 goto tmp_free_and_out
;
2548 confp
= fopen(config_default_dir
, "r");
2549 if (confp
== NULL
) {
2552 "%s: failed to open configuration file %s: %s, checking backup",
2553 __func__
, config_default_dir
,
2554 safe_strerror(errno
));
2556 confp
= vty_use_backup_config(config_default_dir
);
2558 flog_warn(EC_LIB_BACKUP_CONFIG
,
2559 "using backup configuration file!");
2560 fullpath
= config_default_dir
;
2562 flog_err(EC_LIB_VTY
,
2563 "can't open configuration file [%s]",
2564 config_default_dir
);
2565 goto tmp_free_and_out
;
2568 fullpath
= config_default_dir
;
2571 vty_read_file(config
, confp
);
2572 read_success
= true;
2576 host_config_set(fullpath
);
2579 XFREE(MTYPE_TMP
, tmp
);
2581 return read_success
;
2584 static void update_xpath(struct vty
*vty
, const char *oldpath
,
2585 const char *newpath
)
2589 for (i
= 0; i
< vty
->xpath_index
; i
++) {
2590 if (!frrstr_startswith(vty
->xpath
[i
], oldpath
))
2593 char *tmp
= frrstr_replace(vty
->xpath
[i
], oldpath
, newpath
);
2594 strlcpy(vty
->xpath
[i
], tmp
, sizeof(vty
->xpath
[0]));
2595 XFREE(MTYPE_TMP
, tmp
);
2599 void vty_update_xpath(const char *oldpath
, const char *newpath
)
2603 frr_each (vtys
, vtysh_sessions
, vty
)
2604 update_xpath(vty
, oldpath
, newpath
);
2605 frr_each (vtys
, vty_sessions
, vty
)
2606 update_xpath(vty
, oldpath
, newpath
);
2609 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2611 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2612 vty_out(vty
, "%% Configuration is locked by other client\n");
2616 vty
->node
= CONFIG_NODE
;
2618 vty
->private_config
= private_config
;
2619 vty
->xpath_index
= 0;
2621 if (private_config
) {
2622 vty
->candidate_config
= nb_config_dup(running_config
);
2623 vty
->candidate_config_base
= nb_config_dup(running_config
);
2625 "Warning: uncommitted changes will be discarded on exit.\n\n");
2627 vty
->candidate_config
= vty_shared_candidate_config
;
2628 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2629 vty
->candidate_config_base
=
2630 nb_config_dup(running_config
);
2636 void vty_config_exit(struct vty
*vty
)
2638 enum node_type node
= vty
->node
;
2639 struct cmd_node
*cnode
;
2641 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2642 * somewhere below CONFIG_NODE */
2643 while (node
&& node
!= CONFIG_NODE
) {
2644 cnode
= vector_lookup(cmdvec
, node
);
2645 node
= cnode
->parent_node
;
2647 if (node
!= CONFIG_NODE
)
2648 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2651 while (vty
->node
!= ENABLE_NODE
)
2652 /* will call vty_config_node_exit() below */
2656 int vty_config_node_exit(struct vty
*vty
)
2658 vty
->xpath_index
= 0;
2660 /* Perform any pending commits. */
2661 (void)nb_cli_pending_commit_check(vty
);
2663 /* Check if there's a pending confirmed commit. */
2664 if (vty
->t_confirmed_commit_timeout
) {
2666 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2667 nb_cli_confirmed_commit_rollback(vty
);
2668 nb_cli_confirmed_commit_clean(vty
);
2671 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2673 if (vty
->candidate_config
) {
2674 if (vty
->private_config
)
2675 nb_config_free(vty
->candidate_config
);
2676 vty
->candidate_config
= NULL
;
2678 if (vty
->candidate_config_base
) {
2679 nb_config_free(vty
->candidate_config_base
);
2680 vty
->candidate_config_base
= NULL
;
2683 vty
->config
= false;
2687 /* Master of the threads. */
2688 static struct thread_master
*vty_master
;
2690 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2694 thread_add_read(vty_master
, vty_accept
, vty_serv
,
2695 vty_serv
->sock
, &vty_serv
->t_accept
);
2699 thread_add_read(vty_master
, vtysh_accept
, vty_serv
,
2700 vty_serv
->sock
, &vty_serv
->t_accept
);
2704 assert(!"vty_event_serv() called incorrectly");
2708 static void vty_event(enum vty_event event
, struct vty
*vty
)
2713 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2717 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2722 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2725 /* Time out treatment. */
2726 if (vty
->v_timeout
) {
2727 THREAD_OFF(vty
->t_timeout
);
2728 thread_add_timer(vty_master
, vty_timeout
, vty
,
2729 vty
->v_timeout
, &vty
->t_timeout
);
2733 thread_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2736 case VTY_TIMEOUT_RESET
:
2737 THREAD_OFF(vty
->t_timeout
);
2739 thread_add_timer(vty_master
, vty_timeout
, vty
,
2740 vty
->v_timeout
, &vty
->t_timeout
);
2743 assert(!"vty_event() called incorrectly");
2747 DEFUN_NOSH (config_who
,
2750 "Display who is on vty\n")
2754 frr_each (vtys
, vty_sessions
, v
)
2755 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2756 v
->config
? "*" : " ", v
->fd
, v
->address
,
2757 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2761 /* Move to vty configuration mode. */
2762 DEFUN_NOSH (line_vty
,
2765 "Configure a terminal line\n"
2766 "Virtual terminal\n")
2768 vty
->node
= VTY_NODE
;
2772 /* Set time out value. */
2773 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2774 const char *sec_str
)
2776 unsigned long timeout
= 0;
2778 /* min_str and sec_str are already checked by parser. So it must be
2779 all digit string. */
2781 timeout
= strtol(min_str
, NULL
, 10);
2785 timeout
+= strtol(sec_str
, NULL
, 10);
2787 vty_timeout_val
= timeout
;
2788 vty
->v_timeout
= timeout
;
2789 vty_event(VTY_TIMEOUT_RESET
, vty
);
2795 DEFUN (exec_timeout_min
,
2796 exec_timeout_min_cmd
,
2797 "exec-timeout (0-35791)",
2798 "Set timeout value\n"
2799 "Timeout value in minutes\n")
2802 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2805 DEFUN (exec_timeout_sec
,
2806 exec_timeout_sec_cmd
,
2807 "exec-timeout (0-35791) (0-2147483)",
2808 "Set the EXEC timeout\n"
2809 "Timeout in minutes\n"
2810 "Timeout in seconds\n")
2813 int idx_number_2
= 2;
2814 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2815 argv
[idx_number_2
]->arg
);
2818 DEFUN (no_exec_timeout
,
2819 no_exec_timeout_cmd
,
2822 "Set the EXEC timeout\n")
2824 return exec_timeout(vty
, NULL
, NULL
);
2827 /* Set vty access class. */
2828 DEFUN (vty_access_class
,
2829 vty_access_class_cmd
,
2830 "access-class WORD",
2831 "Filter connections based on an IP access list\n"
2835 if (vty_accesslist_name
)
2836 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2838 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2843 /* Clear vty access class. */
2844 DEFUN (no_vty_access_class
,
2845 no_vty_access_class_cmd
,
2846 "no access-class [WORD]",
2848 "Filter connections based on an IP access list\n"
2852 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2853 if (!vty_accesslist_name
2854 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2855 vty_out(vty
, "Access-class is not currently applied to vty\n");
2856 return CMD_WARNING_CONFIG_FAILED
;
2859 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2861 vty_accesslist_name
= NULL
;
2866 /* Set vty access class. */
2867 DEFUN (vty_ipv6_access_class
,
2868 vty_ipv6_access_class_cmd
,
2869 "ipv6 access-class WORD",
2871 "Filter connections based on an IP access list\n"
2872 "IPv6 access list\n")
2875 if (vty_ipv6_accesslist_name
)
2876 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2878 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2883 /* Clear vty access class. */
2884 DEFUN (no_vty_ipv6_access_class
,
2885 no_vty_ipv6_access_class_cmd
,
2886 "no ipv6 access-class [WORD]",
2889 "Filter connections based on an IP access list\n"
2890 "IPv6 access list\n")
2893 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2895 if (!vty_ipv6_accesslist_name
2896 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2898 "IPv6 access-class is not currently applied to vty\n");
2899 return CMD_WARNING_CONFIG_FAILED
;
2902 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2904 vty_ipv6_accesslist_name
= NULL
;
2913 "Enable password checking\n")
2915 no_password_check
= 0;
2919 DEFUN (no_vty_login
,
2923 "Enable password checking\n")
2925 no_password_check
= 1;
2929 DEFUN (service_advanced_vty
,
2930 service_advanced_vty_cmd
,
2931 "service advanced-vty",
2932 "Set up miscellaneous service\n"
2933 "Enable advanced mode vty interface\n")
2939 DEFUN (no_service_advanced_vty
,
2940 no_service_advanced_vty_cmd
,
2941 "no service advanced-vty",
2943 "Set up miscellaneous service\n"
2944 "Enable advanced mode vty interface\n")
2950 DEFUN_NOSH(terminal_monitor
,
2951 terminal_monitor_cmd
,
2952 "terminal monitor [detach]",
2953 "Set terminal line parameters\n"
2954 "Copy debug output to the current terminal line\n"
2955 "Keep logging feed open independent of VTY session\n")
2959 if (vty
->type
!= VTY_SHELL_SERV
) {
2960 vty_out(vty
, "%% not supported\n");
2965 struct zlog_live_cfg detach_log
= {};
2967 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
2968 zlog_live_disown(&detach_log
);
2970 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
2973 vty_out(vty
, "%% error opening live log: %m\n");
2977 vty_pass_fd(vty
, fd_ret
);
2981 DEFUN_NOSH(no_terminal_monitor
,
2982 no_terminal_monitor_cmd
,
2983 "no terminal monitor",
2985 "Set terminal line parameters\n"
2986 "Copy debug output to the current terminal line\n")
2988 zlog_live_close(&vty
->live_log
);
2992 DEFUN_NOSH(terminal_no_monitor
,
2993 terminal_no_monitor_cmd
,
2994 "terminal no monitor",
2995 "Set terminal line parameters\n"
2997 "Copy debug output to the current terminal line\n")
2999 return no_terminal_monitor(self
, vty
, argc
, argv
);
3003 DEFUN_NOSH (show_history
,
3007 "Display the session command history\n")
3011 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3012 if (index
== VTY_MAXHIST
) {
3017 if (vty
->hist
[index
] != NULL
)
3018 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3027 DEFPY (log_commands
,
3029 "[no] log commands",
3032 "Log all commands\n")
3035 if (do_log_commands_perm
) {
3037 "Daemon started with permanent logging turned on for commands, ignoring\n");
3041 do_log_commands
= false;
3043 do_log_commands
= true;
3048 /* Display current configuration. */
3049 static int vty_config_write(struct vty
*vty
)
3051 vty_frame(vty
, "line vty\n");
3053 if (vty_accesslist_name
)
3054 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3056 if (vty_ipv6_accesslist_name
)
3057 vty_out(vty
, " ipv6 access-class %s\n",
3058 vty_ipv6_accesslist_name
);
3061 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3062 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3063 vty_timeout_val
% 60);
3066 if (no_password_check
)
3067 vty_out(vty
, " no login\n");
3069 vty_endframe(vty
, "exit\n");
3071 if (do_log_commands
)
3072 vty_out(vty
, "log commands\n");
3074 vty_out(vty
, "!\n");
3079 static int vty_config_write(struct vty
*vty
);
3080 struct cmd_node vty_node
= {
3083 .parent_node
= CONFIG_NODE
,
3084 .prompt
= "%s(config-line)# ",
3085 .config_write
= vty_config_write
,
3088 /* Reset all VTY status. */
3089 void vty_reset(void)
3093 frr_each_safe (vtys
, vty_sessions
, vty
) {
3094 buffer_reset(vty
->lbuf
);
3095 buffer_reset(vty
->obuf
);
3096 vty
->status
= VTY_CLOSE
;
3100 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3102 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3103 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3106 static void vty_save_cwd(void)
3110 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3114 * At this point if these go wrong, more than likely
3115 * the whole world is coming down around us
3116 * Hence not worrying about it too much.
3118 if (chdir(SYSCONFDIR
)) {
3119 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3120 "Failure to chdir to %s, errno: %d",
3124 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3125 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3126 "Failure to getcwd, errno: %d", errno
);
3132 char *vty_get_cwd(void)
3137 int vty_shell(struct vty
*vty
)
3139 return vty
->type
== VTY_SHELL
? 1 : 0;
3142 int vty_shell_serv(struct vty
*vty
)
3144 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3147 void vty_init_vtysh(void)
3149 /* currently nothing to do, but likely to have future use */
3152 /* Install vty's own commands like `who' command. */
3153 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3155 /* For further configuration read, preserve current directory. */
3158 vty_master
= master_thread
;
3160 atexit(vty_stdio_atexit
);
3162 /* Install bgp top node. */
3163 install_node(&vty_node
);
3165 install_element(VIEW_NODE
, &config_who_cmd
);
3166 install_element(VIEW_NODE
, &show_history_cmd
);
3167 install_element(CONFIG_NODE
, &line_vty_cmd
);
3168 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3169 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3170 install_element(CONFIG_NODE
, &show_history_cmd
);
3171 install_element(CONFIG_NODE
, &log_commands_cmd
);
3173 if (do_command_logging
) {
3174 do_log_commands
= true;
3175 do_log_commands_perm
= true;
3178 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3179 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3180 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3182 install_default(VTY_NODE
);
3183 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3184 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3185 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3186 install_element(VTY_NODE
, &vty_access_class_cmd
);
3187 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3188 install_element(VTY_NODE
, &vty_login_cmd
);
3189 install_element(VTY_NODE
, &no_vty_login_cmd
);
3190 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3191 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3194 void vty_terminate(void)
3197 struct vty_serv
*vtyserv
;
3199 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3203 /* default state of vty_sessions is initialized & empty. */
3204 vtys_fini(vty_sessions
);
3205 vtys_init(vty_sessions
);
3207 /* vty_reset() doesn't close vtysh sessions */
3208 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3209 buffer_reset(vty
->lbuf
);
3210 buffer_reset(vty
->obuf
);
3211 vty
->status
= VTY_CLOSE
;
3215 vtys_fini(vtysh_sessions
);
3216 vtys_init(vtysh_sessions
);
3218 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
3219 THREAD_OFF(vtyserv
->t_accept
);
3220 close(vtyserv
->sock
);
3221 XFREE(MTYPE_VTY_SERV
, vtyserv
);
3224 vtyservs_fini(vty_servs
);
3225 vtyservs_init(vty_servs
);