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 PREDECL_DLIST(vtyservs
);
71 struct vtyservs_item itm
;
76 struct thread
*t_accept
;
79 DECLARE_DLIST(vtyservs
, struct vty_serv
, itm
);
81 static void vty_event_serv(enum vty_event event
, struct vty_serv
*);
82 static void vty_event(enum vty_event
, struct vty
*);
84 /* Extern host structure from command.c */
85 extern struct host host
;
87 /* active listeners */
88 static struct vtyservs_head vty_servs
[1] = {INIT_DLIST(vty_servs
[0])};
90 /* active connections */
91 static struct vtys_head vty_sessions
[1] = {INIT_DLIST(vty_sessions
[0])};
92 static struct vtys_head vtysh_sessions
[1] = {INIT_DLIST(vtysh_sessions
[0])};
94 /* Vty timeout value. */
95 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
97 /* Vty access-class command */
98 static char *vty_accesslist_name
= NULL
;
100 /* Vty access-calss for IPv6. */
101 static char *vty_ipv6_accesslist_name
= NULL
;
103 /* Current directory. */
104 static char vty_cwd
[MAXPATHLEN
];
106 /* Login password check. */
107 static int no_password_check
= 0;
109 /* Integrated configuration file path */
110 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
112 static bool do_log_commands
;
113 static bool do_log_commands_perm
;
115 void vty_frame(struct vty
*vty
, const char *format
, ...)
119 va_start(args
, format
);
120 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
121 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
122 vty
->frame_pos
= strlen(vty
->frame
);
126 void vty_endframe(struct vty
*vty
, const char *endtext
)
128 if (vty
->frame_pos
== 0 && endtext
)
129 vty_out(vty
, "%s", endtext
);
133 bool vty_set_include(struct vty
*vty
, const char *regexp
)
141 regfree(&vty
->include
);
147 errcode
= regcomp(&vty
->include
, regexp
,
148 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
151 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
152 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
160 /* VTY standard output function. */
161 int vty_out(struct vty
*vty
, const char *format
, ...)
168 /* format string may contain %m, keep errno intact for printfrr */
169 int saved_errno
= errno
;
171 if (vty
->frame_pos
) {
173 vty_out(vty
, "%s", vty
->frame
);
176 va_start(args
, format
);
178 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
185 vector lines
= frrstr_split_vec(p
, "\n");
187 /* Place first value in the cache */
188 char *firstline
= vector_slot(lines
, 0);
189 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
191 /* If our split returned more than one entry, time to filter */
192 if (vector_active(lines
) > 1) {
194 * returned string is MTYPE_TMP so it matches the MTYPE
195 * of everything else in the vector
197 char *bstr
= buffer_getstr(vty
->lbuf
);
198 buffer_reset(vty
->lbuf
);
199 XFREE(MTYPE_TMP
, lines
->index
[0]);
200 vector_set_index(lines
, 0, bstr
);
201 frrstr_filter_vec(lines
, &vty
->include
);
202 vector_compact(lines
);
204 * Consider the string "foo\n". If the regex is an empty string
205 * and the line ended with a newline, then the vector will look
211 * If the regex isn't empty, the vector will look like:
215 * In this case we'd like to preserve the newline, so we add
216 * the empty string [1] as in the first example.
218 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
219 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
220 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
222 filtered
= frrstr_join_vec(lines
, "\n");
228 frrstr_strvec_free(lines
);
239 /* print with crlf replacement */
240 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
245 fprintf(vty
->of
, "%s", filtered
);
247 } else if (vty
->of_saved
) {
248 fprintf(vty
->of_saved
, "%s", filtered
);
249 fflush(vty
->of_saved
);
255 /* print without crlf replacement */
256 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
262 if (vty
->filter
&& filtered
)
263 XFREE(MTYPE_TMP
, filtered
);
265 /* If p is not different with buf, it is allocated buffer. */
267 XFREE(MTYPE_VTY_OUT_BUF
, p
);
272 static int vty_json_helper(struct vty
*vty
, struct json_object
*json
,
280 text
= json_object_to_json_string_ext(
282 vty_out(vty
, "%s\n", text
);
283 json_object_free(json
);
288 int vty_json(struct vty
*vty
, struct json_object
*json
)
290 return vty_json_helper(vty
, json
,
291 JSON_C_TO_STRING_PRETTY
|
292 JSON_C_TO_STRING_NOSLASHESCAPE
);
295 int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
)
297 return vty_json_helper(vty
, json
, JSON_C_TO_STRING_NOSLASHESCAPE
);
300 /* Output current time to the vty. */
301 void vty_time_print(struct vty
*vty
, int cr
)
303 char buf
[FRR_TIMESTAMP_LEN
];
305 if (frr_timestamp(0, buf
, sizeof(buf
)) == 0) {
306 zlog_info("frr_timestamp error");
310 vty_out(vty
, "%s\n", buf
);
312 vty_out(vty
, "%s ", buf
);
317 /* Say hello to vty interface. */
318 void vty_hello(struct vty
*vty
)
324 f
= fopen(host
.motdfile
, "r");
326 while (fgets(buf
, sizeof(buf
), f
)) {
328 /* work backwards to ignore trailling isspace()
330 for (s
= buf
+ strlen(buf
);
331 (s
> buf
) && isspace((unsigned char)s
[-1]);
335 vty_out(vty
, "%s\n", buf
);
339 vty_out(vty
, "MOTD file not found\n");
340 } else if (host
.motd
)
341 vty_out(vty
, "%s", host
.motd
);
344 #pragma GCC diagnostic push
345 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
346 /* prompt formatting has a %s in the cmd_node prompt string.
348 * Also for some reason GCC emits the warning on the end of the function
349 * (optimization maybe?) rather than on the vty_out line, so this pragma
350 * wraps the entire function rather than just the vty_out line.
353 /* Put out prompt and wait input from user. */
354 static void vty_prompt(struct vty
*vty
)
356 if (vty
->type
== VTY_TERM
) {
357 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
360 #pragma GCC diagnostic pop
362 /* Send WILL TELOPT_ECHO to remote server. */
363 static void vty_will_echo(struct vty
*vty
)
365 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
366 vty_out(vty
, "%s", cmd
);
369 /* Make suppress Go-Ahead telnet option. */
370 static void vty_will_suppress_go_ahead(struct vty
*vty
)
372 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
373 vty_out(vty
, "%s", cmd
);
376 /* Make don't use linemode over telnet. */
377 static void vty_dont_linemode(struct vty
*vty
)
379 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
380 vty_out(vty
, "%s", cmd
);
383 /* Use window size. */
384 static void vty_do_window_size(struct vty
*vty
)
386 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
387 vty_out(vty
, "%s", cmd
);
390 /* Authentication of vty */
391 static void vty_auth(struct vty
*vty
, char *buf
)
394 enum node_type next_node
= 0;
400 passwd
= host
.password_encrypt
;
402 passwd
= host
.password
;
404 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
406 next_node
= VIEW_NODE
;
408 case AUTH_ENABLE_NODE
:
410 passwd
= host
.enable_encrypt
;
412 passwd
= host
.enable
;
413 next_node
= ENABLE_NODE
;
419 fail
= strcmp(crypt(buf
, passwd
), passwd
);
421 fail
= strcmp(buf
, passwd
);
427 vty
->node
= next_node
; /* Success ! */
430 if (vty
->fail
>= 3) {
431 if (vty
->node
== AUTH_NODE
) {
433 "%% Bad passwords, too many failures!\n");
434 vty
->status
= VTY_CLOSE
;
436 /* AUTH_ENABLE_NODE */
439 "%% Bad enable passwords, too many failures!\n");
440 vty
->status
= VTY_CLOSE
;
446 /* Command execution over the vty interface. */
447 static int vty_command(struct vty
*vty
, char *buf
)
450 const char *protocolname
;
456 * Log non empty command lines
458 if (do_log_commands
&&
459 strncmp(buf
, "echo PING", strlen("echo PING")) != 0)
462 /* Skip white spaces. */
463 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
466 if (cp
!= NULL
&& *cp
!= '\0') {
467 char vty_str
[VTY_BUFSIZ
];
468 char prompt_str
[VTY_BUFSIZ
];
470 /* format the base vty info */
471 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s", vty
->fd
,
474 /* format the prompt */
475 #pragma GCC diagnostic push
476 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
477 /* prompt formatting has a %s in the cmd_node prompt string */
478 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
480 #pragma GCC diagnostic pop
482 /* now log the command */
483 zlog_notice("%s%s", prompt_str
, buf
);
488 unsigned long walltime
, cputime
;
490 /* cmd_execute() may change cputime_enabled if we're executing the
491 * "service cputime-stats" command, which can result in nonsensical
492 * and very confusing warnings
494 bool cputime_enabled_here
= cputime_enabled
;
498 ret
= cmd_execute(vty
, buf
, NULL
, 0);
502 walltime
= thread_consumed_time(&after
, &before
, &cputime
);
504 if (cputime_enabled_here
&& cputime_enabled
&& cputime_threshold
505 && cputime
> cputime_threshold
)
506 /* Warn about CPU hog that must be fixed. */
507 flog_warn(EC_LIB_SLOW_THREAD_CPU
,
508 "CPU HOG: command took %lums (cpu time %lums): %s",
509 walltime
/ 1000, cputime
/ 1000, buf
);
510 else if (walltime_threshold
&& walltime
> walltime_threshold
)
511 flog_warn(EC_LIB_SLOW_THREAD_WALL
,
512 "STARVATION: command took %lums (cpu time %lums): %s",
513 walltime
/ 1000, cputime
/ 1000, buf
);
515 /* Get the name of the protocol if any */
516 protocolname
= frr_protoname
;
518 if (ret
!= CMD_SUCCESS
)
521 if (vty
->type
== VTY_FILE
)
522 vty_out(vty
, "Warning...\n");
524 case CMD_ERR_AMBIGUOUS
:
525 vty_out(vty
, "%% Ambiguous command.\n");
527 case CMD_ERR_NO_MATCH
:
528 vty_out(vty
, "%% [%s] Unknown command: %s\n",
531 case CMD_ERR_INCOMPLETE
:
532 vty_out(vty
, "%% Command incomplete.\n");
539 static const char telnet_backward_char
= 0x08;
540 static const char telnet_space_char
= ' ';
542 /* Basic function to write buffer to vty. */
543 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
545 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
548 /* Should we do buffering here ? And make vty_flush (vty) ? */
549 buffer_put(vty
->obuf
, buf
, nbytes
);
552 /* Basic function to insert character into vty. */
553 static void vty_self_insert(struct vty
*vty
, char c
)
558 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
561 length
= vty
->length
- vty
->cp
;
562 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
563 vty
->buf
[vty
->cp
] = c
;
565 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
566 for (i
= 0; i
< length
; i
++)
567 vty_write(vty
, &telnet_backward_char
, 1);
572 vty
->buf
[vty
->length
] = '\0';
575 /* Self insert character 'c' in overwrite mode. */
576 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
578 if (vty
->cp
== vty
->length
) {
579 vty_self_insert(vty
, c
);
583 vty
->buf
[vty
->cp
++] = c
;
584 vty_write(vty
, &c
, 1);
588 * Insert a string into vty->buf at the current cursor position.
590 * If the resultant string would be larger than VTY_BUFSIZ it is
593 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
595 if (vty
->cp
== VTY_BUFSIZ
)
598 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
599 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
601 vty
->length
= MAX(vty
->cp
, vty
->length
);
602 vty
->buf
[vty
->length
] = '\0';
603 vty_write(vty
, str
, nwrite
);
606 /* Forward character. */
607 static void vty_forward_char(struct vty
*vty
)
609 if (vty
->cp
< vty
->length
) {
610 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
615 /* Backward character. */
616 static void vty_backward_char(struct vty
*vty
)
620 vty_write(vty
, &telnet_backward_char
, 1);
624 /* Move to the beginning of the line. */
625 static void vty_beginning_of_line(struct vty
*vty
)
628 vty_backward_char(vty
);
631 /* Move to the end of the line. */
632 static void vty_end_of_line(struct vty
*vty
)
634 while (vty
->cp
< vty
->length
)
635 vty_forward_char(vty
);
638 static void vty_kill_line_from_beginning(struct vty
*);
639 static void vty_redraw_line(struct vty
*);
641 /* Print command line history. This function is called from
642 vty_next_line and vty_previous_line. */
643 static void vty_history_print(struct vty
*vty
)
647 vty_kill_line_from_beginning(vty
);
649 /* Get previous line from history buffer */
650 length
= strlen(vty
->hist
[vty
->hp
]);
651 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
652 vty
->cp
= vty
->length
= length
;
653 vty
->buf
[vty
->length
] = '\0';
655 /* Redraw current line */
656 vty_redraw_line(vty
);
659 /* Show next command line history. */
660 static void vty_next_line(struct vty
*vty
)
664 if (vty
->hp
== vty
->hindex
)
667 /* Try is there history exist or not. */
669 if (try_index
== (VTY_MAXHIST
- 1))
674 /* If there is not history return. */
675 if (vty
->hist
[try_index
] == NULL
)
680 vty_history_print(vty
);
683 /* Show previous command line history. */
684 static void vty_previous_line(struct vty
*vty
)
690 try_index
= VTY_MAXHIST
- 1;
694 if (vty
->hist
[try_index
] == NULL
)
699 vty_history_print(vty
);
702 /* This function redraw all of the command line character. */
703 static void vty_redraw_line(struct vty
*vty
)
705 vty_write(vty
, vty
->buf
, vty
->length
);
706 vty
->cp
= vty
->length
;
710 static void vty_forward_word(struct vty
*vty
)
712 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
713 vty_forward_char(vty
);
715 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
716 vty_forward_char(vty
);
719 /* Backward word without skipping training space. */
720 static void vty_backward_pure_word(struct vty
*vty
)
722 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
723 vty_backward_char(vty
);
727 static void vty_backward_word(struct vty
*vty
)
729 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
730 vty_backward_char(vty
);
732 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
733 vty_backward_char(vty
);
736 /* When '^D' is typed at the beginning of the line we move to the down
738 static void vty_down_level(struct vty
*vty
)
746 /* When '^Z' is received from vty, move down to the enable mode. */
747 static void vty_end_config(struct vty
*vty
)
752 vty_config_exit(vty
);
753 vty
->node
= ENABLE_NODE
;
760 /* Delete a character at the current point. */
761 static void vty_delete_char(struct vty
*vty
)
766 if (vty
->length
== 0) {
771 if (vty
->cp
== vty
->length
)
772 return; /* completion need here? */
774 size
= vty
->length
- vty
->cp
;
777 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
778 vty
->buf
[vty
->length
] = '\0';
780 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
783 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
784 vty_write(vty
, &telnet_space_char
, 1);
786 for (i
= 0; i
< size
; i
++)
787 vty_write(vty
, &telnet_backward_char
, 1);
790 /* Delete a character before the point. */
791 static void vty_delete_backward_char(struct vty
*vty
)
796 vty_backward_char(vty
);
797 vty_delete_char(vty
);
800 /* Kill rest of line from current point. */
801 static void vty_kill_line(struct vty
*vty
)
806 size
= vty
->length
- vty
->cp
;
811 for (i
= 0; i
< size
; i
++)
812 vty_write(vty
, &telnet_space_char
, 1);
813 for (i
= 0; i
< size
; i
++)
814 vty_write(vty
, &telnet_backward_char
, 1);
816 memset(&vty
->buf
[vty
->cp
], 0, size
);
817 vty
->length
= vty
->cp
;
820 /* Kill line from the beginning. */
821 static void vty_kill_line_from_beginning(struct vty
*vty
)
823 vty_beginning_of_line(vty
);
827 /* Delete a word before the point. */
828 static void vty_forward_kill_word(struct vty
*vty
)
830 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
831 vty_delete_char(vty
);
832 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
833 vty_delete_char(vty
);
836 /* Delete a word before the point. */
837 static void vty_backward_kill_word(struct vty
*vty
)
839 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
840 vty_delete_backward_char(vty
);
841 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
842 vty_delete_backward_char(vty
);
845 /* Transpose chars before or at the point. */
846 static void vty_transpose_chars(struct vty
*vty
)
850 /* If length is short or point is near by the beginning of line then
852 if (vty
->length
< 2 || vty
->cp
< 1)
855 /* In case of point is located at the end of the line. */
856 if (vty
->cp
== vty
->length
) {
857 c1
= vty
->buf
[vty
->cp
- 1];
858 c2
= vty
->buf
[vty
->cp
- 2];
860 vty_backward_char(vty
);
861 vty_backward_char(vty
);
862 vty_self_insert_overwrite(vty
, c1
);
863 vty_self_insert_overwrite(vty
, c2
);
865 c1
= vty
->buf
[vty
->cp
];
866 c2
= vty
->buf
[vty
->cp
- 1];
868 vty_backward_char(vty
);
869 vty_self_insert_overwrite(vty
, c1
);
870 vty_self_insert_overwrite(vty
, c2
);
874 /* Do completion at vty interface. */
875 static void vty_complete_command(struct vty
*vty
)
879 char **matched
= NULL
;
882 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
885 vline
= cmd_make_strvec(vty
->buf
);
889 /* In case of 'help \t'. */
890 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
891 vector_set(vline
, NULL
);
893 matched
= cmd_complete_command(vline
, vty
, &ret
);
895 cmd_free_strvec(vline
);
899 case CMD_ERR_AMBIGUOUS
:
900 vty_out(vty
, "%% Ambiguous command.\n");
902 vty_redraw_line(vty
);
904 case CMD_ERR_NO_MATCH
:
905 /* vty_out (vty, "%% There is no matched command.\n"); */
907 vty_redraw_line(vty
);
909 case CMD_COMPLETE_FULL_MATCH
:
911 /* 2016-11-28 equinox -- need to debug, SEGV here */
912 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
914 vty_redraw_line(vty
);
918 vty_redraw_line(vty
);
919 vty_backward_pure_word(vty
);
920 vty_insert_word_overwrite(vty
, matched
[0]);
921 vty_self_insert(vty
, ' ');
922 XFREE(MTYPE_COMPLETION
, matched
[0]);
924 case CMD_COMPLETE_MATCH
:
926 vty_redraw_line(vty
);
927 vty_backward_pure_word(vty
);
928 vty_insert_word_overwrite(vty
, matched
[0]);
929 XFREE(MTYPE_COMPLETION
, matched
[0]);
931 case CMD_COMPLETE_LIST_MATCH
:
932 for (i
= 0; matched
[i
] != NULL
; i
++) {
933 if (i
!= 0 && ((i
% 6) == 0))
935 vty_out(vty
, "%-10s ", matched
[i
]);
936 XFREE(MTYPE_COMPLETION
, matched
[i
]);
941 vty_redraw_line(vty
);
943 case CMD_ERR_NOTHING_TODO
:
945 vty_redraw_line(vty
);
950 XFREE(MTYPE_TMP
, matched
);
953 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
954 unsigned int desc_width
, struct cmd_token
*token
)
962 if (desc_width
<= 0) {
963 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
967 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
969 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
970 for (pos
= desc_width
; pos
> 0; pos
--)
971 if (*(p
+ pos
) == ' ')
979 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
984 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
986 XFREE(MTYPE_TMP
, buf
);
989 /* Describe matched command function. */
990 static void vty_describe_command(struct vty
*vty
)
995 unsigned int i
, width
, desc_width
;
996 struct cmd_token
*token
, *token_cr
= NULL
;
998 vline
= cmd_make_strvec(vty
->buf
);
1000 /* In case of '> ?'. */
1001 if (vline
== NULL
) {
1002 vline
= vector_init(1);
1003 vector_set(vline
, NULL
);
1004 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1005 vector_set(vline
, NULL
);
1007 describe
= cmd_describe_command(vline
, vty
, &ret
);
1011 /* Ambiguous error. */
1013 case CMD_ERR_AMBIGUOUS
:
1014 vty_out(vty
, "%% Ambiguous command.\n");
1017 case CMD_ERR_NO_MATCH
:
1018 vty_out(vty
, "%% There is no matched command.\n");
1023 /* Get width of command string. */
1025 for (i
= 0; i
< vector_active(describe
); i
++)
1026 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1029 if (token
->text
[0] == '\0')
1032 len
= strlen(token
->text
);
1038 /* Get width of description string. */
1039 desc_width
= vty
->width
- (width
+ 6);
1041 /* Print out description. */
1042 for (i
= 0; i
< vector_active(describe
); i
++)
1043 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1044 if (token
->text
[0] == '\0')
1047 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1053 vty_out(vty
, " %-s\n", token
->text
);
1054 else if (desc_width
>= strlen(token
->desc
))
1055 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1058 vty_describe_fold(vty
, width
, desc_width
,
1061 if (IS_VARYING_TOKEN(token
->type
)) {
1062 const char *ref
= vector_slot(
1063 vline
, vector_active(vline
) - 1);
1065 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1066 cmd_variable_complete(token
, ref
, varcomps
);
1068 if (vector_active(varcomps
) > 0) {
1069 char *ac
= cmd_variable_comp2str(
1070 varcomps
, vty
->width
);
1071 vty_out(vty
, "%s\n", ac
);
1072 XFREE(MTYPE_TMP
, ac
);
1075 vector_free(varcomps
);
1079 if ((token
= token_cr
)) {
1081 vty_out(vty
, " %-s\n", token
->text
);
1082 else if (desc_width
>= strlen(token
->desc
))
1083 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1086 vty_describe_fold(vty
, width
, desc_width
, token
);
1090 cmd_free_strvec(vline
);
1092 vector_free(describe
);
1095 vty_redraw_line(vty
);
1098 static void vty_clear_buf(struct vty
*vty
)
1100 memset(vty
->buf
, 0, vty
->max
);
1103 /* ^C stop current input and do not add command line to the history. */
1104 static void vty_stop_input(struct vty
*vty
)
1106 vty
->cp
= vty
->length
= 0;
1111 vty_config_exit(vty
);
1112 vty
->node
= ENABLE_NODE
;
1117 /* Set history pointer to the latest one. */
1118 vty
->hp
= vty
->hindex
;
1121 /* Add current command line to the history buffer. */
1122 static void vty_hist_add(struct vty
*vty
)
1126 if (vty
->length
== 0)
1129 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1131 /* Ignore the same string as previous one. */
1132 if (vty
->hist
[index
])
1133 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1134 vty
->hp
= vty
->hindex
;
1138 /* Insert history entry. */
1139 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1140 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1142 /* History index rotation. */
1144 if (vty
->hindex
== VTY_MAXHIST
)
1147 vty
->hp
= vty
->hindex
;
1150 /* #define TELNET_OPTION_DEBUG */
1152 /* Get telnet window size. */
1153 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1155 #ifdef TELNET_OPTION_DEBUG
1158 for (i
= 0; i
< nbytes
; i
++) {
1161 vty_out(vty
, "IAC ");
1164 vty_out(vty
, "WILL ");
1167 vty_out(vty
, "WONT ");
1170 vty_out(vty
, "DO ");
1173 vty_out(vty
, "DONT ");
1176 vty_out(vty
, "SB ");
1179 vty_out(vty
, "SE ");
1182 vty_out(vty
, "TELOPT_ECHO \n");
1185 vty_out(vty
, "TELOPT_SGA \n");
1188 vty_out(vty
, "TELOPT_NAWS \n");
1191 vty_out(vty
, "%x ", buf
[i
]);
1197 #endif /* TELNET_OPTION_DEBUG */
1202 vty
->iac_sb_in_progress
= 1;
1205 if (!vty
->iac_sb_in_progress
)
1208 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1209 vty
->iac_sb_in_progress
= 0;
1212 switch (vty
->sb_buf
[0]) {
1214 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1217 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1219 (unsigned long)vty
->sb_len
);
1220 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1223 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1224 (unsigned long)sizeof(vty
->sb_buf
),
1225 TELNET_NAWS_SB_LEN
);
1227 vty
->width
= ((vty
->sb_buf
[1] << 8)
1229 vty
->height
= ((vty
->sb_buf
[3] << 8)
1231 #ifdef TELNET_OPTION_DEBUG
1233 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1234 vty
->width
, vty
->height
);
1239 vty
->iac_sb_in_progress
= 0;
1248 /* Execute current command line. */
1249 static int vty_execute(struct vty
*vty
)
1255 switch (vty
->node
) {
1257 case AUTH_ENABLE_NODE
:
1258 vty_auth(vty
, vty
->buf
);
1261 ret
= vty_command(vty
, vty
->buf
);
1262 if (vty
->type
== VTY_TERM
)
1267 /* Clear command line buffer. */
1268 vty
->cp
= vty
->length
= 0;
1271 if (vty
->status
!= VTY_CLOSE
)
1277 #define CONTROL(X) ((X) - '@')
1278 #define VTY_NORMAL 0
1279 #define VTY_PRE_ESCAPE 1
1280 #define VTY_ESCAPE 2
1283 /* Escape character command map. */
1284 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1288 vty_previous_line(vty
);
1294 vty_forward_char(vty
);
1297 vty_backward_char(vty
);
1303 /* Go back to normal mode. */
1304 vty
->escape
= VTY_NORMAL
;
1307 /* Quit print out to the buffer. */
1308 static void vty_buffer_reset(struct vty
*vty
)
1310 buffer_reset(vty
->obuf
);
1311 buffer_reset(vty
->lbuf
);
1313 vty_redraw_line(vty
);
1316 /* Read data via vty socket. */
1317 static void vty_read(struct thread
*thread
)
1321 unsigned char buf
[VTY_READ_BUFSIZ
];
1323 struct vty
*vty
= THREAD_ARG(thread
);
1325 /* Read raw data from socket */
1326 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1328 if (ERRNO_IO_RETRY(errno
)) {
1329 vty_event(VTY_READ
, vty
);
1334 "%s: read error on vty client fd %d, closing: %s",
1335 __func__
, vty
->fd
, safe_strerror(errno
));
1336 buffer_reset(vty
->obuf
);
1337 buffer_reset(vty
->lbuf
);
1339 vty
->status
= VTY_CLOSE
;
1342 for (i
= 0; i
< nbytes
; i
++) {
1343 if (buf
[i
] == IAC
) {
1352 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1353 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1354 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1360 /* In case of telnet command */
1362 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1369 if (vty
->status
== VTY_MORE
) {
1374 vty_buffer_reset(vty
);
1382 /* Escape character. */
1383 if (vty
->escape
== VTY_ESCAPE
) {
1384 vty_escape_map(buf
[i
], vty
);
1388 /* Pre-escape status. */
1389 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1392 vty
->escape
= VTY_ESCAPE
;
1395 vty_backward_word(vty
);
1396 vty
->escape
= VTY_NORMAL
;
1399 vty_forward_word(vty
);
1400 vty
->escape
= VTY_NORMAL
;
1403 vty_forward_kill_word(vty
);
1404 vty
->escape
= VTY_NORMAL
;
1408 vty_backward_kill_word(vty
);
1409 vty
->escape
= VTY_NORMAL
;
1412 vty
->escape
= VTY_NORMAL
;
1418 if (vty
->escape
== VTY_CR
) {
1419 /* if we get CR+NL, the NL results in an extra empty
1420 * prompt line being printed without this; just drop
1421 * the NL if it immediately follows CR.
1423 vty
->escape
= VTY_NORMAL
;
1431 vty_beginning_of_line(vty
);
1434 vty_backward_char(vty
);
1437 vty_stop_input(vty
);
1440 vty_delete_char(vty
);
1443 vty_end_of_line(vty
);
1446 vty_forward_char(vty
);
1450 vty_delete_backward_char(vty
);
1459 vty_previous_line(vty
);
1462 vty_transpose_chars(vty
);
1465 vty_kill_line_from_beginning(vty
);
1468 vty_backward_kill_word(vty
);
1471 vty_end_config(vty
);
1474 vty
->escape
= VTY_CR
;
1478 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1481 if (vty
->pass_fd
!= -1) {
1482 close(vty
->pass_fd
);
1487 vty_complete_command(vty
);
1490 if (vty
->node
== AUTH_NODE
1491 || vty
->node
== AUTH_ENABLE_NODE
)
1492 vty_self_insert(vty
, buf
[i
]);
1494 vty_describe_command(vty
);
1497 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1498 vty
->escape
= VTY_ESCAPE
;
1501 vty
->escape
= VTY_PRE_ESCAPE
;
1504 if (buf
[i
] > 31 && buf
[i
] < 127)
1505 vty_self_insert(vty
, buf
[i
]);
1511 if (vty
->status
== VTY_CLOSE
)
1514 vty_event(VTY_WRITE
, vty
);
1515 vty_event(VTY_READ
, vty
);
1519 /* Flush buffer to the vty. */
1520 static void vty_flush(struct thread
*thread
)
1523 buffer_status_t flushrc
;
1524 struct vty
*vty
= THREAD_ARG(thread
);
1526 /* Tempolary disable read thread. */
1527 if (vty
->lines
== 0)
1528 THREAD_OFF(vty
->t_read
);
1530 /* Function execution continue. */
1531 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1533 /* N.B. if width is 0, that means we don't know the window size. */
1534 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1535 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1536 else if (vty
->status
== VTY_MORELINE
)
1537 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1540 flushrc
= buffer_flush_window(
1541 vty
->obuf
, vty
->wfd
, vty
->width
,
1542 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1545 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1547 buffer_reset(vty
->lbuf
);
1548 buffer_reset(vty
->obuf
);
1552 if (vty
->status
== VTY_CLOSE
)
1555 vty
->status
= VTY_NORMAL
;
1556 if (vty
->lines
== 0)
1557 vty_event(VTY_READ
, vty
);
1560 case BUFFER_PENDING
:
1561 /* There is more data waiting to be written. */
1562 vty
->status
= VTY_MORE
;
1563 if (vty
->lines
== 0)
1564 vty_event(VTY_WRITE
, vty
);
1569 /* Allocate new vty struct. */
1570 struct vty
*vty_new(void)
1572 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1574 new->fd
= new->wfd
= -1;
1576 new->lbuf
= buffer_new(0);
1577 new->obuf
= buffer_new(0); /* Use default buffer size. */
1578 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1579 new->max
= VTY_BUFSIZ
;
1586 /* allocate and initialise vty */
1587 static struct vty
*vty_new_init(int vty_sock
)
1593 vty
->wfd
= vty_sock
;
1594 vty
->type
= VTY_TERM
;
1595 vty
->node
= AUTH_NODE
;
1600 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1603 vty
->xpath_index
= 0;
1604 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1605 vty
->private_config
= false;
1606 vty
->candidate_config
= vty_shared_candidate_config
;
1607 vty
->status
= VTY_NORMAL
;
1610 vty
->iac_sb_in_progress
= 0;
1613 vtys_add_tail(vty_sessions
, vty
);
1618 /* Create new vty structure. */
1619 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1621 char buf
[SU_ADDRSTRLEN
];
1624 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1626 /* Allocate new vty structure and set up default values. */
1627 vty
= vty_new_init(vty_sock
);
1629 /* configurable parameters not part of basic init */
1630 vty
->v_timeout
= vty_timeout_val
;
1631 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1632 if (no_password_check
) {
1634 vty
->node
= ENABLE_NODE
;
1636 vty
->node
= VIEW_NODE
;
1638 if (host
.lines
>= 0)
1639 vty
->lines
= host
.lines
;
1641 if (!no_password_check
) {
1642 /* Vty is not available if password isn't set. */
1643 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1644 vty_out(vty
, "Vty password is not set.\n");
1645 vty
->status
= VTY_CLOSE
;
1651 /* Say hello to the world. */
1653 if (!no_password_check
)
1654 vty_out(vty
, "\nUser Access Verification\n\n");
1656 /* Setting up terminal. */
1658 vty_will_suppress_go_ahead(vty
);
1660 vty_dont_linemode(vty
);
1661 vty_do_window_size(vty
);
1662 /* vty_dont_lflow_ahead (vty); */
1666 /* Add read/write thread. */
1667 vty_event(VTY_WRITE
, vty
);
1668 vty_event(VTY_READ
, vty
);
1673 /* create vty for stdio */
1674 static struct termios stdio_orig_termios
;
1675 static struct vty
*stdio_vty
= NULL
;
1676 static bool stdio_termios
= false;
1677 static void (*stdio_vty_atclose
)(int isexit
);
1679 static void vty_stdio_reset(int isexit
)
1683 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1684 stdio_termios
= false;
1688 if (stdio_vty_atclose
)
1689 stdio_vty_atclose(isexit
);
1690 stdio_vty_atclose
= NULL
;
1694 static void vty_stdio_atexit(void)
1699 void vty_stdio_suspend(void)
1704 THREAD_OFF(stdio_vty
->t_write
);
1705 THREAD_OFF(stdio_vty
->t_read
);
1706 THREAD_OFF(stdio_vty
->t_timeout
);
1709 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1710 stdio_termios
= false;
1713 void vty_stdio_resume(void)
1718 if (!tcgetattr(0, &stdio_orig_termios
)) {
1719 struct termios termios
;
1721 termios
= stdio_orig_termios
;
1722 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1723 | IGNCR
| ICRNL
| IXON
);
1724 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1725 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1726 termios
.c_cflag
|= CS8
;
1727 tcsetattr(0, TCSANOW
, &termios
);
1728 stdio_termios
= true;
1731 vty_prompt(stdio_vty
);
1733 /* Add read/write thread. */
1734 vty_event(VTY_WRITE
, stdio_vty
);
1735 vty_event(VTY_READ
, stdio_vty
);
1738 void vty_stdio_close(void)
1742 vty_close(stdio_vty
);
1745 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1749 /* refuse creating two vtys on stdio */
1753 vty
= stdio_vty
= vty_new_init(0);
1754 stdio_vty_atclose
= atclose
;
1757 /* always have stdio vty in a known _unchangeable_ state, don't want
1759 * to have any effect here to make sure scripting this works as intended
1761 vty
->node
= ENABLE_NODE
;
1763 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1769 /* Accept connection from the network. */
1770 static void vty_accept(struct thread
*thread
)
1772 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1777 int accept_sock
= vtyserv
->sock
;
1779 struct access_list
*acl
= NULL
;
1781 /* We continue hearing vty socket. */
1782 vty_event_serv(VTY_SERV
, vtyserv
);
1784 memset(&su
, 0, sizeof(union sockunion
));
1786 /* We can handle IPv4 or IPv6 socket. */
1787 vty_sock
= sockunion_accept(accept_sock
, &su
);
1789 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1790 safe_strerror(errno
));
1793 set_nonblocking(vty_sock
);
1794 set_cloexec(vty_sock
);
1796 if (!sockunion2hostprefix(&su
, &p
)) {
1798 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1803 /* VTY's accesslist apply. */
1804 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1805 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1806 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1807 zlog_info("Vty connection refused from %pSU", &su
);
1813 /* VTY's ipv6 accesslist apply. */
1814 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1815 if ((acl
= access_list_lookup(AFI_IP6
,
1816 vty_ipv6_accesslist_name
))
1817 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1818 zlog_info("Vty connection refused from %pSU", &su
);
1825 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1828 zlog_info("can't set sockopt to vty_sock : %s",
1829 safe_strerror(errno
));
1831 zlog_info("Vty connection from %pSU", &su
);
1833 vty_create(vty_sock
, &su
);
1836 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1839 struct addrinfo req
;
1840 struct addrinfo
*ainfo
;
1841 struct addrinfo
*ainfo_save
;
1843 char port_str
[BUFSIZ
];
1845 memset(&req
, 0, sizeof(req
));
1846 req
.ai_flags
= AI_PASSIVE
;
1847 req
.ai_family
= AF_UNSPEC
;
1848 req
.ai_socktype
= SOCK_STREAM
;
1849 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1850 port_str
[sizeof(port_str
) - 1] = '\0';
1852 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1855 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1863 struct vty_serv
*vtyserv
;
1865 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1868 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1869 ainfo
->ai_protocol
);
1873 sockopt_v6only(ainfo
->ai_family
, sock
);
1874 sockopt_reuseaddr(sock
);
1875 sockopt_reuseport(sock
);
1878 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1880 close(sock
); /* Avoid sd leak. */
1884 ret
= listen(sock
, 3);
1886 close(sock
); /* Avoid sd leak. */
1890 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1891 vtyserv
->sock
= sock
;
1892 vtyservs_add_tail(vty_servs
, vtyserv
);
1894 vty_event_serv(VTY_SERV
, vtyserv
);
1895 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1897 freeaddrinfo(ainfo_save
);
1901 /* For sockaddr_un. */
1904 /* VTY shell UNIX domain socket. */
1905 static void vty_serv_un(const char *path
)
1907 struct vty_serv
*vtyserv
;
1910 struct sockaddr_un serv
;
1912 struct zprivs_ids_t ids
;
1914 /* First of all, unlink existing socket */
1918 old_mask
= umask(0007);
1920 /* Make UNIX domain socket. */
1921 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1923 flog_err_sys(EC_LIB_SOCKET
,
1924 "Cannot create unix stream socket: %s",
1925 safe_strerror(errno
));
1929 /* Make server socket. */
1930 memset(&serv
, 0, sizeof(serv
));
1931 serv
.sun_family
= AF_UNIX
;
1932 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1933 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1934 len
= serv
.sun_len
= SUN_LEN(&serv
);
1936 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1937 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1941 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1943 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1944 safe_strerror(errno
));
1945 close(sock
); /* Avoid sd leak. */
1949 ret
= listen(sock
, 5);
1951 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1952 safe_strerror(errno
));
1953 close(sock
); /* Avoid sd leak. */
1959 zprivs_get_ids(&ids
);
1961 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1962 earlier for the case when we don't need to chown the file
1963 type casting it here to make a compare */
1964 if ((int)ids
.gid_vty
> 0) {
1965 /* set group of socket */
1966 if (chown(path
, -1, ids
.gid_vty
)) {
1967 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1968 "vty_serv_un: could chown socket, %s",
1969 safe_strerror(errno
));
1973 vtyserv
= XCALLOC(MTYPE_VTY_SERV
, sizeof(*vtyserv
));
1974 vtyserv
->sock
= sock
;
1975 vtyserv
->vtysh
= true;
1976 vtyservs_add_tail(vty_servs
, vtyserv
);
1978 vty_event_serv(VTYSH_SERV
, vtyserv
);
1981 /* #define VTYSH_DEBUG 1 */
1983 static void vtysh_accept(struct thread
*thread
)
1985 struct vty_serv
*vtyserv
= THREAD_ARG(thread
);
1986 int accept_sock
= vtyserv
->sock
;
1989 struct sockaddr_un client
;
1992 vty_event_serv(VTYSH_SERV
, vtyserv
);
1994 memset(&client
, 0, sizeof(client
));
1995 client_len
= sizeof(struct sockaddr_un
);
1997 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
1998 (socklen_t
*)&client_len
);
2001 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2002 safe_strerror(errno
));
2006 if (set_nonblocking(sock
) < 0) {
2009 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2010 sock
, safe_strerror(errno
));
2017 printf("VTY shell accept\n");
2018 #endif /* VTYSH_DEBUG */
2023 vty
->type
= VTY_SHELL_SERV
;
2024 vty
->node
= VIEW_NODE
;
2025 vtys_add_tail(vtysh_sessions
, vty
);
2027 vty_event(VTYSH_READ
, vty
);
2030 static int vtysh_do_pass_fd(struct vty
*vty
)
2032 struct iovec iov
[1] = {
2034 .iov_base
= vty
->pass_fd_status
,
2035 .iov_len
= sizeof(vty
->pass_fd_status
),
2039 uint8_t buf
[CMSG_SPACE(sizeof(int))];
2040 struct cmsghdr align
;
2042 struct msghdr mh
= {
2044 .msg_iovlen
= array_size(iov
),
2045 .msg_control
= u
.buf
,
2046 .msg_controllen
= sizeof(u
.buf
),
2048 struct cmsghdr
*cmh
= CMSG_FIRSTHDR(&mh
);
2051 memset(&u
.buf
, 0, sizeof(u
.buf
));
2052 cmh
->cmsg_level
= SOL_SOCKET
;
2053 cmh
->cmsg_type
= SCM_RIGHTS
;
2054 cmh
->cmsg_len
= CMSG_LEN(sizeof(int));
2055 memcpy(CMSG_DATA(cmh
), &vty
->pass_fd
, sizeof(int));
2057 ret
= sendmsg(vty
->wfd
, &mh
, 0);
2058 if (ret
< 0 && ERRNO_IO_RETRY(errno
))
2059 return BUFFER_PENDING
;
2061 close(vty
->pass_fd
);
2063 vty
->status
= VTY_NORMAL
;
2066 return BUFFER_ERROR
;
2068 /* resume accepting commands (suspended in vtysh_read) */
2069 vty_event(VTYSH_READ
, vty
);
2071 if ((size_t)ret
< sizeof(vty
->pass_fd_status
)) {
2072 size_t remains
= sizeof(vty
->pass_fd_status
) - ret
;
2074 buffer_put(vty
->obuf
, vty
->pass_fd_status
+ ret
, remains
);
2075 return BUFFER_PENDING
;
2077 return BUFFER_EMPTY
;
2080 static int vtysh_flush(struct vty
*vty
)
2084 ret
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
2085 if (ret
== BUFFER_EMPTY
&& vty
->status
== VTY_PASSFD
)
2086 ret
= vtysh_do_pass_fd(vty
);
2089 case BUFFER_PENDING
:
2090 vty_event(VTYSH_WRITE
, vty
);
2093 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2095 buffer_reset(vty
->lbuf
);
2096 buffer_reset(vty
->obuf
);
2105 void vty_pass_fd(struct vty
*vty
, int fd
)
2107 if (vty
->pass_fd
!= -1)
2108 close(vty
->pass_fd
);
2113 static void vtysh_read(struct thread
*thread
)
2119 unsigned char buf
[VTY_READ_BUFSIZ
];
2121 uint8_t header
[4] = {0, 0, 0, 0};
2123 sock
= THREAD_FD(thread
);
2124 vty
= THREAD_ARG(thread
);
2126 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2128 if (ERRNO_IO_RETRY(errno
)) {
2129 vty_event(VTYSH_READ
, vty
);
2134 "%s: read failed on vtysh client fd %d, closing: %s",
2135 __func__
, sock
, safe_strerror(errno
));
2137 buffer_reset(vty
->lbuf
);
2138 buffer_reset(vty
->obuf
);
2141 printf("close vtysh\n");
2142 #endif /* VTYSH_DEBUG */
2147 printf("line: %.*s\n", nbytes
, buf
);
2148 #endif /* VTYSH_DEBUG */
2150 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2151 /* Clear command line buffer. */
2152 vty
->cp
= vty
->length
= 0;
2154 vty_out(vty
, "%% Command is too long.\n");
2156 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2157 vty
->buf
[vty
->length
++] = *p
;
2159 /* Pass this line to parser. */
2160 ret
= vty_execute(vty
);
2161 /* Note that vty_execute clears the command buffer and resets
2162 vty->length to 0. */
2164 /* Return result. */
2166 printf("result: %d\n", ret
);
2167 printf("vtysh node: %d\n", vty
->node
);
2168 #endif /* VTYSH_DEBUG */
2170 if (vty
->pass_fd
!= -1) {
2171 memset(vty
->pass_fd_status
, 0, 4);
2172 vty
->pass_fd_status
[3] = ret
;
2173 vty
->status
= VTY_PASSFD
;
2176 vty_event(VTYSH_WRITE
, vty
);
2178 /* this introduces a "sequence point"
2179 * command output is written normally,
2180 * read processing is suspended until
2182 * then retcode + FD is written
2183 * then normal processing resumes
2185 * => skip vty_event(VTYSH_READ, vty)!
2190 /* hack for asynchronous "write integrated"
2191 * - other commands in "buf" will be ditched
2192 * - input during pending config-write is
2194 if (ret
== CMD_SUSPEND
)
2197 /* warning: watchfrr hardcodes this result write
2200 buffer_put(vty
->obuf
, header
, 4);
2202 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2203 /* Try to flush results; exit if a write
2210 if (vty
->status
== VTY_CLOSE
)
2213 vty_event(VTYSH_READ
, vty
);
2216 static void vtysh_write(struct thread
*thread
)
2218 struct vty
*vty
= THREAD_ARG(thread
);
2225 /* Determine address family to bind. */
2226 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2228 /* If port is set to 0, do not listen on TCP/IP at all! */
2230 vty_serv_sock_addrinfo(addr
, port
);
2237 static void vty_error_delete(void *arg
)
2239 struct vty_error
*ve
= arg
;
2241 XFREE(MTYPE_TMP
, ve
);
2244 /* Close vty interface. Warning: call this only from functions that
2245 will be careful not to access the vty afterwards (since it has
2246 now been freed). This is safest from top-level functions (called
2247 directly by the thread dispatcher). */
2248 void vty_close(struct vty
*vty
)
2251 bool was_stdio
= false;
2253 /* Drop out of configure / transaction if needed. */
2254 vty_config_exit(vty
);
2256 /* Cancel threads.*/
2257 THREAD_OFF(vty
->t_read
);
2258 THREAD_OFF(vty
->t_write
);
2259 THREAD_OFF(vty
->t_timeout
);
2261 if (vty
->pass_fd
!= -1) {
2262 close(vty
->pass_fd
);
2265 zlog_live_close(&vty
->live_log
);
2268 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2270 /* Free input buffer. */
2271 buffer_free(vty
->obuf
);
2272 buffer_free(vty
->lbuf
);
2274 /* Free command history. */
2275 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2276 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2280 if (vty
->fd
!= -1) {
2281 if (vty
->type
== VTY_SHELL_SERV
)
2282 vtys_del(vtysh_sessions
, vty
);
2284 vtys_del(vty_sessions
, vty
);
2287 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2291 * note check is for fd > STDERR_FILENO, not fd != -1.
2292 * We never close stdin/stdout/stderr here, because we may be
2293 * running in foreground mode with logging to stdout. Also,
2294 * additionally, we'd need to replace these fds with /dev/null. */
2295 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2297 if (vty
->fd
> STDERR_FILENO
)
2299 if (vty
->fd
== STDIN_FILENO
)
2302 XFREE(MTYPE_VTY
, vty
->buf
);
2305 vty
->error
->del
= vty_error_delete
;
2306 list_delete(&vty
->error
);
2310 XFREE(MTYPE_VTY
, vty
);
2316 /* When time out occur output message then close connection. */
2317 static void vty_timeout(struct thread
*thread
)
2321 vty
= THREAD_ARG(thread
);
2325 buffer_reset(vty
->lbuf
);
2326 buffer_reset(vty
->obuf
);
2327 vty_out(vty
, "\nVty connection is timed out.\n");
2329 /* Close connection. */
2330 vty
->status
= VTY_CLOSE
;
2334 /* Read up configuration file from file_name. */
2335 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2339 struct vty_error
*ve
;
2340 struct listnode
*node
;
2341 unsigned int line_num
= 0;
2344 /* vty_close won't close stderr; if some config command prints
2345 * something it'll end up there. (not ideal; it'd be better if output
2346 * from a file-load went to logging instead. Also note that if this
2347 * function is called after daemonizing, stderr will be /dev/null.)
2349 * vty->fd will be -1 from vty_new()
2351 vty
->wfd
= STDERR_FILENO
;
2352 vty
->type
= VTY_FILE
;
2353 vty
->node
= CONFIG_NODE
;
2356 vty
->candidate_config
= config
;
2358 vty
->private_config
= true;
2359 vty
->candidate_config
= nb_config_new(NULL
);
2362 /* Execute configuration file */
2363 ret
= config_from_file(vty
, confp
, &line_num
);
2365 /* Flush any previous errors before printing messages below */
2366 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2368 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2369 const char *message
= NULL
;
2373 case CMD_ERR_AMBIGUOUS
:
2374 message
= "Ambiguous command";
2376 case CMD_ERR_NO_MATCH
:
2377 message
= "No such command";
2380 message
= "Command returned Warning";
2382 case CMD_WARNING_CONFIG_FAILED
:
2383 message
= "Command returned Warning Config Failed";
2385 case CMD_ERR_INCOMPLETE
:
2386 message
= "Command returned Incomplete";
2388 case CMD_ERR_EXEED_ARGC_MAX
:
2390 "Command exceeded maximum number of Arguments";
2393 message
= "Command returned unhandled error message";
2397 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2398 nl
= strchr(ve
->error_buf
, '\n');
2401 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2402 message
, ve
->line_num
, ve
->error_buf
);
2407 * Automatically commit the candidate configuration after
2408 * reading the configuration file.
2410 if (config
== NULL
) {
2411 struct nb_context context
= {};
2412 char errmsg
[BUFSIZ
] = {0};
2414 context
.client
= NB_CLIENT_CLI
;
2416 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2417 "Read configuration file", NULL
,
2418 errmsg
, sizeof(errmsg
));
2419 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2421 "%s: failed to read configuration file: %s (%s)",
2422 __func__
, nb_err_name(ret
), errmsg
);
2428 static FILE *vty_use_backup_config(const char *fullpath
)
2430 char *fullpath_sav
, *fullpath_tmp
;
2436 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2437 fullpath_sav
= malloc(fullpath_sav_sz
);
2438 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2439 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2441 sav
= open(fullpath_sav
, O_RDONLY
);
2447 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2448 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2450 /* Open file to configuration write. */
2451 tmp
= mkstemp(fullpath_tmp
);
2455 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2458 while ((c
= read(sav
, buffer
, 512)) > 0) {
2459 if (write(tmp
, buffer
, c
) <= 0)
2465 if (rename(fullpath_tmp
, fullpath
) == 0)
2466 ret
= fopen(fullpath
, "r");
2468 unlink(fullpath_tmp
);
2473 unlink(fullpath_tmp
);
2483 /* Read up configuration file from file_name. */
2484 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2485 char *config_default_dir
)
2487 char cwd
[MAXPATHLEN
];
2489 const char *fullpath
;
2491 bool read_success
= false;
2493 /* If -f flag specified. */
2494 if (config_file
!= NULL
) {
2495 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2496 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2499 "%s: failure to determine Current Working Directory %d!",
2501 goto tmp_free_and_out
;
2503 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2504 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2505 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2508 fullpath
= config_file
;
2510 confp
= fopen(fullpath
, "r");
2512 if (confp
== NULL
) {
2514 EC_LIB_BACKUP_CONFIG
,
2515 "%s: failed to open configuration file %s: %s, checking backup",
2516 __func__
, fullpath
, safe_strerror(errno
));
2518 confp
= vty_use_backup_config(fullpath
);
2520 flog_warn(EC_LIB_BACKUP_CONFIG
,
2521 "using backup configuration file!");
2525 "%s: can't open configuration file [%s]",
2526 __func__
, config_file
);
2527 goto tmp_free_and_out
;
2532 host_config_set(config_default_dir
);
2536 struct stat conf_stat
;
2538 /* !!!!PLEASE LEAVE!!!!
2539 * This is NEEDED for use with vtysh -b, or else you can get
2540 * a real configuration food fight with a lot garbage in the
2541 * merged configuration file it creates coming from the per
2542 * daemon configuration files. This also allows the daemons
2543 * to start if there default configuration file is not
2544 * present or ignore them, as needed when using vtysh -b to
2545 * configure the daemons at boot - MAG
2548 /* Stat for vtysh Zebra.conf, if found startup and wait for
2549 * boot configuration
2552 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2553 ret
= stat(integrate_default
, &conf_stat
);
2555 read_success
= true;
2556 goto tmp_free_and_out
;
2560 confp
= fopen(config_default_dir
, "r");
2561 if (confp
== NULL
) {
2564 "%s: failed to open configuration file %s: %s, checking backup",
2565 __func__
, config_default_dir
,
2566 safe_strerror(errno
));
2568 confp
= vty_use_backup_config(config_default_dir
);
2570 flog_warn(EC_LIB_BACKUP_CONFIG
,
2571 "using backup configuration file!");
2572 fullpath
= config_default_dir
;
2574 flog_err(EC_LIB_VTY
,
2575 "can't open configuration file [%s]",
2576 config_default_dir
);
2577 goto tmp_free_and_out
;
2580 fullpath
= config_default_dir
;
2583 vty_read_file(config
, confp
);
2584 read_success
= true;
2588 host_config_set(fullpath
);
2591 XFREE(MTYPE_TMP
, tmp
);
2593 return read_success
;
2596 static void update_xpath(struct vty
*vty
, const char *oldpath
,
2597 const char *newpath
)
2601 for (i
= 0; i
< vty
->xpath_index
; i
++) {
2602 if (!frrstr_startswith(vty
->xpath
[i
], oldpath
))
2605 char *tmp
= frrstr_replace(vty
->xpath
[i
], oldpath
, newpath
);
2606 strlcpy(vty
->xpath
[i
], tmp
, sizeof(vty
->xpath
[0]));
2607 XFREE(MTYPE_TMP
, tmp
);
2611 void vty_update_xpath(const char *oldpath
, const char *newpath
)
2615 frr_each (vtys
, vtysh_sessions
, vty
)
2616 update_xpath(vty
, oldpath
, newpath
);
2617 frr_each (vtys
, vty_sessions
, vty
)
2618 update_xpath(vty
, oldpath
, newpath
);
2621 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2623 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2624 vty_out(vty
, "%% Configuration is locked by other client\n");
2628 vty
->node
= CONFIG_NODE
;
2630 vty
->private_config
= private_config
;
2631 vty
->xpath_index
= 0;
2633 if (private_config
) {
2634 vty
->candidate_config
= nb_config_dup(running_config
);
2635 vty
->candidate_config_base
= nb_config_dup(running_config
);
2637 "Warning: uncommitted changes will be discarded on exit.\n\n");
2639 vty
->candidate_config
= vty_shared_candidate_config
;
2640 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2641 vty
->candidate_config_base
=
2642 nb_config_dup(running_config
);
2648 void vty_config_exit(struct vty
*vty
)
2650 enum node_type node
= vty
->node
;
2651 struct cmd_node
*cnode
;
2653 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2654 * somewhere below CONFIG_NODE */
2655 while (node
&& node
!= CONFIG_NODE
) {
2656 cnode
= vector_lookup(cmdvec
, node
);
2657 node
= cnode
->parent_node
;
2659 if (node
!= CONFIG_NODE
)
2660 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2663 while (vty
->node
!= ENABLE_NODE
)
2664 /* will call vty_config_node_exit() below */
2668 int vty_config_node_exit(struct vty
*vty
)
2670 vty
->xpath_index
= 0;
2672 /* Perform any pending commits. */
2673 (void)nb_cli_pending_commit_check(vty
);
2675 /* Check if there's a pending confirmed commit. */
2676 if (vty
->t_confirmed_commit_timeout
) {
2678 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2679 nb_cli_confirmed_commit_rollback(vty
);
2680 nb_cli_confirmed_commit_clean(vty
);
2683 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2685 if (vty
->candidate_config
) {
2686 if (vty
->private_config
)
2687 nb_config_free(vty
->candidate_config
);
2688 vty
->candidate_config
= NULL
;
2690 if (vty
->candidate_config_base
) {
2691 nb_config_free(vty
->candidate_config_base
);
2692 vty
->candidate_config_base
= NULL
;
2695 vty
->config
= false;
2699 /* Master of the threads. */
2700 static struct thread_master
*vty_master
;
2702 static void vty_event_serv(enum vty_event event
, struct vty_serv
*vty_serv
)
2706 thread_add_read(vty_master
, vty_accept
, vty_serv
,
2707 vty_serv
->sock
, &vty_serv
->t_accept
);
2711 thread_add_read(vty_master
, vtysh_accept
, vty_serv
,
2712 vty_serv
->sock
, &vty_serv
->t_accept
);
2717 case VTY_TIMEOUT_RESET
:
2720 assert(!"vty_event_serv() called incorrectly");
2724 static void vty_event(enum vty_event event
, struct vty
*vty
)
2729 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2733 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2738 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2741 /* Time out treatment. */
2742 if (vty
->v_timeout
) {
2743 THREAD_OFF(vty
->t_timeout
);
2744 thread_add_timer(vty_master
, vty_timeout
, vty
,
2745 vty
->v_timeout
, &vty
->t_timeout
);
2749 thread_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2752 case VTY_TIMEOUT_RESET
:
2753 THREAD_OFF(vty
->t_timeout
);
2755 thread_add_timer(vty_master
, vty_timeout
, vty
,
2756 vty
->v_timeout
, &vty
->t_timeout
);
2760 assert(!"vty_event() called incorrectly");
2764 DEFUN_NOSH (config_who
,
2767 "Display who is on vty\n")
2771 frr_each (vtys
, vty_sessions
, v
)
2772 vty_out(vty
, "%svty[%d] connected from %s%s.\n",
2773 v
->config
? "*" : " ", v
->fd
, v
->address
,
2774 zlog_live_is_null(&v
->live_log
) ? "" : ", live log");
2778 /* Move to vty configuration mode. */
2779 DEFUN_NOSH (line_vty
,
2782 "Configure a terminal line\n"
2783 "Virtual terminal\n")
2785 vty
->node
= VTY_NODE
;
2789 /* Set time out value. */
2790 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2791 const char *sec_str
)
2793 unsigned long timeout
= 0;
2795 /* min_str and sec_str are already checked by parser. So it must be
2796 all digit string. */
2798 timeout
= strtol(min_str
, NULL
, 10);
2802 timeout
+= strtol(sec_str
, NULL
, 10);
2804 vty_timeout_val
= timeout
;
2805 vty
->v_timeout
= timeout
;
2806 vty_event(VTY_TIMEOUT_RESET
, vty
);
2812 DEFUN (exec_timeout_min
,
2813 exec_timeout_min_cmd
,
2814 "exec-timeout (0-35791)",
2815 "Set timeout value\n"
2816 "Timeout value in minutes\n")
2819 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2822 DEFUN (exec_timeout_sec
,
2823 exec_timeout_sec_cmd
,
2824 "exec-timeout (0-35791) (0-2147483)",
2825 "Set the EXEC timeout\n"
2826 "Timeout in minutes\n"
2827 "Timeout in seconds\n")
2830 int idx_number_2
= 2;
2831 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2832 argv
[idx_number_2
]->arg
);
2835 DEFUN (no_exec_timeout
,
2836 no_exec_timeout_cmd
,
2839 "Set the EXEC timeout\n")
2841 return exec_timeout(vty
, NULL
, NULL
);
2844 /* Set vty access class. */
2845 DEFUN (vty_access_class
,
2846 vty_access_class_cmd
,
2847 "access-class WORD",
2848 "Filter connections based on an IP access list\n"
2852 if (vty_accesslist_name
)
2853 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2855 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2860 /* Clear vty access class. */
2861 DEFUN (no_vty_access_class
,
2862 no_vty_access_class_cmd
,
2863 "no access-class [WORD]",
2865 "Filter connections based on an IP access list\n"
2869 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2870 if (!vty_accesslist_name
2871 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2872 vty_out(vty
, "Access-class is not currently applied to vty\n");
2873 return CMD_WARNING_CONFIG_FAILED
;
2876 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2878 vty_accesslist_name
= NULL
;
2883 /* Set vty access class. */
2884 DEFUN (vty_ipv6_access_class
,
2885 vty_ipv6_access_class_cmd
,
2886 "ipv6 access-class WORD",
2888 "Filter connections based on an IP access list\n"
2889 "IPv6 access list\n")
2892 if (vty_ipv6_accesslist_name
)
2893 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2895 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2900 /* Clear vty access class. */
2901 DEFUN (no_vty_ipv6_access_class
,
2902 no_vty_ipv6_access_class_cmd
,
2903 "no ipv6 access-class [WORD]",
2906 "Filter connections based on an IP access list\n"
2907 "IPv6 access list\n")
2910 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2912 if (!vty_ipv6_accesslist_name
2913 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2915 "IPv6 access-class is not currently applied to vty\n");
2916 return CMD_WARNING_CONFIG_FAILED
;
2919 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2921 vty_ipv6_accesslist_name
= NULL
;
2930 "Enable password checking\n")
2932 no_password_check
= 0;
2936 DEFUN (no_vty_login
,
2940 "Enable password checking\n")
2942 no_password_check
= 1;
2946 DEFUN (service_advanced_vty
,
2947 service_advanced_vty_cmd
,
2948 "service advanced-vty",
2949 "Set up miscellaneous service\n"
2950 "Enable advanced mode vty interface\n")
2956 DEFUN (no_service_advanced_vty
,
2957 no_service_advanced_vty_cmd
,
2958 "no service advanced-vty",
2960 "Set up miscellaneous service\n"
2961 "Enable advanced mode vty interface\n")
2967 DEFUN_NOSH(terminal_monitor
,
2968 terminal_monitor_cmd
,
2969 "terminal monitor [detach]",
2970 "Set terminal line parameters\n"
2971 "Copy debug output to the current terminal line\n"
2972 "Keep logging feed open independent of VTY session\n")
2976 if (vty
->type
!= VTY_SHELL_SERV
) {
2977 vty_out(vty
, "%% not supported\n");
2982 struct zlog_live_cfg detach_log
= {};
2984 zlog_live_open(&detach_log
, LOG_DEBUG
, &fd_ret
);
2985 zlog_live_disown(&detach_log
);
2987 zlog_live_open(&vty
->live_log
, LOG_DEBUG
, &fd_ret
);
2990 vty_out(vty
, "%% error opening live log: %m\n");
2994 vty_pass_fd(vty
, fd_ret
);
2998 DEFUN_NOSH(no_terminal_monitor
,
2999 no_terminal_monitor_cmd
,
3000 "no terminal monitor",
3002 "Set terminal line parameters\n"
3003 "Copy debug output to the current terminal line\n")
3005 zlog_live_close(&vty
->live_log
);
3009 DEFUN_NOSH(terminal_no_monitor
,
3010 terminal_no_monitor_cmd
,
3011 "terminal no monitor",
3012 "Set terminal line parameters\n"
3014 "Copy debug output to the current terminal line\n")
3016 return no_terminal_monitor(self
, vty
, argc
, argv
);
3020 DEFUN_NOSH (show_history
,
3024 "Display the session command history\n")
3028 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3029 if (index
== VTY_MAXHIST
) {
3034 if (vty
->hist
[index
] != NULL
)
3035 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3044 DEFPY (log_commands
,
3046 "[no] log commands",
3049 "Log all commands\n")
3052 if (do_log_commands_perm
) {
3054 "Daemon started with permanent logging turned on for commands, ignoring\n");
3058 do_log_commands
= false;
3060 do_log_commands
= true;
3065 /* Display current configuration. */
3066 static int vty_config_write(struct vty
*vty
)
3068 vty_frame(vty
, "line vty\n");
3070 if (vty_accesslist_name
)
3071 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3073 if (vty_ipv6_accesslist_name
)
3074 vty_out(vty
, " ipv6 access-class %s\n",
3075 vty_ipv6_accesslist_name
);
3078 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3079 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3080 vty_timeout_val
% 60);
3083 if (no_password_check
)
3084 vty_out(vty
, " no login\n");
3086 vty_endframe(vty
, "exit\n");
3088 if (do_log_commands
)
3089 vty_out(vty
, "log commands\n");
3091 vty_out(vty
, "!\n");
3096 static int vty_config_write(struct vty
*vty
);
3097 struct cmd_node vty_node
= {
3100 .parent_node
= CONFIG_NODE
,
3101 .prompt
= "%s(config-line)# ",
3102 .config_write
= vty_config_write
,
3105 /* Reset all VTY status. */
3106 void vty_reset(void)
3110 frr_each_safe (vtys
, vty_sessions
, vty
) {
3111 buffer_reset(vty
->lbuf
);
3112 buffer_reset(vty
->obuf
);
3113 vty
->status
= VTY_CLOSE
;
3117 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3119 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3120 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3123 static void vty_save_cwd(void)
3127 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3131 * At this point if these go wrong, more than likely
3132 * the whole world is coming down around us
3133 * Hence not worrying about it too much.
3135 if (chdir(SYSCONFDIR
)) {
3136 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3137 "Failure to chdir to %s, errno: %d",
3141 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3142 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3143 "Failure to getcwd, errno: %d", errno
);
3149 char *vty_get_cwd(void)
3154 int vty_shell(struct vty
*vty
)
3156 return vty
->type
== VTY_SHELL
? 1 : 0;
3159 int vty_shell_serv(struct vty
*vty
)
3161 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3164 void vty_init_vtysh(void)
3166 /* currently nothing to do, but likely to have future use */
3169 /* Install vty's own commands like `who' command. */
3170 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3172 /* For further configuration read, preserve current directory. */
3175 vty_master
= master_thread
;
3177 atexit(vty_stdio_atexit
);
3179 /* Install bgp top node. */
3180 install_node(&vty_node
);
3182 install_element(VIEW_NODE
, &config_who_cmd
);
3183 install_element(VIEW_NODE
, &show_history_cmd
);
3184 install_element(CONFIG_NODE
, &line_vty_cmd
);
3185 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3186 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3187 install_element(CONFIG_NODE
, &show_history_cmd
);
3188 install_element(CONFIG_NODE
, &log_commands_cmd
);
3190 if (do_command_logging
) {
3191 do_log_commands
= true;
3192 do_log_commands_perm
= true;
3195 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3196 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3197 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3199 install_default(VTY_NODE
);
3200 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3201 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3202 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3203 install_element(VTY_NODE
, &vty_access_class_cmd
);
3204 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3205 install_element(VTY_NODE
, &vty_login_cmd
);
3206 install_element(VTY_NODE
, &no_vty_login_cmd
);
3207 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3208 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3211 void vty_terminate(void)
3214 struct vty_serv
*vtyserv
;
3216 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3220 /* default state of vty_sessions is initialized & empty. */
3221 vtys_fini(vty_sessions
);
3222 vtys_init(vty_sessions
);
3224 /* vty_reset() doesn't close vtysh sessions */
3225 frr_each_safe (vtys
, vtysh_sessions
, vty
) {
3226 buffer_reset(vty
->lbuf
);
3227 buffer_reset(vty
->obuf
);
3228 vty
->status
= VTY_CLOSE
;
3232 vtys_fini(vtysh_sessions
);
3233 vtys_init(vtysh_sessions
);
3235 while ((vtyserv
= vtyservs_pop(vty_servs
))) {
3236 THREAD_OFF(vtyserv
->t_accept
);
3237 close(vtyserv
->sock
);
3238 XFREE(MTYPE_VTY_SERV
, vtyserv
);
3241 vtyservs_fini(vty_servs
);
3242 vtyservs_init(vty_servs
);