2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
25 #include <sys/types.h>
26 #include <sys/types.h>
27 #ifdef HAVE_LIBPCREPOSIX
28 #include <pcreposix.h>
31 #endif /* HAVE_LIBPCREPOSIX */
38 #include "sockunion.h"
48 #include "lib_errors.h"
49 #include "northbound_cli.h"
52 #include <arpa/telnet.h>
55 #ifndef VTYSH_EXTRACT_PL
56 #include "lib/vty_clippy.c"
59 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
60 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
61 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
76 static void vty_event(enum event
, int, struct vty
*);
78 /* Extern host structure from command.c */
79 extern struct host host
;
81 /* Vector which store each vty structure. */
84 /* Vty timeout value. */
85 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
87 /* Vty access-class command */
88 static char *vty_accesslist_name
= NULL
;
90 /* Vty access-calss for IPv6. */
91 static char *vty_ipv6_accesslist_name
= NULL
;
93 /* VTY server thread. */
94 static vector Vvty_serv_thread
;
96 /* Current directory. */
97 static char vty_cwd
[MAXPATHLEN
];
99 /* Login password check. */
100 static int no_password_check
= 0;
102 /* Integrated configuration file path */
103 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
105 static bool do_log_commands
;
106 static bool do_log_commands_perm
;
108 void vty_frame(struct vty
*vty
, const char *format
, ...)
112 va_start(args
, format
);
113 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
114 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
115 vty
->frame_pos
= strlen(vty
->frame
);
119 void vty_endframe(struct vty
*vty
, const char *endtext
)
121 if (vty
->frame_pos
== 0 && endtext
)
122 vty_out(vty
, "%s", endtext
);
126 bool vty_set_include(struct vty
*vty
, const char *regexp
)
134 regfree(&vty
->include
);
140 errcode
= regcomp(&vty
->include
, regexp
,
141 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
144 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
145 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
153 /* VTY standard output function. */
154 int vty_out(struct vty
*vty
, const char *format
, ...)
162 if (vty
->frame_pos
) {
164 vty_out(vty
, "%s", vty
->frame
);
167 va_start(args
, format
);
168 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
175 vector lines
= frrstr_split_vec(p
, "\n");
177 /* Place first value in the cache */
178 char *firstline
= vector_slot(lines
, 0);
179 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
181 /* If our split returned more than one entry, time to filter */
182 if (vector_active(lines
) > 1) {
184 * returned string is MTYPE_TMP so it matches the MTYPE
185 * of everything else in the vector
187 char *bstr
= buffer_getstr(vty
->lbuf
);
188 buffer_reset(vty
->lbuf
);
189 XFREE(MTYPE_TMP
, lines
->index
[0]);
190 vector_set_index(lines
, 0, bstr
);
191 frrstr_filter_vec(lines
, &vty
->include
);
192 vector_compact(lines
);
194 * Consider the string "foo\n". If the regex is an empty string
195 * and the line ended with a newline, then the vector will look
201 * If the regex isn't empty, the vector will look like:
205 * In this case we'd like to preserve the newline, so we add
206 * the empty string [1] as in the first example.
208 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
209 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
210 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
212 filtered
= frrstr_join_vec(lines
, "\n");
218 frrstr_strvec_free(lines
);
229 /* print with crlf replacement */
230 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
235 fprintf(vty
->of
, "%s", filtered
);
237 } else if (vty
->of_saved
) {
238 fprintf(vty
->of_saved
, "%s", filtered
);
239 fflush(vty
->of_saved
);
245 /* print without crlf replacement */
246 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
252 if (vty
->filter
&& filtered
)
253 XFREE(MTYPE_TMP
, filtered
);
255 /* If p is not different with buf, it is allocated buffer. */
257 XFREE(MTYPE_VTY_OUT_BUF
, p
);
262 static int vty_log_out(struct vty
*vty
, const char *level
,
263 const char *proto_str
, const char *msg
,
264 struct timestamp_control
*ctl
)
270 if (!ctl
->already_rendered
) {
271 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
273 ctl
->already_rendered
= 1;
275 if (ctl
->len
+ 1 >= sizeof(buf
))
277 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
282 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
285 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
286 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
289 if (((ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", msg
)) < 0)
290 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
296 if (write(vty
->wfd
, buf
, len
) < 0) {
297 if (ERRNO_IO_RETRY(errno
))
298 /* Kernel buffer is full, probably too much debugging
300 drop the data and ignore. */
302 /* Fatal I/O error. */
304 0; /* disable monitoring to avoid infinite recursion */
305 flog_err(EC_LIB_SOCKET
,
306 "%s: write failed to vty client fd %d, closing: %s",
307 __func__
, vty
->fd
, safe_strerror(errno
));
308 buffer_reset(vty
->obuf
);
309 buffer_reset(vty
->lbuf
);
310 /* cannot call vty_close, because a parent routine may still try
311 to access the vty struct */
312 vty
->status
= VTY_CLOSE
;
313 shutdown(vty
->fd
, SHUT_RDWR
);
319 /* Output current time to the vty. */
320 void vty_time_print(struct vty
*vty
, int cr
)
322 char buf
[QUAGGA_TIMESTAMP_LEN
];
324 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
325 zlog_info("quagga_timestamp error");
329 vty_out(vty
, "%s\n", buf
);
331 vty_out(vty
, "%s ", buf
);
336 /* Say hello to vty interface. */
337 void vty_hello(struct vty
*vty
)
343 f
= fopen(host
.motdfile
, "r");
345 while (fgets(buf
, sizeof(buf
), f
)) {
347 /* work backwards to ignore trailling isspace()
349 for (s
= buf
+ strlen(buf
);
350 (s
> buf
) && isspace((unsigned char)s
[-1]);
354 vty_out(vty
, "%s\n", buf
);
358 vty_out(vty
, "MOTD file not found\n");
359 } else if (host
.motd
)
360 vty_out(vty
, "%s", host
.motd
);
362 #if CONFDATE > 20200901
363 CPP_NOTICE("Please remove solaris code from system as it is deprecated");
366 zlog_warn("If you are using FRR on Solaris, the FRR developers would love to hear from you\n");
367 zlog_warn("Please send email to dev@lists.frrouting.org about this message\n");
368 zlog_warn("We are considering deprecating Solaris and want to find users of Solaris systems\n");
372 /* Put out prompt and wait input from user. */
373 static void vty_prompt(struct vty
*vty
)
375 if (vty
->type
== VTY_TERM
) {
376 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
380 /* Send WILL TELOPT_ECHO to remote server. */
381 static void vty_will_echo(struct vty
*vty
)
383 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
384 vty_out(vty
, "%s", cmd
);
387 /* Make suppress Go-Ahead telnet option. */
388 static void vty_will_suppress_go_ahead(struct vty
*vty
)
390 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
391 vty_out(vty
, "%s", cmd
);
394 /* Make don't use linemode over telnet. */
395 static void vty_dont_linemode(struct vty
*vty
)
397 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
398 vty_out(vty
, "%s", cmd
);
401 /* Use window size. */
402 static void vty_do_window_size(struct vty
*vty
)
404 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
405 vty_out(vty
, "%s", cmd
);
408 #if 0 /* Currently not used. */
409 /* Make don't use lflow vty interface. */
411 vty_dont_lflow_ahead (struct vty
*vty
)
413 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
414 vty_out (vty
, "%s", cmd
);
418 /* Authentication of vty */
419 static void vty_auth(struct vty
*vty
, char *buf
)
422 enum node_type next_node
= 0;
424 char *crypt(const char *, const char *);
429 passwd
= host
.password_encrypt
;
431 passwd
= host
.password
;
433 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
435 next_node
= VIEW_NODE
;
437 case AUTH_ENABLE_NODE
:
439 passwd
= host
.enable_encrypt
;
441 passwd
= host
.enable
;
442 next_node
= ENABLE_NODE
;
448 fail
= strcmp(crypt(buf
, passwd
), passwd
);
450 fail
= strcmp(buf
, passwd
);
456 vty
->node
= next_node
; /* Success ! */
459 if (vty
->fail
>= 3) {
460 if (vty
->node
== AUTH_NODE
) {
462 "%% Bad passwords, too many failures!\n");
463 vty
->status
= VTY_CLOSE
;
465 /* AUTH_ENABLE_NODE */
468 "%% Bad enable passwords, too many failures!\n");
469 vty
->status
= VTY_CLOSE
;
475 /* Command execution over the vty interface. */
476 static int vty_command(struct vty
*vty
, char *buf
)
479 const char *protocolname
;
485 * Log non empty command lines
490 /* Skip white spaces. */
491 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
494 if (cp
!= NULL
&& *cp
!= '\0') {
496 char vty_str
[VTY_BUFSIZ
];
497 char prompt_str
[VTY_BUFSIZ
];
499 /* format the base vty info */
500 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
502 for (i
= 0; i
< vector_active(vtyvec
); i
++)
503 if (vty
== vector_slot(vtyvec
, i
)) {
504 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
509 /* format the prompt */
510 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
513 /* now log the command */
514 zlog_notice("%s%s", prompt_str
, buf
);
517 #ifdef CONSUMED_TIME_CHECK
521 unsigned long realtime
, cputime
;
524 #endif /* CONSUMED_TIME_CHECK */
526 ret
= cmd_execute(vty
, buf
, NULL
, 0);
528 /* Get the name of the protocol if any */
529 protocolname
= frr_protoname
;
531 #ifdef CONSUMED_TIME_CHECK
533 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
534 > CONSUMED_TIME_CHECK
)
535 /* Warn about CPU hog that must be fixed. */
538 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
539 realtime
/ 1000, cputime
/ 1000, buf
);
541 #endif /* CONSUMED_TIME_CHECK */
543 if (ret
!= CMD_SUCCESS
)
546 if (vty
->type
== VTY_FILE
)
547 vty_out(vty
, "Warning...\n");
549 case CMD_ERR_AMBIGUOUS
:
550 vty_out(vty
, "%% Ambiguous command.\n");
552 case CMD_ERR_NO_MATCH
:
553 vty_out(vty
, "%% [%s] Unknown command: %s\n",
556 case CMD_ERR_INCOMPLETE
:
557 vty_out(vty
, "%% Command incomplete.\n");
564 static const char telnet_backward_char
= 0x08;
565 static const char telnet_space_char
= ' ';
567 /* Basic function to write buffer to vty. */
568 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
570 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
573 /* Should we do buffering here ? And make vty_flush (vty) ? */
574 buffer_put(vty
->obuf
, buf
, nbytes
);
577 /* Basic function to insert character into vty. */
578 static void vty_self_insert(struct vty
*vty
, char c
)
583 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
586 length
= vty
->length
- vty
->cp
;
587 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
588 vty
->buf
[vty
->cp
] = c
;
590 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
591 for (i
= 0; i
< length
; i
++)
592 vty_write(vty
, &telnet_backward_char
, 1);
597 vty
->buf
[vty
->length
] = '\0';
600 /* Self insert character 'c' in overwrite mode. */
601 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
603 if (vty
->cp
== vty
->length
) {
604 vty_self_insert(vty
, c
);
608 vty
->buf
[vty
->cp
++] = c
;
609 vty_write(vty
, &c
, 1);
613 * Insert a string into vty->buf at the current cursor position.
615 * If the resultant string would be larger than VTY_BUFSIZ it is
618 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
620 if (vty
->cp
== VTY_BUFSIZ
)
623 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
624 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
626 vty
->length
= MAX(vty
->cp
, vty
->length
);
627 vty
->buf
[vty
->length
] = '\0';
628 vty_write(vty
, str
, nwrite
);
631 /* Forward character. */
632 static void vty_forward_char(struct vty
*vty
)
634 if (vty
->cp
< vty
->length
) {
635 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
640 /* Backward character. */
641 static void vty_backward_char(struct vty
*vty
)
645 vty_write(vty
, &telnet_backward_char
, 1);
649 /* Move to the beginning of the line. */
650 static void vty_beginning_of_line(struct vty
*vty
)
653 vty_backward_char(vty
);
656 /* Move to the end of the line. */
657 static void vty_end_of_line(struct vty
*vty
)
659 while (vty
->cp
< vty
->length
)
660 vty_forward_char(vty
);
663 static void vty_kill_line_from_beginning(struct vty
*);
664 static void vty_redraw_line(struct vty
*);
666 /* Print command line history. This function is called from
667 vty_next_line and vty_previous_line. */
668 static void vty_history_print(struct vty
*vty
)
672 vty_kill_line_from_beginning(vty
);
674 /* Get previous line from history buffer */
675 length
= strlen(vty
->hist
[vty
->hp
]);
676 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
677 vty
->cp
= vty
->length
= length
;
678 vty
->buf
[vty
->length
] = '\0';
680 /* Redraw current line */
681 vty_redraw_line(vty
);
684 /* Show next command line history. */
685 static void vty_next_line(struct vty
*vty
)
689 if (vty
->hp
== vty
->hindex
)
692 /* Try is there history exist or not. */
694 if (try_index
== (VTY_MAXHIST
- 1))
699 /* If there is not history return. */
700 if (vty
->hist
[try_index
] == NULL
)
705 vty_history_print(vty
);
708 /* Show previous command line history. */
709 static void vty_previous_line(struct vty
*vty
)
715 try_index
= VTY_MAXHIST
- 1;
719 if (vty
->hist
[try_index
] == NULL
)
724 vty_history_print(vty
);
727 /* This function redraw all of the command line character. */
728 static void vty_redraw_line(struct vty
*vty
)
730 vty_write(vty
, vty
->buf
, vty
->length
);
731 vty
->cp
= vty
->length
;
735 static void vty_forward_word(struct vty
*vty
)
737 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
738 vty_forward_char(vty
);
740 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
741 vty_forward_char(vty
);
744 /* Backward word without skipping training space. */
745 static void vty_backward_pure_word(struct vty
*vty
)
747 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
748 vty_backward_char(vty
);
752 static void vty_backward_word(struct vty
*vty
)
754 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
755 vty_backward_char(vty
);
757 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
758 vty_backward_char(vty
);
761 /* When '^D' is typed at the beginning of the line we move to the down
763 static void vty_down_level(struct vty
*vty
)
771 /* When '^Z' is received from vty, move down to the enable mode. */
772 static void vty_end_config(struct vty
*vty
)
777 vty_config_exit(vty
);
778 vty
->node
= ENABLE_NODE
;
785 /* Delete a charcter at the current point. */
786 static void vty_delete_char(struct vty
*vty
)
791 if (vty
->length
== 0) {
796 if (vty
->cp
== vty
->length
)
797 return; /* completion need here? */
799 size
= vty
->length
- vty
->cp
;
802 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
803 vty
->buf
[vty
->length
] = '\0';
805 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
808 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
809 vty_write(vty
, &telnet_space_char
, 1);
811 for (i
= 0; i
< size
; i
++)
812 vty_write(vty
, &telnet_backward_char
, 1);
815 /* Delete a character before the point. */
816 static void vty_delete_backward_char(struct vty
*vty
)
821 vty_backward_char(vty
);
822 vty_delete_char(vty
);
825 /* Kill rest of line from current point. */
826 static void vty_kill_line(struct vty
*vty
)
831 size
= vty
->length
- vty
->cp
;
836 for (i
= 0; i
< size
; i
++)
837 vty_write(vty
, &telnet_space_char
, 1);
838 for (i
= 0; i
< size
; i
++)
839 vty_write(vty
, &telnet_backward_char
, 1);
841 memset(&vty
->buf
[vty
->cp
], 0, size
);
842 vty
->length
= vty
->cp
;
845 /* Kill line from the beginning. */
846 static void vty_kill_line_from_beginning(struct vty
*vty
)
848 vty_beginning_of_line(vty
);
852 /* Delete a word before the point. */
853 static void vty_forward_kill_word(struct vty
*vty
)
855 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
856 vty_delete_char(vty
);
857 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
858 vty_delete_char(vty
);
861 /* Delete a word before the point. */
862 static void vty_backward_kill_word(struct vty
*vty
)
864 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
865 vty_delete_backward_char(vty
);
866 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
867 vty_delete_backward_char(vty
);
870 /* Transpose chars before or at the point. */
871 static void vty_transpose_chars(struct vty
*vty
)
875 /* If length is short or point is near by the beginning of line then
877 if (vty
->length
< 2 || vty
->cp
< 1)
880 /* In case of point is located at the end of the line. */
881 if (vty
->cp
== vty
->length
) {
882 c1
= vty
->buf
[vty
->cp
- 1];
883 c2
= vty
->buf
[vty
->cp
- 2];
885 vty_backward_char(vty
);
886 vty_backward_char(vty
);
887 vty_self_insert_overwrite(vty
, c1
);
888 vty_self_insert_overwrite(vty
, c2
);
890 c1
= vty
->buf
[vty
->cp
];
891 c2
= vty
->buf
[vty
->cp
- 1];
893 vty_backward_char(vty
);
894 vty_self_insert_overwrite(vty
, c1
);
895 vty_self_insert_overwrite(vty
, c2
);
899 /* Do completion at vty interface. */
900 static void vty_complete_command(struct vty
*vty
)
904 char **matched
= NULL
;
907 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
910 vline
= cmd_make_strvec(vty
->buf
);
914 /* In case of 'help \t'. */
915 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
916 vector_set(vline
, NULL
);
918 matched
= cmd_complete_command(vline
, vty
, &ret
);
920 cmd_free_strvec(vline
);
924 case CMD_ERR_AMBIGUOUS
:
925 vty_out(vty
, "%% Ambiguous command.\n");
927 vty_redraw_line(vty
);
929 case CMD_ERR_NO_MATCH
:
930 /* vty_out (vty, "%% There is no matched command.\n"); */
932 vty_redraw_line(vty
);
934 case CMD_COMPLETE_FULL_MATCH
:
936 /* 2016-11-28 equinox -- need to debug, SEGV here */
937 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
939 vty_redraw_line(vty
);
943 vty_redraw_line(vty
);
944 vty_backward_pure_word(vty
);
945 vty_insert_word_overwrite(vty
, matched
[0]);
946 vty_self_insert(vty
, ' ');
947 XFREE(MTYPE_COMPLETION
, matched
[0]);
949 case CMD_COMPLETE_MATCH
:
951 vty_redraw_line(vty
);
952 vty_backward_pure_word(vty
);
953 vty_insert_word_overwrite(vty
, matched
[0]);
954 XFREE(MTYPE_COMPLETION
, matched
[0]);
956 case CMD_COMPLETE_LIST_MATCH
:
957 for (i
= 0; matched
[i
] != NULL
; i
++) {
958 if (i
!= 0 && ((i
% 6) == 0))
960 vty_out(vty
, "%-10s ", matched
[i
]);
961 XFREE(MTYPE_COMPLETION
, matched
[i
]);
966 vty_redraw_line(vty
);
968 case CMD_ERR_NOTHING_TODO
:
970 vty_redraw_line(vty
);
975 XFREE(MTYPE_TMP
, matched
);
978 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
979 unsigned int desc_width
, struct cmd_token
*token
)
987 if (desc_width
<= 0) {
988 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
992 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
994 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
995 for (pos
= desc_width
; pos
> 0; pos
--)
996 if (*(p
+ pos
) == ' ')
1002 memcpy(buf
, p
, pos
);
1004 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1009 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1011 XFREE(MTYPE_TMP
, buf
);
1014 /* Describe matched command function. */
1015 static void vty_describe_command(struct vty
*vty
)
1020 unsigned int i
, width
, desc_width
;
1021 struct cmd_token
*token
, *token_cr
= NULL
;
1023 vline
= cmd_make_strvec(vty
->buf
);
1025 /* In case of '> ?'. */
1026 if (vline
== NULL
) {
1027 vline
= vector_init(1);
1028 vector_set(vline
, NULL
);
1029 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1030 vector_set(vline
, NULL
);
1032 describe
= cmd_describe_command(vline
, vty
, &ret
);
1036 /* Ambiguous error. */
1038 case CMD_ERR_AMBIGUOUS
:
1039 vty_out(vty
, "%% Ambiguous command.\n");
1042 case CMD_ERR_NO_MATCH
:
1043 vty_out(vty
, "%% There is no matched command.\n");
1048 /* Get width of command string. */
1050 for (i
= 0; i
< vector_active(describe
); i
++)
1051 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1054 if (token
->text
[0] == '\0')
1057 len
= strlen(token
->text
);
1063 /* Get width of description string. */
1064 desc_width
= vty
->width
- (width
+ 6);
1066 /* Print out description. */
1067 for (i
= 0; i
< vector_active(describe
); i
++)
1068 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1069 if (token
->text
[0] == '\0')
1072 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1078 vty_out(vty
, " %-s\n", token
->text
);
1079 else if (desc_width
>= strlen(token
->desc
))
1080 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1083 vty_describe_fold(vty
, width
, desc_width
,
1086 if (IS_VARYING_TOKEN(token
->type
)) {
1087 const char *ref
= vector_slot(
1088 vline
, vector_active(vline
) - 1);
1090 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1091 cmd_variable_complete(token
, ref
, varcomps
);
1093 if (vector_active(varcomps
) > 0) {
1094 char *ac
= cmd_variable_comp2str(
1095 varcomps
, vty
->width
);
1096 vty_out(vty
, "%s\n", ac
);
1097 XFREE(MTYPE_TMP
, ac
);
1100 vector_free(varcomps
);
1103 vty_out (vty
, " %-*s %s\n", width
1104 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1105 desc
->str
? desc
->str
: "");
1109 if ((token
= token_cr
)) {
1111 vty_out(vty
, " %-s\n", token
->text
);
1112 else if (desc_width
>= strlen(token
->desc
))
1113 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1116 vty_describe_fold(vty
, width
, desc_width
, token
);
1120 cmd_free_strvec(vline
);
1122 vector_free(describe
);
1125 vty_redraw_line(vty
);
1128 static void vty_clear_buf(struct vty
*vty
)
1130 memset(vty
->buf
, 0, vty
->max
);
1133 /* ^C stop current input and do not add command line to the history. */
1134 static void vty_stop_input(struct vty
*vty
)
1136 vty
->cp
= vty
->length
= 0;
1141 vty_config_exit(vty
);
1142 vty
->node
= ENABLE_NODE
;
1147 /* Set history pointer to the latest one. */
1148 vty
->hp
= vty
->hindex
;
1151 /* Add current command line to the history buffer. */
1152 static void vty_hist_add(struct vty
*vty
)
1156 if (vty
->length
== 0)
1159 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1161 /* Ignore the same string as previous one. */
1162 if (vty
->hist
[index
])
1163 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1164 vty
->hp
= vty
->hindex
;
1168 /* Insert history entry. */
1169 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1170 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1172 /* History index rotation. */
1174 if (vty
->hindex
== VTY_MAXHIST
)
1177 vty
->hp
= vty
->hindex
;
1180 /* #define TELNET_OPTION_DEBUG */
1182 /* Get telnet window size. */
1183 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1185 #ifdef TELNET_OPTION_DEBUG
1188 for (i
= 0; i
< nbytes
; i
++) {
1191 vty_out(vty
, "IAC ");
1194 vty_out(vty
, "WILL ");
1197 vty_out(vty
, "WONT ");
1200 vty_out(vty
, "DO ");
1203 vty_out(vty
, "DONT ");
1206 vty_out(vty
, "SB ");
1209 vty_out(vty
, "SE ");
1212 vty_out(vty
, "TELOPT_ECHO \n");
1215 vty_out(vty
, "TELOPT_SGA \n");
1218 vty_out(vty
, "TELOPT_NAWS \n");
1221 vty_out(vty
, "%x ", buf
[i
]);
1227 #endif /* TELNET_OPTION_DEBUG */
1232 vty
->iac_sb_in_progress
= 1;
1235 if (!vty
->iac_sb_in_progress
)
1238 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1239 vty
->iac_sb_in_progress
= 0;
1242 switch (vty
->sb_buf
[0]) {
1244 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1247 "RFC 1073 violation detected: telnet NAWS option "
1248 "should send %d characters, but we received %lu",
1250 (unsigned long)vty
->sb_len
);
1251 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1254 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1255 (unsigned long)sizeof(vty
->sb_buf
),
1256 TELNET_NAWS_SB_LEN
);
1258 vty
->width
= ((vty
->sb_buf
[1] << 8)
1260 vty
->height
= ((vty
->sb_buf
[3] << 8)
1262 #ifdef TELNET_OPTION_DEBUG
1264 "TELNET NAWS window size negotiation completed: "
1265 "width %d, height %d\n",
1266 vty
->width
, vty
->height
);
1271 vty
->iac_sb_in_progress
= 0;
1280 /* Execute current command line. */
1281 static int vty_execute(struct vty
*vty
)
1287 switch (vty
->node
) {
1289 case AUTH_ENABLE_NODE
:
1290 vty_auth(vty
, vty
->buf
);
1293 ret
= vty_command(vty
, vty
->buf
);
1294 if (vty
->type
== VTY_TERM
)
1299 /* Clear command line buffer. */
1300 vty
->cp
= vty
->length
= 0;
1303 if (vty
->status
!= VTY_CLOSE
)
1309 #define CONTROL(X) ((X) - '@')
1310 #define VTY_NORMAL 0
1311 #define VTY_PRE_ESCAPE 1
1312 #define VTY_ESCAPE 2
1314 /* Escape character command map. */
1315 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1319 vty_previous_line(vty
);
1325 vty_forward_char(vty
);
1328 vty_backward_char(vty
);
1334 /* Go back to normal mode. */
1335 vty
->escape
= VTY_NORMAL
;
1338 /* Quit print out to the buffer. */
1339 static void vty_buffer_reset(struct vty
*vty
)
1341 buffer_reset(vty
->obuf
);
1342 buffer_reset(vty
->lbuf
);
1344 vty_redraw_line(vty
);
1347 /* Read data via vty socket. */
1348 static int vty_read(struct thread
*thread
)
1352 unsigned char buf
[VTY_READ_BUFSIZ
];
1354 int vty_sock
= THREAD_FD(thread
);
1355 struct vty
*vty
= THREAD_ARG(thread
);
1357 /* Read raw data from socket */
1358 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1360 if (ERRNO_IO_RETRY(errno
)) {
1361 vty_event(VTY_READ
, vty_sock
, vty
);
1364 vty
->monitor
= 0; /* disable monitoring to avoid
1365 infinite recursion */
1368 "%s: read error on vty client fd %d, closing: %s",
1369 __func__
, vty
->fd
, safe_strerror(errno
));
1370 buffer_reset(vty
->obuf
);
1371 buffer_reset(vty
->lbuf
);
1373 vty
->status
= VTY_CLOSE
;
1376 for (i
= 0; i
< nbytes
; i
++) {
1377 if (buf
[i
] == IAC
) {
1386 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1387 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1388 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1394 /* In case of telnet command */
1396 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1403 if (vty
->status
== VTY_MORE
) {
1408 vty_buffer_reset(vty
);
1410 #if 0 /* More line does not work for "show ip bgp". */
1413 vty
->status
= VTY_MORELINE
;
1422 /* Escape character. */
1423 if (vty
->escape
== VTY_ESCAPE
) {
1424 vty_escape_map(buf
[i
], vty
);
1428 /* Pre-escape status. */
1429 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1432 vty
->escape
= VTY_ESCAPE
;
1435 vty_backward_word(vty
);
1436 vty
->escape
= VTY_NORMAL
;
1439 vty_forward_word(vty
);
1440 vty
->escape
= VTY_NORMAL
;
1443 vty_forward_kill_word(vty
);
1444 vty
->escape
= VTY_NORMAL
;
1448 vty_backward_kill_word(vty
);
1449 vty
->escape
= VTY_NORMAL
;
1452 vty
->escape
= VTY_NORMAL
;
1460 vty_beginning_of_line(vty
);
1463 vty_backward_char(vty
);
1466 vty_stop_input(vty
);
1469 vty_delete_char(vty
);
1472 vty_end_of_line(vty
);
1475 vty_forward_char(vty
);
1479 vty_delete_backward_char(vty
);
1488 vty_previous_line(vty
);
1491 vty_transpose_chars(vty
);
1494 vty_kill_line_from_beginning(vty
);
1497 vty_backward_kill_word(vty
);
1500 vty_end_config(vty
);
1508 vty_complete_command(vty
);
1511 if (vty
->node
== AUTH_NODE
1512 || vty
->node
== AUTH_ENABLE_NODE
)
1513 vty_self_insert(vty
, buf
[i
]);
1515 vty_describe_command(vty
);
1518 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1519 vty
->escape
= VTY_ESCAPE
;
1522 vty
->escape
= VTY_PRE_ESCAPE
;
1525 if (buf
[i
] > 31 && buf
[i
] < 127)
1526 vty_self_insert(vty
, buf
[i
]);
1532 if (vty
->status
== VTY_CLOSE
)
1535 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1536 vty_event(VTY_READ
, vty_sock
, vty
);
1541 /* Flush buffer to the vty. */
1542 static int vty_flush(struct thread
*thread
)
1545 buffer_status_t flushrc
;
1546 int vty_sock
= THREAD_FD(thread
);
1547 struct vty
*vty
= THREAD_ARG(thread
);
1549 /* Tempolary disable read thread. */
1550 if (vty
->lines
== 0)
1551 THREAD_OFF(vty
->t_read
);
1553 /* Function execution continue. */
1554 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1556 /* N.B. if width is 0, that means we don't know the window size. */
1557 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1558 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1559 else if (vty
->status
== VTY_MORELINE
)
1560 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1563 flushrc
= buffer_flush_window(
1564 vty
->obuf
, vty_sock
, vty
->width
,
1565 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1569 0; /* disable monitoring to avoid infinite recursion */
1570 zlog_info("buffer_flush failed on vty client fd %d, closing",
1572 buffer_reset(vty
->lbuf
);
1573 buffer_reset(vty
->obuf
);
1577 if (vty
->status
== VTY_CLOSE
)
1580 vty
->status
= VTY_NORMAL
;
1581 if (vty
->lines
== 0)
1582 vty_event(VTY_READ
, vty_sock
, vty
);
1585 case BUFFER_PENDING
:
1586 /* There is more data waiting to be written. */
1587 vty
->status
= VTY_MORE
;
1588 if (vty
->lines
== 0)
1589 vty_event(VTY_WRITE
, vty_sock
, vty
);
1596 /* Allocate new vty struct. */
1597 struct vty
*vty_new(void)
1599 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1601 new->fd
= new->wfd
= -1;
1603 new->lbuf
= buffer_new(0);
1604 new->obuf
= buffer_new(0); /* Use default buffer size. */
1605 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1606 new->max
= VTY_BUFSIZ
;
1612 /* allocate and initialise vty */
1613 static struct vty
*vty_new_init(int vty_sock
)
1619 vty
->wfd
= vty_sock
;
1620 vty
->type
= VTY_TERM
;
1621 vty
->node
= AUTH_NODE
;
1626 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1629 vty
->xpath_index
= 0;
1630 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1631 vty
->private_config
= false;
1632 vty
->candidate_config
= vty_shared_candidate_config
;
1633 vector_set_index(vtyvec
, vty_sock
, vty
);
1634 vty
->status
= VTY_NORMAL
;
1637 vty
->iac_sb_in_progress
= 0;
1643 /* Create new vty structure. */
1644 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1646 char buf
[SU_ADDRSTRLEN
];
1649 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1651 /* Allocate new vty structure and set up default values. */
1652 vty
= vty_new_init(vty_sock
);
1654 /* configurable parameters not part of basic init */
1655 vty
->v_timeout
= vty_timeout_val
;
1656 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1657 if (no_password_check
) {
1659 vty
->node
= ENABLE_NODE
;
1661 vty
->node
= VIEW_NODE
;
1663 if (host
.lines
>= 0)
1664 vty
->lines
= host
.lines
;
1666 if (!no_password_check
) {
1667 /* Vty is not available if password isn't set. */
1668 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1669 vty_out(vty
, "Vty password is not set.\n");
1670 vty
->status
= VTY_CLOSE
;
1676 /* Say hello to the world. */
1678 if (!no_password_check
)
1679 vty_out(vty
, "\nUser Access Verification\n\n");
1681 /* Setting up terminal. */
1683 vty_will_suppress_go_ahead(vty
);
1685 vty_dont_linemode(vty
);
1686 vty_do_window_size(vty
);
1687 /* vty_dont_lflow_ahead (vty); */
1691 /* Add read/write thread. */
1692 vty_event(VTY_WRITE
, vty_sock
, vty
);
1693 vty_event(VTY_READ
, vty_sock
, vty
);
1698 /* create vty for stdio */
1699 static struct termios stdio_orig_termios
;
1700 static struct vty
*stdio_vty
= NULL
;
1701 static bool stdio_termios
= false;
1702 static void (*stdio_vty_atclose
)(int isexit
);
1704 static void vty_stdio_reset(int isexit
)
1708 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1709 stdio_termios
= false;
1713 if (stdio_vty_atclose
)
1714 stdio_vty_atclose(isexit
);
1715 stdio_vty_atclose
= NULL
;
1719 static void vty_stdio_atexit(void)
1724 void vty_stdio_suspend(void)
1729 THREAD_OFF(stdio_vty
->t_write
);
1730 THREAD_OFF(stdio_vty
->t_read
);
1731 THREAD_OFF(stdio_vty
->t_timeout
);
1734 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1735 stdio_termios
= false;
1738 void vty_stdio_resume(void)
1743 if (!tcgetattr(0, &stdio_orig_termios
)) {
1744 struct termios termios
;
1746 termios
= stdio_orig_termios
;
1747 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1748 | IGNCR
| ICRNL
| IXON
);
1749 termios
.c_oflag
&= ~OPOST
;
1750 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1751 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1752 termios
.c_cflag
|= CS8
;
1753 tcsetattr(0, TCSANOW
, &termios
);
1754 stdio_termios
= true;
1757 vty_prompt(stdio_vty
);
1759 /* Add read/write thread. */
1760 vty_event(VTY_WRITE
, 1, stdio_vty
);
1761 vty_event(VTY_READ
, 0, stdio_vty
);
1764 void vty_stdio_close(void)
1768 vty_close(stdio_vty
);
1771 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1775 /* refuse creating two vtys on stdio */
1779 vty
= stdio_vty
= vty_new_init(0);
1780 stdio_vty_atclose
= atclose
;
1783 /* always have stdio vty in a known _unchangeable_ state, don't want
1785 * to have any effect here to make sure scripting this works as intended
1787 vty
->node
= ENABLE_NODE
;
1789 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1795 /* Accept connection from the network. */
1796 static int vty_accept(struct thread
*thread
)
1804 struct access_list
*acl
= NULL
;
1805 char buf
[SU_ADDRSTRLEN
];
1807 accept_sock
= THREAD_FD(thread
);
1809 /* We continue hearing vty socket. */
1810 vty_event(VTY_SERV
, accept_sock
, NULL
);
1812 memset(&su
, 0, sizeof(union sockunion
));
1814 /* We can handle IPv4 or IPv6 socket. */
1815 vty_sock
= sockunion_accept(accept_sock
, &su
);
1817 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1818 safe_strerror(errno
));
1821 set_nonblocking(vty_sock
);
1822 set_cloexec(vty_sock
);
1824 sockunion2hostprefix(&su
, &p
);
1826 /* VTY's accesslist apply. */
1827 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1828 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1829 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1830 zlog_info("Vty connection refused from %s",
1831 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1834 /* continue accepting connections */
1835 vty_event(VTY_SERV
, accept_sock
, NULL
);
1841 /* VTY's ipv6 accesslist apply. */
1842 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1843 if ((acl
= access_list_lookup(AFI_IP6
,
1844 vty_ipv6_accesslist_name
))
1845 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1846 zlog_info("Vty connection refused from %s",
1847 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1850 /* continue accepting connections */
1851 vty_event(VTY_SERV
, accept_sock
, NULL
);
1858 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1861 zlog_info("can't set sockopt to vty_sock : %s",
1862 safe_strerror(errno
));
1864 zlog_info("Vty connection from %s",
1865 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1867 vty_create(vty_sock
, &su
);
1872 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1875 struct addrinfo req
;
1876 struct addrinfo
*ainfo
;
1877 struct addrinfo
*ainfo_save
;
1879 char port_str
[BUFSIZ
];
1881 memset(&req
, 0, sizeof(struct addrinfo
));
1882 req
.ai_flags
= AI_PASSIVE
;
1883 req
.ai_family
= AF_UNSPEC
;
1884 req
.ai_socktype
= SOCK_STREAM
;
1885 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1886 port_str
[sizeof(port_str
) - 1] = '\0';
1888 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1891 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1899 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1902 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1903 ainfo
->ai_protocol
);
1907 sockopt_v6only(ainfo
->ai_family
, sock
);
1908 sockopt_reuseaddr(sock
);
1909 sockopt_reuseport(sock
);
1912 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1914 close(sock
); /* Avoid sd leak. */
1918 ret
= listen(sock
, 3);
1920 close(sock
); /* Avoid sd leak. */
1924 vty_event(VTY_SERV
, sock
, NULL
);
1925 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1927 freeaddrinfo(ainfo_save
);
1931 /* For sockaddr_un. */
1934 /* VTY shell UNIX domain socket. */
1935 static void vty_serv_un(const char *path
)
1939 struct sockaddr_un serv
;
1941 struct zprivs_ids_t ids
;
1943 /* First of all, unlink existing socket */
1947 old_mask
= umask(0007);
1949 /* Make UNIX domain socket. */
1950 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1952 flog_err_sys(EC_LIB_SOCKET
,
1953 "Cannot create unix stream socket: %s",
1954 safe_strerror(errno
));
1958 /* Make server socket. */
1959 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1960 serv
.sun_family
= AF_UNIX
;
1961 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1962 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1963 len
= serv
.sun_len
= SUN_LEN(&serv
);
1965 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1966 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1970 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1972 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1973 safe_strerror(errno
));
1974 close(sock
); /* Avoid sd leak. */
1978 ret
= listen(sock
, 5);
1980 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1981 safe_strerror(errno
));
1982 close(sock
); /* Avoid sd leak. */
1988 zprivs_get_ids(&ids
);
1990 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1991 earlier for the case when we don't need to chown the file
1992 type casting it here to make a compare */
1993 if ((int)ids
.gid_vty
> 0) {
1994 /* set group of socket */
1995 if (chown(path
, -1, ids
.gid_vty
)) {
1996 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1997 "vty_serv_un: could chown socket, %s",
1998 safe_strerror(errno
));
2002 vty_event(VTYSH_SERV
, sock
, NULL
);
2005 /* #define VTYSH_DEBUG 1 */
2007 static int vtysh_accept(struct thread
*thread
)
2012 struct sockaddr_un client
;
2015 accept_sock
= THREAD_FD(thread
);
2017 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2019 memset(&client
, 0, sizeof(struct sockaddr_un
));
2020 client_len
= sizeof(struct sockaddr_un
);
2022 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2023 (socklen_t
*)&client_len
);
2026 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2027 safe_strerror(errno
));
2031 if (set_nonblocking(sock
) < 0) {
2034 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2035 sock
, safe_strerror(errno
));
2042 printf("VTY shell accept\n");
2043 #endif /* VTYSH_DEBUG */
2048 vty
->type
= VTY_SHELL_SERV
;
2049 vty
->node
= VIEW_NODE
;
2051 vty_event(VTYSH_READ
, sock
, vty
);
2056 static int vtysh_flush(struct vty
*vty
)
2058 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2059 case BUFFER_PENDING
:
2060 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2064 0; /* disable monitoring to avoid infinite recursion */
2065 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2067 buffer_reset(vty
->lbuf
);
2068 buffer_reset(vty
->obuf
);
2077 static int vtysh_read(struct thread
*thread
)
2083 unsigned char buf
[VTY_READ_BUFSIZ
];
2085 uint8_t header
[4] = {0, 0, 0, 0};
2087 sock
= THREAD_FD(thread
);
2088 vty
= THREAD_ARG(thread
);
2090 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2092 if (ERRNO_IO_RETRY(errno
)) {
2093 vty_event(VTYSH_READ
, sock
, vty
);
2096 vty
->monitor
= 0; /* disable monitoring to avoid
2097 infinite recursion */
2100 "%s: read failed on vtysh client fd %d, closing: %s",
2101 __func__
, sock
, safe_strerror(errno
));
2103 buffer_reset(vty
->lbuf
);
2104 buffer_reset(vty
->obuf
);
2107 printf("close vtysh\n");
2108 #endif /* VTYSH_DEBUG */
2113 printf("line: %.*s\n", nbytes
, buf
);
2114 #endif /* VTYSH_DEBUG */
2116 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2117 /* Clear command line buffer. */
2118 vty
->cp
= vty
->length
= 0;
2120 vty_out(vty
, "%% Command is too long.\n");
2122 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2123 vty
->buf
[vty
->length
++] = *p
;
2125 /* Pass this line to parser. */
2126 ret
= vty_execute(vty
);
2127 /* Note that vty_execute clears the command buffer and resets
2128 vty->length to 0. */
2130 /* Return result. */
2132 printf("result: %d\n", ret
);
2133 printf("vtysh node: %d\n", vty
->node
);
2134 #endif /* VTYSH_DEBUG */
2136 /* hack for asynchronous "write integrated"
2137 * - other commands in "buf" will be ditched
2138 * - input during pending config-write is
2140 if (ret
== CMD_SUSPEND
)
2143 /* warning: watchfrr hardcodes this result write
2146 buffer_put(vty
->obuf
, header
, 4);
2148 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2149 /* Try to flush results; exit if a write
2156 if (vty
->status
== VTY_CLOSE
)
2159 vty_event(VTYSH_READ
, sock
, vty
);
2164 static int vtysh_write(struct thread
*thread
)
2166 struct vty
*vty
= THREAD_ARG(thread
);
2174 /* Determine address family to bind. */
2175 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2177 /* If port is set to 0, do not listen on TCP/IP at all! */
2179 vty_serv_sock_addrinfo(addr
, port
);
2186 static void vty_error_delete(void *arg
)
2188 struct vty_error
*ve
= arg
;
2190 XFREE(MTYPE_TMP
, ve
);
2193 /* Close vty interface. Warning: call this only from functions that
2194 will be careful not to access the vty afterwards (since it has
2195 now been freed). This is safest from top-level functions (called
2196 directly by the thread dispatcher). */
2197 void vty_close(struct vty
*vty
)
2200 bool was_stdio
= false;
2202 /* Drop out of configure / transaction if needed. */
2203 vty_config_exit(vty
);
2205 /* Cancel threads.*/
2206 THREAD_OFF(vty
->t_read
);
2207 THREAD_OFF(vty
->t_write
);
2208 THREAD_OFF(vty
->t_timeout
);
2211 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2213 /* Free input buffer. */
2214 buffer_free(vty
->obuf
);
2215 buffer_free(vty
->lbuf
);
2217 /* Free command history. */
2218 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2219 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2224 vector_unset(vtyvec
, vty
->fd
);
2226 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2230 * note check is for fd > STDERR_FILENO, not fd != -1.
2231 * We never close stdin/stdout/stderr here, because we may be
2232 * running in foreground mode with logging to stdout. Also,
2233 * additionally, we'd need to replace these fds with /dev/null. */
2234 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2236 if (vty
->fd
> STDERR_FILENO
)
2238 if (vty
->fd
== STDIN_FILENO
)
2241 XFREE(MTYPE_VTY
, vty
->buf
);
2244 vty
->error
->del
= vty_error_delete
;
2245 list_delete(&vty
->error
);
2249 XFREE(MTYPE_VTY
, vty
);
2255 /* When time out occur output message then close connection. */
2256 static int vty_timeout(struct thread
*thread
)
2260 vty
= THREAD_ARG(thread
);
2264 buffer_reset(vty
->lbuf
);
2265 buffer_reset(vty
->obuf
);
2266 vty_out(vty
, "\nVty connection is timed out.\n");
2268 /* Close connection. */
2269 vty
->status
= VTY_CLOSE
;
2275 /* Read up configuration file from file_name. */
2276 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2280 struct vty_error
*ve
;
2281 struct listnode
*node
;
2282 unsigned int line_num
= 0;
2285 /* vty_close won't close stderr; if some config command prints
2286 * something it'll end up there. (not ideal; it'd be beter if output
2287 * from a file-load went to logging instead. Also note that if this
2288 * function is called after daemonizing, stderr will be /dev/null.)
2290 * vty->fd will be -1 from vty_new()
2292 vty
->wfd
= STDERR_FILENO
;
2293 vty
->type
= VTY_FILE
;
2294 vty
->node
= CONFIG_NODE
;
2297 vty
->candidate_config
= config
;
2299 vty
->private_config
= true;
2300 vty
->candidate_config
= nb_config_new(NULL
);
2303 /* Execute configuration file */
2304 ret
= config_from_file(vty
, confp
, &line_num
);
2306 /* Flush any previous errors before printing messages below */
2307 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2309 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2310 const char *message
= NULL
;
2314 case CMD_ERR_AMBIGUOUS
:
2315 message
= "Ambiguous command";
2317 case CMD_ERR_NO_MATCH
:
2318 message
= "No such command";
2321 message
= "Command returned Warning";
2323 case CMD_WARNING_CONFIG_FAILED
:
2324 message
= "Command returned Warning Config Failed";
2326 case CMD_ERR_INCOMPLETE
:
2327 message
= "Command returned Incomplete";
2329 case CMD_ERR_EXEED_ARGC_MAX
:
2331 "Command exceeded maximum number of Arguments";
2334 message
= "Command returned unhandled error message";
2338 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2339 nl
= strchr(ve
->error_buf
, '\n');
2342 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2343 message
, ve
->line_num
, ve
->error_buf
);
2348 * Automatically commit the candidate configuration after
2349 * reading the configuration file.
2351 if (config
== NULL
) {
2352 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2353 vty
, true, "Read configuration file",
2355 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2356 zlog_err("%s: failed to read configuration file.",
2363 static FILE *vty_use_backup_config(const char *fullpath
)
2365 char *fullpath_sav
, *fullpath_tmp
;
2371 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2372 fullpath_sav
= malloc(fullpath_sav_sz
);
2373 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2374 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2376 sav
= open(fullpath_sav
, O_RDONLY
);
2382 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2383 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2385 /* Open file to configuration write. */
2386 tmp
= mkstemp(fullpath_tmp
);
2390 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2393 while ((c
= read(sav
, buffer
, 512)) > 0) {
2394 if (write(tmp
, buffer
, c
) <= 0)
2400 if (rename(fullpath_tmp
, fullpath
) == 0)
2401 ret
= fopen(fullpath
, "r");
2403 unlink(fullpath_tmp
);
2408 unlink(fullpath_tmp
);
2418 /* Read up configuration file from file_name. */
2419 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2420 char *config_default_dir
)
2422 char cwd
[MAXPATHLEN
];
2424 const char *fullpath
;
2426 bool read_success
= false;
2428 /* If -f flag specified. */
2429 if (config_file
!= NULL
) {
2430 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2431 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2434 "%s: failure to determine Current Working Directory %d!",
2436 goto tmp_free_and_out
;
2438 tmp
= XMALLOC(MTYPE_TMP
,
2439 strlen(cwd
) + strlen(config_file
) + 2);
2440 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2443 fullpath
= config_file
;
2445 confp
= fopen(fullpath
, "r");
2447 if (confp
== NULL
) {
2449 EC_LIB_BACKUP_CONFIG
,
2450 "%s: failed to open configuration file %s: %s, checking backup",
2451 __func__
, fullpath
, safe_strerror(errno
));
2453 confp
= vty_use_backup_config(fullpath
);
2456 EC_LIB_BACKUP_CONFIG
,
2457 "WARNING: using backup configuration file!");
2461 "%s: can't open configuration file [%s]",
2462 __func__
, config_file
);
2463 goto tmp_free_and_out
;
2468 host_config_set(config_default_dir
);
2472 struct stat conf_stat
;
2474 /* !!!!PLEASE LEAVE!!!!
2475 * This is NEEDED for use with vtysh -b, or else you can get
2476 * a real configuration food fight with a lot garbage in the
2477 * merged configuration file it creates coming from the per
2478 * daemon configuration files. This also allows the daemons
2479 * to start if there default configuration file is not
2480 * present or ignore them, as needed when using vtysh -b to
2481 * configure the daemons at boot - MAG
2484 /* Stat for vtysh Zebra.conf, if found startup and wait for
2485 * boot configuration
2488 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2489 ret
= stat(integrate_default
, &conf_stat
);
2491 read_success
= true;
2492 goto tmp_free_and_out
;
2496 confp
= fopen(config_default_dir
, "r");
2497 if (confp
== NULL
) {
2500 "%s: failed to open configuration file %s: %s, checking backup",
2501 __func__
, config_default_dir
,
2502 safe_strerror(errno
));
2504 confp
= vty_use_backup_config(config_default_dir
);
2507 EC_LIB_BACKUP_CONFIG
,
2508 "WARNING: using backup configuration file!");
2509 fullpath
= config_default_dir
;
2511 flog_err(EC_LIB_VTY
,
2512 "can't open configuration file [%s]",
2513 config_default_dir
);
2514 goto tmp_free_and_out
;
2517 fullpath
= config_default_dir
;
2520 vty_read_file(config
, confp
);
2521 read_success
= true;
2525 host_config_set(fullpath
);
2528 XFREE(MTYPE_TMP
, tmp
);
2530 return read_success
;
2533 /* Small utility function which output log to the VTY. */
2534 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2535 struct timestamp_control
*ctl
)
2543 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2544 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2546 vty_log_out(vty
, level
, proto_str
, msg
, ctl
);
2549 /* Async-signal-safe version of vty_log for fixed strings. */
2550 void vty_log_fixed(char *buf
, size_t len
)
2553 struct iovec iov
[2];
2554 char crlf
[4] = "\r\n";
2556 /* vty may not have been initialised */
2560 iov
[0].iov_base
= buf
;
2561 iov
[0].iov_len
= len
;
2562 iov
[1].iov_base
= crlf
;
2565 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2567 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2568 /* N.B. We don't care about the return code, since
2570 most likely just about to die anyway. */
2571 if (writev(vty
->wfd
, iov
, 2) == -1) {
2572 fprintf(stderr
, "Failure to writev: %d\n",
2579 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2581 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2582 vty_out(vty
, "%% Configuration is locked by other client\n");
2586 vty
->node
= CONFIG_NODE
;
2588 vty
->private_config
= private_config
;
2589 vty
->xpath_index
= 0;
2591 if (private_config
) {
2592 vty
->candidate_config
= nb_config_dup(running_config
);
2593 vty
->candidate_config_base
= nb_config_dup(running_config
);
2595 "Warning: uncommitted changes will be discarded on exit.\n\n");
2597 vty
->candidate_config
= vty_shared_candidate_config
;
2598 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2599 vty
->candidate_config_base
=
2600 nb_config_dup(running_config
);
2606 void vty_config_exit(struct vty
*vty
)
2608 enum node_type node
= vty
->node
;
2609 struct cmd_node
*cnode
;
2611 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2612 * somewhere below CONFIG_NODE */
2613 while (node
&& node
!= CONFIG_NODE
) {
2614 cnode
= vector_lookup(cmdvec
, node
);
2615 node
= cnode
->parent_node
;
2617 if (node
!= CONFIG_NODE
)
2618 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2621 while (vty
->node
!= ENABLE_NODE
)
2622 /* will call vty_config_node_exit() below */
2626 int vty_config_node_exit(struct vty
*vty
)
2628 vty
->xpath_index
= 0;
2630 /* Check if there's a pending confirmed commit. */
2631 if (vty
->t_confirmed_commit_timeout
) {
2633 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2634 nb_cli_confirmed_commit_rollback(vty
);
2635 nb_cli_confirmed_commit_clean(vty
);
2638 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2640 if (vty
->candidate_config
) {
2641 if (vty
->private_config
)
2642 nb_config_free(vty
->candidate_config
);
2643 vty
->candidate_config
= NULL
;
2645 if (vty
->candidate_config_base
) {
2646 nb_config_free(vty
->candidate_config_base
);
2647 vty
->candidate_config_base
= NULL
;
2650 vty
->config
= false;
2654 /* Master of the threads. */
2655 static struct thread_master
*vty_master
;
2657 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2659 struct thread
*vty_serv_thread
= NULL
;
2663 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2665 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2669 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2671 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2674 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2678 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2683 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2685 /* Time out treatment. */
2686 if (vty
->v_timeout
) {
2687 THREAD_OFF(vty
->t_timeout
);
2688 thread_add_timer(vty_master
, vty_timeout
, vty
,
2689 vty
->v_timeout
, &vty
->t_timeout
);
2693 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2696 case VTY_TIMEOUT_RESET
:
2697 THREAD_OFF(vty
->t_timeout
);
2699 thread_add_timer(vty_master
, vty_timeout
, vty
,
2700 vty
->v_timeout
, &vty
->t_timeout
);
2705 DEFUN_NOSH (config_who
,
2708 "Display who is on vty\n")
2713 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2714 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2715 vty_out(vty
, "%svty[%d] connected from %s.\n",
2716 v
->config
? "*" : " ", i
, v
->address
);
2720 /* Move to vty configuration mode. */
2721 DEFUN_NOSH (line_vty
,
2724 "Configure a terminal line\n"
2725 "Virtual terminal\n")
2727 vty
->node
= VTY_NODE
;
2731 /* Set time out value. */
2732 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2733 const char *sec_str
)
2735 unsigned long timeout
= 0;
2737 /* min_str and sec_str are already checked by parser. So it must be
2738 all digit string. */
2740 timeout
= strtol(min_str
, NULL
, 10);
2744 timeout
+= strtol(sec_str
, NULL
, 10);
2746 vty_timeout_val
= timeout
;
2747 vty
->v_timeout
= timeout
;
2748 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2754 DEFUN (exec_timeout_min
,
2755 exec_timeout_min_cmd
,
2756 "exec-timeout (0-35791)",
2757 "Set timeout value\n"
2758 "Timeout value in minutes\n")
2761 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2764 DEFUN (exec_timeout_sec
,
2765 exec_timeout_sec_cmd
,
2766 "exec-timeout (0-35791) (0-2147483)",
2767 "Set the EXEC timeout\n"
2768 "Timeout in minutes\n"
2769 "Timeout in seconds\n")
2772 int idx_number_2
= 2;
2773 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2774 argv
[idx_number_2
]->arg
);
2777 DEFUN (no_exec_timeout
,
2778 no_exec_timeout_cmd
,
2781 "Set the EXEC timeout\n")
2783 return exec_timeout(vty
, NULL
, NULL
);
2786 /* Set vty access class. */
2787 DEFUN (vty_access_class
,
2788 vty_access_class_cmd
,
2789 "access-class WORD",
2790 "Filter connections based on an IP access list\n"
2794 if (vty_accesslist_name
)
2795 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2797 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2802 /* Clear vty access class. */
2803 DEFUN (no_vty_access_class
,
2804 no_vty_access_class_cmd
,
2805 "no access-class [WORD]",
2807 "Filter connections based on an IP access list\n"
2811 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2812 if (!vty_accesslist_name
2813 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2814 vty_out(vty
, "Access-class is not currently applied to vty\n");
2815 return CMD_WARNING_CONFIG_FAILED
;
2818 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2820 vty_accesslist_name
= NULL
;
2825 /* Set vty access class. */
2826 DEFUN (vty_ipv6_access_class
,
2827 vty_ipv6_access_class_cmd
,
2828 "ipv6 access-class WORD",
2830 "Filter connections based on an IP access list\n"
2831 "IPv6 access list\n")
2834 if (vty_ipv6_accesslist_name
)
2835 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2837 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2842 /* Clear vty access class. */
2843 DEFUN (no_vty_ipv6_access_class
,
2844 no_vty_ipv6_access_class_cmd
,
2845 "no ipv6 access-class [WORD]",
2848 "Filter connections based on an IP access list\n"
2849 "IPv6 access list\n")
2852 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2854 if (!vty_ipv6_accesslist_name
2855 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2857 "IPv6 access-class is not currently applied to vty\n");
2858 return CMD_WARNING_CONFIG_FAILED
;
2861 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2863 vty_ipv6_accesslist_name
= NULL
;
2872 "Enable password checking\n")
2874 no_password_check
= 0;
2878 DEFUN (no_vty_login
,
2882 "Enable password checking\n")
2884 no_password_check
= 1;
2888 DEFUN (service_advanced_vty
,
2889 service_advanced_vty_cmd
,
2890 "service advanced-vty",
2891 "Set up miscellaneous service\n"
2892 "Enable advanced mode vty interface\n")
2898 DEFUN (no_service_advanced_vty
,
2899 no_service_advanced_vty_cmd
,
2900 "no service advanced-vty",
2902 "Set up miscellaneous service\n"
2903 "Enable advanced mode vty interface\n")
2909 DEFUN_NOSH (terminal_monitor
,
2910 terminal_monitor_cmd
,
2912 "Set terminal line parameters\n"
2913 "Copy debug output to the current terminal line\n")
2919 DEFUN_NOSH (terminal_no_monitor
,
2920 terminal_no_monitor_cmd
,
2921 "terminal no monitor",
2922 "Set terminal line parameters\n"
2924 "Copy debug output to the current terminal line\n")
2930 DEFUN_NOSH (no_terminal_monitor
,
2931 no_terminal_monitor_cmd
,
2932 "no terminal monitor",
2934 "Set terminal line parameters\n"
2935 "Copy debug output to the current terminal line\n")
2937 return terminal_no_monitor(self
, vty
, argc
, argv
);
2941 DEFUN_NOSH (show_history
,
2945 "Display the session command history\n")
2949 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2950 if (index
== VTY_MAXHIST
) {
2955 if (vty
->hist
[index
] != NULL
)
2956 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2965 DEFPY (log_commands
,
2967 "[no] log commands",
2970 "Log all commands\n")
2973 if (do_log_commands_perm
) {
2975 "Daemon started with permanent logging turned on for commands, ignoring\n");
2979 do_log_commands
= false;
2981 do_log_commands
= true;
2986 /* Display current configuration. */
2987 static int vty_config_write(struct vty
*vty
)
2989 vty_out(vty
, "line vty\n");
2991 if (vty_accesslist_name
)
2992 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2994 if (vty_ipv6_accesslist_name
)
2995 vty_out(vty
, " ipv6 access-class %s\n",
2996 vty_ipv6_accesslist_name
);
2999 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3000 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3001 vty_timeout_val
% 60);
3004 if (no_password_check
)
3005 vty_out(vty
, " no login\n");
3007 if (do_log_commands
)
3008 vty_out(vty
, "log commands\n");
3010 vty_out(vty
, "!\n");
3015 static int vty_config_write(struct vty
*vty
);
3016 struct cmd_node vty_node
= {
3019 .parent_node
= CONFIG_NODE
,
3020 .prompt
= "%s(config-line)# ",
3021 .config_write
= vty_config_write
,
3024 /* Reset all VTY status. */
3025 void vty_reset(void)
3029 struct thread
*vty_serv_thread
;
3031 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3032 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3033 buffer_reset(vty
->lbuf
);
3034 buffer_reset(vty
->obuf
);
3035 vty
->status
= VTY_CLOSE
;
3039 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3040 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3042 THREAD_OFF(vty_serv_thread
);
3043 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3047 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3049 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3050 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3053 static void vty_save_cwd(void)
3057 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3061 * At this point if these go wrong, more than likely
3062 * the whole world is coming down around us
3063 * Hence not worrying about it too much.
3065 if (!chdir(SYSCONFDIR
)) {
3066 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3067 "Failure to chdir to %s, errno: %d",
3071 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3072 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3073 "Failure to getcwd, errno: %d", errno
);
3079 char *vty_get_cwd(void)
3084 int vty_shell(struct vty
*vty
)
3086 return vty
->type
== VTY_SHELL
? 1 : 0;
3089 int vty_shell_serv(struct vty
*vty
)
3091 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3094 void vty_init_vtysh(void)
3096 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3099 /* Install vty's own commands like `who' command. */
3100 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3102 /* For further configuration read, preserve current directory. */
3105 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3107 vty_master
= master_thread
;
3109 atexit(vty_stdio_atexit
);
3111 /* Initilize server thread vector. */
3112 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3114 /* Install bgp top node. */
3115 install_node(&vty_node
);
3117 install_element(VIEW_NODE
, &config_who_cmd
);
3118 install_element(VIEW_NODE
, &show_history_cmd
);
3119 install_element(CONFIG_NODE
, &line_vty_cmd
);
3120 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3121 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3122 install_element(CONFIG_NODE
, &show_history_cmd
);
3123 install_element(CONFIG_NODE
, &log_commands_cmd
);
3125 if (do_command_logging
) {
3126 do_log_commands
= true;
3127 do_log_commands_perm
= true;
3130 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3131 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3132 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3134 install_default(VTY_NODE
);
3135 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3136 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3137 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3138 install_element(VTY_NODE
, &vty_access_class_cmd
);
3139 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3140 install_element(VTY_NODE
, &vty_login_cmd
);
3141 install_element(VTY_NODE
, &no_vty_login_cmd
);
3142 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3143 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3146 void vty_terminate(void)
3148 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3150 if (vtyvec
&& Vvty_serv_thread
) {
3152 vector_free(vtyvec
);
3153 vector_free(Vvty_serv_thread
);
3155 Vvty_serv_thread
= NULL
;