2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
25 #include <sys/types.h>
33 #include "sockunion.h"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
46 #include <arpa/telnet.h>
49 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
50 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
51 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
66 static void vty_event(enum event
, int, struct vty
*);
68 /* Extern host structure from command.c */
69 extern struct host host
;
71 /* Vector which store each vty structure. */
74 /* Vty timeout value. */
75 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
77 /* Vty access-class command */
78 static char *vty_accesslist_name
= NULL
;
80 /* Vty access-calss for IPv6. */
81 static char *vty_ipv6_accesslist_name
= NULL
;
83 /* VTY server thread. */
84 static vector Vvty_serv_thread
;
86 /* Current directory. */
89 /* Login password check. */
90 static int no_password_check
= 0;
92 /* Integrated configuration file path */
93 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
95 static int do_log_commands
= 0;
97 void vty_frame(struct vty
*vty
, const char *format
, ...)
101 va_start(args
, format
);
102 vsnprintf(vty
->frame
+ vty
->frame_pos
,
103 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
104 vty
->frame_pos
= strlen(vty
->frame
);
108 void vty_endframe(struct vty
*vty
, const char *endtext
)
110 if (vty
->frame_pos
== 0 && endtext
)
111 vty_out(vty
, "%s", endtext
);
115 bool vty_set_include(struct vty
*vty
, const char *regexp
)
123 regfree(&vty
->include
);
129 errcode
= regcomp(&vty
->include
, regexp
,
130 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
133 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
134 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
142 /* VTY standard output function. */
143 int vty_out(struct vty
*vty
, const char *format
, ...)
152 if (vty
->frame_pos
) {
154 vty_out(vty
, "%s", vty
->frame
);
157 /* Try to write to initial buffer. */
158 va_start(args
, format
);
159 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
162 /* Initial buffer is not enough. */
163 if (len
< 0 || len
>= size
) {
170 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
174 va_start(args
, format
);
175 len
= vsnprintf(p
, size
, format
, args
);
178 if (len
> -1 && len
< size
)
183 /* When initial buffer is enough to store all output. */
189 vector lines
= frrstr_split_vec(p
, "\n");
191 /* Place first value in the cache */
192 char *firstline
= vector_slot(lines
, 0);
193 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
195 /* If our split returned more than one entry, time to filter */
196 if (vector_active(lines
) > 1) {
198 * returned string is MTYPE_TMP so it matches the MTYPE
199 * of everything else in the vector
201 char *bstr
= buffer_getstr(vty
->lbuf
);
202 buffer_reset(vty
->lbuf
);
203 XFREE(MTYPE_TMP
, lines
->index
[0]);
204 vector_set_index(lines
, 0, bstr
);
205 frrstr_filter_vec(lines
, &vty
->include
);
206 vector_compact(lines
);
208 * Consider the string "foo\n". If the regex is an empty string
209 * and the line ended with a newline, then the vector will look
215 * If the regex isn't empty, the vector will look like:
219 * In this case we'd like to preserve the newline, so we add
220 * the empty string [1] as in the first example.
222 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
223 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
224 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
226 filtered
= frrstr_join_vec(lines
, "\n");
232 frrstr_strvec_free(lines
);
243 /* print with crlf replacement */
244 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
248 fprintf(vty
->of
, "%s", filtered
);
254 /* print without crlf replacement */
255 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
261 if (vty
->filter
&& filtered
)
262 XFREE(MTYPE_TMP
, filtered
);
264 /* If p is not different with buf, it is allocated buffer. */
266 XFREE(MTYPE_VTY_OUT_BUF
, p
);
271 static int vty_log_out(struct vty
*vty
, const char *level
,
272 const char *proto_str
, const char *format
,
273 struct timestamp_control
*ctl
, va_list va
)
279 if (!ctl
->already_rendered
) {
280 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
282 ctl
->already_rendered
= 1;
284 if (ctl
->len
+ 1 >= sizeof(buf
))
286 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
291 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
294 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
295 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
298 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
299 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
305 if (write(vty
->wfd
, buf
, len
) < 0) {
306 if (ERRNO_IO_RETRY(errno
))
307 /* Kernel buffer is full, probably too much debugging
309 drop the data and ignore. */
311 /* Fatal I/O error. */
313 0; /* disable monitoring to avoid infinite recursion */
314 flog_err(EC_LIB_SOCKET
,
315 "%s: write failed to vty client fd %d, closing: %s",
316 __func__
, vty
->fd
, safe_strerror(errno
));
317 buffer_reset(vty
->obuf
);
318 buffer_reset(vty
->lbuf
);
319 /* cannot call vty_close, because a parent routine may still try
320 to access the vty struct */
321 vty
->status
= VTY_CLOSE
;
322 shutdown(vty
->fd
, SHUT_RDWR
);
328 /* Output current time to the vty. */
329 void vty_time_print(struct vty
*vty
, int cr
)
331 char buf
[QUAGGA_TIMESTAMP_LEN
];
333 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
334 zlog_info("quagga_timestamp error");
338 vty_out(vty
, "%s\n", buf
);
340 vty_out(vty
, "%s ", buf
);
345 /* Say hello to vty interface. */
346 void vty_hello(struct vty
*vty
)
352 f
= fopen(host
.motdfile
, "r");
354 while (fgets(buf
, sizeof(buf
), f
)) {
356 /* work backwards to ignore trailling isspace()
358 for (s
= buf
+ strlen(buf
);
359 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
362 vty_out(vty
, "%s\n", buf
);
366 vty_out(vty
, "MOTD file not found\n");
367 } else if (host
.motd
)
368 vty_out(vty
, "%s", host
.motd
);
371 /* Put out prompt and wait input from user. */
372 static void vty_prompt(struct vty
*vty
)
374 if (vty
->type
== VTY_TERM
) {
375 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
379 /* Send WILL TELOPT_ECHO to remote server. */
380 static void vty_will_echo(struct vty
*vty
)
382 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
383 vty_out(vty
, "%s", cmd
);
386 /* Make suppress Go-Ahead telnet option. */
387 static void vty_will_suppress_go_ahead(struct vty
*vty
)
389 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
390 vty_out(vty
, "%s", cmd
);
393 /* Make don't use linemode over telnet. */
394 static void vty_dont_linemode(struct vty
*vty
)
396 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
397 vty_out(vty
, "%s", cmd
);
400 /* Use window size. */
401 static void vty_do_window_size(struct vty
*vty
)
403 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
404 vty_out(vty
, "%s", cmd
);
407 #if 0 /* Currently not used. */
408 /* Make don't use lflow vty interface. */
410 vty_dont_lflow_ahead (struct vty
*vty
)
412 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
413 vty_out (vty
, "%s", cmd
);
417 /* Authentication of vty */
418 static void vty_auth(struct vty
*vty
, char *buf
)
421 enum node_type next_node
= 0;
423 char *crypt(const char *, const char *);
428 passwd
= host
.password_encrypt
;
430 passwd
= host
.password
;
432 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
434 next_node
= VIEW_NODE
;
436 case AUTH_ENABLE_NODE
:
438 passwd
= host
.enable_encrypt
;
440 passwd
= host
.enable
;
441 next_node
= ENABLE_NODE
;
447 fail
= strcmp(crypt(buf
, passwd
), passwd
);
449 fail
= strcmp(buf
, passwd
);
455 vty
->node
= next_node
; /* Success ! */
458 if (vty
->fail
>= 3) {
459 if (vty
->node
== AUTH_NODE
) {
461 "%% Bad passwords, too many failures!\n");
462 vty
->status
= VTY_CLOSE
;
464 /* AUTH_ENABLE_NODE */
467 "%% Bad enable passwords, too many failures!\n");
468 vty
->status
= VTY_CLOSE
;
474 /* Command execution over the vty interface. */
475 static int vty_command(struct vty
*vty
, char *buf
)
478 const char *protocolname
;
484 * Log non empty command lines
489 /* Skip white spaces. */
490 while (isspace((int)*cp
) && *cp
!= '\0')
493 if (cp
!= NULL
&& *cp
!= '\0') {
495 char vty_str
[VTY_BUFSIZ
];
496 char prompt_str
[VTY_BUFSIZ
];
498 /* format the base vty info */
499 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
501 for (i
= 0; i
< vector_active(vtyvec
); i
++)
502 if (vty
== vector_slot(vtyvec
, i
)) {
503 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
508 /* format the prompt */
509 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
512 /* now log the command */
513 zlog_notice("%s%s", prompt_str
, buf
);
516 #ifdef CONSUMED_TIME_CHECK
520 unsigned long realtime
, cputime
;
523 #endif /* CONSUMED_TIME_CHECK */
525 ret
= cmd_execute(vty
, buf
, NULL
, 0);
527 /* Get the name of the protocol if any */
528 protocolname
= frr_protoname
;
530 #ifdef CONSUMED_TIME_CHECK
532 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
533 > CONSUMED_TIME_CHECK
)
534 /* Warn about CPU hog that must be fixed. */
537 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
538 realtime
/ 1000, cputime
/ 1000, buf
);
540 #endif /* CONSUMED_TIME_CHECK */
542 if (ret
!= CMD_SUCCESS
)
545 if (vty
->type
== VTY_FILE
)
546 vty_out(vty
, "Warning...\n");
548 case CMD_ERR_AMBIGUOUS
:
549 vty_out(vty
, "%% Ambiguous command.\n");
551 case CMD_ERR_NO_MATCH
:
552 vty_out(vty
, "%% [%s] Unknown command: %s\n",
555 case CMD_ERR_INCOMPLETE
:
556 vty_out(vty
, "%% Command incomplete.\n");
563 static const char telnet_backward_char
= 0x08;
564 static const char telnet_space_char
= ' ';
566 /* Basic function to write buffer to vty. */
567 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
569 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
572 /* Should we do buffering here ? And make vty_flush (vty) ? */
573 buffer_put(vty
->obuf
, buf
, nbytes
);
576 /* Basic function to insert character into vty. */
577 static void vty_self_insert(struct vty
*vty
, char c
)
582 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
585 length
= vty
->length
- vty
->cp
;
586 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
587 vty
->buf
[vty
->cp
] = c
;
589 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
590 for (i
= 0; i
< length
; i
++)
591 vty_write(vty
, &telnet_backward_char
, 1);
596 vty
->buf
[vty
->length
] = '\0';
599 /* Self insert character 'c' in overwrite mode. */
600 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
602 if (vty
->cp
== vty
->length
) {
603 vty_self_insert(vty
, c
);
607 vty
->buf
[vty
->cp
++] = c
;
608 vty_write(vty
, &c
, 1);
612 * Insert a string into vty->buf at the current cursor position.
614 * If the resultant string would be larger than VTY_BUFSIZ it is
617 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
619 if (vty
->cp
== VTY_BUFSIZ
)
622 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
623 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
625 vty
->length
= MAX(vty
->cp
, vty
->length
);
626 vty
->buf
[vty
->length
] = '\0';
627 vty_write(vty
, str
, nwrite
);
630 /* Forward character. */
631 static void vty_forward_char(struct vty
*vty
)
633 if (vty
->cp
< vty
->length
) {
634 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
639 /* Backward character. */
640 static void vty_backward_char(struct vty
*vty
)
644 vty_write(vty
, &telnet_backward_char
, 1);
648 /* Move to the beginning of the line. */
649 static void vty_beginning_of_line(struct vty
*vty
)
652 vty_backward_char(vty
);
655 /* Move to the end of the line. */
656 static void vty_end_of_line(struct vty
*vty
)
658 while (vty
->cp
< vty
->length
)
659 vty_forward_char(vty
);
662 static void vty_kill_line_from_beginning(struct vty
*);
663 static void vty_redraw_line(struct vty
*);
665 /* Print command line history. This function is called from
666 vty_next_line and vty_previous_line. */
667 static void vty_history_print(struct vty
*vty
)
671 vty_kill_line_from_beginning(vty
);
673 /* Get previous line from history buffer */
674 length
= strlen(vty
->hist
[vty
->hp
]);
675 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
676 vty
->cp
= vty
->length
= length
;
677 vty
->buf
[vty
->length
] = '\0';
679 /* Redraw current line */
680 vty_redraw_line(vty
);
683 /* Show next command line history. */
684 static void vty_next_line(struct vty
*vty
)
688 if (vty
->hp
== vty
->hindex
)
691 /* Try is there history exist or not. */
693 if (try_index
== (VTY_MAXHIST
- 1))
698 /* If there is not history return. */
699 if (vty
->hist
[try_index
] == NULL
)
704 vty_history_print(vty
);
707 /* Show previous command line history. */
708 static void vty_previous_line(struct vty
*vty
)
714 try_index
= VTY_MAXHIST
- 1;
718 if (vty
->hist
[try_index
] == NULL
)
723 vty_history_print(vty
);
726 /* This function redraw all of the command line character. */
727 static void vty_redraw_line(struct vty
*vty
)
729 vty_write(vty
, vty
->buf
, vty
->length
);
730 vty
->cp
= vty
->length
;
734 static void vty_forward_word(struct vty
*vty
)
736 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
737 vty_forward_char(vty
);
739 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
740 vty_forward_char(vty
);
743 /* Backward word without skipping training space. */
744 static void vty_backward_pure_word(struct vty
*vty
)
746 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
747 vty_backward_char(vty
);
751 static void vty_backward_word(struct vty
*vty
)
753 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
754 vty_backward_char(vty
);
756 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
757 vty_backward_char(vty
);
760 /* When '^D' is typed at the beginning of the line we move to the down
762 static void vty_down_level(struct vty
*vty
)
770 /* When '^Z' is received from vty, move down to the enable mode. */
771 static void vty_end_config(struct vty
*vty
)
776 vty_config_exit(vty
);
777 vty
->node
= ENABLE_NODE
;
784 /* Delete a charcter at the current point. */
785 static void vty_delete_char(struct vty
*vty
)
790 if (vty
->length
== 0) {
795 if (vty
->cp
== vty
->length
)
796 return; /* completion need here? */
798 size
= vty
->length
- vty
->cp
;
801 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
802 vty
->buf
[vty
->length
] = '\0';
804 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
807 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
808 vty_write(vty
, &telnet_space_char
, 1);
810 for (i
= 0; i
< size
; i
++)
811 vty_write(vty
, &telnet_backward_char
, 1);
814 /* Delete a character before the point. */
815 static void vty_delete_backward_char(struct vty
*vty
)
820 vty_backward_char(vty
);
821 vty_delete_char(vty
);
824 /* Kill rest of line from current point. */
825 static void vty_kill_line(struct vty
*vty
)
830 size
= vty
->length
- vty
->cp
;
835 for (i
= 0; i
< size
; i
++)
836 vty_write(vty
, &telnet_space_char
, 1);
837 for (i
= 0; i
< size
; i
++)
838 vty_write(vty
, &telnet_backward_char
, 1);
840 memset(&vty
->buf
[vty
->cp
], 0, size
);
841 vty
->length
= vty
->cp
;
844 /* Kill line from the beginning. */
845 static void vty_kill_line_from_beginning(struct vty
*vty
)
847 vty_beginning_of_line(vty
);
851 /* Delete a word before the point. */
852 static void vty_forward_kill_word(struct vty
*vty
)
854 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
855 vty_delete_char(vty
);
856 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
857 vty_delete_char(vty
);
860 /* Delete a word before the point. */
861 static void vty_backward_kill_word(struct vty
*vty
)
863 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
864 vty_delete_backward_char(vty
);
865 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
866 vty_delete_backward_char(vty
);
869 /* Transpose chars before or at the point. */
870 static void vty_transpose_chars(struct vty
*vty
)
874 /* If length is short or point is near by the beginning of line then
876 if (vty
->length
< 2 || vty
->cp
< 1)
879 /* In case of point is located at the end of the line. */
880 if (vty
->cp
== vty
->length
) {
881 c1
= vty
->buf
[vty
->cp
- 1];
882 c2
= vty
->buf
[vty
->cp
- 2];
884 vty_backward_char(vty
);
885 vty_backward_char(vty
);
886 vty_self_insert_overwrite(vty
, c1
);
887 vty_self_insert_overwrite(vty
, c2
);
889 c1
= vty
->buf
[vty
->cp
];
890 c2
= vty
->buf
[vty
->cp
- 1];
892 vty_backward_char(vty
);
893 vty_self_insert_overwrite(vty
, c1
);
894 vty_self_insert_overwrite(vty
, c2
);
898 /* Do completion at vty interface. */
899 static void vty_complete_command(struct vty
*vty
)
903 char **matched
= NULL
;
906 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
909 vline
= cmd_make_strvec(vty
->buf
);
913 /* In case of 'help \t'. */
914 if (isspace((int)vty
->buf
[vty
->length
- 1]))
915 vector_set(vline
, NULL
);
917 matched
= cmd_complete_command(vline
, vty
, &ret
);
919 cmd_free_strvec(vline
);
923 case CMD_ERR_AMBIGUOUS
:
924 vty_out(vty
, "%% Ambiguous command.\n");
926 vty_redraw_line(vty
);
928 case CMD_ERR_NO_MATCH
:
929 /* vty_out (vty, "%% There is no matched command.\n"); */
931 vty_redraw_line(vty
);
933 case CMD_COMPLETE_FULL_MATCH
:
935 /* 2016-11-28 equinox -- need to debug, SEGV here */
936 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
938 vty_redraw_line(vty
);
942 vty_redraw_line(vty
);
943 vty_backward_pure_word(vty
);
944 vty_insert_word_overwrite(vty
, matched
[0]);
945 vty_self_insert(vty
, ' ');
946 XFREE(MTYPE_COMPLETION
, matched
[0]);
948 case CMD_COMPLETE_MATCH
:
950 vty_redraw_line(vty
);
951 vty_backward_pure_word(vty
);
952 vty_insert_word_overwrite(vty
, matched
[0]);
953 XFREE(MTYPE_COMPLETION
, matched
[0]);
955 case CMD_COMPLETE_LIST_MATCH
:
956 for (i
= 0; matched
[i
] != NULL
; i
++) {
957 if (i
!= 0 && ((i
% 6) == 0))
959 vty_out(vty
, "%-10s ", matched
[i
]);
960 XFREE(MTYPE_COMPLETION
, matched
[i
]);
965 vty_redraw_line(vty
);
967 case CMD_ERR_NOTHING_TODO
:
969 vty_redraw_line(vty
);
974 XFREE(MTYPE_TMP
, matched
);
977 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
978 unsigned int desc_width
, struct cmd_token
*token
)
986 if (desc_width
<= 0) {
987 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
991 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
993 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
994 for (pos
= desc_width
; pos
> 0; pos
--)
995 if (*(p
+ pos
) == ' ')
1001 strncpy(buf
, p
, pos
);
1003 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1008 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1010 XFREE(MTYPE_TMP
, buf
);
1013 /* Describe matched command function. */
1014 static void vty_describe_command(struct vty
*vty
)
1019 unsigned int i
, width
, desc_width
;
1020 struct cmd_token
*token
, *token_cr
= NULL
;
1022 vline
= cmd_make_strvec(vty
->buf
);
1024 /* In case of '> ?'. */
1025 if (vline
== NULL
) {
1026 vline
= vector_init(1);
1027 vector_set(vline
, NULL
);
1028 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1029 vector_set(vline
, NULL
);
1031 describe
= cmd_describe_command(vline
, vty
, &ret
);
1035 /* Ambiguous error. */
1037 case CMD_ERR_AMBIGUOUS
:
1038 vty_out(vty
, "%% Ambiguous command.\n");
1041 case CMD_ERR_NO_MATCH
:
1042 vty_out(vty
, "%% There is no matched command.\n");
1047 /* Get width of command string. */
1049 for (i
= 0; i
< vector_active(describe
); i
++)
1050 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1053 if (token
->text
[0] == '\0')
1056 len
= strlen(token
->text
);
1062 /* Get width of description string. */
1063 desc_width
= vty
->width
- (width
+ 6);
1065 /* Print out description. */
1066 for (i
= 0; i
< vector_active(describe
); i
++)
1067 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1068 if (token
->text
[0] == '\0')
1071 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1077 vty_out(vty
, " %-s\n", token
->text
);
1078 else if (desc_width
>= strlen(token
->desc
))
1079 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1082 vty_describe_fold(vty
, width
, desc_width
,
1085 if (IS_VARYING_TOKEN(token
->type
)) {
1086 const char *ref
= vector_slot(
1087 vline
, vector_active(vline
) - 1);
1089 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1090 cmd_variable_complete(token
, ref
, varcomps
);
1092 if (vector_active(varcomps
) > 0) {
1093 char *ac
= cmd_variable_comp2str(
1094 varcomps
, vty
->width
);
1095 vty_out(vty
, "%s\n", ac
);
1096 XFREE(MTYPE_TMP
, ac
);
1099 vector_free(varcomps
);
1102 vty_out (vty
, " %-*s %s\n", width
1103 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1104 desc
->str
? desc
->str
: "");
1108 if ((token
= token_cr
)) {
1110 vty_out(vty
, " %-s\n", token
->text
);
1111 else if (desc_width
>= strlen(token
->desc
))
1112 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1115 vty_describe_fold(vty
, width
, desc_width
, token
);
1119 cmd_free_strvec(vline
);
1121 vector_free(describe
);
1124 vty_redraw_line(vty
);
1127 static void vty_clear_buf(struct vty
*vty
)
1129 memset(vty
->buf
, 0, vty
->max
);
1132 /* ^C stop current input and do not add command line to the history. */
1133 static void vty_stop_input(struct vty
*vty
)
1135 vty
->cp
= vty
->length
= 0;
1140 vty_config_exit(vty
);
1141 vty
->node
= ENABLE_NODE
;
1146 /* Set history pointer to the latest one. */
1147 vty
->hp
= vty
->hindex
;
1150 /* Add current command line to the history buffer. */
1151 static void vty_hist_add(struct vty
*vty
)
1155 if (vty
->length
== 0)
1158 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1160 /* Ignore the same string as previous one. */
1161 if (vty
->hist
[index
])
1162 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1163 vty
->hp
= vty
->hindex
;
1167 /* Insert history entry. */
1168 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1169 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1171 /* History index rotation. */
1173 if (vty
->hindex
== VTY_MAXHIST
)
1176 vty
->hp
= vty
->hindex
;
1179 /* #define TELNET_OPTION_DEBUG */
1181 /* Get telnet window size. */
1182 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1184 #ifdef TELNET_OPTION_DEBUG
1187 for (i
= 0; i
< nbytes
; i
++) {
1190 vty_out(vty
, "IAC ");
1193 vty_out(vty
, "WILL ");
1196 vty_out(vty
, "WONT ");
1199 vty_out(vty
, "DO ");
1202 vty_out(vty
, "DONT ");
1205 vty_out(vty
, "SB ");
1208 vty_out(vty
, "SE ");
1211 vty_out(vty
, "TELOPT_ECHO \n");
1214 vty_out(vty
, "TELOPT_SGA \n");
1217 vty_out(vty
, "TELOPT_NAWS \n");
1220 vty_out(vty
, "%x ", buf
[i
]);
1226 #endif /* TELNET_OPTION_DEBUG */
1231 vty
->iac_sb_in_progress
= 1;
1235 if (!vty
->iac_sb_in_progress
)
1238 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1239 vty
->iac_sb_in_progress
= 0;
1242 switch (vty
->sb_buf
[0]) {
1244 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1247 "RFC 1073 violation detected: telnet NAWS option "
1248 "should send %d characters, but we received %lu",
1250 (unsigned long)vty
->sb_len
);
1251 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1254 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1255 (unsigned long)sizeof(vty
->sb_buf
),
1256 TELNET_NAWS_SB_LEN
);
1258 vty
->width
= ((vty
->sb_buf
[1] << 8)
1260 vty
->height
= ((vty
->sb_buf
[3] << 8)
1262 #ifdef TELNET_OPTION_DEBUG
1264 "TELNET NAWS window size negotiation completed: "
1265 "width %d, height %d\n",
1266 vty
->width
, vty
->height
);
1271 vty
->iac_sb_in_progress
= 0;
1281 /* Execute current command line. */
1282 static int vty_execute(struct vty
*vty
)
1288 switch (vty
->node
) {
1290 case AUTH_ENABLE_NODE
:
1291 vty_auth(vty
, vty
->buf
);
1294 ret
= vty_command(vty
, vty
->buf
);
1295 if (vty
->type
== VTY_TERM
)
1300 /* Clear command line buffer. */
1301 vty
->cp
= vty
->length
= 0;
1304 if (vty
->status
!= VTY_CLOSE
)
1310 #define CONTROL(X) ((X) - '@')
1311 #define VTY_NORMAL 0
1312 #define VTY_PRE_ESCAPE 1
1313 #define VTY_ESCAPE 2
1315 /* Escape character command map. */
1316 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1320 vty_previous_line(vty
);
1326 vty_forward_char(vty
);
1329 vty_backward_char(vty
);
1335 /* Go back to normal mode. */
1336 vty
->escape
= VTY_NORMAL
;
1339 /* Quit print out to the buffer. */
1340 static void vty_buffer_reset(struct vty
*vty
)
1342 buffer_reset(vty
->obuf
);
1343 buffer_reset(vty
->lbuf
);
1345 vty_redraw_line(vty
);
1348 /* Read data via vty socket. */
1349 static int vty_read(struct thread
*thread
)
1353 unsigned char buf
[VTY_READ_BUFSIZ
];
1355 int vty_sock
= THREAD_FD(thread
);
1356 struct vty
*vty
= THREAD_ARG(thread
);
1359 /* Read raw data from socket */
1360 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1362 if (ERRNO_IO_RETRY(errno
)) {
1363 vty_event(VTY_READ
, vty_sock
, vty
);
1366 vty
->monitor
= 0; /* disable monitoring to avoid
1367 infinite recursion */
1370 "%s: read error on vty client fd %d, closing: %s",
1371 __func__
, vty
->fd
, safe_strerror(errno
));
1372 buffer_reset(vty
->obuf
);
1373 buffer_reset(vty
->lbuf
);
1375 vty
->status
= VTY_CLOSE
;
1378 for (i
= 0; i
< nbytes
; i
++) {
1379 if (buf
[i
] == IAC
) {
1388 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1389 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1390 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1396 /* In case of telnet command */
1398 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1405 if (vty
->status
== VTY_MORE
) {
1410 vty_buffer_reset(vty
);
1412 #if 0 /* More line does not work for "show ip bgp". */
1415 vty
->status
= VTY_MORELINE
;
1424 /* Escape character. */
1425 if (vty
->escape
== VTY_ESCAPE
) {
1426 vty_escape_map(buf
[i
], vty
);
1430 /* Pre-escape status. */
1431 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1434 vty
->escape
= VTY_ESCAPE
;
1437 vty_backward_word(vty
);
1438 vty
->escape
= VTY_NORMAL
;
1441 vty_forward_word(vty
);
1442 vty
->escape
= VTY_NORMAL
;
1445 vty_forward_kill_word(vty
);
1446 vty
->escape
= VTY_NORMAL
;
1450 vty_backward_kill_word(vty
);
1451 vty
->escape
= VTY_NORMAL
;
1454 vty
->escape
= VTY_NORMAL
;
1462 vty_beginning_of_line(vty
);
1465 vty_backward_char(vty
);
1468 vty_stop_input(vty
);
1471 vty_delete_char(vty
);
1474 vty_end_of_line(vty
);
1477 vty_forward_char(vty
);
1481 vty_delete_backward_char(vty
);
1490 vty_previous_line(vty
);
1493 vty_transpose_chars(vty
);
1496 vty_kill_line_from_beginning(vty
);
1499 vty_backward_kill_word(vty
);
1502 vty_end_config(vty
);
1510 vty_complete_command(vty
);
1513 if (vty
->node
== AUTH_NODE
1514 || vty
->node
== AUTH_ENABLE_NODE
)
1515 vty_self_insert(vty
, buf
[i
]);
1517 vty_describe_command(vty
);
1520 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1521 vty
->escape
= VTY_ESCAPE
;
1524 vty
->escape
= VTY_PRE_ESCAPE
;
1527 if (buf
[i
] > 31 && buf
[i
] < 127)
1528 vty_self_insert(vty
, buf
[i
]);
1534 if (vty
->status
== VTY_CLOSE
)
1537 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1538 vty_event(VTY_READ
, vty_sock
, vty
);
1543 /* Flush buffer to the vty. */
1544 static int vty_flush(struct thread
*thread
)
1547 buffer_status_t flushrc
;
1548 int vty_sock
= THREAD_FD(thread
);
1549 struct vty
*vty
= THREAD_ARG(thread
);
1551 vty
->t_write
= NULL
;
1553 /* Tempolary disable read thread. */
1554 if ((vty
->lines
== 0) && vty
->t_read
) {
1555 thread_cancel(vty
->t_read
);
1559 /* Function execution continue. */
1560 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1562 /* N.B. if width is 0, that means we don't know the window size. */
1563 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1564 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1565 else if (vty
->status
== VTY_MORELINE
)
1566 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1569 flushrc
= buffer_flush_window(
1570 vty
->obuf
, vty_sock
, vty
->width
,
1571 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1575 0; /* disable monitoring to avoid infinite recursion */
1576 zlog_info("buffer_flush failed on vty client fd %d, closing",
1578 buffer_reset(vty
->lbuf
);
1579 buffer_reset(vty
->obuf
);
1583 if (vty
->status
== VTY_CLOSE
)
1586 vty
->status
= VTY_NORMAL
;
1587 if (vty
->lines
== 0)
1588 vty_event(VTY_READ
, vty_sock
, vty
);
1591 case BUFFER_PENDING
:
1592 /* There is more data waiting to be written. */
1593 vty
->status
= VTY_MORE
;
1594 if (vty
->lines
== 0)
1595 vty_event(VTY_WRITE
, vty_sock
, vty
);
1602 /* Allocate new vty struct. */
1603 struct vty
*vty_new(void)
1605 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1607 new->fd
= new->wfd
= -1;
1609 new->lbuf
= buffer_new(0);
1610 new->obuf
= buffer_new(0); /* Use default buffer size. */
1611 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1612 new->max
= VTY_BUFSIZ
;
1618 /* allocate and initialise vty */
1619 static struct vty
*vty_new_init(int vty_sock
)
1625 vty
->wfd
= vty_sock
;
1626 vty
->type
= VTY_TERM
;
1627 vty
->node
= AUTH_NODE
;
1632 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1635 vty
->xpath_index
= 0;
1636 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1637 vty
->private_config
= false;
1638 vty
->candidate_config
= vty_shared_candidate_config
;
1639 vector_set_index(vtyvec
, vty_sock
, vty
);
1640 vty
->status
= VTY_NORMAL
;
1643 vty
->iac_sb_in_progress
= 0;
1649 /* Create new vty structure. */
1650 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1652 char buf
[SU_ADDRSTRLEN
];
1655 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1657 /* Allocate new vty structure and set up default values. */
1658 vty
= vty_new_init(vty_sock
);
1660 /* configurable parameters not part of basic init */
1661 vty
->v_timeout
= vty_timeout_val
;
1662 strcpy(vty
->address
, buf
);
1663 if (no_password_check
) {
1665 vty
->node
= ENABLE_NODE
;
1667 vty
->node
= VIEW_NODE
;
1669 if (host
.lines
>= 0)
1670 vty
->lines
= host
.lines
;
1672 if (!no_password_check
) {
1673 /* Vty is not available if password isn't set. */
1674 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1675 vty_out(vty
, "Vty password is not set.\n");
1676 vty
->status
= VTY_CLOSE
;
1682 /* Say hello to the world. */
1684 if (!no_password_check
)
1685 vty_out(vty
, "\nUser Access Verification\n\n");
1687 /* Setting up terminal. */
1689 vty_will_suppress_go_ahead(vty
);
1691 vty_dont_linemode(vty
);
1692 vty_do_window_size(vty
);
1693 /* vty_dont_lflow_ahead (vty); */
1697 /* Add read/write thread. */
1698 vty_event(VTY_WRITE
, vty_sock
, vty
);
1699 vty_event(VTY_READ
, vty_sock
, vty
);
1704 /* create vty for stdio */
1705 static struct termios stdio_orig_termios
;
1706 static struct vty
*stdio_vty
= NULL
;
1707 static bool stdio_termios
= false;
1708 static void (*stdio_vty_atclose
)(int isexit
);
1710 static void vty_stdio_reset(int isexit
)
1714 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1715 stdio_termios
= false;
1719 if (stdio_vty_atclose
)
1720 stdio_vty_atclose(isexit
);
1721 stdio_vty_atclose
= NULL
;
1725 static void vty_stdio_atexit(void)
1730 void vty_stdio_suspend(void)
1735 if (stdio_vty
->t_write
)
1736 thread_cancel(stdio_vty
->t_write
);
1737 if (stdio_vty
->t_read
)
1738 thread_cancel(stdio_vty
->t_read
);
1739 if (stdio_vty
->t_timeout
)
1740 thread_cancel(stdio_vty
->t_timeout
);
1743 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1744 stdio_termios
= false;
1747 void vty_stdio_resume(void)
1752 if (!tcgetattr(0, &stdio_orig_termios
)) {
1753 struct termios termios
;
1755 termios
= stdio_orig_termios
;
1756 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1757 | IGNCR
| ICRNL
| IXON
);
1758 termios
.c_oflag
&= ~OPOST
;
1759 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1760 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1761 termios
.c_cflag
|= CS8
;
1762 tcsetattr(0, TCSANOW
, &termios
);
1763 stdio_termios
= true;
1766 vty_prompt(stdio_vty
);
1768 /* Add read/write thread. */
1769 vty_event(VTY_WRITE
, 1, stdio_vty
);
1770 vty_event(VTY_READ
, 0, stdio_vty
);
1773 void vty_stdio_close(void)
1777 vty_close(stdio_vty
);
1780 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1784 /* refuse creating two vtys on stdio */
1788 vty
= stdio_vty
= vty_new_init(0);
1789 stdio_vty_atclose
= atclose
;
1792 /* always have stdio vty in a known _unchangeable_ state, don't want
1794 * to have any effect here to make sure scripting this works as intended
1796 vty
->node
= ENABLE_NODE
;
1798 strcpy(vty
->address
, "console");
1804 /* Accept connection from the network. */
1805 static int vty_accept(struct thread
*thread
)
1813 struct access_list
*acl
= NULL
;
1814 char buf
[SU_ADDRSTRLEN
];
1816 accept_sock
= THREAD_FD(thread
);
1818 /* We continue hearing vty socket. */
1819 vty_event(VTY_SERV
, accept_sock
, NULL
);
1821 memset(&su
, 0, sizeof(union sockunion
));
1823 /* We can handle IPv4 or IPv6 socket. */
1824 vty_sock
= sockunion_accept(accept_sock
, &su
);
1826 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1827 safe_strerror(errno
));
1830 set_nonblocking(vty_sock
);
1831 set_cloexec(vty_sock
);
1833 sockunion2hostprefix(&su
, &p
);
1835 /* VTY's accesslist apply. */
1836 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1837 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1838 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1839 zlog_info("Vty connection refused from %s",
1840 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1843 /* continue accepting connections */
1844 vty_event(VTY_SERV
, accept_sock
, NULL
);
1850 /* VTY's ipv6 accesslist apply. */
1851 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1852 if ((acl
= access_list_lookup(AFI_IP6
,
1853 vty_ipv6_accesslist_name
))
1854 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1855 zlog_info("Vty connection refused from %s",
1856 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1859 /* continue accepting connections */
1860 vty_event(VTY_SERV
, accept_sock
, NULL
);
1867 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1870 zlog_info("can't set sockopt to vty_sock : %s",
1871 safe_strerror(errno
));
1873 zlog_info("Vty connection from %s",
1874 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1876 vty_create(vty_sock
, &su
);
1881 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1884 struct addrinfo req
;
1885 struct addrinfo
*ainfo
;
1886 struct addrinfo
*ainfo_save
;
1888 char port_str
[BUFSIZ
];
1890 memset(&req
, 0, sizeof(struct addrinfo
));
1891 req
.ai_flags
= AI_PASSIVE
;
1892 req
.ai_family
= AF_UNSPEC
;
1893 req
.ai_socktype
= SOCK_STREAM
;
1894 sprintf(port_str
, "%d", port
);
1895 port_str
[sizeof(port_str
) - 1] = '\0';
1897 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1900 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1908 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1911 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1912 ainfo
->ai_protocol
);
1916 sockopt_v6only(ainfo
->ai_family
, sock
);
1917 sockopt_reuseaddr(sock
);
1918 sockopt_reuseport(sock
);
1921 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1923 close(sock
); /* Avoid sd leak. */
1927 ret
= listen(sock
, 3);
1929 close(sock
); /* Avoid sd leak. */
1933 vty_event(VTY_SERV
, sock
, NULL
);
1934 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1936 freeaddrinfo(ainfo_save
);
1940 /* For sockaddr_un. */
1943 /* VTY shell UNIX domain socket. */
1944 static void vty_serv_un(const char *path
)
1948 struct sockaddr_un serv
;
1950 struct zprivs_ids_t ids
;
1952 /* First of all, unlink existing socket */
1956 old_mask
= umask(0007);
1958 /* Make UNIX domain socket. */
1959 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1961 flog_err_sys(EC_LIB_SOCKET
,
1962 "Cannot create unix stream socket: %s",
1963 safe_strerror(errno
));
1967 /* Make server socket. */
1968 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1969 serv
.sun_family
= AF_UNIX
;
1970 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1971 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1972 len
= serv
.sun_len
= SUN_LEN(&serv
);
1974 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1975 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1979 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1981 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1982 safe_strerror(errno
));
1983 close(sock
); /* Avoid sd leak. */
1987 ret
= listen(sock
, 5);
1989 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1990 safe_strerror(errno
));
1991 close(sock
); /* Avoid sd leak. */
1997 zprivs_get_ids(&ids
);
1999 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2000 earlier for the case when we don't need to chown the file
2001 type casting it here to make a compare */
2002 if ((int)ids
.gid_vty
> 0) {
2003 /* set group of socket */
2004 if (chown(path
, -1, ids
.gid_vty
)) {
2005 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2006 "vty_serv_un: could chown socket, %s",
2007 safe_strerror(errno
));
2011 vty_event(VTYSH_SERV
, sock
, NULL
);
2014 /* #define VTYSH_DEBUG 1 */
2016 static int vtysh_accept(struct thread
*thread
)
2021 struct sockaddr_un client
;
2024 accept_sock
= THREAD_FD(thread
);
2026 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2028 memset(&client
, 0, sizeof(struct sockaddr_un
));
2029 client_len
= sizeof(struct sockaddr_un
);
2031 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2032 (socklen_t
*)&client_len
);
2035 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2036 safe_strerror(errno
));
2040 if (set_nonblocking(sock
) < 0) {
2043 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2044 sock
, safe_strerror(errno
));
2051 printf("VTY shell accept\n");
2052 #endif /* VTYSH_DEBUG */
2057 vty
->type
= VTY_SHELL_SERV
;
2058 vty
->node
= VIEW_NODE
;
2060 vty_event(VTYSH_READ
, sock
, vty
);
2065 static int vtysh_flush(struct vty
*vty
)
2067 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2068 case BUFFER_PENDING
:
2069 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2073 0; /* disable monitoring to avoid infinite recursion */
2074 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2076 buffer_reset(vty
->lbuf
);
2077 buffer_reset(vty
->obuf
);
2087 static int vtysh_read(struct thread
*thread
)
2093 unsigned char buf
[VTY_READ_BUFSIZ
];
2095 uint8_t header
[4] = {0, 0, 0, 0};
2097 sock
= THREAD_FD(thread
);
2098 vty
= THREAD_ARG(thread
);
2101 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2103 if (ERRNO_IO_RETRY(errno
)) {
2104 vty_event(VTYSH_READ
, sock
, vty
);
2107 vty
->monitor
= 0; /* disable monitoring to avoid
2108 infinite recursion */
2111 "%s: read failed on vtysh client fd %d, closing: %s",
2112 __func__
, sock
, safe_strerror(errno
));
2114 buffer_reset(vty
->lbuf
);
2115 buffer_reset(vty
->obuf
);
2118 printf("close vtysh\n");
2119 #endif /* VTYSH_DEBUG */
2124 printf("line: %.*s\n", nbytes
, buf
);
2125 #endif /* VTYSH_DEBUG */
2127 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2128 /* Clear command line buffer. */
2129 vty
->cp
= vty
->length
= 0;
2131 vty_out(vty
, "%% Command is too long.\n");
2133 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2134 vty
->buf
[vty
->length
++] = *p
;
2136 /* Pass this line to parser. */
2137 ret
= vty_execute(vty
);
2138 /* Note that vty_execute clears the command buffer and resets
2139 vty->length to 0. */
2141 /* Return result. */
2143 printf("result: %d\n", ret
);
2144 printf("vtysh node: %d\n", vty
->node
);
2145 #endif /* VTYSH_DEBUG */
2147 /* hack for asynchronous "write integrated"
2148 * - other commands in "buf" will be ditched
2149 * - input during pending config-write is
2151 if (ret
== CMD_SUSPEND
)
2154 /* warning: watchfrr hardcodes this result write
2157 buffer_put(vty
->obuf
, header
, 4);
2159 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2160 /* Try to flush results; exit if a write
2167 if (vty
->status
== VTY_CLOSE
)
2170 vty_event(VTYSH_READ
, sock
, vty
);
2175 static int vtysh_write(struct thread
*thread
)
2177 struct vty
*vty
= THREAD_ARG(thread
);
2179 vty
->t_write
= NULL
;
2186 /* Determine address family to bind. */
2187 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2189 /* If port is set to 0, do not listen on TCP/IP at all! */
2191 vty_serv_sock_addrinfo(addr
, port
);
2198 static void vty_error_delete(void *arg
)
2200 struct vty_error
*ve
= arg
;
2202 XFREE(MTYPE_TMP
, ve
);
2205 /* Close vty interface. Warning: call this only from functions that
2206 will be careful not to access the vty afterwards (since it has
2207 now been freed). This is safest from top-level functions (called
2208 directly by the thread dispatcher). */
2209 void vty_close(struct vty
*vty
)
2212 bool was_stdio
= false;
2214 /* Cancel threads.*/
2216 thread_cancel(vty
->t_read
);
2218 thread_cancel(vty
->t_write
);
2220 thread_cancel(vty
->t_timeout
);
2223 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2225 /* Free input buffer. */
2226 buffer_free(vty
->obuf
);
2227 buffer_free(vty
->lbuf
);
2229 /* Free command history. */
2230 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2231 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2236 vector_unset(vtyvec
, vty
->fd
);
2238 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2242 * note check is for fd > STDERR_FILENO, not fd != -1.
2243 * We never close stdin/stdout/stderr here, because we may be
2244 * running in foreground mode with logging to stdout. Also,
2245 * additionally, we'd need to replace these fds with /dev/null. */
2246 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2248 if (vty
->fd
> STDERR_FILENO
)
2250 if (vty
->fd
== STDIN_FILENO
)
2253 XFREE(MTYPE_VTY
, vty
->buf
);
2256 vty
->error
->del
= vty_error_delete
;
2257 list_delete(&vty
->error
);
2260 /* Check configure. */
2261 vty_config_exit(vty
);
2264 XFREE(MTYPE_VTY
, vty
);
2270 /* When time out occur output message then close connection. */
2271 static int vty_timeout(struct thread
*thread
)
2275 vty
= THREAD_ARG(thread
);
2276 vty
->t_timeout
= NULL
;
2280 buffer_reset(vty
->lbuf
);
2281 buffer_reset(vty
->obuf
);
2282 vty_out(vty
, "\nVty connection is timed out.\n");
2284 /* Close connection. */
2285 vty
->status
= VTY_CLOSE
;
2291 /* Read up configuration file from file_name. */
2292 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2296 struct vty_error
*ve
;
2297 struct listnode
*node
;
2298 unsigned int line_num
= 0;
2301 /* vty_close won't close stderr; if some config command prints
2302 * something it'll end up there. (not ideal; it'd be beter if output
2303 * from a file-load went to logging instead. Also note that if this
2304 * function is called after daemonizing, stderr will be /dev/null.)
2306 * vty->fd will be -1 from vty_new()
2308 vty
->wfd
= STDERR_FILENO
;
2309 vty
->type
= VTY_FILE
;
2310 vty
->node
= CONFIG_NODE
;
2312 vty
->candidate_config
= config
;
2314 vty
->private_config
= true;
2315 vty
->candidate_config
= nb_config_new(NULL
);
2318 /* Execute configuration file */
2319 ret
= config_from_file(vty
, confp
, &line_num
);
2321 /* Flush any previous errors before printing messages below */
2322 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2324 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2325 const char *message
= NULL
;
2329 case CMD_ERR_AMBIGUOUS
:
2330 message
= "Ambiguous command";
2332 case CMD_ERR_NO_MATCH
:
2333 message
= "No such command";
2336 message
= "Command returned Warning";
2338 case CMD_WARNING_CONFIG_FAILED
:
2339 message
= "Command returned Warning Config Failed";
2341 case CMD_ERR_INCOMPLETE
:
2342 message
= "Command returned Incomplete";
2344 case CMD_ERR_EXEED_ARGC_MAX
:
2346 "Command exceeded maximum number of Arguments";
2349 message
= "Command returned unhandled error message";
2353 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2354 nl
= strchr(ve
->error_buf
, '\n');
2357 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2358 message
, ve
->line_num
, ve
->error_buf
);
2363 * Automatically commit the candidate configuration after
2364 * reading the configuration file.
2366 if (config
== NULL
&& vty
->candidate_config
2367 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2368 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2369 vty
, true, "Read configuration file",
2371 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2372 zlog_err("%s: failed to read configuration file.",
2379 static FILE *vty_use_backup_config(const char *fullpath
)
2381 char *fullpath_sav
, *fullpath_tmp
;
2387 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2388 strcpy(fullpath_sav
, fullpath
);
2389 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2391 sav
= open(fullpath_sav
, O_RDONLY
);
2397 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2398 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2400 /* Open file to configuration write. */
2401 tmp
= mkstemp(fullpath_tmp
);
2405 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2408 while ((c
= read(sav
, buffer
, 512)) > 0) {
2409 if (write(tmp
, buffer
, c
) <= 0)
2415 if (rename(fullpath_tmp
, fullpath
) == 0)
2416 ret
= fopen(fullpath
, "r");
2418 unlink(fullpath_tmp
);
2423 unlink(fullpath_tmp
);
2433 /* Read up configuration file from file_name. */
2434 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2435 char *config_default_dir
)
2437 char cwd
[MAXPATHLEN
];
2439 const char *fullpath
;
2441 bool read_success
= false;
2443 /* If -f flag specified. */
2444 if (config_file
!= NULL
) {
2445 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2446 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2449 "%s: failure to determine Current Working Directory %d!",
2451 goto tmp_free_and_out
;
2453 tmp
= XMALLOC(MTYPE_TMP
,
2454 strlen(cwd
) + strlen(config_file
) + 2);
2455 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2458 fullpath
= config_file
;
2460 confp
= fopen(fullpath
, "r");
2462 if (confp
== NULL
) {
2464 EC_LIB_BACKUP_CONFIG
,
2465 "%s: failed to open configuration file %s: %s, checking backup",
2466 __func__
, fullpath
, safe_strerror(errno
));
2468 confp
= vty_use_backup_config(fullpath
);
2471 EC_LIB_BACKUP_CONFIG
,
2472 "WARNING: using backup configuration file!");
2476 "%s: can't open configuration file [%s]",
2477 __func__
, config_file
);
2478 goto tmp_free_and_out
;
2483 host_config_set(config_default_dir
);
2487 struct stat conf_stat
;
2489 /* !!!!PLEASE LEAVE!!!!
2490 * This is NEEDED for use with vtysh -b, or else you can get
2491 * a real configuration food fight with a lot garbage in the
2492 * merged configuration file it creates coming from the per
2493 * daemon configuration files. This also allows the daemons
2494 * to start if there default configuration file is not
2495 * present or ignore them, as needed when using vtysh -b to
2496 * configure the daemons at boot - MAG
2499 /* Stat for vtysh Zebra.conf, if found startup and wait for
2500 * boot configuration
2503 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2504 ret
= stat(integrate_default
, &conf_stat
);
2506 read_success
= true;
2507 goto tmp_free_and_out
;
2511 confp
= fopen(config_default_dir
, "r");
2512 if (confp
== NULL
) {
2515 "%s: failed to open configuration file %s: %s, checking backup",
2516 __func__
, config_default_dir
,
2517 safe_strerror(errno
));
2519 confp
= vty_use_backup_config(config_default_dir
);
2522 EC_LIB_BACKUP_CONFIG
,
2523 "WARNING: using backup configuration file!");
2524 fullpath
= config_default_dir
;
2526 flog_err(EC_LIB_VTY
,
2527 "can't open configuration file [%s]",
2528 config_default_dir
);
2529 goto tmp_free_and_out
;
2532 fullpath
= config_default_dir
;
2535 vty_read_file(config
, confp
);
2536 read_success
= true;
2540 host_config_set(fullpath
);
2543 XFREE(MTYPE_TMP
, tmp
);
2545 return read_success
;
2548 /* Small utility function which output log to the VTY. */
2549 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2550 struct timestamp_control
*ctl
, va_list va
)
2558 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2559 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2563 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2569 /* Async-signal-safe version of vty_log for fixed strings. */
2570 void vty_log_fixed(char *buf
, size_t len
)
2573 struct iovec iov
[2];
2574 char crlf
[4] = "\r\n";
2576 /* vty may not have been initialised */
2580 iov
[0].iov_base
= buf
;
2581 iov
[0].iov_len
= len
;
2582 iov
[1].iov_base
= crlf
;
2585 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2587 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2588 /* N.B. We don't care about the return code, since
2590 most likely just about to die anyway. */
2591 if (writev(vty
->wfd
, iov
, 2) == -1) {
2592 fprintf(stderr
, "Failure to writev: %d\n",
2599 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2601 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2602 vty_out(vty
, "%% Configuration is locked by other client\n");
2606 vty
->node
= CONFIG_NODE
;
2608 vty
->private_config
= private_config
;
2609 vty
->xpath_index
= 0;
2611 pthread_rwlock_rdlock(&running_config
->lock
);
2613 if (private_config
) {
2614 vty
->candidate_config
= nb_config_dup(running_config
);
2615 vty
->candidate_config_base
=
2616 nb_config_dup(running_config
);
2618 "Warning: uncommitted changes will be discarded on exit.\n\n");
2620 vty
->candidate_config
= vty_shared_candidate_config
;
2621 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2622 vty
->candidate_config_base
=
2623 nb_config_dup(running_config
);
2626 pthread_rwlock_unlock(&running_config
->lock
);
2631 void vty_config_exit(struct vty
*vty
)
2633 /* Check if there's a pending confirmed commit. */
2634 if (vty
->t_confirmed_commit_timeout
) {
2636 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2637 nb_cli_confirmed_commit_rollback(vty
);
2638 nb_cli_confirmed_commit_clean(vty
);
2641 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2643 if (vty
->candidate_config
) {
2644 if (vty
->private_config
)
2645 nb_config_free(vty
->candidate_config
);
2646 vty
->candidate_config
= NULL
;
2648 if (vty
->candidate_config_base
) {
2649 nb_config_free(vty
->candidate_config_base
);
2650 vty
->candidate_config_base
= NULL
;
2653 vty
->config
= false;
2656 /* Master of the threads. */
2657 static struct thread_master
*vty_master
;
2659 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2661 struct thread
*vty_serv_thread
= NULL
;
2665 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2667 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2671 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2673 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2677 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2681 vty
->t_write
= NULL
;
2682 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2688 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2690 /* Time out treatment. */
2691 if (vty
->v_timeout
) {
2693 thread_cancel(vty
->t_timeout
);
2694 vty
->t_timeout
= NULL
;
2695 thread_add_timer(vty_master
, vty_timeout
, vty
,
2696 vty
->v_timeout
, &vty
->t_timeout
);
2700 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2703 case VTY_TIMEOUT_RESET
:
2704 if (vty
->t_timeout
) {
2705 thread_cancel(vty
->t_timeout
);
2706 vty
->t_timeout
= NULL
;
2708 if (vty
->v_timeout
) {
2709 vty
->t_timeout
= NULL
;
2710 thread_add_timer(vty_master
, vty_timeout
, vty
,
2711 vty
->v_timeout
, &vty
->t_timeout
);
2717 DEFUN_NOSH (config_who
,
2720 "Display who is on vty\n")
2725 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2726 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2727 vty_out(vty
, "%svty[%d] connected from %s.\n",
2728 v
->config
? "*" : " ", i
, v
->address
);
2732 /* Move to vty configuration mode. */
2733 DEFUN_NOSH (line_vty
,
2736 "Configure a terminal line\n"
2737 "Virtual terminal\n")
2739 vty
->node
= VTY_NODE
;
2743 /* Set time out value. */
2744 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2745 const char *sec_str
)
2747 unsigned long timeout
= 0;
2749 /* min_str and sec_str are already checked by parser. So it must be
2750 all digit string. */
2752 timeout
= strtol(min_str
, NULL
, 10);
2756 timeout
+= strtol(sec_str
, NULL
, 10);
2758 vty_timeout_val
= timeout
;
2759 vty
->v_timeout
= timeout
;
2760 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2766 DEFUN (exec_timeout_min
,
2767 exec_timeout_min_cmd
,
2768 "exec-timeout (0-35791)",
2769 "Set timeout value\n"
2770 "Timeout value in minutes\n")
2773 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2776 DEFUN (exec_timeout_sec
,
2777 exec_timeout_sec_cmd
,
2778 "exec-timeout (0-35791) (0-2147483)",
2779 "Set the EXEC timeout\n"
2780 "Timeout in minutes\n"
2781 "Timeout in seconds\n")
2784 int idx_number_2
= 2;
2785 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2786 argv
[idx_number_2
]->arg
);
2789 DEFUN (no_exec_timeout
,
2790 no_exec_timeout_cmd
,
2793 "Set the EXEC timeout\n")
2795 return exec_timeout(vty
, NULL
, NULL
);
2798 /* Set vty access class. */
2799 DEFUN (vty_access_class
,
2800 vty_access_class_cmd
,
2801 "access-class WORD",
2802 "Filter connections based on an IP access list\n"
2806 if (vty_accesslist_name
)
2807 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2809 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2814 /* Clear vty access class. */
2815 DEFUN (no_vty_access_class
,
2816 no_vty_access_class_cmd
,
2817 "no access-class [WORD]",
2819 "Filter connections based on an IP access list\n"
2823 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2824 if (!vty_accesslist_name
2825 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2826 vty_out(vty
, "Access-class is not currently applied to vty\n");
2827 return CMD_WARNING_CONFIG_FAILED
;
2830 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2832 vty_accesslist_name
= NULL
;
2837 /* Set vty access class. */
2838 DEFUN (vty_ipv6_access_class
,
2839 vty_ipv6_access_class_cmd
,
2840 "ipv6 access-class WORD",
2842 "Filter connections based on an IP access list\n"
2843 "IPv6 access list\n")
2846 if (vty_ipv6_accesslist_name
)
2847 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2849 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2854 /* Clear vty access class. */
2855 DEFUN (no_vty_ipv6_access_class
,
2856 no_vty_ipv6_access_class_cmd
,
2857 "no ipv6 access-class [WORD]",
2860 "Filter connections based on an IP access list\n"
2861 "IPv6 access list\n")
2864 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2866 if (!vty_ipv6_accesslist_name
2867 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2869 "IPv6 access-class is not currently applied to vty\n");
2870 return CMD_WARNING_CONFIG_FAILED
;
2873 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2875 vty_ipv6_accesslist_name
= NULL
;
2884 "Enable password checking\n")
2886 no_password_check
= 0;
2890 DEFUN (no_vty_login
,
2894 "Enable password checking\n")
2896 no_password_check
= 1;
2900 DEFUN (service_advanced_vty
,
2901 service_advanced_vty_cmd
,
2902 "service advanced-vty",
2903 "Set up miscellaneous service\n"
2904 "Enable advanced mode vty interface\n")
2910 DEFUN (no_service_advanced_vty
,
2911 no_service_advanced_vty_cmd
,
2912 "no service advanced-vty",
2914 "Set up miscellaneous service\n"
2915 "Enable advanced mode vty interface\n")
2921 DEFUN_NOSH (terminal_monitor
,
2922 terminal_monitor_cmd
,
2924 "Set terminal line parameters\n"
2925 "Copy debug output to the current terminal line\n")
2931 DEFUN_NOSH (terminal_no_monitor
,
2932 terminal_no_monitor_cmd
,
2933 "terminal no monitor",
2934 "Set terminal line parameters\n"
2936 "Copy debug output to the current terminal line\n")
2942 DEFUN_NOSH (no_terminal_monitor
,
2943 no_terminal_monitor_cmd
,
2944 "no terminal monitor",
2946 "Set terminal line parameters\n"
2947 "Copy debug output to the current terminal line\n")
2949 return terminal_no_monitor(self
, vty
, argc
, argv
);
2953 DEFUN_NOSH (show_history
,
2957 "Display the session command history\n")
2961 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2962 if (index
== VTY_MAXHIST
) {
2967 if (vty
->hist
[index
] != NULL
)
2968 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2977 DEFUN (log_commands
,
2981 "Log all commands (can't be unset without restart)\n")
2983 do_log_commands
= 1;
2987 /* Display current configuration. */
2988 static int vty_config_write(struct vty
*vty
)
2990 vty_out(vty
, "line vty\n");
2992 if (vty_accesslist_name
)
2993 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2995 if (vty_ipv6_accesslist_name
)
2996 vty_out(vty
, " ipv6 access-class %s\n",
2997 vty_ipv6_accesslist_name
);
3000 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3001 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3002 vty_timeout_val
% 60);
3005 if (no_password_check
)
3006 vty_out(vty
, " no login\n");
3008 if (do_log_commands
)
3009 vty_out(vty
, "log commands\n");
3011 vty_out(vty
, "!\n");
3016 struct cmd_node vty_node
= {
3017 VTY_NODE
, "%s(config-line)# ", 1,
3020 /* Reset all VTY status. */
3021 void vty_reset(void)
3025 struct thread
*vty_serv_thread
;
3027 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3028 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3029 buffer_reset(vty
->lbuf
);
3030 buffer_reset(vty
->obuf
);
3031 vty
->status
= VTY_CLOSE
;
3035 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3036 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3038 thread_cancel(vty_serv_thread
);
3039 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3043 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3045 if (vty_accesslist_name
) {
3046 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3047 vty_accesslist_name
= NULL
;
3050 if (vty_ipv6_accesslist_name
) {
3051 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3052 vty_ipv6_accesslist_name
= NULL
;
3056 static void vty_save_cwd(void)
3058 char cwd
[MAXPATHLEN
];
3061 c
= getcwd(cwd
, MAXPATHLEN
);
3065 * At this point if these go wrong, more than likely
3066 * the whole world is coming down around us
3067 * Hence not worrying about it too much.
3069 if (!chdir(SYSCONFDIR
)) {
3070 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3071 "Failure to chdir to %s, errno: %d",
3075 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3076 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3077 "Failure to getcwd, errno: %d", errno
);
3082 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3083 strcpy(vty_cwd
, cwd
);
3086 char *vty_get_cwd(void)
3091 int vty_shell(struct vty
*vty
)
3093 return vty
->type
== VTY_SHELL
? 1 : 0;
3096 int vty_shell_serv(struct vty
*vty
)
3098 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3101 void vty_init_vtysh(void)
3103 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3106 /* Install vty's own commands like `who' command. */
3107 void vty_init(struct thread_master
*master_thread
)
3109 /* For further configuration read, preserve current directory. */
3112 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3114 vty_master
= master_thread
;
3116 atexit(vty_stdio_atexit
);
3118 /* Initilize server thread vector. */
3119 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3121 /* Install bgp top node. */
3122 install_node(&vty_node
, vty_config_write
);
3124 install_element(VIEW_NODE
, &config_who_cmd
);
3125 install_element(VIEW_NODE
, &show_history_cmd
);
3126 install_element(CONFIG_NODE
, &line_vty_cmd
);
3127 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3128 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3129 install_element(CONFIG_NODE
, &show_history_cmd
);
3130 install_element(CONFIG_NODE
, &log_commands_cmd
);
3131 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3132 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3133 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3135 install_default(VTY_NODE
);
3136 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3137 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3138 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3139 install_element(VTY_NODE
, &vty_access_class_cmd
);
3140 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3141 install_element(VTY_NODE
, &vty_login_cmd
);
3142 install_element(VTY_NODE
, &no_vty_login_cmd
);
3143 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3144 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3147 void vty_terminate(void)
3149 XFREE(MTYPE_TMP
, vty_cwd
);
3151 if (vtyvec
&& Vvty_serv_thread
) {
3153 vector_free(vtyvec
);
3154 vector_free(Vvty_serv_thread
);
3156 Vvty_serv_thread
= NULL
;