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>
33 #include "sockunion.h"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
46 #include <arpa/telnet.h>
49 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
50 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
51 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
66 static void vty_event(enum event
, int, struct vty
*);
68 /* Extern host structure from command.c */
69 extern struct host host
;
71 /* Vector which store each vty structure. */
74 /* Vty timeout value. */
75 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
77 /* Vty access-class command */
78 static char *vty_accesslist_name
= NULL
;
80 /* Vty access-calss for IPv6. */
81 static char *vty_ipv6_accesslist_name
= NULL
;
83 /* VTY server thread. */
84 static vector Vvty_serv_thread
;
86 /* Current directory. */
89 /* Exclusive configuration lock. */
90 struct vty
*vty_exclusive_lock
;
92 /* Login password check. */
93 static int no_password_check
= 0;
95 /* Integrated configuration file path */
96 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
98 static int do_log_commands
= 0;
100 void vty_frame(struct vty
*vty
, const char *format
, ...)
104 va_start(args
, format
);
105 vsnprintf(vty
->frame
+ vty
->frame_pos
,
106 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
107 vty
->frame_pos
= strlen(vty
->frame
);
111 void vty_endframe(struct vty
*vty
, const char *endtext
)
113 if (vty
->frame_pos
== 0 && endtext
)
114 vty_out(vty
, "%s", endtext
);
118 bool vty_set_include(struct vty
*vty
, const char *regexp
)
126 regfree(&vty
->include
);
132 errcode
= regcomp(&vty
->include
, regexp
,
133 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
136 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
137 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
145 /* VTY standard output function. */
146 int vty_out(struct vty
*vty
, const char *format
, ...)
155 if (vty
->frame_pos
) {
157 vty_out(vty
, "%s", vty
->frame
);
160 /* Try to write to initial buffer. */
161 va_start(args
, format
);
162 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
165 /* Initial buffer is not enough. */
166 if (len
< 0 || len
>= size
) {
173 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
177 va_start(args
, format
);
178 len
= vsnprintf(p
, size
, format
, args
);
181 if (len
> -1 && len
< size
)
186 /* When initial buffer is enough to store all output. */
192 vector lines
= frrstr_split_vec(p
, "\n");
194 /* Place first value in the cache */
195 char *firstline
= vector_slot(lines
, 0);
196 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
198 /* If our split returned more than one entry, time to filter */
199 if (vector_active(lines
) > 1) {
201 * returned string is MTYPE_TMP so it matches the MTYPE
202 * of everything else in the vector
204 char *bstr
= buffer_getstr(vty
->lbuf
);
205 buffer_reset(vty
->lbuf
);
206 XFREE(MTYPE_TMP
, lines
->index
[0]);
207 vector_set_index(lines
, 0, bstr
);
208 frrstr_filter_vec(lines
, &vty
->include
);
209 vector_compact(lines
);
211 * Consider the string "foo\n". If the regex is an empty string
212 * and the line ended with a newline, then the vector will look
218 * If the regex isn't empty, the vector will look like:
222 * In this case we'd like to preserve the newline, so we add
223 * the empty string [1] as in the first example.
225 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
226 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
227 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
229 filtered
= frrstr_join_vec(lines
, "\n");
235 frrstr_strvec_free(lines
);
246 /* print with crlf replacement */
247 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
251 fprintf(vty
->of
, "%s", filtered
);
257 /* print without crlf replacement */
258 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
264 if (vty
->filter
&& filtered
)
265 XFREE(MTYPE_TMP
, filtered
);
267 /* If p is not different with buf, it is allocated buffer. */
269 XFREE(MTYPE_VTY_OUT_BUF
, p
);
274 static int vty_log_out(struct vty
*vty
, const char *level
,
275 const char *proto_str
, const char *format
,
276 struct timestamp_control
*ctl
, va_list va
)
282 if (!ctl
->already_rendered
) {
283 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
285 ctl
->already_rendered
= 1;
287 if (ctl
->len
+ 1 >= sizeof(buf
))
289 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
294 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
297 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
298 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
301 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
302 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
308 if (write(vty
->wfd
, buf
, len
) < 0) {
309 if (ERRNO_IO_RETRY(errno
))
310 /* Kernel buffer is full, probably too much debugging
312 drop the data and ignore. */
314 /* Fatal I/O error. */
316 0; /* disable monitoring to avoid infinite recursion */
317 flog_err(EC_LIB_SOCKET
,
318 "%s: write failed to vty client fd %d, closing: %s",
319 __func__
, vty
->fd
, safe_strerror(errno
));
320 buffer_reset(vty
->obuf
);
321 buffer_reset(vty
->lbuf
);
322 /* cannot call vty_close, because a parent routine may still try
323 to access the vty struct */
324 vty
->status
= VTY_CLOSE
;
325 shutdown(vty
->fd
, SHUT_RDWR
);
331 /* Output current time to the vty. */
332 void vty_time_print(struct vty
*vty
, int cr
)
334 char buf
[QUAGGA_TIMESTAMP_LEN
];
336 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
337 zlog_info("quagga_timestamp error");
341 vty_out(vty
, "%s\n", buf
);
343 vty_out(vty
, "%s ", buf
);
348 /* Say hello to vty interface. */
349 void vty_hello(struct vty
*vty
)
355 f
= fopen(host
.motdfile
, "r");
357 while (fgets(buf
, sizeof(buf
), f
)) {
359 /* work backwards to ignore trailling isspace()
361 for (s
= buf
+ strlen(buf
);
362 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
365 vty_out(vty
, "%s\n", buf
);
369 vty_out(vty
, "MOTD file not found\n");
370 } else if (host
.motd
)
371 vty_out(vty
, "%s", host
.motd
);
374 /* Put out prompt and wait input from user. */
375 static void vty_prompt(struct vty
*vty
)
377 if (vty
->type
== VTY_TERM
) {
378 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
382 /* Send WILL TELOPT_ECHO to remote server. */
383 static void vty_will_echo(struct vty
*vty
)
385 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
386 vty_out(vty
, "%s", cmd
);
389 /* Make suppress Go-Ahead telnet option. */
390 static void vty_will_suppress_go_ahead(struct vty
*vty
)
392 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
393 vty_out(vty
, "%s", cmd
);
396 /* Make don't use linemode over telnet. */
397 static void vty_dont_linemode(struct vty
*vty
)
399 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
400 vty_out(vty
, "%s", cmd
);
403 /* Use window size. */
404 static void vty_do_window_size(struct vty
*vty
)
406 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
407 vty_out(vty
, "%s", cmd
);
410 #if 0 /* Currently not used. */
411 /* Make don't use lflow vty interface. */
413 vty_dont_lflow_ahead (struct vty
*vty
)
415 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
416 vty_out (vty
, "%s", cmd
);
420 /* Authentication of vty */
421 static void vty_auth(struct vty
*vty
, char *buf
)
424 enum node_type next_node
= 0;
426 char *crypt(const char *, const char *);
431 passwd
= host
.password_encrypt
;
433 passwd
= host
.password
;
435 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
437 next_node
= VIEW_NODE
;
439 case AUTH_ENABLE_NODE
:
441 passwd
= host
.enable_encrypt
;
443 passwd
= host
.enable
;
444 next_node
= ENABLE_NODE
;
450 fail
= strcmp(crypt(buf
, passwd
), passwd
);
452 fail
= strcmp(buf
, passwd
);
458 vty
->node
= next_node
; /* Success ! */
461 if (vty
->fail
>= 3) {
462 if (vty
->node
== AUTH_NODE
) {
464 "%% Bad passwords, too many failures!\n");
465 vty
->status
= VTY_CLOSE
;
467 /* AUTH_ENABLE_NODE */
470 "%% Bad enable passwords, too many failures!\n");
471 vty
->status
= VTY_CLOSE
;
477 /* Command execution over the vty interface. */
478 static int vty_command(struct vty
*vty
, char *buf
)
481 const char *protocolname
;
487 * Log non empty command lines
492 /* Skip white spaces. */
493 while (isspace((int)*cp
) && *cp
!= '\0')
496 if (cp
!= NULL
&& *cp
!= '\0') {
498 char vty_str
[VTY_BUFSIZ
];
499 char prompt_str
[VTY_BUFSIZ
];
501 /* format the base vty info */
502 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
504 for (i
= 0; i
< vector_active(vtyvec
); i
++)
505 if (vty
== vector_slot(vtyvec
, i
)) {
506 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
511 /* format the prompt */
512 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
515 /* now log the command */
516 zlog_notice("%s%s", prompt_str
, buf
);
519 #ifdef CONSUMED_TIME_CHECK
523 unsigned long realtime
, cputime
;
526 #endif /* CONSUMED_TIME_CHECK */
528 ret
= cmd_execute(vty
, buf
, NULL
, 0);
530 /* Get the name of the protocol if any */
531 protocolname
= frr_protoname
;
533 #ifdef CONSUMED_TIME_CHECK
535 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
536 > CONSUMED_TIME_CHECK
)
537 /* Warn about CPU hog that must be fixed. */
540 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
541 realtime
/ 1000, cputime
/ 1000, buf
);
543 #endif /* CONSUMED_TIME_CHECK */
545 if (ret
!= CMD_SUCCESS
)
548 if (vty
->type
== VTY_FILE
)
549 vty_out(vty
, "Warning...\n");
551 case CMD_ERR_AMBIGUOUS
:
552 vty_out(vty
, "%% Ambiguous command.\n");
554 case CMD_ERR_NO_MATCH
:
555 vty_out(vty
, "%% [%s] Unknown command: %s\n",
558 case CMD_ERR_INCOMPLETE
:
559 vty_out(vty
, "%% Command incomplete.\n");
566 static const char telnet_backward_char
= 0x08;
567 static const char telnet_space_char
= ' ';
569 /* Basic function to write buffer to vty. */
570 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
572 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
575 /* Should we do buffering here ? And make vty_flush (vty) ? */
576 buffer_put(vty
->obuf
, buf
, nbytes
);
579 /* Basic function to insert character into vty. */
580 static void vty_self_insert(struct vty
*vty
, char c
)
585 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
588 length
= vty
->length
- vty
->cp
;
589 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
590 vty
->buf
[vty
->cp
] = c
;
592 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
593 for (i
= 0; i
< length
; i
++)
594 vty_write(vty
, &telnet_backward_char
, 1);
599 vty
->buf
[vty
->length
] = '\0';
602 /* Self insert character 'c' in overwrite mode. */
603 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
605 if (vty
->cp
== vty
->length
) {
606 vty_self_insert(vty
, c
);
610 vty
->buf
[vty
->cp
++] = c
;
611 vty_write(vty
, &c
, 1);
615 * Insert a string into vty->buf at the current cursor position.
617 * If the resultant string would be larger than VTY_BUFSIZ it is
620 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
622 if (vty
->cp
== VTY_BUFSIZ
)
625 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
626 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
628 vty
->length
= MAX(vty
->cp
, vty
->length
);
629 vty
->buf
[vty
->length
] = '\0';
630 vty_write(vty
, str
, nwrite
);
633 /* Forward character. */
634 static void vty_forward_char(struct vty
*vty
)
636 if (vty
->cp
< vty
->length
) {
637 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
642 /* Backward character. */
643 static void vty_backward_char(struct vty
*vty
)
647 vty_write(vty
, &telnet_backward_char
, 1);
651 /* Move to the beginning of the line. */
652 static void vty_beginning_of_line(struct vty
*vty
)
655 vty_backward_char(vty
);
658 /* Move to the end of the line. */
659 static void vty_end_of_line(struct vty
*vty
)
661 while (vty
->cp
< vty
->length
)
662 vty_forward_char(vty
);
665 static void vty_kill_line_from_beginning(struct vty
*);
666 static void vty_redraw_line(struct vty
*);
668 /* Print command line history. This function is called from
669 vty_next_line and vty_previous_line. */
670 static void vty_history_print(struct vty
*vty
)
674 vty_kill_line_from_beginning(vty
);
676 /* Get previous line from history buffer */
677 length
= strlen(vty
->hist
[vty
->hp
]);
678 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
679 vty
->cp
= vty
->length
= length
;
680 vty
->buf
[vty
->length
] = '\0';
682 /* Redraw current line */
683 vty_redraw_line(vty
);
686 /* Show next command line history. */
687 static void vty_next_line(struct vty
*vty
)
691 if (vty
->hp
== vty
->hindex
)
694 /* Try is there history exist or not. */
696 if (try_index
== (VTY_MAXHIST
- 1))
701 /* If there is not history return. */
702 if (vty
->hist
[try_index
] == NULL
)
707 vty_history_print(vty
);
710 /* Show previous command line history. */
711 static void vty_previous_line(struct vty
*vty
)
717 try_index
= VTY_MAXHIST
- 1;
721 if (vty
->hist
[try_index
] == NULL
)
726 vty_history_print(vty
);
729 /* This function redraw all of the command line character. */
730 static void vty_redraw_line(struct vty
*vty
)
732 vty_write(vty
, vty
->buf
, vty
->length
);
733 vty
->cp
= vty
->length
;
737 static void vty_forward_word(struct vty
*vty
)
739 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
740 vty_forward_char(vty
);
742 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
743 vty_forward_char(vty
);
746 /* Backward word without skipping training space. */
747 static void vty_backward_pure_word(struct vty
*vty
)
749 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
750 vty_backward_char(vty
);
754 static void vty_backward_word(struct vty
*vty
)
756 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
757 vty_backward_char(vty
);
759 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
760 vty_backward_char(vty
);
763 /* When '^D' is typed at the beginning of the line we move to the down
765 static void vty_down_level(struct vty
*vty
)
773 /* When '^Z' is received from vty, move down to the enable mode. */
774 static void vty_end_config(struct vty
*vty
)
793 case BGP_VRF_POLICY_NODE
:
794 case BGP_VNC_DEFAULTS_NODE
:
795 case BGP_VNC_NVE_GROUP_NODE
:
796 case BGP_VNC_L2_GROUP_NODE
:
811 case LDP_IPV4_IFACE_NODE
:
812 case LDP_IPV6_IFACE_NODE
:
814 case LDP_PSEUDOWIRE_NODE
:
816 case OPENFABRIC_NODE
:
818 case KEYCHAIN_KEY_NODE
:
820 case BGP_EVPN_VNI_NODE
:
823 vty_config_exit(vty
);
824 vty
->node
= ENABLE_NODE
;
827 /* Unknown node, we have to ignore it. */
831 vty
->xpath_index
= 0;
837 /* Delete a charcter at the current point. */
838 static void vty_delete_char(struct vty
*vty
)
843 if (vty
->length
== 0) {
848 if (vty
->cp
== vty
->length
)
849 return; /* completion need here? */
851 size
= vty
->length
- vty
->cp
;
854 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
855 vty
->buf
[vty
->length
] = '\0';
857 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
860 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
861 vty_write(vty
, &telnet_space_char
, 1);
863 for (i
= 0; i
< size
; i
++)
864 vty_write(vty
, &telnet_backward_char
, 1);
867 /* Delete a character before the point. */
868 static void vty_delete_backward_char(struct vty
*vty
)
873 vty_backward_char(vty
);
874 vty_delete_char(vty
);
877 /* Kill rest of line from current point. */
878 static void vty_kill_line(struct vty
*vty
)
883 size
= vty
->length
- vty
->cp
;
888 for (i
= 0; i
< size
; i
++)
889 vty_write(vty
, &telnet_space_char
, 1);
890 for (i
= 0; i
< size
; i
++)
891 vty_write(vty
, &telnet_backward_char
, 1);
893 memset(&vty
->buf
[vty
->cp
], 0, size
);
894 vty
->length
= vty
->cp
;
897 /* Kill line from the beginning. */
898 static void vty_kill_line_from_beginning(struct vty
*vty
)
900 vty_beginning_of_line(vty
);
904 /* Delete a word before the point. */
905 static void vty_forward_kill_word(struct vty
*vty
)
907 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
908 vty_delete_char(vty
);
909 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
910 vty_delete_char(vty
);
913 /* Delete a word before the point. */
914 static void vty_backward_kill_word(struct vty
*vty
)
916 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
917 vty_delete_backward_char(vty
);
918 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
919 vty_delete_backward_char(vty
);
922 /* Transpose chars before or at the point. */
923 static void vty_transpose_chars(struct vty
*vty
)
927 /* If length is short or point is near by the beginning of line then
929 if (vty
->length
< 2 || vty
->cp
< 1)
932 /* In case of point is located at the end of the line. */
933 if (vty
->cp
== vty
->length
) {
934 c1
= vty
->buf
[vty
->cp
- 1];
935 c2
= vty
->buf
[vty
->cp
- 2];
937 vty_backward_char(vty
);
938 vty_backward_char(vty
);
939 vty_self_insert_overwrite(vty
, c1
);
940 vty_self_insert_overwrite(vty
, c2
);
942 c1
= vty
->buf
[vty
->cp
];
943 c2
= vty
->buf
[vty
->cp
- 1];
945 vty_backward_char(vty
);
946 vty_self_insert_overwrite(vty
, c1
);
947 vty_self_insert_overwrite(vty
, c2
);
951 /* Do completion at vty interface. */
952 static void vty_complete_command(struct vty
*vty
)
956 char **matched
= NULL
;
959 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
962 vline
= cmd_make_strvec(vty
->buf
);
966 /* In case of 'help \t'. */
967 if (isspace((int)vty
->buf
[vty
->length
- 1]))
968 vector_set(vline
, NULL
);
970 matched
= cmd_complete_command(vline
, vty
, &ret
);
972 cmd_free_strvec(vline
);
976 case CMD_ERR_AMBIGUOUS
:
977 vty_out(vty
, "%% Ambiguous command.\n");
979 vty_redraw_line(vty
);
981 case CMD_ERR_NO_MATCH
:
982 /* vty_out (vty, "%% There is no matched command.\n"); */
984 vty_redraw_line(vty
);
986 case CMD_COMPLETE_FULL_MATCH
:
988 /* 2016-11-28 equinox -- need to debug, SEGV here */
989 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
991 vty_redraw_line(vty
);
995 vty_redraw_line(vty
);
996 vty_backward_pure_word(vty
);
997 vty_insert_word_overwrite(vty
, matched
[0]);
998 vty_self_insert(vty
, ' ');
999 XFREE(MTYPE_COMPLETION
, matched
[0]);
1001 case CMD_COMPLETE_MATCH
:
1003 vty_redraw_line(vty
);
1004 vty_backward_pure_word(vty
);
1005 vty_insert_word_overwrite(vty
, matched
[0]);
1006 XFREE(MTYPE_COMPLETION
, matched
[0]);
1008 case CMD_COMPLETE_LIST_MATCH
:
1009 for (i
= 0; matched
[i
] != NULL
; i
++) {
1010 if (i
!= 0 && ((i
% 6) == 0))
1012 vty_out(vty
, "%-10s ", matched
[i
]);
1013 XFREE(MTYPE_COMPLETION
, matched
[i
]);
1018 vty_redraw_line(vty
);
1020 case CMD_ERR_NOTHING_TODO
:
1022 vty_redraw_line(vty
);
1028 XFREE(MTYPE_TMP
, matched
);
1031 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
1032 unsigned int desc_width
, struct cmd_token
*token
)
1035 const char *cmd
, *p
;
1040 if (desc_width
<= 0) {
1041 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1045 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1047 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1048 for (pos
= desc_width
; pos
> 0; pos
--)
1049 if (*(p
+ pos
) == ' ')
1055 strncpy(buf
, p
, pos
);
1057 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1062 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1064 XFREE(MTYPE_TMP
, buf
);
1067 /* Describe matched command function. */
1068 static void vty_describe_command(struct vty
*vty
)
1073 unsigned int i
, width
, desc_width
;
1074 struct cmd_token
*token
, *token_cr
= NULL
;
1076 vline
= cmd_make_strvec(vty
->buf
);
1078 /* In case of '> ?'. */
1079 if (vline
== NULL
) {
1080 vline
= vector_init(1);
1081 vector_set(vline
, NULL
);
1082 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1083 vector_set(vline
, NULL
);
1085 describe
= cmd_describe_command(vline
, vty
, &ret
);
1089 /* Ambiguous error. */
1091 case CMD_ERR_AMBIGUOUS
:
1092 vty_out(vty
, "%% Ambiguous command.\n");
1095 case CMD_ERR_NO_MATCH
:
1096 vty_out(vty
, "%% There is no matched command.\n");
1101 /* Get width of command string. */
1103 for (i
= 0; i
< vector_active(describe
); i
++)
1104 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1107 if (token
->text
[0] == '\0')
1110 len
= strlen(token
->text
);
1116 /* Get width of description string. */
1117 desc_width
= vty
->width
- (width
+ 6);
1119 /* Print out description. */
1120 for (i
= 0; i
< vector_active(describe
); i
++)
1121 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1122 if (token
->text
[0] == '\0')
1125 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1131 vty_out(vty
, " %-s\n", token
->text
);
1132 else if (desc_width
>= strlen(token
->desc
))
1133 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1136 vty_describe_fold(vty
, width
, desc_width
,
1139 if (IS_VARYING_TOKEN(token
->type
)) {
1140 const char *ref
= vector_slot(
1141 vline
, vector_active(vline
) - 1);
1143 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1144 cmd_variable_complete(token
, ref
, varcomps
);
1146 if (vector_active(varcomps
) > 0) {
1147 char *ac
= cmd_variable_comp2str(
1148 varcomps
, vty
->width
);
1149 vty_out(vty
, "%s\n", ac
);
1150 XFREE(MTYPE_TMP
, ac
);
1153 vector_free(varcomps
);
1156 vty_out (vty
, " %-*s %s\n", width
1157 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1158 desc
->str
? desc
->str
: "");
1162 if ((token
= token_cr
)) {
1164 vty_out(vty
, " %-s\n", token
->text
);
1165 else if (desc_width
>= strlen(token
->desc
))
1166 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1169 vty_describe_fold(vty
, width
, desc_width
, token
);
1173 cmd_free_strvec(vline
);
1175 vector_free(describe
);
1178 vty_redraw_line(vty
);
1181 static void vty_clear_buf(struct vty
*vty
)
1183 memset(vty
->buf
, 0, vty
->max
);
1186 /* ^C stop current input and do not add command line to the history. */
1187 static void vty_stop_input(struct vty
*vty
)
1189 vty
->cp
= vty
->length
= 0;
1193 switch (vty
->node
) {
1196 /* Nothing to do. */
1199 case INTERFACE_NODE
:
1213 case LDP_IPV4_IFACE_NODE
:
1214 case LDP_IPV6_IFACE_NODE
:
1215 case LDP_L2VPN_NODE
:
1216 case LDP_PSEUDOWIRE_NODE
:
1218 case OPENFABRIC_NODE
:
1220 case KEYCHAIN_KEY_NODE
:
1224 vty_config_exit(vty
);
1225 vty
->node
= ENABLE_NODE
;
1228 /* Unknown node, we have to ignore it. */
1233 /* Set history pointer to the latest one. */
1234 vty
->hp
= vty
->hindex
;
1237 /* Add current command line to the history buffer. */
1238 static void vty_hist_add(struct vty
*vty
)
1242 if (vty
->length
== 0)
1245 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1247 /* Ignore the same string as previous one. */
1248 if (vty
->hist
[index
])
1249 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1250 vty
->hp
= vty
->hindex
;
1254 /* Insert history entry. */
1255 if (vty
->hist
[vty
->hindex
])
1256 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1257 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1259 /* History index rotation. */
1261 if (vty
->hindex
== VTY_MAXHIST
)
1264 vty
->hp
= vty
->hindex
;
1267 /* #define TELNET_OPTION_DEBUG */
1269 /* Get telnet window size. */
1270 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1272 #ifdef TELNET_OPTION_DEBUG
1275 for (i
= 0; i
< nbytes
; i
++) {
1278 vty_out(vty
, "IAC ");
1281 vty_out(vty
, "WILL ");
1284 vty_out(vty
, "WONT ");
1287 vty_out(vty
, "DO ");
1290 vty_out(vty
, "DONT ");
1293 vty_out(vty
, "SB ");
1296 vty_out(vty
, "SE ");
1299 vty_out(vty
, "TELOPT_ECHO \n");
1302 vty_out(vty
, "TELOPT_SGA \n");
1305 vty_out(vty
, "TELOPT_NAWS \n");
1308 vty_out(vty
, "%x ", buf
[i
]);
1314 #endif /* TELNET_OPTION_DEBUG */
1319 vty
->iac_sb_in_progress
= 1;
1323 if (!vty
->iac_sb_in_progress
)
1326 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1327 vty
->iac_sb_in_progress
= 0;
1330 switch (vty
->sb_buf
[0]) {
1332 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1335 "RFC 1073 violation detected: telnet NAWS option "
1336 "should send %d characters, but we received %lu",
1338 (unsigned long)vty
->sb_len
);
1339 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1342 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1343 (unsigned long)sizeof(vty
->sb_buf
),
1344 TELNET_NAWS_SB_LEN
);
1346 vty
->width
= ((vty
->sb_buf
[1] << 8)
1348 vty
->height
= ((vty
->sb_buf
[3] << 8)
1350 #ifdef TELNET_OPTION_DEBUG
1352 "TELNET NAWS window size negotiation completed: "
1353 "width %d, height %d\n",
1354 vty
->width
, vty
->height
);
1359 vty
->iac_sb_in_progress
= 0;
1369 /* Execute current command line. */
1370 static int vty_execute(struct vty
*vty
)
1376 switch (vty
->node
) {
1378 case AUTH_ENABLE_NODE
:
1379 vty_auth(vty
, vty
->buf
);
1382 ret
= vty_command(vty
, vty
->buf
);
1383 if (vty
->type
== VTY_TERM
)
1388 /* Clear command line buffer. */
1389 vty
->cp
= vty
->length
= 0;
1392 if (vty
->status
!= VTY_CLOSE
)
1398 #define CONTROL(X) ((X) - '@')
1399 #define VTY_NORMAL 0
1400 #define VTY_PRE_ESCAPE 1
1401 #define VTY_ESCAPE 2
1403 /* Escape character command map. */
1404 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1408 vty_previous_line(vty
);
1414 vty_forward_char(vty
);
1417 vty_backward_char(vty
);
1423 /* Go back to normal mode. */
1424 vty
->escape
= VTY_NORMAL
;
1427 /* Quit print out to the buffer. */
1428 static void vty_buffer_reset(struct vty
*vty
)
1430 buffer_reset(vty
->obuf
);
1431 buffer_reset(vty
->lbuf
);
1433 vty_redraw_line(vty
);
1436 /* Read data via vty socket. */
1437 static int vty_read(struct thread
*thread
)
1441 unsigned char buf
[VTY_READ_BUFSIZ
];
1443 int vty_sock
= THREAD_FD(thread
);
1444 struct vty
*vty
= THREAD_ARG(thread
);
1447 /* Read raw data from socket */
1448 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1450 if (ERRNO_IO_RETRY(errno
)) {
1451 vty_event(VTY_READ
, vty_sock
, vty
);
1454 vty
->monitor
= 0; /* disable monitoring to avoid
1455 infinite recursion */
1458 "%s: read error on vty client fd %d, closing: %s",
1459 __func__
, vty
->fd
, safe_strerror(errno
));
1460 buffer_reset(vty
->obuf
);
1461 buffer_reset(vty
->lbuf
);
1463 vty
->status
= VTY_CLOSE
;
1466 for (i
= 0; i
< nbytes
; i
++) {
1467 if (buf
[i
] == IAC
) {
1476 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1477 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1478 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1484 /* In case of telnet command */
1486 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1493 if (vty
->status
== VTY_MORE
) {
1498 vty_buffer_reset(vty
);
1500 #if 0 /* More line does not work for "show ip bgp". */
1503 vty
->status
= VTY_MORELINE
;
1512 /* Escape character. */
1513 if (vty
->escape
== VTY_ESCAPE
) {
1514 vty_escape_map(buf
[i
], vty
);
1518 /* Pre-escape status. */
1519 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1522 vty
->escape
= VTY_ESCAPE
;
1525 vty_backward_word(vty
);
1526 vty
->escape
= VTY_NORMAL
;
1529 vty_forward_word(vty
);
1530 vty
->escape
= VTY_NORMAL
;
1533 vty_forward_kill_word(vty
);
1534 vty
->escape
= VTY_NORMAL
;
1538 vty_backward_kill_word(vty
);
1539 vty
->escape
= VTY_NORMAL
;
1542 vty
->escape
= VTY_NORMAL
;
1550 vty_beginning_of_line(vty
);
1553 vty_backward_char(vty
);
1556 vty_stop_input(vty
);
1559 vty_delete_char(vty
);
1562 vty_end_of_line(vty
);
1565 vty_forward_char(vty
);
1569 vty_delete_backward_char(vty
);
1578 vty_previous_line(vty
);
1581 vty_transpose_chars(vty
);
1584 vty_kill_line_from_beginning(vty
);
1587 vty_backward_kill_word(vty
);
1590 vty_end_config(vty
);
1598 vty_complete_command(vty
);
1601 if (vty
->node
== AUTH_NODE
1602 || vty
->node
== AUTH_ENABLE_NODE
)
1603 vty_self_insert(vty
, buf
[i
]);
1605 vty_describe_command(vty
);
1608 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1609 vty
->escape
= VTY_ESCAPE
;
1612 vty
->escape
= VTY_PRE_ESCAPE
;
1615 if (buf
[i
] > 31 && buf
[i
] < 127)
1616 vty_self_insert(vty
, buf
[i
]);
1622 if (vty
->status
== VTY_CLOSE
)
1625 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1626 vty_event(VTY_READ
, vty_sock
, vty
);
1631 /* Flush buffer to the vty. */
1632 static int vty_flush(struct thread
*thread
)
1635 buffer_status_t flushrc
;
1636 int vty_sock
= THREAD_FD(thread
);
1637 struct vty
*vty
= THREAD_ARG(thread
);
1639 vty
->t_write
= NULL
;
1641 /* Tempolary disable read thread. */
1642 if ((vty
->lines
== 0) && vty
->t_read
) {
1643 thread_cancel(vty
->t_read
);
1647 /* Function execution continue. */
1648 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1650 /* N.B. if width is 0, that means we don't know the window size. */
1651 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1652 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1653 else if (vty
->status
== VTY_MORELINE
)
1654 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1657 flushrc
= buffer_flush_window(
1658 vty
->obuf
, vty_sock
, vty
->width
,
1659 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1663 0; /* disable monitoring to avoid infinite recursion */
1664 zlog_info("buffer_flush failed on vty client fd %d, closing",
1666 buffer_reset(vty
->lbuf
);
1667 buffer_reset(vty
->obuf
);
1671 if (vty
->status
== VTY_CLOSE
)
1674 vty
->status
= VTY_NORMAL
;
1675 if (vty
->lines
== 0)
1676 vty_event(VTY_READ
, vty_sock
, vty
);
1679 case BUFFER_PENDING
:
1680 /* There is more data waiting to be written. */
1681 vty
->status
= VTY_MORE
;
1682 if (vty
->lines
== 0)
1683 vty_event(VTY_WRITE
, vty_sock
, vty
);
1690 /* Allocate new vty struct. */
1691 struct vty
*vty_new()
1693 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1695 new->fd
= new->wfd
= -1;
1697 new->lbuf
= buffer_new(0);
1698 new->obuf
= buffer_new(0); /* Use default buffer size. */
1699 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1700 new->max
= VTY_BUFSIZ
;
1706 /* allocate and initialise vty */
1707 static struct vty
*vty_new_init(int vty_sock
)
1713 vty
->wfd
= vty_sock
;
1714 vty
->type
= VTY_TERM
;
1715 vty
->node
= AUTH_NODE
;
1720 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1723 vty
->xpath_index
= 0;
1724 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1725 vty
->private_config
= false;
1726 vty
->candidate_config
= vty_shared_candidate_config
;
1727 vector_set_index(vtyvec
, vty_sock
, vty
);
1728 vty
->status
= VTY_NORMAL
;
1731 vty
->iac_sb_in_progress
= 0;
1737 /* Create new vty structure. */
1738 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1740 char buf
[SU_ADDRSTRLEN
];
1743 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1745 /* Allocate new vty structure and set up default values. */
1746 vty
= vty_new_init(vty_sock
);
1748 /* configurable parameters not part of basic init */
1749 vty
->v_timeout
= vty_timeout_val
;
1750 strcpy(vty
->address
, buf
);
1751 if (no_password_check
) {
1753 vty
->node
= ENABLE_NODE
;
1755 vty
->node
= VIEW_NODE
;
1757 if (host
.lines
>= 0)
1758 vty
->lines
= host
.lines
;
1760 if (!no_password_check
) {
1761 /* Vty is not available if password isn't set. */
1762 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1763 vty_out(vty
, "Vty password is not set.\n");
1764 vty
->status
= VTY_CLOSE
;
1770 /* Say hello to the world. */
1772 if (!no_password_check
)
1773 vty_out(vty
, "\nUser Access Verification\n\n");
1775 /* Setting up terminal. */
1777 vty_will_suppress_go_ahead(vty
);
1779 vty_dont_linemode(vty
);
1780 vty_do_window_size(vty
);
1781 /* vty_dont_lflow_ahead (vty); */
1785 /* Add read/write thread. */
1786 vty_event(VTY_WRITE
, vty_sock
, vty
);
1787 vty_event(VTY_READ
, vty_sock
, vty
);
1792 /* create vty for stdio */
1793 static struct termios stdio_orig_termios
;
1794 static struct vty
*stdio_vty
= NULL
;
1795 static bool stdio_termios
= false;
1796 static void (*stdio_vty_atclose
)(int isexit
);
1798 static void vty_stdio_reset(int isexit
)
1802 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1803 stdio_termios
= false;
1807 if (stdio_vty_atclose
)
1808 stdio_vty_atclose(isexit
);
1809 stdio_vty_atclose
= NULL
;
1813 static void vty_stdio_atexit(void)
1818 void vty_stdio_suspend(void)
1823 if (stdio_vty
->t_write
)
1824 thread_cancel(stdio_vty
->t_write
);
1825 if (stdio_vty
->t_read
)
1826 thread_cancel(stdio_vty
->t_read
);
1827 if (stdio_vty
->t_timeout
)
1828 thread_cancel(stdio_vty
->t_timeout
);
1831 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1832 stdio_termios
= false;
1835 void vty_stdio_resume(void)
1840 if (!tcgetattr(0, &stdio_orig_termios
)) {
1841 struct termios termios
;
1843 termios
= stdio_orig_termios
;
1844 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1845 | IGNCR
| ICRNL
| IXON
);
1846 termios
.c_oflag
&= ~OPOST
;
1847 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1848 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1849 termios
.c_cflag
|= CS8
;
1850 tcsetattr(0, TCSANOW
, &termios
);
1851 stdio_termios
= true;
1854 vty_prompt(stdio_vty
);
1856 /* Add read/write thread. */
1857 vty_event(VTY_WRITE
, 1, stdio_vty
);
1858 vty_event(VTY_READ
, 0, stdio_vty
);
1861 void vty_stdio_close(void)
1865 vty_close(stdio_vty
);
1868 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1872 /* refuse creating two vtys on stdio */
1876 vty
= stdio_vty
= vty_new_init(0);
1877 stdio_vty_atclose
= atclose
;
1880 /* always have stdio vty in a known _unchangeable_ state, don't want
1882 * to have any effect here to make sure scripting this works as intended
1884 vty
->node
= ENABLE_NODE
;
1886 strcpy(vty
->address
, "console");
1892 /* Accept connection from the network. */
1893 static int vty_accept(struct thread
*thread
)
1901 struct access_list
*acl
= NULL
;
1902 char buf
[SU_ADDRSTRLEN
];
1904 accept_sock
= THREAD_FD(thread
);
1906 /* We continue hearing vty socket. */
1907 vty_event(VTY_SERV
, accept_sock
, NULL
);
1909 memset(&su
, 0, sizeof(union sockunion
));
1911 /* We can handle IPv4 or IPv6 socket. */
1912 vty_sock
= sockunion_accept(accept_sock
, &su
);
1914 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1915 safe_strerror(errno
));
1918 set_nonblocking(vty_sock
);
1919 set_cloexec(vty_sock
);
1921 sockunion2hostprefix(&su
, &p
);
1923 /* VTY's accesslist apply. */
1924 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1925 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1926 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1927 zlog_info("Vty connection refused from %s",
1928 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1931 /* continue accepting connections */
1932 vty_event(VTY_SERV
, accept_sock
, NULL
);
1938 /* VTY's ipv6 accesslist apply. */
1939 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1940 if ((acl
= access_list_lookup(AFI_IP6
,
1941 vty_ipv6_accesslist_name
))
1942 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1943 zlog_info("Vty connection refused from %s",
1944 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1947 /* continue accepting connections */
1948 vty_event(VTY_SERV
, accept_sock
, NULL
);
1955 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1958 zlog_info("can't set sockopt to vty_sock : %s",
1959 safe_strerror(errno
));
1961 zlog_info("Vty connection from %s",
1962 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1964 vty_create(vty_sock
, &su
);
1969 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1972 struct addrinfo req
;
1973 struct addrinfo
*ainfo
;
1974 struct addrinfo
*ainfo_save
;
1976 char port_str
[BUFSIZ
];
1978 memset(&req
, 0, sizeof(struct addrinfo
));
1979 req
.ai_flags
= AI_PASSIVE
;
1980 req
.ai_family
= AF_UNSPEC
;
1981 req
.ai_socktype
= SOCK_STREAM
;
1982 sprintf(port_str
, "%d", port
);
1983 port_str
[sizeof(port_str
) - 1] = '\0';
1985 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1988 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1996 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1999 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
2000 ainfo
->ai_protocol
);
2004 sockopt_v6only(ainfo
->ai_family
, sock
);
2005 sockopt_reuseaddr(sock
);
2006 sockopt_reuseport(sock
);
2009 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
2011 close(sock
); /* Avoid sd leak. */
2015 ret
= listen(sock
, 3);
2017 close(sock
); /* Avoid sd leak. */
2021 vty_event(VTY_SERV
, sock
, NULL
);
2022 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
2024 freeaddrinfo(ainfo_save
);
2028 /* For sockaddr_un. */
2031 /* VTY shell UNIX domain socket. */
2032 static void vty_serv_un(const char *path
)
2036 struct sockaddr_un serv
;
2038 struct zprivs_ids_t ids
;
2040 /* First of all, unlink existing socket */
2044 old_mask
= umask(0007);
2046 /* Make UNIX domain socket. */
2047 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2049 flog_err_sys(EC_LIB_SOCKET
,
2050 "Cannot create unix stream socket: %s",
2051 safe_strerror(errno
));
2055 /* Make server socket. */
2056 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2057 serv
.sun_family
= AF_UNIX
;
2058 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2059 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2060 len
= serv
.sun_len
= SUN_LEN(&serv
);
2062 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2063 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2067 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2069 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
2070 safe_strerror(errno
));
2071 close(sock
); /* Avoid sd leak. */
2075 ret
= listen(sock
, 5);
2077 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
2078 safe_strerror(errno
));
2079 close(sock
); /* Avoid sd leak. */
2085 zprivs_get_ids(&ids
);
2087 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2088 earlier for the case when we don't need to chown the file
2089 type casting it here to make a compare */
2090 if ((int)ids
.gid_vty
> 0) {
2091 /* set group of socket */
2092 if (chown(path
, -1, ids
.gid_vty
)) {
2093 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2094 "vty_serv_un: could chown socket, %s",
2095 safe_strerror(errno
));
2099 vty_event(VTYSH_SERV
, sock
, NULL
);
2102 /* #define VTYSH_DEBUG 1 */
2104 static int vtysh_accept(struct thread
*thread
)
2109 struct sockaddr_un client
;
2112 accept_sock
= THREAD_FD(thread
);
2114 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2116 memset(&client
, 0, sizeof(struct sockaddr_un
));
2117 client_len
= sizeof(struct sockaddr_un
);
2119 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2120 (socklen_t
*)&client_len
);
2123 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2124 safe_strerror(errno
));
2128 if (set_nonblocking(sock
) < 0) {
2131 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2132 sock
, safe_strerror(errno
));
2139 printf("VTY shell accept\n");
2140 #endif /* VTYSH_DEBUG */
2145 vty
->type
= VTY_SHELL_SERV
;
2146 vty
->node
= VIEW_NODE
;
2148 vty_event(VTYSH_READ
, sock
, vty
);
2153 static int vtysh_flush(struct vty
*vty
)
2155 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2156 case BUFFER_PENDING
:
2157 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2161 0; /* disable monitoring to avoid infinite recursion */
2162 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2164 buffer_reset(vty
->lbuf
);
2165 buffer_reset(vty
->obuf
);
2175 static int vtysh_read(struct thread
*thread
)
2181 unsigned char buf
[VTY_READ_BUFSIZ
];
2183 uint8_t header
[4] = {0, 0, 0, 0};
2185 sock
= THREAD_FD(thread
);
2186 vty
= THREAD_ARG(thread
);
2189 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2191 if (ERRNO_IO_RETRY(errno
)) {
2192 vty_event(VTYSH_READ
, sock
, vty
);
2195 vty
->monitor
= 0; /* disable monitoring to avoid
2196 infinite recursion */
2199 "%s: read failed on vtysh client fd %d, closing: %s",
2200 __func__
, sock
, safe_strerror(errno
));
2202 buffer_reset(vty
->lbuf
);
2203 buffer_reset(vty
->obuf
);
2206 printf("close vtysh\n");
2207 #endif /* VTYSH_DEBUG */
2212 printf("line: %.*s\n", nbytes
, buf
);
2213 #endif /* VTYSH_DEBUG */
2215 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2216 /* Clear command line buffer. */
2217 vty
->cp
= vty
->length
= 0;
2219 vty_out(vty
, "%% Command is too long.\n");
2221 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2222 vty
->buf
[vty
->length
++] = *p
;
2224 /* Pass this line to parser. */
2225 ret
= vty_execute(vty
);
2226 /* Note that vty_execute clears the command buffer and resets
2227 vty->length to 0. */
2229 /* Return result. */
2231 printf("result: %d\n", ret
);
2232 printf("vtysh node: %d\n", vty
->node
);
2233 #endif /* VTYSH_DEBUG */
2235 /* hack for asynchronous "write integrated"
2236 * - other commands in "buf" will be ditched
2237 * - input during pending config-write is
2239 if (ret
== CMD_SUSPEND
)
2242 /* warning: watchfrr hardcodes this result write
2245 buffer_put(vty
->obuf
, header
, 4);
2247 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2248 /* Try to flush results; exit if a write
2255 if (vty
->status
== VTY_CLOSE
)
2258 vty_event(VTYSH_READ
, sock
, vty
);
2263 static int vtysh_write(struct thread
*thread
)
2265 struct vty
*vty
= THREAD_ARG(thread
);
2267 vty
->t_write
= NULL
;
2274 /* Determine address family to bind. */
2275 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2277 /* If port is set to 0, do not listen on TCP/IP at all! */
2279 vty_serv_sock_addrinfo(addr
, port
);
2286 static void vty_error_delete(void *arg
)
2288 struct vty_error
*ve
= arg
;
2290 XFREE(MTYPE_TMP
, ve
);
2293 /* Close vty interface. Warning: call this only from functions that
2294 will be careful not to access the vty afterwards (since it has
2295 now been freed). This is safest from top-level functions (called
2296 directly by the thread dispatcher). */
2297 void vty_close(struct vty
*vty
)
2300 bool was_stdio
= false;
2302 /* Cancel threads.*/
2304 thread_cancel(vty
->t_read
);
2306 thread_cancel(vty
->t_write
);
2308 thread_cancel(vty
->t_timeout
);
2311 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2313 /* Free input buffer. */
2314 buffer_free(vty
->obuf
);
2315 buffer_free(vty
->lbuf
);
2317 /* Free command history. */
2318 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2320 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2324 vector_unset(vtyvec
, vty
->fd
);
2326 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2330 * note check is for fd > STDERR_FILENO, not fd != -1.
2331 * We never close stdin/stdout/stderr here, because we may be
2332 * running in foreground mode with logging to stdout. Also,
2333 * additionally, we'd need to replace these fds with /dev/null. */
2334 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2336 if (vty
->fd
> STDERR_FILENO
)
2338 if (vty
->fd
== STDIN_FILENO
)
2342 XFREE(MTYPE_VTY
, vty
->buf
);
2345 vty
->error
->del
= vty_error_delete
;
2346 list_delete(&vty
->error
);
2349 /* Check configure. */
2350 vty_config_exit(vty
);
2353 XFREE(MTYPE_VTY
, vty
);
2359 /* When time out occur output message then close connection. */
2360 static int vty_timeout(struct thread
*thread
)
2364 vty
= THREAD_ARG(thread
);
2365 vty
->t_timeout
= NULL
;
2369 buffer_reset(vty
->lbuf
);
2370 buffer_reset(vty
->obuf
);
2371 vty_out(vty
, "\nVty connection is timed out.\n");
2373 /* Close connection. */
2374 vty
->status
= VTY_CLOSE
;
2380 /* Read up configuration file from file_name. */
2381 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2385 struct vty_error
*ve
;
2386 struct listnode
*node
;
2387 unsigned int line_num
= 0;
2390 /* vty_close won't close stderr; if some config command prints
2391 * something it'll end up there. (not ideal; it'd be beter if output
2392 * from a file-load went to logging instead. Also note that if this
2393 * function is called after daemonizing, stderr will be /dev/null.)
2395 * vty->fd will be -1 from vty_new()
2397 vty
->wfd
= STDERR_FILENO
;
2398 vty
->type
= VTY_FILE
;
2399 vty
->node
= CONFIG_NODE
;
2401 vty
->candidate_config
= config
;
2403 vty
->private_config
= true;
2404 vty
->candidate_config
= nb_config_new(NULL
);
2407 /* Execute configuration file */
2408 ret
= config_from_file(vty
, confp
, &line_num
);
2410 /* Flush any previous errors before printing messages below */
2411 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2413 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2414 const char *message
= NULL
;
2418 case CMD_ERR_AMBIGUOUS
:
2419 message
= "Ambiguous command";
2421 case CMD_ERR_NO_MATCH
:
2422 message
= "No such command";
2425 message
= "Command returned Warning";
2427 case CMD_WARNING_CONFIG_FAILED
:
2428 message
= "Command returned Warning Config Failed";
2430 case CMD_ERR_INCOMPLETE
:
2431 message
= "Command returned Incomplete";
2433 case CMD_ERR_EXEED_ARGC_MAX
:
2435 "Command exceeded maximum number of Arguments";
2438 message
= "Command returned unhandled error message";
2442 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2443 nl
= strchr(ve
->error_buf
, '\n');
2446 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2447 message
, ve
->line_num
, ve
->error_buf
);
2452 * Automatically commit the candidate configuration after
2453 * reading the configuration file.
2455 if (config
== NULL
&& vty
->candidate_config
2456 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2457 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2458 true, "Read configuration file",
2460 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2461 zlog_err("%s: failed to read configuration file.",
2468 static FILE *vty_use_backup_config(const char *fullpath
)
2470 char *fullpath_sav
, *fullpath_tmp
;
2476 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2477 strcpy(fullpath_sav
, fullpath
);
2478 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2480 sav
= open(fullpath_sav
, O_RDONLY
);
2486 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2487 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2489 /* Open file to configuration write. */
2490 tmp
= mkstemp(fullpath_tmp
);
2494 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2497 while ((c
= read(sav
, buffer
, 512)) > 0) {
2498 if (write(tmp
, buffer
, c
) <= 0)
2504 if (rename(fullpath_tmp
, fullpath
) == 0)
2505 ret
= fopen(fullpath
, "r");
2507 unlink(fullpath_tmp
);
2512 unlink(fullpath_tmp
);
2522 /* Read up configuration file from file_name. */
2523 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2524 char *config_default_dir
)
2526 char cwd
[MAXPATHLEN
];
2528 const char *fullpath
;
2530 bool read_success
= false;
2532 /* If -f flag specified. */
2533 if (config_file
!= NULL
) {
2534 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2535 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2538 "%s: failure to determine Current Working Directory %d!",
2540 goto tmp_free_and_out
;
2542 tmp
= XMALLOC(MTYPE_TMP
,
2543 strlen(cwd
) + strlen(config_file
) + 2);
2544 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2547 fullpath
= config_file
;
2549 confp
= fopen(fullpath
, "r");
2551 if (confp
== NULL
) {
2553 EC_LIB_BACKUP_CONFIG
,
2554 "%s: failed to open configuration file %s: %s, checking backup",
2555 __func__
, fullpath
, safe_strerror(errno
));
2557 confp
= vty_use_backup_config(fullpath
);
2560 EC_LIB_BACKUP_CONFIG
,
2561 "WARNING: using backup configuration file!");
2565 "%s: can't open configuration file [%s]",
2566 __func__
, config_file
);
2567 goto tmp_free_and_out
;
2572 host_config_set(config_default_dir
);
2576 struct stat conf_stat
;
2578 /* !!!!PLEASE LEAVE!!!!
2579 * This is NEEDED for use with vtysh -b, or else you can get
2580 * a real configuration food fight with a lot garbage in the
2581 * merged configuration file it creates coming from the per
2582 * daemon configuration files. This also allows the daemons
2583 * to start if there default configuration file is not
2584 * present or ignore them, as needed when using vtysh -b to
2585 * configure the daemons at boot - MAG
2588 /* Stat for vtysh Zebra.conf, if found startup and wait for
2589 * boot configuration
2592 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2593 ret
= stat(integrate_default
, &conf_stat
);
2595 read_success
= true;
2596 goto tmp_free_and_out
;
2600 confp
= fopen(config_default_dir
, "r");
2601 if (confp
== NULL
) {
2604 "%s: failed to open configuration file %s: %s, checking backup",
2605 __func__
, config_default_dir
,
2606 safe_strerror(errno
));
2608 confp
= vty_use_backup_config(config_default_dir
);
2611 EC_LIB_BACKUP_CONFIG
,
2612 "WARNING: using backup configuration file!");
2613 fullpath
= config_default_dir
;
2615 flog_err(EC_LIB_VTY
,
2616 "can't open configuration file [%s]",
2617 config_default_dir
);
2618 goto tmp_free_and_out
;
2621 fullpath
= config_default_dir
;
2624 vty_read_file(config
, confp
);
2625 read_success
= true;
2629 host_config_set(fullpath
);
2633 XFREE(MTYPE_TMP
, tmp
);
2635 return read_success
;
2638 /* Small utility function which output log to the VTY. */
2639 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2640 struct timestamp_control
*ctl
, va_list va
)
2648 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2649 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2653 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2659 /* Async-signal-safe version of vty_log for fixed strings. */
2660 void vty_log_fixed(char *buf
, size_t len
)
2663 struct iovec iov
[2];
2664 char crlf
[4] = "\r\n";
2666 /* vty may not have been initialised */
2670 iov
[0].iov_base
= buf
;
2671 iov
[0].iov_len
= len
;
2672 iov
[1].iov_base
= crlf
;
2675 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2677 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2678 /* N.B. We don't care about the return code, since
2680 most likely just about to die anyway. */
2681 if (writev(vty
->wfd
, iov
, 2) == -1) {
2682 fprintf(stderr
, "Failure to writev: %d\n",
2689 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2691 if (exclusive
&& !vty_config_exclusive_lock(vty
)) {
2692 vty_out(vty
, "VTY configuration is locked by other VTY\n");
2696 vty
->node
= CONFIG_NODE
;
2698 vty
->private_config
= private_config
;
2700 if (private_config
) {
2701 vty
->candidate_config
= nb_config_dup(running_config
);
2702 vty
->candidate_config_base
= nb_config_dup(running_config
);
2704 "Warning: uncommitted changes will be discarded on exit.\n\n");
2706 vty
->candidate_config
= vty_shared_candidate_config
;
2707 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2708 vty
->candidate_config_base
=
2709 nb_config_dup(running_config
);
2715 void vty_config_exit(struct vty
*vty
)
2717 /* Check if there's a pending confirmed commit. */
2718 if (vty
->t_confirmed_commit_timeout
) {
2720 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2721 nb_cli_confirmed_commit_rollback(vty
);
2722 nb_cli_confirmed_commit_clean(vty
);
2725 vty_config_exclusive_unlock(vty
);
2727 if (vty
->candidate_config
) {
2728 if (vty
->private_config
)
2729 nb_config_free(vty
->candidate_config
);
2730 vty
->candidate_config
= NULL
;
2732 if (vty
->candidate_config_base
) {
2733 nb_config_free(vty
->candidate_config_base
);
2734 vty
->candidate_config_base
= NULL
;
2738 int vty_config_exclusive_lock(struct vty
*vty
)
2740 if (vty_exclusive_lock
== NULL
) {
2741 vty_exclusive_lock
= vty
;
2747 void vty_config_exclusive_unlock(struct vty
*vty
)
2749 if (vty_exclusive_lock
== vty
)
2750 vty_exclusive_lock
= NULL
;
2753 /* Master of the threads. */
2754 static struct thread_master
*vty_master
;
2756 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2758 struct thread
*vty_serv_thread
= NULL
;
2762 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2764 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2768 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2770 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2774 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2778 vty
->t_write
= NULL
;
2779 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2785 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2787 /* Time out treatment. */
2788 if (vty
->v_timeout
) {
2790 thread_cancel(vty
->t_timeout
);
2791 vty
->t_timeout
= NULL
;
2792 thread_add_timer(vty_master
, vty_timeout
, vty
,
2793 vty
->v_timeout
, &vty
->t_timeout
);
2797 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2800 case VTY_TIMEOUT_RESET
:
2801 if (vty
->t_timeout
) {
2802 thread_cancel(vty
->t_timeout
);
2803 vty
->t_timeout
= NULL
;
2805 if (vty
->v_timeout
) {
2806 vty
->t_timeout
= NULL
;
2807 thread_add_timer(vty_master
, vty_timeout
, vty
,
2808 vty
->v_timeout
, &vty
->t_timeout
);
2814 DEFUN_NOSH (config_who
,
2817 "Display who is on vty\n")
2822 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2823 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2824 vty_out(vty
, "%svty[%d] connected from %s.\n",
2825 v
->config
? "*" : " ", i
, v
->address
);
2829 /* Move to vty configuration mode. */
2830 DEFUN_NOSH (line_vty
,
2833 "Configure a terminal line\n"
2834 "Virtual terminal\n")
2836 vty
->node
= VTY_NODE
;
2840 /* Set time out value. */
2841 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2842 const char *sec_str
)
2844 unsigned long timeout
= 0;
2846 /* min_str and sec_str are already checked by parser. So it must be
2847 all digit string. */
2849 timeout
= strtol(min_str
, NULL
, 10);
2853 timeout
+= strtol(sec_str
, NULL
, 10);
2855 vty_timeout_val
= timeout
;
2856 vty
->v_timeout
= timeout
;
2857 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2863 DEFUN (exec_timeout_min
,
2864 exec_timeout_min_cmd
,
2865 "exec-timeout (0-35791)",
2866 "Set timeout value\n"
2867 "Timeout value in minutes\n")
2870 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2873 DEFUN (exec_timeout_sec
,
2874 exec_timeout_sec_cmd
,
2875 "exec-timeout (0-35791) (0-2147483)",
2876 "Set the EXEC timeout\n"
2877 "Timeout in minutes\n"
2878 "Timeout in seconds\n")
2881 int idx_number_2
= 2;
2882 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2883 argv
[idx_number_2
]->arg
);
2886 DEFUN (no_exec_timeout
,
2887 no_exec_timeout_cmd
,
2890 "Set the EXEC timeout\n")
2892 return exec_timeout(vty
, NULL
, NULL
);
2895 /* Set vty access class. */
2896 DEFUN (vty_access_class
,
2897 vty_access_class_cmd
,
2898 "access-class WORD",
2899 "Filter connections based on an IP access list\n"
2903 if (vty_accesslist_name
)
2904 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2906 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2911 /* Clear vty access class. */
2912 DEFUN (no_vty_access_class
,
2913 no_vty_access_class_cmd
,
2914 "no access-class [WORD]",
2916 "Filter connections based on an IP access list\n"
2920 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2921 if (!vty_accesslist_name
2922 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2923 vty_out(vty
, "Access-class is not currently applied to vty\n");
2924 return CMD_WARNING_CONFIG_FAILED
;
2927 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2929 vty_accesslist_name
= NULL
;
2934 /* Set vty access class. */
2935 DEFUN (vty_ipv6_access_class
,
2936 vty_ipv6_access_class_cmd
,
2937 "ipv6 access-class WORD",
2939 "Filter connections based on an IP access list\n"
2940 "IPv6 access list\n")
2943 if (vty_ipv6_accesslist_name
)
2944 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2946 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2951 /* Clear vty access class. */
2952 DEFUN (no_vty_ipv6_access_class
,
2953 no_vty_ipv6_access_class_cmd
,
2954 "no ipv6 access-class [WORD]",
2957 "Filter connections based on an IP access list\n"
2958 "IPv6 access list\n")
2961 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2963 if (!vty_ipv6_accesslist_name
2964 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2966 "IPv6 access-class is not currently applied to vty\n");
2967 return CMD_WARNING_CONFIG_FAILED
;
2970 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2972 vty_ipv6_accesslist_name
= NULL
;
2981 "Enable password checking\n")
2983 no_password_check
= 0;
2987 DEFUN (no_vty_login
,
2991 "Enable password checking\n")
2993 no_password_check
= 1;
2997 DEFUN (service_advanced_vty
,
2998 service_advanced_vty_cmd
,
2999 "service advanced-vty",
3000 "Set up miscellaneous service\n"
3001 "Enable advanced mode vty interface\n")
3007 DEFUN (no_service_advanced_vty
,
3008 no_service_advanced_vty_cmd
,
3009 "no service advanced-vty",
3011 "Set up miscellaneous service\n"
3012 "Enable advanced mode vty interface\n")
3018 DEFUN_NOSH (terminal_monitor
,
3019 terminal_monitor_cmd
,
3021 "Set terminal line parameters\n"
3022 "Copy debug output to the current terminal line\n")
3028 DEFUN_NOSH (terminal_no_monitor
,
3029 terminal_no_monitor_cmd
,
3030 "terminal no monitor",
3031 "Set terminal line parameters\n"
3033 "Copy debug output to the current terminal line\n")
3039 DEFUN_NOSH (no_terminal_monitor
,
3040 no_terminal_monitor_cmd
,
3041 "no terminal monitor",
3043 "Set terminal line parameters\n"
3044 "Copy debug output to the current terminal line\n")
3046 return terminal_no_monitor(self
, vty
, argc
, argv
);
3050 DEFUN_NOSH (show_history
,
3054 "Display the session command history\n")
3058 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
3059 if (index
== VTY_MAXHIST
) {
3064 if (vty
->hist
[index
] != NULL
)
3065 vty_out(vty
, " %s\n", vty
->hist
[index
]);
3074 DEFUN (log_commands
,
3078 "Log all commands (can't be unset without restart)\n")
3080 do_log_commands
= 1;
3084 /* Display current configuration. */
3085 static int vty_config_write(struct vty
*vty
)
3087 vty_out(vty
, "line vty\n");
3089 if (vty_accesslist_name
)
3090 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3092 if (vty_ipv6_accesslist_name
)
3093 vty_out(vty
, " ipv6 access-class %s\n",
3094 vty_ipv6_accesslist_name
);
3097 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3098 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3099 vty_timeout_val
% 60);
3102 if (no_password_check
)
3103 vty_out(vty
, " no login\n");
3105 if (do_log_commands
)
3106 vty_out(vty
, "log commands\n");
3108 vty_out(vty
, "!\n");
3113 struct cmd_node vty_node
= {
3114 VTY_NODE
, "%s(config-line)# ", 1,
3117 /* Reset all VTY status. */
3122 struct thread
*vty_serv_thread
;
3124 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3125 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3126 buffer_reset(vty
->lbuf
);
3127 buffer_reset(vty
->obuf
);
3128 vty
->status
= VTY_CLOSE
;
3132 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3133 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3135 thread_cancel(vty_serv_thread
);
3136 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3140 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3142 if (vty_accesslist_name
) {
3143 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3144 vty_accesslist_name
= NULL
;
3147 if (vty_ipv6_accesslist_name
) {
3148 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3149 vty_ipv6_accesslist_name
= NULL
;
3153 static void vty_save_cwd(void)
3155 char cwd
[MAXPATHLEN
];
3158 c
= getcwd(cwd
, MAXPATHLEN
);
3162 * At this point if these go wrong, more than likely
3163 * the whole world is coming down around us
3164 * Hence not worrying about it too much.
3166 if (!chdir(SYSCONFDIR
)) {
3167 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3168 "Failure to chdir to %s, errno: %d",
3172 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3173 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3174 "Failure to getcwd, errno: %d", errno
);
3179 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3180 strcpy(vty_cwd
, cwd
);
3188 int vty_shell(struct vty
*vty
)
3190 return vty
->type
== VTY_SHELL
? 1 : 0;
3193 int vty_shell_serv(struct vty
*vty
)
3195 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3198 void vty_init_vtysh()
3200 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3203 /* Install vty's own commands like `who' command. */
3204 void vty_init(struct thread_master
*master_thread
)
3206 /* For further configuration read, preserve current directory. */
3209 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3211 vty_master
= master_thread
;
3213 atexit(vty_stdio_atexit
);
3215 /* Initilize server thread vector. */
3216 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3218 /* Install bgp top node. */
3219 install_node(&vty_node
, vty_config_write
);
3221 install_element(VIEW_NODE
, &config_who_cmd
);
3222 install_element(VIEW_NODE
, &show_history_cmd
);
3223 install_element(CONFIG_NODE
, &line_vty_cmd
);
3224 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3225 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3226 install_element(CONFIG_NODE
, &show_history_cmd
);
3227 install_element(CONFIG_NODE
, &log_commands_cmd
);
3228 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3229 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3230 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3232 install_default(VTY_NODE
);
3233 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3234 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3235 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3236 install_element(VTY_NODE
, &vty_access_class_cmd
);
3237 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3238 install_element(VTY_NODE
, &vty_login_cmd
);
3239 install_element(VTY_NODE
, &no_vty_login_cmd
);
3240 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3241 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3244 void vty_terminate(void)
3247 XFREE(MTYPE_TMP
, vty_cwd
);
3249 if (vtyvec
&& Vvty_serv_thread
) {
3251 vector_free(vtyvec
);
3252 vector_free(Vvty_serv_thread
);
3254 Vvty_serv_thread
= NULL
;