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 */
28 #include "sockunion.h"
38 #include "lib_errors.h"
39 #include "northbound_cli.h"
43 #include <arpa/telnet.h>
46 #include "lib/vty_clippy.c"
48 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY");
49 DEFINE_MTYPE_STATIC(LIB
, VTY_SERV
, "VTY server");
50 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer");
51 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history");
53 DECLARE_DLIST(vtys
, struct vty
, itm
);
68 struct nb_config
*vty_mgmt_candidate_config
;
70 static uintptr_t mgmt_lib_hndl
;
71 static bool mgmt_fe_connected
;
72 static bool mgmt_candidate_ds_wr_locked
;
73 static uint64_t mgmt_client_id_next
;
74 static uint64_t mgmt_last_req_id
= UINT64_MAX
;
76 static bool vty_debug
;
77 #define VTY_DBG(fmt, ...) \
80 zlog_debug(fmt, ##__VA_ARGS__); \
83 PREDECL_DLIST(vtyservs
);
86 struct vtyservs_item itm
;
91 struct event
*t_accept
;
94 DECLARE_DLIST(vtyservs
, struct vty_serv
, itm
);
96 static void vty_event_serv(enum vty_event event
, struct vty_serv
*);
97 static void vty_event(enum vty_event
, struct vty
*);
98 static int vtysh_flush(struct vty
*vty
);
100 /* Extern host structure from command.c */
101 extern struct host host
;
103 /* active listeners */
104 static struct vtyservs_head vty_servs
[1] = {INIT_DLIST(vty_servs
[0])};
106 /* active connections */
107 static struct vtys_head vty_sessions
[1] = {INIT_DLIST(vty_sessions
[0])};
108 static struct vtys_head vtysh_sessions
[1] = {INIT_DLIST(vtysh_sessions
[0])};
110 /* Vty timeout value. */
111 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
113 /* Vty access-class command */
114 static char *vty_accesslist_name
= NULL
;
116 /* Vty access-calss for IPv6. */
117 static char *vty_ipv6_accesslist_name
= NULL
;
119 /* Current directory. */
120 static char vty_cwd
[MAXPATHLEN
];
122 /* Login password check. */
123 static int no_password_check
= 0;
125 /* Integrated configuration file path */
126 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
128 bool vty_log_commands
;
129 static bool vty_log_commands_perm
;
131 void vty_mgmt_resume_response(struct vty
*vty
, bool success
)
133 uint8_t header
[4] = {0, 0, 0, 0};
134 int ret
= CMD_SUCCESS
;
136 if (!vty
->mgmt_req_pending
) {
138 "vty response called without setting mgmt_req_pending");
143 ret
= CMD_WARNING_CONFIG_FAILED
;
145 vty
->mgmt_req_pending
= false;
147 buffer_put(vty
->obuf
, header
, 4);
149 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
150 /* Try to flush results; exit if a write
155 if (vty
->status
== VTY_CLOSE
)
158 vty_event(VTYSH_READ
, vty
);
161 void vty_frame(struct vty
*vty
, const char *format
, ...)
165 va_start(args
, format
);
166 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
167 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
168 vty
->frame_pos
= strlen(vty
->frame
);
172 void vty_endframe(struct vty
*vty
, const char *endtext
)
174 if (vty
->frame_pos
== 0 && endtext
)
175 vty_out(vty
, "%s", endtext
);
179 bool vty_set_include(struct vty
*vty
, const char *regexp
)
187 regfree(&vty
->include
);
193 errcode
= regcomp(&vty
->include
, regexp
,
194 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
197 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
198 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
206 /* VTY standard output function. */
207 int vty_out(struct vty
*vty
, const char *format
, ...)
214 /* format string may contain %m, keep errno intact for printfrr */
215 int saved_errno
= errno
;
217 if (vty
->frame_pos
) {
219 vty_out(vty
, "%s", vty
->frame
);
222 va_start(args
, format
);
224 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
231 vector lines
= frrstr_split_vec(p
, "\n");
233 /* Place first value in the cache */
234 char *firstline
= vector_slot(lines
, 0);
235 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
237 /* If our split returned more than one entry, time to filter */
238 if (vector_active(lines
) > 1) {
240 * returned string is MTYPE_TMP so it matches the MTYPE
241 * of everything else in the vector
243 char *bstr
= buffer_getstr(vty
->lbuf
);
244 buffer_reset(vty
->lbuf
);
245 XFREE(MTYPE_TMP
, lines
->index
[0]);
246 vector_set_index(lines
, 0, bstr
);
247 frrstr_filter_vec(lines
, &vty
->include
);
248 vector_compact(lines
);
250 * Consider the string "foo\n". If the regex is an empty string
251 * and the line ended with a newline, then the vector will look
257 * If the regex isn't empty, the vector will look like:
261 * In this case we'd like to preserve the newline, so we add
262 * the empty string [1] as in the first example.
264 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
265 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
266 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
268 filtered
= frrstr_join_vec(lines
, "\n");
274 frrstr_strvec_free(lines
);
285 /* print with crlf replacement */
286 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
291 fprintf(vty
->of
, "%s", filtered
);
293 } else if (vty
->of_saved
) {
294 fprintf(vty
->of_saved
, "%s", filtered
);
295 fflush(vty
->of_saved
);
301 /* print without crlf replacement */
302 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
308 if (vty
->filter
&& filtered
)
309 XFREE(MTYPE_TMP
, filtered
);
311 /* If p is not different with buf, it is allocated buffer. */
313 XFREE(MTYPE_VTY_OUT_BUF
, p
);
318 static int vty_json_helper(struct vty
*vty
, struct json_object
*json
,
326 text
= json_object_to_json_string_ext(
328 vty_out(vty
, "%s\n", text
);
329 json_object_free(json
);
334 int vty_json(struct vty
*vty
, struct json_object
*json
)
336 return vty_json_helper(vty
, json
,
337 JSON_C_TO_STRING_PRETTY
|
338 JSON_C_TO_STRING_NOSLASHESCAPE
);
341 int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
)
343 return vty_json_helper(vty
, json
, JSON_C_TO_STRING_NOSLASHESCAPE
);
346 void vty_json_empty(struct vty
*vty
)
348 json_object
*json
= json_object_new_object();
353 /* Output current time to the vty. */
354 void vty_time_print(struct vty
*vty
, int cr
)
356 char buf
[FRR_TIMESTAMP_LEN
];
358 if (frr_timestamp(0, buf
, sizeof(buf
)) == 0) {
359 zlog_info("frr_timestamp error");
363 vty_out(vty
, "%s\n", buf
);
365 vty_out(vty
, "%s ", buf
);
370 /* Say hello to vty interface. */
371 void vty_hello(struct vty
*vty
)
377 f
= fopen(host
.motdfile
, "r");
379 while (fgets(buf
, sizeof(buf
), f
)) {
381 /* work backwards to ignore trailling isspace()
383 for (s
= buf
+ strlen(buf
);
384 (s
> buf
) && isspace((unsigned char)s
[-1]);
388 vty_out(vty
, "%s\n", buf
);
392 vty_out(vty
, "MOTD file not found\n");
393 } else if (host
.motd
)
394 vty_out(vty
, "%s", host
.motd
);
397 #pragma GCC diagnostic push
398 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
399 /* prompt formatting has a %s in the cmd_node prompt string.
401 * Also for some reason GCC emits the warning on the end of the function
402 * (optimization maybe?) rather than on the vty_out line, so this pragma
403 * wraps the entire function rather than just the vty_out line.
406 /* Put out prompt and wait input from user. */
407 static void vty_prompt(struct vty
*vty
)
409 if (vty
->type
== VTY_TERM
) {
410 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
413 #pragma GCC diagnostic pop
415 /* Send WILL TELOPT_ECHO to remote server. */
416 static void vty_will_echo(struct vty
*vty
)
418 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
419 vty_out(vty
, "%s", cmd
);
422 /* Make suppress Go-Ahead telnet option. */
423 static void vty_will_suppress_go_ahead(struct vty
*vty
)
425 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
426 vty_out(vty
, "%s", cmd
);
429 /* Make don't use linemode over telnet. */
430 static void vty_dont_linemode(struct vty
*vty
)
432 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
433 vty_out(vty
, "%s", cmd
);
436 /* Use window size. */
437 static void vty_do_window_size(struct vty
*vty
)
439 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
440 vty_out(vty
, "%s", cmd
);
443 /* Authentication of vty */
444 static void vty_auth(struct vty
*vty
, char *buf
)
447 enum node_type next_node
= 0;
453 passwd
= host
.password_encrypt
;
455 passwd
= host
.password
;
457 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
459 next_node
= VIEW_NODE
;
461 case AUTH_ENABLE_NODE
:
463 passwd
= host
.enable_encrypt
;
465 passwd
= host
.enable
;
466 next_node
= ENABLE_NODE
;
472 fail
= strcmp(crypt(buf
, passwd
), passwd
);
474 fail
= strcmp(buf
, passwd
);
480 vty
->node
= next_node
; /* Success ! */
483 if (vty
->fail
>= 3) {
484 if (vty
->node
== AUTH_NODE
) {
486 "%% Bad passwords, too many failures!\n");
487 vty
->status
= VTY_CLOSE
;
489 /* AUTH_ENABLE_NODE */
492 "%% Bad enable passwords, too many failures!\n");
493 vty
->status
= VTY_CLOSE
;
499 /* Command execution over the vty interface. */
500 static int vty_command(struct vty
*vty
, char *buf
)
503 const char *protocolname
;
509 * Log non empty command lines
511 if (vty_log_commands
&&
512 strncmp(buf
, "echo PING", strlen("echo PING")) != 0)
515 /* Skip white spaces. */
516 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
519 if (cp
!= NULL
&& *cp
!= '\0') {
520 char vty_str
[VTY_BUFSIZ
];
521 char prompt_str
[VTY_BUFSIZ
];
523 /* format the base vty info */
524 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
527 /* format the prompt */
528 #pragma GCC diagnostic push
529 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
530 /* prompt formatting has a %s in the cmd_node prompt string */
531 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
533 #pragma GCC diagnostic pop
535 /* now log the command */
536 zlog_notice("%s%s", prompt_str
, buf
);
541 unsigned long walltime
, cputime
;
543 /* cmd_execute() may change cputime_enabled if we're executing the
544 * "service cputime-stats" command, which can result in nonsensical
545 * and very confusing warnings
547 bool cputime_enabled_here
= cputime_enabled
;
551 ret
= cmd_execute(vty
, buf
, NULL
, 0);
555 walltime
= event_consumed_time(&after
, &before
, &cputime
);
557 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
558 && cputime
> cputime_threshold
)
559 /* Warn about CPU hog that must be fixed. */
560 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
561 "CPU HOG: command took %lums (cpu time %lums): %s",
562 walltime
/ 1000, cputime
/ 1000, buf
);
563 else if (walltime_threshold
&& walltime
> walltime_threshold
)
564 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
565 "STARVATION: command took %lums (cpu time %lums): %s",
566 walltime
/ 1000, cputime
/ 1000, buf
);
568 /* Get the name of the protocol if any */
569 protocolname
= frr_protoname
;
571 if (ret
!= CMD_SUCCESS
)
574 if (vty
->type
== VTY_FILE
)
575 vty_out(vty
, "Warning...\n");
577 case CMD_ERR_AMBIGUOUS
:
578 vty_out(vty
, "%% Ambiguous command.\n");
580 case CMD_ERR_NO_MATCH
:
581 vty_out(vty
, "%% [%s] Unknown command: %s\n",
584 case CMD_ERR_INCOMPLETE
:
585 vty_out(vty
, "%% Command incomplete.\n");
592 static const char telnet_backward_char
= 0x08;
593 static const char telnet_space_char
= ' ';
595 /* Basic function to write buffer to vty. */
596 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
598 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
601 /* Should we do buffering here ? And make vty_flush (vty) ? */
602 buffer_put(vty
->obuf
, buf
, nbytes
);
605 /* Basic function to insert character into vty. */
606 static void vty_self_insert(struct vty
*vty
, char c
)
611 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
614 length
= vty
->length
- vty
->cp
;
615 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
616 vty
->buf
[vty
->cp
] = c
;
618 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
619 for (i
= 0; i
< length
; i
++)
620 vty_write(vty
, &telnet_backward_char
, 1);
625 vty
->buf
[vty
->length
] = '\0';
628 /* Self insert character 'c' in overwrite mode. */
629 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
631 if (vty
->cp
== vty
->length
) {
632 vty_self_insert(vty
, c
);
636 vty
->buf
[vty
->cp
++] = c
;
637 vty_write(vty
, &c
, 1);
641 * Insert a string into vty->buf at the current cursor position.
643 * If the resultant string would be larger than VTY_BUFSIZ it is
646 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
648 if (vty
->cp
== VTY_BUFSIZ
)
651 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
652 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
654 vty
->length
= MAX(vty
->cp
, vty
->length
);
655 vty
->buf
[vty
->length
] = '\0';
656 vty_write(vty
, str
, nwrite
);
659 /* Forward character. */
660 static void vty_forward_char(struct vty
*vty
)
662 if (vty
->cp
< vty
->length
) {
663 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
668 /* Backward character. */
669 static void vty_backward_char(struct vty
*vty
)
673 vty_write(vty
, &telnet_backward_char
, 1);
677 /* Move to the beginning of the line. */
678 static void vty_beginning_of_line(struct vty
*vty
)
681 vty_backward_char(vty
);
684 /* Move to the end of the line. */
685 static void vty_end_of_line(struct vty
*vty
)
687 while (vty
->cp
< vty
->length
)
688 vty_forward_char(vty
);
691 static void vty_kill_line_from_beginning(struct vty
*);
692 static void vty_redraw_line(struct vty
*);
694 /* Print command line history. This function is called from
695 vty_next_line and vty_previous_line. */
696 static void vty_history_print(struct vty
*vty
)
700 vty_kill_line_from_beginning(vty
);
702 /* Get previous line from history buffer */
703 length
= strlen(vty
->hist
[vty
->hp
]);
704 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
705 vty
->cp
= vty
->length
= length
;
706 vty
->buf
[vty
->length
] = '\0';
708 /* Redraw current line */
709 vty_redraw_line(vty
);
712 /* Show next command line history. */
713 static void vty_next_line(struct vty
*vty
)
717 if (vty
->hp
== vty
->hindex
)
720 /* Try is there history exist or not. */
722 if (try_index
== (VTY_MAXHIST
- 1))
727 /* If there is not history return. */
728 if (vty
->hist
[try_index
] == NULL
)
733 vty_history_print(vty
);
736 /* Show previous command line history. */
737 static void vty_previous_line(struct vty
*vty
)
743 try_index
= VTY_MAXHIST
- 1;
747 if (vty
->hist
[try_index
] == NULL
)
752 vty_history_print(vty
);
755 /* This function redraw all of the command line character. */
756 static void vty_redraw_line(struct vty
*vty
)
758 vty_write(vty
, vty
->buf
, vty
->length
);
759 vty
->cp
= vty
->length
;
763 static void vty_forward_word(struct vty
*vty
)
765 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
766 vty_forward_char(vty
);
768 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
769 vty_forward_char(vty
);
772 /* Backward word without skipping training space. */
773 static void vty_backward_pure_word(struct vty
*vty
)
775 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
776 vty_backward_char(vty
);
780 static void vty_backward_word(struct vty
*vty
)
782 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
783 vty_backward_char(vty
);
785 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
786 vty_backward_char(vty
);
789 /* When '^D' is typed at the beginning of the line we move to the down
791 static void vty_down_level(struct vty
*vty
)
799 /* When '^Z' is received from vty, move down to the enable mode. */
800 static void vty_end_config(struct vty
*vty
)
805 vty_config_exit(vty
);
806 vty
->node
= ENABLE_NODE
;
813 /* Delete a character at the current point. */
814 static void vty_delete_char(struct vty
*vty
)
819 if (vty
->length
== 0) {
824 if (vty
->cp
== vty
->length
)
825 return; /* completion need here? */
827 size
= vty
->length
- vty
->cp
;
830 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
831 vty
->buf
[vty
->length
] = '\0';
833 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
836 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
837 vty_write(vty
, &telnet_space_char
, 1);
839 for (i
= 0; i
< size
; i
++)
840 vty_write(vty
, &telnet_backward_char
, 1);
843 /* Delete a character before the point. */
844 static void vty_delete_backward_char(struct vty
*vty
)
849 vty_backward_char(vty
);
850 vty_delete_char(vty
);
853 /* Kill rest of line from current point. */
854 static void vty_kill_line(struct vty
*vty
)
859 size
= vty
->length
- vty
->cp
;
864 for (i
= 0; i
< size
; i
++)
865 vty_write(vty
, &telnet_space_char
, 1);
866 for (i
= 0; i
< size
; i
++)
867 vty_write(vty
, &telnet_backward_char
, 1);
869 memset(&vty
->buf
[vty
->cp
], 0, size
);
870 vty
->length
= vty
->cp
;
873 /* Kill line from the beginning. */
874 static void vty_kill_line_from_beginning(struct vty
*vty
)
876 vty_beginning_of_line(vty
);
880 /* Delete a word before the point. */
881 static void vty_forward_kill_word(struct vty
*vty
)
883 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
884 vty_delete_char(vty
);
885 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
886 vty_delete_char(vty
);
889 /* Delete a word before the point. */
890 static void vty_backward_kill_word(struct vty
*vty
)
892 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
893 vty_delete_backward_char(vty
);
894 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
895 vty_delete_backward_char(vty
);
898 /* Transpose chars before or at the point. */
899 static void vty_transpose_chars(struct vty
*vty
)
903 /* If length is short or point is near by the beginning of line then
905 if (vty
->length
< 2 || vty
->cp
< 1)
908 /* In case of point is located at the end of the line. */
909 if (vty
->cp
== vty
->length
) {
910 c1
= vty
->buf
[vty
->cp
- 1];
911 c2
= vty
->buf
[vty
->cp
- 2];
913 vty_backward_char(vty
);
914 vty_backward_char(vty
);
915 vty_self_insert_overwrite(vty
, c1
);
916 vty_self_insert_overwrite(vty
, c2
);
918 c1
= vty
->buf
[vty
->cp
];
919 c2
= vty
->buf
[vty
->cp
- 1];
921 vty_backward_char(vty
);
922 vty_self_insert_overwrite(vty
, c1
);
923 vty_self_insert_overwrite(vty
, c2
);
927 /* Do completion at vty interface. */
928 static void vty_complete_command(struct vty
*vty
)
932 char **matched
= NULL
;
935 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
938 vline
= cmd_make_strvec(vty
->buf
);
942 /* In case of 'help \t'. */
943 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
944 vector_set(vline
, NULL
);
946 matched
= cmd_complete_command(vline
, vty
, &ret
);
948 cmd_free_strvec(vline
);
952 case CMD_ERR_AMBIGUOUS
:
953 vty_out(vty
, "%% Ambiguous command.\n");
955 vty_redraw_line(vty
);
957 case CMD_ERR_NO_MATCH
:
958 /* vty_out (vty, "%% There is no matched command.\n"); */
960 vty_redraw_line(vty
);
962 case CMD_COMPLETE_FULL_MATCH
:
964 /* 2016-11-28 equinox -- need to debug, SEGV here */
965 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
967 vty_redraw_line(vty
);
971 vty_redraw_line(vty
);
972 vty_backward_pure_word(vty
);
973 vty_insert_word_overwrite(vty
, matched
[0]);
974 vty_self_insert(vty
, ' ');
975 XFREE(MTYPE_COMPLETION
, matched
[0]);
977 case CMD_COMPLETE_MATCH
:
979 vty_redraw_line(vty
);
980 vty_backward_pure_word(vty
);
981 vty_insert_word_overwrite(vty
, matched
[0]);
982 XFREE(MTYPE_COMPLETION
, matched
[0]);
984 case CMD_COMPLETE_LIST_MATCH
:
985 for (i
= 0; matched
[i
] != NULL
; i
++) {
986 if (i
!= 0 && ((i
% 6) == 0))
988 vty_out(vty
, "%-10s ", matched
[i
]);
989 XFREE(MTYPE_COMPLETION
, matched
[i
]);
994 vty_redraw_line(vty
);
996 case CMD_ERR_NOTHING_TODO
:
998 vty_redraw_line(vty
);
1003 XFREE(MTYPE_TMP
, matched
);
1006 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
1007 unsigned int desc_width
, struct cmd_token
*token
)
1010 const char *cmd
, *p
;
1015 if (desc_width
<= 0) {
1016 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1020 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1022 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1023 for (pos
= desc_width
; pos
> 0; pos
--)
1024 if (*(p
+ pos
) == ' ')
1030 memcpy(buf
, p
, pos
);
1032 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1037 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1039 XFREE(MTYPE_TMP
, buf
);
1042 /* Describe matched command function. */
1043 static void vty_describe_command(struct vty
*vty
)
1048 unsigned int i
, width
, desc_width
;
1049 struct cmd_token
*token
, *token_cr
= NULL
;
1051 vline
= cmd_make_strvec(vty
->buf
);
1053 /* In case of '> ?'. */
1054 if (vline
== NULL
) {
1055 vline
= vector_init(1);
1056 vector_set(vline
, NULL
);
1057 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1058 vector_set(vline
, NULL
);
1060 describe
= cmd_describe_command(vline
, vty
, &ret
);
1064 /* Ambiguous error. */
1066 case CMD_ERR_AMBIGUOUS
:
1067 vty_out(vty
, "%% Ambiguous command.\n");
1070 case CMD_ERR_NO_MATCH
:
1071 vty_out(vty
, "%% There is no matched command.\n");
1076 /* Get width of command string. */
1078 for (i
= 0; i
< vector_active(describe
); i
++)
1079 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1082 if (token
->text
[0] == '\0')
1085 len
= strlen(token
->text
);
1091 /* Get width of description string. */
1092 desc_width
= vty
->width
- (width
+ 6);
1094 /* Print out description. */
1095 for (i
= 0; i
< vector_active(describe
); i
++)
1096 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1097 if (token
->text
[0] == '\0')
1100 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1106 vty_out(vty
, " %-s\n", token
->text
);
1107 else if (desc_width
>= strlen(token
->desc
))
1108 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1111 vty_describe_fold(vty
, width
, desc_width
,
1114 if (IS_VARYING_TOKEN(token
->type
)) {
1115 const char *ref
= vector_slot(
1116 vline
, vector_active(vline
) - 1);
1118 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1119 cmd_variable_complete(token
, ref
, varcomps
);
1121 if (vector_active(varcomps
) > 0) {
1122 char *ac
= cmd_variable_comp2str(
1123 varcomps
, vty
->width
);
1124 vty_out(vty
, "%s\n", ac
);
1125 XFREE(MTYPE_TMP
, ac
);
1128 vector_free(varcomps
);
1132 if ((token
= token_cr
)) {
1134 vty_out(vty
, " %-s\n", token
->text
);
1135 else if (desc_width
>= strlen(token
->desc
))
1136 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1139 vty_describe_fold(vty
, width
, desc_width
, token
);
1143 cmd_free_strvec(vline
);
1145 vector_free(describe
);
1148 vty_redraw_line(vty
);
1151 static void vty_clear_buf(struct vty
*vty
)
1153 memset(vty
->buf
, 0, vty
->max
);
1156 /* ^C stop current input and do not add command line to the history. */
1157 static void vty_stop_input(struct vty
*vty
)
1159 vty
->cp
= vty
->length
= 0;
1164 vty_config_exit(vty
);
1165 vty
->node
= ENABLE_NODE
;
1170 /* Set history pointer to the latest one. */
1171 vty
->hp
= vty
->hindex
;
1174 /* Add current command line to the history buffer. */
1175 static void vty_hist_add(struct vty
*vty
)
1179 if (vty
->length
== 0)
1182 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1184 /* Ignore the same string as previous one. */
1185 if (vty
->hist
[index
])
1186 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1187 vty
->hp
= vty
->hindex
;
1191 /* Insert history entry. */
1192 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1193 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1195 /* History index rotation. */
1197 if (vty
->hindex
== VTY_MAXHIST
)
1200 vty
->hp
= vty
->hindex
;
1203 /* #define TELNET_OPTION_DEBUG */
1205 /* Get telnet window size. */
1206 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1208 #ifdef TELNET_OPTION_DEBUG
1211 for (i
= 0; i
< nbytes
; i
++) {
1214 vty_out(vty
, "IAC ");
1217 vty_out(vty
, "WILL ");
1220 vty_out(vty
, "WONT ");
1223 vty_out(vty
, "DO ");
1226 vty_out(vty
, "DONT ");
1229 vty_out(vty
, "SB ");
1232 vty_out(vty
, "SE ");
1235 vty_out(vty
, "TELOPT_ECHO \n");
1238 vty_out(vty
, "TELOPT_SGA \n");
1241 vty_out(vty
, "TELOPT_NAWS \n");
1244 vty_out(vty
, "%x ", buf
[i
]);
1250 #endif /* TELNET_OPTION_DEBUG */
1255 vty
->iac_sb_in_progress
= 1;
1258 if (!vty
->iac_sb_in_progress
)
1261 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1262 vty
->iac_sb_in_progress
= 0;
1265 switch (vty
->sb_buf
[0]) {
1267 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1270 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1272 (unsigned long)vty
->sb_len
);
1273 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1276 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1277 (unsigned long)sizeof(vty
->sb_buf
),
1278 TELNET_NAWS_SB_LEN
);
1280 vty
->width
= ((vty
->sb_buf
[1] << 8)
1282 vty
->height
= ((vty
->sb_buf
[3] << 8)
1284 #ifdef TELNET_OPTION_DEBUG
1286 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1287 vty
->width
, vty
->height
);
1292 vty
->iac_sb_in_progress
= 0;
1301 /* Execute current command line. */
1302 static int vty_execute(struct vty
*vty
)
1308 switch (vty
->node
) {
1310 case AUTH_ENABLE_NODE
:
1311 vty_auth(vty
, vty
->buf
);
1314 ret
= vty_command(vty
, vty
->buf
);
1315 if (vty
->type
== VTY_TERM
)
1320 /* Clear command line buffer. */
1321 vty
->cp
= vty
->length
= 0;
1324 if (vty
->status
!= VTY_CLOSE
)
1330 #define CONTROL(X) ((X) - '@')
1331 #define VTY_NORMAL 0
1332 #define VTY_PRE_ESCAPE 1
1333 #define VTY_ESCAPE 2
1336 /* Escape character command map. */
1337 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1341 vty_previous_line(vty
);
1347 vty_forward_char(vty
);
1350 vty_backward_char(vty
);
1356 /* Go back to normal mode. */
1357 vty
->escape
= VTY_NORMAL
;
1360 /* Quit print out to the buffer. */
1361 static void vty_buffer_reset(struct vty
*vty
)
1363 buffer_reset(vty
->obuf
);
1364 buffer_reset(vty
->lbuf
);
1366 vty_redraw_line(vty
);
1369 /* Read data via vty socket. */
1370 static void vty_read(struct event
*thread
)
1374 unsigned char buf
[VTY_READ_BUFSIZ
];
1376 struct vty
*vty
= EVENT_ARG(thread
);
1378 /* Read raw data from socket */
1379 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1381 if (ERRNO_IO_RETRY(errno
)) {
1382 vty_event(VTY_READ
, vty
);
1387 "%s: read error on vty client fd %d, closing: %s",
1388 __func__
, vty
->fd
, safe_strerror(errno
));
1389 buffer_reset(vty
->obuf
);
1390 buffer_reset(vty
->lbuf
);
1392 vty
->status
= VTY_CLOSE
;
1395 for (i
= 0; i
< nbytes
; i
++) {
1396 if (buf
[i
] == IAC
) {
1405 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1406 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1407 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1413 /* In case of telnet command */
1415 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1422 if (vty
->status
== VTY_MORE
) {
1427 vty_buffer_reset(vty
);
1435 /* Escape character. */
1436 if (vty
->escape
== VTY_ESCAPE
) {
1437 vty_escape_map(buf
[i
], vty
);
1441 /* Pre-escape status. */
1442 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1445 vty
->escape
= VTY_ESCAPE
;
1448 vty_backward_word(vty
);
1449 vty
->escape
= VTY_NORMAL
;
1452 vty_forward_word(vty
);
1453 vty
->escape
= VTY_NORMAL
;
1456 vty_forward_kill_word(vty
);
1457 vty
->escape
= VTY_NORMAL
;
1461 vty_backward_kill_word(vty
);
1462 vty
->escape
= VTY_NORMAL
;
1465 vty
->escape
= VTY_NORMAL
;
1471 if (vty
->escape
== VTY_CR
) {
1472 /* if we get CR+NL, the NL results in an extra empty
1473 * prompt line being printed without this; just drop
1474 * the NL if it immediately follows CR.
1476 vty
->escape
= VTY_NORMAL
;
1484 vty_beginning_of_line(vty
);
1487 vty_backward_char(vty
);
1490 vty_stop_input(vty
);
1493 vty_delete_char(vty
);
1496 vty_end_of_line(vty
);
1499 vty_forward_char(vty
);
1503 vty_delete_backward_char(vty
);
1512 vty_previous_line(vty
);
1515 vty_transpose_chars(vty
);
1518 vty_kill_line_from_beginning(vty
);
1521 vty_backward_kill_word(vty
);
1524 vty_end_config(vty
);
1527 vty
->escape
= VTY_CR
;
1531 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1534 if (vty
->pass_fd
!= -1) {
1535 close(vty
->pass_fd
);
1540 vty_complete_command(vty
);
1543 if (vty
->node
== AUTH_NODE
1544 || vty
->node
== AUTH_ENABLE_NODE
)
1545 vty_self_insert(vty
, buf
[i
]);
1547 vty_describe_command(vty
);
1550 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1551 vty
->escape
= VTY_ESCAPE
;
1554 vty
->escape
= VTY_PRE_ESCAPE
;
1557 if (buf
[i
] > 31 && buf
[i
] < 127)
1558 vty_self_insert(vty
, buf
[i
]);
1564 if (vty
->status
== VTY_CLOSE
)
1567 vty_event(VTY_WRITE
, vty
);
1568 vty_event(VTY_READ
, vty
);
1572 /* Flush buffer to the vty. */
1573 static void vty_flush(struct event
*thread
)
1576 buffer_status_t flushrc
;
1577 struct vty
*vty
= EVENT_ARG(thread
);
1579 /* Tempolary disable read thread. */
1580 if (vty
->lines
== 0)
1581 EVENT_OFF(vty
->t_read
);
1583 /* Function execution continue. */
1584 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1586 /* N.B. if width is 0, that means we don't know the window size. */
1587 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1588 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1589 else if (vty
->status
== VTY_MORELINE
)
1590 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1593 flushrc
= buffer_flush_window(
1594 vty
->obuf
, vty
->wfd
, vty
->width
,
1595 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1598 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1600 buffer_reset(vty
->lbuf
);
1601 buffer_reset(vty
->obuf
);
1605 if (vty
->status
== VTY_CLOSE
)
1608 vty
->status
= VTY_NORMAL
;
1609 if (vty
->lines
== 0)
1610 vty_event(VTY_READ
, vty
);
1613 case BUFFER_PENDING
:
1614 /* There is more data waiting to be written. */
1615 vty
->status
= VTY_MORE
;
1616 if (vty
->lines
== 0)
1617 vty_event(VTY_WRITE
, vty
);
1622 /* Allocate new vty struct. */
1623 struct vty
*vty_new(void)
1625 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1627 new->fd
= new->wfd
= -1;
1629 new->lbuf
= buffer_new(0);
1630 new->obuf
= buffer_new(0); /* Use default buffer size. */
1631 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1632 new->max
= VTY_BUFSIZ
;
1635 if (mgmt_lib_hndl
) {
1636 new->mgmt_client_id
= mgmt_client_id_next
++;
1637 if (mgmt_fe_create_client_session(
1638 mgmt_lib_hndl
, new->mgmt_client_id
,
1639 (uintptr_t) new) != MGMTD_SUCCESS
)
1641 "Failed to open a MGMTD Frontend session for VTY session %p!!",
1649 /* allocate and initialise vty */
1650 static struct vty
*vty_new_init(int vty_sock
)
1656 vty
->wfd
= vty_sock
;
1657 vty
->type
= VTY_TERM
;
1658 vty
->node
= AUTH_NODE
;
1663 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1666 vty
->xpath_index
= 0;
1667 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1668 vty
->private_config
= false;
1669 vty
->candidate_config
= vty_shared_candidate_config
;
1670 vty
->status
= VTY_NORMAL
;
1673 vty
->iac_sb_in_progress
= 0;
1676 vtys_add_tail(vty_sessions
, vty
);
1681 /* Create new vty structure. */
1682 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1684 char buf
[SU_ADDRSTRLEN
];
1687 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1689 /* Allocate new vty structure and set up default values. */
1690 vty
= vty_new_init(vty_sock
);
1692 /* configurable parameters not part of basic init */
1693 vty
->v_timeout
= vty_timeout_val
;
1694 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1695 if (no_password_check
) {
1697 vty
->node
= ENABLE_NODE
;
1699 vty
->node
= VIEW_NODE
;
1701 if (host
.lines
>= 0)
1702 vty
->lines
= host
.lines
;
1704 if (!no_password_check
) {
1705 /* Vty is not available if password isn't set. */
1706 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1707 vty_out(vty
, "Vty password is not set.\n");
1708 vty
->status
= VTY_CLOSE
;
1714 /* Say hello to the world. */
1716 if (!no_password_check
)
1717 vty_out(vty
, "\nUser Access Verification\n\n");
1719 /* Setting up terminal. */
1721 vty_will_suppress_go_ahead(vty
);
1723 vty_dont_linemode(vty
);
1724 vty_do_window_size(vty
);
1725 /* vty_dont_lflow_ahead (vty); */
1729 /* Add read/write thread. */
1730 vty_event(VTY_WRITE
, vty
);
1731 vty_event(VTY_READ
, vty
);
1736 /* create vty for stdio */
1737 static struct termios stdio_orig_termios
;
1738 static struct vty
*stdio_vty
= NULL
;
1739 static bool stdio_termios
= false;
1740 static void (*stdio_vty_atclose
)(int isexit
);
1742 static void vty_stdio_reset(int isexit
)
1746 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1747 stdio_termios
= false;
1751 if (stdio_vty_atclose
)
1752 stdio_vty_atclose(isexit
);
1753 stdio_vty_atclose
= NULL
;
1757 static void vty_stdio_atexit(void)
1762 void vty_stdio_suspend(void)
1767 EVENT_OFF(stdio_vty
->t_write
);
1768 EVENT_OFF(stdio_vty
->t_read
);
1769 EVENT_OFF(stdio_vty
->t_timeout
);
1772 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1773 stdio_termios
= false;
1776 void vty_stdio_resume(void)
1781 if (!tcgetattr(0, &stdio_orig_termios
)) {
1782 struct termios termios
;
1784 termios
= stdio_orig_termios
;
1785 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1786 | IGNCR
| ICRNL
| IXON
);
1787 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1788 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1789 termios
.c_cflag
|= CS8
;
1790 tcsetattr(0, TCSANOW
, &termios
);
1791 stdio_termios
= true;
1794 vty_prompt(stdio_vty
);
1796 /* Add read/write thread. */
1797 vty_event(VTY_WRITE
, stdio_vty
);
1798 vty_event(VTY_READ
, stdio_vty
);
1801 void vty_stdio_close(void)
1805 vty_close(stdio_vty
);
1808 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1812 /* refuse creating two vtys on stdio */
1816 vty
= stdio_vty
= vty_new_init(0);
1817 stdio_vty_atclose
= atclose
;
1820 /* always have stdio vty in a known _unchangeable_ state, don't want
1822 * to have any effect here to make sure scripting this works as intended
1824 vty
->node
= ENABLE_NODE
;
1826 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1832 /* Accept connection from the network. */
1833 static void vty_accept(struct event
*thread
)
1835 struct vty_serv
*vtyserv
= EVENT_ARG(thread
);
1840 int accept_sock
= vtyserv
->sock
;
1842 struct access_list
*acl
= NULL
;
1844 /* We continue hearing vty socket. */
1845 vty_event_serv(VTY_SERV
, vtyserv
);
1847 memset(&su
, 0, sizeof(union sockunion
));
1849 /* We can handle IPv4 or IPv6 socket. */
1850 vty_sock
= sockunion_accept(accept_sock
, &su
);
1852 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1853 safe_strerror(errno
));
1856 set_nonblocking(vty_sock
);
1857 set_cloexec(vty_sock
);
1859 if (!sockunion2hostprefix(&su
, &p
)) {
1861 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1866 /* VTY's accesslist apply. */
1867 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1868 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1869 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1870 zlog_info("Vty connection refused from %pSU", &su
);
1876 /* VTY's ipv6 accesslist apply. */
1877 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1878 if ((acl
= access_list_lookup(AFI_IP6
,
1879 vty_ipv6_accesslist_name
))
1880 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1881 zlog_info("Vty connection refused from %pSU", &su
);
1888 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1891 zlog_info("can't set sockopt to vty_sock : %s",
1892 safe_strerror(errno
));
1894 zlog_info("Vty connection from %pSU", &su
);
1896 vty_create(vty_sock
, &su
);
1899 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1902 struct addrinfo req
;
1903 struct addrinfo
*ainfo
;
1904 struct addrinfo
*ainfo_save
;
1906 char port_str
[BUFSIZ
];
1908 memset(&req
, 0, sizeof(req
));
1909 req
.ai_flags
= AI_PASSIVE
;
1910 req
.ai_family
= AF_UNSPEC
;
1911 req
.ai_socktype
= SOCK_STREAM
;
1912 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1913 port_str
[sizeof(port_str
) - 1] = '\0';
1915 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1918 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1926 struct vty_serv
*vtyserv
;
1928 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1931 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1932 ainfo
->ai_protocol
);
1936 sockopt_v6only(ainfo
->ai_family
, sock
);
1937 sockopt_reuseaddr(sock
);
1938 sockopt_reuseport(sock
);
1941 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1943 close(sock
); /* Avoid sd leak. */
1947 ret
= listen(sock
, 3);
1949 close(sock
); /* Avoid sd leak. */
1953 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1954 vtyserv
->sock
= sock
;
1955 vtyservs_add_tail(vty_servs
, vtyserv
);
1957 vty_event_serv(VTY_SERV
, vtyserv
);
1958 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1960 freeaddrinfo(ainfo_save
);
1964 /* For sockaddr_un. */
1967 /* VTY shell UNIX domain socket. */
1968 static void vty_serv_un(const char *path
)
1970 struct vty_serv
*vtyserv
;
1973 struct sockaddr_un serv
;
1975 struct zprivs_ids_t ids
;
1977 /* First of all, unlink existing socket */
1981 old_mask
= umask(0007);
1983 /* Make UNIX domain socket. */
1984 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1986 flog_err_sys(EC_LIB_SOCKET
,
1987 "Cannot create unix stream socket: %s",
1988 safe_strerror(errno
));
1992 /* Make server socket. */
1993 memset(&serv
, 0, sizeof(serv
));
1994 serv
.sun_family
= AF_UNIX
;
1995 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1996 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1997 len
= serv
.sun_len
= SUN_LEN(&serv
);
1999 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2000 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2004 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2006 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
2007 safe_strerror(errno
));
2008 close(sock
); /* Avoid sd leak. */
2012 ret
= listen(sock
, 5);
2014 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
2015 safe_strerror(errno
));
2016 close(sock
); /* Avoid sd leak. */
2022 zprivs_get_ids(&ids
);
2024 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2025 earlier for the case when we don't need to chown the file
2026 type casting it here to make a compare */
2027 if ((int)ids
.gid_vty
> 0) {
2028 /* set group of socket */
2029 if (chown(path
, -1, ids
.gid_vty
)) {
2030 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2031 "vty_serv_un: could chown socket, %s",
2032 safe_strerror(errno
));
2036 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
2037 vtyserv
->sock
= sock
;
2038 vtyserv
->vtysh
= true;
2039 vtyservs_add_tail(vty_servs
, vtyserv
);
2041 vty_event_serv(VTYSH_SERV
, vtyserv
);
2044 /* #define VTYSH_DEBUG 1 */
2046 static void vtysh_accept(struct event
*thread
)
2048 struct vty_serv
*vtyserv
= EVENT_ARG(thread
);
2049 int accept_sock
= vtyserv
->sock
;
2052 struct sockaddr_un client
;
2055 vty_event_serv(VTYSH_SERV
, vtyserv
);
2057 memset(&client
, 0, sizeof(client
));
2058 client_len
= sizeof(struct sockaddr_un
);
2060 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2061 (socklen_t
*)&client_len
);
2064 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2065 safe_strerror(errno
));
2069 if (set_nonblocking(sock
) < 0) {
2072 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2073 sock
, safe_strerror(errno
));
2080 printf("VTY shell accept\n");
2081 #endif /* VTYSH_DEBUG */
2086 vty
->type
= VTY_SHELL_SERV
;
2087 vty
->node
= VIEW_NODE
;
2088 vtys_add_tail(vtysh_sessions
, vty
);
2090 vty_event(VTYSH_READ
, vty
);
2093 static int vtysh_do_pass_fd(struct vty
*vty
)
2095 struct iovec iov
[1] = {
2097 .iov_base
= vty
->pass_fd_status
,
2098 .iov_len
= sizeof(vty
->pass_fd_status
),
2102 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2103 struct cmsghdr align
;
2105 struct msghdr mh
= {
2107 .msg_iovlen
= array_size(iov
),
2108 .msg_control
= u
.buf
,
2109 .msg_controllen
= sizeof(u
.buf
),
2111 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2114 memset(&u
.buf
, 0, sizeof(u
.buf
));
2115 cmh
->cmsg_level
= SOL_SOCKET
;
2116 cmh
->cmsg_type
= SCM_RIGHTS
;
2117 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2118 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2120 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2121 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2122 return BUFFER_PENDING
;
2124 close(vty
->pass_fd
);
2126 vty
->status
= VTY_NORMAL
;
2129 return BUFFER_ERROR
;
2131 /* resume accepting commands (suspended in vtysh_read) */
2132 vty_event(VTYSH_READ
, vty
);
2134 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2135 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2137 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2138 return BUFFER_PENDING
;
2140 return BUFFER_EMPTY
;
2143 static int vtysh_flush(struct vty
*vty
)
2147 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2148 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2149 ret
= vtysh_do_pass_fd(vty
);
2152 case BUFFER_PENDING
:
2153 vty_event(VTYSH_WRITE
, vty
);
2156 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2158 buffer_reset(vty
->lbuf
);
2159 buffer_reset(vty
->obuf
);
2168 void vty_pass_fd(struct vty
*vty
, int fd
)
2170 if (vty
->pass_fd
!= -1)
2171 close(vty
->pass_fd
);
2176 static void vtysh_read(struct event
*thread
)
2182 unsigned char buf
[VTY_READ_BUFSIZ
];
2184 uint8_t header
[4] = {0, 0, 0, 0};
2186 sock
= EVENT_FD(thread
);
2187 vty
= EVENT_ARG(thread
);
2189 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2191 if (ERRNO_IO_RETRY(errno
)) {
2192 vty_event(VTYSH_READ
, vty
);
2197 "%s: read failed on vtysh client fd %d, closing: %s",
2198 __func__
, sock
, safe_strerror(errno
));
2200 buffer_reset(vty
->lbuf
);
2201 buffer_reset(vty
->obuf
);
2204 printf("close vtysh\n");
2205 #endif /* VTYSH_DEBUG */
2210 printf("line: %.*s\n", nbytes
, buf
);
2211 #endif /* VTYSH_DEBUG */
2213 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2214 /* Clear command line buffer. */
2215 vty
->cp
= vty
->length
= 0;
2217 vty_out(vty
, "%% Command is too long.\n");
2219 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2220 vty
->buf
[vty
->length
++] = *p
;
2222 /* Pass this line to parser. */
2223 ret
= vty_execute(vty
);
2224 /* Note that vty_execute clears the command buffer and resets
2225 vty->length to 0. */
2227 /* Return result. */
2229 printf("result: %d\n", ret
);
2230 printf("vtysh node: %d\n", vty
->node
);
2231 #endif /* VTYSH_DEBUG */
2233 if (vty
->pass_fd
!= -1) {
2234 memset(vty
->pass_fd_status
, 0, 4);
2235 vty
->pass_fd_status
[3] = ret
;
2236 vty
->status
= VTY_PASSFD
;
2239 vty_event(VTYSH_WRITE
, vty
);
2241 /* this introduces a "sequence point"
2242 * command output is written normally,
2243 * read processing is suspended until
2245 * then retcode + FD is written
2246 * then normal processing resumes
2248 * => skip vty_event(VTYSH_READ, vty)!
2253 /* hack for asynchronous "write integrated"
2254 * - other commands in "buf" will be ditched
2255 * - input during pending config-write is
2257 if (ret
== CMD_SUSPEND
)
2260 /* with new infra we need to stop response till
2261 * we get response through callback.
2263 if (vty
->mgmt_req_pending
)
2266 /* warning: watchfrr hardcodes this result write
2269 buffer_put(vty
->obuf
, header
, 4);
2271 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2272 /* Try to flush results; exit if a write
2279 if (vty
->status
== VTY_CLOSE
)
2282 vty_event(VTYSH_READ
, vty
);
2285 static void vtysh_write(struct event
*thread
)
2287 struct vty
*vty
= EVENT_ARG(thread
);
2294 /* Determine address family to bind. */
2295 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2297 /* If port is set to 0, do not listen on TCP/IP at all! */
2299 vty_serv_sock_addrinfo(addr
, port
);
2306 static void vty_error_delete(void *arg
)
2308 struct vty_error
*ve
= arg
;
2310 XFREE(MTYPE_TMP
, ve
);
2313 /* Close vty interface. Warning: call this only from functions that
2314 will be careful not to access the vty afterwards (since it has
2315 now been freed). This is safest from top-level functions (called
2316 directly by the thread dispatcher). */
2317 void vty_close(struct vty
*vty
)
2320 bool was_stdio
= false;
2322 if (mgmt_lib_hndl
) {
2323 mgmt_fe_destroy_client_session(mgmt_lib_hndl
,
2324 vty
->mgmt_client_id
);
2325 vty
->mgmt_session_id
= 0;
2328 /* Drop out of configure / transaction if needed. */
2329 vty_config_exit(vty
);
2331 /* Cancel threads.*/
2332 EVENT_OFF(vty
->t_read
);
2333 EVENT_OFF(vty
->t_write
);
2334 EVENT_OFF(vty
->t_timeout
);
2336 if (vty
->pass_fd
!= -1) {
2337 close(vty
->pass_fd
);
2340 zlog_live_close(&vty
->live_log
);
2343 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2345 /* Free input buffer. */
2346 buffer_free(vty
->obuf
);
2347 buffer_free(vty
->lbuf
);
2349 /* Free command history. */
2350 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2351 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2355 if (vty
->fd
!= -1) {
2356 if (vty
->type
== VTY_SHELL_SERV
)
2357 vtys_del(vtysh_sessions
, vty
);
2359 vtys_del(vty_sessions
, vty
);
2362 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2366 * note check is for fd > STDERR_FILENO, not fd != -1.
2367 * We never close stdin/stdout/stderr here, because we may be
2368 * running in foreground mode with logging to stdout. Also,
2369 * additionally, we'd need to replace these fds with /dev/null. */
2370 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2372 if (vty
->fd
> STDERR_FILENO
)
2374 if (vty
->fd
== STDIN_FILENO
)
2377 XFREE(MTYPE_VTY
, vty
->buf
);
2380 vty
->error
->del
= vty_error_delete
;
2381 list_delete(&vty
->error
);
2385 XFREE(MTYPE_VTY
, vty
);
2391 /* When time out occur output message then close connection. */
2392 static void vty_timeout(struct event
*thread
)
2396 vty
= EVENT_ARG(thread
);
2400 buffer_reset(vty
->lbuf
);
2401 buffer_reset(vty
->obuf
);
2402 vty_out(vty
, "\nVty connection is timed out.\n");
2404 /* Close connection. */
2405 vty
->status
= VTY_CLOSE
;
2409 /* Read up configuration file from file_name. */
2410 void vty_read_file(struct nb_config
*config
, FILE *confp
)
2414 struct vty_error
*ve
;
2415 struct listnode
*node
;
2416 unsigned int line_num
= 0;
2419 /* vty_close won't close stderr; if some config command prints
2420 * something it'll end up there. (not ideal; it'd be better if output
2421 * from a file-load went to logging instead. Also note that if this
2422 * function is called after daemonizing, stderr will be /dev/null.)
2424 * vty->fd will be -1 from vty_new()
2426 vty
->wfd
= STDERR_FILENO
;
2427 vty
->type
= VTY_FILE
;
2428 vty
->node
= CONFIG_NODE
;
2431 vty
->candidate_config
= config
;
2433 vty
->private_config
= true;
2434 vty
->candidate_config
= nb_config_new(NULL
);
2437 /* Execute configuration file */
2438 ret
= config_from_file(vty
, confp
, &line_num
);
2440 /* Flush any previous errors before printing messages below */
2441 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2443 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2444 const char *message
= NULL
;
2448 case CMD_ERR_AMBIGUOUS
:
2449 message
= "Ambiguous command";
2451 case CMD_ERR_NO_MATCH
:
2452 message
= "No such command";
2455 message
= "Command returned Warning";
2457 case CMD_WARNING_CONFIG_FAILED
:
2458 message
= "Command returned Warning Config Failed";
2460 case CMD_ERR_INCOMPLETE
:
2461 message
= "Command returned Incomplete";
2463 case CMD_ERR_EXEED_ARGC_MAX
:
2465 "Command exceeded maximum number of Arguments";
2468 message
= "Command returned unhandled error message";
2472 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2473 nl
= strchr(ve
->error_buf
, '\n');
2476 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2477 message
, ve
->line_num
, ve
->error_buf
);
2482 * Automatically commit the candidate configuration after
2483 * reading the configuration file.
2485 if (config
== NULL
) {
2486 struct nb_context context
= {};
2487 char errmsg
[BUFSIZ
] = {0};
2489 context
.client
= NB_CLIENT_CLI
;
2491 ret
= nb_candidate_commit(context
, vty
->candidate_config
, true,
2492 "Read configuration file", NULL
,
2493 errmsg
, sizeof(errmsg
));
2494 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2496 "%s: failed to read configuration file: %s (%s)",
2497 __func__
, nb_err_name(ret
), errmsg
);
2503 static FILE *vty_use_backup_config(const char *fullpath
)
2505 char *fullpath_sav
, *fullpath_tmp
;
2511 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2512 fullpath_sav
= malloc(fullpath_sav_sz
);
2513 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2514 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2516 sav
= open(fullpath_sav
, O_RDONLY
);
2522 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2523 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2525 /* Open file to configuration write. */
2526 tmp
= mkstemp(fullpath_tmp
);
2530 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2533 while ((c
= read(sav
, buffer
, 512)) > 0) {
2534 if (write(tmp
, buffer
, c
) <= 0)
2540 if (rename(fullpath_tmp
, fullpath
) == 0)
2541 ret
= fopen(fullpath
, "r");
2543 unlink(fullpath_tmp
);
2548 unlink(fullpath_tmp
);
2558 /* Read up configuration file from file_name. */
2559 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2560 char *config_default_dir
)
2562 char cwd
[MAXPATHLEN
];
2564 const char *fullpath
;
2566 bool read_success
= false;
2568 /* If -f flag specified. */
2569 if (config_file
!= NULL
) {
2570 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2571 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2574 "%s: failure to determine Current Working Directory %d!",
2576 goto tmp_free_and_out
;
2578 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2579 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2580 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2583 fullpath
= config_file
;
2585 confp
= fopen(fullpath
, "r");
2587 if (confp
== NULL
) {
2589 EC_LIB_BACKUP_CONFIG
,
2590 "%s: failed to open configuration file %s: %s, checking backup",
2591 __func__
, fullpath
, safe_strerror(errno
));
2593 confp
= vty_use_backup_config(fullpath
);
2595 flog_warn(EC_LIB_BACKUP_CONFIG
,
2596 "using backup configuration file!");
2600 "%s: can't open configuration file [%s]",
2601 __func__
, config_file
);
2602 goto tmp_free_and_out
;
2607 host_config_set(config_default_dir
);
2611 struct stat conf_stat
;
2613 /* !!!!PLEASE LEAVE!!!!
2614 * This is NEEDED for use with vtysh -b, or else you can get
2615 * a real configuration food fight with a lot garbage in the
2616 * merged configuration file it creates coming from the per
2617 * daemon configuration files. This also allows the daemons
2618 * to start if there default configuration file is not
2619 * present or ignore them, as needed when using vtysh -b to
2620 * configure the daemons at boot - MAG
2623 /* Stat for vtysh Zebra.conf, if found startup and wait for
2624 * boot configuration
2627 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2628 ret
= stat(integrate_default
, &conf_stat
);
2630 read_success
= true;
2631 goto tmp_free_and_out
;
2635 confp
= fopen(config_default_dir
, "r");
2636 if (confp
== NULL
) {
2639 "%s: failed to open configuration file %s: %s, checking backup",
2640 __func__
, config_default_dir
,
2641 safe_strerror(errno
));
2643 confp
= vty_use_backup_config(config_default_dir
);
2645 flog_warn(EC_LIB_BACKUP_CONFIG
,
2646 "using backup configuration file!");
2647 fullpath
= config_default_dir
;
2649 flog_err(EC_LIB_VTY
,
2650 "can't open configuration file [%s]",
2651 config_default_dir
);
2652 goto tmp_free_and_out
;
2655 fullpath
= config_default_dir
;
2658 vty_read_file(config
, confp
);
2659 read_success
= true;
2663 host_config_set(fullpath
);
2666 XFREE(MTYPE_TMP
, tmp
);
2668 return read_success
;
2671 static void update_xpath(struct vty
*vty
, const char *oldpath
,
2672 const char *newpath
)
2676 for (i
= 0; i
< vty
->xpath_index
; i
++) {
2677 if (!frrstr_startswith(vty
->xpath
[i
], oldpath
))
2680 char *tmp
= frrstr_replace(vty
->xpath
[i
], oldpath
, newpath
);
2681 strlcpy(vty
->xpath
[i
], tmp
, sizeof(vty
->xpath
[0]));
2682 XFREE(MTYPE_TMP
, tmp
);
2686 void vty_update_xpath(const char *oldpath
, const char *newpath
)
2690 frr_each (vtys
, vtysh_sessions
, vty
)
2691 update_xpath(vty
, oldpath
, newpath
);
2692 frr_each (vtys
, vty_sessions
, vty
)
2693 update_xpath(vty
, oldpath
, newpath
);
2696 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2698 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2699 vty_out(vty
, "%% Configuration is locked by other client\n");
2703 if (vty_mgmt_fe_enabled()) {
2704 if (!mgmt_candidate_ds_wr_locked
) {
2705 if (vty_mgmt_send_lockds_req(vty
, MGMTD_DS_CANDIDATE
,
2707 vty_out(vty
, "Not able to lock candidate DS\n");
2712 "Candidate DS already locked by different session\n");
2716 vty
->mgmt_locked_candidate_ds
= true;
2717 mgmt_candidate_ds_wr_locked
= true;
2720 vty
->node
= CONFIG_NODE
;
2722 vty
->private_config
= private_config
;
2723 vty
->xpath_index
= 0;
2725 if (private_config
) {
2726 vty
->candidate_config
= nb_config_dup(running_config
);
2727 vty
->candidate_config_base
= nb_config_dup(running_config
);
2729 "Warning: uncommitted changes will be discarded on exit.\n\n");
2732 * NOTE: On the MGMTD daemon we point the VTY candidate DS to
2733 * the global MGMTD candidate DS. Else we point to the VTY
2734 * Shared Candidate Config.
2736 vty
->candidate_config
= vty_mgmt_candidate_config
2737 ? vty_mgmt_candidate_config
2738 : vty_shared_candidate_config
;
2739 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2740 vty
->candidate_config_base
=
2741 nb_config_dup(running_config
);
2747 void vty_config_exit(struct vty
*vty
)
2749 enum node_type node
= vty
->node
;
2750 struct cmd_node
*cnode
;
2752 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2753 * somewhere below CONFIG_NODE */
2754 while (node
&& node
!= CONFIG_NODE
) {
2755 cnode
= vector_lookup(cmdvec
, node
);
2756 node
= cnode
->parent_node
;
2758 if (node
!= CONFIG_NODE
)
2759 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2762 while (vty
->node
!= ENABLE_NODE
)
2763 /* will call vty_config_node_exit() below */
2767 int vty_config_node_exit(struct vty
*vty
)
2769 vty
->xpath_index
= 0;
2771 if (vty_mgmt_fe_enabled() && mgmt_candidate_ds_wr_locked
&&
2772 vty
->mgmt_locked_candidate_ds
) {
2773 if (vty_mgmt_send_lockds_req(vty
, MGMTD_DS_CANDIDATE
, false) !=
2775 vty_out(vty
, "Not able to unlock candidate DS\n");
2779 vty
->mgmt_locked_candidate_ds
= false;
2780 mgmt_candidate_ds_wr_locked
= false;
2783 /* Perform any pending commits. */
2784 (void)nb_cli_pending_commit_check(vty
);
2786 /* Check if there's a pending confirmed commit. */
2787 if (vty
->t_confirmed_commit_timeout
) {
2789 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2790 nb_cli_confirmed_commit_rollback(vty
);
2791 nb_cli_confirmed_commit_clean(vty
);
2794 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2796 if (vty
->candidate_config
) {
2797 if (vty
->private_config
)
2798 nb_config_free(vty
->candidate_config
);
2799 vty
->candidate_config
= NULL
;
2801 if (vty
->candidate_config_base
) {
2802 nb_config_free(vty
->candidate_config_base
);
2803 vty
->candidate_config_base
= NULL
;
2806 vty
->config
= false;
2810 /* Master of the threads. */
2811 static struct event_loop
*vty_master
;
2813 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2817 event_add_read(vty_master
, vty_accept
, vty_serv
, vty_serv
->sock
,
2818 &vty_serv
->t_accept
);
2822 event_add_read(vty_master
, vtysh_accept
, vty_serv
,
2823 vty_serv
->sock
, &vty_serv
->t_accept
);
2828 case VTY_TIMEOUT_RESET
:
2831 assert(!"vty_event_serv() called incorrectly");
2835 static void vty_event(enum vty_event event
, struct vty
*vty
)
2840 event_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2844 event_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2849 event_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2852 /* Time out treatment. */
2853 if (vty
->v_timeout
) {
2854 EVENT_OFF(vty
->t_timeout
);
2855 event_add_timer(vty_master
, vty_timeout
, vty
,
2856 vty
->v_timeout
, &vty
->t_timeout
);
2860 event_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2863 case VTY_TIMEOUT_RESET
:
2864 EVENT_OFF(vty
->t_timeout
);
2866 event_add_timer(vty_master
, vty_timeout
, vty
,
2867 vty
->v_timeout
, &vty
->t_timeout
);
2871 assert(!"vty_event() called incorrectly");
2875 DEFUN_NOSH (config_who
,
2878 "Display who is on vty\n")
2882 frr_each (vtys
, vty_sessions
, v
)
2883 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2884 v
->config
? "*" : " ", v
->fd
, v
->address
,
2885 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2889 /* Move to vty configuration mode. */
2890 DEFUN_NOSH (line_vty
,
2893 "Configure a terminal line\n"
2894 "Virtual terminal\n")
2896 vty
->node
= VTY_NODE
;
2900 /* Set time out value. */
2901 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2902 const char *sec_str
)
2904 unsigned long timeout
= 0;
2906 /* min_str and sec_str are already checked by parser. So it must be
2907 all digit string. */
2909 timeout
= strtol(min_str
, NULL
, 10);
2913 timeout
+= strtol(sec_str
, NULL
, 10);
2915 vty_timeout_val
= timeout
;
2916 vty
->v_timeout
= timeout
;
2917 vty_event(VTY_TIMEOUT_RESET
, vty
);
2923 DEFUN (exec_timeout_min
,
2924 exec_timeout_min_cmd
,
2925 "exec-timeout (0-35791)",
2926 "Set timeout value\n"
2927 "Timeout value in minutes\n")
2930 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2933 DEFUN (exec_timeout_sec
,
2934 exec_timeout_sec_cmd
,
2935 "exec-timeout (0-35791) (0-2147483)",
2936 "Set the EXEC timeout\n"
2937 "Timeout in minutes\n"
2938 "Timeout in seconds\n")
2941 int idx_number_2
= 2;
2942 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2943 argv
[idx_number_2
]->arg
);
2946 DEFUN (no_exec_timeout
,
2947 no_exec_timeout_cmd
,
2950 "Set the EXEC timeout\n")
2952 return exec_timeout(vty
, NULL
, NULL
);
2955 /* Set vty access class. */
2956 DEFUN (vty_access_class
,
2957 vty_access_class_cmd
,
2958 "access-class WORD",
2959 "Filter connections based on an IP access list\n"
2963 if (vty_accesslist_name
)
2964 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2966 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2971 /* Clear vty access class. */
2972 DEFUN (no_vty_access_class
,
2973 no_vty_access_class_cmd
,
2974 "no access-class [WORD]",
2976 "Filter connections based on an IP access list\n"
2980 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2981 if (!vty_accesslist_name
2982 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2983 vty_out(vty
, "Access-class is not currently applied to vty\n");
2984 return CMD_WARNING_CONFIG_FAILED
;
2987 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2989 vty_accesslist_name
= NULL
;
2994 /* Set vty access class. */
2995 DEFUN (vty_ipv6_access_class
,
2996 vty_ipv6_access_class_cmd
,
2997 "ipv6 access-class WORD",
2999 "Filter connections based on an IP access list\n"
3000 "IPv6 access list\n")
3003 if (vty_ipv6_accesslist_name
)
3004 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3006 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
3011 /* Clear vty access class. */
3012 DEFUN (no_vty_ipv6_access_class
,
3013 no_vty_ipv6_access_class_cmd
,
3014 "no ipv6 access-class [WORD]",
3017 "Filter connections based on an IP access list\n"
3018 "IPv6 access list\n")
3021 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
3023 if (!vty_ipv6_accesslist_name
3024 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
3026 "IPv6 access-class is not currently applied to vty\n");
3027 return CMD_WARNING_CONFIG_FAILED
;
3030 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3032 vty_ipv6_accesslist_name
= NULL
;
3041 "Enable password checking\n")
3043 no_password_check
= 0;
3047 DEFUN (no_vty_login
,
3051 "Enable password checking\n")
3053 no_password_check
= 1;
3057 DEFUN (service_advanced_vty
,
3058 service_advanced_vty_cmd
,
3059 "service advanced-vty",
3060 "Set up miscellaneous service\n"
3061 "Enable advanced mode vty interface\n")
3067 DEFUN (no_service_advanced_vty
,
3068 no_service_advanced_vty_cmd
,
3069 "no service advanced-vty",
3071 "Set up miscellaneous service\n"
3072 "Enable advanced mode vty interface\n")
3078 DEFUN_NOSH(terminal_monitor
,
3079 terminal_monitor_cmd
,
3080 "terminal monitor [detach]",
3081 "Set terminal line parameters\n"
3082 "Copy debug output to the current terminal line\n"
3083 "Keep logging feed open independent of VTY session\n")
3087 if (vty
->type
!= VTY_SHELL_SERV
) {
3088 vty_out(vty
, "%% not supported\n");
3093 struct zlog_live_cfg detach_log
= {};
3095 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
3096 zlog_live_disown(&detach_log
);
3098 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
3101 vty_out(vty
, "%% error opening live log: %m\n");
3105 vty_pass_fd(vty
, fd_ret
);
3109 DEFUN_NOSH(no_terminal_monitor
,
3110 no_terminal_monitor_cmd
,
3111 "no terminal monitor",
3113 "Set terminal line parameters\n"
3114 "Copy debug output to the current terminal line\n")
3116 zlog_live_close(&vty
->live_log
);
3120 DEFUN_NOSH(terminal_no_monitor
,
3121 terminal_no_monitor_cmd
,
3122 "terminal no monitor",
3123 "Set terminal line parameters\n"
3125 "Copy debug output to the current terminal line\n")
3127 return no_terminal_monitor(self
, vty
, argc
, argv
);
3131 DEFUN_NOSH (show_history
,
3135 "Display the session command history\n")
3139 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3140 if (index
== VTY_MAXHIST
) {
3145 if (vty
->hist
[index
] != NULL
)
3146 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3155 DEFPY (log_commands
,
3157 "[no] log commands",
3160 "Log all commands\n")
3163 if (vty_log_commands_perm
) {
3165 "Daemon started with permanent logging turned on for commands, ignoring\n");
3169 vty_log_commands
= false;
3171 vty_log_commands
= true;
3176 /* Display current configuration. */
3177 static int vty_config_write(struct vty
*vty
)
3179 vty_frame(vty
, "line vty\n");
3181 if (vty_accesslist_name
)
3182 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3184 if (vty_ipv6_accesslist_name
)
3185 vty_out(vty
, " ipv6 access-class %s\n",
3186 vty_ipv6_accesslist_name
);
3189 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3190 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3191 vty_timeout_val
% 60);
3194 if (no_password_check
)
3195 vty_out(vty
, " no login\n");
3197 vty_endframe(vty
, "exit\n");
3199 if (vty_log_commands
)
3200 vty_out(vty
, "log commands\n");
3202 vty_out(vty
, "!\n");
3207 static int vty_config_write(struct vty
*vty
);
3208 struct cmd_node vty_node
= {
3211 .parent_node
= CONFIG_NODE
,
3212 .prompt
= "%s(config-line)# ",
3213 .config_write
= vty_config_write
,
3216 /* Reset all VTY status. */
3217 void vty_reset(void)
3221 frr_each_safe (vtys
, vty_sessions
, vty
) {
3222 buffer_reset(vty
->lbuf
);
3223 buffer_reset(vty
->obuf
);
3224 vty
->status
= VTY_CLOSE
;
3228 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3230 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3231 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3234 static void vty_save_cwd(void)
3238 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3242 * At this point if these go wrong, more than likely
3243 * the whole world is coming down around us
3244 * Hence not worrying about it too much.
3246 if (chdir(SYSCONFDIR
)) {
3247 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3248 "Failure to chdir to %s, errno: %d",
3252 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3253 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3254 "Failure to getcwd, errno: %d", errno
);
3260 char *vty_get_cwd(void)
3265 int vty_shell(struct vty
*vty
)
3267 return vty
->type
== VTY_SHELL
? 1 : 0;
3270 int vty_shell_serv(struct vty
*vty
)
3272 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3275 void vty_init_vtysh(void)
3277 /* currently nothing to do, but likely to have future use */
3280 static void vty_mgmt_server_connected(uintptr_t lib_hndl
, uintptr_t usr_data
,
3283 VTY_DBG("%sGot %sconnected %s MGMTD Frontend Server",
3284 !connected
? "ERROR: " : "", !connected
? "dis: " : "",
3285 !connected
? "from" : "to");
3287 mgmt_fe_connected
= connected
;
3290 * TODO: Setup or teardown front-end sessions for existing
3295 static void vty_mgmt_session_created(uintptr_t lib_hndl
, uintptr_t usr_data
,
3296 uint64_t client_id
, bool create
,
3297 bool success
, uintptr_t session_id
,
3298 uintptr_t session_ctx
)
3302 vty
= (struct vty
*)session_ctx
;
3305 zlog_err("%s session for client %" PRIu64
" failed!",
3306 create
? "Creating" : "Destroying", client_id
);
3310 VTY_DBG("%s session for client %" PRIu64
" successfully",
3311 create
? "Created" : "Destroyed", client_id
);
3313 vty
->mgmt_session_id
= session_id
;
3316 static void vty_mgmt_ds_lock_notified(uintptr_t lib_hndl
, uintptr_t usr_data
,
3317 uint64_t client_id
, uintptr_t session_id
,
3318 uintptr_t session_ctx
, uint64_t req_id
,
3319 bool lock_ds
, bool success
,
3320 Mgmtd__DatastoreId ds_id
,
3321 char *errmsg_if_any
)
3325 vty
= (struct vty
*)session_ctx
;
3328 zlog_err("%socking for DS %u failed, Err: '%s'",
3329 lock_ds
? "L" : "Unl", ds_id
, errmsg_if_any
);
3330 vty_out(vty
, "ERROR: %socking for DS %u failed, Err: '%s'\n",
3331 lock_ds
? "L" : "Unl", ds_id
, errmsg_if_any
);
3333 VTY_DBG("%socked DS %u successfully", lock_ds
? "L" : "Unl",
3337 vty_mgmt_resume_response(vty
, success
);
3340 static void vty_mgmt_set_config_result_notified(
3341 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3342 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3343 bool success
, Mgmtd__DatastoreId ds_id
, char *errmsg_if_any
)
3347 vty
= (struct vty
*)session_ctx
;
3350 zlog_err("SET_CONFIG request for client 0x%" PRIx64
3351 " failed, Error: '%s'",
3352 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3353 vty_out(vty
, "ERROR: SET_CONFIG request failed, Error: %s\n",
3354 errmsg_if_any
? errmsg_if_any
: "Unknown");
3356 VTY_DBG("SET_CONFIG request for client 0x%" PRIx64
3357 " req-id %" PRIu64
" was successfull",
3361 vty_mgmt_resume_response(vty
, success
);
3364 static void vty_mgmt_commit_config_result_notified(
3365 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3366 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3367 bool success
, Mgmtd__DatastoreId src_ds_id
,
3368 Mgmtd__DatastoreId dst_ds_id
, bool validate_only
, char *errmsg_if_any
)
3372 vty
= (struct vty
*)session_ctx
;
3375 zlog_err("COMMIT_CONFIG request for client 0x%" PRIx64
3376 " failed, Error: '%s'",
3377 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3378 vty_out(vty
, "ERROR: COMMIT_CONFIG request failed, Error: %s\n",
3379 errmsg_if_any
? errmsg_if_any
: "Unknown");
3381 VTY_DBG("COMMIT_CONFIG request for client 0x%" PRIx64
3382 " req-id %" PRIu64
" was successfull",
3385 vty_out(vty
, "MGMTD: %s\n", errmsg_if_any
);
3388 vty_mgmt_resume_response(vty
, success
);
3391 static enum mgmt_result
vty_mgmt_get_data_result_notified(
3392 uintptr_t lib_hndl
, uintptr_t usr_data
, uint64_t client_id
,
3393 uintptr_t session_id
, uintptr_t session_ctx
, uint64_t req_id
,
3394 bool success
, Mgmtd__DatastoreId ds_id
, Mgmtd__YangData
**yang_data
,
3395 size_t num_data
, int next_key
, char *errmsg_if_any
)
3400 vty
= (struct vty
*)session_ctx
;
3403 zlog_err("GET_DATA request for client 0x%" PRIx64
3404 " failed, Error: '%s'",
3405 client_id
, errmsg_if_any
? errmsg_if_any
: "Unknown");
3406 vty_out(vty
, "ERROR: GET_DATA request failed, Error: %s\n",
3407 errmsg_if_any
? errmsg_if_any
: "Unknown");
3408 vty_mgmt_resume_response(vty
, success
);
3409 return MGMTD_INTERNAL_ERROR
;
3412 VTY_DBG("GET_DATA request succeeded, client 0x%" PRIx64
3416 if (req_id
!= mgmt_last_req_id
) {
3417 mgmt_last_req_id
= req_id
;
3418 vty_out(vty
, "[\n");
3421 for (indx
= 0; indx
< num_data
; indx
++) {
3422 vty_out(vty
, " \"%s\": \"%s\"\n", yang_data
[indx
]->xpath
,
3423 yang_data
[indx
]->value
->encoded_str_val
);
3426 vty_out(vty
, "]\n");
3427 vty_mgmt_resume_response(vty
, success
);
3430 return MGMTD_SUCCESS
;
3433 static struct mgmt_fe_client_params client_params
= {
3434 .client_connect_notify
= vty_mgmt_server_connected
,
3435 .client_session_notify
= vty_mgmt_session_created
,
3436 .lock_ds_notify
= vty_mgmt_ds_lock_notified
,
3437 .set_config_notify
= vty_mgmt_set_config_result_notified
,
3438 .commit_config_notify
= vty_mgmt_commit_config_result_notified
,
3439 .get_data_notify
= vty_mgmt_get_data_result_notified
,
3442 void vty_init_mgmt_fe(void)
3445 zlog_err("Always call vty_mgmt_init_fe() after vty_init()!!");
3449 assert(!mgmt_lib_hndl
);
3450 snprintf(client_params
.name
, sizeof(client_params
.name
), "%s-%lld",
3451 frr_get_progname(), (long long)getpid());
3452 mgmt_lib_hndl
= mgmt_fe_client_lib_init(&client_params
, vty_master
);
3453 assert(mgmt_lib_hndl
);
3456 bool vty_mgmt_fe_enabled(void)
3458 return mgmt_lib_hndl
&& mgmt_fe_connected
? true : false;
3461 int vty_mgmt_send_lockds_req(struct vty
*vty
, Mgmtd__DatastoreId ds_id
,
3464 enum mgmt_result ret
;
3466 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3468 ret
= mgmt_fe_lock_ds(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3469 vty
->mgmt_req_id
, ds_id
, lock
);
3470 if (ret
!= MGMTD_SUCCESS
) {
3471 zlog_err("Failed sending %sLOCK-DS-REQ req-id %" PRIu64
,
3472 lock
? "" : "UN", vty
->mgmt_req_id
);
3473 vty_out(vty
, "Failed to send %sLOCK-DS-REQ to MGMTD!\n",
3478 vty
->mgmt_req_pending
= true;
3484 int vty_mgmt_send_config_data(struct vty
*vty
)
3486 Mgmtd__YangDataValue value
[VTY_MAXCFGCHANGES
];
3487 Mgmtd__YangData cfg_data
[VTY_MAXCFGCHANGES
];
3488 Mgmtd__YangCfgDataReq cfg_req
[VTY_MAXCFGCHANGES
];
3489 Mgmtd__YangCfgDataReq
*cfgreq
[VTY_MAXCFGCHANGES
] = {0};
3492 bool implicit_commit
= false;
3494 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3496 for (indx
= 0; indx
< vty
->num_cfg_changes
; indx
++) {
3497 mgmt_yang_data_init(&cfg_data
[cnt
]);
3499 if (vty
->cfg_changes
[indx
].value
) {
3500 mgmt_yang_data_value_init(&value
[cnt
]);
3501 value
[cnt
].encoded_str_val
=
3502 (char *)vty
->cfg_changes
[indx
].value
;
3503 value
[cnt
].value_case
=
3504 MGMTD__YANG_DATA_VALUE__VALUE_ENCODED_STR_VAL
;
3505 cfg_data
[cnt
].value
= &value
[cnt
];
3508 cfg_data
[cnt
].xpath
= vty
->cfg_changes
[indx
].xpath
;
3510 mgmt_yang_cfg_data_req_init(&cfg_req
[cnt
]);
3511 cfg_req
[cnt
].data
= &cfg_data
[cnt
];
3512 switch (vty
->cfg_changes
[indx
].operation
) {
3514 cfg_req
[cnt
].req_type
=
3515 MGMTD__CFG_DATA_REQ_TYPE__DELETE_DATA
;
3521 case NB_OP_PRE_VALIDATE
:
3522 case NB_OP_APPLY_FINISH
:
3523 cfg_req
[cnt
].req_type
=
3524 MGMTD__CFG_DATA_REQ_TYPE__SET_DATA
;
3526 case NB_OP_GET_ELEM
:
3527 case NB_OP_GET_NEXT
:
3528 case NB_OP_GET_KEYS
:
3529 case NB_OP_LOOKUP_ENTRY
:
3531 assert(!"Invalid type of operation");
3534 assert(!"non-enum value, invalid");
3537 cfgreq
[cnt
] = &cfg_req
[cnt
];
3542 implicit_commit
= vty_needs_implicit_commit(vty
);
3543 if (cnt
&& mgmt_fe_set_config_data(
3544 mgmt_lib_hndl
, vty
->mgmt_session_id
,
3545 vty
->mgmt_req_id
, MGMTD_DS_CANDIDATE
, cfgreq
,
3546 cnt
, implicit_commit
,
3547 MGMTD_DS_RUNNING
) != MGMTD_SUCCESS
) {
3548 zlog_err("Failed to send %d Config Xpaths to MGMTD!!",
3550 vty_out(vty
, "Failed to send SETCFG-REQ to MGMTD!\n");
3554 vty
->mgmt_req_pending
= true;
3560 int vty_mgmt_send_commit_config(struct vty
*vty
, bool validate_only
, bool abort
)
3562 enum mgmt_result ret
;
3564 if (mgmt_lib_hndl
&& vty
->mgmt_session_id
) {
3566 ret
= mgmt_fe_commit_config_data(
3567 mgmt_lib_hndl
, vty
->mgmt_session_id
, vty
->mgmt_req_id
,
3568 MGMTD_DS_CANDIDATE
, MGMTD_DS_RUNNING
, validate_only
,
3570 if (ret
!= MGMTD_SUCCESS
) {
3571 zlog_err("Failed sending COMMIT-REQ req-id %" PRIu64
,
3573 vty_out(vty
, "Failed to send COMMIT-REQ to MGMTD!\n");
3577 vty
->mgmt_req_pending
= true;
3578 vty
->mgmt_num_pending_setcfg
= 0;
3584 int vty_mgmt_send_get_config(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
3585 const char **xpath_list
, int num_req
)
3587 enum mgmt_result ret
;
3588 Mgmtd__YangData yang_data
[VTY_MAXCFGCHANGES
];
3589 Mgmtd__YangGetDataReq get_req
[VTY_MAXCFGCHANGES
];
3590 Mgmtd__YangGetDataReq
*getreq
[VTY_MAXCFGCHANGES
];
3595 for (i
= 0; i
< num_req
; i
++) {
3596 mgmt_yang_get_data_req_init(&get_req
[i
]);
3597 mgmt_yang_data_init(&yang_data
[i
]);
3599 yang_data
->xpath
= (char *)xpath_list
[i
];
3601 get_req
[i
].data
= &yang_data
[i
];
3602 getreq
[i
] = &get_req
[i
];
3604 ret
= mgmt_fe_get_config_data(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3605 vty
->mgmt_req_id
, datastore
, getreq
,
3608 if (ret
!= MGMTD_SUCCESS
) {
3610 "Failed to send GET-CONFIG to MGMTD for req-id %" PRIu64
3613 vty_out(vty
, "Failed to send GET-CONFIG to MGMTD!\n");
3617 vty
->mgmt_req_pending
= true;
3622 int vty_mgmt_send_get_data(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
3623 const char **xpath_list
, int num_req
)
3625 enum mgmt_result ret
;
3626 Mgmtd__YangData yang_data
[VTY_MAXCFGCHANGES
];
3627 Mgmtd__YangGetDataReq get_req
[VTY_MAXCFGCHANGES
];
3628 Mgmtd__YangGetDataReq
*getreq
[VTY_MAXCFGCHANGES
];
3633 for (i
= 0; i
< num_req
; i
++) {
3634 mgmt_yang_get_data_req_init(&get_req
[i
]);
3635 mgmt_yang_data_init(&yang_data
[i
]);
3637 yang_data
->xpath
= (char *)xpath_list
[i
];
3639 get_req
[i
].data
= &yang_data
[i
];
3640 getreq
[i
] = &get_req
[i
];
3642 ret
= mgmt_fe_get_data(mgmt_lib_hndl
, vty
->mgmt_session_id
,
3643 vty
->mgmt_req_id
, datastore
, getreq
, num_req
);
3645 if (ret
!= MGMTD_SUCCESS
) {
3646 zlog_err("Failed to send GET-DATA to MGMTD for req-id %" PRIu64
3649 vty_out(vty
, "Failed to send GET-DATA to MGMTD!\n");
3653 vty
->mgmt_req_pending
= true;
3658 /* Install vty's own commands like `who' command. */
3659 void vty_init(struct event_loop
*master_thread
, bool do_command_logging
)
3661 /* For further configuration read, preserve current directory. */
3664 vty_master
= master_thread
;
3666 atexit(vty_stdio_atexit
);
3668 /* Install bgp top node. */
3669 install_node(&vty_node
);
3671 install_element(VIEW_NODE
, &config_who_cmd
);
3672 install_element(VIEW_NODE
, &show_history_cmd
);
3673 install_element(CONFIG_NODE
, &line_vty_cmd
);
3674 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3675 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3676 install_element(CONFIG_NODE
, &show_history_cmd
);
3677 install_element(CONFIG_NODE
, &log_commands_cmd
);
3679 if (do_command_logging
) {
3680 vty_log_commands
= true;
3681 vty_log_commands_perm
= true;
3684 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3685 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3686 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3688 install_default(VTY_NODE
);
3689 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3690 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3691 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3692 install_element(VTY_NODE
, &vty_access_class_cmd
);
3693 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3694 install_element(VTY_NODE
, &vty_login_cmd
);
3695 install_element(VTY_NODE
, &no_vty_login_cmd
);
3696 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3697 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3700 void vty_terminate(void)
3703 struct vty_serv
*vtyserv
;
3705 if (mgmt_lib_hndl
) {
3706 mgmt_fe_client_lib_destroy(mgmt_lib_hndl
);
3710 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3714 /* default state of vty_sessions is initialized & empty. */
3715 vtys_fini(vty_sessions
);
3716 vtys_init(vty_sessions
);
3718 /* vty_reset() doesn't close vtysh sessions */
3719 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3720 buffer_reset(vty
->lbuf
);
3721 buffer_reset(vty
->obuf
);
3722 vty
->status
= VTY_CLOSE
;
3726 vtys_fini(vtysh_sessions
);
3727 vtys_init(vtysh_sessions
);
3729 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
3730 EVENT_OFF(vtyserv
->t_accept
);
3731 close(vtyserv
->sock
);
3732 XFREE(MTYPE_VTY_SERV
, vtyserv
);
3735 vtyservs_fini(vty_servs
);
3736 vtyservs_init(vty_servs
);