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"
44 #include <arpa/telnet.h>
47 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
48 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
49 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
64 static void vty_event(enum event
, int, struct vty
*);
66 /* Extern host structure from command.c */
67 extern struct host host
;
69 /* Vector which store each vty structure. */
72 /* Vty timeout value. */
73 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
75 /* Vty access-class command */
76 static char *vty_accesslist_name
= NULL
;
78 /* Vty access-calss for IPv6. */
79 static char *vty_ipv6_accesslist_name
= NULL
;
81 /* VTY server thread. */
82 static vector Vvty_serv_thread
;
84 /* Current directory. */
88 static int vty_config
;
89 static int vty_config_is_lockless
= 0;
91 /* Login password check. */
92 static int no_password_check
= 0;
94 /* Integrated configuration file path */
95 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
97 static int do_log_commands
= 0;
99 void vty_frame(struct vty
*vty
, const char *format
, ...)
103 va_start(args
, format
);
104 vsnprintf(vty
->frame
+ vty
->frame_pos
,
105 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
106 vty
->frame_pos
= strlen(vty
->frame
);
110 void vty_endframe(struct vty
*vty
, const char *endtext
)
112 if (vty
->frame_pos
== 0 && endtext
)
113 vty_out(vty
, "%s", endtext
);
117 bool vty_set_include(struct vty
*vty
, const char *regexp
)
125 regfree(&vty
->include
);
131 errcode
= regcomp(&vty
->include
, regexp
,
132 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
135 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
136 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
144 /* VTY standard output function. */
145 int vty_out(struct vty
*vty
, const char *format
, ...)
154 if (vty
->frame_pos
) {
156 vty_out(vty
, "%s", vty
->frame
);
159 /* Try to write to initial buffer. */
160 va_start(args
, format
);
161 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
164 /* Initial buffer is not enough. */
165 if (len
< 0 || len
>= size
) {
172 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
176 va_start(args
, format
);
177 len
= vsnprintf(p
, size
, format
, args
);
180 if (len
> -1 && len
< size
)
185 /* When initial buffer is enough to store all output. */
191 vector lines
= frrstr_split_vec(p
, "\n");
193 /* Place first value in the cache */
194 char *firstline
= vector_slot(lines
, 0);
195 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
197 /* If our split returned more than one entry, time to filter */
198 if (vector_active(lines
) > 1) {
200 * returned string is MTYPE_TMP so it matches the MTYPE
201 * of everything else in the vector
203 char *bstr
= buffer_getstr(vty
->lbuf
);
204 buffer_reset(vty
->lbuf
);
205 XFREE(MTYPE_TMP
, lines
->index
[0]);
206 vector_set_index(lines
, 0, bstr
);
207 frrstr_filter_vec(lines
, &vty
->include
);
208 vector_compact(lines
);
210 * Consider the string "foo\n". If the regex is an empty string
211 * and the line ended with a newline, then the vector will look
217 * If the regex isn't empty, the vector will look like:
221 * In this case we'd like to preserve the newline, so we add
222 * the empty string [1] as in the first example.
224 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
225 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
226 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
228 filtered
= frrstr_join_vec(lines
, "\n");
234 frrstr_strvec_free(lines
);
245 /* print with crlf replacement */
246 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
250 fprintf(vty
->of
, "%s", filtered
);
256 /* print without crlf replacement */
257 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
263 if (vty
->filter
&& filtered
)
264 XFREE(MTYPE_TMP
, filtered
);
266 /* If p is not different with buf, it is allocated buffer. */
268 XFREE(MTYPE_VTY_OUT_BUF
, p
);
273 static int vty_log_out(struct vty
*vty
, const char *level
,
274 const char *proto_str
, const char *format
,
275 struct timestamp_control
*ctl
, va_list va
)
281 if (!ctl
->already_rendered
) {
282 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
284 ctl
->already_rendered
= 1;
286 if (ctl
->len
+ 1 >= sizeof(buf
))
288 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
293 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
296 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
297 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
300 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
301 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
307 if (write(vty
->wfd
, buf
, len
) < 0) {
308 if (ERRNO_IO_RETRY(errno
))
309 /* Kernel buffer is full, probably too much debugging
311 drop the data and ignore. */
313 /* Fatal I/O error. */
315 0; /* disable monitoring to avoid infinite recursion */
316 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
317 __func__
, vty
->fd
, safe_strerror(errno
));
318 buffer_reset(vty
->obuf
);
319 buffer_reset(vty
->lbuf
);
320 /* cannot call vty_close, because a parent routine may still try
321 to access the vty struct */
322 vty
->status
= VTY_CLOSE
;
323 shutdown(vty
->fd
, SHUT_RDWR
);
329 /* Output current time to the vty. */
330 void vty_time_print(struct vty
*vty
, int cr
)
332 char buf
[QUAGGA_TIMESTAMP_LEN
];
334 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
335 zlog_info("quagga_timestamp error");
339 vty_out(vty
, "%s\n", buf
);
341 vty_out(vty
, "%s ", buf
);
346 /* Say hello to vty interface. */
347 void vty_hello(struct vty
*vty
)
353 f
= fopen(host
.motdfile
, "r");
355 while (fgets(buf
, sizeof(buf
), f
)) {
357 /* work backwards to ignore trailling isspace()
359 for (s
= buf
+ strlen(buf
);
360 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
363 vty_out(vty
, "%s\n", buf
);
367 vty_out(vty
, "MOTD file not found\n");
368 } else if (host
.motd
)
369 vty_out(vty
, "%s", host
.motd
);
372 /* Put out prompt and wait input from user. */
373 static void vty_prompt(struct vty
*vty
)
375 if (vty
->type
== VTY_TERM
) {
376 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
380 /* Send WILL TELOPT_ECHO to remote server. */
381 static void vty_will_echo(struct vty
*vty
)
383 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
384 vty_out(vty
, "%s", cmd
);
387 /* Make suppress Go-Ahead telnet option. */
388 static void vty_will_suppress_go_ahead(struct vty
*vty
)
390 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
391 vty_out(vty
, "%s", cmd
);
394 /* Make don't use linemode over telnet. */
395 static void vty_dont_linemode(struct vty
*vty
)
397 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
398 vty_out(vty
, "%s", cmd
);
401 /* Use window size. */
402 static void vty_do_window_size(struct vty
*vty
)
404 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
405 vty_out(vty
, "%s", cmd
);
408 #if 0 /* Currently not used. */
409 /* Make don't use lflow vty interface. */
411 vty_dont_lflow_ahead (struct vty
*vty
)
413 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
414 vty_out (vty
, "%s", cmd
);
418 /* Authentication of vty */
419 static void vty_auth(struct vty
*vty
, char *buf
)
422 enum node_type next_node
= 0;
424 char *crypt(const char *, const char *);
429 passwd
= host
.password_encrypt
;
431 passwd
= host
.password
;
433 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
435 next_node
= VIEW_NODE
;
437 case AUTH_ENABLE_NODE
:
439 passwd
= host
.enable_encrypt
;
441 passwd
= host
.enable
;
442 next_node
= ENABLE_NODE
;
448 fail
= strcmp(crypt(buf
, passwd
), passwd
);
450 fail
= strcmp(buf
, passwd
);
456 vty
->node
= next_node
; /* Success ! */
459 if (vty
->fail
>= 3) {
460 if (vty
->node
== AUTH_NODE
) {
462 "%% Bad passwords, too many failures!\n");
463 vty
->status
= VTY_CLOSE
;
465 /* AUTH_ENABLE_NODE */
468 "%% Bad enable passwords, too many failures!\n");
469 vty
->status
= VTY_CLOSE
;
475 /* Command execution over the vty interface. */
476 static int vty_command(struct vty
*vty
, char *buf
)
479 const char *protocolname
;
483 * Log non empty command lines
488 /* Skip white spaces. */
489 while (isspace((int)*cp
) && *cp
!= '\0')
492 if (cp
!= NULL
&& *cp
!= '\0') {
494 char vty_str
[VTY_BUFSIZ
];
495 char prompt_str
[VTY_BUFSIZ
];
497 /* format the base vty info */
498 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
500 for (i
= 0; i
< vector_active(vtyvec
); i
++)
501 if (vty
== vector_slot(vtyvec
, i
)) {
502 snprintf(vty_str
, sizeof(vty_str
),
503 "vty[%d]@%s", i
, vty
->address
);
507 /* format the prompt */
508 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
511 /* now log the command */
512 zlog_err("%s%s", prompt_str
, buf
);
515 #ifdef CONSUMED_TIME_CHECK
519 unsigned long realtime
, cputime
;
522 #endif /* CONSUMED_TIME_CHECK */
524 ret
= cmd_execute(vty
, buf
, NULL
, 0);
526 /* Get the name of the protocol if any */
527 protocolname
= frr_protoname
;
529 #ifdef CONSUMED_TIME_CHECK
531 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
532 > CONSUMED_TIME_CHECK
)
533 /* Warn about CPU hog that must be fixed. */
535 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
536 realtime
/ 1000, cputime
/ 1000, buf
);
538 #endif /* CONSUMED_TIME_CHECK */
540 if (ret
!= CMD_SUCCESS
)
543 if (vty
->type
== VTY_FILE
)
544 vty_out(vty
, "Warning...\n");
546 case CMD_ERR_AMBIGUOUS
:
547 vty_out(vty
, "%% Ambiguous command.\n");
549 case CMD_ERR_NO_MATCH
:
550 vty_out(vty
, "%% [%s] Unknown command: %s\n",
553 case CMD_ERR_INCOMPLETE
:
554 vty_out(vty
, "%% Command incomplete.\n");
561 static const char telnet_backward_char
= 0x08;
562 static const char telnet_space_char
= ' ';
564 /* Basic function to write buffer to vty. */
565 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
567 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
570 /* Should we do buffering here ? And make vty_flush (vty) ? */
571 buffer_put(vty
->obuf
, buf
, nbytes
);
574 /* Basic function to insert character into vty. */
575 static void vty_self_insert(struct vty
*vty
, char c
)
580 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
583 length
= vty
->length
- vty
->cp
;
584 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
585 vty
->buf
[vty
->cp
] = c
;
587 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
588 for (i
= 0; i
< length
; i
++)
589 vty_write(vty
, &telnet_backward_char
, 1);
594 vty
->buf
[vty
->length
] = '\0';
597 /* Self insert character 'c' in overwrite mode. */
598 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
600 if (vty
->cp
== vty
->length
) {
601 vty_self_insert(vty
, c
);
605 vty
->buf
[vty
->cp
++] = c
;
606 vty_write(vty
, &c
, 1);
610 * Insert a string into vty->buf at the current cursor position.
612 * If the resultant string would be larger than VTY_BUFSIZ it is
615 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
617 if (vty
->cp
== VTY_BUFSIZ
)
620 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
621 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
623 vty
->length
= MAX(vty
->cp
, vty
->length
);
624 vty
->buf
[vty
->length
] = '\0';
625 vty_write(vty
, str
, nwrite
);
628 /* Forward character. */
629 static void vty_forward_char(struct vty
*vty
)
631 if (vty
->cp
< vty
->length
) {
632 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
637 /* Backward character. */
638 static void vty_backward_char(struct vty
*vty
)
642 vty_write(vty
, &telnet_backward_char
, 1);
646 /* Move to the beginning of the line. */
647 static void vty_beginning_of_line(struct vty
*vty
)
650 vty_backward_char(vty
);
653 /* Move to the end of the line. */
654 static void vty_end_of_line(struct vty
*vty
)
656 while (vty
->cp
< vty
->length
)
657 vty_forward_char(vty
);
660 static void vty_kill_line_from_beginning(struct vty
*);
661 static void vty_redraw_line(struct vty
*);
663 /* Print command line history. This function is called from
664 vty_next_line and vty_previous_line. */
665 static void vty_history_print(struct vty
*vty
)
669 vty_kill_line_from_beginning(vty
);
671 /* Get previous line from history buffer */
672 length
= strlen(vty
->hist
[vty
->hp
]);
673 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
674 vty
->cp
= vty
->length
= length
;
675 vty
->buf
[vty
->length
] = '\0';
677 /* Redraw current line */
678 vty_redraw_line(vty
);
681 /* Show next command line history. */
682 static void vty_next_line(struct vty
*vty
)
686 if (vty
->hp
== vty
->hindex
)
689 /* Try is there history exist or not. */
691 if (try_index
== (VTY_MAXHIST
- 1))
696 /* If there is not history return. */
697 if (vty
->hist
[try_index
] == NULL
)
702 vty_history_print(vty
);
705 /* Show previous command line history. */
706 static void vty_previous_line(struct vty
*vty
)
712 try_index
= VTY_MAXHIST
- 1;
716 if (vty
->hist
[try_index
] == NULL
)
721 vty_history_print(vty
);
724 /* This function redraw all of the command line character. */
725 static void vty_redraw_line(struct vty
*vty
)
727 vty_write(vty
, vty
->buf
, vty
->length
);
728 vty
->cp
= vty
->length
;
732 static void vty_forward_word(struct vty
*vty
)
734 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
735 vty_forward_char(vty
);
737 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
738 vty_forward_char(vty
);
741 /* Backward word without skipping training space. */
742 static void vty_backward_pure_word(struct vty
*vty
)
744 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
745 vty_backward_char(vty
);
749 static void vty_backward_word(struct vty
*vty
)
751 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
752 vty_backward_char(vty
);
754 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
755 vty_backward_char(vty
);
758 /* When '^D' is typed at the beginning of the line we move to the down
760 static void vty_down_level(struct vty
*vty
)
768 /* When '^Z' is received from vty, move down to the enable mode. */
769 static void vty_end_config(struct vty
*vty
)
788 case BGP_VRF_POLICY_NODE
:
789 case BGP_VNC_DEFAULTS_NODE
:
790 case BGP_VNC_NVE_GROUP_NODE
:
791 case BGP_VNC_L2_GROUP_NODE
:
806 case LDP_IPV4_IFACE_NODE
:
807 case LDP_IPV6_IFACE_NODE
:
809 case LDP_PSEUDOWIRE_NODE
:
812 case KEYCHAIN_KEY_NODE
:
814 case BGP_EVPN_VNI_NODE
:
815 vty_config_unlock(vty
);
816 vty
->node
= ENABLE_NODE
;
819 /* Unknown node, we have to ignore it. */
827 /* Delete a charcter at the current point. */
828 static void vty_delete_char(struct vty
*vty
)
833 if (vty
->length
== 0) {
838 if (vty
->cp
== vty
->length
)
839 return; /* completion need here? */
841 size
= vty
->length
- vty
->cp
;
844 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
845 vty
->buf
[vty
->length
] = '\0';
847 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
850 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
851 vty_write(vty
, &telnet_space_char
, 1);
853 for (i
= 0; i
< size
; i
++)
854 vty_write(vty
, &telnet_backward_char
, 1);
857 /* Delete a character before the point. */
858 static void vty_delete_backward_char(struct vty
*vty
)
863 vty_backward_char(vty
);
864 vty_delete_char(vty
);
867 /* Kill rest of line from current point. */
868 static void vty_kill_line(struct vty
*vty
)
873 size
= vty
->length
- vty
->cp
;
878 for (i
= 0; i
< size
; i
++)
879 vty_write(vty
, &telnet_space_char
, 1);
880 for (i
= 0; i
< size
; i
++)
881 vty_write(vty
, &telnet_backward_char
, 1);
883 memset(&vty
->buf
[vty
->cp
], 0, size
);
884 vty
->length
= vty
->cp
;
887 /* Kill line from the beginning. */
888 static void vty_kill_line_from_beginning(struct vty
*vty
)
890 vty_beginning_of_line(vty
);
894 /* Delete a word before the point. */
895 static void vty_forward_kill_word(struct vty
*vty
)
897 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
898 vty_delete_char(vty
);
899 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
900 vty_delete_char(vty
);
903 /* Delete a word before the point. */
904 static void vty_backward_kill_word(struct vty
*vty
)
906 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
907 vty_delete_backward_char(vty
);
908 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
909 vty_delete_backward_char(vty
);
912 /* Transpose chars before or at the point. */
913 static void vty_transpose_chars(struct vty
*vty
)
917 /* If length is short or point is near by the beginning of line then
919 if (vty
->length
< 2 || vty
->cp
< 1)
922 /* In case of point is located at the end of the line. */
923 if (vty
->cp
== vty
->length
) {
924 c1
= vty
->buf
[vty
->cp
- 1];
925 c2
= vty
->buf
[vty
->cp
- 2];
927 vty_backward_char(vty
);
928 vty_backward_char(vty
);
929 vty_self_insert_overwrite(vty
, c1
);
930 vty_self_insert_overwrite(vty
, c2
);
932 c1
= vty
->buf
[vty
->cp
];
933 c2
= vty
->buf
[vty
->cp
- 1];
935 vty_backward_char(vty
);
936 vty_self_insert_overwrite(vty
, c1
);
937 vty_self_insert_overwrite(vty
, c2
);
941 /* Do completion at vty interface. */
942 static void vty_complete_command(struct vty
*vty
)
946 char **matched
= NULL
;
949 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
952 vline
= cmd_make_strvec(vty
->buf
);
956 /* In case of 'help \t'. */
957 if (isspace((int)vty
->buf
[vty
->length
- 1]))
958 vector_set(vline
, NULL
);
960 matched
= cmd_complete_command(vline
, vty
, &ret
);
962 cmd_free_strvec(vline
);
966 case CMD_ERR_AMBIGUOUS
:
967 vty_out(vty
, "%% Ambiguous command.\n");
969 vty_redraw_line(vty
);
971 case CMD_ERR_NO_MATCH
:
972 /* vty_out (vty, "%% There is no matched command.\n"); */
974 vty_redraw_line(vty
);
976 case CMD_COMPLETE_FULL_MATCH
:
978 /* 2016-11-28 equinox -- need to debug, SEGV here */
979 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
981 vty_redraw_line(vty
);
985 vty_redraw_line(vty
);
986 vty_backward_pure_word(vty
);
987 vty_insert_word_overwrite(vty
, matched
[0]);
988 vty_self_insert(vty
, ' ');
989 XFREE(MTYPE_COMPLETION
, matched
[0]);
991 case CMD_COMPLETE_MATCH
:
993 vty_redraw_line(vty
);
994 vty_backward_pure_word(vty
);
995 vty_insert_word_overwrite(vty
, matched
[0]);
996 XFREE(MTYPE_COMPLETION
, matched
[0]);
998 case CMD_COMPLETE_LIST_MATCH
:
999 for (i
= 0; matched
[i
] != NULL
; i
++) {
1000 if (i
!= 0 && ((i
% 6) == 0))
1002 vty_out(vty
, "%-10s ", matched
[i
]);
1003 XFREE(MTYPE_COMPLETION
, matched
[i
]);
1008 vty_redraw_line(vty
);
1010 case CMD_ERR_NOTHING_TODO
:
1012 vty_redraw_line(vty
);
1018 XFREE(MTYPE_TMP
, matched
);
1021 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
1022 unsigned int desc_width
, struct cmd_token
*token
)
1025 const char *cmd
, *p
;
1030 if (desc_width
<= 0) {
1031 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1035 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1037 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1038 for (pos
= desc_width
; pos
> 0; pos
--)
1039 if (*(p
+ pos
) == ' ')
1045 strncpy(buf
, p
, pos
);
1047 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1052 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1054 XFREE(MTYPE_TMP
, buf
);
1057 /* Describe matched command function. */
1058 static void vty_describe_command(struct vty
*vty
)
1063 unsigned int i
, width
, desc_width
;
1064 struct cmd_token
*token
, *token_cr
= NULL
;
1066 vline
= cmd_make_strvec(vty
->buf
);
1068 /* In case of '> ?'. */
1069 if (vline
== NULL
) {
1070 vline
= vector_init(1);
1071 vector_set(vline
, NULL
);
1072 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1073 vector_set(vline
, NULL
);
1075 describe
= cmd_describe_command(vline
, vty
, &ret
);
1079 /* Ambiguous error. */
1081 case CMD_ERR_AMBIGUOUS
:
1082 vty_out(vty
, "%% Ambiguous command.\n");
1085 case CMD_ERR_NO_MATCH
:
1086 vty_out(vty
, "%% There is no matched command.\n");
1091 /* Get width of command string. */
1093 for (i
= 0; i
< vector_active(describe
); i
++)
1094 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1097 if (token
->text
[0] == '\0')
1100 len
= strlen(token
->text
);
1106 /* Get width of description string. */
1107 desc_width
= vty
->width
- (width
+ 6);
1109 /* Print out description. */
1110 for (i
= 0; i
< vector_active(describe
); i
++)
1111 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1112 if (token
->text
[0] == '\0')
1115 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1121 vty_out(vty
, " %-s\n", token
->text
);
1122 else if (desc_width
>= strlen(token
->desc
))
1123 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1126 vty_describe_fold(vty
, width
, desc_width
,
1129 if (IS_VARYING_TOKEN(token
->type
)) {
1130 const char *ref
= vector_slot(
1131 vline
, vector_active(vline
) - 1);
1133 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1134 cmd_variable_complete(token
, ref
, varcomps
);
1136 if (vector_active(varcomps
) > 0) {
1137 char *ac
= cmd_variable_comp2str(
1138 varcomps
, vty
->width
);
1139 vty_out(vty
, "%s\n", ac
);
1140 XFREE(MTYPE_TMP
, ac
);
1143 vector_free(varcomps
);
1146 vty_out (vty
, " %-*s %s\n", width
1147 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1148 desc
->str
? desc
->str
: "");
1152 if ((token
= token_cr
)) {
1154 vty_out(vty
, " %-s\n", token
->text
);
1155 else if (desc_width
>= strlen(token
->desc
))
1156 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1159 vty_describe_fold(vty
, width
, desc_width
, token
);
1163 cmd_free_strvec(vline
);
1165 vector_free(describe
);
1168 vty_redraw_line(vty
);
1171 static void vty_clear_buf(struct vty
*vty
)
1173 memset(vty
->buf
, 0, vty
->max
);
1176 /* ^C stop current input and do not add command line to the history. */
1177 static void vty_stop_input(struct vty
*vty
)
1179 vty
->cp
= vty
->length
= 0;
1183 switch (vty
->node
) {
1186 /* Nothing to do. */
1189 case INTERFACE_NODE
:
1203 case LDP_IPV4_IFACE_NODE
:
1204 case LDP_IPV6_IFACE_NODE
:
1205 case LDP_L2VPN_NODE
:
1206 case LDP_PSEUDOWIRE_NODE
:
1209 case KEYCHAIN_KEY_NODE
:
1211 vty_config_unlock(vty
);
1212 vty
->node
= ENABLE_NODE
;
1215 /* Unknown node, we have to ignore it. */
1220 /* Set history pointer to the latest one. */
1221 vty
->hp
= vty
->hindex
;
1224 /* Add current command line to the history buffer. */
1225 static void vty_hist_add(struct vty
*vty
)
1229 if (vty
->length
== 0)
1232 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1234 /* Ignore the same string as previous one. */
1235 if (vty
->hist
[index
])
1236 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1237 vty
->hp
= vty
->hindex
;
1241 /* Insert history entry. */
1242 if (vty
->hist
[vty
->hindex
])
1243 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1244 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1246 /* History index rotation. */
1248 if (vty
->hindex
== VTY_MAXHIST
)
1251 vty
->hp
= vty
->hindex
;
1254 /* #define TELNET_OPTION_DEBUG */
1256 /* Get telnet window size. */
1257 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1259 #ifdef TELNET_OPTION_DEBUG
1262 for (i
= 0; i
< nbytes
; i
++) {
1265 vty_out(vty
, "IAC ");
1268 vty_out(vty
, "WILL ");
1271 vty_out(vty
, "WONT ");
1274 vty_out(vty
, "DO ");
1277 vty_out(vty
, "DONT ");
1280 vty_out(vty
, "SB ");
1283 vty_out(vty
, "SE ");
1286 vty_out(vty
, "TELOPT_ECHO \n");
1289 vty_out(vty
, "TELOPT_SGA \n");
1292 vty_out(vty
, "TELOPT_NAWS \n");
1295 vty_out(vty
, "%x ", buf
[i
]);
1301 #endif /* TELNET_OPTION_DEBUG */
1306 vty
->iac_sb_in_progress
= 1;
1310 if (!vty
->iac_sb_in_progress
)
1313 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1314 vty
->iac_sb_in_progress
= 0;
1317 switch (vty
->sb_buf
[0]) {
1319 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1321 "RFC 1073 violation detected: telnet NAWS option "
1322 "should send %d characters, but we received %lu",
1324 (unsigned long)vty
->sb_len
);
1325 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1327 "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1328 "too small to handle the telnet NAWS option",
1329 (unsigned long)sizeof(vty
->sb_buf
),
1330 TELNET_NAWS_SB_LEN
);
1332 vty
->width
= ((vty
->sb_buf
[1] << 8)
1334 vty
->height
= ((vty
->sb_buf
[3] << 8)
1336 #ifdef TELNET_OPTION_DEBUG
1338 "TELNET NAWS window size negotiation completed: "
1339 "width %d, height %d\n",
1340 vty
->width
, vty
->height
);
1345 vty
->iac_sb_in_progress
= 0;
1355 /* Execute current command line. */
1356 static int vty_execute(struct vty
*vty
)
1362 switch (vty
->node
) {
1364 case AUTH_ENABLE_NODE
:
1365 vty_auth(vty
, vty
->buf
);
1368 ret
= vty_command(vty
, vty
->buf
);
1369 if (vty
->type
== VTY_TERM
)
1374 /* Clear command line buffer. */
1375 vty
->cp
= vty
->length
= 0;
1378 if (vty
->status
!= VTY_CLOSE
)
1384 #define CONTROL(X) ((X) - '@')
1385 #define VTY_NORMAL 0
1386 #define VTY_PRE_ESCAPE 1
1387 #define VTY_ESCAPE 2
1389 /* Escape character command map. */
1390 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1394 vty_previous_line(vty
);
1400 vty_forward_char(vty
);
1403 vty_backward_char(vty
);
1409 /* Go back to normal mode. */
1410 vty
->escape
= VTY_NORMAL
;
1413 /* Quit print out to the buffer. */
1414 static void vty_buffer_reset(struct vty
*vty
)
1416 buffer_reset(vty
->obuf
);
1417 buffer_reset(vty
->lbuf
);
1419 vty_redraw_line(vty
);
1422 /* Read data via vty socket. */
1423 static int vty_read(struct thread
*thread
)
1427 unsigned char buf
[VTY_READ_BUFSIZ
];
1429 int vty_sock
= THREAD_FD(thread
);
1430 struct vty
*vty
= THREAD_ARG(thread
);
1433 /* Read raw data from socket */
1434 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1436 if (ERRNO_IO_RETRY(errno
)) {
1437 vty_event(VTY_READ
, vty_sock
, vty
);
1440 vty
->monitor
= 0; /* disable monitoring to avoid
1441 infinite recursion */
1443 "%s: read error on vty client fd %d, closing: %s",
1444 __func__
, vty
->fd
, safe_strerror(errno
));
1445 buffer_reset(vty
->obuf
);
1446 buffer_reset(vty
->lbuf
);
1448 vty
->status
= VTY_CLOSE
;
1451 for (i
= 0; i
< nbytes
; i
++) {
1452 if (buf
[i
] == IAC
) {
1461 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1462 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1463 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1469 /* In case of telnet command */
1471 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1478 if (vty
->status
== VTY_MORE
) {
1483 vty_buffer_reset(vty
);
1485 #if 0 /* More line does not work for "show ip bgp". */
1488 vty
->status
= VTY_MORELINE
;
1497 /* Escape character. */
1498 if (vty
->escape
== VTY_ESCAPE
) {
1499 vty_escape_map(buf
[i
], vty
);
1503 /* Pre-escape status. */
1504 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1507 vty
->escape
= VTY_ESCAPE
;
1510 vty_backward_word(vty
);
1511 vty
->escape
= VTY_NORMAL
;
1514 vty_forward_word(vty
);
1515 vty
->escape
= VTY_NORMAL
;
1518 vty_forward_kill_word(vty
);
1519 vty
->escape
= VTY_NORMAL
;
1523 vty_backward_kill_word(vty
);
1524 vty
->escape
= VTY_NORMAL
;
1527 vty
->escape
= VTY_NORMAL
;
1535 vty_beginning_of_line(vty
);
1538 vty_backward_char(vty
);
1541 vty_stop_input(vty
);
1544 vty_delete_char(vty
);
1547 vty_end_of_line(vty
);
1550 vty_forward_char(vty
);
1554 vty_delete_backward_char(vty
);
1563 vty_previous_line(vty
);
1566 vty_transpose_chars(vty
);
1569 vty_kill_line_from_beginning(vty
);
1572 vty_backward_kill_word(vty
);
1575 vty_end_config(vty
);
1583 vty_complete_command(vty
);
1586 if (vty
->node
== AUTH_NODE
1587 || vty
->node
== AUTH_ENABLE_NODE
)
1588 vty_self_insert(vty
, buf
[i
]);
1590 vty_describe_command(vty
);
1593 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1594 vty
->escape
= VTY_ESCAPE
;
1597 vty
->escape
= VTY_PRE_ESCAPE
;
1600 if (buf
[i
] > 31 && buf
[i
] < 127)
1601 vty_self_insert(vty
, buf
[i
]);
1607 if (vty
->status
== VTY_CLOSE
)
1610 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1611 vty_event(VTY_READ
, vty_sock
, vty
);
1616 /* Flush buffer to the vty. */
1617 static int vty_flush(struct thread
*thread
)
1620 buffer_status_t flushrc
;
1621 int vty_sock
= THREAD_FD(thread
);
1622 struct vty
*vty
= THREAD_ARG(thread
);
1624 vty
->t_write
= NULL
;
1626 /* Tempolary disable read thread. */
1627 if ((vty
->lines
== 0) && vty
->t_read
) {
1628 thread_cancel(vty
->t_read
);
1632 /* Function execution continue. */
1633 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1635 /* N.B. if width is 0, that means we don't know the window size. */
1636 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1637 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1638 else if (vty
->status
== VTY_MORELINE
)
1639 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1642 flushrc
= buffer_flush_window(
1643 vty
->obuf
, vty_sock
, vty
->width
,
1644 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1648 0; /* disable monitoring to avoid infinite recursion */
1649 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1651 buffer_reset(vty
->lbuf
);
1652 buffer_reset(vty
->obuf
);
1656 if (vty
->status
== VTY_CLOSE
)
1659 vty
->status
= VTY_NORMAL
;
1660 if (vty
->lines
== 0)
1661 vty_event(VTY_READ
, vty_sock
, vty
);
1664 case BUFFER_PENDING
:
1665 /* There is more data waiting to be written. */
1666 vty
->status
= VTY_MORE
;
1667 if (vty
->lines
== 0)
1668 vty_event(VTY_WRITE
, vty_sock
, vty
);
1675 /* Allocate new vty struct. */
1676 struct vty
*vty_new()
1678 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1680 new->fd
= new->wfd
= -1;
1682 new->lbuf
= buffer_new(0);
1683 new->obuf
= buffer_new(0); /* Use default buffer size. */
1684 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1685 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1686 new->max
= VTY_BUFSIZ
;
1692 /* allocate and initialise vty */
1693 static struct vty
*vty_new_init(int vty_sock
)
1699 vty
->wfd
= vty_sock
;
1700 vty
->type
= VTY_TERM
;
1701 vty
->node
= AUTH_NODE
;
1706 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1709 vector_set_index(vtyvec
, vty_sock
, vty
);
1710 vty
->status
= VTY_NORMAL
;
1713 vty
->iac_sb_in_progress
= 0;
1719 /* Create new vty structure. */
1720 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1722 char buf
[SU_ADDRSTRLEN
];
1725 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1727 /* Allocate new vty structure and set up default values. */
1728 vty
= vty_new_init(vty_sock
);
1730 /* configurable parameters not part of basic init */
1731 vty
->v_timeout
= vty_timeout_val
;
1732 strcpy(vty
->address
, buf
);
1733 if (no_password_check
) {
1735 vty
->node
= ENABLE_NODE
;
1737 vty
->node
= VIEW_NODE
;
1739 if (host
.lines
>= 0)
1740 vty
->lines
= host
.lines
;
1742 if (!no_password_check
) {
1743 /* Vty is not available if password isn't set. */
1744 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1745 vty_out(vty
, "Vty password is not set.\n");
1746 vty
->status
= VTY_CLOSE
;
1752 /* Say hello to the world. */
1754 if (!no_password_check
)
1755 vty_out(vty
, "\nUser Access Verification\n\n");
1757 /* Setting up terminal. */
1759 vty_will_suppress_go_ahead(vty
);
1761 vty_dont_linemode(vty
);
1762 vty_do_window_size(vty
);
1763 /* vty_dont_lflow_ahead (vty); */
1767 /* Add read/write thread. */
1768 vty_event(VTY_WRITE
, vty_sock
, vty
);
1769 vty_event(VTY_READ
, vty_sock
, vty
);
1774 /* create vty for stdio */
1775 static struct termios stdio_orig_termios
;
1776 static struct vty
*stdio_vty
= NULL
;
1777 static bool stdio_termios
= false;
1778 static void (*stdio_vty_atclose
)(int isexit
);
1780 static void vty_stdio_reset(int isexit
)
1784 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1785 stdio_termios
= false;
1789 if (stdio_vty_atclose
)
1790 stdio_vty_atclose(isexit
);
1791 stdio_vty_atclose
= NULL
;
1795 static void vty_stdio_atexit(void)
1800 void vty_stdio_suspend(void)
1805 if (stdio_vty
->t_write
)
1806 thread_cancel(stdio_vty
->t_write
);
1807 if (stdio_vty
->t_read
)
1808 thread_cancel(stdio_vty
->t_read
);
1809 if (stdio_vty
->t_timeout
)
1810 thread_cancel(stdio_vty
->t_timeout
);
1813 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1814 stdio_termios
= false;
1817 void vty_stdio_resume(void)
1822 if (!tcgetattr(0, &stdio_orig_termios
)) {
1823 struct termios termios
;
1825 termios
= stdio_orig_termios
;
1826 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1827 | IGNCR
| ICRNL
| IXON
);
1828 termios
.c_oflag
&= ~OPOST
;
1829 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1830 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1831 termios
.c_cflag
|= CS8
;
1832 tcsetattr(0, TCSANOW
, &termios
);
1833 stdio_termios
= true;
1836 vty_prompt(stdio_vty
);
1838 /* Add read/write thread. */
1839 vty_event(VTY_WRITE
, 1, stdio_vty
);
1840 vty_event(VTY_READ
, 0, stdio_vty
);
1843 void vty_stdio_close(void)
1847 vty_close(stdio_vty
);
1850 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1854 /* refuse creating two vtys on stdio */
1858 vty
= stdio_vty
= vty_new_init(0);
1859 stdio_vty_atclose
= atclose
;
1862 /* always have stdio vty in a known _unchangeable_ state, don't want
1864 * to have any effect here to make sure scripting this works as intended
1866 vty
->node
= ENABLE_NODE
;
1868 strcpy(vty
->address
, "console");
1874 /* Accept connection from the network. */
1875 static int vty_accept(struct thread
*thread
)
1883 struct access_list
*acl
= NULL
;
1884 char buf
[SU_ADDRSTRLEN
];
1886 accept_sock
= THREAD_FD(thread
);
1888 /* We continue hearing vty socket. */
1889 vty_event(VTY_SERV
, accept_sock
, NULL
);
1891 memset(&su
, 0, sizeof(union sockunion
));
1893 /* We can handle IPv4 or IPv6 socket. */
1894 vty_sock
= sockunion_accept(accept_sock
, &su
);
1896 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1899 set_nonblocking(vty_sock
);
1900 set_cloexec(vty_sock
);
1902 sockunion2hostprefix(&su
, &p
);
1904 /* VTY's accesslist apply. */
1905 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1906 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1907 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1908 zlog_info("Vty connection refused from %s",
1909 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1912 /* continue accepting connections */
1913 vty_event(VTY_SERV
, accept_sock
, NULL
);
1919 /* VTY's ipv6 accesslist apply. */
1920 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1921 if ((acl
= access_list_lookup(AFI_IP6
,
1922 vty_ipv6_accesslist_name
))
1923 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1924 zlog_info("Vty connection refused from %s",
1925 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1928 /* continue accepting connections */
1929 vty_event(VTY_SERV
, accept_sock
, NULL
);
1936 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1939 zlog_info("can't set sockopt to vty_sock : %s",
1940 safe_strerror(errno
));
1942 zlog_info("Vty connection from %s",
1943 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1945 vty_create(vty_sock
, &su
);
1950 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1953 struct addrinfo req
;
1954 struct addrinfo
*ainfo
;
1955 struct addrinfo
*ainfo_save
;
1957 char port_str
[BUFSIZ
];
1959 memset(&req
, 0, sizeof(struct addrinfo
));
1960 req
.ai_flags
= AI_PASSIVE
;
1961 req
.ai_family
= AF_UNSPEC
;
1962 req
.ai_socktype
= SOCK_STREAM
;
1963 sprintf(port_str
, "%d", port
);
1964 port_str
[sizeof(port_str
) - 1] = '\0';
1966 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1969 zlog_err("getaddrinfo failed: %s", gai_strerror(ret
));
1976 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1979 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1980 ainfo
->ai_protocol
);
1984 sockopt_v6only(ainfo
->ai_family
, sock
);
1985 sockopt_reuseaddr(sock
);
1986 sockopt_reuseport(sock
);
1989 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1991 close(sock
); /* Avoid sd leak. */
1995 ret
= listen(sock
, 3);
1997 close(sock
); /* Avoid sd leak. */
2001 vty_event(VTY_SERV
, sock
, NULL
);
2002 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
2004 freeaddrinfo(ainfo_save
);
2008 /* For sockaddr_un. */
2011 /* VTY shell UNIX domain socket. */
2012 static void vty_serv_un(const char *path
)
2016 struct sockaddr_un serv
;
2018 struct zprivs_ids_t ids
;
2020 /* First of all, unlink existing socket */
2024 old_mask
= umask(0007);
2026 /* Make UNIX domain socket. */
2027 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2029 zlog_err("Cannot create unix stream socket: %s",
2030 safe_strerror(errno
));
2034 /* Make server socket. */
2035 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2036 serv
.sun_family
= AF_UNIX
;
2037 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2038 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2039 len
= serv
.sun_len
= SUN_LEN(&serv
);
2041 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2042 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2046 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2048 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2049 close(sock
); /* Avoid sd leak. */
2053 ret
= listen(sock
, 5);
2055 zlog_err("listen(fd %d) failed: %s", sock
,
2056 safe_strerror(errno
));
2057 close(sock
); /* Avoid sd leak. */
2063 zprivs_get_ids(&ids
);
2065 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2066 earlier for the case when we don't need to chown the file
2067 type casting it here to make a compare */
2068 if ((int)ids
.gid_vty
> 0) {
2069 /* set group of socket */
2070 if (chown(path
, -1, ids
.gid_vty
)) {
2071 zlog_err("vty_serv_un: could chown socket, %s",
2072 safe_strerror(errno
));
2076 vty_event(VTYSH_SERV
, sock
, NULL
);
2079 /* #define VTYSH_DEBUG 1 */
2081 static int vtysh_accept(struct thread
*thread
)
2086 struct sockaddr_un client
;
2089 accept_sock
= THREAD_FD(thread
);
2091 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2093 memset(&client
, 0, sizeof(struct sockaddr_un
));
2094 client_len
= sizeof(struct sockaddr_un
);
2096 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2097 (socklen_t
*)&client_len
);
2100 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2104 if (set_nonblocking(sock
) < 0) {
2106 "vtysh_accept: could not set vty socket %d to non-blocking,"
2108 sock
, safe_strerror(errno
));
2115 printf("VTY shell accept\n");
2116 #endif /* VTYSH_DEBUG */
2121 vty
->type
= VTY_SHELL_SERV
;
2122 vty
->node
= VIEW_NODE
;
2124 vty_event(VTYSH_READ
, sock
, vty
);
2129 static int vtysh_flush(struct vty
*vty
)
2131 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2132 case BUFFER_PENDING
:
2133 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2137 0; /* disable monitoring to avoid infinite recursion */
2138 zlog_warn("%s: write error to fd %d, closing", __func__
,
2140 buffer_reset(vty
->lbuf
);
2141 buffer_reset(vty
->obuf
);
2151 static int vtysh_read(struct thread
*thread
)
2157 unsigned char buf
[VTY_READ_BUFSIZ
];
2159 uint8_t header
[4] = {0, 0, 0, 0};
2161 sock
= THREAD_FD(thread
);
2162 vty
= THREAD_ARG(thread
);
2165 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2167 if (ERRNO_IO_RETRY(errno
)) {
2168 vty_event(VTYSH_READ
, sock
, vty
);
2171 vty
->monitor
= 0; /* disable monitoring to avoid
2172 infinite recursion */
2174 "%s: read failed on vtysh client fd %d, closing: %s",
2175 __func__
, sock
, safe_strerror(errno
));
2177 buffer_reset(vty
->lbuf
);
2178 buffer_reset(vty
->obuf
);
2181 printf("close vtysh\n");
2182 #endif /* VTYSH_DEBUG */
2187 printf("line: %.*s\n", nbytes
, buf
);
2188 #endif /* VTYSH_DEBUG */
2190 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2191 /* Clear command line buffer. */
2192 vty
->cp
= vty
->length
= 0;
2194 vty_out(vty
, "%% Command is too long.\n");
2196 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2197 vty
->buf
[vty
->length
++] = *p
;
2199 /* Pass this line to parser. */
2200 ret
= vty_execute(vty
);
2201 /* Note that vty_execute clears the command buffer and resets
2202 vty->length to 0. */
2204 /* Return result. */
2206 printf("result: %d\n", ret
);
2207 printf("vtysh node: %d\n", vty
->node
);
2208 #endif /* VTYSH_DEBUG */
2210 /* hack for asynchronous "write integrated"
2211 * - other commands in "buf" will be ditched
2212 * - input during pending config-write is
2214 if (ret
== CMD_SUSPEND
)
2217 /* warning: watchfrr hardcodes this result write
2220 buffer_put(vty
->obuf
, header
, 4);
2222 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2223 /* Try to flush results; exit if a write
2230 if (vty
->status
== VTY_CLOSE
)
2233 vty_event(VTYSH_READ
, sock
, vty
);
2238 static int vtysh_write(struct thread
*thread
)
2240 struct vty
*vty
= THREAD_ARG(thread
);
2242 vty
->t_write
= NULL
;
2249 /* Determine address family to bind. */
2250 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2252 /* If port is set to 0, do not listen on TCP/IP at all! */
2254 vty_serv_sock_addrinfo(addr
, port
);
2261 /* Close vty interface. Warning: call this only from functions that
2262 will be careful not to access the vty afterwards (since it has
2263 now been freed). This is safest from top-level functions (called
2264 directly by the thread dispatcher). */
2265 void vty_close(struct vty
*vty
)
2268 bool was_stdio
= false;
2270 /* Cancel threads.*/
2272 thread_cancel(vty
->t_read
);
2274 thread_cancel(vty
->t_write
);
2276 thread_cancel(vty
->t_timeout
);
2279 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2281 /* Free input buffer. */
2282 buffer_free(vty
->obuf
);
2283 buffer_free(vty
->lbuf
);
2285 /* Free command history. */
2286 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2288 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2292 vector_unset(vtyvec
, vty
->fd
);
2294 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2298 * note check is for fd > STDERR_FILENO, not fd != -1.
2299 * We never close stdin/stdout/stderr here, because we may be
2300 * running in foreground mode with logging to stdout. Also,
2301 * additionally, we'd need to replace these fds with /dev/null. */
2302 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2304 if (vty
->fd
> STDERR_FILENO
) {
2310 XFREE(MTYPE_VTY
, vty
->buf
);
2313 XFREE(MTYPE_VTY
, vty
->error_buf
);
2315 /* Check configure. */
2316 vty_config_unlock(vty
);
2319 XFREE(MTYPE_VTY
, vty
);
2325 /* When time out occur output message then close connection. */
2326 static int vty_timeout(struct thread
*thread
)
2330 vty
= THREAD_ARG(thread
);
2331 vty
->t_timeout
= NULL
;
2335 buffer_reset(vty
->lbuf
);
2336 buffer_reset(vty
->obuf
);
2337 vty_out(vty
, "\nVty connection is timed out.\n");
2339 /* Close connection. */
2340 vty
->status
= VTY_CLOSE
;
2346 /* Read up configuration file from file_name. */
2347 static void vty_read_file(FILE *confp
)
2351 unsigned int line_num
= 0;
2354 /* vty_close won't close stderr; if some config command prints
2355 * something it'll end up there. (not ideal; it'd be beter if output
2356 * from a file-load went to logging instead. Also note that if this
2357 * function is called after daemonizing, stderr will be /dev/null.)
2359 * vty->fd will be -1 from vty_new()
2361 vty
->wfd
= STDERR_FILENO
;
2362 vty
->type
= VTY_FILE
;
2363 vty
->node
= CONFIG_NODE
;
2365 /* Execute configuration file */
2366 ret
= config_from_file(vty
, confp
, &line_num
);
2368 /* Flush any previous errors before printing messages below */
2369 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2371 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2372 const char *message
= NULL
;
2376 case CMD_ERR_AMBIGUOUS
:
2377 message
= "Ambiguous command";
2379 case CMD_ERR_NO_MATCH
:
2380 message
= "No such command";
2383 message
= "Command returned Warning";
2385 case CMD_WARNING_CONFIG_FAILED
:
2386 message
= "Command returned Warning Config Failed";
2388 case CMD_ERR_INCOMPLETE
:
2389 message
= "Command returned Incomplete";
2391 case CMD_ERR_EXEED_ARGC_MAX
:
2393 "Command exceeded maximum number of Arguments";
2396 message
= "Command returned unhandled error message";
2400 nl
= strchr(vty
->error_buf
, '\n');
2403 zlog_err("ERROR: %s on config line %u: %s", message
, line_num
,
2410 static FILE *vty_use_backup_config(const char *fullpath
)
2412 char *fullpath_sav
, *fullpath_tmp
;
2418 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2419 strcpy(fullpath_sav
, fullpath
);
2420 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2422 sav
= open(fullpath_sav
, O_RDONLY
);
2428 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2429 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2431 /* Open file to configuration write. */
2432 tmp
= mkstemp(fullpath_tmp
);
2436 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2439 while ((c
= read(sav
, buffer
, 512)) > 0) {
2440 if (write(tmp
, buffer
, c
) <= 0)
2446 if (rename(fullpath_tmp
, fullpath
) == 0)
2447 ret
= fopen(fullpath
, "r");
2449 unlink(fullpath_tmp
);
2454 unlink(fullpath_tmp
);
2464 /* Read up configuration file from file_name. */
2465 bool vty_read_config(const char *config_file
, char *config_default_dir
)
2467 char cwd
[MAXPATHLEN
];
2469 const char *fullpath
;
2471 bool read_success
= false;
2473 /* If -f flag specified. */
2474 if (config_file
!= NULL
) {
2475 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2476 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2478 "Failure to determine Current Working Directory %d!",
2482 tmp
= XMALLOC(MTYPE_TMP
,
2483 strlen(cwd
) + strlen(config_file
) + 2);
2484 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2487 fullpath
= config_file
;
2489 confp
= fopen(fullpath
, "r");
2491 if (confp
== NULL
) {
2492 zlog_err("%s: failed to open configuration file %s: %s",
2493 __func__
, fullpath
, safe_strerror(errno
));
2495 confp
= vty_use_backup_config(fullpath
);
2498 "WARNING: using backup configuration file!");
2500 zlog_err("can't open configuration file [%s]",
2507 host_config_set(config_default_dir
);
2511 struct stat conf_stat
;
2513 /* !!!!PLEASE LEAVE!!!!
2514 * This is NEEDED for use with vtysh -b, or else you can get
2515 * a real configuration food fight with a lot garbage in the
2516 * merged configuration file it creates coming from the per
2517 * daemon configuration files. This also allows the daemons
2518 * to start if there default configuration file is not
2519 * present or ignore them, as needed when using vtysh -b to
2520 * configure the daemons at boot - MAG
2523 /* Stat for vtysh Zebra.conf, if found startup and wait for
2524 * boot configuration
2527 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2528 ret
= stat(integrate_default
, &conf_stat
);
2530 read_success
= true;
2531 goto tmp_free_and_out
;
2535 confp
= fopen(config_default_dir
, "r");
2536 if (confp
== NULL
) {
2537 zlog_err("%s: failed to open configuration file %s: %s",
2538 __func__
, config_default_dir
,
2539 safe_strerror(errno
));
2541 confp
= vty_use_backup_config(config_default_dir
);
2544 "WARNING: using backup configuration file!");
2545 fullpath
= config_default_dir
;
2547 zlog_err("can't open configuration file [%s]",
2548 config_default_dir
);
2549 goto tmp_free_and_out
;
2552 fullpath
= config_default_dir
;
2555 vty_read_file(confp
);
2556 read_success
= true;
2560 host_config_set(fullpath
);
2564 XFREE(MTYPE_TMP
, tmp
);
2566 return read_success
;
2569 /* Small utility function which output log to the VTY. */
2570 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2571 struct timestamp_control
*ctl
, va_list va
)
2579 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2580 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2584 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2590 /* Async-signal-safe version of vty_log for fixed strings. */
2591 void vty_log_fixed(char *buf
, size_t len
)
2594 struct iovec iov
[2];
2595 char crlf
[4] = "\r\n";
2597 /* vty may not have been initialised */
2601 iov
[0].iov_base
= buf
;
2602 iov
[0].iov_len
= len
;
2603 iov
[1].iov_base
= crlf
;
2606 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2608 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2609 /* N.B. We don't care about the return code, since
2611 most likely just about to die anyway. */
2612 if (writev(vty
->wfd
, iov
, 2) == -1) {
2613 fprintf(stderr
, "Failure to writev: %d\n",
2620 int vty_config_lock(struct vty
*vty
)
2622 if (vty_config_is_lockless
)
2624 if (vty_config
== 0) {
2631 int vty_config_unlock(struct vty
*vty
)
2633 if (vty_config_is_lockless
)
2635 if (vty_config
== 1 && vty
->config
== 1) {
2642 void vty_config_lockless(void)
2644 vty_config_is_lockless
= 1;
2647 /* Master of the threads. */
2648 static struct thread_master
*vty_master
;
2650 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2652 struct thread
*vty_serv_thread
= NULL
;
2656 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2658 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2662 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2664 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2668 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2672 vty
->t_write
= NULL
;
2673 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2679 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2681 /* Time out treatment. */
2682 if (vty
->v_timeout
) {
2684 thread_cancel(vty
->t_timeout
);
2685 vty
->t_timeout
= NULL
;
2686 thread_add_timer(vty_master
, vty_timeout
, vty
,
2687 vty
->v_timeout
, &vty
->t_timeout
);
2691 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2694 case VTY_TIMEOUT_RESET
:
2695 if (vty
->t_timeout
) {
2696 thread_cancel(vty
->t_timeout
);
2697 vty
->t_timeout
= NULL
;
2699 if (vty
->v_timeout
) {
2700 vty
->t_timeout
= NULL
;
2701 thread_add_timer(vty_master
, vty_timeout
, vty
,
2702 vty
->v_timeout
, &vty
->t_timeout
);
2708 DEFUN_NOSH (config_who
,
2711 "Display who is on vty\n")
2716 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2717 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2718 vty_out(vty
, "%svty[%d] connected from %s.\n",
2719 v
->config
? "*" : " ", i
, v
->address
);
2723 /* Move to vty configuration mode. */
2724 DEFUN_NOSH (line_vty
,
2727 "Configure a terminal line\n"
2728 "Virtual terminal\n")
2730 vty
->node
= VTY_NODE
;
2734 /* Set time out value. */
2735 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2736 const char *sec_str
)
2738 unsigned long timeout
= 0;
2740 /* min_str and sec_str are already checked by parser. So it must be
2741 all digit string. */
2743 timeout
= strtol(min_str
, NULL
, 10);
2747 timeout
+= strtol(sec_str
, NULL
, 10);
2749 vty_timeout_val
= timeout
;
2750 vty
->v_timeout
= timeout
;
2751 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2757 DEFUN (exec_timeout_min
,
2758 exec_timeout_min_cmd
,
2759 "exec-timeout (0-35791)",
2760 "Set timeout value\n"
2761 "Timeout value in minutes\n")
2764 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2767 DEFUN (exec_timeout_sec
,
2768 exec_timeout_sec_cmd
,
2769 "exec-timeout (0-35791) (0-2147483)",
2770 "Set the EXEC timeout\n"
2771 "Timeout in minutes\n"
2772 "Timeout in seconds\n")
2775 int idx_number_2
= 2;
2776 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2777 argv
[idx_number_2
]->arg
);
2780 DEFUN (no_exec_timeout
,
2781 no_exec_timeout_cmd
,
2784 "Set the EXEC timeout\n")
2786 return exec_timeout(vty
, NULL
, NULL
);
2789 /* Set vty access class. */
2790 DEFUN (vty_access_class
,
2791 vty_access_class_cmd
,
2792 "access-class WORD",
2793 "Filter connections based on an IP access list\n"
2797 if (vty_accesslist_name
)
2798 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2800 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2805 /* Clear vty access class. */
2806 DEFUN (no_vty_access_class
,
2807 no_vty_access_class_cmd
,
2808 "no access-class [WORD]",
2810 "Filter connections based on an IP access list\n"
2814 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2815 if (!vty_accesslist_name
2816 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2817 vty_out(vty
, "Access-class is not currently applied to vty\n");
2818 return CMD_WARNING_CONFIG_FAILED
;
2821 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2823 vty_accesslist_name
= NULL
;
2828 /* Set vty access class. */
2829 DEFUN (vty_ipv6_access_class
,
2830 vty_ipv6_access_class_cmd
,
2831 "ipv6 access-class WORD",
2833 "Filter connections based on an IP access list\n"
2834 "IPv6 access list\n")
2837 if (vty_ipv6_accesslist_name
)
2838 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2840 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2845 /* Clear vty access class. */
2846 DEFUN (no_vty_ipv6_access_class
,
2847 no_vty_ipv6_access_class_cmd
,
2848 "no ipv6 access-class [WORD]",
2851 "Filter connections based on an IP access list\n"
2852 "IPv6 access list\n")
2855 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2857 if (!vty_ipv6_accesslist_name
2858 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2860 "IPv6 access-class is not currently applied to vty\n");
2861 return CMD_WARNING_CONFIG_FAILED
;
2864 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2866 vty_ipv6_accesslist_name
= NULL
;
2875 "Enable password checking\n")
2877 no_password_check
= 0;
2881 DEFUN (no_vty_login
,
2885 "Enable password checking\n")
2887 no_password_check
= 1;
2891 DEFUN (service_advanced_vty
,
2892 service_advanced_vty_cmd
,
2893 "service advanced-vty",
2894 "Set up miscellaneous service\n"
2895 "Enable advanced mode vty interface\n")
2901 DEFUN (no_service_advanced_vty
,
2902 no_service_advanced_vty_cmd
,
2903 "no service advanced-vty",
2905 "Set up miscellaneous service\n"
2906 "Enable advanced mode vty interface\n")
2912 DEFUN_NOSH (terminal_monitor
,
2913 terminal_monitor_cmd
,
2915 "Set terminal line parameters\n"
2916 "Copy debug output to the current terminal line\n")
2922 DEFUN_NOSH (terminal_no_monitor
,
2923 terminal_no_monitor_cmd
,
2924 "terminal no monitor",
2925 "Set terminal line parameters\n"
2927 "Copy debug output to the current terminal line\n")
2933 DEFUN_NOSH (no_terminal_monitor
,
2934 no_terminal_monitor_cmd
,
2935 "no terminal monitor",
2937 "Set terminal line parameters\n"
2938 "Copy debug output to the current terminal line\n")
2940 return terminal_no_monitor(self
, vty
, argc
, argv
);
2944 DEFUN_NOSH (show_history
,
2948 "Display the session command history\n")
2952 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2953 if (index
== VTY_MAXHIST
) {
2958 if (vty
->hist
[index
] != NULL
)
2959 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2968 DEFUN (log_commands
,
2972 "Log all commands (can't be unset without restart)\n")
2974 do_log_commands
= 1;
2978 /* Display current configuration. */
2979 static int vty_config_write(struct vty
*vty
)
2981 vty_out(vty
, "line vty\n");
2983 if (vty_accesslist_name
)
2984 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2986 if (vty_ipv6_accesslist_name
)
2987 vty_out(vty
, " ipv6 access-class %s\n",
2988 vty_ipv6_accesslist_name
);
2991 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2992 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2993 vty_timeout_val
% 60);
2996 if (no_password_check
)
2997 vty_out(vty
, " no login\n");
2999 if (do_log_commands
)
3000 vty_out(vty
, "log commands\n");
3002 vty_out(vty
, "!\n");
3007 struct cmd_node vty_node
= {
3008 VTY_NODE
, "%s(config-line)# ", 1,
3011 /* Reset all VTY status. */
3016 struct thread
*vty_serv_thread
;
3018 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3019 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3020 buffer_reset(vty
->lbuf
);
3021 buffer_reset(vty
->obuf
);
3022 vty
->status
= VTY_CLOSE
;
3026 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3027 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3029 thread_cancel(vty_serv_thread
);
3030 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3034 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3036 if (vty_accesslist_name
) {
3037 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3038 vty_accesslist_name
= NULL
;
3041 if (vty_ipv6_accesslist_name
) {
3042 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3043 vty_ipv6_accesslist_name
= NULL
;
3047 static void vty_save_cwd(void)
3049 char cwd
[MAXPATHLEN
];
3052 c
= getcwd(cwd
, MAXPATHLEN
);
3056 * At this point if these go wrong, more than likely
3057 * the whole world is coming down around us
3058 * Hence not worrying about it too much.
3060 if (!chdir(SYSCONFDIR
)) {
3061 zlog_err("Failure to chdir to %s, errno: %d",
3065 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3066 zlog_err("Failure to getcwd, errno: %d", errno
);
3071 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3072 strcpy(vty_cwd
, cwd
);
3080 int vty_shell(struct vty
*vty
)
3082 return vty
->type
== VTY_SHELL
? 1 : 0;
3085 int vty_shell_serv(struct vty
*vty
)
3087 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3090 void vty_init_vtysh()
3092 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3095 /* Install vty's own commands like `who' command. */
3096 void vty_init(struct thread_master
*master_thread
)
3098 /* For further configuration read, preserve current directory. */
3101 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3103 vty_master
= master_thread
;
3105 atexit(vty_stdio_atexit
);
3107 /* Initilize server thread vector. */
3108 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3110 /* Install bgp top node. */
3111 install_node(&vty_node
, vty_config_write
);
3113 install_element(VIEW_NODE
, &config_who_cmd
);
3114 install_element(VIEW_NODE
, &show_history_cmd
);
3115 install_element(CONFIG_NODE
, &line_vty_cmd
);
3116 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3117 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3118 install_element(CONFIG_NODE
, &show_history_cmd
);
3119 install_element(CONFIG_NODE
, &log_commands_cmd
);
3120 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3121 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3122 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3124 install_default(VTY_NODE
);
3125 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3126 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3127 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3128 install_element(VTY_NODE
, &vty_access_class_cmd
);
3129 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3130 install_element(VTY_NODE
, &vty_login_cmd
);
3131 install_element(VTY_NODE
, &no_vty_login_cmd
);
3132 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3133 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3136 void vty_terminate(void)
3139 XFREE(MTYPE_TMP
, vty_cwd
);
3141 if (vtyvec
&& Vvty_serv_thread
) {
3143 vector_free(vtyvec
);
3144 vector_free(Vvty_serv_thread
);
3146 Vvty_serv_thread
= NULL
;