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 static 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
);
977 XFREE(MTYPE_TMP
, matched
);
980 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
981 unsigned int desc_width
, struct cmd_token
*token
)
989 if (desc_width
<= 0) {
990 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
994 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
996 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
997 for (pos
= desc_width
; pos
> 0; pos
--)
998 if (*(p
+ pos
) == ' ')
1004 strncpy(buf
, p
, pos
);
1006 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1011 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1013 XFREE(MTYPE_TMP
, buf
);
1016 /* Describe matched command function. */
1017 static void vty_describe_command(struct vty
*vty
)
1022 unsigned int i
, width
, desc_width
;
1023 struct cmd_token
*token
, *token_cr
= NULL
;
1025 vline
= cmd_make_strvec(vty
->buf
);
1027 /* In case of '> ?'. */
1028 if (vline
== NULL
) {
1029 vline
= vector_init(1);
1030 vector_set(vline
, NULL
);
1031 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1032 vector_set(vline
, NULL
);
1034 describe
= cmd_describe_command(vline
, vty
, &ret
);
1038 /* Ambiguous error. */
1040 case CMD_ERR_AMBIGUOUS
:
1041 vty_out(vty
, "%% Ambiguous command.\n");
1044 case CMD_ERR_NO_MATCH
:
1045 vty_out(vty
, "%% There is no matched command.\n");
1050 /* Get width of command string. */
1052 for (i
= 0; i
< vector_active(describe
); i
++)
1053 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1056 if (token
->text
[0] == '\0')
1059 len
= strlen(token
->text
);
1065 /* Get width of description string. */
1066 desc_width
= vty
->width
- (width
+ 6);
1068 /* Print out description. */
1069 for (i
= 0; i
< vector_active(describe
); i
++)
1070 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1071 if (token
->text
[0] == '\0')
1074 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1080 vty_out(vty
, " %-s\n", token
->text
);
1081 else if (desc_width
>= strlen(token
->desc
))
1082 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1085 vty_describe_fold(vty
, width
, desc_width
,
1088 if (IS_VARYING_TOKEN(token
->type
)) {
1089 const char *ref
= vector_slot(
1090 vline
, vector_active(vline
) - 1);
1092 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1093 cmd_variable_complete(token
, ref
, varcomps
);
1095 if (vector_active(varcomps
) > 0) {
1096 char *ac
= cmd_variable_comp2str(
1097 varcomps
, vty
->width
);
1098 vty_out(vty
, "%s\n", ac
);
1099 XFREE(MTYPE_TMP
, ac
);
1102 vector_free(varcomps
);
1105 vty_out (vty
, " %-*s %s\n", width
1106 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1107 desc
->str
? desc
->str
: "");
1111 if ((token
= token_cr
)) {
1113 vty_out(vty
, " %-s\n", token
->text
);
1114 else if (desc_width
>= strlen(token
->desc
))
1115 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1118 vty_describe_fold(vty
, width
, desc_width
, token
);
1122 cmd_free_strvec(vline
);
1124 vector_free(describe
);
1127 vty_redraw_line(vty
);
1130 static void vty_clear_buf(struct vty
*vty
)
1132 memset(vty
->buf
, 0, vty
->max
);
1135 /* ^C stop current input and do not add command line to the history. */
1136 static void vty_stop_input(struct vty
*vty
)
1138 vty
->cp
= vty
->length
= 0;
1143 vty_config_exit(vty
);
1144 vty
->node
= ENABLE_NODE
;
1149 /* Set history pointer to the latest one. */
1150 vty
->hp
= vty
->hindex
;
1153 /* Add current command line to the history buffer. */
1154 static void vty_hist_add(struct vty
*vty
)
1158 if (vty
->length
== 0)
1161 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1163 /* Ignore the same string as previous one. */
1164 if (vty
->hist
[index
])
1165 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1166 vty
->hp
= vty
->hindex
;
1170 /* Insert history entry. */
1171 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1172 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1174 /* History index rotation. */
1176 if (vty
->hindex
== VTY_MAXHIST
)
1179 vty
->hp
= vty
->hindex
;
1182 /* #define TELNET_OPTION_DEBUG */
1184 /* Get telnet window size. */
1185 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1187 #ifdef TELNET_OPTION_DEBUG
1190 for (i
= 0; i
< nbytes
; i
++) {
1193 vty_out(vty
, "IAC ");
1196 vty_out(vty
, "WILL ");
1199 vty_out(vty
, "WONT ");
1202 vty_out(vty
, "DO ");
1205 vty_out(vty
, "DONT ");
1208 vty_out(vty
, "SB ");
1211 vty_out(vty
, "SE ");
1214 vty_out(vty
, "TELOPT_ECHO \n");
1217 vty_out(vty
, "TELOPT_SGA \n");
1220 vty_out(vty
, "TELOPT_NAWS \n");
1223 vty_out(vty
, "%x ", buf
[i
]);
1229 #endif /* TELNET_OPTION_DEBUG */
1234 vty
->iac_sb_in_progress
= 1;
1238 if (!vty
->iac_sb_in_progress
)
1241 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1242 vty
->iac_sb_in_progress
= 0;
1245 switch (vty
->sb_buf
[0]) {
1247 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1250 "RFC 1073 violation detected: telnet NAWS option "
1251 "should send %d characters, but we received %lu",
1253 (unsigned long)vty
->sb_len
);
1254 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1257 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1258 (unsigned long)sizeof(vty
->sb_buf
),
1259 TELNET_NAWS_SB_LEN
);
1261 vty
->width
= ((vty
->sb_buf
[1] << 8)
1263 vty
->height
= ((vty
->sb_buf
[3] << 8)
1265 #ifdef TELNET_OPTION_DEBUG
1267 "TELNET NAWS window size negotiation completed: "
1268 "width %d, height %d\n",
1269 vty
->width
, vty
->height
);
1274 vty
->iac_sb_in_progress
= 0;
1284 /* Execute current command line. */
1285 static int vty_execute(struct vty
*vty
)
1291 switch (vty
->node
) {
1293 case AUTH_ENABLE_NODE
:
1294 vty_auth(vty
, vty
->buf
);
1297 ret
= vty_command(vty
, vty
->buf
);
1298 if (vty
->type
== VTY_TERM
)
1303 /* Clear command line buffer. */
1304 vty
->cp
= vty
->length
= 0;
1307 if (vty
->status
!= VTY_CLOSE
)
1313 #define CONTROL(X) ((X) - '@')
1314 #define VTY_NORMAL 0
1315 #define VTY_PRE_ESCAPE 1
1316 #define VTY_ESCAPE 2
1318 /* Escape character command map. */
1319 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1323 vty_previous_line(vty
);
1329 vty_forward_char(vty
);
1332 vty_backward_char(vty
);
1338 /* Go back to normal mode. */
1339 vty
->escape
= VTY_NORMAL
;
1342 /* Quit print out to the buffer. */
1343 static void vty_buffer_reset(struct vty
*vty
)
1345 buffer_reset(vty
->obuf
);
1346 buffer_reset(vty
->lbuf
);
1348 vty_redraw_line(vty
);
1351 /* Read data via vty socket. */
1352 static int vty_read(struct thread
*thread
)
1356 unsigned char buf
[VTY_READ_BUFSIZ
];
1358 int vty_sock
= THREAD_FD(thread
);
1359 struct vty
*vty
= THREAD_ARG(thread
);
1362 /* Read raw data from socket */
1363 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1365 if (ERRNO_IO_RETRY(errno
)) {
1366 vty_event(VTY_READ
, vty_sock
, vty
);
1369 vty
->monitor
= 0; /* disable monitoring to avoid
1370 infinite recursion */
1373 "%s: read error on vty client fd %d, closing: %s",
1374 __func__
, vty
->fd
, safe_strerror(errno
));
1375 buffer_reset(vty
->obuf
);
1376 buffer_reset(vty
->lbuf
);
1378 vty
->status
= VTY_CLOSE
;
1381 for (i
= 0; i
< nbytes
; i
++) {
1382 if (buf
[i
] == IAC
) {
1391 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1392 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1393 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1399 /* In case of telnet command */
1401 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1408 if (vty
->status
== VTY_MORE
) {
1413 vty_buffer_reset(vty
);
1415 #if 0 /* More line does not work for "show ip bgp". */
1418 vty
->status
= VTY_MORELINE
;
1427 /* Escape character. */
1428 if (vty
->escape
== VTY_ESCAPE
) {
1429 vty_escape_map(buf
[i
], vty
);
1433 /* Pre-escape status. */
1434 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1437 vty
->escape
= VTY_ESCAPE
;
1440 vty_backward_word(vty
);
1441 vty
->escape
= VTY_NORMAL
;
1444 vty_forward_word(vty
);
1445 vty
->escape
= VTY_NORMAL
;
1448 vty_forward_kill_word(vty
);
1449 vty
->escape
= VTY_NORMAL
;
1453 vty_backward_kill_word(vty
);
1454 vty
->escape
= VTY_NORMAL
;
1457 vty
->escape
= VTY_NORMAL
;
1465 vty_beginning_of_line(vty
);
1468 vty_backward_char(vty
);
1471 vty_stop_input(vty
);
1474 vty_delete_char(vty
);
1477 vty_end_of_line(vty
);
1480 vty_forward_char(vty
);
1484 vty_delete_backward_char(vty
);
1493 vty_previous_line(vty
);
1496 vty_transpose_chars(vty
);
1499 vty_kill_line_from_beginning(vty
);
1502 vty_backward_kill_word(vty
);
1505 vty_end_config(vty
);
1513 vty_complete_command(vty
);
1516 if (vty
->node
== AUTH_NODE
1517 || vty
->node
== AUTH_ENABLE_NODE
)
1518 vty_self_insert(vty
, buf
[i
]);
1520 vty_describe_command(vty
);
1523 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1524 vty
->escape
= VTY_ESCAPE
;
1527 vty
->escape
= VTY_PRE_ESCAPE
;
1530 if (buf
[i
] > 31 && buf
[i
] < 127)
1531 vty_self_insert(vty
, buf
[i
]);
1537 if (vty
->status
== VTY_CLOSE
)
1540 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1541 vty_event(VTY_READ
, vty_sock
, vty
);
1546 /* Flush buffer to the vty. */
1547 static int vty_flush(struct thread
*thread
)
1550 buffer_status_t flushrc
;
1551 int vty_sock
= THREAD_FD(thread
);
1552 struct vty
*vty
= THREAD_ARG(thread
);
1554 vty
->t_write
= NULL
;
1556 /* Tempolary disable read thread. */
1557 if ((vty
->lines
== 0) && vty
->t_read
) {
1558 thread_cancel(vty
->t_read
);
1562 /* Function execution continue. */
1563 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1565 /* N.B. if width is 0, that means we don't know the window size. */
1566 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1567 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1568 else if (vty
->status
== VTY_MORELINE
)
1569 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1572 flushrc
= buffer_flush_window(
1573 vty
->obuf
, vty_sock
, vty
->width
,
1574 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1578 0; /* disable monitoring to avoid infinite recursion */
1579 zlog_info("buffer_flush failed on vty client fd %d, closing",
1581 buffer_reset(vty
->lbuf
);
1582 buffer_reset(vty
->obuf
);
1586 if (vty
->status
== VTY_CLOSE
)
1589 vty
->status
= VTY_NORMAL
;
1590 if (vty
->lines
== 0)
1591 vty_event(VTY_READ
, vty_sock
, vty
);
1594 case BUFFER_PENDING
:
1595 /* There is more data waiting to be written. */
1596 vty
->status
= VTY_MORE
;
1597 if (vty
->lines
== 0)
1598 vty_event(VTY_WRITE
, vty_sock
, vty
);
1605 /* Allocate new vty struct. */
1606 struct vty
*vty_new(void)
1608 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1610 new->fd
= new->wfd
= -1;
1612 new->lbuf
= buffer_new(0);
1613 new->obuf
= buffer_new(0); /* Use default buffer size. */
1614 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1615 new->max
= VTY_BUFSIZ
;
1621 /* allocate and initialise vty */
1622 static struct vty
*vty_new_init(int vty_sock
)
1628 vty
->wfd
= vty_sock
;
1629 vty
->type
= VTY_TERM
;
1630 vty
->node
= AUTH_NODE
;
1635 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1638 vty
->xpath_index
= 0;
1639 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1640 vty
->private_config
= false;
1641 vty
->candidate_config
= vty_shared_candidate_config
;
1642 vector_set_index(vtyvec
, vty_sock
, vty
);
1643 vty
->status
= VTY_NORMAL
;
1646 vty
->iac_sb_in_progress
= 0;
1652 /* Create new vty structure. */
1653 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1655 char buf
[SU_ADDRSTRLEN
];
1658 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1660 /* Allocate new vty structure and set up default values. */
1661 vty
= vty_new_init(vty_sock
);
1663 /* configurable parameters not part of basic init */
1664 vty
->v_timeout
= vty_timeout_val
;
1665 strcpy(vty
->address
, buf
);
1666 if (no_password_check
) {
1668 vty
->node
= ENABLE_NODE
;
1670 vty
->node
= VIEW_NODE
;
1672 if (host
.lines
>= 0)
1673 vty
->lines
= host
.lines
;
1675 if (!no_password_check
) {
1676 /* Vty is not available if password isn't set. */
1677 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1678 vty_out(vty
, "Vty password is not set.\n");
1679 vty
->status
= VTY_CLOSE
;
1685 /* Say hello to the world. */
1687 if (!no_password_check
)
1688 vty_out(vty
, "\nUser Access Verification\n\n");
1690 /* Setting up terminal. */
1692 vty_will_suppress_go_ahead(vty
);
1694 vty_dont_linemode(vty
);
1695 vty_do_window_size(vty
);
1696 /* vty_dont_lflow_ahead (vty); */
1700 /* Add read/write thread. */
1701 vty_event(VTY_WRITE
, vty_sock
, vty
);
1702 vty_event(VTY_READ
, vty_sock
, vty
);
1707 /* create vty for stdio */
1708 static struct termios stdio_orig_termios
;
1709 static struct vty
*stdio_vty
= NULL
;
1710 static bool stdio_termios
= false;
1711 static void (*stdio_vty_atclose
)(int isexit
);
1713 static void vty_stdio_reset(int isexit
)
1717 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1718 stdio_termios
= false;
1722 if (stdio_vty_atclose
)
1723 stdio_vty_atclose(isexit
);
1724 stdio_vty_atclose
= NULL
;
1728 static void vty_stdio_atexit(void)
1733 void vty_stdio_suspend(void)
1738 if (stdio_vty
->t_write
)
1739 thread_cancel(stdio_vty
->t_write
);
1740 if (stdio_vty
->t_read
)
1741 thread_cancel(stdio_vty
->t_read
);
1742 if (stdio_vty
->t_timeout
)
1743 thread_cancel(stdio_vty
->t_timeout
);
1746 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1747 stdio_termios
= false;
1750 void vty_stdio_resume(void)
1755 if (!tcgetattr(0, &stdio_orig_termios
)) {
1756 struct termios termios
;
1758 termios
= stdio_orig_termios
;
1759 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1760 | IGNCR
| ICRNL
| IXON
);
1761 termios
.c_oflag
&= ~OPOST
;
1762 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1763 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1764 termios
.c_cflag
|= CS8
;
1765 tcsetattr(0, TCSANOW
, &termios
);
1766 stdio_termios
= true;
1769 vty_prompt(stdio_vty
);
1771 /* Add read/write thread. */
1772 vty_event(VTY_WRITE
, 1, stdio_vty
);
1773 vty_event(VTY_READ
, 0, stdio_vty
);
1776 void vty_stdio_close(void)
1780 vty_close(stdio_vty
);
1783 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1787 /* refuse creating two vtys on stdio */
1791 vty
= stdio_vty
= vty_new_init(0);
1792 stdio_vty_atclose
= atclose
;
1795 /* always have stdio vty in a known _unchangeable_ state, don't want
1797 * to have any effect here to make sure scripting this works as intended
1799 vty
->node
= ENABLE_NODE
;
1801 strcpy(vty
->address
, "console");
1807 /* Accept connection from the network. */
1808 static int vty_accept(struct thread
*thread
)
1816 struct access_list
*acl
= NULL
;
1817 char buf
[SU_ADDRSTRLEN
];
1819 accept_sock
= THREAD_FD(thread
);
1821 /* We continue hearing vty socket. */
1822 vty_event(VTY_SERV
, accept_sock
, NULL
);
1824 memset(&su
, 0, sizeof(union sockunion
));
1826 /* We can handle IPv4 or IPv6 socket. */
1827 vty_sock
= sockunion_accept(accept_sock
, &su
);
1829 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1830 safe_strerror(errno
));
1833 set_nonblocking(vty_sock
);
1834 set_cloexec(vty_sock
);
1836 sockunion2hostprefix(&su
, &p
);
1838 /* VTY's accesslist apply. */
1839 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1840 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1841 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1842 zlog_info("Vty connection refused from %s",
1843 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1846 /* continue accepting connections */
1847 vty_event(VTY_SERV
, accept_sock
, NULL
);
1853 /* VTY's ipv6 accesslist apply. */
1854 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1855 if ((acl
= access_list_lookup(AFI_IP6
,
1856 vty_ipv6_accesslist_name
))
1857 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1858 zlog_info("Vty connection refused from %s",
1859 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1862 /* continue accepting connections */
1863 vty_event(VTY_SERV
, accept_sock
, NULL
);
1870 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1873 zlog_info("can't set sockopt to vty_sock : %s",
1874 safe_strerror(errno
));
1876 zlog_info("Vty connection from %s",
1877 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1879 vty_create(vty_sock
, &su
);
1884 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1887 struct addrinfo req
;
1888 struct addrinfo
*ainfo
;
1889 struct addrinfo
*ainfo_save
;
1891 char port_str
[BUFSIZ
];
1893 memset(&req
, 0, sizeof(struct addrinfo
));
1894 req
.ai_flags
= AI_PASSIVE
;
1895 req
.ai_family
= AF_UNSPEC
;
1896 req
.ai_socktype
= SOCK_STREAM
;
1897 sprintf(port_str
, "%d", port
);
1898 port_str
[sizeof(port_str
) - 1] = '\0';
1900 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1903 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1911 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1914 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1915 ainfo
->ai_protocol
);
1919 sockopt_v6only(ainfo
->ai_family
, sock
);
1920 sockopt_reuseaddr(sock
);
1921 sockopt_reuseport(sock
);
1924 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1926 close(sock
); /* Avoid sd leak. */
1930 ret
= listen(sock
, 3);
1932 close(sock
); /* Avoid sd leak. */
1936 vty_event(VTY_SERV
, sock
, NULL
);
1937 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1939 freeaddrinfo(ainfo_save
);
1943 /* For sockaddr_un. */
1946 /* VTY shell UNIX domain socket. */
1947 static void vty_serv_un(const char *path
)
1951 struct sockaddr_un serv
;
1953 struct zprivs_ids_t ids
;
1955 /* First of all, unlink existing socket */
1959 old_mask
= umask(0007);
1961 /* Make UNIX domain socket. */
1962 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1964 flog_err_sys(EC_LIB_SOCKET
,
1965 "Cannot create unix stream socket: %s",
1966 safe_strerror(errno
));
1970 /* Make server socket. */
1971 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1972 serv
.sun_family
= AF_UNIX
;
1973 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1974 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1975 len
= serv
.sun_len
= SUN_LEN(&serv
);
1977 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1978 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1982 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1984 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1985 safe_strerror(errno
));
1986 close(sock
); /* Avoid sd leak. */
1990 ret
= listen(sock
, 5);
1992 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1993 safe_strerror(errno
));
1994 close(sock
); /* Avoid sd leak. */
2000 zprivs_get_ids(&ids
);
2002 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2003 earlier for the case when we don't need to chown the file
2004 type casting it here to make a compare */
2005 if ((int)ids
.gid_vty
> 0) {
2006 /* set group of socket */
2007 if (chown(path
, -1, ids
.gid_vty
)) {
2008 flog_err_sys(EC_LIB_SYSTEM_CALL
,
2009 "vty_serv_un: could chown socket, %s",
2010 safe_strerror(errno
));
2014 vty_event(VTYSH_SERV
, sock
, NULL
);
2017 /* #define VTYSH_DEBUG 1 */
2019 static int vtysh_accept(struct thread
*thread
)
2024 struct sockaddr_un client
;
2027 accept_sock
= THREAD_FD(thread
);
2029 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2031 memset(&client
, 0, sizeof(struct sockaddr_un
));
2032 client_len
= sizeof(struct sockaddr_un
);
2034 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2035 (socklen_t
*)&client_len
);
2038 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2039 safe_strerror(errno
));
2043 if (set_nonblocking(sock
) < 0) {
2046 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2047 sock
, safe_strerror(errno
));
2054 printf("VTY shell accept\n");
2055 #endif /* VTYSH_DEBUG */
2060 vty
->type
= VTY_SHELL_SERV
;
2061 vty
->node
= VIEW_NODE
;
2063 vty_event(VTYSH_READ
, sock
, vty
);
2068 static int vtysh_flush(struct vty
*vty
)
2070 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2071 case BUFFER_PENDING
:
2072 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2076 0; /* disable monitoring to avoid infinite recursion */
2077 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2079 buffer_reset(vty
->lbuf
);
2080 buffer_reset(vty
->obuf
);
2090 static int vtysh_read(struct thread
*thread
)
2096 unsigned char buf
[VTY_READ_BUFSIZ
];
2098 uint8_t header
[4] = {0, 0, 0, 0};
2100 sock
= THREAD_FD(thread
);
2101 vty
= THREAD_ARG(thread
);
2104 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2106 if (ERRNO_IO_RETRY(errno
)) {
2107 vty_event(VTYSH_READ
, sock
, vty
);
2110 vty
->monitor
= 0; /* disable monitoring to avoid
2111 infinite recursion */
2114 "%s: read failed on vtysh client fd %d, closing: %s",
2115 __func__
, sock
, safe_strerror(errno
));
2117 buffer_reset(vty
->lbuf
);
2118 buffer_reset(vty
->obuf
);
2121 printf("close vtysh\n");
2122 #endif /* VTYSH_DEBUG */
2127 printf("line: %.*s\n", nbytes
, buf
);
2128 #endif /* VTYSH_DEBUG */
2130 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2131 /* Clear command line buffer. */
2132 vty
->cp
= vty
->length
= 0;
2134 vty_out(vty
, "%% Command is too long.\n");
2136 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2137 vty
->buf
[vty
->length
++] = *p
;
2139 /* Pass this line to parser. */
2140 ret
= vty_execute(vty
);
2141 /* Note that vty_execute clears the command buffer and resets
2142 vty->length to 0. */
2144 /* Return result. */
2146 printf("result: %d\n", ret
);
2147 printf("vtysh node: %d\n", vty
->node
);
2148 #endif /* VTYSH_DEBUG */
2150 /* hack for asynchronous "write integrated"
2151 * - other commands in "buf" will be ditched
2152 * - input during pending config-write is
2154 if (ret
== CMD_SUSPEND
)
2157 /* warning: watchfrr hardcodes this result write
2160 buffer_put(vty
->obuf
, header
, 4);
2162 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2163 /* Try to flush results; exit if a write
2170 if (vty
->status
== VTY_CLOSE
)
2173 vty_event(VTYSH_READ
, sock
, vty
);
2178 static int vtysh_write(struct thread
*thread
)
2180 struct vty
*vty
= THREAD_ARG(thread
);
2182 vty
->t_write
= NULL
;
2189 /* Determine address family to bind. */
2190 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2192 /* If port is set to 0, do not listen on TCP/IP at all! */
2194 vty_serv_sock_addrinfo(addr
, port
);
2201 static void vty_error_delete(void *arg
)
2203 struct vty_error
*ve
= arg
;
2205 XFREE(MTYPE_TMP
, ve
);
2208 /* Close vty interface. Warning: call this only from functions that
2209 will be careful not to access the vty afterwards (since it has
2210 now been freed). This is safest from top-level functions (called
2211 directly by the thread dispatcher). */
2212 void vty_close(struct vty
*vty
)
2215 bool was_stdio
= false;
2217 /* Cancel threads.*/
2219 thread_cancel(vty
->t_read
);
2221 thread_cancel(vty
->t_write
);
2223 thread_cancel(vty
->t_timeout
);
2226 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2228 /* Free input buffer. */
2229 buffer_free(vty
->obuf
);
2230 buffer_free(vty
->lbuf
);
2232 /* Free command history. */
2233 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2234 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2239 vector_unset(vtyvec
, vty
->fd
);
2241 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2245 * note check is for fd > STDERR_FILENO, not fd != -1.
2246 * We never close stdin/stdout/stderr here, because we may be
2247 * running in foreground mode with logging to stdout. Also,
2248 * additionally, we'd need to replace these fds with /dev/null. */
2249 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2251 if (vty
->fd
> STDERR_FILENO
)
2253 if (vty
->fd
== STDIN_FILENO
)
2256 XFREE(MTYPE_VTY
, vty
->buf
);
2259 vty
->error
->del
= vty_error_delete
;
2260 list_delete(&vty
->error
);
2263 /* Check configure. */
2264 vty_config_exit(vty
);
2267 XFREE(MTYPE_VTY
, vty
);
2273 /* When time out occur output message then close connection. */
2274 static int vty_timeout(struct thread
*thread
)
2278 vty
= THREAD_ARG(thread
);
2279 vty
->t_timeout
= NULL
;
2283 buffer_reset(vty
->lbuf
);
2284 buffer_reset(vty
->obuf
);
2285 vty_out(vty
, "\nVty connection is timed out.\n");
2287 /* Close connection. */
2288 vty
->status
= VTY_CLOSE
;
2294 /* Read up configuration file from file_name. */
2295 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2299 struct vty_error
*ve
;
2300 struct listnode
*node
;
2301 unsigned int line_num
= 0;
2304 /* vty_close won't close stderr; if some config command prints
2305 * something it'll end up there. (not ideal; it'd be beter if output
2306 * from a file-load went to logging instead. Also note that if this
2307 * function is called after daemonizing, stderr will be /dev/null.)
2309 * vty->fd will be -1 from vty_new()
2311 vty
->wfd
= STDERR_FILENO
;
2312 vty
->type
= VTY_FILE
;
2313 vty
->node
= CONFIG_NODE
;
2315 vty
->candidate_config
= config
;
2317 vty
->private_config
= true;
2318 vty
->candidate_config
= nb_config_new(NULL
);
2321 /* Execute configuration file */
2322 ret
= config_from_file(vty
, confp
, &line_num
);
2324 /* Flush any previous errors before printing messages below */
2325 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2327 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2328 const char *message
= NULL
;
2332 case CMD_ERR_AMBIGUOUS
:
2333 message
= "Ambiguous command";
2335 case CMD_ERR_NO_MATCH
:
2336 message
= "No such command";
2339 message
= "Command returned Warning";
2341 case CMD_WARNING_CONFIG_FAILED
:
2342 message
= "Command returned Warning Config Failed";
2344 case CMD_ERR_INCOMPLETE
:
2345 message
= "Command returned Incomplete";
2347 case CMD_ERR_EXEED_ARGC_MAX
:
2349 "Command exceeded maximum number of Arguments";
2352 message
= "Command returned unhandled error message";
2356 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2357 nl
= strchr(ve
->error_buf
, '\n');
2360 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2361 message
, ve
->line_num
, ve
->error_buf
);
2366 * Automatically commit the candidate configuration after
2367 * reading the configuration file.
2369 if (config
== NULL
&& vty
->candidate_config
2370 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2371 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2372 true, "Read configuration file",
2374 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2375 zlog_err("%s: failed to read configuration file.",
2382 static FILE *vty_use_backup_config(const char *fullpath
)
2384 char *fullpath_sav
, *fullpath_tmp
;
2390 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2391 strcpy(fullpath_sav
, fullpath
);
2392 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2394 sav
= open(fullpath_sav
, O_RDONLY
);
2400 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2401 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2403 /* Open file to configuration write. */
2404 tmp
= mkstemp(fullpath_tmp
);
2408 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2411 while ((c
= read(sav
, buffer
, 512)) > 0) {
2412 if (write(tmp
, buffer
, c
) <= 0)
2418 if (rename(fullpath_tmp
, fullpath
) == 0)
2419 ret
= fopen(fullpath
, "r");
2421 unlink(fullpath_tmp
);
2426 unlink(fullpath_tmp
);
2436 /* Read up configuration file from file_name. */
2437 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2438 char *config_default_dir
)
2440 char cwd
[MAXPATHLEN
];
2442 const char *fullpath
;
2444 bool read_success
= false;
2446 /* If -f flag specified. */
2447 if (config_file
!= NULL
) {
2448 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2449 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2452 "%s: failure to determine Current Working Directory %d!",
2454 goto tmp_free_and_out
;
2456 tmp
= XMALLOC(MTYPE_TMP
,
2457 strlen(cwd
) + strlen(config_file
) + 2);
2458 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2461 fullpath
= config_file
;
2463 confp
= fopen(fullpath
, "r");
2465 if (confp
== NULL
) {
2467 EC_LIB_BACKUP_CONFIG
,
2468 "%s: failed to open configuration file %s: %s, checking backup",
2469 __func__
, fullpath
, safe_strerror(errno
));
2471 confp
= vty_use_backup_config(fullpath
);
2474 EC_LIB_BACKUP_CONFIG
,
2475 "WARNING: using backup configuration file!");
2479 "%s: can't open configuration file [%s]",
2480 __func__
, config_file
);
2481 goto tmp_free_and_out
;
2486 host_config_set(config_default_dir
);
2490 struct stat conf_stat
;
2492 /* !!!!PLEASE LEAVE!!!!
2493 * This is NEEDED for use with vtysh -b, or else you can get
2494 * a real configuration food fight with a lot garbage in the
2495 * merged configuration file it creates coming from the per
2496 * daemon configuration files. This also allows the daemons
2497 * to start if there default configuration file is not
2498 * present or ignore them, as needed when using vtysh -b to
2499 * configure the daemons at boot - MAG
2502 /* Stat for vtysh Zebra.conf, if found startup and wait for
2503 * boot configuration
2506 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2507 ret
= stat(integrate_default
, &conf_stat
);
2509 read_success
= true;
2510 goto tmp_free_and_out
;
2514 confp
= fopen(config_default_dir
, "r");
2515 if (confp
== NULL
) {
2518 "%s: failed to open configuration file %s: %s, checking backup",
2519 __func__
, config_default_dir
,
2520 safe_strerror(errno
));
2522 confp
= vty_use_backup_config(config_default_dir
);
2525 EC_LIB_BACKUP_CONFIG
,
2526 "WARNING: using backup configuration file!");
2527 fullpath
= config_default_dir
;
2529 flog_err(EC_LIB_VTY
,
2530 "can't open configuration file [%s]",
2531 config_default_dir
);
2532 goto tmp_free_and_out
;
2535 fullpath
= config_default_dir
;
2538 vty_read_file(config
, confp
);
2539 read_success
= true;
2543 host_config_set(fullpath
);
2546 XFREE(MTYPE_TMP
, tmp
);
2548 return read_success
;
2551 /* Small utility function which output log to the VTY. */
2552 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2553 struct timestamp_control
*ctl
, va_list va
)
2561 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2562 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2566 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2572 /* Async-signal-safe version of vty_log for fixed strings. */
2573 void vty_log_fixed(char *buf
, size_t len
)
2576 struct iovec iov
[2];
2577 char crlf
[4] = "\r\n";
2579 /* vty may not have been initialised */
2583 iov
[0].iov_base
= buf
;
2584 iov
[0].iov_len
= len
;
2585 iov
[1].iov_base
= crlf
;
2588 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2590 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2591 /* N.B. We don't care about the return code, since
2593 most likely just about to die anyway. */
2594 if (writev(vty
->wfd
, iov
, 2) == -1) {
2595 fprintf(stderr
, "Failure to writev: %d\n",
2602 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2604 if (exclusive
&& !vty_config_exclusive_lock(vty
)) {
2605 vty_out(vty
, "VTY configuration is locked by other VTY\n");
2609 vty
->node
= CONFIG_NODE
;
2611 vty
->private_config
= private_config
;
2612 vty
->xpath_index
= 0;
2614 if (private_config
) {
2615 vty
->candidate_config
= nb_config_dup(running_config
);
2616 vty
->candidate_config_base
= 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
);
2629 void vty_config_exit(struct vty
*vty
)
2631 /* Check if there's a pending confirmed commit. */
2632 if (vty
->t_confirmed_commit_timeout
) {
2634 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2635 nb_cli_confirmed_commit_rollback(vty
);
2636 nb_cli_confirmed_commit_clean(vty
);
2639 vty_config_exclusive_unlock(vty
);
2641 if (vty
->candidate_config
) {
2642 if (vty
->private_config
)
2643 nb_config_free(vty
->candidate_config
);
2644 vty
->candidate_config
= NULL
;
2646 if (vty
->candidate_config_base
) {
2647 nb_config_free(vty
->candidate_config_base
);
2648 vty
->candidate_config_base
= NULL
;
2651 vty
->config
= false;
2654 int vty_config_exclusive_lock(struct vty
*vty
)
2656 if (vty_exclusive_lock
== NULL
) {
2657 vty_exclusive_lock
= vty
;
2663 void vty_config_exclusive_unlock(struct vty
*vty
)
2665 if (vty_exclusive_lock
== vty
)
2666 vty_exclusive_lock
= NULL
;
2669 /* Master of the threads. */
2670 static struct thread_master
*vty_master
;
2672 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2674 struct thread
*vty_serv_thread
= NULL
;
2678 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2680 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2684 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2686 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2690 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2694 vty
->t_write
= NULL
;
2695 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2701 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2703 /* Time out treatment. */
2704 if (vty
->v_timeout
) {
2706 thread_cancel(vty
->t_timeout
);
2707 vty
->t_timeout
= NULL
;
2708 thread_add_timer(vty_master
, vty_timeout
, vty
,
2709 vty
->v_timeout
, &vty
->t_timeout
);
2713 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2716 case VTY_TIMEOUT_RESET
:
2717 if (vty
->t_timeout
) {
2718 thread_cancel(vty
->t_timeout
);
2719 vty
->t_timeout
= NULL
;
2721 if (vty
->v_timeout
) {
2722 vty
->t_timeout
= NULL
;
2723 thread_add_timer(vty_master
, vty_timeout
, vty
,
2724 vty
->v_timeout
, &vty
->t_timeout
);
2730 DEFUN_NOSH (config_who
,
2733 "Display who is on vty\n")
2738 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2739 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2740 vty_out(vty
, "%svty[%d] connected from %s.\n",
2741 v
->config
? "*" : " ", i
, v
->address
);
2745 /* Move to vty configuration mode. */
2746 DEFUN_NOSH (line_vty
,
2749 "Configure a terminal line\n"
2750 "Virtual terminal\n")
2752 vty
->node
= VTY_NODE
;
2756 /* Set time out value. */
2757 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2758 const char *sec_str
)
2760 unsigned long timeout
= 0;
2762 /* min_str and sec_str are already checked by parser. So it must be
2763 all digit string. */
2765 timeout
= strtol(min_str
, NULL
, 10);
2769 timeout
+= strtol(sec_str
, NULL
, 10);
2771 vty_timeout_val
= timeout
;
2772 vty
->v_timeout
= timeout
;
2773 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2779 DEFUN (exec_timeout_min
,
2780 exec_timeout_min_cmd
,
2781 "exec-timeout (0-35791)",
2782 "Set timeout value\n"
2783 "Timeout value in minutes\n")
2786 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2789 DEFUN (exec_timeout_sec
,
2790 exec_timeout_sec_cmd
,
2791 "exec-timeout (0-35791) (0-2147483)",
2792 "Set the EXEC timeout\n"
2793 "Timeout in minutes\n"
2794 "Timeout in seconds\n")
2797 int idx_number_2
= 2;
2798 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2799 argv
[idx_number_2
]->arg
);
2802 DEFUN (no_exec_timeout
,
2803 no_exec_timeout_cmd
,
2806 "Set the EXEC timeout\n")
2808 return exec_timeout(vty
, NULL
, NULL
);
2811 /* Set vty access class. */
2812 DEFUN (vty_access_class
,
2813 vty_access_class_cmd
,
2814 "access-class WORD",
2815 "Filter connections based on an IP access list\n"
2819 if (vty_accesslist_name
)
2820 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2822 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2827 /* Clear vty access class. */
2828 DEFUN (no_vty_access_class
,
2829 no_vty_access_class_cmd
,
2830 "no access-class [WORD]",
2832 "Filter connections based on an IP access list\n"
2836 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2837 if (!vty_accesslist_name
2838 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2839 vty_out(vty
, "Access-class is not currently applied to vty\n");
2840 return CMD_WARNING_CONFIG_FAILED
;
2843 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2845 vty_accesslist_name
= NULL
;
2850 /* Set vty access class. */
2851 DEFUN (vty_ipv6_access_class
,
2852 vty_ipv6_access_class_cmd
,
2853 "ipv6 access-class WORD",
2855 "Filter connections based on an IP access list\n"
2856 "IPv6 access list\n")
2859 if (vty_ipv6_accesslist_name
)
2860 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2862 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2867 /* Clear vty access class. */
2868 DEFUN (no_vty_ipv6_access_class
,
2869 no_vty_ipv6_access_class_cmd
,
2870 "no ipv6 access-class [WORD]",
2873 "Filter connections based on an IP access list\n"
2874 "IPv6 access list\n")
2877 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2879 if (!vty_ipv6_accesslist_name
2880 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2882 "IPv6 access-class is not currently applied to vty\n");
2883 return CMD_WARNING_CONFIG_FAILED
;
2886 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2888 vty_ipv6_accesslist_name
= NULL
;
2897 "Enable password checking\n")
2899 no_password_check
= 0;
2903 DEFUN (no_vty_login
,
2907 "Enable password checking\n")
2909 no_password_check
= 1;
2913 DEFUN (service_advanced_vty
,
2914 service_advanced_vty_cmd
,
2915 "service advanced-vty",
2916 "Set up miscellaneous service\n"
2917 "Enable advanced mode vty interface\n")
2923 DEFUN (no_service_advanced_vty
,
2924 no_service_advanced_vty_cmd
,
2925 "no service advanced-vty",
2927 "Set up miscellaneous service\n"
2928 "Enable advanced mode vty interface\n")
2934 DEFUN_NOSH (terminal_monitor
,
2935 terminal_monitor_cmd
,
2937 "Set terminal line parameters\n"
2938 "Copy debug output to the current terminal line\n")
2944 DEFUN_NOSH (terminal_no_monitor
,
2945 terminal_no_monitor_cmd
,
2946 "terminal no monitor",
2947 "Set terminal line parameters\n"
2949 "Copy debug output to the current terminal line\n")
2955 DEFUN_NOSH (no_terminal_monitor
,
2956 no_terminal_monitor_cmd
,
2957 "no terminal monitor",
2959 "Set terminal line parameters\n"
2960 "Copy debug output to the current terminal line\n")
2962 return terminal_no_monitor(self
, vty
, argc
, argv
);
2966 DEFUN_NOSH (show_history
,
2970 "Display the session command history\n")
2974 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2975 if (index
== VTY_MAXHIST
) {
2980 if (vty
->hist
[index
] != NULL
)
2981 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2990 DEFUN (log_commands
,
2994 "Log all commands (can't be unset without restart)\n")
2996 do_log_commands
= 1;
3000 /* Display current configuration. */
3001 static int vty_config_write(struct vty
*vty
)
3003 vty_out(vty
, "line vty\n");
3005 if (vty_accesslist_name
)
3006 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
3008 if (vty_ipv6_accesslist_name
)
3009 vty_out(vty
, " ipv6 access-class %s\n",
3010 vty_ipv6_accesslist_name
);
3013 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3014 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
3015 vty_timeout_val
% 60);
3018 if (no_password_check
)
3019 vty_out(vty
, " no login\n");
3021 if (do_log_commands
)
3022 vty_out(vty
, "log commands\n");
3024 vty_out(vty
, "!\n");
3029 struct cmd_node vty_node
= {
3030 VTY_NODE
, "%s(config-line)# ", 1,
3033 /* Reset all VTY status. */
3034 void vty_reset(void)
3038 struct thread
*vty_serv_thread
;
3040 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3041 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3042 buffer_reset(vty
->lbuf
);
3043 buffer_reset(vty
->obuf
);
3044 vty
->status
= VTY_CLOSE
;
3048 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3049 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3051 thread_cancel(vty_serv_thread
);
3052 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3056 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3058 if (vty_accesslist_name
) {
3059 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3060 vty_accesslist_name
= NULL
;
3063 if (vty_ipv6_accesslist_name
) {
3064 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3065 vty_ipv6_accesslist_name
= NULL
;
3069 static void vty_save_cwd(void)
3071 char cwd
[MAXPATHLEN
];
3074 c
= getcwd(cwd
, MAXPATHLEN
);
3078 * At this point if these go wrong, more than likely
3079 * the whole world is coming down around us
3080 * Hence not worrying about it too much.
3082 if (!chdir(SYSCONFDIR
)) {
3083 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3084 "Failure to chdir to %s, errno: %d",
3088 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3089 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3090 "Failure to getcwd, errno: %d", errno
);
3095 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3096 strcpy(vty_cwd
, cwd
);
3099 char *vty_get_cwd(void)
3104 int vty_shell(struct vty
*vty
)
3106 return vty
->type
== VTY_SHELL
? 1 : 0;
3109 int vty_shell_serv(struct vty
*vty
)
3111 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3114 void vty_init_vtysh(void)
3116 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3119 /* Install vty's own commands like `who' command. */
3120 void vty_init(struct thread_master
*master_thread
)
3122 /* For further configuration read, preserve current directory. */
3125 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3127 vty_master
= master_thread
;
3129 atexit(vty_stdio_atexit
);
3131 /* Initilize server thread vector. */
3132 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3134 /* Install bgp top node. */
3135 install_node(&vty_node
, vty_config_write
);
3137 install_element(VIEW_NODE
, &config_who_cmd
);
3138 install_element(VIEW_NODE
, &show_history_cmd
);
3139 install_element(CONFIG_NODE
, &line_vty_cmd
);
3140 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3141 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3142 install_element(CONFIG_NODE
, &show_history_cmd
);
3143 install_element(CONFIG_NODE
, &log_commands_cmd
);
3144 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3145 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3146 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3148 install_default(VTY_NODE
);
3149 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3150 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3151 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3152 install_element(VTY_NODE
, &vty_access_class_cmd
);
3153 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3154 install_element(VTY_NODE
, &vty_login_cmd
);
3155 install_element(VTY_NODE
, &no_vty_login_cmd
);
3156 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3157 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3160 void vty_terminate(void)
3162 XFREE(MTYPE_TMP
, vty_cwd
);
3164 if (vtyvec
&& Vvty_serv_thread
) {
3166 vector_free(vtyvec
);
3167 vector_free(Vvty_serv_thread
);
3169 Vvty_serv_thread
= NULL
;