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
, ...)
162 /* format string may contain %m, keep errno intact for printfrr */
163 int saved_errno
= errno
;
165 if (vty
->frame_pos
) {
167 vty_out(vty
, "%s", vty
->frame
);
170 va_start(args
, format
);
172 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
179 vector lines
= frrstr_split_vec(p
, "\n");
181 /* Place first value in the cache */
182 char *firstline
= vector_slot(lines
, 0);
183 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
185 /* If our split returned more than one entry, time to filter */
186 if (vector_active(lines
) > 1) {
188 * returned string is MTYPE_TMP so it matches the MTYPE
189 * of everything else in the vector
191 char *bstr
= buffer_getstr(vty
->lbuf
);
192 buffer_reset(vty
->lbuf
);
193 XFREE(MTYPE_TMP
, lines
->index
[0]);
194 vector_set_index(lines
, 0, bstr
);
195 frrstr_filter_vec(lines
, &vty
->include
);
196 vector_compact(lines
);
198 * Consider the string "foo\n". If the regex is an empty string
199 * and the line ended with a newline, then the vector will look
205 * If the regex isn't empty, the vector will look like:
209 * In this case we'd like to preserve the newline, so we add
210 * the empty string [1] as in the first example.
212 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
213 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
214 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
216 filtered
= frrstr_join_vec(lines
, "\n");
222 frrstr_strvec_free(lines
);
233 /* print with crlf replacement */
234 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
239 fprintf(vty
->of
, "%s", filtered
);
241 } else if (vty
->of_saved
) {
242 fprintf(vty
->of_saved
, "%s", filtered
);
243 fflush(vty
->of_saved
);
249 /* print without crlf replacement */
250 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
256 if (vty
->filter
&& filtered
)
257 XFREE(MTYPE_TMP
, filtered
);
259 /* If p is not different with buf, it is allocated buffer. */
261 XFREE(MTYPE_VTY_OUT_BUF
, p
);
266 static int vty_log_out(struct vty
*vty
, const char *level
,
267 const char *proto_str
, const char *msg
,
268 struct timestamp_control
*ctl
)
274 if (!ctl
->already_rendered
) {
275 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
277 ctl
->already_rendered
= 1;
279 if (ctl
->len
+ 1 >= sizeof(buf
))
281 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
286 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
289 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
290 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
293 if (((ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", msg
)) < 0)
294 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
300 if (write(vty
->wfd
, buf
, len
) < 0) {
301 if (ERRNO_IO_RETRY(errno
))
302 /* Kernel buffer is full, probably too much debugging
304 drop the data and ignore. */
306 /* Fatal I/O error. */
308 0; /* disable monitoring to avoid infinite recursion */
309 flog_err(EC_LIB_SOCKET
,
310 "%s: write failed to vty client fd %d, closing: %s",
311 __func__
, vty
->fd
, safe_strerror(errno
));
312 buffer_reset(vty
->obuf
);
313 buffer_reset(vty
->lbuf
);
314 /* cannot call vty_close, because a parent routine may still try
315 to access the vty struct */
316 vty
->status
= VTY_CLOSE
;
317 shutdown(vty
->fd
, SHUT_RDWR
);
323 /* Output current time to the vty. */
324 void vty_time_print(struct vty
*vty
, int cr
)
326 char buf
[QUAGGA_TIMESTAMP_LEN
];
328 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
329 zlog_info("quagga_timestamp error");
333 vty_out(vty
, "%s\n", buf
);
335 vty_out(vty
, "%s ", buf
);
340 /* Say hello to vty interface. */
341 void vty_hello(struct vty
*vty
)
347 f
= fopen(host
.motdfile
, "r");
349 while (fgets(buf
, sizeof(buf
), f
)) {
351 /* work backwards to ignore trailling isspace()
353 for (s
= buf
+ strlen(buf
);
354 (s
> buf
) && isspace((unsigned char)s
[-1]);
358 vty_out(vty
, "%s\n", buf
);
362 vty_out(vty
, "MOTD file not found\n");
363 } else if (host
.motd
)
364 vty_out(vty
, "%s", host
.motd
);
367 /* Put out prompt and wait input from user. */
368 static void vty_prompt(struct vty
*vty
)
370 if (vty
->type
== VTY_TERM
) {
371 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
375 /* Send WILL TELOPT_ECHO to remote server. */
376 static void vty_will_echo(struct vty
*vty
)
378 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
379 vty_out(vty
, "%s", cmd
);
382 /* Make suppress Go-Ahead telnet option. */
383 static void vty_will_suppress_go_ahead(struct vty
*vty
)
385 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
386 vty_out(vty
, "%s", cmd
);
389 /* Make don't use linemode over telnet. */
390 static void vty_dont_linemode(struct vty
*vty
)
392 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
393 vty_out(vty
, "%s", cmd
);
396 /* Use window size. */
397 static void vty_do_window_size(struct vty
*vty
)
399 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
400 vty_out(vty
, "%s", cmd
);
403 /* Authentication of vty */
404 static void vty_auth(struct vty
*vty
, char *buf
)
407 enum node_type next_node
= 0;
409 char *crypt(const char *, const char *);
414 passwd
= host
.password_encrypt
;
416 passwd
= host
.password
;
418 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
420 next_node
= VIEW_NODE
;
422 case AUTH_ENABLE_NODE
:
424 passwd
= host
.enable_encrypt
;
426 passwd
= host
.enable
;
427 next_node
= ENABLE_NODE
;
433 fail
= strcmp(crypt(buf
, passwd
), passwd
);
435 fail
= strcmp(buf
, passwd
);
441 vty
->node
= next_node
; /* Success ! */
444 if (vty
->fail
>= 3) {
445 if (vty
->node
== AUTH_NODE
) {
447 "%% Bad passwords, too many failures!\n");
448 vty
->status
= VTY_CLOSE
;
450 /* AUTH_ENABLE_NODE */
453 "%% Bad enable passwords, too many failures!\n");
454 vty
->status
= VTY_CLOSE
;
460 /* Command execution over the vty interface. */
461 static int vty_command(struct vty
*vty
, char *buf
)
464 const char *protocolname
;
470 * Log non empty command lines
475 /* Skip white spaces. */
476 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
479 if (cp
!= NULL
&& *cp
!= '\0') {
481 char vty_str
[VTY_BUFSIZ
];
482 char prompt_str
[VTY_BUFSIZ
];
484 /* format the base vty info */
485 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
487 for (i
= 0; i
< vector_active(vtyvec
); i
++)
488 if (vty
== vector_slot(vtyvec
, i
)) {
489 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
494 /* format the prompt */
495 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
498 /* now log the command */
499 zlog_notice("%s%s", prompt_str
, buf
);
502 #ifdef CONSUMED_TIME_CHECK
506 unsigned long realtime
, cputime
;
509 #endif /* CONSUMED_TIME_CHECK */
511 ret
= cmd_execute(vty
, buf
, NULL
, 0);
513 /* Get the name of the protocol if any */
514 protocolname
= frr_protoname
;
516 #ifdef CONSUMED_TIME_CHECK
518 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
519 > CONSUMED_TIME_CHECK
)
520 /* Warn about CPU hog that must be fixed. */
523 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
524 realtime
/ 1000, cputime
/ 1000, buf
);
526 #endif /* CONSUMED_TIME_CHECK */
528 if (ret
!= CMD_SUCCESS
)
531 if (vty
->type
== VTY_FILE
)
532 vty_out(vty
, "Warning...\n");
534 case CMD_ERR_AMBIGUOUS
:
535 vty_out(vty
, "%% Ambiguous command.\n");
537 case CMD_ERR_NO_MATCH
:
538 vty_out(vty
, "%% [%s] Unknown command: %s\n",
541 case CMD_ERR_INCOMPLETE
:
542 vty_out(vty
, "%% Command incomplete.\n");
549 static const char telnet_backward_char
= 0x08;
550 static const char telnet_space_char
= ' ';
552 /* Basic function to write buffer to vty. */
553 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
555 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
558 /* Should we do buffering here ? And make vty_flush (vty) ? */
559 buffer_put(vty
->obuf
, buf
, nbytes
);
562 /* Basic function to insert character into vty. */
563 static void vty_self_insert(struct vty
*vty
, char c
)
568 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
571 length
= vty
->length
- vty
->cp
;
572 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
573 vty
->buf
[vty
->cp
] = c
;
575 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
576 for (i
= 0; i
< length
; i
++)
577 vty_write(vty
, &telnet_backward_char
, 1);
582 vty
->buf
[vty
->length
] = '\0';
585 /* Self insert character 'c' in overwrite mode. */
586 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
588 if (vty
->cp
== vty
->length
) {
589 vty_self_insert(vty
, c
);
593 vty
->buf
[vty
->cp
++] = c
;
594 vty_write(vty
, &c
, 1);
598 * Insert a string into vty->buf at the current cursor position.
600 * If the resultant string would be larger than VTY_BUFSIZ it is
603 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
605 if (vty
->cp
== VTY_BUFSIZ
)
608 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
609 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
611 vty
->length
= MAX(vty
->cp
, vty
->length
);
612 vty
->buf
[vty
->length
] = '\0';
613 vty_write(vty
, str
, nwrite
);
616 /* Forward character. */
617 static void vty_forward_char(struct vty
*vty
)
619 if (vty
->cp
< vty
->length
) {
620 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
625 /* Backward character. */
626 static void vty_backward_char(struct vty
*vty
)
630 vty_write(vty
, &telnet_backward_char
, 1);
634 /* Move to the beginning of the line. */
635 static void vty_beginning_of_line(struct vty
*vty
)
638 vty_backward_char(vty
);
641 /* Move to the end of the line. */
642 static void vty_end_of_line(struct vty
*vty
)
644 while (vty
->cp
< vty
->length
)
645 vty_forward_char(vty
);
648 static void vty_kill_line_from_beginning(struct vty
*);
649 static void vty_redraw_line(struct vty
*);
651 /* Print command line history. This function is called from
652 vty_next_line and vty_previous_line. */
653 static void vty_history_print(struct vty
*vty
)
657 vty_kill_line_from_beginning(vty
);
659 /* Get previous line from history buffer */
660 length
= strlen(vty
->hist
[vty
->hp
]);
661 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
662 vty
->cp
= vty
->length
= length
;
663 vty
->buf
[vty
->length
] = '\0';
665 /* Redraw current line */
666 vty_redraw_line(vty
);
669 /* Show next command line history. */
670 static void vty_next_line(struct vty
*vty
)
674 if (vty
->hp
== vty
->hindex
)
677 /* Try is there history exist or not. */
679 if (try_index
== (VTY_MAXHIST
- 1))
684 /* If there is not history return. */
685 if (vty
->hist
[try_index
] == NULL
)
690 vty_history_print(vty
);
693 /* Show previous command line history. */
694 static void vty_previous_line(struct vty
*vty
)
700 try_index
= VTY_MAXHIST
- 1;
704 if (vty
->hist
[try_index
] == NULL
)
709 vty_history_print(vty
);
712 /* This function redraw all of the command line character. */
713 static void vty_redraw_line(struct vty
*vty
)
715 vty_write(vty
, vty
->buf
, vty
->length
);
716 vty
->cp
= vty
->length
;
720 static void vty_forward_word(struct vty
*vty
)
722 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
723 vty_forward_char(vty
);
725 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
726 vty_forward_char(vty
);
729 /* Backward word without skipping training space. */
730 static void vty_backward_pure_word(struct vty
*vty
)
732 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
733 vty_backward_char(vty
);
737 static void vty_backward_word(struct vty
*vty
)
739 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
740 vty_backward_char(vty
);
742 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
743 vty_backward_char(vty
);
746 /* When '^D' is typed at the beginning of the line we move to the down
748 static void vty_down_level(struct vty
*vty
)
756 /* When '^Z' is received from vty, move down to the enable mode. */
757 static void vty_end_config(struct vty
*vty
)
762 vty_config_exit(vty
);
763 vty
->node
= ENABLE_NODE
;
770 /* Delete a charcter at the current point. */
771 static void vty_delete_char(struct vty
*vty
)
776 if (vty
->length
== 0) {
781 if (vty
->cp
== vty
->length
)
782 return; /* completion need here? */
784 size
= vty
->length
- vty
->cp
;
787 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
788 vty
->buf
[vty
->length
] = '\0';
790 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
793 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
794 vty_write(vty
, &telnet_space_char
, 1);
796 for (i
= 0; i
< size
; i
++)
797 vty_write(vty
, &telnet_backward_char
, 1);
800 /* Delete a character before the point. */
801 static void vty_delete_backward_char(struct vty
*vty
)
806 vty_backward_char(vty
);
807 vty_delete_char(vty
);
810 /* Kill rest of line from current point. */
811 static void vty_kill_line(struct vty
*vty
)
816 size
= vty
->length
- vty
->cp
;
821 for (i
= 0; i
< size
; i
++)
822 vty_write(vty
, &telnet_space_char
, 1);
823 for (i
= 0; i
< size
; i
++)
824 vty_write(vty
, &telnet_backward_char
, 1);
826 memset(&vty
->buf
[vty
->cp
], 0, size
);
827 vty
->length
= vty
->cp
;
830 /* Kill line from the beginning. */
831 static void vty_kill_line_from_beginning(struct vty
*vty
)
833 vty_beginning_of_line(vty
);
837 /* Delete a word before the point. */
838 static void vty_forward_kill_word(struct vty
*vty
)
840 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
841 vty_delete_char(vty
);
842 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
843 vty_delete_char(vty
);
846 /* Delete a word before the point. */
847 static void vty_backward_kill_word(struct vty
*vty
)
849 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
850 vty_delete_backward_char(vty
);
851 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
852 vty_delete_backward_char(vty
);
855 /* Transpose chars before or at the point. */
856 static void vty_transpose_chars(struct vty
*vty
)
860 /* If length is short or point is near by the beginning of line then
862 if (vty
->length
< 2 || vty
->cp
< 1)
865 /* In case of point is located at the end of the line. */
866 if (vty
->cp
== vty
->length
) {
867 c1
= vty
->buf
[vty
->cp
- 1];
868 c2
= vty
->buf
[vty
->cp
- 2];
870 vty_backward_char(vty
);
871 vty_backward_char(vty
);
872 vty_self_insert_overwrite(vty
, c1
);
873 vty_self_insert_overwrite(vty
, c2
);
875 c1
= vty
->buf
[vty
->cp
];
876 c2
= vty
->buf
[vty
->cp
- 1];
878 vty_backward_char(vty
);
879 vty_self_insert_overwrite(vty
, c1
);
880 vty_self_insert_overwrite(vty
, c2
);
884 /* Do completion at vty interface. */
885 static void vty_complete_command(struct vty
*vty
)
889 char **matched
= NULL
;
892 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
895 vline
= cmd_make_strvec(vty
->buf
);
899 /* In case of 'help \t'. */
900 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
901 vector_set(vline
, NULL
);
903 matched
= cmd_complete_command(vline
, vty
, &ret
);
905 cmd_free_strvec(vline
);
909 case CMD_ERR_AMBIGUOUS
:
910 vty_out(vty
, "%% Ambiguous command.\n");
912 vty_redraw_line(vty
);
914 case CMD_ERR_NO_MATCH
:
915 /* vty_out (vty, "%% There is no matched command.\n"); */
917 vty_redraw_line(vty
);
919 case CMD_COMPLETE_FULL_MATCH
:
921 /* 2016-11-28 equinox -- need to debug, SEGV here */
922 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
924 vty_redraw_line(vty
);
928 vty_redraw_line(vty
);
929 vty_backward_pure_word(vty
);
930 vty_insert_word_overwrite(vty
, matched
[0]);
931 vty_self_insert(vty
, ' ');
932 XFREE(MTYPE_COMPLETION
, matched
[0]);
934 case CMD_COMPLETE_MATCH
:
936 vty_redraw_line(vty
);
937 vty_backward_pure_word(vty
);
938 vty_insert_word_overwrite(vty
, matched
[0]);
939 XFREE(MTYPE_COMPLETION
, matched
[0]);
941 case CMD_COMPLETE_LIST_MATCH
:
942 for (i
= 0; matched
[i
] != NULL
; i
++) {
943 if (i
!= 0 && ((i
% 6) == 0))
945 vty_out(vty
, "%-10s ", matched
[i
]);
946 XFREE(MTYPE_COMPLETION
, matched
[i
]);
951 vty_redraw_line(vty
);
953 case CMD_ERR_NOTHING_TODO
:
955 vty_redraw_line(vty
);
960 XFREE(MTYPE_TMP
, matched
);
963 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
964 unsigned int desc_width
, struct cmd_token
*token
)
972 if (desc_width
<= 0) {
973 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
977 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
979 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
980 for (pos
= desc_width
; pos
> 0; pos
--)
981 if (*(p
+ pos
) == ' ')
989 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
994 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
996 XFREE(MTYPE_TMP
, buf
);
999 /* Describe matched command function. */
1000 static void vty_describe_command(struct vty
*vty
)
1005 unsigned int i
, width
, desc_width
;
1006 struct cmd_token
*token
, *token_cr
= NULL
;
1008 vline
= cmd_make_strvec(vty
->buf
);
1010 /* In case of '> ?'. */
1011 if (vline
== NULL
) {
1012 vline
= vector_init(1);
1013 vector_set(vline
, NULL
);
1014 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1015 vector_set(vline
, NULL
);
1017 describe
= cmd_describe_command(vline
, vty
, &ret
);
1021 /* Ambiguous error. */
1023 case CMD_ERR_AMBIGUOUS
:
1024 vty_out(vty
, "%% Ambiguous command.\n");
1027 case CMD_ERR_NO_MATCH
:
1028 vty_out(vty
, "%% There is no matched command.\n");
1033 /* Get width of command string. */
1035 for (i
= 0; i
< vector_active(describe
); i
++)
1036 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1039 if (token
->text
[0] == '\0')
1042 len
= strlen(token
->text
);
1048 /* Get width of description string. */
1049 desc_width
= vty
->width
- (width
+ 6);
1051 /* Print out description. */
1052 for (i
= 0; i
< vector_active(describe
); i
++)
1053 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1054 if (token
->text
[0] == '\0')
1057 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1063 vty_out(vty
, " %-s\n", token
->text
);
1064 else if (desc_width
>= strlen(token
->desc
))
1065 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1068 vty_describe_fold(vty
, width
, desc_width
,
1071 if (IS_VARYING_TOKEN(token
->type
)) {
1072 const char *ref
= vector_slot(
1073 vline
, vector_active(vline
) - 1);
1075 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1076 cmd_variable_complete(token
, ref
, varcomps
);
1078 if (vector_active(varcomps
) > 0) {
1079 char *ac
= cmd_variable_comp2str(
1080 varcomps
, vty
->width
);
1081 vty_out(vty
, "%s\n", ac
);
1082 XFREE(MTYPE_TMP
, ac
);
1085 vector_free(varcomps
);
1089 if ((token
= token_cr
)) {
1091 vty_out(vty
, " %-s\n", token
->text
);
1092 else if (desc_width
>= strlen(token
->desc
))
1093 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1096 vty_describe_fold(vty
, width
, desc_width
, token
);
1100 cmd_free_strvec(vline
);
1102 vector_free(describe
);
1105 vty_redraw_line(vty
);
1108 static void vty_clear_buf(struct vty
*vty
)
1110 memset(vty
->buf
, 0, vty
->max
);
1113 /* ^C stop current input and do not add command line to the history. */
1114 static void vty_stop_input(struct vty
*vty
)
1116 vty
->cp
= vty
->length
= 0;
1121 vty_config_exit(vty
);
1122 vty
->node
= ENABLE_NODE
;
1127 /* Set history pointer to the latest one. */
1128 vty
->hp
= vty
->hindex
;
1131 /* Add current command line to the history buffer. */
1132 static void vty_hist_add(struct vty
*vty
)
1136 if (vty
->length
== 0)
1139 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1141 /* Ignore the same string as previous one. */
1142 if (vty
->hist
[index
])
1143 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1144 vty
->hp
= vty
->hindex
;
1148 /* Insert history entry. */
1149 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1150 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1152 /* History index rotation. */
1154 if (vty
->hindex
== VTY_MAXHIST
)
1157 vty
->hp
= vty
->hindex
;
1160 /* #define TELNET_OPTION_DEBUG */
1162 /* Get telnet window size. */
1163 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1165 #ifdef TELNET_OPTION_DEBUG
1168 for (i
= 0; i
< nbytes
; i
++) {
1171 vty_out(vty
, "IAC ");
1174 vty_out(vty
, "WILL ");
1177 vty_out(vty
, "WONT ");
1180 vty_out(vty
, "DO ");
1183 vty_out(vty
, "DONT ");
1186 vty_out(vty
, "SB ");
1189 vty_out(vty
, "SE ");
1192 vty_out(vty
, "TELOPT_ECHO \n");
1195 vty_out(vty
, "TELOPT_SGA \n");
1198 vty_out(vty
, "TELOPT_NAWS \n");
1201 vty_out(vty
, "%x ", buf
[i
]);
1207 #endif /* TELNET_OPTION_DEBUG */
1212 vty
->iac_sb_in_progress
= 1;
1215 if (!vty
->iac_sb_in_progress
)
1218 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1219 vty
->iac_sb_in_progress
= 0;
1222 switch (vty
->sb_buf
[0]) {
1224 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1227 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
1229 (unsigned long)vty
->sb_len
);
1230 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1233 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1234 (unsigned long)sizeof(vty
->sb_buf
),
1235 TELNET_NAWS_SB_LEN
);
1237 vty
->width
= ((vty
->sb_buf
[1] << 8)
1239 vty
->height
= ((vty
->sb_buf
[3] << 8)
1241 #ifdef TELNET_OPTION_DEBUG
1243 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
1244 vty
->width
, vty
->height
);
1249 vty
->iac_sb_in_progress
= 0;
1258 /* Execute current command line. */
1259 static int vty_execute(struct vty
*vty
)
1265 switch (vty
->node
) {
1267 case AUTH_ENABLE_NODE
:
1268 vty_auth(vty
, vty
->buf
);
1271 ret
= vty_command(vty
, vty
->buf
);
1272 if (vty
->type
== VTY_TERM
)
1277 /* Clear command line buffer. */
1278 vty
->cp
= vty
->length
= 0;
1281 if (vty
->status
!= VTY_CLOSE
)
1287 #define CONTROL(X) ((X) - '@')
1288 #define VTY_NORMAL 0
1289 #define VTY_PRE_ESCAPE 1
1290 #define VTY_ESCAPE 2
1293 /* Escape character command map. */
1294 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1298 vty_previous_line(vty
);
1304 vty_forward_char(vty
);
1307 vty_backward_char(vty
);
1313 /* Go back to normal mode. */
1314 vty
->escape
= VTY_NORMAL
;
1317 /* Quit print out to the buffer. */
1318 static void vty_buffer_reset(struct vty
*vty
)
1320 buffer_reset(vty
->obuf
);
1321 buffer_reset(vty
->lbuf
);
1323 vty_redraw_line(vty
);
1326 /* Read data via vty socket. */
1327 static int vty_read(struct thread
*thread
)
1331 unsigned char buf
[VTY_READ_BUFSIZ
];
1333 struct vty
*vty
= THREAD_ARG(thread
);
1335 /* Read raw data from socket */
1336 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1338 if (ERRNO_IO_RETRY(errno
)) {
1339 vty_event(VTY_READ
, vty
);
1342 vty
->monitor
= 0; /* disable monitoring to avoid
1343 infinite recursion */
1346 "%s: read error on vty client fd %d, closing: %s",
1347 __func__
, vty
->fd
, safe_strerror(errno
));
1348 buffer_reset(vty
->obuf
);
1349 buffer_reset(vty
->lbuf
);
1351 vty
->status
= VTY_CLOSE
;
1354 for (i
= 0; i
< nbytes
; i
++) {
1355 if (buf
[i
] == IAC
) {
1364 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1365 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1366 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1372 /* In case of telnet command */
1374 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1381 if (vty
->status
== VTY_MORE
) {
1386 vty_buffer_reset(vty
);
1394 /* Escape character. */
1395 if (vty
->escape
== VTY_ESCAPE
) {
1396 vty_escape_map(buf
[i
], vty
);
1400 /* Pre-escape status. */
1401 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1404 vty
->escape
= VTY_ESCAPE
;
1407 vty_backward_word(vty
);
1408 vty
->escape
= VTY_NORMAL
;
1411 vty_forward_word(vty
);
1412 vty
->escape
= VTY_NORMAL
;
1415 vty_forward_kill_word(vty
);
1416 vty
->escape
= VTY_NORMAL
;
1420 vty_backward_kill_word(vty
);
1421 vty
->escape
= VTY_NORMAL
;
1424 vty
->escape
= VTY_NORMAL
;
1430 if (vty
->escape
== VTY_CR
) {
1431 /* if we get CR+NL, the NL results in an extra empty
1432 * prompt line being printed without this; just drop
1433 * the NL if it immediately follows CR.
1435 vty
->escape
= VTY_NORMAL
;
1443 vty_beginning_of_line(vty
);
1446 vty_backward_char(vty
);
1449 vty_stop_input(vty
);
1452 vty_delete_char(vty
);
1455 vty_end_of_line(vty
);
1458 vty_forward_char(vty
);
1462 vty_delete_backward_char(vty
);
1471 vty_previous_line(vty
);
1474 vty_transpose_chars(vty
);
1477 vty_kill_line_from_beginning(vty
);
1480 vty_backward_kill_word(vty
);
1483 vty_end_config(vty
);
1486 vty
->escape
= VTY_CR
;
1490 buffer_flush_available(vty
->obuf
, vty
->wfd
);
1494 vty_complete_command(vty
);
1497 if (vty
->node
== AUTH_NODE
1498 || vty
->node
== AUTH_ENABLE_NODE
)
1499 vty_self_insert(vty
, buf
[i
]);
1501 vty_describe_command(vty
);
1504 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1505 vty
->escape
= VTY_ESCAPE
;
1508 vty
->escape
= VTY_PRE_ESCAPE
;
1511 if (buf
[i
] > 31 && buf
[i
] < 127)
1512 vty_self_insert(vty
, buf
[i
]);
1518 if (vty
->status
== VTY_CLOSE
)
1521 vty_event(VTY_WRITE
, vty
);
1522 vty_event(VTY_READ
, vty
);
1527 /* Flush buffer to the vty. */
1528 static int vty_flush(struct thread
*thread
)
1531 buffer_status_t flushrc
;
1532 struct vty
*vty
= THREAD_ARG(thread
);
1534 /* Tempolary disable read thread. */
1535 if (vty
->lines
== 0)
1536 THREAD_OFF(vty
->t_read
);
1538 /* Function execution continue. */
1539 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1541 /* N.B. if width is 0, that means we don't know the window size. */
1542 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1543 flushrc
= buffer_flush_available(vty
->obuf
, vty
->wfd
);
1544 else if (vty
->status
== VTY_MORELINE
)
1545 flushrc
= buffer_flush_window(vty
->obuf
, vty
->wfd
, vty
->width
,
1548 flushrc
= buffer_flush_window(
1549 vty
->obuf
, vty
->wfd
, vty
->width
,
1550 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1554 0; /* disable monitoring to avoid infinite recursion */
1555 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1557 buffer_reset(vty
->lbuf
);
1558 buffer_reset(vty
->obuf
);
1562 if (vty
->status
== VTY_CLOSE
)
1565 vty
->status
= VTY_NORMAL
;
1566 if (vty
->lines
== 0)
1567 vty_event(VTY_READ
, vty
);
1570 case BUFFER_PENDING
:
1571 /* There is more data waiting to be written. */
1572 vty
->status
= VTY_MORE
;
1573 if (vty
->lines
== 0)
1574 vty_event(VTY_WRITE
, vty
);
1581 /* Allocate new vty struct. */
1582 struct vty
*vty_new(void)
1584 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1586 new->fd
= new->wfd
= -1;
1588 new->lbuf
= buffer_new(0);
1589 new->obuf
= buffer_new(0); /* Use default buffer size. */
1590 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1591 new->max
= VTY_BUFSIZ
;
1597 /* allocate and initialise vty */
1598 static struct vty
*vty_new_init(int vty_sock
)
1604 vty
->wfd
= vty_sock
;
1605 vty
->type
= VTY_TERM
;
1606 vty
->node
= AUTH_NODE
;
1611 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1614 vty
->xpath_index
= 0;
1615 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1616 vty
->private_config
= false;
1617 vty
->candidate_config
= vty_shared_candidate_config
;
1618 vector_set_index(vtyvec
, vty_sock
, vty
);
1619 vty
->status
= VTY_NORMAL
;
1622 vty
->iac_sb_in_progress
= 0;
1628 /* Create new vty structure. */
1629 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1631 char buf
[SU_ADDRSTRLEN
];
1634 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1636 /* Allocate new vty structure and set up default values. */
1637 vty
= vty_new_init(vty_sock
);
1639 /* configurable parameters not part of basic init */
1640 vty
->v_timeout
= vty_timeout_val
;
1641 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1642 if (no_password_check
) {
1644 vty
->node
= ENABLE_NODE
;
1646 vty
->node
= VIEW_NODE
;
1648 if (host
.lines
>= 0)
1649 vty
->lines
= host
.lines
;
1651 if (!no_password_check
) {
1652 /* Vty is not available if password isn't set. */
1653 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1654 vty_out(vty
, "Vty password is not set.\n");
1655 vty
->status
= VTY_CLOSE
;
1661 /* Say hello to the world. */
1663 if (!no_password_check
)
1664 vty_out(vty
, "\nUser Access Verification\n\n");
1666 /* Setting up terminal. */
1668 vty_will_suppress_go_ahead(vty
);
1670 vty_dont_linemode(vty
);
1671 vty_do_window_size(vty
);
1672 /* vty_dont_lflow_ahead (vty); */
1676 /* Add read/write thread. */
1677 vty_event(VTY_WRITE
, vty
);
1678 vty_event(VTY_READ
, vty
);
1683 /* create vty for stdio */
1684 static struct termios stdio_orig_termios
;
1685 static struct vty
*stdio_vty
= NULL
;
1686 static bool stdio_termios
= false;
1687 static void (*stdio_vty_atclose
)(int isexit
);
1689 static void vty_stdio_reset(int isexit
)
1693 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1694 stdio_termios
= false;
1698 if (stdio_vty_atclose
)
1699 stdio_vty_atclose(isexit
);
1700 stdio_vty_atclose
= NULL
;
1704 static void vty_stdio_atexit(void)
1709 void vty_stdio_suspend(void)
1714 THREAD_OFF(stdio_vty
->t_write
);
1715 THREAD_OFF(stdio_vty
->t_read
);
1716 THREAD_OFF(stdio_vty
->t_timeout
);
1719 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1720 stdio_termios
= false;
1723 void vty_stdio_resume(void)
1728 if (!tcgetattr(0, &stdio_orig_termios
)) {
1729 struct termios termios
;
1731 termios
= stdio_orig_termios
;
1732 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1733 | IGNCR
| ICRNL
| IXON
);
1734 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1735 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1736 termios
.c_cflag
|= CS8
;
1737 tcsetattr(0, TCSANOW
, &termios
);
1738 stdio_termios
= true;
1741 vty_prompt(stdio_vty
);
1743 /* Add read/write thread. */
1744 vty_event(VTY_WRITE
, stdio_vty
);
1745 vty_event(VTY_READ
, stdio_vty
);
1748 void vty_stdio_close(void)
1752 vty_close(stdio_vty
);
1755 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1759 /* refuse creating two vtys on stdio */
1763 vty
= stdio_vty
= vty_new_init(0);
1764 stdio_vty_atclose
= atclose
;
1767 /* always have stdio vty in a known _unchangeable_ state, don't want
1769 * to have any effect here to make sure scripting this works as intended
1771 vty
->node
= ENABLE_NODE
;
1773 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1779 /* Accept connection from the network. */
1780 static int vty_accept(struct thread
*thread
)
1788 struct access_list
*acl
= NULL
;
1790 accept_sock
= THREAD_FD(thread
);
1792 /* We continue hearing vty socket. */
1793 vty_event_serv(VTY_SERV
, accept_sock
);
1795 memset(&su
, 0, sizeof(union sockunion
));
1797 /* We can handle IPv4 or IPv6 socket. */
1798 vty_sock
= sockunion_accept(accept_sock
, &su
);
1800 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1801 safe_strerror(errno
));
1804 set_nonblocking(vty_sock
);
1805 set_cloexec(vty_sock
);
1807 if (!sockunion2hostprefix(&su
, &p
)) {
1809 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1814 /* VTY's accesslist apply. */
1815 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1816 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1817 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1818 zlog_info("Vty connection refused from %pSU", &su
);
1821 /* continue accepting connections */
1822 vty_event_serv(VTY_SERV
, accept_sock
);
1828 /* VTY's ipv6 accesslist apply. */
1829 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1830 if ((acl
= access_list_lookup(AFI_IP6
,
1831 vty_ipv6_accesslist_name
))
1832 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1833 zlog_info("Vty connection refused from %pSU", &su
);
1836 /* continue accepting connections */
1837 vty_event_serv(VTY_SERV
, accept_sock
);
1844 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1847 zlog_info("can't set sockopt to vty_sock : %s",
1848 safe_strerror(errno
));
1850 zlog_info("Vty connection from %pSU", &su
);
1852 vty_create(vty_sock
, &su
);
1857 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1860 struct addrinfo req
;
1861 struct addrinfo
*ainfo
;
1862 struct addrinfo
*ainfo_save
;
1864 char port_str
[BUFSIZ
];
1866 memset(&req
, 0, sizeof(struct addrinfo
));
1867 req
.ai_flags
= AI_PASSIVE
;
1868 req
.ai_family
= AF_UNSPEC
;
1869 req
.ai_socktype
= SOCK_STREAM
;
1870 snprintf(port_str
, sizeof(port_str
), "%d", port
);
1871 port_str
[sizeof(port_str
) - 1] = '\0';
1873 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1876 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1884 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1887 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1888 ainfo
->ai_protocol
);
1892 sockopt_v6only(ainfo
->ai_family
, sock
);
1893 sockopt_reuseaddr(sock
);
1894 sockopt_reuseport(sock
);
1897 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1899 close(sock
); /* Avoid sd leak. */
1903 ret
= listen(sock
, 3);
1905 close(sock
); /* Avoid sd leak. */
1909 vty_event_serv(VTY_SERV
, sock
);
1910 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1912 freeaddrinfo(ainfo_save
);
1916 /* For sockaddr_un. */
1919 /* VTY shell UNIX domain socket. */
1920 static void vty_serv_un(const char *path
)
1924 struct sockaddr_un serv
;
1926 struct zprivs_ids_t ids
;
1928 /* First of all, unlink existing socket */
1932 old_mask
= umask(0007);
1934 /* Make UNIX domain socket. */
1935 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1937 flog_err_sys(EC_LIB_SOCKET
,
1938 "Cannot create unix stream socket: %s",
1939 safe_strerror(errno
));
1943 /* Make server socket. */
1944 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1945 serv
.sun_family
= AF_UNIX
;
1946 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1947 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1948 len
= serv
.sun_len
= SUN_LEN(&serv
);
1950 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1951 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1955 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1957 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1958 safe_strerror(errno
));
1959 close(sock
); /* Avoid sd leak. */
1963 ret
= listen(sock
, 5);
1965 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1966 safe_strerror(errno
));
1967 close(sock
); /* Avoid sd leak. */
1973 zprivs_get_ids(&ids
);
1975 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1976 earlier for the case when we don't need to chown the file
1977 type casting it here to make a compare */
1978 if ((int)ids
.gid_vty
> 0) {
1979 /* set group of socket */
1980 if (chown(path
, -1, ids
.gid_vty
)) {
1981 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1982 "vty_serv_un: could chown socket, %s",
1983 safe_strerror(errno
));
1987 vty_event_serv(VTYSH_SERV
, sock
);
1990 /* #define VTYSH_DEBUG 1 */
1992 static int vtysh_accept(struct thread
*thread
)
1997 struct sockaddr_un client
;
2000 accept_sock
= THREAD_FD(thread
);
2002 vty_event_serv(VTYSH_SERV
, accept_sock
);
2004 memset(&client
, 0, sizeof(struct sockaddr_un
));
2005 client_len
= sizeof(struct sockaddr_un
);
2007 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2008 (socklen_t
*)&client_len
);
2011 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2012 safe_strerror(errno
));
2016 if (set_nonblocking(sock
) < 0) {
2019 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2020 sock
, safe_strerror(errno
));
2027 printf("VTY shell accept\n");
2028 #endif /* VTYSH_DEBUG */
2033 vty
->type
= VTY_SHELL_SERV
;
2034 vty
->node
= VIEW_NODE
;
2036 vty_event(VTYSH_READ
, vty
);
2041 static int vtysh_flush(struct vty
*vty
)
2043 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2044 case BUFFER_PENDING
:
2045 vty_event(VTYSH_WRITE
, vty
);
2049 0; /* disable monitoring to avoid infinite recursion */
2050 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2052 buffer_reset(vty
->lbuf
);
2053 buffer_reset(vty
->obuf
);
2062 static int vtysh_read(struct thread
*thread
)
2068 unsigned char buf
[VTY_READ_BUFSIZ
];
2070 uint8_t header
[4] = {0, 0, 0, 0};
2072 sock
= THREAD_FD(thread
);
2073 vty
= THREAD_ARG(thread
);
2075 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2077 if (ERRNO_IO_RETRY(errno
)) {
2078 vty_event(VTYSH_READ
, vty
);
2081 vty
->monitor
= 0; /* disable monitoring to avoid
2082 infinite recursion */
2085 "%s: read failed on vtysh client fd %d, closing: %s",
2086 __func__
, sock
, safe_strerror(errno
));
2088 buffer_reset(vty
->lbuf
);
2089 buffer_reset(vty
->obuf
);
2092 printf("close vtysh\n");
2093 #endif /* VTYSH_DEBUG */
2098 printf("line: %.*s\n", nbytes
, buf
);
2099 #endif /* VTYSH_DEBUG */
2101 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2102 /* Clear command line buffer. */
2103 vty
->cp
= vty
->length
= 0;
2105 vty_out(vty
, "%% Command is too long.\n");
2107 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2108 vty
->buf
[vty
->length
++] = *p
;
2110 /* Pass this line to parser. */
2111 ret
= vty_execute(vty
);
2112 /* Note that vty_execute clears the command buffer and resets
2113 vty->length to 0. */
2115 /* Return result. */
2117 printf("result: %d\n", ret
);
2118 printf("vtysh node: %d\n", vty
->node
);
2119 #endif /* VTYSH_DEBUG */
2121 /* hack for asynchronous "write integrated"
2122 * - other commands in "buf" will be ditched
2123 * - input during pending config-write is
2125 if (ret
== CMD_SUSPEND
)
2128 /* warning: watchfrr hardcodes this result write
2131 buffer_put(vty
->obuf
, header
, 4);
2133 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2134 /* Try to flush results; exit if a write
2141 if (vty
->status
== VTY_CLOSE
)
2144 vty_event(VTYSH_READ
, vty
);
2149 static int vtysh_write(struct thread
*thread
)
2151 struct vty
*vty
= THREAD_ARG(thread
);
2159 /* Determine address family to bind. */
2160 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2162 /* If port is set to 0, do not listen on TCP/IP at all! */
2164 vty_serv_sock_addrinfo(addr
, port
);
2171 static void vty_error_delete(void *arg
)
2173 struct vty_error
*ve
= arg
;
2175 XFREE(MTYPE_TMP
, ve
);
2178 /* Close vty interface. Warning: call this only from functions that
2179 will be careful not to access the vty afterwards (since it has
2180 now been freed). This is safest from top-level functions (called
2181 directly by the thread dispatcher). */
2182 void vty_close(struct vty
*vty
)
2185 bool was_stdio
= false;
2187 /* Drop out of configure / transaction if needed. */
2188 vty_config_exit(vty
);
2190 /* Cancel threads.*/
2191 THREAD_OFF(vty
->t_read
);
2192 THREAD_OFF(vty
->t_write
);
2193 THREAD_OFF(vty
->t_timeout
);
2196 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2198 /* Free input buffer. */
2199 buffer_free(vty
->obuf
);
2200 buffer_free(vty
->lbuf
);
2202 /* Free command history. */
2203 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2204 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2209 vector_unset(vtyvec
, vty
->fd
);
2211 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2215 * note check is for fd > STDERR_FILENO, not fd != -1.
2216 * We never close stdin/stdout/stderr here, because we may be
2217 * running in foreground mode with logging to stdout. Also,
2218 * additionally, we'd need to replace these fds with /dev/null. */
2219 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2221 if (vty
->fd
> STDERR_FILENO
)
2223 if (vty
->fd
== STDIN_FILENO
)
2226 XFREE(MTYPE_VTY
, vty
->buf
);
2229 vty
->error
->del
= vty_error_delete
;
2230 list_delete(&vty
->error
);
2234 XFREE(MTYPE_VTY
, vty
);
2240 /* When time out occur output message then close connection. */
2241 static int vty_timeout(struct thread
*thread
)
2245 vty
= THREAD_ARG(thread
);
2249 buffer_reset(vty
->lbuf
);
2250 buffer_reset(vty
->obuf
);
2251 vty_out(vty
, "\nVty connection is timed out.\n");
2253 /* Close connection. */
2254 vty
->status
= VTY_CLOSE
;
2260 /* Read up configuration file from file_name. */
2261 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2265 struct vty_error
*ve
;
2266 struct listnode
*node
;
2267 unsigned int line_num
= 0;
2270 /* vty_close won't close stderr; if some config command prints
2271 * something it'll end up there. (not ideal; it'd be beter if output
2272 * from a file-load went to logging instead. Also note that if this
2273 * function is called after daemonizing, stderr will be /dev/null.)
2275 * vty->fd will be -1 from vty_new()
2277 vty
->wfd
= STDERR_FILENO
;
2278 vty
->type
= VTY_FILE
;
2279 vty
->node
= CONFIG_NODE
;
2282 vty
->candidate_config
= config
;
2284 vty
->private_config
= true;
2285 vty
->candidate_config
= nb_config_new(NULL
);
2288 /* Execute configuration file */
2289 ret
= config_from_file(vty
, confp
, &line_num
);
2291 /* Flush any previous errors before printing messages below */
2292 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2294 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2295 const char *message
= NULL
;
2299 case CMD_ERR_AMBIGUOUS
:
2300 message
= "Ambiguous command";
2302 case CMD_ERR_NO_MATCH
:
2303 message
= "No such command";
2306 message
= "Command returned Warning";
2308 case CMD_WARNING_CONFIG_FAILED
:
2309 message
= "Command returned Warning Config Failed";
2311 case CMD_ERR_INCOMPLETE
:
2312 message
= "Command returned Incomplete";
2314 case CMD_ERR_EXEED_ARGC_MAX
:
2316 "Command exceeded maximum number of Arguments";
2319 message
= "Command returned unhandled error message";
2323 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2324 nl
= strchr(ve
->error_buf
, '\n');
2327 flog_err(EC_LIB_VTY
, "%s on config line %u: %s",
2328 message
, ve
->line_num
, ve
->error_buf
);
2333 * Automatically commit the candidate configuration after
2334 * reading the configuration file.
2336 if (config
== NULL
) {
2337 struct nb_context context
= {};
2338 char errmsg
[BUFSIZ
] = {0};
2340 context
.client
= NB_CLIENT_CLI
;
2342 ret
= nb_candidate_commit(&context
, vty
->candidate_config
, true,
2343 "Read configuration file", NULL
,
2344 errmsg
, sizeof(errmsg
));
2345 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2347 "%s: failed to read configuration file: %s (%s)",
2348 __func__
, nb_err_name(ret
), errmsg
);
2354 static FILE *vty_use_backup_config(const char *fullpath
)
2356 char *fullpath_sav
, *fullpath_tmp
;
2362 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2363 fullpath_sav
= malloc(fullpath_sav_sz
);
2364 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2365 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2367 sav
= open(fullpath_sav
, O_RDONLY
);
2373 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2374 snprintf(fullpath_tmp
, strlen(fullpath
) + 8, "%s.XXXXXX", fullpath
);
2376 /* Open file to configuration write. */
2377 tmp
= mkstemp(fullpath_tmp
);
2381 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2384 while ((c
= read(sav
, buffer
, 512)) > 0) {
2385 if (write(tmp
, buffer
, c
) <= 0)
2391 if (rename(fullpath_tmp
, fullpath
) == 0)
2392 ret
= fopen(fullpath
, "r");
2394 unlink(fullpath_tmp
);
2399 unlink(fullpath_tmp
);
2409 /* Read up configuration file from file_name. */
2410 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2411 char *config_default_dir
)
2413 char cwd
[MAXPATHLEN
];
2415 const char *fullpath
;
2417 bool read_success
= false;
2419 /* If -f flag specified. */
2420 if (config_file
!= NULL
) {
2421 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2422 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2425 "%s: failure to determine Current Working Directory %d!",
2427 goto tmp_free_and_out
;
2429 size_t tmp_len
= strlen(cwd
) + strlen(config_file
) + 2;
2430 tmp
= XMALLOC(MTYPE_TMP
, tmp_len
);
2431 snprintf(tmp
, tmp_len
, "%s/%s", cwd
, config_file
);
2434 fullpath
= config_file
;
2436 confp
= fopen(fullpath
, "r");
2438 if (confp
== NULL
) {
2440 EC_LIB_BACKUP_CONFIG
,
2441 "%s: failed to open configuration file %s: %s, checking backup",
2442 __func__
, fullpath
, safe_strerror(errno
));
2444 confp
= vty_use_backup_config(fullpath
);
2446 flog_warn(EC_LIB_BACKUP_CONFIG
,
2447 "using backup configuration file!");
2451 "%s: can't open configuration file [%s]",
2452 __func__
, config_file
);
2453 goto tmp_free_and_out
;
2458 host_config_set(config_default_dir
);
2462 struct stat conf_stat
;
2464 /* !!!!PLEASE LEAVE!!!!
2465 * This is NEEDED for use with vtysh -b, or else you can get
2466 * a real configuration food fight with a lot garbage in the
2467 * merged configuration file it creates coming from the per
2468 * daemon configuration files. This also allows the daemons
2469 * to start if there default configuration file is not
2470 * present or ignore them, as needed when using vtysh -b to
2471 * configure the daemons at boot - MAG
2474 /* Stat for vtysh Zebra.conf, if found startup and wait for
2475 * boot configuration
2478 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2479 ret
= stat(integrate_default
, &conf_stat
);
2481 read_success
= true;
2482 goto tmp_free_and_out
;
2486 confp
= fopen(config_default_dir
, "r");
2487 if (confp
== NULL
) {
2490 "%s: failed to open configuration file %s: %s, checking backup",
2491 __func__
, config_default_dir
,
2492 safe_strerror(errno
));
2494 confp
= vty_use_backup_config(config_default_dir
);
2496 flog_warn(EC_LIB_BACKUP_CONFIG
,
2497 "using backup configuration file!");
2498 fullpath
= config_default_dir
;
2500 flog_err(EC_LIB_VTY
,
2501 "can't open configuration file [%s]",
2502 config_default_dir
);
2503 goto tmp_free_and_out
;
2506 fullpath
= config_default_dir
;
2509 vty_read_file(config
, confp
);
2510 read_success
= true;
2514 host_config_set(fullpath
);
2517 XFREE(MTYPE_TMP
, tmp
);
2519 return read_success
;
2522 /* Small utility function which output log to the VTY. */
2523 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2524 struct timestamp_control
*ctl
)
2532 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2533 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2535 vty_log_out(vty
, level
, proto_str
, msg
, ctl
);
2538 /* Async-signal-safe version of vty_log for fixed strings. */
2539 void vty_log_fixed(char *buf
, size_t len
)
2542 struct iovec iov
[2];
2543 char crlf
[4] = "\r\n";
2545 /* vty may not have been initialised */
2549 iov
[0].iov_base
= buf
;
2550 iov
[0].iov_len
= len
;
2551 iov
[1].iov_base
= crlf
;
2554 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2556 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2557 /* N.B. We don't care about the return code, since
2559 most likely just about to die anyway. */
2560 if (writev(vty
->wfd
, iov
, 2) == -1) {
2561 fprintf(stderr
, "Failure to writev: %d\n",
2568 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2570 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2571 vty_out(vty
, "%% Configuration is locked by other client\n");
2575 vty
->node
= CONFIG_NODE
;
2577 vty
->private_config
= private_config
;
2578 vty
->xpath_index
= 0;
2580 if (private_config
) {
2581 vty
->candidate_config
= nb_config_dup(running_config
);
2582 vty
->candidate_config_base
= nb_config_dup(running_config
);
2584 "Warning: uncommitted changes will be discarded on exit.\n\n");
2586 vty
->candidate_config
= vty_shared_candidate_config
;
2587 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2588 vty
->candidate_config_base
=
2589 nb_config_dup(running_config
);
2595 void vty_config_exit(struct vty
*vty
)
2597 enum node_type node
= vty
->node
;
2598 struct cmd_node
*cnode
;
2600 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2601 * somewhere below CONFIG_NODE */
2602 while (node
&& node
!= CONFIG_NODE
) {
2603 cnode
= vector_lookup(cmdvec
, node
);
2604 node
= cnode
->parent_node
;
2606 if (node
!= CONFIG_NODE
)
2607 /* called outside config, e.g. vty_close() in ENABLE_NODE */
2610 while (vty
->node
!= ENABLE_NODE
)
2611 /* will call vty_config_node_exit() below */
2615 int vty_config_node_exit(struct vty
*vty
)
2617 vty
->xpath_index
= 0;
2619 /* Perform pending commit if any. */
2620 nb_cli_pending_commit_check(vty
);
2622 /* Check if there's a pending confirmed commit. */
2623 if (vty
->t_confirmed_commit_timeout
) {
2625 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2626 nb_cli_confirmed_commit_rollback(vty
);
2627 nb_cli_confirmed_commit_clean(vty
);
2630 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2632 if (vty
->candidate_config
) {
2633 if (vty
->private_config
)
2634 nb_config_free(vty
->candidate_config
);
2635 vty
->candidate_config
= NULL
;
2637 if (vty
->candidate_config_base
) {
2638 nb_config_free(vty
->candidate_config_base
);
2639 vty
->candidate_config_base
= NULL
;
2642 vty
->config
= false;
2646 /* Master of the threads. */
2647 static struct thread_master
*vty_master
;
2649 static void vty_event_serv(enum event event
, int sock
)
2651 struct thread
*vty_serv_thread
= NULL
;
2655 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
,
2657 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2661 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
,
2663 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2667 assert(!"vty_event_serv() called incorrectly");
2671 static void vty_event(enum event event
, struct vty
*vty
)
2676 thread_add_read(vty_master
, vtysh_read
, vty
, vty
->fd
,
2680 thread_add_write(vty_master
, vtysh_write
, vty
, vty
->wfd
,
2685 thread_add_read(vty_master
, vty_read
, vty
, vty
->fd
,
2688 /* Time out treatment. */
2689 if (vty
->v_timeout
) {
2690 THREAD_OFF(vty
->t_timeout
);
2691 thread_add_timer(vty_master
, vty_timeout
, vty
,
2692 vty
->v_timeout
, &vty
->t_timeout
);
2696 thread_add_write(vty_master
, vty_flush
, vty
, vty
->wfd
,
2699 case VTY_TIMEOUT_RESET
:
2700 THREAD_OFF(vty
->t_timeout
);
2702 thread_add_timer(vty_master
, vty_timeout
, vty
,
2703 vty
->v_timeout
, &vty
->t_timeout
);
2706 assert(!"vty_event() called incorrectly");
2710 DEFUN_NOSH (config_who
,
2713 "Display who is on vty\n")
2718 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2719 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2720 vty_out(vty
, "%svty[%d] connected from %s.\n",
2721 v
->config
? "*" : " ", i
, v
->address
);
2725 /* Move to vty configuration mode. */
2726 DEFUN_NOSH (line_vty
,
2729 "Configure a terminal line\n"
2730 "Virtual terminal\n")
2732 vty
->node
= VTY_NODE
;
2736 /* Set time out value. */
2737 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2738 const char *sec_str
)
2740 unsigned long timeout
= 0;
2742 /* min_str and sec_str are already checked by parser. So it must be
2743 all digit string. */
2745 timeout
= strtol(min_str
, NULL
, 10);
2749 timeout
+= strtol(sec_str
, NULL
, 10);
2751 vty_timeout_val
= timeout
;
2752 vty
->v_timeout
= timeout
;
2753 vty_event(VTY_TIMEOUT_RESET
, vty
);
2759 DEFUN (exec_timeout_min
,
2760 exec_timeout_min_cmd
,
2761 "exec-timeout (0-35791)",
2762 "Set timeout value\n"
2763 "Timeout value in minutes\n")
2766 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2769 DEFUN (exec_timeout_sec
,
2770 exec_timeout_sec_cmd
,
2771 "exec-timeout (0-35791) (0-2147483)",
2772 "Set the EXEC timeout\n"
2773 "Timeout in minutes\n"
2774 "Timeout in seconds\n")
2777 int idx_number_2
= 2;
2778 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2779 argv
[idx_number_2
]->arg
);
2782 DEFUN (no_exec_timeout
,
2783 no_exec_timeout_cmd
,
2786 "Set the EXEC timeout\n")
2788 return exec_timeout(vty
, NULL
, NULL
);
2791 /* Set vty access class. */
2792 DEFUN (vty_access_class
,
2793 vty_access_class_cmd
,
2794 "access-class WORD",
2795 "Filter connections based on an IP access list\n"
2799 if (vty_accesslist_name
)
2800 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2802 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2807 /* Clear vty access class. */
2808 DEFUN (no_vty_access_class
,
2809 no_vty_access_class_cmd
,
2810 "no access-class [WORD]",
2812 "Filter connections based on an IP access list\n"
2816 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2817 if (!vty_accesslist_name
2818 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2819 vty_out(vty
, "Access-class is not currently applied to vty\n");
2820 return CMD_WARNING_CONFIG_FAILED
;
2823 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2825 vty_accesslist_name
= NULL
;
2830 /* Set vty access class. */
2831 DEFUN (vty_ipv6_access_class
,
2832 vty_ipv6_access_class_cmd
,
2833 "ipv6 access-class WORD",
2835 "Filter connections based on an IP access list\n"
2836 "IPv6 access list\n")
2839 if (vty_ipv6_accesslist_name
)
2840 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2842 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2847 /* Clear vty access class. */
2848 DEFUN (no_vty_ipv6_access_class
,
2849 no_vty_ipv6_access_class_cmd
,
2850 "no ipv6 access-class [WORD]",
2853 "Filter connections based on an IP access list\n"
2854 "IPv6 access list\n")
2857 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2859 if (!vty_ipv6_accesslist_name
2860 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2862 "IPv6 access-class is not currently applied to vty\n");
2863 return CMD_WARNING_CONFIG_FAILED
;
2866 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2868 vty_ipv6_accesslist_name
= NULL
;
2877 "Enable password checking\n")
2879 no_password_check
= 0;
2883 DEFUN (no_vty_login
,
2887 "Enable password checking\n")
2889 no_password_check
= 1;
2893 DEFUN (service_advanced_vty
,
2894 service_advanced_vty_cmd
,
2895 "service advanced-vty",
2896 "Set up miscellaneous service\n"
2897 "Enable advanced mode vty interface\n")
2903 DEFUN (no_service_advanced_vty
,
2904 no_service_advanced_vty_cmd
,
2905 "no service advanced-vty",
2907 "Set up miscellaneous service\n"
2908 "Enable advanced mode vty interface\n")
2914 DEFUN_NOSH (terminal_monitor
,
2915 terminal_monitor_cmd
,
2917 "Set terminal line parameters\n"
2918 "Copy debug output to the current terminal line\n")
2924 DEFUN_NOSH (terminal_no_monitor
,
2925 terminal_no_monitor_cmd
,
2926 "terminal no monitor",
2927 "Set terminal line parameters\n"
2929 "Copy debug output to the current terminal line\n")
2935 DEFUN_NOSH (no_terminal_monitor
,
2936 no_terminal_monitor_cmd
,
2937 "no terminal monitor",
2939 "Set terminal line parameters\n"
2940 "Copy debug output to the current terminal line\n")
2942 return terminal_no_monitor(self
, vty
, argc
, argv
);
2946 DEFUN_NOSH (show_history
,
2950 "Display the session command history\n")
2954 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2955 if (index
== VTY_MAXHIST
) {
2960 if (vty
->hist
[index
] != NULL
)
2961 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2970 DEFPY (log_commands
,
2972 "[no] log commands",
2975 "Log all commands\n")
2978 if (do_log_commands_perm
) {
2980 "Daemon started with permanent logging turned on for commands, ignoring\n");
2984 do_log_commands
= false;
2986 do_log_commands
= true;
2991 /* Display current configuration. */
2992 static int vty_config_write(struct vty
*vty
)
2994 vty_out(vty
, "line vty\n");
2996 if (vty_accesslist_name
)
2997 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2999 if (vty_ipv6_accesslist_name
)
3000 vty_out(vty
, " ipv6 access-class %s\n",
3001 vty_ipv6_accesslist_name
);
3004 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3005 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3006 vty_timeout_val
% 60);
3009 if (no_password_check
)
3010 vty_out(vty
, " no login\n");
3012 if (do_log_commands
)
3013 vty_out(vty
, "log commands\n");
3015 vty_out(vty
, "!\n");
3020 static int vty_config_write(struct vty
*vty
);
3021 struct cmd_node vty_node
= {
3024 .parent_node
= CONFIG_NODE
,
3025 .prompt
= "%s(config-line)# ",
3026 .config_write
= vty_config_write
,
3029 /* Reset all VTY status. */
3030 void vty_reset(void)
3034 struct thread
*vty_serv_thread
;
3036 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3037 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3038 buffer_reset(vty
->lbuf
);
3039 buffer_reset(vty
->obuf
);
3040 vty
->status
= VTY_CLOSE
;
3044 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3045 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3047 THREAD_OFF(vty_serv_thread
);
3048 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3052 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3054 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3055 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3058 static void vty_save_cwd(void)
3062 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3066 * At this point if these go wrong, more than likely
3067 * the whole world is coming down around us
3068 * Hence not worrying about it too much.
3070 if (!chdir(SYSCONFDIR
)) {
3071 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3072 "Failure to chdir to %s, errno: %d",
3076 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3077 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3078 "Failure to getcwd, errno: %d", errno
);
3084 char *vty_get_cwd(void)
3089 int vty_shell(struct vty
*vty
)
3091 return vty
->type
== VTY_SHELL
? 1 : 0;
3094 int vty_shell_serv(struct vty
*vty
)
3096 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3099 void vty_init_vtysh(void)
3101 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3104 /* Install vty's own commands like `who' command. */
3105 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3107 /* For further configuration read, preserve current directory. */
3110 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3112 vty_master
= master_thread
;
3114 atexit(vty_stdio_atexit
);
3116 /* Initilize server thread vector. */
3117 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3119 /* Install bgp top node. */
3120 install_node(&vty_node
);
3122 install_element(VIEW_NODE
, &config_who_cmd
);
3123 install_element(VIEW_NODE
, &show_history_cmd
);
3124 install_element(CONFIG_NODE
, &line_vty_cmd
);
3125 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3126 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3127 install_element(CONFIG_NODE
, &show_history_cmd
);
3128 install_element(CONFIG_NODE
, &log_commands_cmd
);
3130 if (do_command_logging
) {
3131 do_log_commands
= true;
3132 do_log_commands_perm
= true;
3135 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3136 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3137 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3139 install_default(VTY_NODE
);
3140 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3141 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3142 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3143 install_element(VTY_NODE
, &vty_access_class_cmd
);
3144 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3145 install_element(VTY_NODE
, &vty_login_cmd
);
3146 install_element(VTY_NODE
, &no_vty_login_cmd
);
3147 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3148 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3151 void vty_terminate(void)
3153 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3155 if (vtyvec
&& Vvty_serv_thread
) {
3157 vector_free(vtyvec
);
3158 vector_free(Vvty_serv_thread
);
3160 Vvty_serv_thread
= NULL
;