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_LIBPCREPOSIX
28 #include <pcreposix.h>
31 #endif /* HAVE_LIBPCREPOSIX */
38 #include "sockunion.h"
48 #include "lib_errors.h"
49 #include "northbound_cli.h"
53 #include <arpa/telnet.h>
56 #ifndef VTYSH_EXTRACT_PL
57 #include "lib/vty_clippy.c"
60 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY");
61 DEFINE_MTYPE_STATIC(LIB
, VTY_SERV
, "VTY server");
62 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer");
63 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history");
65 DECLARE_DLIST(vtys
, struct vty
, itm
);
80 PREDECL_DLIST(vtyservs
);
83 struct vtyservs_item itm
;
88 struct thread
*t_accept
;
91 DECLARE_DLIST(vtyservs
, struct vty_serv
, itm
);
93 static void vty_event_serv(enum vty_event event
, struct vty_serv
*);
94 static void vty_event(enum vty_event
, struct vty
*);
96 /* Extern host structure from command.c */
97 extern struct host host
;
99 /* active listeners */
100 static struct vtyservs_head vty_servs
[1] = {INIT_DLIST(vty_servs
[0])};
102 /* active connections */
103 static struct vtys_head vty_sessions
[1] = {INIT_DLIST(vty_sessions
[0])};
104 static struct vtys_head vtysh_sessions
[1] = {INIT_DLIST(vtysh_sessions
[0])};
106 /* Vty timeout value. */
107 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
109 /* Vty access-class command */
110 static char *vty_accesslist_name
= NULL
;
112 /* Vty access-calss for IPv6. */
113 static char *vty_ipv6_accesslist_name
= NULL
;
115 /* Current directory. */
116 static char vty_cwd
[MAXPATHLEN
];
118 /* Login password check. */
119 static int no_password_check
= 0;
121 /* Integrated configuration file path */
122 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
124 static bool do_log_commands
;
125 static bool do_log_commands_perm
;
127 void vty_frame(struct vty
*vty
, const char *format
, ...)
131 va_start(args
, format
);
132 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
133 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
134 vty
->frame_pos
= strlen(vty
->frame
);
138 void vty_endframe(struct vty
*vty
, const char *endtext
)
140 if (vty
->frame_pos
== 0 && endtext
)
141 vty_out(vty
, "%s", endtext
);
145 bool vty_set_include(struct vty
*vty
, const char *regexp
)
153 regfree(&vty
->include
);
159 errcode
= regcomp(&vty
->include
, regexp
,
160 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
163 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
164 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
172 /* VTY standard output function. */
173 int vty_out(struct vty
*vty
, const char *format
, ...)
180 /* format string may contain %m, keep errno intact for printfrr */
181 int saved_errno
= errno
;
183 if (vty
->frame_pos
) {
185 vty_out(vty
, "%s", vty
->frame
);
188 va_start(args
, format
);
190 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
197 vector lines
= frrstr_split_vec(p
, "\n");
199 /* Place first value in the cache */
200 char *firstline
= vector_slot(lines
, 0);
201 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
203 /* If our split returned more than one entry, time to filter */
204 if (vector_active(lines
) > 1) {
206 * returned string is MTYPE_TMP so it matches the MTYPE
207 * of everything else in the vector
209 char *bstr
= buffer_getstr(vty
->lbuf
);
210 buffer_reset(vty
->lbuf
);
211 XFREE(MTYPE_TMP
, lines
->index
[0]);
212 vector_set_index(lines
, 0, bstr
);
213 frrstr_filter_vec(lines
, &vty
->include
);
214 vector_compact(lines
);
216 * Consider the string "foo\n". If the regex is an empty string
217 * and the line ended with a newline, then the vector will look
223 * If the regex isn't empty, the vector will look like:
227 * In this case we'd like to preserve the newline, so we add
228 * the empty string [1] as in the first example.
230 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
231 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
232 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
234 filtered
= frrstr_join_vec(lines
, "\n");
240 frrstr_strvec_free(lines
);
251 /* print with crlf replacement */
252 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
257 fprintf(vty
->of
, "%s", filtered
);
259 } else if (vty
->of_saved
) {
260 fprintf(vty
->of_saved
, "%s", filtered
);
261 fflush(vty
->of_saved
);
267 /* print without crlf replacement */
268 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
274 if (vty
->filter
&& filtered
)
275 XFREE(MTYPE_TMP
, filtered
);
277 /* If p is not different with buf, it is allocated buffer. */
279 XFREE(MTYPE_VTY_OUT_BUF
, p
);
284 int vty_json(struct vty
*vty
, struct json_object
*json
)
291 text
= json_object_to_json_string_ext(
292 json
, JSON_C_TO_STRING_PRETTY
| JSON_C_TO_STRING_NOSLASHESCAPE
);
293 vty_out(vty
, "%s\n", text
);
294 json_object_free(json
);
299 /* Output current time to the vty. */
300 void vty_time_print(struct vty
*vty
, int cr
)
302 char buf
[FRR_TIMESTAMP_LEN
];
304 if (frr_timestamp(0, buf
, sizeof(buf
)) == 0) {
305 zlog_info("frr_timestamp error");
309 vty_out(vty
, "%s\n", buf
);
311 vty_out(vty
, "%s ", buf
);
316 /* Say hello to vty interface. */
317 void vty_hello(struct vty
*vty
)
323 f
= fopen(host
.motdfile
, "r");
325 while (fgets(buf
, sizeof(buf
), f
)) {
327 /* work backwards to ignore trailling isspace()
329 for (s
= buf
+ strlen(buf
);
330 (s
> buf
) && isspace((unsigned char)s
[-1]);
334 vty_out(vty
, "%s\n", buf
);
338 vty_out(vty
, "MOTD file not found\n");
339 } else if (host
.motd
)
340 vty_out(vty
, "%s", host
.motd
);
343 /* Put out prompt and wait input from user. */
344 static void vty_prompt(struct vty
*vty
)
346 if (vty
->type
== VTY_TERM
) {
347 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
351 /* Send WILL TELOPT_ECHO to remote server. */
352 static void vty_will_echo(struct vty
*vty
)
354 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
355 vty_out(vty
, "%s", cmd
);
358 /* Make suppress Go-Ahead telnet option. */
359 static void vty_will_suppress_go_ahead(struct vty
*vty
)
361 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
362 vty_out(vty
, "%s", cmd
);
365 /* Make don't use linemode over telnet. */
366 static void vty_dont_linemode(struct vty
*vty
)
368 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
369 vty_out(vty
, "%s", cmd
);
372 /* Use window size. */
373 static void vty_do_window_size(struct vty
*vty
)
375 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
376 vty_out(vty
, "%s", cmd
);
379 /* Authentication of vty */
380 static void vty_auth(struct vty
*vty
, char *buf
)
383 enum node_type next_node
= 0;
389 passwd
= host
.password_encrypt
;
391 passwd
= host
.password
;
393 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
395 next_node
= VIEW_NODE
;
397 case AUTH_ENABLE_NODE
:
399 passwd
= host
.enable_encrypt
;
401 passwd
= host
.enable
;
402 next_node
= ENABLE_NODE
;
408 fail
= strcmp(crypt(buf
, passwd
), passwd
);
410 fail
= strcmp(buf
, passwd
);
416 vty
->node
= next_node
; /* Success ! */
419 if (vty
->fail
>= 3) {
420 if (vty
->node
== AUTH_NODE
) {
422 "%% Bad passwords, too many failures!\n");
423 vty
->status
= VTY_CLOSE
;
425 /* AUTH_ENABLE_NODE */
428 "%% Bad enable passwords, too many failures!\n");
429 vty
->status
= VTY_CLOSE
;
435 /* Command execution over the vty interface. */
436 static int vty_command(struct vty
*vty
, char *buf
)
439 const char *protocolname
;
445 * Log non empty command lines
450 /* Skip white spaces. */
451 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
454 if (cp
!= NULL
&& *cp
!= '\0') {
455 char vty_str
[VTY_BUFSIZ
];
456 char prompt_str
[VTY_BUFSIZ
];
458 /* format the base vty info */
459 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
462 /* format the prompt */
463 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
466 /* now log the command */
467 zlog_notice("%s%s", prompt_str
, buf
);
472 unsigned long walltime
, cputime
;
474 /* cmd_execute() may change cputime_enabled if we're executing the
475 * "service cputime-stats" command, which can result in nonsensical
476 * and very confusing warnings
478 bool cputime_enabled_here
= cputime_enabled
;
482 ret
= cmd_execute(vty
, buf
, NULL
, 0);
486 walltime
= thread_consumed_time(&after
, &before
, &cputime
);
488 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
489 && cputime
> cputime_threshold
)
490 /* Warn about CPU hog that must be fixed. */
491 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
492 "CPU HOG: command took %lums (cpu time %lums): %s",
493 walltime
/ 1000, cputime
/ 1000, buf
);
494 else if (walltime_threshold
&& walltime
> walltime_threshold
)
495 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
496 "STARVATION: command took %lums (cpu time %lums): %s",
497 walltime
/ 1000, cputime
/ 1000, buf
);
499 /* Get the name of the protocol if any */
500 protocolname
= frr_protoname
;
502 if (ret
!= CMD_SUCCESS
)
505 if (vty
->type
== VTY_FILE
)
506 vty_out(vty
, "Warning...\n");
508 case CMD_ERR_AMBIGUOUS
:
509 vty_out(vty
, "%% Ambiguous command.\n");
511 case CMD_ERR_NO_MATCH
:
512 vty_out(vty
, "%% [%s] Unknown command: %s\n",
515 case CMD_ERR_INCOMPLETE
:
516 vty_out(vty
, "%% Command incomplete.\n");
523 static const char telnet_backward_char
= 0x08;
524 static const char telnet_space_char
= ' ';
526 /* Basic function to write buffer to vty. */
527 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
529 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
532 /* Should we do buffering here ? And make vty_flush (vty) ? */
533 buffer_put(vty
->obuf
, buf
, nbytes
);
536 /* Basic function to insert character into vty. */
537 static void vty_self_insert(struct vty
*vty
, char c
)
542 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
545 length
= vty
->length
- vty
->cp
;
546 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
547 vty
->buf
[vty
->cp
] = c
;
549 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
550 for (i
= 0; i
< length
; i
++)
551 vty_write(vty
, &telnet_backward_char
, 1);
556 vty
->buf
[vty
->length
] = '\0';
559 /* Self insert character 'c' in overwrite mode. */
560 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
562 if (vty
->cp
== vty
->length
) {
563 vty_self_insert(vty
, c
);
567 vty
->buf
[vty
->cp
++] = c
;
568 vty_write(vty
, &c
, 1);
572 * Insert a string into vty->buf at the current cursor position.
574 * If the resultant string would be larger than VTY_BUFSIZ it is
577 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
579 if (vty
->cp
== VTY_BUFSIZ
)
582 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
583 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
585 vty
->length
= MAX(vty
->cp
, vty
->length
);
586 vty
->buf
[vty
->length
] = '\0';
587 vty_write(vty
, str
, nwrite
);
590 /* Forward character. */
591 static void vty_forward_char(struct vty
*vty
)
593 if (vty
->cp
< vty
->length
) {
594 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
599 /* Backward character. */
600 static void vty_backward_char(struct vty
*vty
)
604 vty_write(vty
, &telnet_backward_char
, 1);
608 /* Move to the beginning of the line. */
609 static void vty_beginning_of_line(struct vty
*vty
)
612 vty_backward_char(vty
);
615 /* Move to the end of the line. */
616 static void vty_end_of_line(struct vty
*vty
)
618 while (vty
->cp
< vty
->length
)
619 vty_forward_char(vty
);
622 static void vty_kill_line_from_beginning(struct vty
*);
623 static void vty_redraw_line(struct vty
*);
625 /* Print command line history. This function is called from
626 vty_next_line and vty_previous_line. */
627 static void vty_history_print(struct vty
*vty
)
631 vty_kill_line_from_beginning(vty
);
633 /* Get previous line from history buffer */
634 length
= strlen(vty
->hist
[vty
->hp
]);
635 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
636 vty
->cp
= vty
->length
= length
;
637 vty
->buf
[vty
->length
] = '\0';
639 /* Redraw current line */
640 vty_redraw_line(vty
);
643 /* Show next command line history. */
644 static void vty_next_line(struct vty
*vty
)
648 if (vty
->hp
== vty
->hindex
)
651 /* Try is there history exist or not. */
653 if (try_index
== (VTY_MAXHIST
- 1))
658 /* If there is not history return. */
659 if (vty
->hist
[try_index
] == NULL
)
664 vty_history_print(vty
);
667 /* Show previous command line history. */
668 static void vty_previous_line(struct vty
*vty
)
674 try_index
= VTY_MAXHIST
- 1;
678 if (vty
->hist
[try_index
] == NULL
)
683 vty_history_print(vty
);
686 /* This function redraw all of the command line character. */
687 static void vty_redraw_line(struct vty
*vty
)
689 vty_write(vty
, vty
->buf
, vty
->length
);
690 vty
->cp
= vty
->length
;
694 static void vty_forward_word(struct vty
*vty
)
696 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
697 vty_forward_char(vty
);
699 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
700 vty_forward_char(vty
);
703 /* Backward word without skipping training space. */
704 static void vty_backward_pure_word(struct vty
*vty
)
706 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
707 vty_backward_char(vty
);
711 static void vty_backward_word(struct vty
*vty
)
713 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
714 vty_backward_char(vty
);
716 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
717 vty_backward_char(vty
);
720 /* When '^D' is typed at the beginning of the line we move to the down
722 static void vty_down_level(struct vty
*vty
)
730 /* When '^Z' is received from vty, move down to the enable mode. */
731 static void vty_end_config(struct vty
*vty
)
736 vty_config_exit(vty
);
737 vty
->node
= ENABLE_NODE
;
744 /* Delete a character at the current point. */
745 static void vty_delete_char(struct vty
*vty
)
750 if (vty
->length
== 0) {
755 if (vty
->cp
== vty
->length
)
756 return; /* completion need here? */
758 size
= vty
->length
- vty
->cp
;
761 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
762 vty
->buf
[vty
->length
] = '\0';
764 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
767 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
768 vty_write(vty
, &telnet_space_char
, 1);
770 for (i
= 0; i
< size
; i
++)
771 vty_write(vty
, &telnet_backward_char
, 1);
774 /* Delete a character before the point. */
775 static void vty_delete_backward_char(struct vty
*vty
)
780 vty_backward_char(vty
);
781 vty_delete_char(vty
);
784 /* Kill rest of line from current point. */
785 static void vty_kill_line(struct vty
*vty
)
790 size
= vty
->length
- vty
->cp
;
795 for (i
= 0; i
< size
; i
++)
796 vty_write(vty
, &telnet_space_char
, 1);
797 for (i
= 0; i
< size
; i
++)
798 vty_write(vty
, &telnet_backward_char
, 1);
800 memset(&vty
->buf
[vty
->cp
], 0, size
);
801 vty
->length
= vty
->cp
;
804 /* Kill line from the beginning. */
805 static void vty_kill_line_from_beginning(struct vty
*vty
)
807 vty_beginning_of_line(vty
);
811 /* Delete a word before the point. */
812 static void vty_forward_kill_word(struct vty
*vty
)
814 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
815 vty_delete_char(vty
);
816 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
817 vty_delete_char(vty
);
820 /* Delete a word before the point. */
821 static void vty_backward_kill_word(struct vty
*vty
)
823 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
824 vty_delete_backward_char(vty
);
825 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
826 vty_delete_backward_char(vty
);
829 /* Transpose chars before or at the point. */
830 static void vty_transpose_chars(struct vty
*vty
)
834 /* If length is short or point is near by the beginning of line then
836 if (vty
->length
< 2 || vty
->cp
< 1)
839 /* In case of point is located at the end of the line. */
840 if (vty
->cp
== vty
->length
) {
841 c1
= vty
->buf
[vty
->cp
- 1];
842 c2
= vty
->buf
[vty
->cp
- 2];
844 vty_backward_char(vty
);
845 vty_backward_char(vty
);
846 vty_self_insert_overwrite(vty
, c1
);
847 vty_self_insert_overwrite(vty
, c2
);
849 c1
= vty
->buf
[vty
->cp
];
850 c2
= vty
->buf
[vty
->cp
- 1];
852 vty_backward_char(vty
);
853 vty_self_insert_overwrite(vty
, c1
);
854 vty_self_insert_overwrite(vty
, c2
);
858 /* Do completion at vty interface. */
859 static void vty_complete_command(struct vty
*vty
)
863 char **matched
= NULL
;
866 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
869 vline
= cmd_make_strvec(vty
->buf
);
873 /* In case of 'help \t'. */
874 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
875 vector_set(vline
, NULL
);
877 matched
= cmd_complete_command(vline
, vty
, &ret
);
879 cmd_free_strvec(vline
);
883 case CMD_ERR_AMBIGUOUS
:
884 vty_out(vty
, "%% Ambiguous command.\n");
886 vty_redraw_line(vty
);
888 case CMD_ERR_NO_MATCH
:
889 /* vty_out (vty, "%% There is no matched command.\n"); */
891 vty_redraw_line(vty
);
893 case CMD_COMPLETE_FULL_MATCH
:
895 /* 2016-11-28 equinox -- need to debug, SEGV here */
896 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
898 vty_redraw_line(vty
);
902 vty_redraw_line(vty
);
903 vty_backward_pure_word(vty
);
904 vty_insert_word_overwrite(vty
, matched
[0]);
905 vty_self_insert(vty
, ' ');
906 XFREE(MTYPE_COMPLETION
, matched
[0]);
908 case CMD_COMPLETE_MATCH
:
910 vty_redraw_line(vty
);
911 vty_backward_pure_word(vty
);
912 vty_insert_word_overwrite(vty
, matched
[0]);
913 XFREE(MTYPE_COMPLETION
, matched
[0]);
915 case CMD_COMPLETE_LIST_MATCH
:
916 for (i
= 0; matched
[i
] != NULL
; i
++) {
917 if (i
!= 0 && ((i
% 6) == 0))
919 vty_out(vty
, "%-10s ", matched
[i
]);
920 XFREE(MTYPE_COMPLETION
, matched
[i
]);
925 vty_redraw_line(vty
);
927 case CMD_ERR_NOTHING_TODO
:
929 vty_redraw_line(vty
);
934 XFREE(MTYPE_TMP
, matched
);
937 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
938 unsigned int desc_width
, struct cmd_token
*token
)
946 if (desc_width
<= 0) {
947 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
951 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
953 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
954 for (pos
= desc_width
; pos
> 0; pos
--)
955 if (*(p
+ pos
) == ' ')
963 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
968 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
970 XFREE(MTYPE_TMP
, buf
);
973 /* Describe matched command function. */
974 static void vty_describe_command(struct vty
*vty
)
979 unsigned int i
, width
, desc_width
;
980 struct cmd_token
*token
, *token_cr
= NULL
;
982 vline
= cmd_make_strvec(vty
->buf
);
984 /* In case of '> ?'. */
986 vline
= vector_init(1);
987 vector_set(vline
, NULL
);
988 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
989 vector_set(vline
, NULL
);
991 describe
= cmd_describe_command(vline
, vty
, &ret
);
995 /* Ambiguous error. */
997 case CMD_ERR_AMBIGUOUS
:
998 vty_out(vty
, "%% Ambiguous command.\n");
1001 case CMD_ERR_NO_MATCH
:
1002 vty_out(vty
, "%% There is no matched command.\n");
1007 /* Get width of command string. */
1009 for (i
= 0; i
< vector_active(describe
); i
++)
1010 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1013 if (token
->text
[0] == '\0')
1016 len
= strlen(token
->text
);
1022 /* Get width of description string. */
1023 desc_width
= vty
->width
- (width
+ 6);
1025 /* Print out description. */
1026 for (i
= 0; i
< vector_active(describe
); i
++)
1027 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1028 if (token
->text
[0] == '\0')
1031 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1037 vty_out(vty
, " %-s\n", token
->text
);
1038 else if (desc_width
>= strlen(token
->desc
))
1039 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1042 vty_describe_fold(vty
, width
, desc_width
,
1045 if (IS_VARYING_TOKEN(token
->type
)) {
1046 const char *ref
= vector_slot(
1047 vline
, vector_active(vline
) - 1);
1049 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1050 cmd_variable_complete(token
, ref
, varcomps
);
1052 if (vector_active(varcomps
) > 0) {
1053 char *ac
= cmd_variable_comp2str(
1054 varcomps
, vty
->width
);
1055 vty_out(vty
, "%s\n", ac
);
1056 XFREE(MTYPE_TMP
, ac
);
1059 vector_free(varcomps
);
1063 if ((token
= token_cr
)) {
1065 vty_out(vty
, " %-s\n", token
->text
);
1066 else if (desc_width
>= strlen(token
->desc
))
1067 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1070 vty_describe_fold(vty
, width
, desc_width
, token
);
1074 cmd_free_strvec(vline
);
1076 vector_free(describe
);
1079 vty_redraw_line(vty
);
1082 static void vty_clear_buf(struct vty
*vty
)
1084 memset(vty
->buf
, 0, vty
->max
);
1087 /* ^C stop current input and do not add command line to the history. */
1088 static void vty_stop_input(struct vty
*vty
)
1090 vty
->cp
= vty
->length
= 0;
1095 vty_config_exit(vty
);
1096 vty
->node
= ENABLE_NODE
;
1101 /* Set history pointer to the latest one. */
1102 vty
->hp
= vty
->hindex
;
1105 /* Add current command line to the history buffer. */
1106 static void vty_hist_add(struct vty
*vty
)
1110 if (vty
->length
== 0)
1113 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1115 /* Ignore the same string as previous one. */
1116 if (vty
->hist
[index
])
1117 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1118 vty
->hp
= vty
->hindex
;
1122 /* Insert history entry. */
1123 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1124 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1126 /* History index rotation. */
1128 if (vty
->hindex
== VTY_MAXHIST
)
1131 vty
->hp
= vty
->hindex
;
1134 /* #define TELNET_OPTION_DEBUG */
1136 /* Get telnet window size. */
1137 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1139 #ifdef TELNET_OPTION_DEBUG
1142 for (i
= 0; i
< nbytes
; i
++) {
1145 vty_out(vty
, "IAC ");
1148 vty_out(vty
, "WILL ");
1151 vty_out(vty
, "WONT ");
1154 vty_out(vty
, "DO ");
1157 vty_out(vty
, "DONT ");
1160 vty_out(vty
, "SB ");
1163 vty_out(vty
, "SE ");
1166 vty_out(vty
, "TELOPT_ECHO \n");
1169 vty_out(vty
, "TELOPT_SGA \n");
1172 vty_out(vty
, "TELOPT_NAWS \n");
1175 vty_out(vty
, "%x ", buf
[i
]);
1181 #endif /* TELNET_OPTION_DEBUG */
1186 vty
->iac_sb_in_progress
= 1;
1189 if (!vty
->iac_sb_in_progress
)
1192 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1193 vty
->iac_sb_in_progress
= 0;
1196 switch (vty
->sb_buf
[0]) {
1198 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1201 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1203 (unsigned long)vty
->sb_len
);
1204 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1207 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1208 (unsigned long)sizeof(vty
->sb_buf
),
1209 TELNET_NAWS_SB_LEN
);
1211 vty
->width
= ((vty
->sb_buf
[1] << 8)
1213 vty
->height
= ((vty
->sb_buf
[3] << 8)
1215 #ifdef TELNET_OPTION_DEBUG
1217 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1218 vty
->width
, vty
->height
);
1223 vty
->iac_sb_in_progress
= 0;
1232 /* Execute current command line. */
1233 static int vty_execute(struct vty
*vty
)
1239 switch (vty
->node
) {
1241 case AUTH_ENABLE_NODE
:
1242 vty_auth(vty
, vty
->buf
);
1245 ret
= vty_command(vty
, vty
->buf
);
1246 if (vty
->type
== VTY_TERM
)
1251 /* Clear command line buffer. */
1252 vty
->cp
= vty
->length
= 0;
1255 if (vty
->status
!= VTY_CLOSE
)
1261 #define CONTROL(X) ((X) - '@')
1262 #define VTY_NORMAL 0
1263 #define VTY_PRE_ESCAPE 1
1264 #define VTY_ESCAPE 2
1267 /* Escape character command map. */
1268 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1272 vty_previous_line(vty
);
1278 vty_forward_char(vty
);
1281 vty_backward_char(vty
);
1287 /* Go back to normal mode. */
1288 vty
->escape
= VTY_NORMAL
;
1291 /* Quit print out to the buffer. */
1292 static void vty_buffer_reset(struct vty
*vty
)
1294 buffer_reset(vty
->obuf
);
1295 buffer_reset(vty
->lbuf
);
1297 vty_redraw_line(vty
);
1300 /* Read data via vty socket. */
1301 static void vty_read(struct thread
*thread
)
1305 unsigned char buf
[VTY_READ_BUFSIZ
];
1307 struct vty
*vty
= THREAD_ARG(thread
);
1309 /* Read raw data from socket */
1310 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1312 if (ERRNO_IO_RETRY(errno
)) {
1313 vty_event(VTY_READ
, vty
);
1318 "%s: read error on vty client fd %d, closing: %s",
1319 __func__
, vty
->fd
, safe_strerror(errno
));
1320 buffer_reset(vty
->obuf
);
1321 buffer_reset(vty
->lbuf
);
1323 vty
->status
= VTY_CLOSE
;
1326 for (i
= 0; i
< nbytes
; i
++) {
1327 if (buf
[i
] == IAC
) {
1336 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1337 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1338 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1344 /* In case of telnet command */
1346 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1353 if (vty
->status
== VTY_MORE
) {
1358 vty_buffer_reset(vty
);
1366 /* Escape character. */
1367 if (vty
->escape
== VTY_ESCAPE
) {
1368 vty_escape_map(buf
[i
], vty
);
1372 /* Pre-escape status. */
1373 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1376 vty
->escape
= VTY_ESCAPE
;
1379 vty_backward_word(vty
);
1380 vty
->escape
= VTY_NORMAL
;
1383 vty_forward_word(vty
);
1384 vty
->escape
= VTY_NORMAL
;
1387 vty_forward_kill_word(vty
);
1388 vty
->escape
= VTY_NORMAL
;
1392 vty_backward_kill_word(vty
);
1393 vty
->escape
= VTY_NORMAL
;
1396 vty
->escape
= VTY_NORMAL
;
1402 if (vty
->escape
== VTY_CR
) {
1403 /* if we get CR+NL, the NL results in an extra empty
1404 * prompt line being printed without this; just drop
1405 * the NL if it immediately follows CR.
1407 vty
->escape
= VTY_NORMAL
;
1415 vty_beginning_of_line(vty
);
1418 vty_backward_char(vty
);
1421 vty_stop_input(vty
);
1424 vty_delete_char(vty
);
1427 vty_end_of_line(vty
);
1430 vty_forward_char(vty
);
1434 vty_delete_backward_char(vty
);
1443 vty_previous_line(vty
);
1446 vty_transpose_chars(vty
);
1449 vty_kill_line_from_beginning(vty
);
1452 vty_backward_kill_word(vty
);
1455 vty_end_config(vty
);
1458 vty
->escape
= VTY_CR
;
1462 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1465 if (vty
->pass_fd
!= -1) {
1466 close(vty
->pass_fd
);
1471 vty_complete_command(vty
);
1474 if (vty
->node
== AUTH_NODE
1475 || vty
->node
== AUTH_ENABLE_NODE
)
1476 vty_self_insert(vty
, buf
[i
]);
1478 vty_describe_command(vty
);
1481 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1482 vty
->escape
= VTY_ESCAPE
;
1485 vty
->escape
= VTY_PRE_ESCAPE
;
1488 if (buf
[i
] > 31 && buf
[i
] < 127)
1489 vty_self_insert(vty
, buf
[i
]);
1495 if (vty
->status
== VTY_CLOSE
)
1498 vty_event(VTY_WRITE
, vty
);
1499 vty_event(VTY_READ
, vty
);
1503 /* Flush buffer to the vty. */
1504 static void vty_flush(struct thread
*thread
)
1507 buffer_status_t flushrc
;
1508 struct vty
*vty
= THREAD_ARG(thread
);
1510 /* Tempolary disable read thread. */
1511 if (vty
->lines
== 0)
1512 THREAD_OFF(vty
->t_read
);
1514 /* Function execution continue. */
1515 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1517 /* N.B. if width is 0, that means we don't know the window size. */
1518 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1519 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1520 else if (vty
->status
== VTY_MORELINE
)
1521 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1524 flushrc
= buffer_flush_window(
1525 vty
->obuf
, vty
->wfd
, vty
->width
,
1526 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1529 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1531 buffer_reset(vty
->lbuf
);
1532 buffer_reset(vty
->obuf
);
1536 if (vty
->status
== VTY_CLOSE
)
1539 vty
->status
= VTY_NORMAL
;
1540 if (vty
->lines
== 0)
1541 vty_event(VTY_READ
, vty
);
1544 case BUFFER_PENDING
:
1545 /* There is more data waiting to be written. */
1546 vty
->status
= VTY_MORE
;
1547 if (vty
->lines
== 0)
1548 vty_event(VTY_WRITE
, vty
);
1553 /* Allocate new vty struct. */
1554 struct vty
*vty_new(void)
1556 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1558 new->fd
= new->wfd
= -1;
1560 new->lbuf
= buffer_new(0);
1561 new->obuf
= buffer_new(0); /* Use default buffer size. */
1562 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1563 new->max
= VTY_BUFSIZ
;
1570 /* allocate and initialise vty */
1571 static struct vty
*vty_new_init(int vty_sock
)
1577 vty
->wfd
= vty_sock
;
1578 vty
->type
= VTY_TERM
;
1579 vty
->node
= AUTH_NODE
;
1584 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1587 vty
->xpath_index
= 0;
1588 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1589 vty
->private_config
= false;
1590 vty
->candidate_config
= vty_shared_candidate_config
;
1591 vty
->status
= VTY_NORMAL
;
1594 vty
->iac_sb_in_progress
= 0;
1597 vtys_add_tail(vty_sessions
, vty
);
1602 /* Create new vty structure. */
1603 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1605 char buf
[SU_ADDRSTRLEN
];
1608 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1610 /* Allocate new vty structure and set up default values. */
1611 vty
= vty_new_init(vty_sock
);
1613 /* configurable parameters not part of basic init */
1614 vty
->v_timeout
= vty_timeout_val
;
1615 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1616 if (no_password_check
) {
1618 vty
->node
= ENABLE_NODE
;
1620 vty
->node
= VIEW_NODE
;
1622 if (host
.lines
>= 0)
1623 vty
->lines
= host
.lines
;
1625 if (!no_password_check
) {
1626 /* Vty is not available if password isn't set. */
1627 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1628 vty_out(vty
, "Vty password is not set.\n");
1629 vty
->status
= VTY_CLOSE
;
1635 /* Say hello to the world. */
1637 if (!no_password_check
)
1638 vty_out(vty
, "\nUser Access Verification\n\n");
1640 /* Setting up terminal. */
1642 vty_will_suppress_go_ahead(vty
);
1644 vty_dont_linemode(vty
);
1645 vty_do_window_size(vty
);
1646 /* vty_dont_lflow_ahead (vty); */
1650 /* Add read/write thread. */
1651 vty_event(VTY_WRITE
, vty
);
1652 vty_event(VTY_READ
, vty
);
1657 /* create vty for stdio */
1658 static struct termios stdio_orig_termios
;
1659 static struct vty
*stdio_vty
= NULL
;
1660 static bool stdio_termios
= false;
1661 static void (*stdio_vty_atclose
)(int isexit
);
1663 static void vty_stdio_reset(int isexit
)
1667 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1668 stdio_termios
= false;
1672 if (stdio_vty_atclose
)
1673 stdio_vty_atclose(isexit
);
1674 stdio_vty_atclose
= NULL
;
1678 static void vty_stdio_atexit(void)
1683 void vty_stdio_suspend(void)
1688 THREAD_OFF(stdio_vty
->t_write
);
1689 THREAD_OFF(stdio_vty
->t_read
);
1690 THREAD_OFF(stdio_vty
->t_timeout
);
1693 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1694 stdio_termios
= false;
1697 void vty_stdio_resume(void)
1702 if (!tcgetattr(0, &stdio_orig_termios
)) {
1703 struct termios termios
;
1705 termios
= stdio_orig_termios
;
1706 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1707 | IGNCR
| ICRNL
| IXON
);
1708 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1709 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1710 termios
.c_cflag
|= CS8
;
1711 tcsetattr(0, TCSANOW
, &termios
);
1712 stdio_termios
= true;
1715 vty_prompt(stdio_vty
);
1717 /* Add read/write thread. */
1718 vty_event(VTY_WRITE
, stdio_vty
);
1719 vty_event(VTY_READ
, stdio_vty
);
1722 void vty_stdio_close(void)
1726 vty_close(stdio_vty
);
1729 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1733 /* refuse creating two vtys on stdio */
1737 vty
= stdio_vty
= vty_new_init(0);
1738 stdio_vty_atclose
= atclose
;
1741 /* always have stdio vty in a known _unchangeable_ state, don't want
1743 * to have any effect here to make sure scripting this works as intended
1745 vty
->node
= ENABLE_NODE
;
1747 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1753 /* Accept connection from the network. */
1754 static void vty_accept(struct thread
*thread
)
1756 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1761 int accept_sock
= vtyserv
->sock
;
1763 struct access_list
*acl
= NULL
;
1765 /* We continue hearing vty socket. */
1766 vty_event_serv(VTY_SERV
, vtyserv
);
1768 memset(&su
, 0, sizeof(union sockunion
));
1770 /* We can handle IPv4 or IPv6 socket. */
1771 vty_sock
= sockunion_accept(accept_sock
, &su
);
1773 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1774 safe_strerror(errno
));
1777 set_nonblocking(vty_sock
);
1778 set_cloexec(vty_sock
);
1780 if (!sockunion2hostprefix(&su
, &p
)) {
1782 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1787 /* VTY's accesslist apply. */
1788 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1789 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1790 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1791 zlog_info("Vty connection refused from %pSU", &su
);
1797 /* VTY's ipv6 accesslist apply. */
1798 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1799 if ((acl
= access_list_lookup(AFI_IP6
,
1800 vty_ipv6_accesslist_name
))
1801 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1802 zlog_info("Vty connection refused from %pSU", &su
);
1809 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1812 zlog_info("can't set sockopt to vty_sock : %s",
1813 safe_strerror(errno
));
1815 zlog_info("Vty connection from %pSU", &su
);
1817 vty_create(vty_sock
, &su
);
1820 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1823 struct addrinfo req
;
1824 struct addrinfo
*ainfo
;
1825 struct addrinfo
*ainfo_save
;
1827 char port_str
[BUFSIZ
];
1829 memset(&req
, 0, sizeof(req
));
1830 req
.ai_flags
= AI_PASSIVE
;
1831 req
.ai_family
= AF_UNSPEC
;
1832 req
.ai_socktype
= SOCK_STREAM
;
1833 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1834 port_str
[sizeof(port_str
) - 1] = '\0';
1836 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1839 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1847 struct vty_serv
*vtyserv
;
1849 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1852 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1853 ainfo
->ai_protocol
);
1857 sockopt_v6only(ainfo
->ai_family
, sock
);
1858 sockopt_reuseaddr(sock
);
1859 sockopt_reuseport(sock
);
1862 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1864 close(sock
); /* Avoid sd leak. */
1868 ret
= listen(sock
, 3);
1870 close(sock
); /* Avoid sd leak. */
1874 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1875 vtyserv
->sock
= sock
;
1876 vtyservs_add_tail(vty_servs
, vtyserv
);
1878 vty_event_serv(VTY_SERV
, vtyserv
);
1879 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1881 freeaddrinfo(ainfo_save
);
1885 /* For sockaddr_un. */
1888 /* VTY shell UNIX domain socket. */
1889 static void vty_serv_un(const char *path
)
1891 struct vty_serv
*vtyserv
;
1894 struct sockaddr_un serv
;
1896 struct zprivs_ids_t ids
;
1898 /* First of all, unlink existing socket */
1902 old_mask
= umask(0007);
1904 /* Make UNIX domain socket. */
1905 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1907 flog_err_sys(EC_LIB_SOCKET
,
1908 "Cannot create unix stream socket: %s",
1909 safe_strerror(errno
));
1913 /* Make server socket. */
1914 memset(&serv
, 0, sizeof(serv
));
1915 serv
.sun_family
= AF_UNIX
;
1916 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1917 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1918 len
= serv
.sun_len
= SUN_LEN(&serv
);
1920 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1921 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1925 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1927 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1928 safe_strerror(errno
));
1929 close(sock
); /* Avoid sd leak. */
1933 ret
= listen(sock
, 5);
1935 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1936 safe_strerror(errno
));
1937 close(sock
); /* Avoid sd leak. */
1943 zprivs_get_ids(&ids
);
1945 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1946 earlier for the case when we don't need to chown the file
1947 type casting it here to make a compare */
1948 if ((int)ids
.gid_vty
> 0) {
1949 /* set group of socket */
1950 if (chown(path
, -1, ids
.gid_vty
)) {
1951 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1952 "vty_serv_un: could chown socket, %s",
1953 safe_strerror(errno
));
1957 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1958 vtyserv
->sock
= sock
;
1959 vtyserv
->vtysh
= true;
1960 vtyservs_add_tail(vty_servs
, vtyserv
);
1962 vty_event_serv(VTYSH_SERV
, vtyserv
);
1965 /* #define VTYSH_DEBUG 1 */
1967 static void vtysh_accept(struct thread
*thread
)
1969 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1970 int accept_sock
= vtyserv
->sock
;
1973 struct sockaddr_un client
;
1976 vty_event_serv(VTYSH_SERV
, vtyserv
);
1978 memset(&client
, 0, sizeof(client
));
1979 client_len
= sizeof(struct sockaddr_un
);
1981 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
1982 (socklen_t
*)&client_len
);
1985 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1986 safe_strerror(errno
));
1990 if (set_nonblocking(sock
) < 0) {
1993 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
1994 sock
, safe_strerror(errno
));
2001 printf("VTY shell accept\n");
2002 #endif /* VTYSH_DEBUG */
2007 vty
->type
= VTY_SHELL_SERV
;
2008 vty
->node
= VIEW_NODE
;
2009 vtys_add_tail(vtysh_sessions
, vty
);
2011 vty_event(VTYSH_READ
, vty
);
2014 static int vtysh_do_pass_fd(struct vty
*vty
)
2016 struct iovec iov
[1] = {
2018 .iov_base
= vty
->pass_fd_status
,
2019 .iov_len
= sizeof(vty
->pass_fd_status
),
2023 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2024 struct cmsghdr align
;
2026 struct msghdr mh
= {
2028 .msg_iovlen
= array_size(iov
),
2029 .msg_control
= u
.buf
,
2030 .msg_controllen
= sizeof(u
.buf
),
2032 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2035 memset(&u
.buf
, 0, sizeof(u
.buf
));
2036 cmh
->cmsg_level
= SOL_SOCKET
;
2037 cmh
->cmsg_type
= SCM_RIGHTS
;
2038 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2039 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2041 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2042 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2043 return BUFFER_PENDING
;
2045 close(vty
->pass_fd
);
2047 vty
->status
= VTY_NORMAL
;
2050 return BUFFER_ERROR
;
2052 /* resume accepting commands (suspended in vtysh_read) */
2053 vty_event(VTYSH_READ
, vty
);
2055 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2056 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2058 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2059 return BUFFER_PENDING
;
2061 return BUFFER_EMPTY
;
2064 static int vtysh_flush(struct vty
*vty
)
2068 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2069 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2070 ret
= vtysh_do_pass_fd(vty
);
2073 case BUFFER_PENDING
:
2074 vty_event(VTYSH_WRITE
, vty
);
2077 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2079 buffer_reset(vty
->lbuf
);
2080 buffer_reset(vty
->obuf
);
2089 void vty_pass_fd(struct vty
*vty
, int fd
)
2091 if (vty
->pass_fd
!= -1)
2092 close(vty
->pass_fd
);
2097 static void vtysh_read(struct thread
*thread
)
2103 unsigned char buf
[VTY_READ_BUFSIZ
];
2105 uint8_t header
[4] = {0, 0, 0, 0};
2107 sock
= THREAD_FD(thread
);
2108 vty
= THREAD_ARG(thread
);
2110 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2112 if (ERRNO_IO_RETRY(errno
)) {
2113 vty_event(VTYSH_READ
, vty
);
2118 "%s: read failed on vtysh client fd %d, closing: %s",
2119 __func__
, sock
, safe_strerror(errno
));
2121 buffer_reset(vty
->lbuf
);
2122 buffer_reset(vty
->obuf
);
2125 printf("close vtysh\n");
2126 #endif /* VTYSH_DEBUG */
2131 printf("line: %.*s\n", nbytes
, buf
);
2132 #endif /* VTYSH_DEBUG */
2134 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2135 /* Clear command line buffer. */
2136 vty
->cp
= vty
->length
= 0;
2138 vty_out(vty
, "%% Command is too long.\n");
2140 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2141 vty
->buf
[vty
->length
++] = *p
;
2143 /* Pass this line to parser. */
2144 ret
= vty_execute(vty
);
2145 /* Note that vty_execute clears the command buffer and resets
2146 vty->length to 0. */
2148 /* Return result. */
2150 printf("result: %d\n", ret
);
2151 printf("vtysh node: %d\n", vty
->node
);
2152 #endif /* VTYSH_DEBUG */
2154 if (vty
->pass_fd
!= -1) {
2155 memset(vty
->pass_fd_status
, 0, 4);
2156 vty
->pass_fd_status
[3] = ret
;
2157 vty
->status
= VTY_PASSFD
;
2160 vty_event(VTYSH_WRITE
, vty
);
2162 /* this introduces a "sequence point"
2163 * command output is written normally,
2164 * read processing is suspended until
2166 * then retcode + FD is written
2167 * then normal processing resumes
2169 * => skip vty_event(VTYSH_READ, vty)!
2174 /* hack for asynchronous "write integrated"
2175 * - other commands in "buf" will be ditched
2176 * - input during pending config-write is
2178 if (ret
== CMD_SUSPEND
)
2181 /* warning: watchfrr hardcodes this result write
2184 buffer_put(vty
->obuf
, header
, 4);
2186 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2187 /* Try to flush results; exit if a write
2194 if (vty
->status
== VTY_CLOSE
)
2197 vty_event(VTYSH_READ
, vty
);
2200 static void vtysh_write(struct thread
*thread
)
2202 struct vty
*vty
= THREAD_ARG(thread
);
2209 /* Determine address family to bind. */
2210 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2212 /* If port is set to 0, do not listen on TCP/IP at all! */
2214 vty_serv_sock_addrinfo(addr
, port
);
2221 static void vty_error_delete(void *arg
)
2223 struct vty_error
*ve
= arg
;
2225 XFREE(MTYPE_TMP
, ve
);
2228 /* Close vty interface. Warning: call this only from functions that
2229 will be careful not to access the vty afterwards (since it has
2230 now been freed). This is safest from top-level functions (called
2231 directly by the thread dispatcher). */
2232 void vty_close(struct vty
*vty
)
2235 bool was_stdio
= false;
2237 /* Drop out of configure / transaction if needed. */
2238 vty_config_exit(vty
);
2240 /* Cancel threads.*/
2241 THREAD_OFF(vty
->t_read
);
2242 THREAD_OFF(vty
->t_write
);
2243 THREAD_OFF(vty
->t_timeout
);
2245 if (vty
->pass_fd
!= -1) {
2246 close(vty
->pass_fd
);
2249 zlog_live_close(&vty
->live_log
);
2252 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2254 /* Free input buffer. */
2255 buffer_free(vty
->obuf
);
2256 buffer_free(vty
->lbuf
);
2258 /* Free command history. */
2259 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2260 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2264 if (vty
->fd
!= -1) {
2265 if (vty
->type
== VTY_SHELL_SERV
)
2266 vtys_del(vtysh_sessions
, vty
);
2268 vtys_del(vty_sessions
, vty
);
2271 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2275 * note check is for fd > STDERR_FILENO, not fd != -1.
2276 * We never close stdin/stdout/stderr here, because we may be
2277 * running in foreground mode with logging to stdout. Also,
2278 * additionally, we'd need to replace these fds with /dev/null. */
2279 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2281 if (vty
->fd
> STDERR_FILENO
)
2283 if (vty
->fd
== STDIN_FILENO
)
2286 XFREE(MTYPE_VTY
, vty
->buf
);
2289 vty
->error
->del
= vty_error_delete
;
2290 list_delete(&vty
->error
);
2294 XFREE(MTYPE_VTY
, vty
);
2300 /* When time out occur output message then close connection. */
2301 static void vty_timeout(struct thread
*thread
)
2305 vty
= THREAD_ARG(thread
);
2309 buffer_reset(vty
->lbuf
);
2310 buffer_reset(vty
->obuf
);
2311 vty_out(vty
, "\nVty connection is timed out.\n");
2313 /* Close connection. */
2314 vty
->status
= VTY_CLOSE
;
2318 /* Read up configuration file from file_name. */
2319 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2323 struct vty_error
*ve
;
2324 struct listnode
*node
;
2325 unsigned int line_num
= 0;
2328 /* vty_close won't close stderr; if some config command prints
2329 * something it'll end up there. (not ideal; it'd be better if output
2330 * from a file-load went to logging instead. Also note that if this
2331 * function is called after daemonizing, stderr will be /dev/null.)
2333 * vty->fd will be -1 from vty_new()
2335 vty
->wfd
= STDERR_FILENO
;
2336 vty
->type
= VTY_FILE
;
2337 vty
->node
= CONFIG_NODE
;
2340 vty
->candidate_config
= config
;
2342 vty
->private_config
= true;
2343 vty
->candidate_config
= nb_config_new(NULL
);
2346 /* Execute configuration file */
2347 ret
= config_from_file(vty
, confp
, &line_num
);
2349 /* Flush any previous errors before printing messages below */
2350 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2352 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2353 const char *message
= NULL
;
2357 case CMD_ERR_AMBIGUOUS
:
2358 message
= "Ambiguous command";
2360 case CMD_ERR_NO_MATCH
:
2361 message
= "No such command";
2364 message
= "Command returned Warning";
2366 case CMD_WARNING_CONFIG_FAILED
:
2367 message
= "Command returned Warning Config Failed";
2369 case CMD_ERR_INCOMPLETE
:
2370 message
= "Command returned Incomplete";
2372 case CMD_ERR_EXEED_ARGC_MAX
:
2374 "Command exceeded maximum number of Arguments";
2377 message
= "Command returned unhandled error message";
2381 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2382 nl
= strchr(ve
->error_buf
, '\n');
2385 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2386 message
, ve
->line_num
, ve
->error_buf
);
2391 * Automatically commit the candidate configuration after
2392 * reading the configuration file.
2394 if (config
== NULL
) {
2395 struct nb_context context
= {};
2396 char errmsg
[BUFSIZ
] = {0};
2398 context
.client
= NB_CLIENT_CLI
;
2400 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2401 "Read configuration file", NULL
,
2402 errmsg
, sizeof(errmsg
));
2403 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2405 "%s: failed to read configuration file: %s (%s)",
2406 __func__
, nb_err_name(ret
), errmsg
);
2412 static FILE *vty_use_backup_config(const char *fullpath
)
2414 char *fullpath_sav
, *fullpath_tmp
;
2420 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2421 fullpath_sav
= malloc(fullpath_sav_sz
);
2422 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2423 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2425 sav
= open(fullpath_sav
, O_RDONLY
);
2431 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2432 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2434 /* Open file to configuration write. */
2435 tmp
= mkstemp(fullpath_tmp
);
2439 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2442 while ((c
= read(sav
, buffer
, 512)) > 0) {
2443 if (write(tmp
, buffer
, c
) <= 0)
2449 if (rename(fullpath_tmp
, fullpath
) == 0)
2450 ret
= fopen(fullpath
, "r");
2452 unlink(fullpath_tmp
);
2457 unlink(fullpath_tmp
);
2467 /* Read up configuration file from file_name. */
2468 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2469 char *config_default_dir
)
2471 char cwd
[MAXPATHLEN
];
2473 const char *fullpath
;
2475 bool read_success
= false;
2477 /* If -f flag specified. */
2478 if (config_file
!= NULL
) {
2479 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2480 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2483 "%s: failure to determine Current Working Directory %d!",
2485 goto tmp_free_and_out
;
2487 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2488 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2489 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2492 fullpath
= config_file
;
2494 confp
= fopen(fullpath
, "r");
2496 if (confp
== NULL
) {
2498 EC_LIB_BACKUP_CONFIG
,
2499 "%s: failed to open configuration file %s: %s, checking backup",
2500 __func__
, fullpath
, safe_strerror(errno
));
2502 confp
= vty_use_backup_config(fullpath
);
2504 flog_warn(EC_LIB_BACKUP_CONFIG
,
2505 "using backup configuration file!");
2509 "%s: can't open configuration file [%s]",
2510 __func__
, config_file
);
2511 goto tmp_free_and_out
;
2516 host_config_set(config_default_dir
);
2520 struct stat conf_stat
;
2522 /* !!!!PLEASE LEAVE!!!!
2523 * This is NEEDED for use with vtysh -b, or else you can get
2524 * a real configuration food fight with a lot garbage in the
2525 * merged configuration file it creates coming from the per
2526 * daemon configuration files. This also allows the daemons
2527 * to start if there default configuration file is not
2528 * present or ignore them, as needed when using vtysh -b to
2529 * configure the daemons at boot - MAG
2532 /* Stat for vtysh Zebra.conf, if found startup and wait for
2533 * boot configuration
2536 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2537 ret
= stat(integrate_default
, &conf_stat
);
2539 read_success
= true;
2540 goto tmp_free_and_out
;
2544 confp
= fopen(config_default_dir
, "r");
2545 if (confp
== NULL
) {
2548 "%s: failed to open configuration file %s: %s, checking backup",
2549 __func__
, config_default_dir
,
2550 safe_strerror(errno
));
2552 confp
= vty_use_backup_config(config_default_dir
);
2554 flog_warn(EC_LIB_BACKUP_CONFIG
,
2555 "using backup configuration file!");
2556 fullpath
= config_default_dir
;
2558 flog_err(EC_LIB_VTY
,
2559 "can't open configuration file [%s]",
2560 config_default_dir
);
2561 goto tmp_free_and_out
;
2564 fullpath
= config_default_dir
;
2567 vty_read_file(config
, confp
);
2568 read_success
= true;
2572 host_config_set(fullpath
);
2575 XFREE(MTYPE_TMP
, tmp
);
2577 return read_success
;
2580 static void update_xpath(struct vty
*vty
, const char *oldpath
,
2581 const char *newpath
)
2585 for (i
= 0; i
< vty
->xpath_index
; i
++) {
2586 if (!frrstr_startswith(vty
->xpath
[i
], oldpath
))
2589 char *tmp
= frrstr_replace(vty
->xpath
[i
], oldpath
, newpath
);
2590 strlcpy(vty
->xpath
[i
], tmp
, sizeof(vty
->xpath
[0]));
2591 XFREE(MTYPE_TMP
, tmp
);
2595 void vty_update_xpath(const char *oldpath
, const char *newpath
)
2599 frr_each (vtys
, vtysh_sessions
, vty
)
2600 update_xpath(vty
, oldpath
, newpath
);
2601 frr_each (vtys
, vty_sessions
, vty
)
2602 update_xpath(vty
, oldpath
, newpath
);
2605 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2607 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2608 vty_out(vty
, "%% Configuration is locked by other client\n");
2612 vty
->node
= CONFIG_NODE
;
2614 vty
->private_config
= private_config
;
2615 vty
->xpath_index
= 0;
2617 if (private_config
) {
2618 vty
->candidate_config
= nb_config_dup(running_config
);
2619 vty
->candidate_config_base
= nb_config_dup(running_config
);
2621 "Warning: uncommitted changes will be discarded on exit.\n\n");
2623 vty
->candidate_config
= vty_shared_candidate_config
;
2624 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2625 vty
->candidate_config_base
=
2626 nb_config_dup(running_config
);
2632 void vty_config_exit(struct vty
*vty
)
2634 enum node_type node
= vty
->node
;
2635 struct cmd_node
*cnode
;
2637 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2638 * somewhere below CONFIG_NODE */
2639 while (node
&& node
!= CONFIG_NODE
) {
2640 cnode
= vector_lookup(cmdvec
, node
);
2641 node
= cnode
->parent_node
;
2643 if (node
!= CONFIG_NODE
)
2644 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2647 while (vty
->node
!= ENABLE_NODE
)
2648 /* will call vty_config_node_exit() below */
2652 int vty_config_node_exit(struct vty
*vty
)
2654 vty
->xpath_index
= 0;
2656 /* Perform any pending commits. */
2657 (void)nb_cli_pending_commit_check(vty
);
2659 /* Check if there's a pending confirmed commit. */
2660 if (vty
->t_confirmed_commit_timeout
) {
2662 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2663 nb_cli_confirmed_commit_rollback(vty
);
2664 nb_cli_confirmed_commit_clean(vty
);
2667 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2669 if (vty
->candidate_config
) {
2670 if (vty
->private_config
)
2671 nb_config_free(vty
->candidate_config
);
2672 vty
->candidate_config
= NULL
;
2674 if (vty
->candidate_config_base
) {
2675 nb_config_free(vty
->candidate_config_base
);
2676 vty
->candidate_config_base
= NULL
;
2679 vty
->config
= false;
2683 /* Master of the threads. */
2684 static struct thread_master
*vty_master
;
2686 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2690 thread_add_read(vty_master
, vty_accept
, vty_serv
,
2691 vty_serv
->sock
, &vty_serv
->t_accept
);
2695 thread_add_read(vty_master
, vtysh_accept
, vty_serv
,
2696 vty_serv
->sock
, &vty_serv
->t_accept
);
2700 assert(!"vty_event_serv() called incorrectly");
2704 static void vty_event(enum vty_event event
, struct vty
*vty
)
2709 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2713 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2718 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2721 /* Time out treatment. */
2722 if (vty
->v_timeout
) {
2723 THREAD_OFF(vty
->t_timeout
);
2724 thread_add_timer(vty_master
, vty_timeout
, vty
,
2725 vty
->v_timeout
, &vty
->t_timeout
);
2729 thread_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2732 case VTY_TIMEOUT_RESET
:
2733 THREAD_OFF(vty
->t_timeout
);
2735 thread_add_timer(vty_master
, vty_timeout
, vty
,
2736 vty
->v_timeout
, &vty
->t_timeout
);
2739 assert(!"vty_event() called incorrectly");
2743 DEFUN_NOSH (config_who
,
2746 "Display who is on vty\n")
2750 frr_each (vtys
, vty_sessions
, v
)
2751 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2752 v
->config
? "*" : " ", v
->fd
, v
->address
,
2753 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2757 /* Move to vty configuration mode. */
2758 DEFUN_NOSH (line_vty
,
2761 "Configure a terminal line\n"
2762 "Virtual terminal\n")
2764 vty
->node
= VTY_NODE
;
2768 /* Set time out value. */
2769 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2770 const char *sec_str
)
2772 unsigned long timeout
= 0;
2774 /* min_str and sec_str are already checked by parser. So it must be
2775 all digit string. */
2777 timeout
= strtol(min_str
, NULL
, 10);
2781 timeout
+= strtol(sec_str
, NULL
, 10);
2783 vty_timeout_val
= timeout
;
2784 vty
->v_timeout
= timeout
;
2785 vty_event(VTY_TIMEOUT_RESET
, vty
);
2791 DEFUN (exec_timeout_min
,
2792 exec_timeout_min_cmd
,
2793 "exec-timeout (0-35791)",
2794 "Set timeout value\n"
2795 "Timeout value in minutes\n")
2798 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2801 DEFUN (exec_timeout_sec
,
2802 exec_timeout_sec_cmd
,
2803 "exec-timeout (0-35791) (0-2147483)",
2804 "Set the EXEC timeout\n"
2805 "Timeout in minutes\n"
2806 "Timeout in seconds\n")
2809 int idx_number_2
= 2;
2810 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2811 argv
[idx_number_2
]->arg
);
2814 DEFUN (no_exec_timeout
,
2815 no_exec_timeout_cmd
,
2818 "Set the EXEC timeout\n")
2820 return exec_timeout(vty
, NULL
, NULL
);
2823 /* Set vty access class. */
2824 DEFUN (vty_access_class
,
2825 vty_access_class_cmd
,
2826 "access-class WORD",
2827 "Filter connections based on an IP access list\n"
2831 if (vty_accesslist_name
)
2832 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2834 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2839 /* Clear vty access class. */
2840 DEFUN (no_vty_access_class
,
2841 no_vty_access_class_cmd
,
2842 "no access-class [WORD]",
2844 "Filter connections based on an IP access list\n"
2848 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2849 if (!vty_accesslist_name
2850 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2851 vty_out(vty
, "Access-class is not currently applied to vty\n");
2852 return CMD_WARNING_CONFIG_FAILED
;
2855 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2857 vty_accesslist_name
= NULL
;
2862 /* Set vty access class. */
2863 DEFUN (vty_ipv6_access_class
,
2864 vty_ipv6_access_class_cmd
,
2865 "ipv6 access-class WORD",
2867 "Filter connections based on an IP access list\n"
2868 "IPv6 access list\n")
2871 if (vty_ipv6_accesslist_name
)
2872 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2874 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2879 /* Clear vty access class. */
2880 DEFUN (no_vty_ipv6_access_class
,
2881 no_vty_ipv6_access_class_cmd
,
2882 "no ipv6 access-class [WORD]",
2885 "Filter connections based on an IP access list\n"
2886 "IPv6 access list\n")
2889 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2891 if (!vty_ipv6_accesslist_name
2892 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2894 "IPv6 access-class is not currently applied to vty\n");
2895 return CMD_WARNING_CONFIG_FAILED
;
2898 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2900 vty_ipv6_accesslist_name
= NULL
;
2909 "Enable password checking\n")
2911 no_password_check
= 0;
2915 DEFUN (no_vty_login
,
2919 "Enable password checking\n")
2921 no_password_check
= 1;
2925 DEFUN (service_advanced_vty
,
2926 service_advanced_vty_cmd
,
2927 "service advanced-vty",
2928 "Set up miscellaneous service\n"
2929 "Enable advanced mode vty interface\n")
2935 DEFUN (no_service_advanced_vty
,
2936 no_service_advanced_vty_cmd
,
2937 "no service advanced-vty",
2939 "Set up miscellaneous service\n"
2940 "Enable advanced mode vty interface\n")
2946 DEFUN_NOSH(terminal_monitor
,
2947 terminal_monitor_cmd
,
2948 "terminal monitor [detach]",
2949 "Set terminal line parameters\n"
2950 "Copy debug output to the current terminal line\n"
2951 "Keep logging feed open independent of VTY session\n")
2955 if (vty
->type
!= VTY_SHELL_SERV
) {
2956 vty_out(vty
, "%% not supported\n");
2961 struct zlog_live_cfg detach_log
= {};
2963 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
2964 zlog_live_disown(&detach_log
);
2966 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
2969 vty_out(vty
, "%% error opening live log: %m\n");
2973 vty_pass_fd(vty
, fd_ret
);
2977 DEFUN_NOSH(no_terminal_monitor
,
2978 no_terminal_monitor_cmd
,
2979 "no terminal monitor",
2981 "Set terminal line parameters\n"
2982 "Copy debug output to the current terminal line\n")
2984 zlog_live_close(&vty
->live_log
);
2988 DEFUN_NOSH(terminal_no_monitor
,
2989 terminal_no_monitor_cmd
,
2990 "terminal no monitor",
2991 "Set terminal line parameters\n"
2993 "Copy debug output to the current terminal line\n")
2995 return no_terminal_monitor(self
, vty
, argc
, argv
);
2999 DEFUN_NOSH (show_history
,
3003 "Display the session command history\n")
3007 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3008 if (index
== VTY_MAXHIST
) {
3013 if (vty
->hist
[index
] != NULL
)
3014 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3023 DEFPY (log_commands
,
3025 "[no] log commands",
3028 "Log all commands\n")
3031 if (do_log_commands_perm
) {
3033 "Daemon started with permanent logging turned on for commands, ignoring\n");
3037 do_log_commands
= false;
3039 do_log_commands
= true;
3044 /* Display current configuration. */
3045 static int vty_config_write(struct vty
*vty
)
3047 vty_frame(vty
, "line vty\n");
3049 if (vty_accesslist_name
)
3050 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3052 if (vty_ipv6_accesslist_name
)
3053 vty_out(vty
, " ipv6 access-class %s\n",
3054 vty_ipv6_accesslist_name
);
3057 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3058 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3059 vty_timeout_val
% 60);
3062 if (no_password_check
)
3063 vty_out(vty
, " no login\n");
3065 vty_endframe(vty
, "exit\n");
3067 if (do_log_commands
)
3068 vty_out(vty
, "log commands\n");
3070 vty_out(vty
, "!\n");
3075 static int vty_config_write(struct vty
*vty
);
3076 struct cmd_node vty_node
= {
3079 .parent_node
= CONFIG_NODE
,
3080 .prompt
= "%s(config-line)# ",
3081 .config_write
= vty_config_write
,
3084 /* Reset all VTY status. */
3085 void vty_reset(void)
3089 frr_each_safe (vtys
, vty_sessions
, vty
) {
3090 buffer_reset(vty
->lbuf
);
3091 buffer_reset(vty
->obuf
);
3092 vty
->status
= VTY_CLOSE
;
3096 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3098 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3099 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3102 static void vty_save_cwd(void)
3106 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3110 * At this point if these go wrong, more than likely
3111 * the whole world is coming down around us
3112 * Hence not worrying about it too much.
3114 if (chdir(SYSCONFDIR
)) {
3115 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3116 "Failure to chdir to %s, errno: %d",
3120 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3121 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3122 "Failure to getcwd, errno: %d", errno
);
3128 char *vty_get_cwd(void)
3133 int vty_shell(struct vty
*vty
)
3135 return vty
->type
== VTY_SHELL
? 1 : 0;
3138 int vty_shell_serv(struct vty
*vty
)
3140 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3143 void vty_init_vtysh(void)
3145 /* currently nothing to do, but likely to have future use */
3148 /* Install vty's own commands like `who' command. */
3149 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3151 /* For further configuration read, preserve current directory. */
3154 vty_master
= master_thread
;
3156 atexit(vty_stdio_atexit
);
3158 /* Install bgp top node. */
3159 install_node(&vty_node
);
3161 install_element(VIEW_NODE
, &config_who_cmd
);
3162 install_element(VIEW_NODE
, &show_history_cmd
);
3163 install_element(CONFIG_NODE
, &line_vty_cmd
);
3164 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3165 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3166 install_element(CONFIG_NODE
, &show_history_cmd
);
3167 install_element(CONFIG_NODE
, &log_commands_cmd
);
3169 if (do_command_logging
) {
3170 do_log_commands
= true;
3171 do_log_commands_perm
= true;
3174 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3175 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3176 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3178 install_default(VTY_NODE
);
3179 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3180 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3181 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3182 install_element(VTY_NODE
, &vty_access_class_cmd
);
3183 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3184 install_element(VTY_NODE
, &vty_login_cmd
);
3185 install_element(VTY_NODE
, &no_vty_login_cmd
);
3186 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3187 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3190 void vty_terminate(void)
3193 struct vty_serv
*vtyserv
;
3195 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3199 /* default state of vty_sessions is initialized & empty. */
3200 vtys_fini(vty_sessions
);
3201 vtys_init(vty_sessions
);
3203 /* vty_reset() doesn't close vtysh sessions */
3204 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3205 buffer_reset(vty
->lbuf
);
3206 buffer_reset(vty
->obuf
);
3207 vty
->status
= VTY_CLOSE
;
3211 vtys_fini(vtysh_sessions
);
3212 vtys_init(vtysh_sessions
);
3214 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
3215 THREAD_OFF(vtyserv
->t_accept
);
3216 close(vtyserv
->sock
);
3217 XFREE(MTYPE_VTY_SERV
, vtyserv
);
3220 vtyservs_fini(vty_servs
);
3221 vtyservs_init(vty_servs
);