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_serv(enum event event
, int sock
);
77 static void vty_event(enum event
, struct vty
*);
79 /* Extern host structure from command.c */
80 extern struct host host
;
82 /* Vector which store each vty structure. */
85 /* Vty timeout value. */
86 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
88 /* Vty access-class command */
89 static char *vty_accesslist_name
= NULL
;
91 /* Vty access-calss for IPv6. */
92 static char *vty_ipv6_accesslist_name
= NULL
;
94 /* VTY server thread. */
95 static vector Vvty_serv_thread
;
97 /* Current directory. */
98 static char vty_cwd
[MAXPATHLEN
];
100 /* Login password check. */
101 static int no_password_check
= 0;
103 /* Integrated configuration file path */
104 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
106 static bool do_log_commands
;
107 static bool do_log_commands_perm
;
109 void vty_frame(struct vty
*vty
, const char *format
, ...)
113 va_start(args
, format
);
114 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
115 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
116 vty
->frame_pos
= strlen(vty
->frame
);
120 void vty_endframe(struct vty
*vty
, const char *endtext
)
122 if (vty
->frame_pos
== 0 && endtext
)
123 vty_out(vty
, "%s", endtext
);
127 bool vty_set_include(struct vty
*vty
, const char *regexp
)
135 regfree(&vty
->include
);
141 errcode
= regcomp(&vty
->include
, regexp
,
142 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
145 regerror(errcode
, &vty
->include
, errbuf
, sizeof(errbuf
));
146 vty_out(vty
, "%% Regex compilation error: %s\n", errbuf
);
154 /* VTY standard output function. */
155 int vty_out(struct vty
*vty
, const char *format
, ...)
163 if (vty
->frame_pos
) {
165 vty_out(vty
, "%s", vty
->frame
);
168 va_start(args
, format
);
169 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
176 vector lines
= frrstr_split_vec(p
, "\n");
178 /* Place first value in the cache */
179 char *firstline
= vector_slot(lines
, 0);
180 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
182 /* If our split returned more than one entry, time to filter */
183 if (vector_active(lines
) > 1) {
185 * returned string is MTYPE_TMP so it matches the MTYPE
186 * of everything else in the vector
188 char *bstr
= buffer_getstr(vty
->lbuf
);
189 buffer_reset(vty
->lbuf
);
190 XFREE(MTYPE_TMP
, lines
->index
[0]);
191 vector_set_index(lines
, 0, bstr
);
192 frrstr_filter_vec(lines
, &vty
->include
);
193 vector_compact(lines
);
195 * Consider the string "foo\n". If the regex is an empty string
196 * and the line ended with a newline, then the vector will look
202 * If the regex isn't empty, the vector will look like:
206 * In this case we'd like to preserve the newline, so we add
207 * the empty string [1] as in the first example.
209 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
210 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
211 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
213 filtered
= frrstr_join_vec(lines
, "\n");
219 frrstr_strvec_free(lines
);
230 /* print with crlf replacement */
231 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
236 fprintf(vty
->of
, "%s", filtered
);
238 } else if (vty
->of_saved
) {
239 fprintf(vty
->of_saved
, "%s", filtered
);
240 fflush(vty
->of_saved
);
246 /* print without crlf replacement */
247 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
253 if (vty
->filter
&& filtered
)
254 XFREE(MTYPE_TMP
, filtered
);
256 /* If p is not different with buf, it is allocated buffer. */
258 XFREE(MTYPE_VTY_OUT_BUF
, p
);
263 static int vty_log_out(struct vty
*vty
, const char *level
,
264 const char *proto_str
, const char *msg
,
265 struct timestamp_control
*ctl
)
271 if (!ctl
->already_rendered
) {
272 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
274 ctl
->already_rendered
= 1;
276 if (ctl
->len
+ 1 >= sizeof(buf
))
278 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
283 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
286 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
287 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
290 if (((ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", msg
)) < 0)
291 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
297 if (write(vty
->wfd
, buf
, len
) < 0) {
298 if (ERRNO_IO_RETRY(errno
))
299 /* Kernel buffer is full, probably too much debugging
301 drop the data and ignore. */
303 /* Fatal I/O error. */
305 0; /* disable monitoring to avoid infinite recursion */
306 flog_err(EC_LIB_SOCKET
,
307 "%s: write failed to vty client fd %d, closing: %s",
308 __func__
, vty
->fd
, safe_strerror(errno
));
309 buffer_reset(vty
->obuf
);
310 buffer_reset(vty
->lbuf
);
311 /* cannot call vty_close, because a parent routine may still try
312 to access the vty struct */
313 vty
->status
= VTY_CLOSE
;
314 shutdown(vty
->fd
, SHUT_RDWR
);
320 /* Output current time to the vty. */
321 void vty_time_print(struct vty
*vty
, int cr
)
323 char buf
[QUAGGA_TIMESTAMP_LEN
];
325 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
326 zlog_info("quagga_timestamp error");
330 vty_out(vty
, "%s\n", buf
);
332 vty_out(vty
, "%s ", buf
);
337 /* Say hello to vty interface. */
338 void vty_hello(struct vty
*vty
)
344 f
= fopen(host
.motdfile
, "r");
346 while (fgets(buf
, sizeof(buf
), f
)) {
348 /* work backwards to ignore trailling isspace()
350 for (s
= buf
+ strlen(buf
);
351 (s
> buf
) && isspace((unsigned char)s
[-1]);
355 vty_out(vty
, "%s\n", buf
);
359 vty_out(vty
, "MOTD file not found\n");
360 } else if (host
.motd
)
361 vty_out(vty
, "%s", host
.motd
);
364 /* Put out prompt and wait input from user. */
365 static void vty_prompt(struct vty
*vty
)
367 if (vty
->type
== VTY_TERM
) {
368 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
372 /* Send WILL TELOPT_ECHO to remote server. */
373 static void vty_will_echo(struct vty
*vty
)
375 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
376 vty_out(vty
, "%s", cmd
);
379 /* Make suppress Go-Ahead telnet option. */
380 static void vty_will_suppress_go_ahead(struct vty
*vty
)
382 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
383 vty_out(vty
, "%s", cmd
);
386 /* Make don't use linemode over telnet. */
387 static void vty_dont_linemode(struct vty
*vty
)
389 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
390 vty_out(vty
, "%s", cmd
);
393 /* Use window size. */
394 static void vty_do_window_size(struct vty
*vty
)
396 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
397 vty_out(vty
, "%s", cmd
);
400 /* Authentication of vty */
401 static void vty_auth(struct vty
*vty
, char *buf
)
404 enum node_type next_node
= 0;
406 char *crypt(const char *, const char *);
411 passwd
= host
.password_encrypt
;
413 passwd
= host
.password
;
415 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
417 next_node
= VIEW_NODE
;
419 case AUTH_ENABLE_NODE
:
421 passwd
= host
.enable_encrypt
;
423 passwd
= host
.enable
;
424 next_node
= ENABLE_NODE
;
430 fail
= strcmp(crypt(buf
, passwd
), passwd
);
432 fail
= strcmp(buf
, passwd
);
438 vty
->node
= next_node
; /* Success ! */
441 if (vty
->fail
>= 3) {
442 if (vty
->node
== AUTH_NODE
) {
444 "%% Bad passwords, too many failures!\n");
445 vty
->status
= VTY_CLOSE
;
447 /* AUTH_ENABLE_NODE */
450 "%% Bad enable passwords, too many failures!\n");
451 vty
->status
= VTY_CLOSE
;
457 /* Command execution over the vty interface. */
458 static int vty_command(struct vty
*vty
, char *buf
)
461 const char *protocolname
;
467 * Log non empty command lines
472 /* Skip white spaces. */
473 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
476 if (cp
!= NULL
&& *cp
!= '\0') {
478 char vty_str
[VTY_BUFSIZ
];
479 char prompt_str
[VTY_BUFSIZ
];
481 /* format the base vty info */
482 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
484 for (i
= 0; i
< vector_active(vtyvec
); i
++)
485 if (vty
== vector_slot(vtyvec
, i
)) {
486 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
491 /* format the prompt */
492 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
495 /* now log the command */
496 zlog_notice("%s%s", prompt_str
, buf
);
499 #ifdef CONSUMED_TIME_CHECK
503 unsigned long realtime
, cputime
;
506 #endif /* CONSUMED_TIME_CHECK */
508 ret
= cmd_execute(vty
, buf
, NULL
, 0);
510 /* Get the name of the protocol if any */
511 protocolname
= frr_protoname
;
513 #ifdef CONSUMED_TIME_CHECK
515 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
516 > CONSUMED_TIME_CHECK
)
517 /* Warn about CPU hog that must be fixed. */
520 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
521 realtime
/ 1000, cputime
/ 1000, buf
);
523 #endif /* CONSUMED_TIME_CHECK */
525 if (ret
!= CMD_SUCCESS
)
528 if (vty
->type
== VTY_FILE
)
529 vty_out(vty
, "Warning...\n");
531 case CMD_ERR_AMBIGUOUS
:
532 vty_out(vty
, "%% Ambiguous command.\n");
534 case CMD_ERR_NO_MATCH
:
535 vty_out(vty
, "%% [%s] Unknown command: %s\n",
538 case CMD_ERR_INCOMPLETE
:
539 vty_out(vty
, "%% Command incomplete.\n");
546 static const char telnet_backward_char
= 0x08;
547 static const char telnet_space_char
= ' ';
549 /* Basic function to write buffer to vty. */
550 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
552 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
555 /* Should we do buffering here ? And make vty_flush (vty) ? */
556 buffer_put(vty
->obuf
, buf
, nbytes
);
559 /* Basic function to insert character into vty. */
560 static void vty_self_insert(struct vty
*vty
, char c
)
565 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
568 length
= vty
->length
- vty
->cp
;
569 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
570 vty
->buf
[vty
->cp
] = c
;
572 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
573 for (i
= 0; i
< length
; i
++)
574 vty_write(vty
, &telnet_backward_char
, 1);
579 vty
->buf
[vty
->length
] = '\0';
582 /* Self insert character 'c' in overwrite mode. */
583 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
585 if (vty
->cp
== vty
->length
) {
586 vty_self_insert(vty
, c
);
590 vty
->buf
[vty
->cp
++] = c
;
591 vty_write(vty
, &c
, 1);
595 * Insert a string into vty->buf at the current cursor position.
597 * If the resultant string would be larger than VTY_BUFSIZ it is
600 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
602 if (vty
->cp
== VTY_BUFSIZ
)
605 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
606 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
608 vty
->length
= MAX(vty
->cp
, vty
->length
);
609 vty
->buf
[vty
->length
] = '\0';
610 vty_write(vty
, str
, nwrite
);
613 /* Forward character. */
614 static void vty_forward_char(struct vty
*vty
)
616 if (vty
->cp
< vty
->length
) {
617 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
622 /* Backward character. */
623 static void vty_backward_char(struct vty
*vty
)
627 vty_write(vty
, &telnet_backward_char
, 1);
631 /* Move to the beginning of the line. */
632 static void vty_beginning_of_line(struct vty
*vty
)
635 vty_backward_char(vty
);
638 /* Move to the end of the line. */
639 static void vty_end_of_line(struct vty
*vty
)
641 while (vty
->cp
< vty
->length
)
642 vty_forward_char(vty
);
645 static void vty_kill_line_from_beginning(struct vty
*);
646 static void vty_redraw_line(struct vty
*);
648 /* Print command line history. This function is called from
649 vty_next_line and vty_previous_line. */
650 static void vty_history_print(struct vty
*vty
)
654 vty_kill_line_from_beginning(vty
);
656 /* Get previous line from history buffer */
657 length
= strlen(vty
->hist
[vty
->hp
]);
658 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
659 vty
->cp
= vty
->length
= length
;
660 vty
->buf
[vty
->length
] = '\0';
662 /* Redraw current line */
663 vty_redraw_line(vty
);
666 /* Show next command line history. */
667 static void vty_next_line(struct vty
*vty
)
671 if (vty
->hp
== vty
->hindex
)
674 /* Try is there history exist or not. */
676 if (try_index
== (VTY_MAXHIST
- 1))
681 /* If there is not history return. */
682 if (vty
->hist
[try_index
] == NULL
)
687 vty_history_print(vty
);
690 /* Show previous command line history. */
691 static void vty_previous_line(struct vty
*vty
)
697 try_index
= VTY_MAXHIST
- 1;
701 if (vty
->hist
[try_index
] == NULL
)
706 vty_history_print(vty
);
709 /* This function redraw all of the command line character. */
710 static void vty_redraw_line(struct vty
*vty
)
712 vty_write(vty
, vty
->buf
, vty
->length
);
713 vty
->cp
= vty
->length
;
717 static void vty_forward_word(struct vty
*vty
)
719 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
720 vty_forward_char(vty
);
722 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
723 vty_forward_char(vty
);
726 /* Backward word without skipping training space. */
727 static void vty_backward_pure_word(struct vty
*vty
)
729 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
730 vty_backward_char(vty
);
734 static void vty_backward_word(struct vty
*vty
)
736 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
737 vty_backward_char(vty
);
739 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
740 vty_backward_char(vty
);
743 /* When '^D' is typed at the beginning of the line we move to the down
745 static void vty_down_level(struct vty
*vty
)
753 /* When '^Z' is received from vty, move down to the enable mode. */
754 static void vty_end_config(struct vty
*vty
)
759 vty_config_exit(vty
);
760 vty
->node
= ENABLE_NODE
;
767 /* Delete a charcter at the current point. */
768 static void vty_delete_char(struct vty
*vty
)
773 if (vty
->length
== 0) {
778 if (vty
->cp
== vty
->length
)
779 return; /* completion need here? */
781 size
= vty
->length
- vty
->cp
;
784 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
785 vty
->buf
[vty
->length
] = '\0';
787 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
790 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
791 vty_write(vty
, &telnet_space_char
, 1);
793 for (i
= 0; i
< size
; i
++)
794 vty_write(vty
, &telnet_backward_char
, 1);
797 /* Delete a character before the point. */
798 static void vty_delete_backward_char(struct vty
*vty
)
803 vty_backward_char(vty
);
804 vty_delete_char(vty
);
807 /* Kill rest of line from current point. */
808 static void vty_kill_line(struct vty
*vty
)
813 size
= vty
->length
- vty
->cp
;
818 for (i
= 0; i
< size
; i
++)
819 vty_write(vty
, &telnet_space_char
, 1);
820 for (i
= 0; i
< size
; i
++)
821 vty_write(vty
, &telnet_backward_char
, 1);
823 memset(&vty
->buf
[vty
->cp
], 0, size
);
824 vty
->length
= vty
->cp
;
827 /* Kill line from the beginning. */
828 static void vty_kill_line_from_beginning(struct vty
*vty
)
830 vty_beginning_of_line(vty
);
834 /* Delete a word before the point. */
835 static void vty_forward_kill_word(struct vty
*vty
)
837 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
838 vty_delete_char(vty
);
839 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
840 vty_delete_char(vty
);
843 /* Delete a word before the point. */
844 static void vty_backward_kill_word(struct vty
*vty
)
846 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
847 vty_delete_backward_char(vty
);
848 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
849 vty_delete_backward_char(vty
);
852 /* Transpose chars before or at the point. */
853 static void vty_transpose_chars(struct vty
*vty
)
857 /* If length is short or point is near by the beginning of line then
859 if (vty
->length
< 2 || vty
->cp
< 1)
862 /* In case of point is located at the end of the line. */
863 if (vty
->cp
== vty
->length
) {
864 c1
= vty
->buf
[vty
->cp
- 1];
865 c2
= vty
->buf
[vty
->cp
- 2];
867 vty_backward_char(vty
);
868 vty_backward_char(vty
);
869 vty_self_insert_overwrite(vty
, c1
);
870 vty_self_insert_overwrite(vty
, c2
);
872 c1
= vty
->buf
[vty
->cp
];
873 c2
= vty
->buf
[vty
->cp
- 1];
875 vty_backward_char(vty
);
876 vty_self_insert_overwrite(vty
, c1
);
877 vty_self_insert_overwrite(vty
, c2
);
881 /* Do completion at vty interface. */
882 static void vty_complete_command(struct vty
*vty
)
886 char **matched
= NULL
;
889 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
892 vline
= cmd_make_strvec(vty
->buf
);
896 /* In case of 'help \t'. */
897 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
898 vector_set(vline
, NULL
);
900 matched
= cmd_complete_command(vline
, vty
, &ret
);
902 cmd_free_strvec(vline
);
906 case CMD_ERR_AMBIGUOUS
:
907 vty_out(vty
, "%% Ambiguous command.\n");
909 vty_redraw_line(vty
);
911 case CMD_ERR_NO_MATCH
:
912 /* vty_out (vty, "%% There is no matched command.\n"); */
914 vty_redraw_line(vty
);
916 case CMD_COMPLETE_FULL_MATCH
:
918 /* 2016-11-28 equinox -- need to debug, SEGV here */
919 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
921 vty_redraw_line(vty
);
925 vty_redraw_line(vty
);
926 vty_backward_pure_word(vty
);
927 vty_insert_word_overwrite(vty
, matched
[0]);
928 vty_self_insert(vty
, ' ');
929 XFREE(MTYPE_COMPLETION
, matched
[0]);
931 case CMD_COMPLETE_MATCH
:
933 vty_redraw_line(vty
);
934 vty_backward_pure_word(vty
);
935 vty_insert_word_overwrite(vty
, matched
[0]);
936 XFREE(MTYPE_COMPLETION
, matched
[0]);
938 case CMD_COMPLETE_LIST_MATCH
:
939 for (i
= 0; matched
[i
] != NULL
; i
++) {
940 if (i
!= 0 && ((i
% 6) == 0))
942 vty_out(vty
, "%-10s ", matched
[i
]);
943 XFREE(MTYPE_COMPLETION
, matched
[i
]);
948 vty_redraw_line(vty
);
950 case CMD_ERR_NOTHING_TODO
:
952 vty_redraw_line(vty
);
957 XFREE(MTYPE_TMP
, matched
);
960 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
961 unsigned int desc_width
, struct cmd_token
*token
)
969 if (desc_width
<= 0) {
970 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
974 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
976 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
977 for (pos
= desc_width
; pos
> 0; pos
--)
978 if (*(p
+ pos
) == ' ')
986 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
991 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
993 XFREE(MTYPE_TMP
, buf
);
996 /* Describe matched command function. */
997 static void vty_describe_command(struct vty
*vty
)
1002 unsigned int i
, width
, desc_width
;
1003 struct cmd_token
*token
, *token_cr
= NULL
;
1005 vline
= cmd_make_strvec(vty
->buf
);
1007 /* In case of '> ?'. */
1008 if (vline
== NULL
) {
1009 vline
= vector_init(1);
1010 vector_set(vline
, NULL
);
1011 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1012 vector_set(vline
, NULL
);
1014 describe
= cmd_describe_command(vline
, vty
, &ret
);
1018 /* Ambiguous error. */
1020 case CMD_ERR_AMBIGUOUS
:
1021 vty_out(vty
, "%% Ambiguous command.\n");
1024 case CMD_ERR_NO_MATCH
:
1025 vty_out(vty
, "%% There is no matched command.\n");
1030 /* Get width of command string. */
1032 for (i
= 0; i
< vector_active(describe
); i
++)
1033 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1036 if (token
->text
[0] == '\0')
1039 len
= strlen(token
->text
);
1045 /* Get width of description string. */
1046 desc_width
= vty
->width
- (width
+ 6);
1048 /* Print out description. */
1049 for (i
= 0; i
< vector_active(describe
); i
++)
1050 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1051 if (token
->text
[0] == '\0')
1054 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1060 vty_out(vty
, " %-s\n", token
->text
);
1061 else if (desc_width
>= strlen(token
->desc
))
1062 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1065 vty_describe_fold(vty
, width
, desc_width
,
1068 if (IS_VARYING_TOKEN(token
->type
)) {
1069 const char *ref
= vector_slot(
1070 vline
, vector_active(vline
) - 1);
1072 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1073 cmd_variable_complete(token
, ref
, varcomps
);
1075 if (vector_active(varcomps
) > 0) {
1076 char *ac
= cmd_variable_comp2str(
1077 varcomps
, vty
->width
);
1078 vty_out(vty
, "%s\n", ac
);
1079 XFREE(MTYPE_TMP
, ac
);
1082 vector_free(varcomps
);
1086 if ((token
= token_cr
)) {
1088 vty_out(vty
, " %-s\n", token
->text
);
1089 else if (desc_width
>= strlen(token
->desc
))
1090 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1093 vty_describe_fold(vty
, width
, desc_width
, token
);
1097 cmd_free_strvec(vline
);
1099 vector_free(describe
);
1102 vty_redraw_line(vty
);
1105 static void vty_clear_buf(struct vty
*vty
)
1107 memset(vty
->buf
, 0, vty
->max
);
1110 /* ^C stop current input and do not add command line to the history. */
1111 static void vty_stop_input(struct vty
*vty
)
1113 vty
->cp
= vty
->length
= 0;
1118 vty_config_exit(vty
);
1119 vty
->node
= ENABLE_NODE
;
1124 /* Set history pointer to the latest one. */
1125 vty
->hp
= vty
->hindex
;
1128 /* Add current command line to the history buffer. */
1129 static void vty_hist_add(struct vty
*vty
)
1133 if (vty
->length
== 0)
1136 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1138 /* Ignore the same string as previous one. */
1139 if (vty
->hist
[index
])
1140 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1141 vty
->hp
= vty
->hindex
;
1145 /* Insert history entry. */
1146 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1147 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1149 /* History index rotation. */
1151 if (vty
->hindex
== VTY_MAXHIST
)
1154 vty
->hp
= vty
->hindex
;
1157 /* #define TELNET_OPTION_DEBUG */
1159 /* Get telnet window size. */
1160 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1162 #ifdef TELNET_OPTION_DEBUG
1165 for (i
= 0; i
< nbytes
; i
++) {
1168 vty_out(vty
, "IAC ");
1171 vty_out(vty
, "WILL ");
1174 vty_out(vty
, "WONT ");
1177 vty_out(vty
, "DO ");
1180 vty_out(vty
, "DONT ");
1183 vty_out(vty
, "SB ");
1186 vty_out(vty
, "SE ");
1189 vty_out(vty
, "TELOPT_ECHO \n");
1192 vty_out(vty
, "TELOPT_SGA \n");
1195 vty_out(vty
, "TELOPT_NAWS \n");
1198 vty_out(vty
, "%x ", buf
[i
]);
1204 #endif /* TELNET_OPTION_DEBUG */
1209 vty
->iac_sb_in_progress
= 1;
1212 if (!vty
->iac_sb_in_progress
)
1215 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1216 vty
->iac_sb_in_progress
= 0;
1219 switch (vty
->sb_buf
[0]) {
1221 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1224 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1226 (unsigned long)vty
->sb_len
);
1227 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1230 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1231 (unsigned long)sizeof(vty
->sb_buf
),
1232 TELNET_NAWS_SB_LEN
);
1234 vty
->width
= ((vty
->sb_buf
[1] << 8)
1236 vty
->height
= ((vty
->sb_buf
[3] << 8)
1238 #ifdef TELNET_OPTION_DEBUG
1240 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1241 vty
->width
, vty
->height
);
1246 vty
->iac_sb_in_progress
= 0;
1255 /* Execute current command line. */
1256 static int vty_execute(struct vty
*vty
)
1262 switch (vty
->node
) {
1264 case AUTH_ENABLE_NODE
:
1265 vty_auth(vty
, vty
->buf
);
1268 ret
= vty_command(vty
, vty
->buf
);
1269 if (vty
->type
== VTY_TERM
)
1274 /* Clear command line buffer. */
1275 vty
->cp
= vty
->length
= 0;
1278 if (vty
->status
!= VTY_CLOSE
)
1284 #define CONTROL(X) ((X) - '@')
1285 #define VTY_NORMAL 0
1286 #define VTY_PRE_ESCAPE 1
1287 #define VTY_ESCAPE 2
1290 /* Escape character command map. */
1291 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1295 vty_previous_line(vty
);
1301 vty_forward_char(vty
);
1304 vty_backward_char(vty
);
1310 /* Go back to normal mode. */
1311 vty
->escape
= VTY_NORMAL
;
1314 /* Quit print out to the buffer. */
1315 static void vty_buffer_reset(struct vty
*vty
)
1317 buffer_reset(vty
->obuf
);
1318 buffer_reset(vty
->lbuf
);
1320 vty_redraw_line(vty
);
1323 /* Read data via vty socket. */
1324 static int vty_read(struct thread
*thread
)
1328 unsigned char buf
[VTY_READ_BUFSIZ
];
1330 struct vty
*vty
= THREAD_ARG(thread
);
1332 /* Read raw data from socket */
1333 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1335 if (ERRNO_IO_RETRY(errno
)) {
1336 vty_event(VTY_READ
, vty
);
1339 vty
->monitor
= 0; /* disable monitoring to avoid
1340 infinite recursion */
1343 "%s: read error on vty client fd %d, closing: %s",
1344 __func__
, vty
->fd
, safe_strerror(errno
));
1345 buffer_reset(vty
->obuf
);
1346 buffer_reset(vty
->lbuf
);
1348 vty
->status
= VTY_CLOSE
;
1351 for (i
= 0; i
< nbytes
; i
++) {
1352 if (buf
[i
] == IAC
) {
1361 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1362 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1363 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1369 /* In case of telnet command */
1371 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1378 if (vty
->status
== VTY_MORE
) {
1383 vty_buffer_reset(vty
);
1391 /* Escape character. */
1392 if (vty
->escape
== VTY_ESCAPE
) {
1393 vty_escape_map(buf
[i
], vty
);
1397 /* Pre-escape status. */
1398 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1401 vty
->escape
= VTY_ESCAPE
;
1404 vty_backward_word(vty
);
1405 vty
->escape
= VTY_NORMAL
;
1408 vty_forward_word(vty
);
1409 vty
->escape
= VTY_NORMAL
;
1412 vty_forward_kill_word(vty
);
1413 vty
->escape
= VTY_NORMAL
;
1417 vty_backward_kill_word(vty
);
1418 vty
->escape
= VTY_NORMAL
;
1421 vty
->escape
= VTY_NORMAL
;
1427 if (vty
->escape
== VTY_CR
) {
1428 /* if we get CR+NL, the NL results in an extra empty
1429 * prompt line being printed without this; just drop
1430 * the NL if it immediately follows CR.
1432 vty
->escape
= VTY_NORMAL
;
1440 vty_beginning_of_line(vty
);
1443 vty_backward_char(vty
);
1446 vty_stop_input(vty
);
1449 vty_delete_char(vty
);
1452 vty_end_of_line(vty
);
1455 vty_forward_char(vty
);
1459 vty_delete_backward_char(vty
);
1468 vty_previous_line(vty
);
1471 vty_transpose_chars(vty
);
1474 vty_kill_line_from_beginning(vty
);
1477 vty_backward_kill_word(vty
);
1480 vty_end_config(vty
);
1483 vty
->escape
= VTY_CR
;
1487 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1491 vty_complete_command(vty
);
1494 if (vty
->node
== AUTH_NODE
1495 || vty
->node
== AUTH_ENABLE_NODE
)
1496 vty_self_insert(vty
, buf
[i
]);
1498 vty_describe_command(vty
);
1501 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1502 vty
->escape
= VTY_ESCAPE
;
1505 vty
->escape
= VTY_PRE_ESCAPE
;
1508 if (buf
[i
] > 31 && buf
[i
] < 127)
1509 vty_self_insert(vty
, buf
[i
]);
1515 if (vty
->status
== VTY_CLOSE
)
1518 vty_event(VTY_WRITE
, vty
);
1519 vty_event(VTY_READ
, vty
);
1524 /* Flush buffer to the vty. */
1525 static int vty_flush(struct thread
*thread
)
1528 buffer_status_t flushrc
;
1529 struct vty
*vty
= THREAD_ARG(thread
);
1531 /* Tempolary disable read thread. */
1532 if (vty
->lines
== 0)
1533 THREAD_OFF(vty
->t_read
);
1535 /* Function execution continue. */
1536 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1538 /* N.B. if width is 0, that means we don't know the window size. */
1539 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1540 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1541 else if (vty
->status
== VTY_MORELINE
)
1542 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1545 flushrc
= buffer_flush_window(
1546 vty
->obuf
, vty
->wfd
, vty
->width
,
1547 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1551 0; /* disable monitoring to avoid infinite recursion */
1552 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1554 buffer_reset(vty
->lbuf
);
1555 buffer_reset(vty
->obuf
);
1559 if (vty
->status
== VTY_CLOSE
)
1562 vty
->status
= VTY_NORMAL
;
1563 if (vty
->lines
== 0)
1564 vty_event(VTY_READ
, vty
);
1567 case BUFFER_PENDING
:
1568 /* There is more data waiting to be written. */
1569 vty
->status
= VTY_MORE
;
1570 if (vty
->lines
== 0)
1571 vty_event(VTY_WRITE
, vty
);
1578 /* Allocate new vty struct. */
1579 struct vty
*vty_new(void)
1581 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1583 new->fd
= new->wfd
= -1;
1585 new->lbuf
= buffer_new(0);
1586 new->obuf
= buffer_new(0); /* Use default buffer size. */
1587 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1588 new->max
= VTY_BUFSIZ
;
1594 /* allocate and initialise vty */
1595 static struct vty
*vty_new_init(int vty_sock
)
1601 vty
->wfd
= vty_sock
;
1602 vty
->type
= VTY_TERM
;
1603 vty
->node
= AUTH_NODE
;
1608 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1611 vty
->xpath_index
= 0;
1612 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1613 vty
->private_config
= false;
1614 vty
->candidate_config
= vty_shared_candidate_config
;
1615 vector_set_index(vtyvec
, vty_sock
, vty
);
1616 vty
->status
= VTY_NORMAL
;
1619 vty
->iac_sb_in_progress
= 0;
1625 /* Create new vty structure. */
1626 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1628 char buf
[SU_ADDRSTRLEN
];
1631 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1633 /* Allocate new vty structure and set up default values. */
1634 vty
= vty_new_init(vty_sock
);
1636 /* configurable parameters not part of basic init */
1637 vty
->v_timeout
= vty_timeout_val
;
1638 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1639 if (no_password_check
) {
1641 vty
->node
= ENABLE_NODE
;
1643 vty
->node
= VIEW_NODE
;
1645 if (host
.lines
>= 0)
1646 vty
->lines
= host
.lines
;
1648 if (!no_password_check
) {
1649 /* Vty is not available if password isn't set. */
1650 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1651 vty_out(vty
, "Vty password is not set.\n");
1652 vty
->status
= VTY_CLOSE
;
1658 /* Say hello to the world. */
1660 if (!no_password_check
)
1661 vty_out(vty
, "\nUser Access Verification\n\n");
1663 /* Setting up terminal. */
1665 vty_will_suppress_go_ahead(vty
);
1667 vty_dont_linemode(vty
);
1668 vty_do_window_size(vty
);
1669 /* vty_dont_lflow_ahead (vty); */
1673 /* Add read/write thread. */
1674 vty_event(VTY_WRITE
, vty
);
1675 vty_event(VTY_READ
, vty
);
1680 /* create vty for stdio */
1681 static struct termios stdio_orig_termios
;
1682 static struct vty
*stdio_vty
= NULL
;
1683 static bool stdio_termios
= false;
1684 static void (*stdio_vty_atclose
)(int isexit
);
1686 static void vty_stdio_reset(int isexit
)
1690 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1691 stdio_termios
= false;
1695 if (stdio_vty_atclose
)
1696 stdio_vty_atclose(isexit
);
1697 stdio_vty_atclose
= NULL
;
1701 static void vty_stdio_atexit(void)
1706 void vty_stdio_suspend(void)
1711 THREAD_OFF(stdio_vty
->t_write
);
1712 THREAD_OFF(stdio_vty
->t_read
);
1713 THREAD_OFF(stdio_vty
->t_timeout
);
1716 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1717 stdio_termios
= false;
1720 void vty_stdio_resume(void)
1725 if (!tcgetattr(0, &stdio_orig_termios
)) {
1726 struct termios termios
;
1728 termios
= stdio_orig_termios
;
1729 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1730 | IGNCR
| ICRNL
| IXON
);
1731 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1732 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1733 termios
.c_cflag
|= CS8
;
1734 tcsetattr(0, TCSANOW
, &termios
);
1735 stdio_termios
= true;
1738 vty_prompt(stdio_vty
);
1740 /* Add read/write thread. */
1741 vty_event(VTY_WRITE
, stdio_vty
);
1742 vty_event(VTY_READ
, stdio_vty
);
1745 void vty_stdio_close(void)
1749 vty_close(stdio_vty
);
1752 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1756 /* refuse creating two vtys on stdio */
1760 vty
= stdio_vty
= vty_new_init(0);
1761 stdio_vty_atclose
= atclose
;
1764 /* always have stdio vty in a known _unchangeable_ state, don't want
1766 * to have any effect here to make sure scripting this works as intended
1768 vty
->node
= ENABLE_NODE
;
1770 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1776 /* Accept connection from the network. */
1777 static int vty_accept(struct thread
*thread
)
1785 struct access_list
*acl
= NULL
;
1787 accept_sock
= THREAD_FD(thread
);
1789 /* We continue hearing vty socket. */
1790 vty_event_serv(VTY_SERV
, accept_sock
);
1792 memset(&su
, 0, sizeof(union sockunion
));
1794 /* We can handle IPv4 or IPv6 socket. */
1795 vty_sock
= sockunion_accept(accept_sock
, &su
);
1797 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1798 safe_strerror(errno
));
1801 set_nonblocking(vty_sock
);
1802 set_cloexec(vty_sock
);
1804 if (!sockunion2hostprefix(&su
, &p
)) {
1806 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1811 /* VTY's accesslist apply. */
1812 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1813 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1814 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1815 zlog_info("Vty connection refused from %pSU", &su
);
1818 /* continue accepting connections */
1819 vty_event_serv(VTY_SERV
, accept_sock
);
1825 /* VTY's ipv6 accesslist apply. */
1826 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1827 if ((acl
= access_list_lookup(AFI_IP6
,
1828 vty_ipv6_accesslist_name
))
1829 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1830 zlog_info("Vty connection refused from %pSU", &su
);
1833 /* continue accepting connections */
1834 vty_event_serv(VTY_SERV
, accept_sock
);
1841 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1844 zlog_info("can't set sockopt to vty_sock : %s",
1845 safe_strerror(errno
));
1847 zlog_info("Vty connection from %pSU", &su
);
1849 vty_create(vty_sock
, &su
);
1854 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1857 struct addrinfo req
;
1858 struct addrinfo
*ainfo
;
1859 struct addrinfo
*ainfo_save
;
1861 char port_str
[BUFSIZ
];
1863 memset(&req
, 0, sizeof(struct addrinfo
));
1864 req
.ai_flags
= AI_PASSIVE
;
1865 req
.ai_family
= AF_UNSPEC
;
1866 req
.ai_socktype
= SOCK_STREAM
;
1867 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1868 port_str
[sizeof(port_str
) - 1] = '\0';
1870 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1873 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1881 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1884 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1885 ainfo
->ai_protocol
);
1889 sockopt_v6only(ainfo
->ai_family
, sock
);
1890 sockopt_reuseaddr(sock
);
1891 sockopt_reuseport(sock
);
1894 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1896 close(sock
); /* Avoid sd leak. */
1900 ret
= listen(sock
, 3);
1902 close(sock
); /* Avoid sd leak. */
1906 vty_event_serv(VTY_SERV
, sock
);
1907 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1909 freeaddrinfo(ainfo_save
);
1913 /* For sockaddr_un. */
1916 /* VTY shell UNIX domain socket. */
1917 static void vty_serv_un(const char *path
)
1921 struct sockaddr_un serv
;
1923 struct zprivs_ids_t ids
;
1925 /* First of all, unlink existing socket */
1929 old_mask
= umask(0007);
1931 /* Make UNIX domain socket. */
1932 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1934 flog_err_sys(EC_LIB_SOCKET
,
1935 "Cannot create unix stream socket: %s",
1936 safe_strerror(errno
));
1940 /* Make server socket. */
1941 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1942 serv
.sun_family
= AF_UNIX
;
1943 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1944 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1945 len
= serv
.sun_len
= SUN_LEN(&serv
);
1947 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1948 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1952 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1954 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1955 safe_strerror(errno
));
1956 close(sock
); /* Avoid sd leak. */
1960 ret
= listen(sock
, 5);
1962 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1963 safe_strerror(errno
));
1964 close(sock
); /* Avoid sd leak. */
1970 zprivs_get_ids(&ids
);
1972 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1973 earlier for the case when we don't need to chown the file
1974 type casting it here to make a compare */
1975 if ((int)ids
.gid_vty
> 0) {
1976 /* set group of socket */
1977 if (chown(path
, -1, ids
.gid_vty
)) {
1978 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1979 "vty_serv_un: could chown socket, %s",
1980 safe_strerror(errno
));
1984 vty_event_serv(VTYSH_SERV
, sock
);
1987 /* #define VTYSH_DEBUG 1 */
1989 static int vtysh_accept(struct thread
*thread
)
1994 struct sockaddr_un client
;
1997 accept_sock
= THREAD_FD(thread
);
1999 vty_event_serv(VTYSH_SERV
, accept_sock
);
2001 memset(&client
, 0, sizeof(struct sockaddr_un
));
2002 client_len
= sizeof(struct sockaddr_un
);
2004 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2005 (socklen_t
*)&client_len
);
2008 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2009 safe_strerror(errno
));
2013 if (set_nonblocking(sock
) < 0) {
2016 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2017 sock
, safe_strerror(errno
));
2024 printf("VTY shell accept\n");
2025 #endif /* VTYSH_DEBUG */
2030 vty
->type
= VTY_SHELL_SERV
;
2031 vty
->node
= VIEW_NODE
;
2033 vty_event(VTYSH_READ
, vty
);
2038 static int vtysh_flush(struct vty
*vty
)
2040 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2041 case BUFFER_PENDING
:
2042 vty_event(VTYSH_WRITE
, vty
);
2046 0; /* disable monitoring to avoid infinite recursion */
2047 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2049 buffer_reset(vty
->lbuf
);
2050 buffer_reset(vty
->obuf
);
2059 static int vtysh_read(struct thread
*thread
)
2065 unsigned char buf
[VTY_READ_BUFSIZ
];
2067 uint8_t header
[4] = {0, 0, 0, 0};
2069 sock
= THREAD_FD(thread
);
2070 vty
= THREAD_ARG(thread
);
2072 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2074 if (ERRNO_IO_RETRY(errno
)) {
2075 vty_event(VTYSH_READ
, vty
);
2078 vty
->monitor
= 0; /* disable monitoring to avoid
2079 infinite recursion */
2082 "%s: read failed on vtysh client fd %d, closing: %s",
2083 __func__
, sock
, safe_strerror(errno
));
2085 buffer_reset(vty
->lbuf
);
2086 buffer_reset(vty
->obuf
);
2089 printf("close vtysh\n");
2090 #endif /* VTYSH_DEBUG */
2095 printf("line: %.*s\n", nbytes
, buf
);
2096 #endif /* VTYSH_DEBUG */
2098 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2099 /* Clear command line buffer. */
2100 vty
->cp
= vty
->length
= 0;
2102 vty_out(vty
, "%% Command is too long.\n");
2104 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2105 vty
->buf
[vty
->length
++] = *p
;
2107 /* Pass this line to parser. */
2108 ret
= vty_execute(vty
);
2109 /* Note that vty_execute clears the command buffer and resets
2110 vty->length to 0. */
2112 /* Return result. */
2114 printf("result: %d\n", ret
);
2115 printf("vtysh node: %d\n", vty
->node
);
2116 #endif /* VTYSH_DEBUG */
2118 /* hack for asynchronous "write integrated"
2119 * - other commands in "buf" will be ditched
2120 * - input during pending config-write is
2122 if (ret
== CMD_SUSPEND
)
2125 /* warning: watchfrr hardcodes this result write
2128 buffer_put(vty
->obuf
, header
, 4);
2130 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2131 /* Try to flush results; exit if a write
2138 if (vty
->status
== VTY_CLOSE
)
2141 vty_event(VTYSH_READ
, vty
);
2146 static int vtysh_write(struct thread
*thread
)
2148 struct vty
*vty
= THREAD_ARG(thread
);
2156 /* Determine address family to bind. */
2157 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2159 /* If port is set to 0, do not listen on TCP/IP at all! */
2161 vty_serv_sock_addrinfo(addr
, port
);
2168 static void vty_error_delete(void *arg
)
2170 struct vty_error
*ve
= arg
;
2172 XFREE(MTYPE_TMP
, ve
);
2175 /* Close vty interface. Warning: call this only from functions that
2176 will be careful not to access the vty afterwards (since it has
2177 now been freed). This is safest from top-level functions (called
2178 directly by the thread dispatcher). */
2179 void vty_close(struct vty
*vty
)
2182 bool was_stdio
= false;
2184 /* Drop out of configure / transaction if needed. */
2185 vty_config_exit(vty
);
2187 /* Cancel threads.*/
2188 THREAD_OFF(vty
->t_read
);
2189 THREAD_OFF(vty
->t_write
);
2190 THREAD_OFF(vty
->t_timeout
);
2193 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2195 /* Free input buffer. */
2196 buffer_free(vty
->obuf
);
2197 buffer_free(vty
->lbuf
);
2199 /* Free command history. */
2200 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2201 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2206 vector_unset(vtyvec
, vty
->fd
);
2208 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2212 * note check is for fd > STDERR_FILENO, not fd != -1.
2213 * We never close stdin/stdout/stderr here, because we may be
2214 * running in foreground mode with logging to stdout. Also,
2215 * additionally, we'd need to replace these fds with /dev/null. */
2216 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2218 if (vty
->fd
> STDERR_FILENO
)
2220 if (vty
->fd
== STDIN_FILENO
)
2223 XFREE(MTYPE_VTY
, vty
->buf
);
2226 vty
->error
->del
= vty_error_delete
;
2227 list_delete(&vty
->error
);
2231 XFREE(MTYPE_VTY
, vty
);
2237 /* When time out occur output message then close connection. */
2238 static int vty_timeout(struct thread
*thread
)
2242 vty
= THREAD_ARG(thread
);
2246 buffer_reset(vty
->lbuf
);
2247 buffer_reset(vty
->obuf
);
2248 vty_out(vty
, "\nVty connection is timed out.\n");
2250 /* Close connection. */
2251 vty
->status
= VTY_CLOSE
;
2257 /* Read up configuration file from file_name. */
2258 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2262 struct vty_error
*ve
;
2263 struct listnode
*node
;
2264 unsigned int line_num
= 0;
2267 /* vty_close won't close stderr; if some config command prints
2268 * something it'll end up there. (not ideal; it'd be beter if output
2269 * from a file-load went to logging instead. Also note that if this
2270 * function is called after daemonizing, stderr will be /dev/null.)
2272 * vty->fd will be -1 from vty_new()
2274 vty
->wfd
= STDERR_FILENO
;
2275 vty
->type
= VTY_FILE
;
2276 vty
->node
= CONFIG_NODE
;
2279 vty
->candidate_config
= config
;
2281 vty
->private_config
= true;
2282 vty
->candidate_config
= nb_config_new(NULL
);
2285 /* Execute configuration file */
2286 ret
= config_from_file(vty
, confp
, &line_num
);
2288 /* Flush any previous errors before printing messages below */
2289 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2291 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2292 const char *message
= NULL
;
2296 case CMD_ERR_AMBIGUOUS
:
2297 message
= "Ambiguous command";
2299 case CMD_ERR_NO_MATCH
:
2300 message
= "No such command";
2303 message
= "Command returned Warning";
2305 case CMD_WARNING_CONFIG_FAILED
:
2306 message
= "Command returned Warning Config Failed";
2308 case CMD_ERR_INCOMPLETE
:
2309 message
= "Command returned Incomplete";
2311 case CMD_ERR_EXEED_ARGC_MAX
:
2313 "Command exceeded maximum number of Arguments";
2316 message
= "Command returned unhandled error message";
2320 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2321 nl
= strchr(ve
->error_buf
, '\n');
2324 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2325 message
, ve
->line_num
, ve
->error_buf
);
2330 * Automatically commit the candidate configuration after
2331 * reading the configuration file.
2333 if (config
== NULL
) {
2334 struct nb_context context
= {};
2335 char errmsg
[BUFSIZ
] = {0};
2337 context
.client
= NB_CLIENT_CLI
;
2339 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2340 "Read configuration file", NULL
,
2341 errmsg
, sizeof(errmsg
));
2342 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2344 "%s: failed to read configuration file: %s (%s)",
2345 __func__
, nb_err_name(ret
), errmsg
);
2351 static FILE *vty_use_backup_config(const char *fullpath
)
2353 char *fullpath_sav
, *fullpath_tmp
;
2359 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2360 fullpath_sav
= malloc(fullpath_sav_sz
);
2361 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2362 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2364 sav
= open(fullpath_sav
, O_RDONLY
);
2370 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2371 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2373 /* Open file to configuration write. */
2374 tmp
= mkstemp(fullpath_tmp
);
2378 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2381 while ((c
= read(sav
, buffer
, 512)) > 0) {
2382 if (write(tmp
, buffer
, c
) <= 0)
2388 if (rename(fullpath_tmp
, fullpath
) == 0)
2389 ret
= fopen(fullpath
, "r");
2391 unlink(fullpath_tmp
);
2396 unlink(fullpath_tmp
);
2406 /* Read up configuration file from file_name. */
2407 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2408 char *config_default_dir
)
2410 char cwd
[MAXPATHLEN
];
2412 const char *fullpath
;
2414 bool read_success
= false;
2416 /* If -f flag specified. */
2417 if (config_file
!= NULL
) {
2418 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2419 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2422 "%s: failure to determine Current Working Directory %d!",
2424 goto tmp_free_and_out
;
2426 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2427 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2428 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2431 fullpath
= config_file
;
2433 confp
= fopen(fullpath
, "r");
2435 if (confp
== NULL
) {
2437 EC_LIB_BACKUP_CONFIG
,
2438 "%s: failed to open configuration file %s: %s, checking backup",
2439 __func__
, fullpath
, safe_strerror(errno
));
2441 confp
= vty_use_backup_config(fullpath
);
2443 flog_warn(EC_LIB_BACKUP_CONFIG
,
2444 "using backup configuration file!");
2448 "%s: can't open configuration file [%s]",
2449 __func__
, config_file
);
2450 goto tmp_free_and_out
;
2455 host_config_set(config_default_dir
);
2459 struct stat conf_stat
;
2461 /* !!!!PLEASE LEAVE!!!!
2462 * This is NEEDED for use with vtysh -b, or else you can get
2463 * a real configuration food fight with a lot garbage in the
2464 * merged configuration file it creates coming from the per
2465 * daemon configuration files. This also allows the daemons
2466 * to start if there default configuration file is not
2467 * present or ignore them, as needed when using vtysh -b to
2468 * configure the daemons at boot - MAG
2471 /* Stat for vtysh Zebra.conf, if found startup and wait for
2472 * boot configuration
2475 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2476 ret
= stat(integrate_default
, &conf_stat
);
2478 read_success
= true;
2479 goto tmp_free_and_out
;
2483 confp
= fopen(config_default_dir
, "r");
2484 if (confp
== NULL
) {
2487 "%s: failed to open configuration file %s: %s, checking backup",
2488 __func__
, config_default_dir
,
2489 safe_strerror(errno
));
2491 confp
= vty_use_backup_config(config_default_dir
);
2493 flog_warn(EC_LIB_BACKUP_CONFIG
,
2494 "using backup configuration file!");
2495 fullpath
= config_default_dir
;
2497 flog_err(EC_LIB_VTY
,
2498 "can't open configuration file [%s]",
2499 config_default_dir
);
2500 goto tmp_free_and_out
;
2503 fullpath
= config_default_dir
;
2506 vty_read_file(config
, confp
);
2507 read_success
= true;
2511 host_config_set(fullpath
);
2514 XFREE(MTYPE_TMP
, tmp
);
2516 return read_success
;
2519 /* Small utility function which output log to the VTY. */
2520 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2521 struct timestamp_control
*ctl
)
2529 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2530 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2532 vty_log_out(vty
, level
, proto_str
, msg
, ctl
);
2535 /* Async-signal-safe version of vty_log for fixed strings. */
2536 void vty_log_fixed(char *buf
, size_t len
)
2539 struct iovec iov
[2];
2540 char crlf
[4] = "\r\n";
2542 /* vty may not have been initialised */
2546 iov
[0].iov_base
= buf
;
2547 iov
[0].iov_len
= len
;
2548 iov
[1].iov_base
= crlf
;
2551 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2553 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2554 /* N.B. We don't care about the return code, since
2556 most likely just about to die anyway. */
2557 if (writev(vty
->wfd
, iov
, 2) == -1) {
2558 fprintf(stderr
, "Failure to writev: %d\n",
2565 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2567 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2568 vty_out(vty
, "%% Configuration is locked by other client\n");
2572 vty
->node
= CONFIG_NODE
;
2574 vty
->private_config
= private_config
;
2575 vty
->xpath_index
= 0;
2577 if (private_config
) {
2578 vty
->candidate_config
= nb_config_dup(running_config
);
2579 vty
->candidate_config_base
= nb_config_dup(running_config
);
2581 "Warning: uncommitted changes will be discarded on exit.\n\n");
2583 vty
->candidate_config
= vty_shared_candidate_config
;
2584 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2585 vty
->candidate_config_base
=
2586 nb_config_dup(running_config
);
2592 void vty_config_exit(struct vty
*vty
)
2594 enum node_type node
= vty
->node
;
2595 struct cmd_node
*cnode
;
2597 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2598 * somewhere below CONFIG_NODE */
2599 while (node
&& node
!= CONFIG_NODE
) {
2600 cnode
= vector_lookup(cmdvec
, node
);
2601 node
= cnode
->parent_node
;
2603 if (node
!= CONFIG_NODE
)
2604 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2607 while (vty
->node
!= ENABLE_NODE
)
2608 /* will call vty_config_node_exit() below */
2612 int vty_config_node_exit(struct vty
*vty
)
2614 vty
->xpath_index
= 0;
2616 /* Perform pending commit if any. */
2617 nb_cli_pending_commit_check(vty
);
2619 /* Check if there's a pending confirmed commit. */
2620 if (vty
->t_confirmed_commit_timeout
) {
2622 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2623 nb_cli_confirmed_commit_rollback(vty
);
2624 nb_cli_confirmed_commit_clean(vty
);
2627 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2629 if (vty
->candidate_config
) {
2630 if (vty
->private_config
)
2631 nb_config_free(vty
->candidate_config
);
2632 vty
->candidate_config
= NULL
;
2634 if (vty
->candidate_config_base
) {
2635 nb_config_free(vty
->candidate_config_base
);
2636 vty
->candidate_config_base
= NULL
;
2639 vty
->config
= false;
2643 /* Master of the threads. */
2644 static struct thread_master
*vty_master
;
2646 static void vty_event_serv(enum event event
, int sock
)
2648 struct thread
*vty_serv_thread
= NULL
;
2652 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
,
2654 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2658 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
,
2660 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2664 assert(!"vty_event_serv() called incorrectly");
2668 static void vty_event(enum event event
, struct vty
*vty
)
2673 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2677 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2682 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
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
, vty
->wfd
,
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
);
2703 assert(!"vty_event() called incorrectly");
2707 DEFUN_NOSH (config_who
,
2710 "Display who is on vty\n")
2715 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2716 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2717 vty_out(vty
, "%svty[%d] connected from %s.\n",
2718 v
->config
? "*" : " ", i
, v
->address
);
2722 /* Move to vty configuration mode. */
2723 DEFUN_NOSH (line_vty
,
2726 "Configure a terminal line\n"
2727 "Virtual terminal\n")
2729 vty
->node
= VTY_NODE
;
2733 /* Set time out value. */
2734 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2735 const char *sec_str
)
2737 unsigned long timeout
= 0;
2739 /* min_str and sec_str are already checked by parser. So it must be
2740 all digit string. */
2742 timeout
= strtol(min_str
, NULL
, 10);
2746 timeout
+= strtol(sec_str
, NULL
, 10);
2748 vty_timeout_val
= timeout
;
2749 vty
->v_timeout
= timeout
;
2750 vty_event(VTY_TIMEOUT_RESET
, vty
);
2756 DEFUN (exec_timeout_min
,
2757 exec_timeout_min_cmd
,
2758 "exec-timeout (0-35791)",
2759 "Set timeout value\n"
2760 "Timeout value in minutes\n")
2763 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2766 DEFUN (exec_timeout_sec
,
2767 exec_timeout_sec_cmd
,
2768 "exec-timeout (0-35791) (0-2147483)",
2769 "Set the EXEC timeout\n"
2770 "Timeout in minutes\n"
2771 "Timeout in seconds\n")
2774 int idx_number_2
= 2;
2775 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2776 argv
[idx_number_2
]->arg
);
2779 DEFUN (no_exec_timeout
,
2780 no_exec_timeout_cmd
,
2783 "Set the EXEC timeout\n")
2785 return exec_timeout(vty
, NULL
, NULL
);
2788 /* Set vty access class. */
2789 DEFUN (vty_access_class
,
2790 vty_access_class_cmd
,
2791 "access-class WORD",
2792 "Filter connections based on an IP access list\n"
2796 if (vty_accesslist_name
)
2797 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2799 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2804 /* Clear vty access class. */
2805 DEFUN (no_vty_access_class
,
2806 no_vty_access_class_cmd
,
2807 "no access-class [WORD]",
2809 "Filter connections based on an IP access list\n"
2813 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2814 if (!vty_accesslist_name
2815 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2816 vty_out(vty
, "Access-class is not currently applied to vty\n");
2817 return CMD_WARNING_CONFIG_FAILED
;
2820 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2822 vty_accesslist_name
= NULL
;
2827 /* Set vty access class. */
2828 DEFUN (vty_ipv6_access_class
,
2829 vty_ipv6_access_class_cmd
,
2830 "ipv6 access-class WORD",
2832 "Filter connections based on an IP access list\n"
2833 "IPv6 access list\n")
2836 if (vty_ipv6_accesslist_name
)
2837 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2839 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2844 /* Clear vty access class. */
2845 DEFUN (no_vty_ipv6_access_class
,
2846 no_vty_ipv6_access_class_cmd
,
2847 "no ipv6 access-class [WORD]",
2850 "Filter connections based on an IP access list\n"
2851 "IPv6 access list\n")
2854 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2856 if (!vty_ipv6_accesslist_name
2857 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2859 "IPv6 access-class is not currently applied to vty\n");
2860 return CMD_WARNING_CONFIG_FAILED
;
2863 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2865 vty_ipv6_accesslist_name
= NULL
;
2874 "Enable password checking\n")
2876 no_password_check
= 0;
2880 DEFUN (no_vty_login
,
2884 "Enable password checking\n")
2886 no_password_check
= 1;
2890 DEFUN (service_advanced_vty
,
2891 service_advanced_vty_cmd
,
2892 "service advanced-vty",
2893 "Set up miscellaneous service\n"
2894 "Enable advanced mode vty interface\n")
2900 DEFUN (no_service_advanced_vty
,
2901 no_service_advanced_vty_cmd
,
2902 "no service advanced-vty",
2904 "Set up miscellaneous service\n"
2905 "Enable advanced mode vty interface\n")
2911 DEFUN_NOSH (terminal_monitor
,
2912 terminal_monitor_cmd
,
2914 "Set terminal line parameters\n"
2915 "Copy debug output to the current terminal line\n")
2921 DEFUN_NOSH (terminal_no_monitor
,
2922 terminal_no_monitor_cmd
,
2923 "terminal no monitor",
2924 "Set terminal line parameters\n"
2926 "Copy debug output to the current terminal line\n")
2932 DEFUN_NOSH (no_terminal_monitor
,
2933 no_terminal_monitor_cmd
,
2934 "no terminal monitor",
2936 "Set terminal line parameters\n"
2937 "Copy debug output to the current terminal line\n")
2939 return terminal_no_monitor(self
, vty
, argc
, argv
);
2943 DEFUN_NOSH (show_history
,
2947 "Display the session command history\n")
2951 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2952 if (index
== VTY_MAXHIST
) {
2957 if (vty
->hist
[index
] != NULL
)
2958 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2967 DEFPY (log_commands
,
2969 "[no] log commands",
2972 "Log all commands\n")
2975 if (do_log_commands_perm
) {
2977 "Daemon started with permanent logging turned on for commands, ignoring\n");
2981 do_log_commands
= false;
2983 do_log_commands
= true;
2988 /* Display current configuration. */
2989 static int vty_config_write(struct vty
*vty
)
2991 vty_out(vty
, "line vty\n");
2993 if (vty_accesslist_name
)
2994 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2996 if (vty_ipv6_accesslist_name
)
2997 vty_out(vty
, " ipv6 access-class %s\n",
2998 vty_ipv6_accesslist_name
);
3001 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3002 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3003 vty_timeout_val
% 60);
3006 if (no_password_check
)
3007 vty_out(vty
, " no login\n");
3009 if (do_log_commands
)
3010 vty_out(vty
, "log commands\n");
3012 vty_out(vty
, "!\n");
3017 static int vty_config_write(struct vty
*vty
);
3018 struct cmd_node vty_node
= {
3021 .parent_node
= CONFIG_NODE
,
3022 .prompt
= "%s(config-line)# ",
3023 .config_write
= vty_config_write
,
3026 /* Reset all VTY status. */
3027 void vty_reset(void)
3031 struct thread
*vty_serv_thread
;
3033 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3034 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3035 buffer_reset(vty
->lbuf
);
3036 buffer_reset(vty
->obuf
);
3037 vty
->status
= VTY_CLOSE
;
3041 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3042 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3044 THREAD_OFF(vty_serv_thread
);
3045 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3049 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3051 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3052 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3055 static void vty_save_cwd(void)
3059 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3063 * At this point if these go wrong, more than likely
3064 * the whole world is coming down around us
3065 * Hence not worrying about it too much.
3067 if (!chdir(SYSCONFDIR
)) {
3068 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3069 "Failure to chdir to %s, errno: %d",
3073 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3074 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3075 "Failure to getcwd, errno: %d", errno
);
3081 char *vty_get_cwd(void)
3086 int vty_shell(struct vty
*vty
)
3088 return vty
->type
== VTY_SHELL
? 1 : 0;
3091 int vty_shell_serv(struct vty
*vty
)
3093 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3096 void vty_init_vtysh(void)
3098 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3101 /* Install vty's own commands like `who' command. */
3102 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3104 /* For further configuration read, preserve current directory. */
3107 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3109 vty_master
= master_thread
;
3111 atexit(vty_stdio_atexit
);
3113 /* Initilize server thread vector. */
3114 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3116 /* Install bgp top node. */
3117 install_node(&vty_node
);
3119 install_element(VIEW_NODE
, &config_who_cmd
);
3120 install_element(VIEW_NODE
, &show_history_cmd
);
3121 install_element(CONFIG_NODE
, &line_vty_cmd
);
3122 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3123 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3124 install_element(CONFIG_NODE
, &show_history_cmd
);
3125 install_element(CONFIG_NODE
, &log_commands_cmd
);
3127 if (do_command_logging
) {
3128 do_log_commands
= true;
3129 do_log_commands_perm
= true;
3132 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3133 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3134 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3136 install_default(VTY_NODE
);
3137 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3138 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3139 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3140 install_element(VTY_NODE
, &vty_access_class_cmd
);
3141 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3142 install_element(VTY_NODE
, &vty_login_cmd
);
3143 install_element(VTY_NODE
, &no_vty_login_cmd
);
3144 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3145 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3148 void vty_terminate(void)
3150 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3152 if (vtyvec
&& Vvty_serv_thread
) {
3154 vector_free(vtyvec
);
3155 vector_free(Vvty_serv_thread
);
3157 Vvty_serv_thread
= NULL
;