2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
25 #include <sys/types.h>
33 #include "sockunion.h"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
46 #include <arpa/telnet.h>
49 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
50 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
51 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
66 static void vty_event(enum event
, int, struct vty
*);
68 /* Extern host structure from command.c */
69 extern struct host host
;
71 /* Vector which store each vty structure. */
74 /* Vty timeout value. */
75 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
77 /* Vty access-class command */
78 static char *vty_accesslist_name
= NULL
;
80 /* Vty access-calss for IPv6. */
81 static char *vty_ipv6_accesslist_name
= NULL
;
83 /* VTY server thread. */
84 static vector Vvty_serv_thread
;
86 /* Current directory. */
89 /* Exclusive configuration lock. */
90 struct vty
*vty_exclusive_lock
;
92 /* Login password check. */
93 static int no_password_check
= 0;
95 /* Integrated configuration file path */
96 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
98 static int do_log_commands
= 0;
100 void vty_frame(struct vty
*vty
, const char *format
, ...)
104 va_start(args
, format
);
105 vsnprintf(vty
->frame
+ vty
->frame_pos
,
106 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
107 vty
->frame_pos
= strlen(vty
->frame
);
111 void vty_endframe(struct vty
*vty
, const char *endtext
)
113 if (vty
->frame_pos
== 0 && endtext
)
114 vty_out(vty
, "%s", endtext
);
118 bool vty_set_include(struct vty
*vty
, const char *regexp
)
126 regfree(&vty
->include
);
132 errcode
= regcomp(&vty
->include
, regexp
,
133 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
136 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
137 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
145 /* VTY standard output function. */
146 int vty_out(struct vty
*vty
, const char *format
, ...)
155 if (vty
->frame_pos
) {
157 vty_out(vty
, "%s", vty
->frame
);
160 /* Try to write to initial buffer. */
161 va_start(args
, format
);
162 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
165 /* Initial buffer is not enough. */
166 if (len
< 0 || len
>= size
) {
173 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
177 va_start(args
, format
);
178 len
= vsnprintf(p
, size
, format
, args
);
181 if (len
> -1 && len
< size
)
186 /* When initial buffer is enough to store all output. */
192 vector lines
= frrstr_split_vec(p
, "\n");
194 /* Place first value in the cache */
195 char *firstline
= vector_slot(lines
, 0);
196 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
198 /* If our split returned more than one entry, time to filter */
199 if (vector_active(lines
) > 1) {
201 * returned string is MTYPE_TMP so it matches the MTYPE
202 * of everything else in the vector
204 char *bstr
= buffer_getstr(vty
->lbuf
);
205 buffer_reset(vty
->lbuf
);
206 XFREE(MTYPE_TMP
, lines
->index
[0]);
207 vector_set_index(lines
, 0, bstr
);
208 frrstr_filter_vec(lines
, &vty
->include
);
209 vector_compact(lines
);
211 * Consider the string "foo\n". If the regex is an empty string
212 * and the line ended with a newline, then the vector will look
218 * If the regex isn't empty, the vector will look like:
222 * In this case we'd like to preserve the newline, so we add
223 * the empty string [1] as in the first example.
225 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
226 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
227 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
229 filtered
= frrstr_join_vec(lines
, "\n");
235 frrstr_strvec_free(lines
);
246 /* print with crlf replacement */
247 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
251 fprintf(vty
->of
, "%s", filtered
);
257 /* print without crlf replacement */
258 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
264 if (vty
->filter
&& filtered
)
265 XFREE(MTYPE_TMP
, filtered
);
267 /* If p is not different with buf, it is allocated buffer. */
269 XFREE(MTYPE_VTY_OUT_BUF
, p
);
274 static int vty_log_out(struct vty
*vty
, const char *level
,
275 const char *proto_str
, const char *format
,
276 struct timestamp_control
*ctl
, va_list va
)
282 if (!ctl
->already_rendered
) {
283 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
285 ctl
->already_rendered
= 1;
287 if (ctl
->len
+ 1 >= sizeof(buf
))
289 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
294 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
297 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
298 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
301 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
302 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
308 if (write(vty
->wfd
, buf
, len
) < 0) {
309 if (ERRNO_IO_RETRY(errno
))
310 /* Kernel buffer is full, probably too much debugging
312 drop the data and ignore. */
314 /* Fatal I/O error. */
316 0; /* disable monitoring to avoid infinite recursion */
317 flog_err(EC_LIB_SOCKET
,
318 "%s: write failed to vty client fd %d, closing: %s",
319 __func__
, vty
->fd
, safe_strerror(errno
));
320 buffer_reset(vty
->obuf
);
321 buffer_reset(vty
->lbuf
);
322 /* cannot call vty_close, because a parent routine may still try
323 to access the vty struct */
324 vty
->status
= VTY_CLOSE
;
325 shutdown(vty
->fd
, SHUT_RDWR
);
331 /* Output current time to the vty. */
332 void vty_time_print(struct vty
*vty
, int cr
)
334 char buf
[QUAGGA_TIMESTAMP_LEN
];
336 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
337 zlog_info("quagga_timestamp error");
341 vty_out(vty
, "%s\n", buf
);
343 vty_out(vty
, "%s ", buf
);
348 /* Say hello to vty interface. */
349 void vty_hello(struct vty
*vty
)
355 f
= fopen(host
.motdfile
, "r");
357 while (fgets(buf
, sizeof(buf
), f
)) {
359 /* work backwards to ignore trailling isspace()
361 for (s
= buf
+ strlen(buf
);
362 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
365 vty_out(vty
, "%s\n", buf
);
369 vty_out(vty
, "MOTD file not found\n");
370 } else if (host
.motd
)
371 vty_out(vty
, "%s", host
.motd
);
374 /* Put out prompt and wait input from user. */
375 static void vty_prompt(struct vty
*vty
)
377 if (vty
->type
== VTY_TERM
) {
378 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
382 /* Send WILL TELOPT_ECHO to remote server. */
383 static void vty_will_echo(struct vty
*vty
)
385 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
386 vty_out(vty
, "%s", cmd
);
389 /* Make suppress Go-Ahead telnet option. */
390 static void vty_will_suppress_go_ahead(struct vty
*vty
)
392 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
393 vty_out(vty
, "%s", cmd
);
396 /* Make don't use linemode over telnet. */
397 static void vty_dont_linemode(struct vty
*vty
)
399 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
400 vty_out(vty
, "%s", cmd
);
403 /* Use window size. */
404 static void vty_do_window_size(struct vty
*vty
)
406 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
407 vty_out(vty
, "%s", cmd
);
410 #if 0 /* Currently not used. */
411 /* Make don't use lflow vty interface. */
413 vty_dont_lflow_ahead (struct vty
*vty
)
415 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
416 vty_out (vty
, "%s", cmd
);
420 /* Authentication of vty */
421 static void vty_auth(struct vty
*vty
, char *buf
)
424 enum node_type next_node
= 0;
426 char *crypt(const char *, const char *);
431 passwd
= host
.password_encrypt
;
433 passwd
= host
.password
;
435 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
437 next_node
= VIEW_NODE
;
439 case AUTH_ENABLE_NODE
:
441 passwd
= host
.enable_encrypt
;
443 passwd
= host
.enable
;
444 next_node
= ENABLE_NODE
;
450 fail
= strcmp(crypt(buf
, passwd
), passwd
);
452 fail
= strcmp(buf
, passwd
);
458 vty
->node
= next_node
; /* Success ! */
461 if (vty
->fail
>= 3) {
462 if (vty
->node
== AUTH_NODE
) {
464 "%% Bad passwords, too many failures!\n");
465 vty
->status
= VTY_CLOSE
;
467 /* AUTH_ENABLE_NODE */
470 "%% Bad enable passwords, too many failures!\n");
471 vty
->status
= VTY_CLOSE
;
477 /* Command execution over the vty interface. */
478 static int vty_command(struct vty
*vty
, char *buf
)
481 const char *protocolname
;
487 * Log non empty command lines
492 /* Skip white spaces. */
493 while (isspace((int)*cp
) && *cp
!= '\0')
496 if (cp
!= NULL
&& *cp
!= '\0') {
498 char vty_str
[VTY_BUFSIZ
];
499 char prompt_str
[VTY_BUFSIZ
];
501 /* format the base vty info */
502 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
504 for (i
= 0; i
< vector_active(vtyvec
); i
++)
505 if (vty
== vector_slot(vtyvec
, i
)) {
506 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
511 /* format the prompt */
512 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
515 /* now log the command */
516 zlog_notice("%s%s", prompt_str
, buf
);
519 #ifdef CONSUMED_TIME_CHECK
523 unsigned long realtime
, cputime
;
526 #endif /* CONSUMED_TIME_CHECK */
528 ret
= cmd_execute(vty
, buf
, NULL
, 0);
530 /* Get the name of the protocol if any */
531 protocolname
= frr_protoname
;
533 #ifdef CONSUMED_TIME_CHECK
535 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
536 > CONSUMED_TIME_CHECK
)
537 /* Warn about CPU hog that must be fixed. */
540 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
541 realtime
/ 1000, cputime
/ 1000, buf
);
543 #endif /* CONSUMED_TIME_CHECK */
545 if (ret
!= CMD_SUCCESS
)
548 if (vty
->type
== VTY_FILE
)
549 vty_out(vty
, "Warning...\n");
551 case CMD_ERR_AMBIGUOUS
:
552 vty_out(vty
, "%% Ambiguous command.\n");
554 case CMD_ERR_NO_MATCH
:
555 vty_out(vty
, "%% [%s] Unknown command: %s\n",
558 case CMD_ERR_INCOMPLETE
:
559 vty_out(vty
, "%% Command incomplete.\n");
566 static const char telnet_backward_char
= 0x08;
567 static const char telnet_space_char
= ' ';
569 /* Basic function to write buffer to vty. */
570 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
572 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
575 /* Should we do buffering here ? And make vty_flush (vty) ? */
576 buffer_put(vty
->obuf
, buf
, nbytes
);
579 /* Basic function to insert character into vty. */
580 static void vty_self_insert(struct vty
*vty
, char c
)
585 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
588 length
= vty
->length
- vty
->cp
;
589 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
590 vty
->buf
[vty
->cp
] = c
;
592 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
593 for (i
= 0; i
< length
; i
++)
594 vty_write(vty
, &telnet_backward_char
, 1);
599 vty
->buf
[vty
->length
] = '\0';
602 /* Self insert character 'c' in overwrite mode. */
603 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
605 if (vty
->cp
== vty
->length
) {
606 vty_self_insert(vty
, c
);
610 vty
->buf
[vty
->cp
++] = c
;
611 vty_write(vty
, &c
, 1);
615 * Insert a string into vty->buf at the current cursor position.
617 * If the resultant string would be larger than VTY_BUFSIZ it is
620 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
622 if (vty
->cp
== VTY_BUFSIZ
)
625 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
626 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
628 vty
->length
= MAX(vty
->cp
, vty
->length
);
629 vty
->buf
[vty
->length
] = '\0';
630 vty_write(vty
, str
, nwrite
);
633 /* Forward character. */
634 static void vty_forward_char(struct vty
*vty
)
636 if (vty
->cp
< vty
->length
) {
637 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
642 /* Backward character. */
643 static void vty_backward_char(struct vty
*vty
)
647 vty_write(vty
, &telnet_backward_char
, 1);
651 /* Move to the beginning of the line. */
652 static void vty_beginning_of_line(struct vty
*vty
)
655 vty_backward_char(vty
);
658 /* Move to the end of the line. */
659 static void vty_end_of_line(struct vty
*vty
)
661 while (vty
->cp
< vty
->length
)
662 vty_forward_char(vty
);
665 static void vty_kill_line_from_beginning(struct vty
*);
666 static void vty_redraw_line(struct vty
*);
668 /* Print command line history. This function is called from
669 vty_next_line and vty_previous_line. */
670 static void vty_history_print(struct vty
*vty
)
674 vty_kill_line_from_beginning(vty
);
676 /* Get previous line from history buffer */
677 length
= strlen(vty
->hist
[vty
->hp
]);
678 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
679 vty
->cp
= vty
->length
= length
;
680 vty
->buf
[vty
->length
] = '\0';
682 /* Redraw current line */
683 vty_redraw_line(vty
);
686 /* Show next command line history. */
687 static void vty_next_line(struct vty
*vty
)
691 if (vty
->hp
== vty
->hindex
)
694 /* Try is there history exist or not. */
696 if (try_index
== (VTY_MAXHIST
- 1))
701 /* If there is not history return. */
702 if (vty
->hist
[try_index
] == NULL
)
707 vty_history_print(vty
);
710 /* Show previous command line history. */
711 static void vty_previous_line(struct vty
*vty
)
717 try_index
= VTY_MAXHIST
- 1;
721 if (vty
->hist
[try_index
] == NULL
)
726 vty_history_print(vty
);
729 /* This function redraw all of the command line character. */
730 static void vty_redraw_line(struct vty
*vty
)
732 vty_write(vty
, vty
->buf
, vty
->length
);
733 vty
->cp
= vty
->length
;
737 static void vty_forward_word(struct vty
*vty
)
739 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
740 vty_forward_char(vty
);
742 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
743 vty_forward_char(vty
);
746 /* Backward word without skipping training space. */
747 static void vty_backward_pure_word(struct vty
*vty
)
749 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
750 vty_backward_char(vty
);
754 static void vty_backward_word(struct vty
*vty
)
756 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
757 vty_backward_char(vty
);
759 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
760 vty_backward_char(vty
);
763 /* When '^D' is typed at the beginning of the line we move to the down
765 static void vty_down_level(struct vty
*vty
)
773 /* When '^Z' is received from vty, move down to the enable mode. */
774 static void vty_end_config(struct vty
*vty
)
779 vty_config_exit(vty
);
780 vty
->node
= ENABLE_NODE
;
787 /* Delete a charcter at the current point. */
788 static void vty_delete_char(struct vty
*vty
)
793 if (vty
->length
== 0) {
798 if (vty
->cp
== vty
->length
)
799 return; /* completion need here? */
801 size
= vty
->length
- vty
->cp
;
804 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
805 vty
->buf
[vty
->length
] = '\0';
807 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
810 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
811 vty_write(vty
, &telnet_space_char
, 1);
813 for (i
= 0; i
< size
; i
++)
814 vty_write(vty
, &telnet_backward_char
, 1);
817 /* Delete a character before the point. */
818 static void vty_delete_backward_char(struct vty
*vty
)
823 vty_backward_char(vty
);
824 vty_delete_char(vty
);
827 /* Kill rest of line from current point. */
828 static void vty_kill_line(struct vty
*vty
)
833 size
= vty
->length
- vty
->cp
;
838 for (i
= 0; i
< size
; i
++)
839 vty_write(vty
, &telnet_space_char
, 1);
840 for (i
= 0; i
< size
; i
++)
841 vty_write(vty
, &telnet_backward_char
, 1);
843 memset(&vty
->buf
[vty
->cp
], 0, size
);
844 vty
->length
= vty
->cp
;
847 /* Kill line from the beginning. */
848 static void vty_kill_line_from_beginning(struct vty
*vty
)
850 vty_beginning_of_line(vty
);
854 /* Delete a word before the point. */
855 static void vty_forward_kill_word(struct vty
*vty
)
857 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
858 vty_delete_char(vty
);
859 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
860 vty_delete_char(vty
);
863 /* Delete a word before the point. */
864 static void vty_backward_kill_word(struct vty
*vty
)
866 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
867 vty_delete_backward_char(vty
);
868 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
869 vty_delete_backward_char(vty
);
872 /* Transpose chars before or at the point. */
873 static void vty_transpose_chars(struct vty
*vty
)
877 /* If length is short or point is near by the beginning of line then
879 if (vty
->length
< 2 || vty
->cp
< 1)
882 /* In case of point is located at the end of the line. */
883 if (vty
->cp
== vty
->length
) {
884 c1
= vty
->buf
[vty
->cp
- 1];
885 c2
= vty
->buf
[vty
->cp
- 2];
887 vty_backward_char(vty
);
888 vty_backward_char(vty
);
889 vty_self_insert_overwrite(vty
, c1
);
890 vty_self_insert_overwrite(vty
, c2
);
892 c1
= vty
->buf
[vty
->cp
];
893 c2
= vty
->buf
[vty
->cp
- 1];
895 vty_backward_char(vty
);
896 vty_self_insert_overwrite(vty
, c1
);
897 vty_self_insert_overwrite(vty
, c2
);
901 /* Do completion at vty interface. */
902 static void vty_complete_command(struct vty
*vty
)
906 char **matched
= NULL
;
909 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
912 vline
= cmd_make_strvec(vty
->buf
);
916 /* In case of 'help \t'. */
917 if (isspace((int)vty
->buf
[vty
->length
- 1]))
918 vector_set(vline
, NULL
);
920 matched
= cmd_complete_command(vline
, vty
, &ret
);
922 cmd_free_strvec(vline
);
926 case CMD_ERR_AMBIGUOUS
:
927 vty_out(vty
, "%% Ambiguous command.\n");
929 vty_redraw_line(vty
);
931 case CMD_ERR_NO_MATCH
:
932 /* vty_out (vty, "%% There is no matched command.\n"); */
934 vty_redraw_line(vty
);
936 case CMD_COMPLETE_FULL_MATCH
:
938 /* 2016-11-28 equinox -- need to debug, SEGV here */
939 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
941 vty_redraw_line(vty
);
945 vty_redraw_line(vty
);
946 vty_backward_pure_word(vty
);
947 vty_insert_word_overwrite(vty
, matched
[0]);
948 vty_self_insert(vty
, ' ');
949 XFREE(MTYPE_COMPLETION
, matched
[0]);
951 case CMD_COMPLETE_MATCH
:
953 vty_redraw_line(vty
);
954 vty_backward_pure_word(vty
);
955 vty_insert_word_overwrite(vty
, matched
[0]);
956 XFREE(MTYPE_COMPLETION
, matched
[0]);
958 case CMD_COMPLETE_LIST_MATCH
:
959 for (i
= 0; matched
[i
] != NULL
; i
++) {
960 if (i
!= 0 && ((i
% 6) == 0))
962 vty_out(vty
, "%-10s ", matched
[i
]);
963 XFREE(MTYPE_COMPLETION
, matched
[i
]);
968 vty_redraw_line(vty
);
970 case CMD_ERR_NOTHING_TODO
:
972 vty_redraw_line(vty
);
978 XFREE(MTYPE_TMP
, matched
);
981 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
982 unsigned int desc_width
, struct cmd_token
*token
)
990 if (desc_width
<= 0) {
991 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
995 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
997 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
998 for (pos
= desc_width
; pos
> 0; pos
--)
999 if (*(p
+ pos
) == ' ')
1005 strncpy(buf
, p
, pos
);
1007 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1012 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1014 XFREE(MTYPE_TMP
, buf
);
1017 /* Describe matched command function. */
1018 static void vty_describe_command(struct vty
*vty
)
1023 unsigned int i
, width
, desc_width
;
1024 struct cmd_token
*token
, *token_cr
= NULL
;
1026 vline
= cmd_make_strvec(vty
->buf
);
1028 /* In case of '> ?'. */
1029 if (vline
== NULL
) {
1030 vline
= vector_init(1);
1031 vector_set(vline
, NULL
);
1032 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1033 vector_set(vline
, NULL
);
1035 describe
= cmd_describe_command(vline
, vty
, &ret
);
1039 /* Ambiguous error. */
1041 case CMD_ERR_AMBIGUOUS
:
1042 vty_out(vty
, "%% Ambiguous command.\n");
1045 case CMD_ERR_NO_MATCH
:
1046 vty_out(vty
, "%% There is no matched command.\n");
1051 /* Get width of command string. */
1053 for (i
= 0; i
< vector_active(describe
); i
++)
1054 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1057 if (token
->text
[0] == '\0')
1060 len
= strlen(token
->text
);
1066 /* Get width of description string. */
1067 desc_width
= vty
->width
- (width
+ 6);
1069 /* Print out description. */
1070 for (i
= 0; i
< vector_active(describe
); i
++)
1071 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1072 if (token
->text
[0] == '\0')
1075 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1081 vty_out(vty
, " %-s\n", token
->text
);
1082 else if (desc_width
>= strlen(token
->desc
))
1083 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1086 vty_describe_fold(vty
, width
, desc_width
,
1089 if (IS_VARYING_TOKEN(token
->type
)) {
1090 const char *ref
= vector_slot(
1091 vline
, vector_active(vline
) - 1);
1093 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1094 cmd_variable_complete(token
, ref
, varcomps
);
1096 if (vector_active(varcomps
) > 0) {
1097 char *ac
= cmd_variable_comp2str(
1098 varcomps
, vty
->width
);
1099 vty_out(vty
, "%s\n", ac
);
1100 XFREE(MTYPE_TMP
, ac
);
1103 vector_free(varcomps
);
1106 vty_out (vty
, " %-*s %s\n", width
1107 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1108 desc
->str
? desc
->str
: "");
1112 if ((token
= token_cr
)) {
1114 vty_out(vty
, " %-s\n", token
->text
);
1115 else if (desc_width
>= strlen(token
->desc
))
1116 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1119 vty_describe_fold(vty
, width
, desc_width
, token
);
1123 cmd_free_strvec(vline
);
1125 vector_free(describe
);
1128 vty_redraw_line(vty
);
1131 static void vty_clear_buf(struct vty
*vty
)
1133 memset(vty
->buf
, 0, vty
->max
);
1136 /* ^C stop current input and do not add command line to the history. */
1137 static void vty_stop_input(struct vty
*vty
)
1139 vty
->cp
= vty
->length
= 0;
1144 vty_config_exit(vty
);
1145 vty
->node
= ENABLE_NODE
;
1150 /* Set history pointer to the latest one. */
1151 vty
->hp
= vty
->hindex
;
1154 /* Add current command line to the history buffer. */
1155 static void vty_hist_add(struct vty
*vty
)
1159 if (vty
->length
== 0)
1162 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1164 /* Ignore the same string as previous one. */
1165 if (vty
->hist
[index
])
1166 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1167 vty
->hp
= vty
->hindex
;
1171 /* Insert history entry. */
1172 if (vty
->hist
[vty
->hindex
])
1173 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1174 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1176 /* History index rotation. */
1178 if (vty
->hindex
== VTY_MAXHIST
)
1181 vty
->hp
= vty
->hindex
;
1184 /* #define TELNET_OPTION_DEBUG */
1186 /* Get telnet window size. */
1187 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1189 #ifdef TELNET_OPTION_DEBUG
1192 for (i
= 0; i
< nbytes
; i
++) {
1195 vty_out(vty
, "IAC ");
1198 vty_out(vty
, "WILL ");
1201 vty_out(vty
, "WONT ");
1204 vty_out(vty
, "DO ");
1207 vty_out(vty
, "DONT ");
1210 vty_out(vty
, "SB ");
1213 vty_out(vty
, "SE ");
1216 vty_out(vty
, "TELOPT_ECHO \n");
1219 vty_out(vty
, "TELOPT_SGA \n");
1222 vty_out(vty
, "TELOPT_NAWS \n");
1225 vty_out(vty
, "%x ", buf
[i
]);
1231 #endif /* TELNET_OPTION_DEBUG */
1236 vty
->iac_sb_in_progress
= 1;
1240 if (!vty
->iac_sb_in_progress
)
1243 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1244 vty
->iac_sb_in_progress
= 0;
1247 switch (vty
->sb_buf
[0]) {
1249 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1252 "RFC 1073 violation detected: telnet NAWS option "
1253 "should send %d characters, but we received %lu",
1255 (unsigned long)vty
->sb_len
);
1256 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1259 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1260 (unsigned long)sizeof(vty
->sb_buf
),
1261 TELNET_NAWS_SB_LEN
);
1263 vty
->width
= ((vty
->sb_buf
[1] << 8)
1265 vty
->height
= ((vty
->sb_buf
[3] << 8)
1267 #ifdef TELNET_OPTION_DEBUG
1269 "TELNET NAWS window size negotiation completed: "
1270 "width %d, height %d\n",
1271 vty
->width
, vty
->height
);
1276 vty
->iac_sb_in_progress
= 0;
1286 /* Execute current command line. */
1287 static int vty_execute(struct vty
*vty
)
1293 switch (vty
->node
) {
1295 case AUTH_ENABLE_NODE
:
1296 vty_auth(vty
, vty
->buf
);
1299 ret
= vty_command(vty
, vty
->buf
);
1300 if (vty
->type
== VTY_TERM
)
1305 /* Clear command line buffer. */
1306 vty
->cp
= vty
->length
= 0;
1309 if (vty
->status
!= VTY_CLOSE
)
1315 #define CONTROL(X) ((X) - '@')
1316 #define VTY_NORMAL 0
1317 #define VTY_PRE_ESCAPE 1
1318 #define VTY_ESCAPE 2
1320 /* Escape character command map. */
1321 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1325 vty_previous_line(vty
);
1331 vty_forward_char(vty
);
1334 vty_backward_char(vty
);
1340 /* Go back to normal mode. */
1341 vty
->escape
= VTY_NORMAL
;
1344 /* Quit print out to the buffer. */
1345 static void vty_buffer_reset(struct vty
*vty
)
1347 buffer_reset(vty
->obuf
);
1348 buffer_reset(vty
->lbuf
);
1350 vty_redraw_line(vty
);
1353 /* Read data via vty socket. */
1354 static int vty_read(struct thread
*thread
)
1358 unsigned char buf
[VTY_READ_BUFSIZ
];
1360 int vty_sock
= THREAD_FD(thread
);
1361 struct vty
*vty
= THREAD_ARG(thread
);
1364 /* Read raw data from socket */
1365 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1367 if (ERRNO_IO_RETRY(errno
)) {
1368 vty_event(VTY_READ
, vty_sock
, vty
);
1371 vty
->monitor
= 0; /* disable monitoring to avoid
1372 infinite recursion */
1375 "%s: read error on vty client fd %d, closing: %s",
1376 __func__
, vty
->fd
, safe_strerror(errno
));
1377 buffer_reset(vty
->obuf
);
1378 buffer_reset(vty
->lbuf
);
1380 vty
->status
= VTY_CLOSE
;
1383 for (i
= 0; i
< nbytes
; i
++) {
1384 if (buf
[i
] == IAC
) {
1393 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1394 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1395 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1401 /* In case of telnet command */
1403 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1410 if (vty
->status
== VTY_MORE
) {
1415 vty_buffer_reset(vty
);
1417 #if 0 /* More line does not work for "show ip bgp". */
1420 vty
->status
= VTY_MORELINE
;
1429 /* Escape character. */
1430 if (vty
->escape
== VTY_ESCAPE
) {
1431 vty_escape_map(buf
[i
], vty
);
1435 /* Pre-escape status. */
1436 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1439 vty
->escape
= VTY_ESCAPE
;
1442 vty_backward_word(vty
);
1443 vty
->escape
= VTY_NORMAL
;
1446 vty_forward_word(vty
);
1447 vty
->escape
= VTY_NORMAL
;
1450 vty_forward_kill_word(vty
);
1451 vty
->escape
= VTY_NORMAL
;
1455 vty_backward_kill_word(vty
);
1456 vty
->escape
= VTY_NORMAL
;
1459 vty
->escape
= VTY_NORMAL
;
1467 vty_beginning_of_line(vty
);
1470 vty_backward_char(vty
);
1473 vty_stop_input(vty
);
1476 vty_delete_char(vty
);
1479 vty_end_of_line(vty
);
1482 vty_forward_char(vty
);
1486 vty_delete_backward_char(vty
);
1495 vty_previous_line(vty
);
1498 vty_transpose_chars(vty
);
1501 vty_kill_line_from_beginning(vty
);
1504 vty_backward_kill_word(vty
);
1507 vty_end_config(vty
);
1515 vty_complete_command(vty
);
1518 if (vty
->node
== AUTH_NODE
1519 || vty
->node
== AUTH_ENABLE_NODE
)
1520 vty_self_insert(vty
, buf
[i
]);
1522 vty_describe_command(vty
);
1525 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1526 vty
->escape
= VTY_ESCAPE
;
1529 vty
->escape
= VTY_PRE_ESCAPE
;
1532 if (buf
[i
] > 31 && buf
[i
] < 127)
1533 vty_self_insert(vty
, buf
[i
]);
1539 if (vty
->status
== VTY_CLOSE
)
1542 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1543 vty_event(VTY_READ
, vty_sock
, vty
);
1548 /* Flush buffer to the vty. */
1549 static int vty_flush(struct thread
*thread
)
1552 buffer_status_t flushrc
;
1553 int vty_sock
= THREAD_FD(thread
);
1554 struct vty
*vty
= THREAD_ARG(thread
);
1556 vty
->t_write
= NULL
;
1558 /* Tempolary disable read thread. */
1559 if ((vty
->lines
== 0) && vty
->t_read
) {
1560 thread_cancel(vty
->t_read
);
1564 /* Function execution continue. */
1565 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1567 /* N.B. if width is 0, that means we don't know the window size. */
1568 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1569 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1570 else if (vty
->status
== VTY_MORELINE
)
1571 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1574 flushrc
= buffer_flush_window(
1575 vty
->obuf
, vty_sock
, vty
->width
,
1576 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1580 0; /* disable monitoring to avoid infinite recursion */
1581 zlog_info("buffer_flush failed on vty client fd %d, closing",
1583 buffer_reset(vty
->lbuf
);
1584 buffer_reset(vty
->obuf
);
1588 if (vty
->status
== VTY_CLOSE
)
1591 vty
->status
= VTY_NORMAL
;
1592 if (vty
->lines
== 0)
1593 vty_event(VTY_READ
, vty_sock
, vty
);
1596 case BUFFER_PENDING
:
1597 /* There is more data waiting to be written. */
1598 vty
->status
= VTY_MORE
;
1599 if (vty
->lines
== 0)
1600 vty_event(VTY_WRITE
, vty_sock
, vty
);
1607 /* Allocate new vty struct. */
1608 struct vty
*vty_new(void)
1610 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1612 new->fd
= new->wfd
= -1;
1614 new->lbuf
= buffer_new(0);
1615 new->obuf
= buffer_new(0); /* Use default buffer size. */
1616 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1617 new->max
= VTY_BUFSIZ
;
1623 /* allocate and initialise vty */
1624 static struct vty
*vty_new_init(int vty_sock
)
1630 vty
->wfd
= vty_sock
;
1631 vty
->type
= VTY_TERM
;
1632 vty
->node
= AUTH_NODE
;
1637 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1640 vty
->xpath_index
= 0;
1641 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1642 vty
->private_config
= false;
1643 vty
->candidate_config
= vty_shared_candidate_config
;
1644 vector_set_index(vtyvec
, vty_sock
, vty
);
1645 vty
->status
= VTY_NORMAL
;
1648 vty
->iac_sb_in_progress
= 0;
1654 /* Create new vty structure. */
1655 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1657 char buf
[SU_ADDRSTRLEN
];
1660 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1662 /* Allocate new vty structure and set up default values. */
1663 vty
= vty_new_init(vty_sock
);
1665 /* configurable parameters not part of basic init */
1666 vty
->v_timeout
= vty_timeout_val
;
1667 strcpy(vty
->address
, buf
);
1668 if (no_password_check
) {
1670 vty
->node
= ENABLE_NODE
;
1672 vty
->node
= VIEW_NODE
;
1674 if (host
.lines
>= 0)
1675 vty
->lines
= host
.lines
;
1677 if (!no_password_check
) {
1678 /* Vty is not available if password isn't set. */
1679 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1680 vty_out(vty
, "Vty password is not set.\n");
1681 vty
->status
= VTY_CLOSE
;
1687 /* Say hello to the world. */
1689 if (!no_password_check
)
1690 vty_out(vty
, "\nUser Access Verification\n\n");
1692 /* Setting up terminal. */
1694 vty_will_suppress_go_ahead(vty
);
1696 vty_dont_linemode(vty
);
1697 vty_do_window_size(vty
);
1698 /* vty_dont_lflow_ahead (vty); */
1702 /* Add read/write thread. */
1703 vty_event(VTY_WRITE
, vty_sock
, vty
);
1704 vty_event(VTY_READ
, vty_sock
, vty
);
1709 /* create vty for stdio */
1710 static struct termios stdio_orig_termios
;
1711 static struct vty
*stdio_vty
= NULL
;
1712 static bool stdio_termios
= false;
1713 static void (*stdio_vty_atclose
)(int isexit
);
1715 static void vty_stdio_reset(int isexit
)
1719 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1720 stdio_termios
= false;
1724 if (stdio_vty_atclose
)
1725 stdio_vty_atclose(isexit
);
1726 stdio_vty_atclose
= NULL
;
1730 static void vty_stdio_atexit(void)
1735 void vty_stdio_suspend(void)
1740 if (stdio_vty
->t_write
)
1741 thread_cancel(stdio_vty
->t_write
);
1742 if (stdio_vty
->t_read
)
1743 thread_cancel(stdio_vty
->t_read
);
1744 if (stdio_vty
->t_timeout
)
1745 thread_cancel(stdio_vty
->t_timeout
);
1748 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1749 stdio_termios
= false;
1752 void vty_stdio_resume(void)
1757 if (!tcgetattr(0, &stdio_orig_termios
)) {
1758 struct termios termios
;
1760 termios
= stdio_orig_termios
;
1761 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1762 | IGNCR
| ICRNL
| IXON
);
1763 termios
.c_oflag
&= ~OPOST
;
1764 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1765 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1766 termios
.c_cflag
|= CS8
;
1767 tcsetattr(0, TCSANOW
, &termios
);
1768 stdio_termios
= true;
1771 vty_prompt(stdio_vty
);
1773 /* Add read/write thread. */
1774 vty_event(VTY_WRITE
, 1, stdio_vty
);
1775 vty_event(VTY_READ
, 0, stdio_vty
);
1778 void vty_stdio_close(void)
1782 vty_close(stdio_vty
);
1785 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1789 /* refuse creating two vtys on stdio */
1793 vty
= stdio_vty
= vty_new_init(0);
1794 stdio_vty_atclose
= atclose
;
1797 /* always have stdio vty in a known _unchangeable_ state, don't want
1799 * to have any effect here to make sure scripting this works as intended
1801 vty
->node
= ENABLE_NODE
;
1803 strcpy(vty
->address
, "console");
1809 /* Accept connection from the network. */
1810 static int vty_accept(struct thread
*thread
)
1818 struct access_list
*acl
= NULL
;
1819 char buf
[SU_ADDRSTRLEN
];
1821 accept_sock
= THREAD_FD(thread
);
1823 /* We continue hearing vty socket. */
1824 vty_event(VTY_SERV
, accept_sock
, NULL
);
1826 memset(&su
, 0, sizeof(union sockunion
));
1828 /* We can handle IPv4 or IPv6 socket. */
1829 vty_sock
= sockunion_accept(accept_sock
, &su
);
1831 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1832 safe_strerror(errno
));
1835 set_nonblocking(vty_sock
);
1836 set_cloexec(vty_sock
);
1838 sockunion2hostprefix(&su
, &p
);
1840 /* VTY's accesslist apply. */
1841 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1842 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1843 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1844 zlog_info("Vty connection refused from %s",
1845 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1848 /* continue accepting connections */
1849 vty_event(VTY_SERV
, accept_sock
, NULL
);
1855 /* VTY's ipv6 accesslist apply. */
1856 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1857 if ((acl
= access_list_lookup(AFI_IP6
,
1858 vty_ipv6_accesslist_name
))
1859 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1860 zlog_info("Vty connection refused from %s",
1861 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1864 /* continue accepting connections */
1865 vty_event(VTY_SERV
, accept_sock
, NULL
);
1872 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1875 zlog_info("can't set sockopt to vty_sock : %s",
1876 safe_strerror(errno
));
1878 zlog_info("Vty connection from %s",
1879 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1881 vty_create(vty_sock
, &su
);
1886 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1889 struct addrinfo req
;
1890 struct addrinfo
*ainfo
;
1891 struct addrinfo
*ainfo_save
;
1893 char port_str
[BUFSIZ
];
1895 memset(&req
, 0, sizeof(struct addrinfo
));
1896 req
.ai_flags
= AI_PASSIVE
;
1897 req
.ai_family
= AF_UNSPEC
;
1898 req
.ai_socktype
= SOCK_STREAM
;
1899 sprintf(port_str
, "%d", port
);
1900 port_str
[sizeof(port_str
) - 1] = '\0';
1902 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1905 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1913 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1916 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1917 ainfo
->ai_protocol
);
1921 sockopt_v6only(ainfo
->ai_family
, sock
);
1922 sockopt_reuseaddr(sock
);
1923 sockopt_reuseport(sock
);
1926 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1928 close(sock
); /* Avoid sd leak. */
1932 ret
= listen(sock
, 3);
1934 close(sock
); /* Avoid sd leak. */
1938 vty_event(VTY_SERV
, sock
, NULL
);
1939 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1941 freeaddrinfo(ainfo_save
);
1945 /* For sockaddr_un. */
1948 /* VTY shell UNIX domain socket. */
1949 static void vty_serv_un(const char *path
)
1953 struct sockaddr_un serv
;
1955 struct zprivs_ids_t ids
;
1957 /* First of all, unlink existing socket */
1961 old_mask
= umask(0007);
1963 /* Make UNIX domain socket. */
1964 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1966 flog_err_sys(EC_LIB_SOCKET
,
1967 "Cannot create unix stream socket: %s",
1968 safe_strerror(errno
));
1972 /* Make server socket. */
1973 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1974 serv
.sun_family
= AF_UNIX
;
1975 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1976 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1977 len
= serv
.sun_len
= SUN_LEN(&serv
);
1979 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1980 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1984 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1986 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1987 safe_strerror(errno
));
1988 close(sock
); /* Avoid sd leak. */
1992 ret
= listen(sock
, 5);
1994 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1995 safe_strerror(errno
));
1996 close(sock
); /* Avoid sd leak. */
2002 zprivs_get_ids(&ids
);
2004 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2005 earlier for the case when we don't need to chown the file
2006 type casting it here to make a compare */
2007 if ((int)ids
.gid_vty
> 0) {
2008 /* set group of socket */
2009 if (chown(path
, -1, ids
.gid_vty
)) {
2010 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2011 "vty_serv_un: could chown socket, %s",
2012 safe_strerror(errno
));
2016 vty_event(VTYSH_SERV
, sock
, NULL
);
2019 /* #define VTYSH_DEBUG 1 */
2021 static int vtysh_accept(struct thread
*thread
)
2026 struct sockaddr_un client
;
2029 accept_sock
= THREAD_FD(thread
);
2031 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2033 memset(&client
, 0, sizeof(struct sockaddr_un
));
2034 client_len
= sizeof(struct sockaddr_un
);
2036 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2037 (socklen_t
*)&client_len
);
2040 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2041 safe_strerror(errno
));
2045 if (set_nonblocking(sock
) < 0) {
2048 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2049 sock
, safe_strerror(errno
));
2056 printf("VTY shell accept\n");
2057 #endif /* VTYSH_DEBUG */
2062 vty
->type
= VTY_SHELL_SERV
;
2063 vty
->node
= VIEW_NODE
;
2065 vty_event(VTYSH_READ
, sock
, vty
);
2070 static int vtysh_flush(struct vty
*vty
)
2072 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2073 case BUFFER_PENDING
:
2074 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2078 0; /* disable monitoring to avoid infinite recursion */
2079 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2081 buffer_reset(vty
->lbuf
);
2082 buffer_reset(vty
->obuf
);
2092 static int vtysh_read(struct thread
*thread
)
2098 unsigned char buf
[VTY_READ_BUFSIZ
];
2100 uint8_t header
[4] = {0, 0, 0, 0};
2102 sock
= THREAD_FD(thread
);
2103 vty
= THREAD_ARG(thread
);
2106 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2108 if (ERRNO_IO_RETRY(errno
)) {
2109 vty_event(VTYSH_READ
, sock
, vty
);
2112 vty
->monitor
= 0; /* disable monitoring to avoid
2113 infinite recursion */
2116 "%s: read failed on vtysh client fd %d, closing: %s",
2117 __func__
, sock
, safe_strerror(errno
));
2119 buffer_reset(vty
->lbuf
);
2120 buffer_reset(vty
->obuf
);
2123 printf("close vtysh\n");
2124 #endif /* VTYSH_DEBUG */
2129 printf("line: %.*s\n", nbytes
, buf
);
2130 #endif /* VTYSH_DEBUG */
2132 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2133 /* Clear command line buffer. */
2134 vty
->cp
= vty
->length
= 0;
2136 vty_out(vty
, "%% Command is too long.\n");
2138 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2139 vty
->buf
[vty
->length
++] = *p
;
2141 /* Pass this line to parser. */
2142 ret
= vty_execute(vty
);
2143 /* Note that vty_execute clears the command buffer and resets
2144 vty->length to 0. */
2146 /* Return result. */
2148 printf("result: %d\n", ret
);
2149 printf("vtysh node: %d\n", vty
->node
);
2150 #endif /* VTYSH_DEBUG */
2152 /* hack for asynchronous "write integrated"
2153 * - other commands in "buf" will be ditched
2154 * - input during pending config-write is
2156 if (ret
== CMD_SUSPEND
)
2159 /* warning: watchfrr hardcodes this result write
2162 buffer_put(vty
->obuf
, header
, 4);
2164 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2165 /* Try to flush results; exit if a write
2172 if (vty
->status
== VTY_CLOSE
)
2175 vty_event(VTYSH_READ
, sock
, vty
);
2180 static int vtysh_write(struct thread
*thread
)
2182 struct vty
*vty
= THREAD_ARG(thread
);
2184 vty
->t_write
= NULL
;
2191 /* Determine address family to bind. */
2192 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2194 /* If port is set to 0, do not listen on TCP/IP at all! */
2196 vty_serv_sock_addrinfo(addr
, port
);
2203 static void vty_error_delete(void *arg
)
2205 struct vty_error
*ve
= arg
;
2207 XFREE(MTYPE_TMP
, ve
);
2210 /* Close vty interface. Warning: call this only from functions that
2211 will be careful not to access the vty afterwards (since it has
2212 now been freed). This is safest from top-level functions (called
2213 directly by the thread dispatcher). */
2214 void vty_close(struct vty
*vty
)
2217 bool was_stdio
= false;
2219 /* Cancel threads.*/
2221 thread_cancel(vty
->t_read
);
2223 thread_cancel(vty
->t_write
);
2225 thread_cancel(vty
->t_timeout
);
2228 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2230 /* Free input buffer. */
2231 buffer_free(vty
->obuf
);
2232 buffer_free(vty
->lbuf
);
2234 /* Free command history. */
2235 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2237 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2241 vector_unset(vtyvec
, vty
->fd
);
2243 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2247 * note check is for fd > STDERR_FILENO, not fd != -1.
2248 * We never close stdin/stdout/stderr here, because we may be
2249 * running in foreground mode with logging to stdout. Also,
2250 * additionally, we'd need to replace these fds with /dev/null. */
2251 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2253 if (vty
->fd
> STDERR_FILENO
)
2255 if (vty
->fd
== STDIN_FILENO
)
2259 XFREE(MTYPE_VTY
, vty
->buf
);
2262 vty
->error
->del
= vty_error_delete
;
2263 list_delete(&vty
->error
);
2266 /* Check configure. */
2267 vty_config_exit(vty
);
2270 XFREE(MTYPE_VTY
, vty
);
2276 /* When time out occur output message then close connection. */
2277 static int vty_timeout(struct thread
*thread
)
2281 vty
= THREAD_ARG(thread
);
2282 vty
->t_timeout
= NULL
;
2286 buffer_reset(vty
->lbuf
);
2287 buffer_reset(vty
->obuf
);
2288 vty_out(vty
, "\nVty connection is timed out.\n");
2290 /* Close connection. */
2291 vty
->status
= VTY_CLOSE
;
2297 /* Read up configuration file from file_name. */
2298 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2302 struct vty_error
*ve
;
2303 struct listnode
*node
;
2304 unsigned int line_num
= 0;
2307 /* vty_close won't close stderr; if some config command prints
2308 * something it'll end up there. (not ideal; it'd be beter if output
2309 * from a file-load went to logging instead. Also note that if this
2310 * function is called after daemonizing, stderr will be /dev/null.)
2312 * vty->fd will be -1 from vty_new()
2314 vty
->wfd
= STDERR_FILENO
;
2315 vty
->type
= VTY_FILE
;
2316 vty
->node
= CONFIG_NODE
;
2318 vty
->candidate_config
= config
;
2320 vty
->private_config
= true;
2321 vty
->candidate_config
= nb_config_new(NULL
);
2324 /* Execute configuration file */
2325 ret
= config_from_file(vty
, confp
, &line_num
);
2327 /* Flush any previous errors before printing messages below */
2328 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2330 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2331 const char *message
= NULL
;
2335 case CMD_ERR_AMBIGUOUS
:
2336 message
= "Ambiguous command";
2338 case CMD_ERR_NO_MATCH
:
2339 message
= "No such command";
2342 message
= "Command returned Warning";
2344 case CMD_WARNING_CONFIG_FAILED
:
2345 message
= "Command returned Warning Config Failed";
2347 case CMD_ERR_INCOMPLETE
:
2348 message
= "Command returned Incomplete";
2350 case CMD_ERR_EXEED_ARGC_MAX
:
2352 "Command exceeded maximum number of Arguments";
2355 message
= "Command returned unhandled error message";
2359 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2360 nl
= strchr(ve
->error_buf
, '\n');
2363 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2364 message
, ve
->line_num
, ve
->error_buf
);
2369 * Automatically commit the candidate configuration after
2370 * reading the configuration file.
2372 if (config
== NULL
&& vty
->candidate_config
2373 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2374 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2375 true, "Read configuration file",
2377 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2378 zlog_err("%s: failed to read configuration file.",
2385 static FILE *vty_use_backup_config(const char *fullpath
)
2387 char *fullpath_sav
, *fullpath_tmp
;
2393 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2394 strcpy(fullpath_sav
, fullpath
);
2395 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2397 sav
= open(fullpath_sav
, O_RDONLY
);
2403 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2404 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2406 /* Open file to configuration write. */
2407 tmp
= mkstemp(fullpath_tmp
);
2411 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2414 while ((c
= read(sav
, buffer
, 512)) > 0) {
2415 if (write(tmp
, buffer
, c
) <= 0)
2421 if (rename(fullpath_tmp
, fullpath
) == 0)
2422 ret
= fopen(fullpath
, "r");
2424 unlink(fullpath_tmp
);
2429 unlink(fullpath_tmp
);
2439 /* Read up configuration file from file_name. */
2440 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2441 char *config_default_dir
)
2443 char cwd
[MAXPATHLEN
];
2445 const char *fullpath
;
2447 bool read_success
= false;
2449 /* If -f flag specified. */
2450 if (config_file
!= NULL
) {
2451 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2452 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2455 "%s: failure to determine Current Working Directory %d!",
2457 goto tmp_free_and_out
;
2459 tmp
= XMALLOC(MTYPE_TMP
,
2460 strlen(cwd
) + strlen(config_file
) + 2);
2461 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2464 fullpath
= config_file
;
2466 confp
= fopen(fullpath
, "r");
2468 if (confp
== NULL
) {
2470 EC_LIB_BACKUP_CONFIG
,
2471 "%s: failed to open configuration file %s: %s, checking backup",
2472 __func__
, fullpath
, safe_strerror(errno
));
2474 confp
= vty_use_backup_config(fullpath
);
2477 EC_LIB_BACKUP_CONFIG
,
2478 "WARNING: using backup configuration file!");
2482 "%s: can't open configuration file [%s]",
2483 __func__
, config_file
);
2484 goto tmp_free_and_out
;
2489 host_config_set(config_default_dir
);
2493 struct stat conf_stat
;
2495 /* !!!!PLEASE LEAVE!!!!
2496 * This is NEEDED for use with vtysh -b, or else you can get
2497 * a real configuration food fight with a lot garbage in the
2498 * merged configuration file it creates coming from the per
2499 * daemon configuration files. This also allows the daemons
2500 * to start if there default configuration file is not
2501 * present or ignore them, as needed when using vtysh -b to
2502 * configure the daemons at boot - MAG
2505 /* Stat for vtysh Zebra.conf, if found startup and wait for
2506 * boot configuration
2509 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2510 ret
= stat(integrate_default
, &conf_stat
);
2512 read_success
= true;
2513 goto tmp_free_and_out
;
2517 confp
= fopen(config_default_dir
, "r");
2518 if (confp
== NULL
) {
2521 "%s: failed to open configuration file %s: %s, checking backup",
2522 __func__
, config_default_dir
,
2523 safe_strerror(errno
));
2525 confp
= vty_use_backup_config(config_default_dir
);
2528 EC_LIB_BACKUP_CONFIG
,
2529 "WARNING: using backup configuration file!");
2530 fullpath
= config_default_dir
;
2532 flog_err(EC_LIB_VTY
,
2533 "can't open configuration file [%s]",
2534 config_default_dir
);
2535 goto tmp_free_and_out
;
2538 fullpath
= config_default_dir
;
2541 vty_read_file(config
, confp
);
2542 read_success
= true;
2546 host_config_set(fullpath
);
2550 XFREE(MTYPE_TMP
, tmp
);
2552 return read_success
;
2555 /* Small utility function which output log to the VTY. */
2556 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2557 struct timestamp_control
*ctl
, va_list va
)
2565 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2566 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2570 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2576 /* Async-signal-safe version of vty_log for fixed strings. */
2577 void vty_log_fixed(char *buf
, size_t len
)
2580 struct iovec iov
[2];
2581 char crlf
[4] = "\r\n";
2583 /* vty may not have been initialised */
2587 iov
[0].iov_base
= buf
;
2588 iov
[0].iov_len
= len
;
2589 iov
[1].iov_base
= crlf
;
2592 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2594 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2595 /* N.B. We don't care about the return code, since
2597 most likely just about to die anyway. */
2598 if (writev(vty
->wfd
, iov
, 2) == -1) {
2599 fprintf(stderr
, "Failure to writev: %d\n",
2606 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2608 if (exclusive
&& !vty_config_exclusive_lock(vty
)) {
2609 vty_out(vty
, "VTY configuration is locked by other VTY\n");
2613 vty
->node
= CONFIG_NODE
;
2615 vty
->private_config
= private_config
;
2616 vty
->xpath_index
= 0;
2618 if (private_config
) {
2619 vty
->candidate_config
= nb_config_dup(running_config
);
2620 vty
->candidate_config_base
= nb_config_dup(running_config
);
2622 "Warning: uncommitted changes will be discarded on exit.\n\n");
2624 vty
->candidate_config
= vty_shared_candidate_config
;
2625 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2626 vty
->candidate_config_base
=
2627 nb_config_dup(running_config
);
2633 void vty_config_exit(struct vty
*vty
)
2635 /* Check if there's a pending confirmed commit. */
2636 if (vty
->t_confirmed_commit_timeout
) {
2638 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2639 nb_cli_confirmed_commit_rollback(vty
);
2640 nb_cli_confirmed_commit_clean(vty
);
2643 vty_config_exclusive_unlock(vty
);
2645 if (vty
->candidate_config
) {
2646 if (vty
->private_config
)
2647 nb_config_free(vty
->candidate_config
);
2648 vty
->candidate_config
= NULL
;
2650 if (vty
->candidate_config_base
) {
2651 nb_config_free(vty
->candidate_config_base
);
2652 vty
->candidate_config_base
= NULL
;
2655 vty
->config
= false;
2658 int vty_config_exclusive_lock(struct vty
*vty
)
2660 if (vty_exclusive_lock
== NULL
) {
2661 vty_exclusive_lock
= vty
;
2667 void vty_config_exclusive_unlock(struct vty
*vty
)
2669 if (vty_exclusive_lock
== vty
)
2670 vty_exclusive_lock
= NULL
;
2673 /* Master of the threads. */
2674 static struct thread_master
*vty_master
;
2676 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2678 struct thread
*vty_serv_thread
= NULL
;
2682 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2684 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2688 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2690 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2694 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2698 vty
->t_write
= NULL
;
2699 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2705 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2707 /* Time out treatment. */
2708 if (vty
->v_timeout
) {
2710 thread_cancel(vty
->t_timeout
);
2711 vty
->t_timeout
= NULL
;
2712 thread_add_timer(vty_master
, vty_timeout
, vty
,
2713 vty
->v_timeout
, &vty
->t_timeout
);
2717 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2720 case VTY_TIMEOUT_RESET
:
2721 if (vty
->t_timeout
) {
2722 thread_cancel(vty
->t_timeout
);
2723 vty
->t_timeout
= NULL
;
2725 if (vty
->v_timeout
) {
2726 vty
->t_timeout
= NULL
;
2727 thread_add_timer(vty_master
, vty_timeout
, vty
,
2728 vty
->v_timeout
, &vty
->t_timeout
);
2734 DEFUN_NOSH (config_who
,
2737 "Display who is on vty\n")
2742 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2743 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2744 vty_out(vty
, "%svty[%d] connected from %s.\n",
2745 v
->config
? "*" : " ", i
, v
->address
);
2749 /* Move to vty configuration mode. */
2750 DEFUN_NOSH (line_vty
,
2753 "Configure a terminal line\n"
2754 "Virtual terminal\n")
2756 vty
->node
= VTY_NODE
;
2760 /* Set time out value. */
2761 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2762 const char *sec_str
)
2764 unsigned long timeout
= 0;
2766 /* min_str and sec_str are already checked by parser. So it must be
2767 all digit string. */
2769 timeout
= strtol(min_str
, NULL
, 10);
2773 timeout
+= strtol(sec_str
, NULL
, 10);
2775 vty_timeout_val
= timeout
;
2776 vty
->v_timeout
= timeout
;
2777 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2783 DEFUN (exec_timeout_min
,
2784 exec_timeout_min_cmd
,
2785 "exec-timeout (0-35791)",
2786 "Set timeout value\n"
2787 "Timeout value in minutes\n")
2790 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2793 DEFUN (exec_timeout_sec
,
2794 exec_timeout_sec_cmd
,
2795 "exec-timeout (0-35791) (0-2147483)",
2796 "Set the EXEC timeout\n"
2797 "Timeout in minutes\n"
2798 "Timeout in seconds\n")
2801 int idx_number_2
= 2;
2802 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2803 argv
[idx_number_2
]->arg
);
2806 DEFUN (no_exec_timeout
,
2807 no_exec_timeout_cmd
,
2810 "Set the EXEC timeout\n")
2812 return exec_timeout(vty
, NULL
, NULL
);
2815 /* Set vty access class. */
2816 DEFUN (vty_access_class
,
2817 vty_access_class_cmd
,
2818 "access-class WORD",
2819 "Filter connections based on an IP access list\n"
2823 if (vty_accesslist_name
)
2824 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2826 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2831 /* Clear vty access class. */
2832 DEFUN (no_vty_access_class
,
2833 no_vty_access_class_cmd
,
2834 "no access-class [WORD]",
2836 "Filter connections based on an IP access list\n"
2840 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2841 if (!vty_accesslist_name
2842 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2843 vty_out(vty
, "Access-class is not currently applied to vty\n");
2844 return CMD_WARNING_CONFIG_FAILED
;
2847 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2849 vty_accesslist_name
= NULL
;
2854 /* Set vty access class. */
2855 DEFUN (vty_ipv6_access_class
,
2856 vty_ipv6_access_class_cmd
,
2857 "ipv6 access-class WORD",
2859 "Filter connections based on an IP access list\n"
2860 "IPv6 access list\n")
2863 if (vty_ipv6_accesslist_name
)
2864 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2866 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2871 /* Clear vty access class. */
2872 DEFUN (no_vty_ipv6_access_class
,
2873 no_vty_ipv6_access_class_cmd
,
2874 "no ipv6 access-class [WORD]",
2877 "Filter connections based on an IP access list\n"
2878 "IPv6 access list\n")
2881 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2883 if (!vty_ipv6_accesslist_name
2884 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2886 "IPv6 access-class is not currently applied to vty\n");
2887 return CMD_WARNING_CONFIG_FAILED
;
2890 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2892 vty_ipv6_accesslist_name
= NULL
;
2901 "Enable password checking\n")
2903 no_password_check
= 0;
2907 DEFUN (no_vty_login
,
2911 "Enable password checking\n")
2913 no_password_check
= 1;
2917 DEFUN (service_advanced_vty
,
2918 service_advanced_vty_cmd
,
2919 "service advanced-vty",
2920 "Set up miscellaneous service\n"
2921 "Enable advanced mode vty interface\n")
2927 DEFUN (no_service_advanced_vty
,
2928 no_service_advanced_vty_cmd
,
2929 "no service advanced-vty",
2931 "Set up miscellaneous service\n"
2932 "Enable advanced mode vty interface\n")
2938 DEFUN_NOSH (terminal_monitor
,
2939 terminal_monitor_cmd
,
2941 "Set terminal line parameters\n"
2942 "Copy debug output to the current terminal line\n")
2948 DEFUN_NOSH (terminal_no_monitor
,
2949 terminal_no_monitor_cmd
,
2950 "terminal no monitor",
2951 "Set terminal line parameters\n"
2953 "Copy debug output to the current terminal line\n")
2959 DEFUN_NOSH (no_terminal_monitor
,
2960 no_terminal_monitor_cmd
,
2961 "no terminal monitor",
2963 "Set terminal line parameters\n"
2964 "Copy debug output to the current terminal line\n")
2966 return terminal_no_monitor(self
, vty
, argc
, argv
);
2970 DEFUN_NOSH (show_history
,
2974 "Display the session command history\n")
2978 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2979 if (index
== VTY_MAXHIST
) {
2984 if (vty
->hist
[index
] != NULL
)
2985 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2994 DEFUN (log_commands
,
2998 "Log all commands (can't be unset without restart)\n")
3000 do_log_commands
= 1;
3004 /* Display current configuration. */
3005 static int vty_config_write(struct vty
*vty
)
3007 vty_out(vty
, "line vty\n");
3009 if (vty_accesslist_name
)
3010 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3012 if (vty_ipv6_accesslist_name
)
3013 vty_out(vty
, " ipv6 access-class %s\n",
3014 vty_ipv6_accesslist_name
);
3017 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3018 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3019 vty_timeout_val
% 60);
3022 if (no_password_check
)
3023 vty_out(vty
, " no login\n");
3025 if (do_log_commands
)
3026 vty_out(vty
, "log commands\n");
3028 vty_out(vty
, "!\n");
3033 struct cmd_node vty_node
= {
3034 VTY_NODE
, "%s(config-line)# ", 1,
3037 /* Reset all VTY status. */
3038 void vty_reset(void)
3042 struct thread
*vty_serv_thread
;
3044 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3045 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3046 buffer_reset(vty
->lbuf
);
3047 buffer_reset(vty
->obuf
);
3048 vty
->status
= VTY_CLOSE
;
3052 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3053 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3055 thread_cancel(vty_serv_thread
);
3056 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3060 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3062 if (vty_accesslist_name
) {
3063 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3064 vty_accesslist_name
= NULL
;
3067 if (vty_ipv6_accesslist_name
) {
3068 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3069 vty_ipv6_accesslist_name
= NULL
;
3073 static void vty_save_cwd(void)
3075 char cwd
[MAXPATHLEN
];
3078 c
= getcwd(cwd
, MAXPATHLEN
);
3082 * At this point if these go wrong, more than likely
3083 * the whole world is coming down around us
3084 * Hence not worrying about it too much.
3086 if (!chdir(SYSCONFDIR
)) {
3087 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3088 "Failure to chdir to %s, errno: %d",
3092 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3093 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3094 "Failure to getcwd, errno: %d", errno
);
3099 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3100 strcpy(vty_cwd
, cwd
);
3103 char *vty_get_cwd(void)
3108 int vty_shell(struct vty
*vty
)
3110 return vty
->type
== VTY_SHELL
? 1 : 0;
3113 int vty_shell_serv(struct vty
*vty
)
3115 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3118 void vty_init_vtysh(void)
3120 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3123 /* Install vty's own commands like `who' command. */
3124 void vty_init(struct thread_master
*master_thread
)
3126 /* For further configuration read, preserve current directory. */
3129 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3131 vty_master
= master_thread
;
3133 atexit(vty_stdio_atexit
);
3135 /* Initilize server thread vector. */
3136 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3138 /* Install bgp top node. */
3139 install_node(&vty_node
, vty_config_write
);
3141 install_element(VIEW_NODE
, &config_who_cmd
);
3142 install_element(VIEW_NODE
, &show_history_cmd
);
3143 install_element(CONFIG_NODE
, &line_vty_cmd
);
3144 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3145 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3146 install_element(CONFIG_NODE
, &show_history_cmd
);
3147 install_element(CONFIG_NODE
, &log_commands_cmd
);
3148 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3149 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3150 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3152 install_default(VTY_NODE
);
3153 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3154 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3155 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3156 install_element(VTY_NODE
, &vty_access_class_cmd
);
3157 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3158 install_element(VTY_NODE
, &vty_login_cmd
);
3159 install_element(VTY_NODE
, &no_vty_login_cmd
);
3160 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3161 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3164 void vty_terminate(void)
3167 XFREE(MTYPE_TMP
, vty_cwd
);
3169 if (vtyvec
&& Vvty_serv_thread
) {
3171 vector_free(vtyvec
);
3172 vector_free(Vvty_serv_thread
);
3174 Vvty_serv_thread
= NULL
;