1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtual terminal [aka TeletYpe] interface routine.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
9 #include <lib/version.h>
10 #include <sys/types.h>
11 #include <sys/types.h>
12 #ifdef HAVE_LIBPCRE2_POSIX
13 #ifndef _FRR_PCRE2_POSIX
14 #define _FRR_PCRE2_POSIX
15 #include <pcre2posix.h>
16 #endif /* _FRR_PCRE2_POSIX */
17 #elif defined(HAVE_LIBPCREPOSIX)
18 #include <pcreposix.h>
21 #endif /* HAVE_LIBPCRE2_POSIX */
29 #include "sockunion.h"
39 #include "lib_errors.h"
40 #include "northbound_cli.h"
44 #include <arpa/telnet.h>
47 #include "lib/vty_clippy.c"
49 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY");
50 DEFINE_MTYPE_STATIC(LIB
, VTY_SERV
, "VTY server");
51 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer");
52 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history");
54 DECLARE_DLIST(vtys
, struct vty
, itm
);
69 struct nb_config
*vty_mgmt_candidate_config
;
71 static uintptr_t mgmt_lib_hndl
;
72 static bool mgmt_fe_connected
;
73 static bool mgmt_candidate_ds_wr_locked
;
74 static uint64_t mgmt_client_id_next
;
75 static uint64_t mgmt_last_req_id
= UINT64_MAX
;
77 PREDECL_DLIST(vtyservs
);
80 struct vtyservs_item itm
;
85 struct event
*t_accept
;
88 DECLARE_DLIST(vtyservs
, struct vty_serv
, itm
);
90 static void vty_event_serv(enum vty_event event
, struct vty_serv
*);
91 static void vty_event(enum vty_event
, struct vty
*);
92 static int vtysh_flush(struct vty
*vty
);
94 /* Extern host structure from command.c */
95 extern struct host host
;
97 /* active listeners */
98 static struct vtyservs_head vty_servs
[1] = {INIT_DLIST(vty_servs
[0])};
100 /* active connections */
101 static struct vtys_head vty_sessions
[1] = {INIT_DLIST(vty_sessions
[0])};
102 static struct vtys_head vtysh_sessions
[1] = {INIT_DLIST(vtysh_sessions
[0])};
104 /* Vty timeout value. */
105 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
107 /* Vty access-class command */
108 static char *vty_accesslist_name
= NULL
;
110 /* Vty access-calss for IPv6. */
111 static char *vty_ipv6_accesslist_name
= NULL
;
113 /* Current directory. */
114 static char vty_cwd
[MAXPATHLEN
];
116 /* Login password check. */
117 static int no_password_check
= 0;
119 /* Integrated configuration file path */
120 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
122 bool vty_log_commands
;
123 static bool vty_log_commands_perm
;
125 char const *const mgmt_daemons
[] = {
130 uint mgmt_daemons_count
= array_size(mgmt_daemons
);
132 void vty_mgmt_resume_response(struct vty
*vty
, bool success
)
134 uint8_t header
[4] = {0, 0, 0, 0};
135 int ret
= CMD_SUCCESS
;
137 if (!vty
->mgmt_req_pending
) {
139 "vty response called without setting mgmt_req_pending");
144 ret
= CMD_WARNING_CONFIG_FAILED
;
146 vty
->mgmt_req_pending
= false;
148 MGMTD_FE_CLIENT_DBG("resuming: %s:", success
? "succeeded" : "failed");
150 if (vty
->type
!= VTY_FILE
) {
152 buffer_put(vty
->obuf
, header
, 4);
153 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0)) {
154 zlog_err("failed to vtysh_flush");
155 /* Try to flush results; exit if a write error occurs */
160 if (vty
->status
== VTY_CLOSE
)
162 else if (vty
->type
!= VTY_FILE
)
163 vty_event(VTYSH_READ
, vty
);
165 /* should we assert here? */
166 zlog_err("mgmtd: unexpected resume while reading config file");
169 void vty_frame(struct vty
*vty
, const char *format
, ...)
173 va_start(args
, format
);
174 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
175 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
176 vty
->frame_pos
= strlen(vty
->frame
);
180 void vty_endframe(struct vty
*vty
, const char *endtext
)
182 if (vty
->frame_pos
== 0 && endtext
)
183 vty_out(vty
, "%s", endtext
);
187 bool vty_set_include(struct vty
*vty
, const char *regexp
)
195 regfree(&vty
->include
);
201 errcode
= regcomp(&vty
->include
, regexp
,
202 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
205 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
206 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
214 /* VTY standard output function. */
215 int vty_out(struct vty
*vty
, const char *format
, ...)
222 /* format string may contain %m, keep errno intact for printfrr */
223 int saved_errno
= errno
;
225 if (vty
->frame_pos
) {
227 vty_out(vty
, "%s", vty
->frame
);
230 va_start(args
, format
);
232 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
239 vector lines
= frrstr_split_vec(p
, "\n");
241 /* Place first value in the cache */
242 char *firstline
= vector_slot(lines
, 0);
243 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
245 /* If our split returned more than one entry, time to filter */
246 if (vector_active(lines
) > 1) {
248 * returned string is MTYPE_TMP so it matches the MTYPE
249 * of everything else in the vector
251 char *bstr
= buffer_getstr(vty
->lbuf
);
252 buffer_reset(vty
->lbuf
);
253 XFREE(MTYPE_TMP
, lines
->index
[0]);
254 vector_set_index(lines
, 0, bstr
);
255 frrstr_filter_vec(lines
, &vty
->include
);
256 vector_compact(lines
);
258 * Consider the string "foo\n". If the regex is an empty string
259 * and the line ended with a newline, then the vector will look
265 * If the regex isn't empty, the vector will look like:
269 * In this case we'd like to preserve the newline, so we add
270 * the empty string [1] as in the first example.
272 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
273 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
274 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
276 filtered
= frrstr_join_vec(lines
, "\n");
282 frrstr_strvec_free(lines
);
293 /* print with crlf replacement */
294 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
299 fprintf(vty
->of
, "%s", filtered
);
301 } else if (vty
->of_saved
) {
302 fprintf(vty
->of_saved
, "%s", filtered
);
303 fflush(vty
->of_saved
);
309 /* print without crlf replacement */
310 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
316 if (vty
->filter
&& filtered
)
317 XFREE(MTYPE_TMP
, filtered
);
319 /* If p is not different with buf, it is allocated buffer. */
321 XFREE(MTYPE_VTY_OUT_BUF
, p
);
326 static int vty_json_helper(struct vty
*vty
, struct json_object
*json
,
334 text
= json_object_to_json_string_ext(
336 vty_out(vty
, "%s\n", text
);
337 json_object_free(json
);
342 int vty_json(struct vty
*vty
, struct json_object
*json
)
344 return vty_json_helper(vty
, json
,
345 JSON_C_TO_STRING_PRETTY
|
346 JSON_C_TO_STRING_NOSLASHESCAPE
);
349 int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
)
351 return vty_json_helper(vty
, json
, JSON_C_TO_STRING_NOSLASHESCAPE
);
354 void vty_json_empty(struct vty
*vty
)
356 json_object
*json
= json_object_new_object();
361 /* Output current time to the vty. */
362 void vty_time_print(struct vty
*vty
, int cr
)
364 char buf
[FRR_TIMESTAMP_LEN
];
366 if (frr_timestamp(0, buf
, sizeof(buf
)) == 0) {
367 zlog_info("frr_timestamp error");
371 vty_out(vty
, "%s\n", buf
);
373 vty_out(vty
, "%s ", buf
);
378 /* Say hello to vty interface. */
379 void vty_hello(struct vty
*vty
)
385 f
= fopen(host
.motdfile
, "r");
387 while (fgets(buf
, sizeof(buf
), f
)) {
389 /* work backwards to ignore trailling isspace()
391 for (s
= buf
+ strlen(buf
);
392 (s
> buf
) && isspace((unsigned char)s
[-1]);
396 vty_out(vty
, "%s\n", buf
);
400 vty_out(vty
, "MOTD file not found\n");
401 } else if (host
.motd
)
402 vty_out(vty
, "%s", host
.motd
);
405 #pragma GCC diagnostic push
406 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
407 /* prompt formatting has a %s in the cmd_node prompt string.
409 * Also for some reason GCC emits the warning on the end of the function
410 * (optimization maybe?) rather than on the vty_out line, so this pragma
411 * wraps the entire function rather than just the vty_out line.
414 /* Put out prompt and wait input from user. */
415 static void vty_prompt(struct vty
*vty
)
417 if (vty
->type
== VTY_TERM
) {
418 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
421 #pragma GCC diagnostic pop
423 /* Send WILL TELOPT_ECHO to remote server. */
424 static void vty_will_echo(struct vty
*vty
)
426 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
427 vty_out(vty
, "%s", cmd
);
430 /* Make suppress Go-Ahead telnet option. */
431 static void vty_will_suppress_go_ahead(struct vty
*vty
)
433 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
434 vty_out(vty
, "%s", cmd
);
437 /* Make don't use linemode over telnet. */
438 static void vty_dont_linemode(struct vty
*vty
)
440 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
441 vty_out(vty
, "%s", cmd
);
444 /* Use window size. */
445 static void vty_do_window_size(struct vty
*vty
)
447 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
448 vty_out(vty
, "%s", cmd
);
451 /* Authentication of vty */
452 static void vty_auth(struct vty
*vty
, char *buf
)
455 enum node_type next_node
= 0;
461 passwd
= host
.password_encrypt
;
463 passwd
= host
.password
;
465 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
467 next_node
= VIEW_NODE
;
469 case AUTH_ENABLE_NODE
:
471 passwd
= host
.enable_encrypt
;
473 passwd
= host
.enable
;
474 next_node
= ENABLE_NODE
;
480 fail
= strcmp(crypt(buf
, passwd
), passwd
);
482 fail
= strcmp(buf
, passwd
);
488 vty
->node
= next_node
; /* Success ! */
491 if (vty
->fail
>= 3) {
492 if (vty
->node
== AUTH_NODE
) {
494 "%% Bad passwords, too many failures!\n");
495 vty
->status
= VTY_CLOSE
;
497 /* AUTH_ENABLE_NODE */
500 "%% Bad enable passwords, too many failures!\n");
501 vty
->status
= VTY_CLOSE
;
507 /* Command execution over the vty interface. */
508 static int vty_command(struct vty
*vty
, char *buf
)
511 const char *protocolname
;
517 * Log non empty command lines
519 if (vty_log_commands
&&
520 strncmp(buf
, "echo PING", strlen("echo PING")) != 0)
523 /* Skip white spaces. */
524 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
527 if (cp
!= NULL
&& *cp
!= '\0') {
528 char vty_str
[VTY_BUFSIZ
];
529 char prompt_str
[VTY_BUFSIZ
];
531 /* format the base vty info */
532 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
535 /* format the prompt */
536 #pragma GCC diagnostic push
537 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
538 /* prompt formatting has a %s in the cmd_node prompt string */
539 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
541 #pragma GCC diagnostic pop
543 /* now log the command */
544 zlog_notice("%s%s", prompt_str
, buf
);
549 unsigned long walltime
, cputime
;
551 /* cmd_execute() may change cputime_enabled if we're executing the
552 * "service cputime-stats" command, which can result in nonsensical
553 * and very confusing warnings
555 bool cputime_enabled_here
= cputime_enabled
;
559 ret
= cmd_execute(vty
, buf
, NULL
, 0);
563 walltime
= event_consumed_time(&after
, &before
, &cputime
);
565 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
566 && cputime
> cputime_threshold
)
567 /* Warn about CPU hog that must be fixed. */
568 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
569 "CPU HOG: command took %lums (cpu time %lums): %s",
570 walltime
/ 1000, cputime
/ 1000, buf
);
571 else if (walltime_threshold
&& walltime
> walltime_threshold
)
572 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
573 "STARVATION: command took %lums (cpu time %lums): %s",
574 walltime
/ 1000, cputime
/ 1000, buf
);
576 /* Get the name of the protocol if any */
577 protocolname
= frr_protoname
;
579 if (ret
!= CMD_SUCCESS
)
582 if (vty
->type
== VTY_FILE
)
583 vty_out(vty
, "Warning...\n");
585 case CMD_ERR_AMBIGUOUS
:
586 vty_out(vty
, "%% Ambiguous command.\n");
588 case CMD_ERR_NO_MATCH
:
589 vty_out(vty
, "%% [%s] Unknown command: %s\n",
592 case CMD_ERR_INCOMPLETE
:
593 vty_out(vty
, "%% Command incomplete.\n");
600 static const char telnet_backward_char
= 0x08;
601 static const char telnet_space_char
= ' ';
603 /* Basic function to write buffer to vty. */
604 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
606 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
609 /* Should we do buffering here ? And make vty_flush (vty) ? */
610 buffer_put(vty
->obuf
, buf
, nbytes
);
613 /* Basic function to insert character into vty. */
614 static void vty_self_insert(struct vty
*vty
, char c
)
619 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
622 length
= vty
->length
- vty
->cp
;
623 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
624 vty
->buf
[vty
->cp
] = c
;
626 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
627 for (i
= 0; i
< length
; i
++)
628 vty_write(vty
, &telnet_backward_char
, 1);
633 vty
->buf
[vty
->length
] = '\0';
636 /* Self insert character 'c' in overwrite mode. */
637 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
639 if (vty
->cp
== vty
->length
) {
640 vty_self_insert(vty
, c
);
644 vty
->buf
[vty
->cp
++] = c
;
645 vty_write(vty
, &c
, 1);
649 * Insert a string into vty->buf at the current cursor position.
651 * If the resultant string would be larger than VTY_BUFSIZ it is
654 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
656 if (vty
->cp
== VTY_BUFSIZ
)
659 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
660 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
662 vty
->length
= MAX(vty
->cp
, vty
->length
);
663 vty
->buf
[vty
->length
] = '\0';
664 vty_write(vty
, str
, nwrite
);
667 /* Forward character. */
668 static void vty_forward_char(struct vty
*vty
)
670 if (vty
->cp
< vty
->length
) {
671 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
676 /* Backward character. */
677 static void vty_backward_char(struct vty
*vty
)
681 vty_write(vty
, &telnet_backward_char
, 1);
685 /* Move to the beginning of the line. */
686 static void vty_beginning_of_line(struct vty
*vty
)
689 vty_backward_char(vty
);
692 /* Move to the end of the line. */
693 static void vty_end_of_line(struct vty
*vty
)
695 while (vty
->cp
< vty
->length
)
696 vty_forward_char(vty
);
699 static void vty_kill_line_from_beginning(struct vty
*);
700 static void vty_redraw_line(struct vty
*);
702 /* Print command line history. This function is called from
703 vty_next_line and vty_previous_line. */
704 static void vty_history_print(struct vty
*vty
)
708 vty_kill_line_from_beginning(vty
);
710 /* Get previous line from history buffer */
711 length
= strlen(vty
->hist
[vty
->hp
]);
712 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
713 vty
->cp
= vty
->length
= length
;
714 vty
->buf
[vty
->length
] = '\0';
716 /* Redraw current line */
717 vty_redraw_line(vty
);
720 /* Show next command line history. */
721 static void vty_next_line(struct vty
*vty
)
725 if (vty
->hp
== vty
->hindex
)
728 /* Try is there history exist or not. */
730 if (try_index
== (VTY_MAXHIST
- 1))
735 /* If there is not history return. */
736 if (vty
->hist
[try_index
] == NULL
)
741 vty_history_print(vty
);
744 /* Show previous command line history. */
745 static void vty_previous_line(struct vty
*vty
)
751 try_index
= VTY_MAXHIST
- 1;
755 if (vty
->hist
[try_index
] == NULL
)
760 vty_history_print(vty
);
763 /* This function redraw all of the command line character. */
764 static void vty_redraw_line(struct vty
*vty
)
766 vty_write(vty
, vty
->buf
, vty
->length
);
767 vty
->cp
= vty
->length
;
771 static void vty_forward_word(struct vty
*vty
)
773 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
774 vty_forward_char(vty
);
776 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
777 vty_forward_char(vty
);
780 /* Backward word without skipping training space. */
781 static void vty_backward_pure_word(struct vty
*vty
)
783 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
784 vty_backward_char(vty
);
788 static void vty_backward_word(struct vty
*vty
)
790 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
791 vty_backward_char(vty
);
793 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
794 vty_backward_char(vty
);
797 /* When '^D' is typed at the beginning of the line we move to the down
799 static void vty_down_level(struct vty
*vty
)
807 /* When '^Z' is received from vty, move down to the enable mode. */
808 static void vty_end_config(struct vty
*vty
)
813 vty_config_exit(vty
);
814 vty
->node
= ENABLE_NODE
;
821 /* Delete a character at the current point. */
822 static void vty_delete_char(struct vty
*vty
)
827 if (vty
->length
== 0) {
832 if (vty
->cp
== vty
->length
)
833 return; /* completion need here? */
835 size
= vty
->length
- vty
->cp
;
838 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
839 vty
->buf
[vty
->length
] = '\0';
841 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
844 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
845 vty_write(vty
, &telnet_space_char
, 1);
847 for (i
= 0; i
< size
; i
++)
848 vty_write(vty
, &telnet_backward_char
, 1);
851 /* Delete a character before the point. */
852 static void vty_delete_backward_char(struct vty
*vty
)
857 vty_backward_char(vty
);
858 vty_delete_char(vty
);
861 /* Kill rest of line from current point. */
862 static void vty_kill_line(struct vty
*vty
)
867 size
= vty
->length
- vty
->cp
;
872 for (i
= 0; i
< size
; i
++)
873 vty_write(vty
, &telnet_space_char
, 1);
874 for (i
= 0; i
< size
; i
++)
875 vty_write(vty
, &telnet_backward_char
, 1);
877 memset(&vty
->buf
[vty
->cp
], 0, size
);
878 vty
->length
= vty
->cp
;
881 /* Kill line from the beginning. */
882 static void vty_kill_line_from_beginning(struct vty
*vty
)
884 vty_beginning_of_line(vty
);
888 /* Delete a word before the point. */
889 static void vty_forward_kill_word(struct vty
*vty
)
891 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
892 vty_delete_char(vty
);
893 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
894 vty_delete_char(vty
);
897 /* Delete a word before the point. */
898 static void vty_backward_kill_word(struct vty
*vty
)
900 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
901 vty_delete_backward_char(vty
);
902 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
903 vty_delete_backward_char(vty
);
906 /* Transpose chars before or at the point. */
907 static void vty_transpose_chars(struct vty
*vty
)
911 /* If length is short or point is near by the beginning of line then
913 if (vty
->length
< 2 || vty
->cp
< 1)
916 /* In case of point is located at the end of the line. */
917 if (vty
->cp
== vty
->length
) {
918 c1
= vty
->buf
[vty
->cp
- 1];
919 c2
= vty
->buf
[vty
->cp
- 2];
921 vty_backward_char(vty
);
922 vty_backward_char(vty
);
923 vty_self_insert_overwrite(vty
, c1
);
924 vty_self_insert_overwrite(vty
, c2
);
926 c1
= vty
->buf
[vty
->cp
];
927 c2
= vty
->buf
[vty
->cp
- 1];
929 vty_backward_char(vty
);
930 vty_self_insert_overwrite(vty
, c1
);
931 vty_self_insert_overwrite(vty
, c2
);
935 /* Do completion at vty interface. */
936 static void vty_complete_command(struct vty
*vty
)
940 char **matched
= NULL
;
943 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
946 vline
= cmd_make_strvec(vty
->buf
);
950 /* In case of 'help \t'. */
951 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
952 vector_set(vline
, NULL
);
954 matched
= cmd_complete_command(vline
, vty
, &ret
);
956 cmd_free_strvec(vline
);
960 case CMD_ERR_AMBIGUOUS
:
961 vty_out(vty
, "%% Ambiguous command.\n");
963 vty_redraw_line(vty
);
965 case CMD_ERR_NO_MATCH
:
966 /* vty_out (vty, "%% There is no matched command.\n"); */
968 vty_redraw_line(vty
);
970 case CMD_COMPLETE_FULL_MATCH
:
972 /* 2016-11-28 equinox -- need to debug, SEGV here */
973 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
975 vty_redraw_line(vty
);
979 vty_redraw_line(vty
);
980 vty_backward_pure_word(vty
);
981 vty_insert_word_overwrite(vty
, matched
[0]);
982 vty_self_insert(vty
, ' ');
983 XFREE(MTYPE_COMPLETION
, matched
[0]);
985 case CMD_COMPLETE_MATCH
:
987 vty_redraw_line(vty
);
988 vty_backward_pure_word(vty
);
989 vty_insert_word_overwrite(vty
, matched
[0]);
990 XFREE(MTYPE_COMPLETION
, matched
[0]);
992 case CMD_COMPLETE_LIST_MATCH
:
993 for (i
= 0; matched
[i
] != NULL
; i
++) {
994 if (i
!= 0 && ((i
% 6) == 0))
996 vty_out(vty
, "%-10s ", matched
[i
]);
997 XFREE(MTYPE_COMPLETION
, matched
[i
]);
1002 vty_redraw_line(vty
);
1004 case CMD_ERR_NOTHING_TODO
:
1006 vty_redraw_line(vty
);
1011 XFREE(MTYPE_TMP
, matched
);
1014 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
1015 unsigned int desc_width
, struct cmd_token
*token
)
1018 const char *cmd
, *p
;
1023 if (desc_width
<= 0) {
1024 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1028 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1030 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1031 for (pos
= desc_width
; pos
> 0; pos
--)
1032 if (*(p
+ pos
) == ' ')
1038 memcpy(buf
, p
, pos
);
1040 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1045 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1047 XFREE(MTYPE_TMP
, buf
);
1050 /* Describe matched command function. */
1051 static void vty_describe_command(struct vty
*vty
)
1056 unsigned int i
, width
, desc_width
;
1057 struct cmd_token
*token
, *token_cr
= NULL
;
1059 vline
= cmd_make_strvec(vty
->buf
);
1061 /* In case of '> ?'. */
1062 if (vline
== NULL
) {
1063 vline
= vector_init(1);
1064 vector_set(vline
, NULL
);
1065 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1066 vector_set(vline
, NULL
);
1068 describe
= cmd_describe_command(vline
, vty
, &ret
);
1072 /* Ambiguous error. */
1074 case CMD_ERR_AMBIGUOUS
:
1075 vty_out(vty
, "%% Ambiguous command.\n");
1078 case CMD_ERR_NO_MATCH
:
1079 vty_out(vty
, "%% There is no matched command.\n");
1084 /* Get width of command string. */
1086 for (i
= 0; i
< vector_active(describe
); i
++)
1087 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1090 if (token
->text
[0] == '\0')
1093 len
= strlen(token
->text
);
1099 /* Get width of description string. */
1100 desc_width
= vty
->width
- (width
+ 6);
1102 /* Print out description. */
1103 for (i
= 0; i
< vector_active(describe
); i
++)
1104 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1105 if (token
->text
[0] == '\0')
1108 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1114 vty_out(vty
, " %-s\n", token
->text
);
1115 else if (desc_width
>= strlen(token
->desc
))
1116 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1119 vty_describe_fold(vty
, width
, desc_width
,
1122 if (IS_VARYING_TOKEN(token
->type
)) {
1123 const char *ref
= vector_slot(
1124 vline
, vector_active(vline
) - 1);
1126 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1127 cmd_variable_complete(token
, ref
, varcomps
);
1129 if (vector_active(varcomps
) > 0) {
1130 char *ac
= cmd_variable_comp2str(
1131 varcomps
, vty
->width
);
1132 vty_out(vty
, "%s\n", ac
);
1133 XFREE(MTYPE_TMP
, ac
);
1136 vector_free(varcomps
);
1140 if ((token
= token_cr
)) {
1142 vty_out(vty
, " %-s\n", token
->text
);
1143 else if (desc_width
>= strlen(token
->desc
))
1144 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1147 vty_describe_fold(vty
, width
, desc_width
, token
);
1151 cmd_free_strvec(vline
);
1153 vector_free(describe
);
1156 vty_redraw_line(vty
);
1159 static void vty_clear_buf(struct vty
*vty
)
1161 memset(vty
->buf
, 0, vty
->max
);
1164 /* ^C stop current input and do not add command line to the history. */
1165 static void vty_stop_input(struct vty
*vty
)
1167 vty
->cp
= vty
->length
= 0;
1172 vty_config_exit(vty
);
1173 vty
->node
= ENABLE_NODE
;
1178 /* Set history pointer to the latest one. */
1179 vty
->hp
= vty
->hindex
;
1182 /* Add current command line to the history buffer. */
1183 static void vty_hist_add(struct vty
*vty
)
1187 if (vty
->length
== 0)
1190 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1192 /* Ignore the same string as previous one. */
1193 if (vty
->hist
[index
])
1194 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1195 vty
->hp
= vty
->hindex
;
1199 /* Insert history entry. */
1200 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1201 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1203 /* History index rotation. */
1205 if (vty
->hindex
== VTY_MAXHIST
)
1208 vty
->hp
= vty
->hindex
;
1211 /* #define TELNET_OPTION_DEBUG */
1213 /* Get telnet window size. */
1214 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1216 #ifdef TELNET_OPTION_DEBUG
1219 for (i
= 0; i
< nbytes
; i
++) {
1222 vty_out(vty
, "IAC ");
1225 vty_out(vty
, "WILL ");
1228 vty_out(vty
, "WONT ");
1231 vty_out(vty
, "DO ");
1234 vty_out(vty
, "DONT ");
1237 vty_out(vty
, "SB ");
1240 vty_out(vty
, "SE ");
1243 vty_out(vty
, "TELOPT_ECHO \n");
1246 vty_out(vty
, "TELOPT_SGA \n");
1249 vty_out(vty
, "TELOPT_NAWS \n");
1252 vty_out(vty
, "%x ", buf
[i
]);
1258 #endif /* TELNET_OPTION_DEBUG */
1263 vty
->iac_sb_in_progress
= 1;
1266 if (!vty
->iac_sb_in_progress
)
1269 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1270 vty
->iac_sb_in_progress
= 0;
1273 switch (vty
->sb_buf
[0]) {
1275 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1278 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1280 (unsigned long)vty
->sb_len
);
1281 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1284 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1285 (unsigned long)sizeof(vty
->sb_buf
),
1286 TELNET_NAWS_SB_LEN
);
1288 vty
->width
= ((vty
->sb_buf
[1] << 8)
1290 vty
->height
= ((vty
->sb_buf
[3] << 8)
1292 #ifdef TELNET_OPTION_DEBUG
1294 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1295 vty
->width
, vty
->height
);
1300 vty
->iac_sb_in_progress
= 0;
1309 /* Execute current command line. */
1310 static int vty_execute(struct vty
*vty
)
1316 switch (vty
->node
) {
1318 case AUTH_ENABLE_NODE
:
1319 vty_auth(vty
, vty
->buf
);
1322 ret
= vty_command(vty
, vty
->buf
);
1323 if (vty
->type
== VTY_TERM
)
1328 /* Clear command line buffer. */
1329 vty
->cp
= vty
->length
= 0;
1332 if (vty
->status
!= VTY_CLOSE
)
1338 #define CONTROL(X) ((X) - '@')
1339 #define VTY_NORMAL 0
1340 #define VTY_PRE_ESCAPE 1
1341 #define VTY_ESCAPE 2
1344 /* Escape character command map. */
1345 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1349 vty_previous_line(vty
);
1355 vty_forward_char(vty
);
1358 vty_backward_char(vty
);
1364 /* Go back to normal mode. */
1365 vty
->escape
= VTY_NORMAL
;
1368 /* Quit print out to the buffer. */
1369 static void vty_buffer_reset(struct vty
*vty
)
1371 buffer_reset(vty
->obuf
);
1372 buffer_reset(vty
->lbuf
);
1374 vty_redraw_line(vty
);
1377 /* Read data via vty socket. */
1378 static void vty_read(struct event
*thread
)
1382 unsigned char buf
[VTY_READ_BUFSIZ
];
1384 struct vty
*vty
= EVENT_ARG(thread
);
1386 /* Read raw data from socket */
1387 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1389 if (ERRNO_IO_RETRY(errno
)) {
1390 vty_event(VTY_READ
, vty
);
1395 "%s: read error on vty client fd %d, closing: %s",
1396 __func__
, vty
->fd
, safe_strerror(errno
));
1397 buffer_reset(vty
->obuf
);
1398 buffer_reset(vty
->lbuf
);
1400 vty
->status
= VTY_CLOSE
;
1403 for (i
= 0; i
< nbytes
; i
++) {
1404 if (buf
[i
] == IAC
) {
1413 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1414 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1415 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1421 /* In case of telnet command */
1423 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1430 if (vty
->status
== VTY_MORE
) {
1435 vty_buffer_reset(vty
);
1443 /* Escape character. */
1444 if (vty
->escape
== VTY_ESCAPE
) {
1445 vty_escape_map(buf
[i
], vty
);
1449 /* Pre-escape status. */
1450 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1453 vty
->escape
= VTY_ESCAPE
;
1456 vty_backward_word(vty
);
1457 vty
->escape
= VTY_NORMAL
;
1460 vty_forward_word(vty
);
1461 vty
->escape
= VTY_NORMAL
;
1464 vty_forward_kill_word(vty
);
1465 vty
->escape
= VTY_NORMAL
;
1469 vty_backward_kill_word(vty
);
1470 vty
->escape
= VTY_NORMAL
;
1473 vty
->escape
= VTY_NORMAL
;
1479 if (vty
->escape
== VTY_CR
) {
1480 /* if we get CR+NL, the NL results in an extra empty
1481 * prompt line being printed without this; just drop
1482 * the NL if it immediately follows CR.
1484 vty
->escape
= VTY_NORMAL
;
1492 vty_beginning_of_line(vty
);
1495 vty_backward_char(vty
);
1498 vty_stop_input(vty
);
1501 vty_delete_char(vty
);
1504 vty_end_of_line(vty
);
1507 vty_forward_char(vty
);
1511 vty_delete_backward_char(vty
);
1520 vty_previous_line(vty
);
1523 vty_transpose_chars(vty
);
1526 vty_kill_line_from_beginning(vty
);
1529 vty_backward_kill_word(vty
);
1532 vty_end_config(vty
);
1535 vty
->escape
= VTY_CR
;
1539 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1542 if (vty
->pass_fd
!= -1) {
1543 close(vty
->pass_fd
);
1548 vty_complete_command(vty
);
1551 if (vty
->node
== AUTH_NODE
1552 || vty
->node
== AUTH_ENABLE_NODE
)
1553 vty_self_insert(vty
, buf
[i
]);
1555 vty_describe_command(vty
);
1558 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1559 vty
->escape
= VTY_ESCAPE
;
1562 vty
->escape
= VTY_PRE_ESCAPE
;
1565 if (buf
[i
] > 31 && buf
[i
] < 127)
1566 vty_self_insert(vty
, buf
[i
]);
1572 if (vty
->status
== VTY_CLOSE
)
1575 vty_event(VTY_WRITE
, vty
);
1576 vty_event(VTY_READ
, vty
);
1580 /* Flush buffer to the vty. */
1581 static void vty_flush(struct event
*thread
)
1584 buffer_status_t flushrc
;
1585 struct vty
*vty
= EVENT_ARG(thread
);
1587 /* Tempolary disable read thread. */
1588 if (vty
->lines
== 0)
1589 EVENT_OFF(vty
->t_read
);
1591 /* Function execution continue. */
1592 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1594 /* N.B. if width is 0, that means we don't know the window size. */
1595 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1596 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1597 else if (vty
->status
== VTY_MORELINE
)
1598 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1601 flushrc
= buffer_flush_window(
1602 vty
->obuf
, vty
->wfd
, vty
->width
,
1603 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1606 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1608 buffer_reset(vty
->lbuf
);
1609 buffer_reset(vty
->obuf
);
1613 if (vty
->status
== VTY_CLOSE
)
1616 vty
->status
= VTY_NORMAL
;
1617 if (vty
->lines
== 0)
1618 vty_event(VTY_READ
, vty
);
1621 case BUFFER_PENDING
:
1622 /* There is more data waiting to be written. */
1623 vty
->status
= VTY_MORE
;
1624 if (vty
->lines
== 0)
1625 vty_event(VTY_WRITE
, vty
);
1630 /* Allocate new vty struct. */
1631 struct vty
*vty_new(void)
1633 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1635 new->fd
= new->wfd
= -1;
1637 new->lbuf
= buffer_new(0);
1638 new->obuf
= buffer_new(0); /* Use default buffer size. */
1639 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1640 new->max
= VTY_BUFSIZ
;
1643 if (mgmt_lib_hndl
) {
1644 if (!mgmt_client_id_next
)
1645 mgmt_client_id_next
++;
1646 new->mgmt_client_id
= mgmt_client_id_next
++;
1647 if (mgmt_fe_create_client_session(
1648 mgmt_lib_hndl
, new->mgmt_client_id
,
1649 (uintptr_t) new) != MGMTD_SUCCESS
)
1651 "Failed to open a MGMTD Frontend session for VTY session %p!!",
1659 /* allocate and initialise vty */
1660 static struct vty
*vty_new_init(int vty_sock
)
1666 vty
->wfd
= vty_sock
;
1667 vty
->type
= VTY_TERM
;
1668 vty
->node
= AUTH_NODE
;
1673 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1676 vty
->xpath_index
= 0;
1677 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1678 vty
->private_config
= false;
1679 vty
->candidate_config
= vty_shared_candidate_config
;
1680 vty
->status
= VTY_NORMAL
;
1683 vty
->iac_sb_in_progress
= 0;
1686 vtys_add_tail(vty_sessions
, vty
);
1691 /* Create new vty structure. */
1692 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1694 char buf
[SU_ADDRSTRLEN
];
1697 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1699 /* Allocate new vty structure and set up default values. */
1700 vty
= vty_new_init(vty_sock
);
1702 /* configurable parameters not part of basic init */
1703 vty
->v_timeout
= vty_timeout_val
;
1704 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1705 if (no_password_check
) {
1707 vty
->node
= ENABLE_NODE
;
1709 vty
->node
= VIEW_NODE
;
1711 if (host
.lines
>= 0)
1712 vty
->lines
= host
.lines
;
1714 if (!no_password_check
) {
1715 /* Vty is not available if password isn't set. */
1716 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1717 vty_out(vty
, "Vty password is not set.\n");
1718 vty
->status
= VTY_CLOSE
;
1724 /* Say hello to the world. */
1726 if (!no_password_check
)
1727 vty_out(vty
, "\nUser Access Verification\n\n");
1729 /* Setting up terminal. */
1731 vty_will_suppress_go_ahead(vty
);
1733 vty_dont_linemode(vty
);
1734 vty_do_window_size(vty
);
1735 /* vty_dont_lflow_ahead (vty); */
1739 /* Add read/write thread. */
1740 vty_event(VTY_WRITE
, vty
);
1741 vty_event(VTY_READ
, vty
);
1746 /* create vty for stdio */
1747 static struct termios stdio_orig_termios
;
1748 static struct vty
*stdio_vty
= NULL
;
1749 static bool stdio_termios
= false;
1750 static void (*stdio_vty_atclose
)(int isexit
);
1752 static void vty_stdio_reset(int isexit
)
1756 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1757 stdio_termios
= false;
1761 if (stdio_vty_atclose
)
1762 stdio_vty_atclose(isexit
);
1763 stdio_vty_atclose
= NULL
;
1767 static void vty_stdio_atexit(void)
1772 void vty_stdio_suspend(void)
1777 EVENT_OFF(stdio_vty
->t_write
);
1778 EVENT_OFF(stdio_vty
->t_read
);
1779 EVENT_OFF(stdio_vty
->t_timeout
);
1782 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1783 stdio_termios
= false;
1786 void vty_stdio_resume(void)
1791 if (!tcgetattr(0, &stdio_orig_termios
)) {
1792 struct termios termios
;
1794 termios
= stdio_orig_termios
;
1795 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1796 | IGNCR
| ICRNL
| IXON
);
1797 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1798 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1799 termios
.c_cflag
|= CS8
;
1800 tcsetattr(0, TCSANOW
, &termios
);
1801 stdio_termios
= true;
1804 vty_prompt(stdio_vty
);
1806 /* Add read/write thread. */
1807 vty_event(VTY_WRITE
, stdio_vty
);
1808 vty_event(VTY_READ
, stdio_vty
);
1811 void vty_stdio_close(void)
1815 vty_close(stdio_vty
);
1818 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1822 /* refuse creating two vtys on stdio */
1826 vty
= stdio_vty
= vty_new_init(0);
1827 stdio_vty_atclose
= atclose
;
1830 /* always have stdio vty in a known _unchangeable_ state, don't want
1832 * to have any effect here to make sure scripting this works as intended
1834 vty
->node
= ENABLE_NODE
;
1836 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1842 /* Accept connection from the network. */
1843 static void vty_accept(struct event
*thread
)
1845 struct vty_serv
*vtyserv
= EVENT_ARG(thread
);
1850 int accept_sock
= vtyserv
->sock
;
1852 struct access_list
*acl
= NULL
;
1854 /* We continue hearing vty socket. */
1855 vty_event_serv(VTY_SERV
, vtyserv
);
1857 memset(&su
, 0, sizeof(union sockunion
));
1859 /* We can handle IPv4 or IPv6 socket. */
1860 vty_sock
= sockunion_accept(accept_sock
, &su
);
1862 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1863 safe_strerror(errno
));
1866 set_nonblocking(vty_sock
);
1867 set_cloexec(vty_sock
);
1869 if (!sockunion2hostprefix(&su
, &p
)) {
1871 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1876 /* VTY's accesslist apply. */
1877 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1878 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1879 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1880 zlog_info("Vty connection refused from %pSU", &su
);
1886 /* VTY's ipv6 accesslist apply. */
1887 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1888 if ((acl
= access_list_lookup(AFI_IP6
,
1889 vty_ipv6_accesslist_name
))
1890 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1891 zlog_info("Vty connection refused from %pSU", &su
);
1898 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1901 zlog_info("can't set sockopt to vty_sock : %s",
1902 safe_strerror(errno
));
1904 zlog_info("Vty connection from %pSU", &su
);
1906 vty_create(vty_sock
, &su
);
1909 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1912 struct addrinfo req
;
1913 struct addrinfo
*ainfo
;
1914 struct addrinfo
*ainfo_save
;
1916 char port_str
[BUFSIZ
];
1918 memset(&req
, 0, sizeof(req
));
1919 req
.ai_flags
= AI_PASSIVE
;
1920 req
.ai_family
= AF_UNSPEC
;
1921 req
.ai_socktype
= SOCK_STREAM
;
1922 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1923 port_str
[sizeof(port_str
) - 1] = '\0';
1925 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1928 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1936 struct vty_serv
*vtyserv
;
1938 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1941 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1942 ainfo
->ai_protocol
);
1946 sockopt_v6only(ainfo
->ai_family
, sock
);
1947 sockopt_reuseaddr(sock
);
1948 sockopt_reuseport(sock
);
1951 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1953 close(sock
); /* Avoid sd leak. */
1957 ret
= listen(sock
, 3);
1959 close(sock
); /* Avoid sd leak. */
1963 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1964 vtyserv
->sock
= sock
;
1965 vtyservs_add_tail(vty_servs
, vtyserv
);
1967 vty_event_serv(VTY_SERV
, vtyserv
);
1968 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1970 freeaddrinfo(ainfo_save
);
1974 /* For sockaddr_un. */
1977 /* VTY shell UNIX domain socket. */
1978 static void vty_serv_un(const char *path
)
1980 struct vty_serv
*vtyserv
;
1983 struct sockaddr_un serv
;
1985 struct zprivs_ids_t ids
;
1987 /* First of all, unlink existing socket */
1991 old_mask
= umask(0007);
1993 /* Make UNIX domain socket. */
1994 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1996 flog_err_sys(EC_LIB_SOCKET
,
1997 "Cannot create unix stream socket: %s",
1998 safe_strerror(errno
));
2002 /* Make server socket. */
2003 memset(&serv
, 0, sizeof(serv
));
2004 serv
.sun_family
= AF_UNIX
;
2005 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2006 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2007 len
= serv
.sun_len
= SUN_LEN(&serv
);
2009 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2010 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2014 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2016 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
2017 safe_strerror(errno
));
2018 close(sock
); /* Avoid sd leak. */
2022 ret
= listen(sock
, 5);
2024 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
2025 safe_strerror(errno
));
2026 close(sock
); /* Avoid sd leak. */
2032 zprivs_get_ids(&ids
);
2034 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2035 earlier for the case when we don't need to chown the file
2036 type casting it here to make a compare */
2037 if ((int)ids
.gid_vty
> 0) {
2038 /* set group of socket */
2039 if (chown(path
, -1, ids
.gid_vty
)) {
2040 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2041 "vty_serv_un: could chown socket, %s",
2042 safe_strerror(errno
));
2046 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
2047 vtyserv
->sock
= sock
;
2048 vtyserv
->vtysh
= true;
2049 vtyservs_add_tail(vty_servs
, vtyserv
);
2051 vty_event_serv(VTYSH_SERV
, vtyserv
);
2054 /* #define VTYSH_DEBUG 1 */
2056 static void vtysh_accept(struct event
*thread
)
2058 struct vty_serv
*vtyserv
= EVENT_ARG(thread
);
2059 int accept_sock
= vtyserv
->sock
;
2062 struct sockaddr_un client
;
2065 vty_event_serv(VTYSH_SERV
, vtyserv
);
2067 memset(&client
, 0, sizeof(client
));
2068 client_len
= sizeof(struct sockaddr_un
);
2070 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2071 (socklen_t
*)&client_len
);
2074 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2075 safe_strerror(errno
));
2079 if (set_nonblocking(sock
) < 0) {
2082 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2083 sock
, safe_strerror(errno
));
2090 printf("VTY shell accept\n");
2091 #endif /* VTYSH_DEBUG */
2096 vty
->type
= VTY_SHELL_SERV
;
2097 vty
->node
= VIEW_NODE
;
2098 vtys_add_tail(vtysh_sessions
, vty
);
2100 vty_event(VTYSH_READ
, vty
);
2103 static int vtysh_do_pass_fd(struct vty
*vty
)
2105 struct iovec iov
[1] = {
2107 .iov_base
= vty
->pass_fd_status
,
2108 .iov_len
= sizeof(vty
->pass_fd_status
),
2112 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2113 struct cmsghdr align
;
2115 struct msghdr mh
= {
2117 .msg_iovlen
= array_size(iov
),
2118 .msg_control
= u
.buf
,
2119 .msg_controllen
= sizeof(u
.buf
),
2121 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2124 memset(&u
.buf
, 0, sizeof(u
.buf
));
2125 cmh
->cmsg_level
= SOL_SOCKET
;
2126 cmh
->cmsg_type
= SCM_RIGHTS
;
2127 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2128 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2130 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2131 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2132 return BUFFER_PENDING
;
2134 close(vty
->pass_fd
);
2136 vty
->status
= VTY_NORMAL
;
2139 return BUFFER_ERROR
;
2141 /* resume accepting commands (suspended in vtysh_read) */
2142 vty_event(VTYSH_READ
, vty
);
2144 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2145 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2147 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2148 return BUFFER_PENDING
;
2150 return BUFFER_EMPTY
;
2153 static int vtysh_flush(struct vty
*vty
)
2157 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2158 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2159 ret
= vtysh_do_pass_fd(vty
);
2162 case BUFFER_PENDING
:
2163 vty_event(VTYSH_WRITE
, vty
);
2166 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2168 buffer_reset(vty
->lbuf
);
2169 buffer_reset(vty
->obuf
);
2178 void vty_pass_fd(struct vty
*vty
, int fd
)
2180 if (vty
->pass_fd
!= -1)
2181 close(vty
->pass_fd
);
2186 bool mgmt_vty_read_configs(void)
2188 char path
[PATH_MAX
];
2196 vty
->wfd
= STDERR_FILENO
;
2197 vty
->type
= VTY_FILE
;
2198 vty
->node
= CONFIG_NODE
;
2200 vty
->pending_allowed
= true;
2201 vty
->candidate_config
= vty_shared_candidate_config
;
2202 vty
->mgmt_locked_candidate_ds
= true;
2203 mgmt_candidate_ds_wr_locked
= true;
2206 for (index
= 0; index
< array_size(mgmt_daemons
); index
++) {
2207 snprintf(path
, sizeof(path
), "%s/%s.conf", frr_sysconfdir
,
2208 mgmt_daemons
[index
]);
2210 confp
= vty_open_config(path
, config_default
);
2214 zlog_info("mgmtd: reading config file: %s", path
);
2216 /* Execute configuration file */
2218 (void)config_from_file(vty
, confp
, &line_num
);
2222 snprintf(path
, sizeof(path
), "%s/mgmtd.conf", frr_sysconfdir
);
2223 confp
= vty_open_config(path
, config_default
);
2227 snprintf(path
, sizeof(path
), "%s/zebra.conf", frr_sysconfdir
);
2228 orig
= XSTRDUP(MTYPE_TMP
, host_config_get());
2230 zlog_info("mgmtd: trying backup config file: %s", path
);
2231 confp
= vty_open_config(path
, config_default
);
2233 host_config_set(path
);
2234 XFREE(MTYPE_TMP
, orig
);
2238 zlog_info("mgmtd: reading config file: %s", path
);
2241 (void)config_from_file(vty
, confp
, &line_num
);
2245 vty
->pending_allowed
= false;
2247 vty
->mgmt_locked_candidate_ds
= false;
2248 mgmt_candidate_ds_wr_locked
= false;
2253 vty_read_file_finish(vty
, NULL
);
2255 zlog_info("mgmtd: finished reading config files");
2260 static void vtysh_read(struct event
*thread
)
2266 unsigned char buf
[VTY_READ_BUFSIZ
];
2268 uint8_t header
[4] = {0, 0, 0, 0};
2270 sock
= EVENT_FD(thread
);
2271 vty
= EVENT_ARG(thread
);
2273 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2275 if (ERRNO_IO_RETRY(errno
)) {
2276 vty_event(VTYSH_READ
, vty
);
2281 "%s: read failed on vtysh client fd %d, closing: %s",
2282 __func__
, sock
, safe_strerror(errno
));
2284 buffer_reset(vty
->lbuf
);
2285 buffer_reset(vty
->obuf
);
2288 printf("close vtysh\n");
2289 #endif /* VTYSH_DEBUG */
2294 printf("line: %.*s\n", nbytes
, buf
);
2295 #endif /* VTYSH_DEBUG */
2297 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2298 /* Clear command line buffer. */
2299 vty
->cp
= vty
->length
= 0;
2301 vty_out(vty
, "%% Command is too long.\n");
2303 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2304 vty
->buf
[vty
->length
++] = *p
;
2306 /* Pass this line to parser. */
2307 ret
= vty_execute(vty
);
2308 /* Note that vty_execute clears the command buffer and resets
2309 vty->length to 0. */
2311 /* Return result. */
2313 printf("result: %d\n", ret
);
2314 printf("vtysh node: %d\n", vty
->node
);
2315 #endif /* VTYSH_DEBUG */
2317 if (vty
->pass_fd
!= -1) {
2318 memset(vty
->pass_fd_status
, 0, 4);
2319 vty
->pass_fd_status
[3] = ret
;
2320 vty
->status
= VTY_PASSFD
;
2323 vty_event(VTYSH_WRITE
, vty
);
2325 /* this introduces a "sequence point"
2326 * command output is written normally,
2327 * read processing is suspended until
2329 * then retcode + FD is written
2330 * then normal processing resumes
2332 * => skip vty_event(VTYSH_READ, vty)!
2337 /* hack for asynchronous "write integrated"
2338 * - other commands in "buf" will be ditched
2339 * - input during pending config-write is
2341 if (ret
== CMD_SUSPEND
)
2344 /* with new infra we need to stop response till
2345 * we get response through callback.
2347 if (vty
->mgmt_req_pending
)
2350 /* warning: watchfrr hardcodes this result write
2353 buffer_put(vty
->obuf
, header
, 4);
2355 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2356 /* Try to flush results; exit if a write
2363 if (vty
->status
== VTY_CLOSE
)
2366 vty_event(VTYSH_READ
, vty
);
2369 static void vtysh_write(struct event
*thread
)
2371 struct vty
*vty
= EVENT_ARG(thread
);
2378 /* Determine address family to bind. */
2379 void vty_serv_start(const char *addr
, unsigned short port
, const char *path
)
2381 /* If port is set to 0, do not listen on TCP/IP at all! */
2383 vty_serv_sock_addrinfo(addr
, port
);
2390 void vty_serv_stop(void)
2392 struct vty_serv
*vtyserv
;
2394 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
2395 EVENT_OFF(vtyserv
->t_accept
);
2396 close(vtyserv
->sock
);
2397 XFREE(MTYPE_VTY_SERV
, vtyserv
);
2400 vtyservs_fini(vty_servs
);
2401 vtyservs_init(vty_servs
);
2404 static void vty_error_delete(void *arg
)
2406 struct vty_error
*ve
= arg
;
2408 XFREE(MTYPE_TMP
, ve
);
2411 /* Close vty interface. Warning: call this only from functions that
2412 will be careful not to access the vty afterwards (since it has
2413 now been freed). This is safest from top-level functions (called
2414 directly by the thread dispatcher). */
2415 void vty_close(struct vty
*vty
)
2418 bool was_stdio
= false;
2420 vty
->status
= VTY_CLOSE
;
2422 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
2423 mgmt_fe_destroy_client_session(mgmt_lib_hndl
,
2424 vty
->mgmt_client_id
);
2425 vty
->mgmt_session_id
= 0;
2428 /* Drop out of configure / transaction if needed. */
2429 vty_config_exit(vty
);
2431 /* Cancel threads.*/
2432 EVENT_OFF(vty
->t_read
);
2433 EVENT_OFF(vty
->t_write
);
2434 EVENT_OFF(vty
->t_timeout
);
2436 if (vty
->pass_fd
!= -1) {
2437 close(vty
->pass_fd
);
2440 zlog_live_close(&vty
->live_log
);
2443 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2445 /* Free input buffer. */
2446 buffer_free(vty
->obuf
);
2447 buffer_free(vty
->lbuf
);
2449 /* Free command history. */
2450 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2451 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2455 if (vty
->fd
!= -1) {
2456 if (vty
->type
== VTY_SHELL_SERV
)
2457 vtys_del(vtysh_sessions
, vty
);
2458 else if (vty
->type
== VTY_TERM
)
2459 vtys_del(vty_sessions
, vty
);
2462 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2466 * note check is for fd > STDERR_FILENO, not fd != -1.
2467 * We never close stdin/stdout/stderr here, because we may be
2468 * running in foreground mode with logging to stdout. Also,
2469 * additionally, we'd need to replace these fds with /dev/null. */
2470 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2472 if (vty
->fd
> STDERR_FILENO
)
2474 if (vty
->fd
== STDIN_FILENO
)
2477 XFREE(MTYPE_TMP
, vty
->pending_cmds_buf
);
2478 XFREE(MTYPE_VTY
, vty
->buf
);
2481 vty
->error
->del
= vty_error_delete
;
2482 list_delete(&vty
->error
);
2486 XFREE(MTYPE_VTY
, vty
);
2492 /* When time out occur output message then close connection. */
2493 static void vty_timeout(struct event
*thread
)
2497 vty
= EVENT_ARG(thread
);
2501 buffer_reset(vty
->lbuf
);
2502 buffer_reset(vty
->obuf
);
2503 vty_out(vty
, "\nVty connection is timed out.\n");
2505 /* Close connection. */
2506 vty
->status
= VTY_CLOSE
;
2510 /* Read up configuration file from file_name. */
2511 void vty_read_file(struct nb_config
*config
, FILE *confp
)
2514 unsigned int line_num
= 0;
2517 /* vty_close won't close stderr; if some config command prints
2518 * something it'll end up there. (not ideal; it'd be better if output
2519 * from a file-load went to logging instead. Also note that if this
2520 * function is called after daemonizing, stderr will be /dev/null.)
2522 * vty->fd will be -1 from vty_new()
2524 vty
->wfd
= STDERR_FILENO
;
2525 vty
->type
= VTY_FILE
;
2526 vty
->node
= CONFIG_NODE
;
2529 vty
->candidate_config
= config
;
2531 vty
->private_config
= true;
2532 vty
->candidate_config
= nb_config_new(NULL
);
2535 /* Execute configuration file */
2536 (void)config_from_file(vty
, confp
, &line_num
);
2538 vty_read_file_finish(vty
, config
);
2541 void vty_read_file_finish(struct vty
*vty
, struct nb_config
*config
)
2543 struct vty_error
*ve
;
2544 struct listnode
*node
;
2546 /* Flush any previous errors before printing messages below */
2547 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2549 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2550 const char *message
= NULL
;
2553 switch (ve
->cmd_ret
) {
2555 message
= "Command succeeded";
2557 case CMD_ERR_NOTHING_TODO
:
2558 message
= "Nothing to do";
2560 case CMD_ERR_AMBIGUOUS
:
2561 message
= "Ambiguous command";
2563 case CMD_ERR_NO_MATCH
:
2564 message
= "No such command";
2567 message
= "Command returned Warning";
2569 case CMD_WARNING_CONFIG_FAILED
:
2570 message
= "Command returned Warning Config Failed";
2572 case CMD_ERR_INCOMPLETE
:
2573 message
= "Command returned Incomplete";
2575 case CMD_ERR_EXEED_ARGC_MAX
:
2577 "Command exceeded maximum number of Arguments";
2580 message
= "Command returned unhandled error message";
2584 nl
= strchr(ve
->error_buf
, '\n');
2587 flog_err(EC_LIB_VTY
, "%s on config line %u: %s", message
,
2588 ve
->line_num
, ve
->error_buf
);
2592 * Automatically commit the candidate configuration after
2593 * reading the configuration file.
2595 if (config
== NULL
) {
2596 struct nb_context context
= {};
2597 char errmsg
[BUFSIZ
] = {0};
2600 context
.client
= NB_CLIENT_CLI
;
2602 ret
= nb_candidate_commit(context
, vty
->candidate_config
, true,
2603 "Read configuration file", NULL
,
2604 errmsg
, sizeof(errmsg
));
2605 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2607 "%s: failed to read configuration file: %s (%s)",
2608 __func__
, nb_err_name(ret
), errmsg
);
2614 static FILE *vty_use_backup_config(const char *fullpath
)
2616 char *fullpath_sav
, *fullpath_tmp
;
2622 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2623 fullpath_sav
= malloc(fullpath_sav_sz
);
2624 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2625 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2627 sav
= open(fullpath_sav
, O_RDONLY
);
2633 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2634 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2636 /* Open file to configuration write. */
2637 tmp
= mkstemp(fullpath_tmp
);
2641 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2644 while ((c
= read(sav
, buffer
, 512)) > 0) {
2645 if (write(tmp
, buffer
, c
) <= 0)
2651 if (rename(fullpath_tmp
, fullpath
) == 0)
2652 ret
= fopen(fullpath
, "r");
2654 unlink(fullpath_tmp
);
2659 unlink(fullpath_tmp
);
2669 FILE *vty_open_config(const char *config_file
, char *config_default_dir
)
2671 char cwd
[MAXPATHLEN
];
2673 const char *fullpath
;
2676 /* If -f flag specified. */
2677 if (config_file
!= NULL
) {
2678 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2679 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2682 "%s: failure to determine Current Working Directory %d!",
2684 goto tmp_free_and_out
;
2686 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2687 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2688 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2691 fullpath
= config_file
;
2693 confp
= fopen(fullpath
, "r");
2695 if (confp
== NULL
) {
2697 EC_LIB_BACKUP_CONFIG
,
2698 "%s: failed to open configuration file %s: %s, checking backup",
2699 __func__
, fullpath
, safe_strerror(errno
));
2701 confp
= vty_use_backup_config(fullpath
);
2703 flog_warn(EC_LIB_BACKUP_CONFIG
,
2704 "using backup configuration file!");
2708 "%s: can't open configuration file [%s]",
2709 __func__
, config_file
);
2710 goto tmp_free_and_out
;
2715 host_config_set(config_default_dir
);
2719 struct stat conf_stat
;
2721 /* !!!!PLEASE LEAVE!!!!
2722 * This is NEEDED for use with vtysh -b, or else you can get
2723 * a real configuration food fight with a lot garbage in the
2724 * merged configuration file it creates coming from the per
2725 * daemon configuration files. This also allows the daemons
2726 * to start if there default configuration file is not
2727 * present or ignore them, as needed when using vtysh -b to
2728 * configure the daemons at boot - MAG
2731 /* Stat for vtysh Zebra.conf, if found startup and wait for
2732 * boot configuration
2735 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2736 ret
= stat(integrate_default
, &conf_stat
);
2738 goto tmp_free_and_out
;
2741 confp
= fopen(config_default_dir
, "r");
2742 if (confp
== NULL
) {
2745 "%s: failed to open configuration file %s: %s, checking backup",
2746 __func__
, config_default_dir
,
2747 safe_strerror(errno
));
2749 confp
= vty_use_backup_config(config_default_dir
);
2751 flog_warn(EC_LIB_BACKUP_CONFIG
,
2752 "using backup configuration file!");
2753 fullpath
= config_default_dir
;
2755 flog_err(EC_LIB_VTY
,
2756 "can't open configuration file [%s]",
2757 config_default_dir
);
2758 goto tmp_free_and_out
;
2761 fullpath
= config_default_dir
;
2764 host_config_set(fullpath
);
2767 XFREE(MTYPE_TMP
, tmp
);
2773 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2774 char *config_default_dir
)
2778 confp
= vty_open_config(config_file
, config_default_dir
);
2782 vty_read_file(config
, confp
);
2789 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2791 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2792 vty_out(vty
, "%% Configuration is locked by other client\n");
2796 if (vty_mgmt_fe_enabled()) {
2797 if (!mgmt_candidate_ds_wr_locked
) {
2798 if (vty_mgmt_send_lockds_req(vty
, MGMTD_DS_CANDIDATE
,
2800 vty_out(vty
, "Not able to lock candidate DS\n");
2805 "Candidate DS already locked by different session\n");
2809 vty
->mgmt_locked_candidate_ds
= true;
2810 mgmt_candidate_ds_wr_locked
= true;
2813 vty
->node
= CONFIG_NODE
;
2815 vty
->private_config
= private_config
;
2816 vty
->xpath_index
= 0;
2818 if (private_config
) {
2819 vty
->candidate_config
= nb_config_dup(running_config
);
2820 vty
->candidate_config_base
= nb_config_dup(running_config
);
2822 "Warning: uncommitted changes will be discarded on exit.\n\n");
2825 * NOTE: On the MGMTD daemon we point the VTY candidate DS to
2826 * the global MGMTD candidate DS. Else we point to the VTY
2827 * Shared Candidate Config.
2829 vty
->candidate_config
= vty_mgmt_candidate_config
2830 ? vty_mgmt_candidate_config
2831 : vty_shared_candidate_config
;
2832 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2833 vty
->candidate_config_base
=
2834 nb_config_dup(running_config
);
2840 void vty_config_exit(struct vty
*vty
)
2842 enum node_type node
= vty
->node
;
2843 struct cmd_node
*cnode
;
2845 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2846 * somewhere below CONFIG_NODE */
2847 while (node
&& node
!= CONFIG_NODE
) {
2848 cnode
= vector_lookup(cmdvec
, node
);
2849 node
= cnode
->parent_node
;
2851 if (node
!= CONFIG_NODE
)
2852 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2855 while (vty
->node
!= ENABLE_NODE
)
2856 /* will call vty_config_node_exit() below */
2860 int vty_config_node_exit(struct vty
*vty
)
2862 vty
->xpath_index
= 0;
2865 * If we are not reading config file and we are mgmtd FE and we are
2866 * locked then unlock.
2868 if (vty
->type
!= VTY_FILE
&& vty_mgmt_fe_enabled() &&
2869 mgmt_candidate_ds_wr_locked
&& vty
->mgmt_locked_candidate_ds
) {
2870 if (vty_mgmt_send_lockds_req(vty
, MGMTD_DS_CANDIDATE
, false) !=
2872 vty_out(vty
, "Not able to unlock candidate DS\n");
2876 vty
->mgmt_locked_candidate_ds
= false;
2877 mgmt_candidate_ds_wr_locked
= false;
2880 /* Perform any pending commits. */
2881 (void)nb_cli_pending_commit_check(vty
);
2883 /* Check if there's a pending confirmed commit. */
2884 if (vty
->t_confirmed_commit_timeout
) {
2886 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2887 nb_cli_confirmed_commit_rollback(vty
);
2888 nb_cli_confirmed_commit_clean(vty
);
2891 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2893 if (vty
->candidate_config
) {
2894 if (vty
->private_config
)
2895 nb_config_free(vty
->candidate_config
);
2896 vty
->candidate_config
= NULL
;
2898 if (vty
->candidate_config_base
) {
2899 nb_config_free(vty
->candidate_config_base
);
2900 vty
->candidate_config_base
= NULL
;
2903 vty
->config
= false;
2906 * If this is a config file and we are dropping out of config end
2909 if (vty
->type
== VTY_FILE
&& vty
->status
!= VTY_CLOSE
) {
2910 vty_out(vty
, "exit from config node while reading config file");
2911 vty
->status
= VTY_CLOSE
;
2917 /* Master of the threads. */
2918 static struct event_loop
*vty_master
;
2920 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2924 event_add_read(vty_master
, vty_accept
, vty_serv
, vty_serv
->sock
,
2925 &vty_serv
->t_accept
);
2929 event_add_read(vty_master
, vtysh_accept
, vty_serv
,
2930 vty_serv
->sock
, &vty_serv
->t_accept
);
2935 case VTY_TIMEOUT_RESET
:
2938 assert(!"vty_event_serv() called incorrectly");
2942 static void vty_event(enum vty_event event
, struct vty
*vty
)
2947 event_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2951 event_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2956 event_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2959 /* Time out treatment. */
2960 if (vty
->v_timeout
) {
2961 EVENT_OFF(vty
->t_timeout
);
2962 event_add_timer(vty_master
, vty_timeout
, vty
,
2963 vty
->v_timeout
, &vty
->t_timeout
);
2967 event_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2970 case VTY_TIMEOUT_RESET
:
2971 EVENT_OFF(vty
->t_timeout
);
2973 event_add_timer(vty_master
, vty_timeout
, vty
,
2974 vty
->v_timeout
, &vty
->t_timeout
);
2978 assert(!"vty_event() called incorrectly");
2982 DEFUN_NOSH (config_who
,
2985 "Display who is on vty\n")
2989 frr_each (vtys
, vty_sessions
, v
)
2990 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2991 v
->config
? "*" : " ", v
->fd
, v
->address
,
2992 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2996 /* Move to vty configuration mode. */
2997 DEFUN_NOSH (line_vty
,
3000 "Configure a terminal line\n"
3001 "Virtual terminal\n")
3003 vty
->node
= VTY_NODE
;
3007 /* Set time out value. */
3008 static int exec_timeout(struct vty
*vty
, const char *min_str
,
3009 const char *sec_str
)
3011 unsigned long timeout
= 0;
3013 /* min_str and sec_str are already checked by parser. So it must be
3014 all digit string. */
3016 timeout
= strtol(min_str
, NULL
, 10);
3020 timeout
+= strtol(sec_str
, NULL
, 10);
3022 vty_timeout_val
= timeout
;
3023 vty
->v_timeout
= timeout
;
3024 vty_event(VTY_TIMEOUT_RESET
, vty
);
3030 DEFUN (exec_timeout_min
,
3031 exec_timeout_min_cmd
,
3032 "exec-timeout (0-35791)",
3033 "Set timeout value\n"
3034 "Timeout value in minutes\n")
3037 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
3040 DEFUN (exec_timeout_sec
,
3041 exec_timeout_sec_cmd
,
3042 "exec-timeout (0-35791) (0-2147483)",
3043 "Set the EXEC timeout\n"
3044 "Timeout in minutes\n"
3045 "Timeout in seconds\n")
3048 int idx_number_2
= 2;
3049 return exec_timeout(vty
, argv
[idx_number
]->arg
,
3050 argv
[idx_number_2
]->arg
);
3053 DEFUN (no_exec_timeout
,
3054 no_exec_timeout_cmd
,
3057 "Set the EXEC timeout\n")
3059 return exec_timeout(vty
, NULL
, NULL
);
3062 /* Set vty access class. */
3063 DEFUN (vty_access_class
,
3064 vty_access_class_cmd
,
3065 "access-class WORD",
3066 "Filter connections based on an IP access list\n"
3070 if (vty_accesslist_name
)
3071 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3073 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
3078 /* Clear vty access class. */
3079 DEFUN (no_vty_access_class
,
3080 no_vty_access_class_cmd
,
3081 "no access-class [WORD]",
3083 "Filter connections based on an IP access list\n"
3087 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
3088 if (!vty_accesslist_name
3089 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
3090 vty_out(vty
, "Access-class is not currently applied to vty\n");
3091 return CMD_WARNING_CONFIG_FAILED
;
3094 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3096 vty_accesslist_name
= NULL
;
3101 /* Set vty access class. */
3102 DEFUN (vty_ipv6_access_class
,
3103 vty_ipv6_access_class_cmd
,
3104 "ipv6 access-class WORD",
3106 "Filter connections based on an IP access list\n"
3107 "IPv6 access list\n")
3110 if (vty_ipv6_accesslist_name
)
3111 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3113 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
3118 /* Clear vty access class. */
3119 DEFUN (no_vty_ipv6_access_class
,
3120 no_vty_ipv6_access_class_cmd
,
3121 "no ipv6 access-class [WORD]",
3124 "Filter connections based on an IP access list\n"
3125 "IPv6 access list\n")
3128 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
3130 if (!vty_ipv6_accesslist_name
3131 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
3133 "IPv6 access-class is not currently applied to vty\n");
3134 return CMD_WARNING_CONFIG_FAILED
;
3137 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3139 vty_ipv6_accesslist_name
= NULL
;
3148 "Enable password checking\n")
3150 no_password_check
= 0;
3154 DEFUN (no_vty_login
,
3158 "Enable password checking\n")
3160 no_password_check
= 1;
3164 DEFUN (service_advanced_vty
,
3165 service_advanced_vty_cmd
,
3166 "service advanced-vty",
3167 "Set up miscellaneous service\n"
3168 "Enable advanced mode vty interface\n")
3174 DEFUN (no_service_advanced_vty
,
3175 no_service_advanced_vty_cmd
,
3176 "no service advanced-vty",
3178 "Set up miscellaneous service\n"
3179 "Enable advanced mode vty interface\n")
3185 DEFUN_NOSH(terminal_monitor
,
3186 terminal_monitor_cmd
,
3187 "terminal monitor [detach]",
3188 "Set terminal line parameters\n"
3189 "Copy debug output to the current terminal line\n"
3190 "Keep logging feed open independent of VTY session\n")
3194 if (vty
->type
!= VTY_SHELL_SERV
) {
3195 vty_out(vty
, "%% not supported\n");
3200 struct zlog_live_cfg detach_log
= {};
3202 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
3203 zlog_live_disown(&detach_log
);
3205 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
3208 vty_out(vty
, "%% error opening live log: %m\n");
3212 vty_pass_fd(vty
, fd_ret
);
3216 DEFUN_NOSH(no_terminal_monitor
,
3217 no_terminal_monitor_cmd
,
3218 "no terminal monitor",
3220 "Set terminal line parameters\n"
3221 "Copy debug output to the current terminal line\n")
3223 zlog_live_close(&vty
->live_log
);
3227 DEFUN_NOSH(terminal_no_monitor
,
3228 terminal_no_monitor_cmd
,
3229 "terminal no monitor",
3230 "Set terminal line parameters\n"
3232 "Copy debug output to the current terminal line\n")
3234 return no_terminal_monitor(self
, vty
, argc
, argv
);
3238 DEFUN_NOSH (show_history
,
3242 "Display the session command history\n")
3246 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3247 if (index
== VTY_MAXHIST
) {
3252 if (vty
->hist
[index
] != NULL
)
3253 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3262 DEFPY (log_commands
,
3264 "[no] log commands",
3267 "Log all commands\n")
3270 if (vty_log_commands_perm
) {
3272 "Daemon started with permanent logging turned on for commands, ignoring\n");
3276 vty_log_commands
= false;
3278 vty_log_commands
= true;
3283 /* Display current configuration. */
3284 static int vty_config_write(struct vty
*vty
)
3286 vty_frame(vty
, "line vty\n");
3288 if (vty_accesslist_name
)
3289 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3291 if (vty_ipv6_accesslist_name
)
3292 vty_out(vty
, " ipv6 access-class %s\n",
3293 vty_ipv6_accesslist_name
);
3296 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3297 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3298 vty_timeout_val
% 60);
3301 if (no_password_check
)
3302 vty_out(vty
, " no login\n");
3304 vty_endframe(vty
, "exit\n");
3306 if (vty_log_commands
)
3307 vty_out(vty
, "log commands\n");
3309 vty_out(vty
, "!\n");
3314 static int vty_config_write(struct vty
*vty
);
3315 struct cmd_node vty_node
= {
3318 .parent_node
= CONFIG_NODE
,
3319 .prompt
= "%s(config-line)# ",
3320 .config_write
= vty_config_write
,
3323 /* Reset all VTY status. */
3324 void vty_reset(void)
3328 frr_each_safe (vtys
, vty_sessions
, vty
) {
3329 buffer_reset(vty
->lbuf
);
3330 buffer_reset(vty
->obuf
);
3331 vty
->status
= VTY_CLOSE
;
3335 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3337 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3338 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3341 static void vty_save_cwd(void)
3345 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3349 * At this point if these go wrong, more than likely
3350 * the whole world is coming down around us
3351 * Hence not worrying about it too much.
3353 if (chdir(SYSCONFDIR
)) {
3354 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3355 "Failure to chdir to %s, errno: %d",
3359 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3360 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3361 "Failure to getcwd, errno: %d", errno
);
3367 char *vty_get_cwd(void)
3372 int vty_shell(struct vty
*vty
)
3374 return vty
->type
== VTY_SHELL
? 1 : 0;
3377 int vty_shell_serv(struct vty
*vty
)
3379 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3382 void vty_init_vtysh(void)
3384 /* currently nothing to do, but likely to have future use */
3389 * These functions allow for CLI handling to be placed inside daemons; however,
3390 * currently they are only used by mgmtd, with mgmtd having each daemons CLI
3391 * functionality linked into it. This design choice was taken for efficiency.
3394 static void vty_mgmt_server_connected(uintptr_t lib_hndl
, uintptr_t usr_data
,
3397 MGMTD_FE_CLIENT_DBG("Got %sconnected %s MGMTD Frontend Server",
3398 !connected
? "dis: " : "",
3399 !connected
? "from" : "to");
3402 * We should not have any sessions for connecting or disconnecting case.
3403 * The fe client library will delete all session on disconnect before
3406 assert(mgmt_fe_client_session_count(lib_hndl
) == 0);
3408 mgmt_fe_connected
= connected
;
3410 /* Start or stop listening for vty connections */
3412 frr_vty_serv_start();
3414 frr_vty_serv_stop();
3418 * A session has successfully been created for a vty.
3420 static void vty_mgmt_session_notify(uintptr_t lib_hndl
, uintptr_t usr_data
,
3421 uint64_t client_id
, bool create
,
3422 bool success
, uintptr_t session_id
,
3423 uintptr_t session_ctx
)
3427 vty
= (struct vty
*)session_ctx
;
3430 zlog_err("%s session for client %" PRIu64
" failed!",
3431 create
? "Creating" : "Destroying", client_id
);
3435 MGMTD_FE_CLIENT_DBG("%s session for client %" PRIu64
" successfully",
3436 create
? "Created" : "Destroyed", client_id
);
3439 assert(session_id
!= 0);
3440 vty
->mgmt_session_id
= session_id
;
3442 vty
->mgmt_session_id
= 0;
3447 static void vty_mgmt_ds_lock_notified(uintptr_t lib_hndl
, uintptr_t usr_data
,
3448 uint64_t client_id
, uintptr_t session_id
,
3449 uintptr_t session_ctx
, uint64_t req_id
,
3450 bool lock_ds
, bool success
,
3451 Mgmtd__DatastoreId ds_id
,
3452 char *errmsg_if_any
)
3456 vty
= (struct vty
*)session_ctx
;
3459 zlog_err("%socking for DS %u failed, Err: '%s'",
3460 lock_ds
? "L" : "Unl", ds_id
, errmsg_if_any
);
3461 vty_out(vty
, "ERROR: %socking for DS %u failed, Err: '%s'\n",
3462 lock_ds
? "L" : "Unl", ds_id
, errmsg_if_any
);
3464 MGMTD_FE_CLIENT_DBG("%socked DS %u successfully",
3465 lock_ds
? "L" : "Unl", ds_id
);
3468 vty_mgmt_resume_response(vty
, success
);
3471 static void vty_mgmt_set_config_result_notified(
3472 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3473 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3474 bool success
, Mgmtd__DatastoreId ds_id
, char *errmsg_if_any
)
3478 vty
= (struct vty
*)session_ctx
;
3481 zlog_err("SET_CONFIG request for client 0x%" PRIx64
3482 " failed, Error: '%s'",
3483 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3484 vty_out(vty
, "ERROR: SET_CONFIG request failed, Error: %s\n",
3485 errmsg_if_any
? errmsg_if_any
: "Unknown");
3487 MGMTD_FE_CLIENT_DBG("SET_CONFIG request for client 0x%" PRIx64
3488 " req-id %" PRIu64
" was successfull",
3492 vty_mgmt_resume_response(vty
, success
);
3495 static void vty_mgmt_commit_config_result_notified(
3496 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3497 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3498 bool success
, Mgmtd__DatastoreId src_ds_id
,
3499 Mgmtd__DatastoreId dst_ds_id
, bool validate_only
, char *errmsg_if_any
)
3503 vty
= (struct vty
*)session_ctx
;
3506 zlog_err("COMMIT_CONFIG request for client 0x%" PRIx64
3507 " failed, Error: '%s'",
3508 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3509 vty_out(vty
, "ERROR: COMMIT_CONFIG request failed, Error: %s\n",
3510 errmsg_if_any
? errmsg_if_any
: "Unknown");
3512 MGMTD_FE_CLIENT_DBG(
3513 "COMMIT_CONFIG request for client 0x%" PRIx64
3514 " req-id %" PRIu64
" was successfull",
3517 vty_out(vty
, "MGMTD: %s\n", errmsg_if_any
);
3520 vty_mgmt_resume_response(vty
, success
);
3523 static enum mgmt_result
vty_mgmt_get_data_result_notified(
3524 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3525 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3526 bool success
, Mgmtd__DatastoreId ds_id
, Mgmtd__YangData
**yang_data
,
3527 size_t num_data
, int next_key
, char *errmsg_if_any
)
3532 vty
= (struct vty
*)session_ctx
;
3535 zlog_err("GET_DATA request for client 0x%" PRIx64
3536 " failed, Error: '%s'",
3537 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3538 vty_out(vty
, "ERROR: GET_DATA request failed, Error: %s\n",
3539 errmsg_if_any
? errmsg_if_any
: "Unknown");
3540 vty_mgmt_resume_response(vty
, success
);
3541 return MGMTD_INTERNAL_ERROR
;
3544 MGMTD_FE_CLIENT_DBG("GET_DATA request succeeded, client 0x%" PRIx64
3548 if (req_id
!= mgmt_last_req_id
) {
3549 mgmt_last_req_id
= req_id
;
3550 vty_out(vty
, "[\n");
3553 for (indx
= 0; indx
< num_data
; indx
++) {
3554 vty_out(vty
, " \"%s\": \"%s\"\n", yang_data
[indx
]->xpath
,
3555 yang_data
[indx
]->value
->encoded_str_val
);
3558 vty_out(vty
, "]\n");
3559 vty_mgmt_resume_response(vty
, success
);
3562 return MGMTD_SUCCESS
;
3565 static struct mgmt_fe_client_params client_params
= {
3566 .client_connect_notify
= vty_mgmt_server_connected
,
3567 .client_session_notify
= vty_mgmt_session_notify
,
3568 .lock_ds_notify
= vty_mgmt_ds_lock_notified
,
3569 .set_config_notify
= vty_mgmt_set_config_result_notified
,
3570 .commit_config_notify
= vty_mgmt_commit_config_result_notified
,
3571 .get_data_notify
= vty_mgmt_get_data_result_notified
,
3574 void vty_init_mgmt_fe(void)
3577 zlog_err("Always call vty_mgmt_init_fe() after vty_init()!!");
3581 assert(!mgmt_lib_hndl
);
3582 snprintf(client_params
.name
, sizeof(client_params
.name
), "%s-%lld",
3583 frr_get_progname(), (long long)getpid());
3584 mgmt_lib_hndl
= mgmt_fe_client_lib_init(&client_params
, vty_master
);
3585 assert(mgmt_lib_hndl
);
3588 bool vty_mgmt_fe_enabled(void)
3590 return mgmt_lib_hndl
&& mgmt_fe_connected
;
3593 bool vty_mgmt_should_process_cli_apply_changes(struct vty
*vty
)
3595 return vty
->type
!= VTY_FILE
&& vty_mgmt_fe_enabled();
3598 int vty_mgmt_send_lockds_req(struct vty
*vty
, Mgmtd__DatastoreId ds_id
,
3601 enum mgmt_result ret
;
3603 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3605 ret
= mgmt_fe_lock_ds(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3606 vty
->mgmt_req_id
, ds_id
, lock
);
3607 if (ret
!= MGMTD_SUCCESS
) {
3608 zlog_err("Failed sending %sLOCK-DS-REQ req-id %" PRIu64
,
3609 lock
? "" : "UN", vty
->mgmt_req_id
);
3610 vty_out(vty
, "Failed to send %sLOCK-DS-REQ to MGMTD!\n",
3615 vty
->mgmt_req_pending
= true;
3621 int vty_mgmt_send_config_data(struct vty
*vty
)
3623 Mgmtd__YangDataValue value
[VTY_MAXCFGCHANGES
];
3624 Mgmtd__YangData cfg_data
[VTY_MAXCFGCHANGES
];
3625 Mgmtd__YangCfgDataReq cfg_req
[VTY_MAXCFGCHANGES
];
3626 Mgmtd__YangCfgDataReq
*cfgreq
[VTY_MAXCFGCHANGES
] = {0};
3629 bool implicit_commit
= false;
3631 if (vty
->type
== VTY_FILE
) {
3633 * if this is a config file read we will not send any of the
3634 * changes until we are done reading the file and have modified
3635 * the local candidate DS.
3637 assert(vty
->mgmt_locked_candidate_ds
);
3638 /* no-one else should be sending data right now */
3639 assert(!vty
->mgmt_num_pending_setcfg
);
3644 if (mgmt_lib_hndl
&& vty
->mgmt_client_id
&& !vty
->mgmt_session_id
) {
3646 * We are connected to mgmtd but we do not yet have an
3647 * established session. this means we need to send any changes
3648 * made during this "down-time" to all backend clients when this
3649 * FE client finishes coming up.
3651 MGMTD_FE_CLIENT_DBG("skipping as no session exists");
3655 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3657 for (indx
= 0; indx
< vty
->num_cfg_changes
; indx
++) {
3658 mgmt_yang_data_init(&cfg_data
[cnt
]);
3660 if (vty
->cfg_changes
[indx
].value
) {
3661 mgmt_yang_data_value_init(&value
[cnt
]);
3662 value
[cnt
].encoded_str_val
=
3663 (char *)vty
->cfg_changes
[indx
].value
;
3664 value
[cnt
].value_case
=
3665 MGMTD__YANG_DATA_VALUE__VALUE_ENCODED_STR_VAL
;
3666 cfg_data
[cnt
].value
= &value
[cnt
];
3669 cfg_data
[cnt
].xpath
= vty
->cfg_changes
[indx
].xpath
;
3671 mgmt_yang_cfg_data_req_init(&cfg_req
[cnt
]);
3672 cfg_req
[cnt
].data
= &cfg_data
[cnt
];
3673 switch (vty
->cfg_changes
[indx
].operation
) {
3675 cfg_req
[cnt
].req_type
=
3676 MGMTD__CFG_DATA_REQ_TYPE__DELETE_DATA
;
3682 case NB_OP_PRE_VALIDATE
:
3683 case NB_OP_APPLY_FINISH
:
3684 cfg_req
[cnt
].req_type
=
3685 MGMTD__CFG_DATA_REQ_TYPE__SET_DATA
;
3687 case NB_OP_GET_ELEM
:
3688 case NB_OP_GET_NEXT
:
3689 case NB_OP_GET_KEYS
:
3690 case NB_OP_LOOKUP_ENTRY
:
3692 assert(!"Invalid type of operation");
3695 assert(!"non-enum value, invalid");
3698 cfgreq
[cnt
] = &cfg_req
[cnt
];
3703 implicit_commit
= vty_needs_implicit_commit(vty
);
3704 if (cnt
&& mgmt_fe_set_config_data(
3705 mgmt_lib_hndl
, vty
->mgmt_session_id
,
3706 vty
->mgmt_req_id
, MGMTD_DS_CANDIDATE
, cfgreq
,
3707 cnt
, implicit_commit
,
3708 MGMTD_DS_RUNNING
) != MGMTD_SUCCESS
) {
3709 zlog_err("Failed to send %d Config Xpaths to MGMTD!!",
3711 vty_out(vty
, "Failed to send SETCFG-REQ to MGMTD!\n");
3715 vty
->mgmt_req_pending
= true;
3721 int vty_mgmt_send_commit_config(struct vty
*vty
, bool validate_only
, bool abort
)
3723 enum mgmt_result ret
;
3725 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3727 ret
= mgmt_fe_commit_config_data(
3728 mgmt_lib_hndl
, vty
->mgmt_session_id
, vty
->mgmt_req_id
,
3729 MGMTD_DS_CANDIDATE
, MGMTD_DS_RUNNING
, validate_only
,
3731 if (ret
!= MGMTD_SUCCESS
) {
3732 zlog_err("Failed sending COMMIT-REQ req-id %" PRIu64
,
3734 vty_out(vty
, "Failed to send COMMIT-REQ to MGMTD!\n");
3738 vty
->mgmt_req_pending
= true;
3739 vty
->mgmt_num_pending_setcfg
= 0;
3745 int vty_mgmt_send_get_config(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
3746 const char **xpath_list
, int num_req
)
3748 enum mgmt_result ret
;
3749 Mgmtd__YangData yang_data
[VTY_MAXCFGCHANGES
];
3750 Mgmtd__YangGetDataReq get_req
[VTY_MAXCFGCHANGES
];
3751 Mgmtd__YangGetDataReq
*getreq
[VTY_MAXCFGCHANGES
];
3756 for (i
= 0; i
< num_req
; i
++) {
3757 mgmt_yang_get_data_req_init(&get_req
[i
]);
3758 mgmt_yang_data_init(&yang_data
[i
]);
3760 yang_data
->xpath
= (char *)xpath_list
[i
];
3762 get_req
[i
].data
= &yang_data
[i
];
3763 getreq
[i
] = &get_req
[i
];
3765 ret
= mgmt_fe_get_config_data(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3766 vty
->mgmt_req_id
, datastore
, getreq
,
3769 if (ret
!= MGMTD_SUCCESS
) {
3771 "Failed to send GET-CONFIG to MGMTD for req-id %" PRIu64
3774 vty_out(vty
, "Failed to send GET-CONFIG to MGMTD!\n");
3778 vty
->mgmt_req_pending
= true;
3783 int vty_mgmt_send_get_data(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
3784 const char **xpath_list
, int num_req
)
3786 enum mgmt_result ret
;
3787 Mgmtd__YangData yang_data
[VTY_MAXCFGCHANGES
];
3788 Mgmtd__YangGetDataReq get_req
[VTY_MAXCFGCHANGES
];
3789 Mgmtd__YangGetDataReq
*getreq
[VTY_MAXCFGCHANGES
];
3794 for (i
= 0; i
< num_req
; i
++) {
3795 mgmt_yang_get_data_req_init(&get_req
[i
]);
3796 mgmt_yang_data_init(&yang_data
[i
]);
3798 yang_data
->xpath
= (char *)xpath_list
[i
];
3800 get_req
[i
].data
= &yang_data
[i
];
3801 getreq
[i
] = &get_req
[i
];
3803 ret
= mgmt_fe_get_data(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3804 vty
->mgmt_req_id
, datastore
, getreq
, num_req
);
3806 if (ret
!= MGMTD_SUCCESS
) {
3807 zlog_err("Failed to send GET-DATA to MGMTD for req-id %" PRIu64
3810 vty_out(vty
, "Failed to send GET-DATA to MGMTD!\n");
3814 vty
->mgmt_req_pending
= true;
3819 /* Install vty's own commands like `who' command. */
3820 void vty_init(struct event_loop
*master_thread
, bool do_command_logging
)
3822 /* For further configuration read, preserve current directory. */
3825 vty_master
= master_thread
;
3827 atexit(vty_stdio_atexit
);
3829 /* Install bgp top node. */
3830 install_node(&vty_node
);
3832 install_element(VIEW_NODE
, &config_who_cmd
);
3833 install_element(VIEW_NODE
, &show_history_cmd
);
3834 install_element(CONFIG_NODE
, &line_vty_cmd
);
3835 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3836 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3837 install_element(CONFIG_NODE
, &show_history_cmd
);
3838 install_element(CONFIG_NODE
, &log_commands_cmd
);
3840 if (do_command_logging
) {
3841 vty_log_commands
= true;
3842 vty_log_commands_perm
= true;
3845 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3846 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3847 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3849 install_default(VTY_NODE
);
3850 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3851 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3852 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3853 install_element(VTY_NODE
, &vty_access_class_cmd
);
3854 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3855 install_element(VTY_NODE
, &vty_login_cmd
);
3856 install_element(VTY_NODE
, &no_vty_login_cmd
);
3857 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3858 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3861 void vty_terminate(void)
3865 if (mgmt_lib_hndl
) {
3866 mgmt_fe_client_lib_destroy();
3870 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3874 /* default state of vty_sessions is initialized & empty. */
3875 vtys_fini(vty_sessions
);
3876 vtys_init(vty_sessions
);
3878 /* vty_reset() doesn't close vtysh sessions */
3879 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3880 buffer_reset(vty
->lbuf
);
3881 buffer_reset(vty
->obuf
);
3882 vty
->status
= VTY_CLOSE
;
3886 vtys_fini(vtysh_sessions
);
3887 vtys_init(vtysh_sessions
);