2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
25 #include <sys/types.h>
33 #include "sockunion.h"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
47 #include <arpa/telnet.h>
50 #ifndef VTYSH_EXTRACT_PL
51 #include "lib/vty_clippy.c"
54 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
55 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
56 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
71 static void vty_event(enum event
, int, struct vty
*);
73 /* Extern host structure from command.c */
74 extern struct host host
;
76 /* Vector which store each vty structure. */
79 /* Vty timeout value. */
80 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
82 /* Vty access-class command */
83 static char *vty_accesslist_name
= NULL
;
85 /* Vty access-calss for IPv6. */
86 static char *vty_ipv6_accesslist_name
= NULL
;
88 /* VTY server thread. */
89 static vector Vvty_serv_thread
;
91 /* Current directory. */
92 char vty_cwd
[MAXPATHLEN
];
94 /* Login password check. */
95 static int no_password_check
= 0;
97 /* Integrated configuration file path */
98 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
100 static bool do_log_commands
;
101 static bool do_log_commands_perm
;
103 void vty_frame(struct vty
*vty
, const char *format
, ...)
107 va_start(args
, format
);
108 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
109 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
110 vty
->frame_pos
= strlen(vty
->frame
);
114 void vty_endframe(struct vty
*vty
, const char *endtext
)
116 if (vty
->frame_pos
== 0 && endtext
)
117 vty_out(vty
, "%s", endtext
);
121 bool vty_set_include(struct vty
*vty
, const char *regexp
)
129 regfree(&vty
->include
);
135 errcode
= regcomp(&vty
->include
, regexp
,
136 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
139 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
140 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
148 /* VTY standard output function. */
149 int vty_out(struct vty
*vty
, const char *format
, ...)
157 if (vty
->frame_pos
) {
159 vty_out(vty
, "%s", vty
->frame
);
162 va_start(args
, format
);
163 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
170 vector lines
= frrstr_split_vec(p
, "\n");
172 /* Place first value in the cache */
173 char *firstline
= vector_slot(lines
, 0);
174 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
176 /* If our split returned more than one entry, time to filter */
177 if (vector_active(lines
) > 1) {
179 * returned string is MTYPE_TMP so it matches the MTYPE
180 * of everything else in the vector
182 char *bstr
= buffer_getstr(vty
->lbuf
);
183 buffer_reset(vty
->lbuf
);
184 XFREE(MTYPE_TMP
, lines
->index
[0]);
185 vector_set_index(lines
, 0, bstr
);
186 frrstr_filter_vec(lines
, &vty
->include
);
187 vector_compact(lines
);
189 * Consider the string "foo\n". If the regex is an empty string
190 * and the line ended with a newline, then the vector will look
196 * If the regex isn't empty, the vector will look like:
200 * In this case we'd like to preserve the newline, so we add
201 * the empty string [1] as in the first example.
203 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
204 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
205 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
207 filtered
= frrstr_join_vec(lines
, "\n");
213 frrstr_strvec_free(lines
);
224 /* print with crlf replacement */
225 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
229 fprintf(vty
->of
, "%s", filtered
);
235 /* print without crlf replacement */
236 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
242 if (vty
->filter
&& filtered
)
243 XFREE(MTYPE_TMP
, filtered
);
245 /* If p is not different with buf, it is allocated buffer. */
247 XFREE(MTYPE_VTY_OUT_BUF
, p
);
252 static int vty_log_out(struct vty
*vty
, const char *level
,
253 const char *proto_str
, const char *msg
,
254 struct timestamp_control
*ctl
)
260 if (!ctl
->already_rendered
) {
261 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
263 ctl
->already_rendered
= 1;
265 if (ctl
->len
+ 1 >= sizeof(buf
))
267 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
272 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
275 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
276 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
279 if (((ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", msg
)) < 0)
280 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
286 if (write(vty
->wfd
, buf
, len
) < 0) {
287 if (ERRNO_IO_RETRY(errno
))
288 /* Kernel buffer is full, probably too much debugging
290 drop the data and ignore. */
292 /* Fatal I/O error. */
294 0; /* disable monitoring to avoid infinite recursion */
295 flog_err(EC_LIB_SOCKET
,
296 "%s: write failed to vty client fd %d, closing: %s",
297 __func__
, vty
->fd
, safe_strerror(errno
));
298 buffer_reset(vty
->obuf
);
299 buffer_reset(vty
->lbuf
);
300 /* cannot call vty_close, because a parent routine may still try
301 to access the vty struct */
302 vty
->status
= VTY_CLOSE
;
303 shutdown(vty
->fd
, SHUT_RDWR
);
309 /* Output current time to the vty. */
310 void vty_time_print(struct vty
*vty
, int cr
)
312 char buf
[QUAGGA_TIMESTAMP_LEN
];
314 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
315 zlog_info("quagga_timestamp error");
319 vty_out(vty
, "%s\n", buf
);
321 vty_out(vty
, "%s ", buf
);
326 /* Say hello to vty interface. */
327 void vty_hello(struct vty
*vty
)
333 f
= fopen(host
.motdfile
, "r");
335 while (fgets(buf
, sizeof(buf
), f
)) {
337 /* work backwards to ignore trailling isspace()
339 for (s
= buf
+ strlen(buf
);
340 (s
> buf
) && isspace((unsigned char)s
[-1]);
344 vty_out(vty
, "%s\n", buf
);
348 vty_out(vty
, "MOTD file not found\n");
349 } else if (host
.motd
)
350 vty_out(vty
, "%s", host
.motd
);
352 #if CONFDATE > 20200901
353 CPP_NOTICE("Please remove solaris code from system as it is deprecated");
356 zlog_warn("If you are using FRR on Solaris, the FRR developers would love to hear from you\n");
357 zlog_warn("Please send email to dev@lists.frrouting.org about this message\n");
358 zlog_warn("We are considering deprecating Solaris and want to find users of Solaris systems\n");
362 /* Put out prompt and wait input from user. */
363 static void vty_prompt(struct vty
*vty
)
365 if (vty
->type
== VTY_TERM
) {
366 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
370 /* Send WILL TELOPT_ECHO to remote server. */
371 static void vty_will_echo(struct vty
*vty
)
373 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
374 vty_out(vty
, "%s", cmd
);
377 /* Make suppress Go-Ahead telnet option. */
378 static void vty_will_suppress_go_ahead(struct vty
*vty
)
380 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
381 vty_out(vty
, "%s", cmd
);
384 /* Make don't use linemode over telnet. */
385 static void vty_dont_linemode(struct vty
*vty
)
387 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
388 vty_out(vty
, "%s", cmd
);
391 /* Use window size. */
392 static void vty_do_window_size(struct vty
*vty
)
394 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
395 vty_out(vty
, "%s", cmd
);
398 #if 0 /* Currently not used. */
399 /* Make don't use lflow vty interface. */
401 vty_dont_lflow_ahead (struct vty
*vty
)
403 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
404 vty_out (vty
, "%s", cmd
);
408 /* Authentication of vty */
409 static void vty_auth(struct vty
*vty
, char *buf
)
412 enum node_type next_node
= 0;
414 char *crypt(const char *, const char *);
419 passwd
= host
.password_encrypt
;
421 passwd
= host
.password
;
423 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
425 next_node
= VIEW_NODE
;
427 case AUTH_ENABLE_NODE
:
429 passwd
= host
.enable_encrypt
;
431 passwd
= host
.enable
;
432 next_node
= ENABLE_NODE
;
438 fail
= strcmp(crypt(buf
, passwd
), passwd
);
440 fail
= strcmp(buf
, passwd
);
446 vty
->node
= next_node
; /* Success ! */
449 if (vty
->fail
>= 3) {
450 if (vty
->node
== AUTH_NODE
) {
452 "%% Bad passwords, too many failures!\n");
453 vty
->status
= VTY_CLOSE
;
455 /* AUTH_ENABLE_NODE */
458 "%% Bad enable passwords, too many failures!\n");
459 vty
->status
= VTY_CLOSE
;
465 /* Command execution over the vty interface. */
466 static int vty_command(struct vty
*vty
, char *buf
)
469 const char *protocolname
;
475 * Log non empty command lines
480 /* Skip white spaces. */
481 while (isspace((unsigned char)*cp
) && *cp
!= '\0')
484 if (cp
!= NULL
&& *cp
!= '\0') {
486 char vty_str
[VTY_BUFSIZ
];
487 char prompt_str
[VTY_BUFSIZ
];
489 /* format the base vty info */
490 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
492 for (i
= 0; i
< vector_active(vtyvec
); i
++)
493 if (vty
== vector_slot(vtyvec
, i
)) {
494 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
499 /* format the prompt */
500 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
503 /* now log the command */
504 zlog_notice("%s%s", prompt_str
, buf
);
507 #ifdef CONSUMED_TIME_CHECK
511 unsigned long realtime
, cputime
;
514 #endif /* CONSUMED_TIME_CHECK */
516 ret
= cmd_execute(vty
, buf
, NULL
, 0);
518 /* Get the name of the protocol if any */
519 protocolname
= frr_protoname
;
521 #ifdef CONSUMED_TIME_CHECK
523 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
524 > CONSUMED_TIME_CHECK
)
525 /* Warn about CPU hog that must be fixed. */
528 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
529 realtime
/ 1000, cputime
/ 1000, buf
);
531 #endif /* CONSUMED_TIME_CHECK */
533 if (ret
!= CMD_SUCCESS
)
536 if (vty
->type
== VTY_FILE
)
537 vty_out(vty
, "Warning...\n");
539 case CMD_ERR_AMBIGUOUS
:
540 vty_out(vty
, "%% Ambiguous command.\n");
542 case CMD_ERR_NO_MATCH
:
543 vty_out(vty
, "%% [%s] Unknown command: %s\n",
546 case CMD_ERR_INCOMPLETE
:
547 vty_out(vty
, "%% Command incomplete.\n");
554 static const char telnet_backward_char
= 0x08;
555 static const char telnet_space_char
= ' ';
557 /* Basic function to write buffer to vty. */
558 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
560 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
563 /* Should we do buffering here ? And make vty_flush (vty) ? */
564 buffer_put(vty
->obuf
, buf
, nbytes
);
567 /* Basic function to insert character into vty. */
568 static void vty_self_insert(struct vty
*vty
, char c
)
573 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
576 length
= vty
->length
- vty
->cp
;
577 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
578 vty
->buf
[vty
->cp
] = c
;
580 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
581 for (i
= 0; i
< length
; i
++)
582 vty_write(vty
, &telnet_backward_char
, 1);
587 vty
->buf
[vty
->length
] = '\0';
590 /* Self insert character 'c' in overwrite mode. */
591 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
593 if (vty
->cp
== vty
->length
) {
594 vty_self_insert(vty
, c
);
598 vty
->buf
[vty
->cp
++] = c
;
599 vty_write(vty
, &c
, 1);
603 * Insert a string into vty->buf at the current cursor position.
605 * If the resultant string would be larger than VTY_BUFSIZ it is
608 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
610 if (vty
->cp
== VTY_BUFSIZ
)
613 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
614 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
616 vty
->length
= MAX(vty
->cp
, vty
->length
);
617 vty
->buf
[vty
->length
] = '\0';
618 vty_write(vty
, str
, nwrite
);
621 /* Forward character. */
622 static void vty_forward_char(struct vty
*vty
)
624 if (vty
->cp
< vty
->length
) {
625 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
630 /* Backward character. */
631 static void vty_backward_char(struct vty
*vty
)
635 vty_write(vty
, &telnet_backward_char
, 1);
639 /* Move to the beginning of the line. */
640 static void vty_beginning_of_line(struct vty
*vty
)
643 vty_backward_char(vty
);
646 /* Move to the end of the line. */
647 static void vty_end_of_line(struct vty
*vty
)
649 while (vty
->cp
< vty
->length
)
650 vty_forward_char(vty
);
653 static void vty_kill_line_from_beginning(struct vty
*);
654 static void vty_redraw_line(struct vty
*);
656 /* Print command line history. This function is called from
657 vty_next_line and vty_previous_line. */
658 static void vty_history_print(struct vty
*vty
)
662 vty_kill_line_from_beginning(vty
);
664 /* Get previous line from history buffer */
665 length
= strlen(vty
->hist
[vty
->hp
]);
666 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
667 vty
->cp
= vty
->length
= length
;
668 vty
->buf
[vty
->length
] = '\0';
670 /* Redraw current line */
671 vty_redraw_line(vty
);
674 /* Show next command line history. */
675 static void vty_next_line(struct vty
*vty
)
679 if (vty
->hp
== vty
->hindex
)
682 /* Try is there history exist or not. */
684 if (try_index
== (VTY_MAXHIST
- 1))
689 /* If there is not history return. */
690 if (vty
->hist
[try_index
] == NULL
)
695 vty_history_print(vty
);
698 /* Show previous command line history. */
699 static void vty_previous_line(struct vty
*vty
)
705 try_index
= VTY_MAXHIST
- 1;
709 if (vty
->hist
[try_index
] == NULL
)
714 vty_history_print(vty
);
717 /* This function redraw all of the command line character. */
718 static void vty_redraw_line(struct vty
*vty
)
720 vty_write(vty
, vty
->buf
, vty
->length
);
721 vty
->cp
= vty
->length
;
725 static void vty_forward_word(struct vty
*vty
)
727 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
728 vty_forward_char(vty
);
730 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
731 vty_forward_char(vty
);
734 /* Backward word without skipping training space. */
735 static void vty_backward_pure_word(struct vty
*vty
)
737 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
738 vty_backward_char(vty
);
742 static void vty_backward_word(struct vty
*vty
)
744 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
745 vty_backward_char(vty
);
747 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
748 vty_backward_char(vty
);
751 /* When '^D' is typed at the beginning of the line we move to the down
753 static void vty_down_level(struct vty
*vty
)
761 /* When '^Z' is received from vty, move down to the enable mode. */
762 static void vty_end_config(struct vty
*vty
)
767 vty_config_exit(vty
);
768 vty
->node
= ENABLE_NODE
;
775 /* Delete a charcter at the current point. */
776 static void vty_delete_char(struct vty
*vty
)
781 if (vty
->length
== 0) {
786 if (vty
->cp
== vty
->length
)
787 return; /* completion need here? */
789 size
= vty
->length
- vty
->cp
;
792 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
793 vty
->buf
[vty
->length
] = '\0';
795 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
798 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
799 vty_write(vty
, &telnet_space_char
, 1);
801 for (i
= 0; i
< size
; i
++)
802 vty_write(vty
, &telnet_backward_char
, 1);
805 /* Delete a character before the point. */
806 static void vty_delete_backward_char(struct vty
*vty
)
811 vty_backward_char(vty
);
812 vty_delete_char(vty
);
815 /* Kill rest of line from current point. */
816 static void vty_kill_line(struct vty
*vty
)
821 size
= vty
->length
- vty
->cp
;
826 for (i
= 0; i
< size
; i
++)
827 vty_write(vty
, &telnet_space_char
, 1);
828 for (i
= 0; i
< size
; i
++)
829 vty_write(vty
, &telnet_backward_char
, 1);
831 memset(&vty
->buf
[vty
->cp
], 0, size
);
832 vty
->length
= vty
->cp
;
835 /* Kill line from the beginning. */
836 static void vty_kill_line_from_beginning(struct vty
*vty
)
838 vty_beginning_of_line(vty
);
842 /* Delete a word before the point. */
843 static void vty_forward_kill_word(struct vty
*vty
)
845 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
846 vty_delete_char(vty
);
847 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
848 vty_delete_char(vty
);
851 /* Delete a word before the point. */
852 static void vty_backward_kill_word(struct vty
*vty
)
854 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
855 vty_delete_backward_char(vty
);
856 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
857 vty_delete_backward_char(vty
);
860 /* Transpose chars before or at the point. */
861 static void vty_transpose_chars(struct vty
*vty
)
865 /* If length is short or point is near by the beginning of line then
867 if (vty
->length
< 2 || vty
->cp
< 1)
870 /* In case of point is located at the end of the line. */
871 if (vty
->cp
== vty
->length
) {
872 c1
= vty
->buf
[vty
->cp
- 1];
873 c2
= vty
->buf
[vty
->cp
- 2];
875 vty_backward_char(vty
);
876 vty_backward_char(vty
);
877 vty_self_insert_overwrite(vty
, c1
);
878 vty_self_insert_overwrite(vty
, c2
);
880 c1
= vty
->buf
[vty
->cp
];
881 c2
= vty
->buf
[vty
->cp
- 1];
883 vty_backward_char(vty
);
884 vty_self_insert_overwrite(vty
, c1
);
885 vty_self_insert_overwrite(vty
, c2
);
889 /* Do completion at vty interface. */
890 static void vty_complete_command(struct vty
*vty
)
894 char **matched
= NULL
;
897 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
900 vline
= cmd_make_strvec(vty
->buf
);
904 /* In case of 'help \t'. */
905 if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
906 vector_set(vline
, NULL
);
908 matched
= cmd_complete_command(vline
, vty
, &ret
);
910 cmd_free_strvec(vline
);
914 case CMD_ERR_AMBIGUOUS
:
915 vty_out(vty
, "%% Ambiguous command.\n");
917 vty_redraw_line(vty
);
919 case CMD_ERR_NO_MATCH
:
920 /* vty_out (vty, "%% There is no matched command.\n"); */
922 vty_redraw_line(vty
);
924 case CMD_COMPLETE_FULL_MATCH
:
926 /* 2016-11-28 equinox -- need to debug, SEGV here */
927 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
929 vty_redraw_line(vty
);
933 vty_redraw_line(vty
);
934 vty_backward_pure_word(vty
);
935 vty_insert_word_overwrite(vty
, matched
[0]);
936 vty_self_insert(vty
, ' ');
937 XFREE(MTYPE_COMPLETION
, matched
[0]);
939 case CMD_COMPLETE_MATCH
:
941 vty_redraw_line(vty
);
942 vty_backward_pure_word(vty
);
943 vty_insert_word_overwrite(vty
, matched
[0]);
944 XFREE(MTYPE_COMPLETION
, matched
[0]);
946 case CMD_COMPLETE_LIST_MATCH
:
947 for (i
= 0; matched
[i
] != NULL
; i
++) {
948 if (i
!= 0 && ((i
% 6) == 0))
950 vty_out(vty
, "%-10s ", matched
[i
]);
951 XFREE(MTYPE_COMPLETION
, matched
[i
]);
956 vty_redraw_line(vty
);
958 case CMD_ERR_NOTHING_TODO
:
960 vty_redraw_line(vty
);
965 XFREE(MTYPE_TMP
, matched
);
968 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
969 unsigned int desc_width
, struct cmd_token
*token
)
977 if (desc_width
<= 0) {
978 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
982 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
984 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
985 for (pos
= desc_width
; pos
> 0; pos
--)
986 if (*(p
+ pos
) == ' ')
994 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
999 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1001 XFREE(MTYPE_TMP
, buf
);
1004 /* Describe matched command function. */
1005 static void vty_describe_command(struct vty
*vty
)
1010 unsigned int i
, width
, desc_width
;
1011 struct cmd_token
*token
, *token_cr
= NULL
;
1013 vline
= cmd_make_strvec(vty
->buf
);
1015 /* In case of '> ?'. */
1016 if (vline
== NULL
) {
1017 vline
= vector_init(1);
1018 vector_set(vline
, NULL
);
1019 } else if (isspace((unsigned char)vty
->buf
[vty
->length
- 1]))
1020 vector_set(vline
, NULL
);
1022 describe
= cmd_describe_command(vline
, vty
, &ret
);
1026 /* Ambiguous error. */
1028 case CMD_ERR_AMBIGUOUS
:
1029 vty_out(vty
, "%% Ambiguous command.\n");
1032 case CMD_ERR_NO_MATCH
:
1033 vty_out(vty
, "%% There is no matched command.\n");
1038 /* Get width of command string. */
1040 for (i
= 0; i
< vector_active(describe
); i
++)
1041 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1044 if (token
->text
[0] == '\0')
1047 len
= strlen(token
->text
);
1053 /* Get width of description string. */
1054 desc_width
= vty
->width
- (width
+ 6);
1056 /* Print out description. */
1057 for (i
= 0; i
< vector_active(describe
); i
++)
1058 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1059 if (token
->text
[0] == '\0')
1062 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1068 vty_out(vty
, " %-s\n", token
->text
);
1069 else if (desc_width
>= strlen(token
->desc
))
1070 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1073 vty_describe_fold(vty
, width
, desc_width
,
1076 if (IS_VARYING_TOKEN(token
->type
)) {
1077 const char *ref
= vector_slot(
1078 vline
, vector_active(vline
) - 1);
1080 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1081 cmd_variable_complete(token
, ref
, varcomps
);
1083 if (vector_active(varcomps
) > 0) {
1084 char *ac
= cmd_variable_comp2str(
1085 varcomps
, vty
->width
);
1086 vty_out(vty
, "%s\n", ac
);
1087 XFREE(MTYPE_TMP
, ac
);
1090 vector_free(varcomps
);
1093 vty_out (vty
, " %-*s %s\n", width
1094 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1095 desc
->str
? desc
->str
: "");
1099 if ((token
= token_cr
)) {
1101 vty_out(vty
, " %-s\n", token
->text
);
1102 else if (desc_width
>= strlen(token
->desc
))
1103 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1106 vty_describe_fold(vty
, width
, desc_width
, token
);
1110 cmd_free_strvec(vline
);
1112 vector_free(describe
);
1115 vty_redraw_line(vty
);
1118 static void vty_clear_buf(struct vty
*vty
)
1120 memset(vty
->buf
, 0, vty
->max
);
1123 /* ^C stop current input and do not add command line to the history. */
1124 static void vty_stop_input(struct vty
*vty
)
1126 vty
->cp
= vty
->length
= 0;
1131 vty_config_exit(vty
);
1132 vty
->node
= ENABLE_NODE
;
1137 /* Set history pointer to the latest one. */
1138 vty
->hp
= vty
->hindex
;
1141 /* Add current command line to the history buffer. */
1142 static void vty_hist_add(struct vty
*vty
)
1146 if (vty
->length
== 0)
1149 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1151 /* Ignore the same string as previous one. */
1152 if (vty
->hist
[index
])
1153 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1154 vty
->hp
= vty
->hindex
;
1158 /* Insert history entry. */
1159 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1160 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1162 /* History index rotation. */
1164 if (vty
->hindex
== VTY_MAXHIST
)
1167 vty
->hp
= vty
->hindex
;
1170 /* #define TELNET_OPTION_DEBUG */
1172 /* Get telnet window size. */
1173 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1175 #ifdef TELNET_OPTION_DEBUG
1178 for (i
= 0; i
< nbytes
; i
++) {
1181 vty_out(vty
, "IAC ");
1184 vty_out(vty
, "WILL ");
1187 vty_out(vty
, "WONT ");
1190 vty_out(vty
, "DO ");
1193 vty_out(vty
, "DONT ");
1196 vty_out(vty
, "SB ");
1199 vty_out(vty
, "SE ");
1202 vty_out(vty
, "TELOPT_ECHO \n");
1205 vty_out(vty
, "TELOPT_SGA \n");
1208 vty_out(vty
, "TELOPT_NAWS \n");
1211 vty_out(vty
, "%x ", buf
[i
]);
1217 #endif /* TELNET_OPTION_DEBUG */
1222 vty
->iac_sb_in_progress
= 1;
1226 if (!vty
->iac_sb_in_progress
)
1229 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1230 vty
->iac_sb_in_progress
= 0;
1233 switch (vty
->sb_buf
[0]) {
1235 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1238 "RFC 1073 violation detected: telnet NAWS option "
1239 "should send %d characters, but we received %lu",
1241 (unsigned long)vty
->sb_len
);
1242 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1245 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1246 (unsigned long)sizeof(vty
->sb_buf
),
1247 TELNET_NAWS_SB_LEN
);
1249 vty
->width
= ((vty
->sb_buf
[1] << 8)
1251 vty
->height
= ((vty
->sb_buf
[3] << 8)
1253 #ifdef TELNET_OPTION_DEBUG
1255 "TELNET NAWS window size negotiation completed: "
1256 "width %d, height %d\n",
1257 vty
->width
, vty
->height
);
1262 vty
->iac_sb_in_progress
= 0;
1272 /* Execute current command line. */
1273 static int vty_execute(struct vty
*vty
)
1279 switch (vty
->node
) {
1281 case AUTH_ENABLE_NODE
:
1282 vty_auth(vty
, vty
->buf
);
1285 ret
= vty_command(vty
, vty
->buf
);
1286 if (vty
->type
== VTY_TERM
)
1291 /* Clear command line buffer. */
1292 vty
->cp
= vty
->length
= 0;
1295 if (vty
->status
!= VTY_CLOSE
)
1301 #define CONTROL(X) ((X) - '@')
1302 #define VTY_NORMAL 0
1303 #define VTY_PRE_ESCAPE 1
1304 #define VTY_ESCAPE 2
1306 /* Escape character command map. */
1307 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1311 vty_previous_line(vty
);
1317 vty_forward_char(vty
);
1320 vty_backward_char(vty
);
1326 /* Go back to normal mode. */
1327 vty
->escape
= VTY_NORMAL
;
1330 /* Quit print out to the buffer. */
1331 static void vty_buffer_reset(struct vty
*vty
)
1333 buffer_reset(vty
->obuf
);
1334 buffer_reset(vty
->lbuf
);
1336 vty_redraw_line(vty
);
1339 /* Read data via vty socket. */
1340 static int vty_read(struct thread
*thread
)
1344 unsigned char buf
[VTY_READ_BUFSIZ
];
1346 int vty_sock
= THREAD_FD(thread
);
1347 struct vty
*vty
= THREAD_ARG(thread
);
1349 /* Read raw data from socket */
1350 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1352 if (ERRNO_IO_RETRY(errno
)) {
1353 vty_event(VTY_READ
, vty_sock
, vty
);
1356 vty
->monitor
= 0; /* disable monitoring to avoid
1357 infinite recursion */
1360 "%s: read error on vty client fd %d, closing: %s",
1361 __func__
, vty
->fd
, safe_strerror(errno
));
1362 buffer_reset(vty
->obuf
);
1363 buffer_reset(vty
->lbuf
);
1365 vty
->status
= VTY_CLOSE
;
1368 for (i
= 0; i
< nbytes
; i
++) {
1369 if (buf
[i
] == IAC
) {
1378 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1379 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1380 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1386 /* In case of telnet command */
1388 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1395 if (vty
->status
== VTY_MORE
) {
1400 vty_buffer_reset(vty
);
1402 #if 0 /* More line does not work for "show ip bgp". */
1405 vty
->status
= VTY_MORELINE
;
1414 /* Escape character. */
1415 if (vty
->escape
== VTY_ESCAPE
) {
1416 vty_escape_map(buf
[i
], vty
);
1420 /* Pre-escape status. */
1421 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1424 vty
->escape
= VTY_ESCAPE
;
1427 vty_backward_word(vty
);
1428 vty
->escape
= VTY_NORMAL
;
1431 vty_forward_word(vty
);
1432 vty
->escape
= VTY_NORMAL
;
1435 vty_forward_kill_word(vty
);
1436 vty
->escape
= VTY_NORMAL
;
1440 vty_backward_kill_word(vty
);
1441 vty
->escape
= VTY_NORMAL
;
1444 vty
->escape
= VTY_NORMAL
;
1452 vty_beginning_of_line(vty
);
1455 vty_backward_char(vty
);
1458 vty_stop_input(vty
);
1461 vty_delete_char(vty
);
1464 vty_end_of_line(vty
);
1467 vty_forward_char(vty
);
1471 vty_delete_backward_char(vty
);
1480 vty_previous_line(vty
);
1483 vty_transpose_chars(vty
);
1486 vty_kill_line_from_beginning(vty
);
1489 vty_backward_kill_word(vty
);
1492 vty_end_config(vty
);
1500 vty_complete_command(vty
);
1503 if (vty
->node
== AUTH_NODE
1504 || vty
->node
== AUTH_ENABLE_NODE
)
1505 vty_self_insert(vty
, buf
[i
]);
1507 vty_describe_command(vty
);
1510 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1511 vty
->escape
= VTY_ESCAPE
;
1514 vty
->escape
= VTY_PRE_ESCAPE
;
1517 if (buf
[i
] > 31 && buf
[i
] < 127)
1518 vty_self_insert(vty
, buf
[i
]);
1524 if (vty
->status
== VTY_CLOSE
)
1527 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1528 vty_event(VTY_READ
, vty_sock
, vty
);
1533 /* Flush buffer to the vty. */
1534 static int vty_flush(struct thread
*thread
)
1537 buffer_status_t flushrc
;
1538 int vty_sock
= THREAD_FD(thread
);
1539 struct vty
*vty
= THREAD_ARG(thread
);
1541 /* Tempolary disable read thread. */
1542 if (vty
->lines
== 0)
1543 THREAD_OFF(vty
->t_read
);
1545 /* Function execution continue. */
1546 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1548 /* N.B. if width is 0, that means we don't know the window size. */
1549 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1550 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1551 else if (vty
->status
== VTY_MORELINE
)
1552 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1555 flushrc
= buffer_flush_window(
1556 vty
->obuf
, vty_sock
, vty
->width
,
1557 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1561 0; /* disable monitoring to avoid infinite recursion */
1562 zlog_info("buffer_flush failed on vty client fd %d, closing",
1564 buffer_reset(vty
->lbuf
);
1565 buffer_reset(vty
->obuf
);
1569 if (vty
->status
== VTY_CLOSE
)
1572 vty
->status
= VTY_NORMAL
;
1573 if (vty
->lines
== 0)
1574 vty_event(VTY_READ
, vty_sock
, vty
);
1577 case BUFFER_PENDING
:
1578 /* There is more data waiting to be written. */
1579 vty
->status
= VTY_MORE
;
1580 if (vty
->lines
== 0)
1581 vty_event(VTY_WRITE
, vty_sock
, vty
);
1588 /* Allocate new vty struct. */
1589 struct vty
*vty_new(void)
1591 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1593 new->fd
= new->wfd
= -1;
1595 new->lbuf
= buffer_new(0);
1596 new->obuf
= buffer_new(0); /* Use default buffer size. */
1597 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1598 new->max
= VTY_BUFSIZ
;
1604 /* allocate and initialise vty */
1605 static struct vty
*vty_new_init(int vty_sock
)
1611 vty
->wfd
= vty_sock
;
1612 vty
->type
= VTY_TERM
;
1613 vty
->node
= AUTH_NODE
;
1618 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1621 vty
->xpath_index
= 0;
1622 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1623 vty
->private_config
= false;
1624 vty
->candidate_config
= vty_shared_candidate_config
;
1625 vector_set_index(vtyvec
, vty_sock
, vty
);
1626 vty
->status
= VTY_NORMAL
;
1629 vty
->iac_sb_in_progress
= 0;
1635 /* Create new vty structure. */
1636 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1638 char buf
[SU_ADDRSTRLEN
];
1641 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1643 /* Allocate new vty structure and set up default values. */
1644 vty
= vty_new_init(vty_sock
);
1646 /* configurable parameters not part of basic init */
1647 vty
->v_timeout
= vty_timeout_val
;
1648 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1649 if (no_password_check
) {
1651 vty
->node
= ENABLE_NODE
;
1653 vty
->node
= VIEW_NODE
;
1655 if (host
.lines
>= 0)
1656 vty
->lines
= host
.lines
;
1658 if (!no_password_check
) {
1659 /* Vty is not available if password isn't set. */
1660 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1661 vty_out(vty
, "Vty password is not set.\n");
1662 vty
->status
= VTY_CLOSE
;
1668 /* Say hello to the world. */
1670 if (!no_password_check
)
1671 vty_out(vty
, "\nUser Access Verification\n\n");
1673 /* Setting up terminal. */
1675 vty_will_suppress_go_ahead(vty
);
1677 vty_dont_linemode(vty
);
1678 vty_do_window_size(vty
);
1679 /* vty_dont_lflow_ahead (vty); */
1683 /* Add read/write thread. */
1684 vty_event(VTY_WRITE
, vty_sock
, vty
);
1685 vty_event(VTY_READ
, vty_sock
, vty
);
1690 /* create vty for stdio */
1691 static struct termios stdio_orig_termios
;
1692 static struct vty
*stdio_vty
= NULL
;
1693 static bool stdio_termios
= false;
1694 static void (*stdio_vty_atclose
)(int isexit
);
1696 static void vty_stdio_reset(int isexit
)
1700 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1701 stdio_termios
= false;
1705 if (stdio_vty_atclose
)
1706 stdio_vty_atclose(isexit
);
1707 stdio_vty_atclose
= NULL
;
1711 static void vty_stdio_atexit(void)
1716 void vty_stdio_suspend(void)
1721 THREAD_OFF(stdio_vty
->t_write
);
1722 THREAD_OFF(stdio_vty
->t_read
);
1723 THREAD_OFF(stdio_vty
->t_timeout
);
1726 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1727 stdio_termios
= false;
1730 void vty_stdio_resume(void)
1735 if (!tcgetattr(0, &stdio_orig_termios
)) {
1736 struct termios termios
;
1738 termios
= stdio_orig_termios
;
1739 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1740 | IGNCR
| ICRNL
| IXON
);
1741 termios
.c_oflag
&= ~OPOST
;
1742 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1743 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1744 termios
.c_cflag
|= CS8
;
1745 tcsetattr(0, TCSANOW
, &termios
);
1746 stdio_termios
= true;
1749 vty_prompt(stdio_vty
);
1751 /* Add read/write thread. */
1752 vty_event(VTY_WRITE
, 1, stdio_vty
);
1753 vty_event(VTY_READ
, 0, stdio_vty
);
1756 void vty_stdio_close(void)
1760 vty_close(stdio_vty
);
1763 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1767 /* refuse creating two vtys on stdio */
1771 vty
= stdio_vty
= vty_new_init(0);
1772 stdio_vty_atclose
= atclose
;
1775 /* always have stdio vty in a known _unchangeable_ state, don't want
1777 * to have any effect here to make sure scripting this works as intended
1779 vty
->node
= ENABLE_NODE
;
1781 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1787 /* Accept connection from the network. */
1788 static int vty_accept(struct thread
*thread
)
1796 struct access_list
*acl
= NULL
;
1797 char buf
[SU_ADDRSTRLEN
];
1799 accept_sock
= THREAD_FD(thread
);
1801 /* We continue hearing vty socket. */
1802 vty_event(VTY_SERV
, accept_sock
, NULL
);
1804 memset(&su
, 0, sizeof(union sockunion
));
1806 /* We can handle IPv4 or IPv6 socket. */
1807 vty_sock
= sockunion_accept(accept_sock
, &su
);
1809 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1810 safe_strerror(errno
));
1813 set_nonblocking(vty_sock
);
1814 set_cloexec(vty_sock
);
1816 sockunion2hostprefix(&su
, &p
);
1818 /* VTY's accesslist apply. */
1819 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1820 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1821 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1822 zlog_info("Vty connection refused from %s",
1823 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1826 /* continue accepting connections */
1827 vty_event(VTY_SERV
, accept_sock
, NULL
);
1833 /* VTY's ipv6 accesslist apply. */
1834 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1835 if ((acl
= access_list_lookup(AFI_IP6
,
1836 vty_ipv6_accesslist_name
))
1837 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1838 zlog_info("Vty connection refused from %s",
1839 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1842 /* continue accepting connections */
1843 vty_event(VTY_SERV
, accept_sock
, NULL
);
1850 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1853 zlog_info("can't set sockopt to vty_sock : %s",
1854 safe_strerror(errno
));
1856 zlog_info("Vty connection from %s",
1857 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1859 vty_create(vty_sock
, &su
);
1864 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1867 struct addrinfo req
;
1868 struct addrinfo
*ainfo
;
1869 struct addrinfo
*ainfo_save
;
1871 char port_str
[BUFSIZ
];
1873 memset(&req
, 0, sizeof(struct addrinfo
));
1874 req
.ai_flags
= AI_PASSIVE
;
1875 req
.ai_family
= AF_UNSPEC
;
1876 req
.ai_socktype
= SOCK_STREAM
;
1877 sprintf(port_str
, "%d", port
);
1878 port_str
[sizeof(port_str
) - 1] = '\0';
1880 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1883 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1891 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1894 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1895 ainfo
->ai_protocol
);
1899 sockopt_v6only(ainfo
->ai_family
, sock
);
1900 sockopt_reuseaddr(sock
);
1901 sockopt_reuseport(sock
);
1904 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1906 close(sock
); /* Avoid sd leak. */
1910 ret
= listen(sock
, 3);
1912 close(sock
); /* Avoid sd leak. */
1916 vty_event(VTY_SERV
, sock
, NULL
);
1917 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1919 freeaddrinfo(ainfo_save
);
1923 /* For sockaddr_un. */
1926 /* VTY shell UNIX domain socket. */
1927 static void vty_serv_un(const char *path
)
1931 struct sockaddr_un serv
;
1933 struct zprivs_ids_t ids
;
1935 /* First of all, unlink existing socket */
1939 old_mask
= umask(0007);
1941 /* Make UNIX domain socket. */
1942 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1944 flog_err_sys(EC_LIB_SOCKET
,
1945 "Cannot create unix stream socket: %s",
1946 safe_strerror(errno
));
1950 /* Make server socket. */
1951 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1952 serv
.sun_family
= AF_UNIX
;
1953 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1954 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1955 len
= serv
.sun_len
= SUN_LEN(&serv
);
1957 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1958 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1962 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1964 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1965 safe_strerror(errno
));
1966 close(sock
); /* Avoid sd leak. */
1970 ret
= listen(sock
, 5);
1972 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1973 safe_strerror(errno
));
1974 close(sock
); /* Avoid sd leak. */
1980 zprivs_get_ids(&ids
);
1982 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1983 earlier for the case when we don't need to chown the file
1984 type casting it here to make a compare */
1985 if ((int)ids
.gid_vty
> 0) {
1986 /* set group of socket */
1987 if (chown(path
, -1, ids
.gid_vty
)) {
1988 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1989 "vty_serv_un: could chown socket, %s",
1990 safe_strerror(errno
));
1994 vty_event(VTYSH_SERV
, sock
, NULL
);
1997 /* #define VTYSH_DEBUG 1 */
1999 static int vtysh_accept(struct thread
*thread
)
2004 struct sockaddr_un client
;
2007 accept_sock
= THREAD_FD(thread
);
2009 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2011 memset(&client
, 0, sizeof(struct sockaddr_un
));
2012 client_len
= sizeof(struct sockaddr_un
);
2014 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2015 (socklen_t
*)&client_len
);
2018 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2019 safe_strerror(errno
));
2023 if (set_nonblocking(sock
) < 0) {
2026 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2027 sock
, safe_strerror(errno
));
2034 printf("VTY shell accept\n");
2035 #endif /* VTYSH_DEBUG */
2040 vty
->type
= VTY_SHELL_SERV
;
2041 vty
->node
= VIEW_NODE
;
2043 vty_event(VTYSH_READ
, sock
, vty
);
2048 static int vtysh_flush(struct vty
*vty
)
2050 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2051 case BUFFER_PENDING
:
2052 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2056 0; /* disable monitoring to avoid infinite recursion */
2057 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2059 buffer_reset(vty
->lbuf
);
2060 buffer_reset(vty
->obuf
);
2070 static int vtysh_read(struct thread
*thread
)
2076 unsigned char buf
[VTY_READ_BUFSIZ
];
2078 uint8_t header
[4] = {0, 0, 0, 0};
2080 sock
= THREAD_FD(thread
);
2081 vty
= THREAD_ARG(thread
);
2083 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2085 if (ERRNO_IO_RETRY(errno
)) {
2086 vty_event(VTYSH_READ
, sock
, vty
);
2089 vty
->monitor
= 0; /* disable monitoring to avoid
2090 infinite recursion */
2093 "%s: read failed on vtysh client fd %d, closing: %s",
2094 __func__
, sock
, safe_strerror(errno
));
2096 buffer_reset(vty
->lbuf
);
2097 buffer_reset(vty
->obuf
);
2100 printf("close vtysh\n");
2101 #endif /* VTYSH_DEBUG */
2106 printf("line: %.*s\n", nbytes
, buf
);
2107 #endif /* VTYSH_DEBUG */
2109 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2110 /* Clear command line buffer. */
2111 vty
->cp
= vty
->length
= 0;
2113 vty_out(vty
, "%% Command is too long.\n");
2115 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2116 vty
->buf
[vty
->length
++] = *p
;
2118 /* Pass this line to parser. */
2119 ret
= vty_execute(vty
);
2120 /* Note that vty_execute clears the command buffer and resets
2121 vty->length to 0. */
2123 /* Return result. */
2125 printf("result: %d\n", ret
);
2126 printf("vtysh node: %d\n", vty
->node
);
2127 #endif /* VTYSH_DEBUG */
2129 /* hack for asynchronous "write integrated"
2130 * - other commands in "buf" will be ditched
2131 * - input during pending config-write is
2133 if (ret
== CMD_SUSPEND
)
2136 /* warning: watchfrr hardcodes this result write
2139 buffer_put(vty
->obuf
, header
, 4);
2141 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2142 /* Try to flush results; exit if a write
2149 if (vty
->status
== VTY_CLOSE
)
2152 vty_event(VTYSH_READ
, sock
, vty
);
2157 static int vtysh_write(struct thread
*thread
)
2159 struct vty
*vty
= THREAD_ARG(thread
);
2167 /* Determine address family to bind. */
2168 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2170 /* If port is set to 0, do not listen on TCP/IP at all! */
2172 vty_serv_sock_addrinfo(addr
, port
);
2179 static void vty_error_delete(void *arg
)
2181 struct vty_error
*ve
= arg
;
2183 XFREE(MTYPE_TMP
, ve
);
2186 /* Close vty interface. Warning: call this only from functions that
2187 will be careful not to access the vty afterwards (since it has
2188 now been freed). This is safest from top-level functions (called
2189 directly by the thread dispatcher). */
2190 void vty_close(struct vty
*vty
)
2193 bool was_stdio
= false;
2195 /* Cancel threads.*/
2196 THREAD_OFF(vty
->t_read
);
2197 THREAD_OFF(vty
->t_write
);
2198 THREAD_OFF(vty
->t_timeout
);
2201 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2203 /* Free input buffer. */
2204 buffer_free(vty
->obuf
);
2205 buffer_free(vty
->lbuf
);
2207 /* Free command history. */
2208 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2209 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2214 vector_unset(vtyvec
, vty
->fd
);
2216 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2220 * note check is for fd > STDERR_FILENO, not fd != -1.
2221 * We never close stdin/stdout/stderr here, because we may be
2222 * running in foreground mode with logging to stdout. Also,
2223 * additionally, we'd need to replace these fds with /dev/null. */
2224 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2226 if (vty
->fd
> STDERR_FILENO
)
2228 if (vty
->fd
== STDIN_FILENO
)
2231 XFREE(MTYPE_VTY
, vty
->buf
);
2234 vty
->error
->del
= vty_error_delete
;
2235 list_delete(&vty
->error
);
2238 /* Check configure. */
2239 vty_config_exit(vty
);
2242 XFREE(MTYPE_VTY
, vty
);
2248 /* When time out occur output message then close connection. */
2249 static int vty_timeout(struct thread
*thread
)
2253 vty
= THREAD_ARG(thread
);
2257 buffer_reset(vty
->lbuf
);
2258 buffer_reset(vty
->obuf
);
2259 vty_out(vty
, "\nVty connection is timed out.\n");
2261 /* Close connection. */
2262 vty
->status
= VTY_CLOSE
;
2268 /* Read up configuration file from file_name. */
2269 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2273 struct vty_error
*ve
;
2274 struct listnode
*node
;
2275 unsigned int line_num
= 0;
2278 /* vty_close won't close stderr; if some config command prints
2279 * something it'll end up there. (not ideal; it'd be beter if output
2280 * from a file-load went to logging instead. Also note that if this
2281 * function is called after daemonizing, stderr will be /dev/null.)
2283 * vty->fd will be -1 from vty_new()
2285 vty
->wfd
= STDERR_FILENO
;
2286 vty
->type
= VTY_FILE
;
2287 vty
->node
= CONFIG_NODE
;
2290 vty
->candidate_config
= config
;
2292 vty
->private_config
= true;
2293 vty
->candidate_config
= nb_config_new(NULL
);
2296 /* Execute configuration file */
2297 ret
= config_from_file(vty
, confp
, &line_num
);
2299 /* Flush any previous errors before printing messages below */
2300 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2302 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2303 const char *message
= NULL
;
2307 case CMD_ERR_AMBIGUOUS
:
2308 message
= "Ambiguous command";
2310 case CMD_ERR_NO_MATCH
:
2311 message
= "No such command";
2314 message
= "Command returned Warning";
2316 case CMD_WARNING_CONFIG_FAILED
:
2317 message
= "Command returned Warning Config Failed";
2319 case CMD_ERR_INCOMPLETE
:
2320 message
= "Command returned Incomplete";
2322 case CMD_ERR_EXEED_ARGC_MAX
:
2324 "Command exceeded maximum number of Arguments";
2327 message
= "Command returned unhandled error message";
2331 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2332 nl
= strchr(ve
->error_buf
, '\n');
2335 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2336 message
, ve
->line_num
, ve
->error_buf
);
2341 * Automatically commit the candidate configuration after
2342 * reading the configuration file.
2344 if (config
== NULL
) {
2345 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2346 vty
, true, "Read configuration file",
2348 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2349 zlog_err("%s: failed to read configuration file.",
2356 static FILE *vty_use_backup_config(const char *fullpath
)
2358 char *fullpath_sav
, *fullpath_tmp
;
2364 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2365 fullpath_sav
= malloc(fullpath_sav_sz
);
2366 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2367 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2369 sav
= open(fullpath_sav
, O_RDONLY
);
2375 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2376 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2378 /* Open file to configuration write. */
2379 tmp
= mkstemp(fullpath_tmp
);
2383 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2386 while ((c
= read(sav
, buffer
, 512)) > 0) {
2387 if (write(tmp
, buffer
, c
) <= 0)
2393 if (rename(fullpath_tmp
, fullpath
) == 0)
2394 ret
= fopen(fullpath
, "r");
2396 unlink(fullpath_tmp
);
2401 unlink(fullpath_tmp
);
2411 /* Read up configuration file from file_name. */
2412 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2413 char *config_default_dir
)
2415 char cwd
[MAXPATHLEN
];
2417 const char *fullpath
;
2419 bool read_success
= false;
2421 /* If -f flag specified. */
2422 if (config_file
!= NULL
) {
2423 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2424 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2427 "%s: failure to determine Current Working Directory %d!",
2429 goto tmp_free_and_out
;
2431 tmp
= XMALLOC(MTYPE_TMP
,
2432 strlen(cwd
) + strlen(config_file
) + 2);
2433 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2436 fullpath
= config_file
;
2438 confp
= fopen(fullpath
, "r");
2440 if (confp
== NULL
) {
2442 EC_LIB_BACKUP_CONFIG
,
2443 "%s: failed to open configuration file %s: %s, checking backup",
2444 __func__
, fullpath
, safe_strerror(errno
));
2446 confp
= vty_use_backup_config(fullpath
);
2449 EC_LIB_BACKUP_CONFIG
,
2450 "WARNING: using backup configuration file!");
2454 "%s: can't open configuration file [%s]",
2455 __func__
, config_file
);
2456 goto tmp_free_and_out
;
2461 host_config_set(config_default_dir
);
2465 struct stat conf_stat
;
2467 /* !!!!PLEASE LEAVE!!!!
2468 * This is NEEDED for use with vtysh -b, or else you can get
2469 * a real configuration food fight with a lot garbage in the
2470 * merged configuration file it creates coming from the per
2471 * daemon configuration files. This also allows the daemons
2472 * to start if there default configuration file is not
2473 * present or ignore them, as needed when using vtysh -b to
2474 * configure the daemons at boot - MAG
2477 /* Stat for vtysh Zebra.conf, if found startup and wait for
2478 * boot configuration
2481 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2482 ret
= stat(integrate_default
, &conf_stat
);
2484 read_success
= true;
2485 goto tmp_free_and_out
;
2489 confp
= fopen(config_default_dir
, "r");
2490 if (confp
== NULL
) {
2493 "%s: failed to open configuration file %s: %s, checking backup",
2494 __func__
, config_default_dir
,
2495 safe_strerror(errno
));
2497 confp
= vty_use_backup_config(config_default_dir
);
2500 EC_LIB_BACKUP_CONFIG
,
2501 "WARNING: using backup configuration file!");
2502 fullpath
= config_default_dir
;
2504 flog_err(EC_LIB_VTY
,
2505 "can't open configuration file [%s]",
2506 config_default_dir
);
2507 goto tmp_free_and_out
;
2510 fullpath
= config_default_dir
;
2513 vty_read_file(config
, confp
);
2514 read_success
= true;
2518 host_config_set(fullpath
);
2521 XFREE(MTYPE_TMP
, tmp
);
2523 return read_success
;
2526 /* Small utility function which output log to the VTY. */
2527 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2528 struct timestamp_control
*ctl
)
2536 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2537 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2539 vty_log_out(vty
, level
, proto_str
, msg
, ctl
);
2542 /* Async-signal-safe version of vty_log for fixed strings. */
2543 void vty_log_fixed(char *buf
, size_t len
)
2546 struct iovec iov
[2];
2547 char crlf
[4] = "\r\n";
2549 /* vty may not have been initialised */
2553 iov
[0].iov_base
= buf
;
2554 iov
[0].iov_len
= len
;
2555 iov
[1].iov_base
= crlf
;
2558 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2560 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2561 /* N.B. We don't care about the return code, since
2563 most likely just about to die anyway. */
2564 if (writev(vty
->wfd
, iov
, 2) == -1) {
2565 fprintf(stderr
, "Failure to writev: %d\n",
2572 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2574 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2575 vty_out(vty
, "%% Configuration is locked by other client\n");
2579 vty
->node
= CONFIG_NODE
;
2581 vty
->private_config
= private_config
;
2582 vty
->xpath_index
= 0;
2584 if (private_config
) {
2585 vty
->candidate_config
= nb_config_dup(running_config
);
2586 vty
->candidate_config_base
= nb_config_dup(running_config
);
2588 "Warning: uncommitted changes will be discarded on exit.\n\n");
2590 vty
->candidate_config
= vty_shared_candidate_config
;
2591 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2592 vty
->candidate_config_base
=
2593 nb_config_dup(running_config
);
2599 void vty_config_exit(struct vty
*vty
)
2601 /* Check if there's a pending confirmed commit. */
2602 if (vty
->t_confirmed_commit_timeout
) {
2604 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2605 nb_cli_confirmed_commit_rollback(vty
);
2606 nb_cli_confirmed_commit_clean(vty
);
2609 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2611 if (vty
->candidate_config
) {
2612 if (vty
->private_config
)
2613 nb_config_free(vty
->candidate_config
);
2614 vty
->candidate_config
= NULL
;
2616 if (vty
->candidate_config_base
) {
2617 nb_config_free(vty
->candidate_config_base
);
2618 vty
->candidate_config_base
= NULL
;
2621 vty
->config
= false;
2624 /* Master of the threads. */
2625 static struct thread_master
*vty_master
;
2627 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2629 struct thread
*vty_serv_thread
= NULL
;
2633 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2635 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2639 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2641 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2644 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2648 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2653 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2655 /* Time out treatment. */
2656 if (vty
->v_timeout
) {
2657 THREAD_OFF(vty
->t_timeout
);
2658 thread_add_timer(vty_master
, vty_timeout
, vty
,
2659 vty
->v_timeout
, &vty
->t_timeout
);
2663 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2666 case VTY_TIMEOUT_RESET
:
2667 THREAD_OFF(vty
->t_timeout
);
2669 thread_add_timer(vty_master
, vty_timeout
, vty
,
2670 vty
->v_timeout
, &vty
->t_timeout
);
2675 DEFUN_NOSH (config_who
,
2678 "Display who is on vty\n")
2683 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2684 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2685 vty_out(vty
, "%svty[%d] connected from %s.\n",
2686 v
->config
? "*" : " ", i
, v
->address
);
2690 /* Move to vty configuration mode. */
2691 DEFUN_NOSH (line_vty
,
2694 "Configure a terminal line\n"
2695 "Virtual terminal\n")
2697 vty
->node
= VTY_NODE
;
2701 /* Set time out value. */
2702 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2703 const char *sec_str
)
2705 unsigned long timeout
= 0;
2707 /* min_str and sec_str are already checked by parser. So it must be
2708 all digit string. */
2710 timeout
= strtol(min_str
, NULL
, 10);
2714 timeout
+= strtol(sec_str
, NULL
, 10);
2716 vty_timeout_val
= timeout
;
2717 vty
->v_timeout
= timeout
;
2718 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2724 DEFUN (exec_timeout_min
,
2725 exec_timeout_min_cmd
,
2726 "exec-timeout (0-35791)",
2727 "Set timeout value\n"
2728 "Timeout value in minutes\n")
2731 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2734 DEFUN (exec_timeout_sec
,
2735 exec_timeout_sec_cmd
,
2736 "exec-timeout (0-35791) (0-2147483)",
2737 "Set the EXEC timeout\n"
2738 "Timeout in minutes\n"
2739 "Timeout in seconds\n")
2742 int idx_number_2
= 2;
2743 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2744 argv
[idx_number_2
]->arg
);
2747 DEFUN (no_exec_timeout
,
2748 no_exec_timeout_cmd
,
2751 "Set the EXEC timeout\n")
2753 return exec_timeout(vty
, NULL
, NULL
);
2756 /* Set vty access class. */
2757 DEFUN (vty_access_class
,
2758 vty_access_class_cmd
,
2759 "access-class WORD",
2760 "Filter connections based on an IP access list\n"
2764 if (vty_accesslist_name
)
2765 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2767 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2772 /* Clear vty access class. */
2773 DEFUN (no_vty_access_class
,
2774 no_vty_access_class_cmd
,
2775 "no access-class [WORD]",
2777 "Filter connections based on an IP access list\n"
2781 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2782 if (!vty_accesslist_name
2783 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2784 vty_out(vty
, "Access-class is not currently applied to vty\n");
2785 return CMD_WARNING_CONFIG_FAILED
;
2788 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2790 vty_accesslist_name
= NULL
;
2795 /* Set vty access class. */
2796 DEFUN (vty_ipv6_access_class
,
2797 vty_ipv6_access_class_cmd
,
2798 "ipv6 access-class WORD",
2800 "Filter connections based on an IP access list\n"
2801 "IPv6 access list\n")
2804 if (vty_ipv6_accesslist_name
)
2805 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2807 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2812 /* Clear vty access class. */
2813 DEFUN (no_vty_ipv6_access_class
,
2814 no_vty_ipv6_access_class_cmd
,
2815 "no ipv6 access-class [WORD]",
2818 "Filter connections based on an IP access list\n"
2819 "IPv6 access list\n")
2822 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2824 if (!vty_ipv6_accesslist_name
2825 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2827 "IPv6 access-class is not currently applied to vty\n");
2828 return CMD_WARNING_CONFIG_FAILED
;
2831 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2833 vty_ipv6_accesslist_name
= NULL
;
2842 "Enable password checking\n")
2844 no_password_check
= 0;
2848 DEFUN (no_vty_login
,
2852 "Enable password checking\n")
2854 no_password_check
= 1;
2858 DEFUN (service_advanced_vty
,
2859 service_advanced_vty_cmd
,
2860 "service advanced-vty",
2861 "Set up miscellaneous service\n"
2862 "Enable advanced mode vty interface\n")
2868 DEFUN (no_service_advanced_vty
,
2869 no_service_advanced_vty_cmd
,
2870 "no service advanced-vty",
2872 "Set up miscellaneous service\n"
2873 "Enable advanced mode vty interface\n")
2879 DEFUN_NOSH (terminal_monitor
,
2880 terminal_monitor_cmd
,
2882 "Set terminal line parameters\n"
2883 "Copy debug output to the current terminal line\n")
2889 DEFUN_NOSH (terminal_no_monitor
,
2890 terminal_no_monitor_cmd
,
2891 "terminal no monitor",
2892 "Set terminal line parameters\n"
2894 "Copy debug output to the current terminal line\n")
2900 DEFUN_NOSH (no_terminal_monitor
,
2901 no_terminal_monitor_cmd
,
2902 "no terminal monitor",
2904 "Set terminal line parameters\n"
2905 "Copy debug output to the current terminal line\n")
2907 return terminal_no_monitor(self
, vty
, argc
, argv
);
2911 DEFUN_NOSH (show_history
,
2915 "Display the session command history\n")
2919 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2920 if (index
== VTY_MAXHIST
) {
2925 if (vty
->hist
[index
] != NULL
)
2926 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2935 DEFPY (log_commands
,
2937 "[no] log commands",
2940 "Log all commands\n")
2943 if (do_log_commands_perm
) {
2945 "Daemon started with permanent logging turned on for commands, ignoring\n");
2949 do_log_commands
= false;
2951 do_log_commands
= true;
2956 /* Display current configuration. */
2957 static int vty_config_write(struct vty
*vty
)
2959 vty_out(vty
, "line vty\n");
2961 if (vty_accesslist_name
)
2962 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2964 if (vty_ipv6_accesslist_name
)
2965 vty_out(vty
, " ipv6 access-class %s\n",
2966 vty_ipv6_accesslist_name
);
2969 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2970 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2971 vty_timeout_val
% 60);
2974 if (no_password_check
)
2975 vty_out(vty
, " no login\n");
2977 if (do_log_commands
)
2978 vty_out(vty
, "log commands\n");
2980 vty_out(vty
, "!\n");
2985 struct cmd_node vty_node
= {
2986 VTY_NODE
, "%s(config-line)# ", 1,
2989 /* Reset all VTY status. */
2990 void vty_reset(void)
2994 struct thread
*vty_serv_thread
;
2996 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2997 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2998 buffer_reset(vty
->lbuf
);
2999 buffer_reset(vty
->obuf
);
3000 vty
->status
= VTY_CLOSE
;
3004 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3005 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3007 THREAD_OFF(vty_serv_thread
);
3008 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3012 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3014 if (vty_accesslist_name
) {
3015 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3016 vty_accesslist_name
= NULL
;
3019 if (vty_ipv6_accesslist_name
) {
3020 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3021 vty_ipv6_accesslist_name
= NULL
;
3025 static void vty_save_cwd(void)
3029 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3033 * At this point if these go wrong, more than likely
3034 * the whole world is coming down around us
3035 * Hence not worrying about it too much.
3037 if (!chdir(SYSCONFDIR
)) {
3038 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3039 "Failure to chdir to %s, errno: %d",
3043 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3044 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3045 "Failure to getcwd, errno: %d", errno
);
3051 char *vty_get_cwd(void)
3056 int vty_shell(struct vty
*vty
)
3058 return vty
->type
== VTY_SHELL
? 1 : 0;
3061 int vty_shell_serv(struct vty
*vty
)
3063 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3066 void vty_init_vtysh(void)
3068 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3071 /* Install vty's own commands like `who' command. */
3072 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3074 /* For further configuration read, preserve current directory. */
3077 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3079 vty_master
= master_thread
;
3081 atexit(vty_stdio_atexit
);
3083 /* Initilize server thread vector. */
3084 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3086 /* Install bgp top node. */
3087 install_node(&vty_node
, vty_config_write
);
3089 install_element(VIEW_NODE
, &config_who_cmd
);
3090 install_element(VIEW_NODE
, &show_history_cmd
);
3091 install_element(CONFIG_NODE
, &line_vty_cmd
);
3092 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3093 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3094 install_element(CONFIG_NODE
, &show_history_cmd
);
3095 install_element(CONFIG_NODE
, &log_commands_cmd
);
3097 if (do_command_logging
) {
3098 do_log_commands
= true;
3099 do_log_commands_perm
= true;
3102 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3103 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3104 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3106 install_default(VTY_NODE
);
3107 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3108 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3109 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3110 install_element(VTY_NODE
, &vty_access_class_cmd
);
3111 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3112 install_element(VTY_NODE
, &vty_login_cmd
);
3113 install_element(VTY_NODE
, &no_vty_login_cmd
);
3114 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3115 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3118 void vty_terminate(void)
3120 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3122 if (vtyvec
&& Vvty_serv_thread
) {
3124 vector_free(vtyvec
);
3125 vector_free(Vvty_serv_thread
);
3127 Vvty_serv_thread
= NULL
;