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>
32 #include "sockunion.h"
43 #include <arpa/telnet.h>
46 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
47 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
48 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
63 static void vty_event(enum event
, int, struct vty
*);
65 /* Extern host structure from command.c */
66 extern struct host host
;
68 /* Vector which store each vty structure. */
71 /* Vty timeout value. */
72 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
74 /* Vty access-class command */
75 static char *vty_accesslist_name
= NULL
;
77 /* Vty access-calss for IPv6. */
78 static char *vty_ipv6_accesslist_name
= NULL
;
80 /* VTY server thread. */
81 static vector Vvty_serv_thread
;
83 /* Current directory. */
87 static int vty_config
;
88 static int vty_config_is_lockless
= 0;
90 /* Login password check. */
91 static int no_password_check
= 0;
93 /* Integrated configuration file path */
94 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
96 static int do_log_commands
= 0;
98 void vty_frame(struct vty
*vty
, const char *format
, ...)
102 va_start(args
, format
);
103 vsnprintf(vty
->frame
+ vty
->frame_pos
,
104 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
105 vty
->frame_pos
= strlen(vty
->frame
);
109 void vty_endframe(struct vty
*vty
, const char *endtext
)
111 if (vty
->frame_pos
== 0 && endtext
)
112 vty_out(vty
, "%s", endtext
);
116 bool vty_set_include(struct vty
*vty
, const char *regexp
)
122 if (!regexp
&& vty
->filter
) {
123 regfree(&vty
->include
);
128 errcode
= regcomp(&vty
->include
, regexp
,
129 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
132 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
133 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
141 /* VTY standard output function. */
142 int vty_out(struct vty
*vty
, const char *format
, ...)
151 if (vty
->frame_pos
) {
153 vty_out(vty
, "%s", vty
->frame
);
156 /* Try to write to initial buffer. */
157 va_start(args
, format
);
158 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
161 /* Initial buffer is not enough. */
162 if (len
< 0 || len
>= size
) {
169 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
173 va_start(args
, format
);
174 len
= vsnprintf(p
, size
, format
, args
);
177 if (len
> -1 && len
< size
)
182 /* When initial buffer is enough to store all output. */
188 vector lines
= frrstr_split_vec(buf
, "\n");
190 frrstr_filter_vec(lines
, &vty
->include
);
191 vector_compact(lines
);
194 * Consider the string "foo\n". If the regex is an empty string
195 * and the line ended with a newline, then the vector will look
201 * If the regex isn't empty, the vector will look like:
205 * In this case we'd like to preserve the newline, so we add
206 * the empty string [1] as in the first example.
208 if (buf
[strlen(buf
) - 1] == '\n' && vector_active(lines
) > 0
209 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
210 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
212 filtered
= frrstr_join_vec(lines
, "\n");
213 frrstr_strvec_free(lines
);
220 /* print with crlf replacement */
221 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
225 fprintf(vty
->of
, "%s", filtered
);
231 /* print without crlf replacement */
232 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
237 XFREE(MTYPE_TMP
, filtered
);
239 /* If p is not different with buf, it is allocated buffer. */
241 XFREE(MTYPE_VTY_OUT_BUF
, p
);
246 static int vty_log_out(struct vty
*vty
, const char *level
,
247 const char *proto_str
, const char *format
,
248 struct timestamp_control
*ctl
, va_list va
)
254 if (!ctl
->already_rendered
) {
255 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
257 ctl
->already_rendered
= 1;
259 if (ctl
->len
+ 1 >= sizeof(buf
))
261 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
266 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
269 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
270 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
273 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
274 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
280 if (write(vty
->wfd
, buf
, len
) < 0) {
281 if (ERRNO_IO_RETRY(errno
))
282 /* Kernel buffer is full, probably too much debugging
284 drop the data and ignore. */
286 /* Fatal I/O error. */
288 0; /* disable monitoring to avoid infinite recursion */
289 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
290 __func__
, vty
->fd
, safe_strerror(errno
));
291 buffer_reset(vty
->obuf
);
292 /* cannot call vty_close, because a parent routine may still try
293 to access the vty struct */
294 vty
->status
= VTY_CLOSE
;
295 shutdown(vty
->fd
, SHUT_RDWR
);
301 /* Output current time to the vty. */
302 void vty_time_print(struct vty
*vty
, int cr
)
304 char buf
[QUAGGA_TIMESTAMP_LEN
];
306 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
307 zlog_info("quagga_timestamp error");
311 vty_out(vty
, "%s\n", buf
);
313 vty_out(vty
, "%s ", buf
);
318 /* Say hello to vty interface. */
319 void vty_hello(struct vty
*vty
)
325 f
= fopen(host
.motdfile
, "r");
327 while (fgets(buf
, sizeof(buf
), f
)) {
329 /* work backwards to ignore trailling isspace()
331 for (s
= buf
+ strlen(buf
);
332 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
335 vty_out(vty
, "%s\n", buf
);
339 vty_out(vty
, "MOTD file not found\n");
340 } else if (host
.motd
)
341 vty_out(vty
, "%s", host
.motd
);
344 /* Put out prompt and wait input from user. */
345 static void vty_prompt(struct vty
*vty
)
347 if (vty
->type
== VTY_TERM
) {
348 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
352 /* Send WILL TELOPT_ECHO to remote server. */
353 static void vty_will_echo(struct vty
*vty
)
355 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
356 vty_out(vty
, "%s", cmd
);
359 /* Make suppress Go-Ahead telnet option. */
360 static void vty_will_suppress_go_ahead(struct vty
*vty
)
362 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
363 vty_out(vty
, "%s", cmd
);
366 /* Make don't use linemode over telnet. */
367 static void vty_dont_linemode(struct vty
*vty
)
369 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
370 vty_out(vty
, "%s", cmd
);
373 /* Use window size. */
374 static void vty_do_window_size(struct vty
*vty
)
376 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
377 vty_out(vty
, "%s", cmd
);
380 #if 0 /* Currently not used. */
381 /* Make don't use lflow vty interface. */
383 vty_dont_lflow_ahead (struct vty
*vty
)
385 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
386 vty_out (vty
, "%s", cmd
);
390 /* Authentication of vty */
391 static void vty_auth(struct vty
*vty
, char *buf
)
394 enum node_type next_node
= 0;
396 char *crypt(const char *, const char *);
401 passwd
= host
.password_encrypt
;
403 passwd
= host
.password
;
405 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
407 next_node
= VIEW_NODE
;
409 case AUTH_ENABLE_NODE
:
411 passwd
= host
.enable_encrypt
;
413 passwd
= host
.enable
;
414 next_node
= ENABLE_NODE
;
420 fail
= strcmp(crypt(buf
, passwd
), passwd
);
422 fail
= strcmp(buf
, passwd
);
428 vty
->node
= next_node
; /* Success ! */
431 if (vty
->fail
>= 3) {
432 if (vty
->node
== AUTH_NODE
) {
434 "%% Bad passwords, too many failures!\n");
435 vty
->status
= VTY_CLOSE
;
437 /* AUTH_ENABLE_NODE */
440 "%% Bad enable passwords, too many failures!\n");
441 vty
->status
= VTY_CLOSE
;
447 /* Command execution over the vty interface. */
448 static int vty_command(struct vty
*vty
, char *buf
)
451 const char *protocolname
;
455 * Log non empty command lines
460 /* Skip white spaces. */
461 while (isspace((int)*cp
) && *cp
!= '\0')
464 if (cp
!= NULL
&& *cp
!= '\0') {
466 char vty_str
[VTY_BUFSIZ
];
467 char prompt_str
[VTY_BUFSIZ
];
469 /* format the base vty info */
470 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
472 for (i
= 0; i
< vector_active(vtyvec
); i
++)
473 if (vty
== vector_slot(vtyvec
, i
)) {
474 snprintf(vty_str
, sizeof(vty_str
),
475 "vty[%d]@%s", i
, vty
->address
);
479 /* format the prompt */
480 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
483 /* now log the command */
484 zlog_err("%s%s", prompt_str
, buf
);
487 #ifdef CONSUMED_TIME_CHECK
491 unsigned long realtime
, cputime
;
494 #endif /* CONSUMED_TIME_CHECK */
496 ret
= cmd_execute(vty
, buf
, NULL
, 0);
498 /* Get the name of the protocol if any */
499 protocolname
= frr_protoname
;
501 #ifdef CONSUMED_TIME_CHECK
503 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
504 > CONSUMED_TIME_CHECK
)
505 /* Warn about CPU hog that must be fixed. */
507 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
508 realtime
/ 1000, cputime
/ 1000, buf
);
510 #endif /* CONSUMED_TIME_CHECK */
512 if (ret
!= CMD_SUCCESS
)
515 if (vty
->type
== VTY_FILE
)
516 vty_out(vty
, "Warning...\n");
518 case CMD_ERR_AMBIGUOUS
:
519 vty_out(vty
, "%% Ambiguous command.\n");
521 case CMD_ERR_NO_MATCH
:
522 vty_out(vty
, "%% [%s] Unknown command: %s\n",
525 case CMD_ERR_INCOMPLETE
:
526 vty_out(vty
, "%% Command incomplete.\n");
533 static const char telnet_backward_char
= 0x08;
534 static const char telnet_space_char
= ' ';
536 /* Basic function to write buffer to vty. */
537 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
539 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
542 /* Should we do buffering here ? And make vty_flush (vty) ? */
543 buffer_put(vty
->obuf
, buf
, nbytes
);
546 /* Basic function to insert character into vty. */
547 static void vty_self_insert(struct vty
*vty
, char c
)
552 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
555 length
= vty
->length
- vty
->cp
;
556 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
557 vty
->buf
[vty
->cp
] = c
;
559 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
560 for (i
= 0; i
< length
; i
++)
561 vty_write(vty
, &telnet_backward_char
, 1);
566 vty
->buf
[vty
->length
] = '\0';
569 /* Self insert character 'c' in overwrite mode. */
570 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
572 if (vty
->cp
== vty
->length
) {
573 vty_self_insert(vty
, c
);
577 vty
->buf
[vty
->cp
++] = c
;
578 vty_write(vty
, &c
, 1);
582 * Insert a string into vty->buf at the current cursor position.
584 * If the resultant string would be larger than VTY_BUFSIZ it is
587 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
589 if (vty
->cp
== VTY_BUFSIZ
)
592 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
593 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
595 vty
->length
= MAX(vty
->cp
, vty
->length
);
596 vty
->buf
[vty
->length
] = '\0';
597 vty_write(vty
, str
, nwrite
);
600 /* Forward character. */
601 static void vty_forward_char(struct vty
*vty
)
603 if (vty
->cp
< vty
->length
) {
604 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
609 /* Backward character. */
610 static void vty_backward_char(struct vty
*vty
)
614 vty_write(vty
, &telnet_backward_char
, 1);
618 /* Move to the beginning of the line. */
619 static void vty_beginning_of_line(struct vty
*vty
)
622 vty_backward_char(vty
);
625 /* Move to the end of the line. */
626 static void vty_end_of_line(struct vty
*vty
)
628 while (vty
->cp
< vty
->length
)
629 vty_forward_char(vty
);
632 static void vty_kill_line_from_beginning(struct vty
*);
633 static void vty_redraw_line(struct vty
*);
635 /* Print command line history. This function is called from
636 vty_next_line and vty_previous_line. */
637 static void vty_history_print(struct vty
*vty
)
641 vty_kill_line_from_beginning(vty
);
643 /* Get previous line from history buffer */
644 length
= strlen(vty
->hist
[vty
->hp
]);
645 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
646 vty
->cp
= vty
->length
= length
;
647 vty
->buf
[vty
->length
] = '\0';
649 /* Redraw current line */
650 vty_redraw_line(vty
);
653 /* Show next command line history. */
654 static void vty_next_line(struct vty
*vty
)
658 if (vty
->hp
== vty
->hindex
)
661 /* Try is there history exist or not. */
663 if (try_index
== (VTY_MAXHIST
- 1))
668 /* If there is not history return. */
669 if (vty
->hist
[try_index
] == NULL
)
674 vty_history_print(vty
);
677 /* Show previous command line history. */
678 static void vty_previous_line(struct vty
*vty
)
684 try_index
= VTY_MAXHIST
- 1;
688 if (vty
->hist
[try_index
] == NULL
)
693 vty_history_print(vty
);
696 /* This function redraw all of the command line character. */
697 static void vty_redraw_line(struct vty
*vty
)
699 vty_write(vty
, vty
->buf
, vty
->length
);
700 vty
->cp
= vty
->length
;
704 static void vty_forward_word(struct vty
*vty
)
706 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
707 vty_forward_char(vty
);
709 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
710 vty_forward_char(vty
);
713 /* Backward word without skipping training space. */
714 static void vty_backward_pure_word(struct vty
*vty
)
716 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
717 vty_backward_char(vty
);
721 static void vty_backward_word(struct vty
*vty
)
723 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
724 vty_backward_char(vty
);
726 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
727 vty_backward_char(vty
);
730 /* When '^D' is typed at the beginning of the line we move to the down
732 static void vty_down_level(struct vty
*vty
)
740 /* When '^Z' is received from vty, move down to the enable mode. */
741 static void vty_end_config(struct vty
*vty
)
760 case BGP_VRF_POLICY_NODE
:
761 case BGP_VNC_DEFAULTS_NODE
:
762 case BGP_VNC_NVE_GROUP_NODE
:
763 case BGP_VNC_L2_GROUP_NODE
:
778 case LDP_IPV4_IFACE_NODE
:
779 case LDP_IPV6_IFACE_NODE
:
781 case LDP_PSEUDOWIRE_NODE
:
784 case KEYCHAIN_KEY_NODE
:
786 case BGP_EVPN_VNI_NODE
:
787 vty_config_unlock(vty
);
788 vty
->node
= ENABLE_NODE
;
791 /* Unknown node, we have to ignore it. */
799 /* Delete a charcter at the current point. */
800 static void vty_delete_char(struct vty
*vty
)
805 if (vty
->length
== 0) {
810 if (vty
->cp
== vty
->length
)
811 return; /* completion need here? */
813 size
= vty
->length
- vty
->cp
;
816 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
817 vty
->buf
[vty
->length
] = '\0';
819 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
822 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
823 vty_write(vty
, &telnet_space_char
, 1);
825 for (i
= 0; i
< size
; i
++)
826 vty_write(vty
, &telnet_backward_char
, 1);
829 /* Delete a character before the point. */
830 static void vty_delete_backward_char(struct vty
*vty
)
835 vty_backward_char(vty
);
836 vty_delete_char(vty
);
839 /* Kill rest of line from current point. */
840 static void vty_kill_line(struct vty
*vty
)
845 size
= vty
->length
- vty
->cp
;
850 for (i
= 0; i
< size
; i
++)
851 vty_write(vty
, &telnet_space_char
, 1);
852 for (i
= 0; i
< size
; i
++)
853 vty_write(vty
, &telnet_backward_char
, 1);
855 memset(&vty
->buf
[vty
->cp
], 0, size
);
856 vty
->length
= vty
->cp
;
859 /* Kill line from the beginning. */
860 static void vty_kill_line_from_beginning(struct vty
*vty
)
862 vty_beginning_of_line(vty
);
866 /* Delete a word before the point. */
867 static void vty_forward_kill_word(struct vty
*vty
)
869 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
870 vty_delete_char(vty
);
871 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
872 vty_delete_char(vty
);
875 /* Delete a word before the point. */
876 static void vty_backward_kill_word(struct vty
*vty
)
878 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
879 vty_delete_backward_char(vty
);
880 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
881 vty_delete_backward_char(vty
);
884 /* Transpose chars before or at the point. */
885 static void vty_transpose_chars(struct vty
*vty
)
889 /* If length is short or point is near by the beginning of line then
891 if (vty
->length
< 2 || vty
->cp
< 1)
894 /* In case of point is located at the end of the line. */
895 if (vty
->cp
== vty
->length
) {
896 c1
= vty
->buf
[vty
->cp
- 1];
897 c2
= vty
->buf
[vty
->cp
- 2];
899 vty_backward_char(vty
);
900 vty_backward_char(vty
);
901 vty_self_insert_overwrite(vty
, c1
);
902 vty_self_insert_overwrite(vty
, c2
);
904 c1
= vty
->buf
[vty
->cp
];
905 c2
= vty
->buf
[vty
->cp
- 1];
907 vty_backward_char(vty
);
908 vty_self_insert_overwrite(vty
, c1
);
909 vty_self_insert_overwrite(vty
, c2
);
913 /* Do completion at vty interface. */
914 static void vty_complete_command(struct vty
*vty
)
918 char **matched
= NULL
;
921 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
924 vline
= cmd_make_strvec(vty
->buf
);
928 /* In case of 'help \t'. */
929 if (isspace((int)vty
->buf
[vty
->length
- 1]))
930 vector_set(vline
, NULL
);
932 matched
= cmd_complete_command(vline
, vty
, &ret
);
934 cmd_free_strvec(vline
);
938 case CMD_ERR_AMBIGUOUS
:
939 vty_out(vty
, "%% Ambiguous command.\n");
941 vty_redraw_line(vty
);
943 case CMD_ERR_NO_MATCH
:
944 /* vty_out (vty, "%% There is no matched command.\n"); */
946 vty_redraw_line(vty
);
948 case CMD_COMPLETE_FULL_MATCH
:
950 /* 2016-11-28 equinox -- need to debug, SEGV here */
951 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
953 vty_redraw_line(vty
);
957 vty_redraw_line(vty
);
958 vty_backward_pure_word(vty
);
959 vty_insert_word_overwrite(vty
, matched
[0]);
960 vty_self_insert(vty
, ' ');
961 XFREE(MTYPE_COMPLETION
, matched
[0]);
963 case CMD_COMPLETE_MATCH
:
965 vty_redraw_line(vty
);
966 vty_backward_pure_word(vty
);
967 vty_insert_word_overwrite(vty
, matched
[0]);
968 XFREE(MTYPE_COMPLETION
, matched
[0]);
970 case CMD_COMPLETE_LIST_MATCH
:
971 for (i
= 0; matched
[i
] != NULL
; i
++) {
972 if (i
!= 0 && ((i
% 6) == 0))
974 vty_out(vty
, "%-10s ", matched
[i
]);
975 XFREE(MTYPE_COMPLETION
, matched
[i
]);
980 vty_redraw_line(vty
);
982 case CMD_ERR_NOTHING_TODO
:
984 vty_redraw_line(vty
);
990 XFREE(MTYPE_TMP
, matched
);
993 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
994 unsigned int desc_width
, struct cmd_token
*token
)
1002 if (desc_width
<= 0) {
1003 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1007 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1009 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1010 for (pos
= desc_width
; pos
> 0; pos
--)
1011 if (*(p
+ pos
) == ' ')
1017 strncpy(buf
, p
, pos
);
1019 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1024 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1026 XFREE(MTYPE_TMP
, buf
);
1029 /* Describe matched command function. */
1030 static void vty_describe_command(struct vty
*vty
)
1035 unsigned int i
, width
, desc_width
;
1036 struct cmd_token
*token
, *token_cr
= NULL
;
1038 vline
= cmd_make_strvec(vty
->buf
);
1040 /* In case of '> ?'. */
1041 if (vline
== NULL
) {
1042 vline
= vector_init(1);
1043 vector_set(vline
, NULL
);
1044 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1045 vector_set(vline
, NULL
);
1047 describe
= cmd_describe_command(vline
, vty
, &ret
);
1051 /* Ambiguous error. */
1053 case CMD_ERR_AMBIGUOUS
:
1054 vty_out(vty
, "%% Ambiguous command.\n");
1057 case CMD_ERR_NO_MATCH
:
1058 vty_out(vty
, "%% There is no matched command.\n");
1063 /* Get width of command string. */
1065 for (i
= 0; i
< vector_active(describe
); i
++)
1066 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1069 if (token
->text
[0] == '\0')
1072 len
= strlen(token
->text
);
1078 /* Get width of description string. */
1079 desc_width
= vty
->width
- (width
+ 6);
1081 /* Print out description. */
1082 for (i
= 0; i
< vector_active(describe
); i
++)
1083 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1084 if (token
->text
[0] == '\0')
1087 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1093 vty_out(vty
, " %-s\n", token
->text
);
1094 else if (desc_width
>= strlen(token
->desc
))
1095 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1098 vty_describe_fold(vty
, width
, desc_width
,
1101 if (IS_VARYING_TOKEN(token
->type
)) {
1102 const char *ref
= vector_slot(
1103 vline
, vector_active(vline
) - 1);
1105 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1106 cmd_variable_complete(token
, ref
, varcomps
);
1108 if (vector_active(varcomps
) > 0) {
1109 char *ac
= cmd_variable_comp2str(
1110 varcomps
, vty
->width
);
1111 vty_out(vty
, "%s\n", ac
);
1112 XFREE(MTYPE_TMP
, ac
);
1115 vector_free(varcomps
);
1118 vty_out (vty
, " %-*s %s\n", width
1119 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1120 desc
->str
? desc
->str
: "");
1124 if ((token
= token_cr
)) {
1126 vty_out(vty
, " %-s\n", token
->text
);
1127 else if (desc_width
>= strlen(token
->desc
))
1128 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1131 vty_describe_fold(vty
, width
, desc_width
, token
);
1135 cmd_free_strvec(vline
);
1137 vector_free(describe
);
1140 vty_redraw_line(vty
);
1143 static void vty_clear_buf(struct vty
*vty
)
1145 memset(vty
->buf
, 0, vty
->max
);
1148 /* ^C stop current input and do not add command line to the history. */
1149 static void vty_stop_input(struct vty
*vty
)
1151 vty
->cp
= vty
->length
= 0;
1155 switch (vty
->node
) {
1158 /* Nothing to do. */
1161 case INTERFACE_NODE
:
1175 case LDP_IPV4_IFACE_NODE
:
1176 case LDP_IPV6_IFACE_NODE
:
1177 case LDP_L2VPN_NODE
:
1178 case LDP_PSEUDOWIRE_NODE
:
1181 case KEYCHAIN_KEY_NODE
:
1183 vty_config_unlock(vty
);
1184 vty
->node
= ENABLE_NODE
;
1187 /* Unknown node, we have to ignore it. */
1192 /* Set history pointer to the latest one. */
1193 vty
->hp
= vty
->hindex
;
1196 /* Add current command line to the history buffer. */
1197 static void vty_hist_add(struct vty
*vty
)
1201 if (vty
->length
== 0)
1204 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1206 /* Ignore the same string as previous one. */
1207 if (vty
->hist
[index
])
1208 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1209 vty
->hp
= vty
->hindex
;
1213 /* Insert history entry. */
1214 if (vty
->hist
[vty
->hindex
])
1215 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1216 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1218 /* History index rotation. */
1220 if (vty
->hindex
== VTY_MAXHIST
)
1223 vty
->hp
= vty
->hindex
;
1226 /* #define TELNET_OPTION_DEBUG */
1228 /* Get telnet window size. */
1229 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1231 #ifdef TELNET_OPTION_DEBUG
1234 for (i
= 0; i
< nbytes
; i
++) {
1237 vty_out(vty
, "IAC ");
1240 vty_out(vty
, "WILL ");
1243 vty_out(vty
, "WONT ");
1246 vty_out(vty
, "DO ");
1249 vty_out(vty
, "DONT ");
1252 vty_out(vty
, "SB ");
1255 vty_out(vty
, "SE ");
1258 vty_out(vty
, "TELOPT_ECHO \n");
1261 vty_out(vty
, "TELOPT_SGA \n");
1264 vty_out(vty
, "TELOPT_NAWS \n");
1267 vty_out(vty
, "%x ", buf
[i
]);
1273 #endif /* TELNET_OPTION_DEBUG */
1278 vty
->iac_sb_in_progress
= 1;
1282 if (!vty
->iac_sb_in_progress
)
1285 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1286 vty
->iac_sb_in_progress
= 0;
1289 switch (vty
->sb_buf
[0]) {
1291 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1293 "RFC 1073 violation detected: telnet NAWS option "
1294 "should send %d characters, but we received %lu",
1296 (unsigned long)vty
->sb_len
);
1297 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1299 "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1300 "too small to handle the telnet NAWS option",
1301 (unsigned long)sizeof(vty
->sb_buf
),
1302 TELNET_NAWS_SB_LEN
);
1304 vty
->width
= ((vty
->sb_buf
[1] << 8)
1306 vty
->height
= ((vty
->sb_buf
[3] << 8)
1308 #ifdef TELNET_OPTION_DEBUG
1310 "TELNET NAWS window size negotiation completed: "
1311 "width %d, height %d\n",
1312 vty
->width
, vty
->height
);
1317 vty
->iac_sb_in_progress
= 0;
1327 /* Execute current command line. */
1328 static int vty_execute(struct vty
*vty
)
1334 switch (vty
->node
) {
1336 case AUTH_ENABLE_NODE
:
1337 vty_auth(vty
, vty
->buf
);
1340 ret
= vty_command(vty
, vty
->buf
);
1341 if (vty
->type
== VTY_TERM
)
1346 /* Clear command line buffer. */
1347 vty
->cp
= vty
->length
= 0;
1350 if (vty
->status
!= VTY_CLOSE
)
1356 #define CONTROL(X) ((X) - '@')
1357 #define VTY_NORMAL 0
1358 #define VTY_PRE_ESCAPE 1
1359 #define VTY_ESCAPE 2
1361 /* Escape character command map. */
1362 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1366 vty_previous_line(vty
);
1372 vty_forward_char(vty
);
1375 vty_backward_char(vty
);
1381 /* Go back to normal mode. */
1382 vty
->escape
= VTY_NORMAL
;
1385 /* Quit print out to the buffer. */
1386 static void vty_buffer_reset(struct vty
*vty
)
1388 buffer_reset(vty
->obuf
);
1390 vty_redraw_line(vty
);
1393 /* Read data via vty socket. */
1394 static int vty_read(struct thread
*thread
)
1398 unsigned char buf
[VTY_READ_BUFSIZ
];
1400 int vty_sock
= THREAD_FD(thread
);
1401 struct vty
*vty
= THREAD_ARG(thread
);
1404 /* Read raw data from socket */
1405 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1407 if (ERRNO_IO_RETRY(errno
)) {
1408 vty_event(VTY_READ
, vty_sock
, vty
);
1411 vty
->monitor
= 0; /* disable monitoring to avoid
1412 infinite recursion */
1414 "%s: read error on vty client fd %d, closing: %s",
1415 __func__
, vty
->fd
, safe_strerror(errno
));
1416 buffer_reset(vty
->obuf
);
1418 vty
->status
= VTY_CLOSE
;
1421 for (i
= 0; i
< nbytes
; i
++) {
1422 if (buf
[i
] == IAC
) {
1431 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1432 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1433 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1439 /* In case of telnet command */
1441 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1448 if (vty
->status
== VTY_MORE
) {
1453 vty_buffer_reset(vty
);
1455 #if 0 /* More line does not work for "show ip bgp". */
1458 vty
->status
= VTY_MORELINE
;
1467 /* Escape character. */
1468 if (vty
->escape
== VTY_ESCAPE
) {
1469 vty_escape_map(buf
[i
], vty
);
1473 /* Pre-escape status. */
1474 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1477 vty
->escape
= VTY_ESCAPE
;
1480 vty_backward_word(vty
);
1481 vty
->escape
= VTY_NORMAL
;
1484 vty_forward_word(vty
);
1485 vty
->escape
= VTY_NORMAL
;
1488 vty_forward_kill_word(vty
);
1489 vty
->escape
= VTY_NORMAL
;
1493 vty_backward_kill_word(vty
);
1494 vty
->escape
= VTY_NORMAL
;
1497 vty
->escape
= VTY_NORMAL
;
1505 vty_beginning_of_line(vty
);
1508 vty_backward_char(vty
);
1511 vty_stop_input(vty
);
1514 vty_delete_char(vty
);
1517 vty_end_of_line(vty
);
1520 vty_forward_char(vty
);
1524 vty_delete_backward_char(vty
);
1533 vty_previous_line(vty
);
1536 vty_transpose_chars(vty
);
1539 vty_kill_line_from_beginning(vty
);
1542 vty_backward_kill_word(vty
);
1545 vty_end_config(vty
);
1553 vty_complete_command(vty
);
1556 if (vty
->node
== AUTH_NODE
1557 || vty
->node
== AUTH_ENABLE_NODE
)
1558 vty_self_insert(vty
, buf
[i
]);
1560 vty_describe_command(vty
);
1563 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1564 vty
->escape
= VTY_ESCAPE
;
1567 vty
->escape
= VTY_PRE_ESCAPE
;
1570 if (buf
[i
] > 31 && buf
[i
] < 127)
1571 vty_self_insert(vty
, buf
[i
]);
1577 if (vty
->status
== VTY_CLOSE
)
1580 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1581 vty_event(VTY_READ
, vty_sock
, vty
);
1586 /* Flush buffer to the vty. */
1587 static int vty_flush(struct thread
*thread
)
1590 buffer_status_t flushrc
;
1591 int vty_sock
= THREAD_FD(thread
);
1592 struct vty
*vty
= THREAD_ARG(thread
);
1594 vty
->t_write
= NULL
;
1596 /* Tempolary disable read thread. */
1597 if ((vty
->lines
== 0) && vty
->t_read
) {
1598 thread_cancel(vty
->t_read
);
1602 /* Function execution continue. */
1603 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1605 /* N.B. if width is 0, that means we don't know the window size. */
1606 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1607 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1608 else if (vty
->status
== VTY_MORELINE
)
1609 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1612 flushrc
= buffer_flush_window(
1613 vty
->obuf
, vty_sock
, vty
->width
,
1614 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1618 0; /* disable monitoring to avoid infinite recursion */
1619 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1621 buffer_reset(vty
->obuf
);
1625 if (vty
->status
== VTY_CLOSE
)
1628 vty
->status
= VTY_NORMAL
;
1629 if (vty
->lines
== 0)
1630 vty_event(VTY_READ
, vty_sock
, vty
);
1633 case BUFFER_PENDING
:
1634 /* There is more data waiting to be written. */
1635 vty
->status
= VTY_MORE
;
1636 if (vty
->lines
== 0)
1637 vty_event(VTY_WRITE
, vty_sock
, vty
);
1644 /* Allocate new vty struct. */
1645 struct vty
*vty_new()
1647 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1649 new->fd
= new->wfd
= -1;
1650 new->obuf
= buffer_new(0); /* Use default buffer size. */
1651 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1652 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1653 new->max
= VTY_BUFSIZ
;
1659 /* allocate and initialise vty */
1660 static struct vty
*vty_new_init(int vty_sock
)
1666 vty
->wfd
= vty_sock
;
1667 vty
->type
= VTY_TERM
;
1668 vty
->node
= AUTH_NODE
;
1673 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1676 vector_set_index(vtyvec
, vty_sock
, vty
);
1677 vty
->status
= VTY_NORMAL
;
1680 vty
->iac_sb_in_progress
= 0;
1686 /* Create new vty structure. */
1687 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1689 char buf
[SU_ADDRSTRLEN
];
1692 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1694 /* Allocate new vty structure and set up default values. */
1695 vty
= vty_new_init(vty_sock
);
1697 /* configurable parameters not part of basic init */
1698 vty
->v_timeout
= vty_timeout_val
;
1699 strcpy(vty
->address
, buf
);
1700 if (no_password_check
) {
1702 vty
->node
= ENABLE_NODE
;
1704 vty
->node
= VIEW_NODE
;
1706 if (host
.lines
>= 0)
1707 vty
->lines
= host
.lines
;
1709 if (!no_password_check
) {
1710 /* Vty is not available if password isn't set. */
1711 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1712 vty_out(vty
, "Vty password is not set.\n");
1713 vty
->status
= VTY_CLOSE
;
1719 /* Say hello to the world. */
1721 if (!no_password_check
)
1722 vty_out(vty
, "\nUser Access Verification\n\n");
1724 /* Setting up terminal. */
1726 vty_will_suppress_go_ahead(vty
);
1728 vty_dont_linemode(vty
);
1729 vty_do_window_size(vty
);
1730 /* vty_dont_lflow_ahead (vty); */
1734 /* Add read/write thread. */
1735 vty_event(VTY_WRITE
, vty_sock
, vty
);
1736 vty_event(VTY_READ
, vty_sock
, vty
);
1741 /* create vty for stdio */
1742 static struct termios stdio_orig_termios
;
1743 static struct vty
*stdio_vty
= NULL
;
1744 static bool stdio_termios
= false;
1745 static void (*stdio_vty_atclose
)(int isexit
);
1747 static void vty_stdio_reset(int isexit
)
1751 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1752 stdio_termios
= false;
1756 if (stdio_vty_atclose
)
1757 stdio_vty_atclose(isexit
);
1758 stdio_vty_atclose
= NULL
;
1762 static void vty_stdio_atexit(void)
1767 void vty_stdio_suspend(void)
1772 if (stdio_vty
->t_write
)
1773 thread_cancel(stdio_vty
->t_write
);
1774 if (stdio_vty
->t_read
)
1775 thread_cancel(stdio_vty
->t_read
);
1776 if (stdio_vty
->t_timeout
)
1777 thread_cancel(stdio_vty
->t_timeout
);
1780 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1781 stdio_termios
= false;
1784 void vty_stdio_resume(void)
1789 if (!tcgetattr(0, &stdio_orig_termios
)) {
1790 struct termios termios
;
1792 termios
= stdio_orig_termios
;
1793 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1794 | IGNCR
| ICRNL
| IXON
);
1795 termios
.c_oflag
&= ~OPOST
;
1796 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1797 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1798 termios
.c_cflag
|= CS8
;
1799 tcsetattr(0, TCSANOW
, &termios
);
1800 stdio_termios
= true;
1803 vty_prompt(stdio_vty
);
1805 /* Add read/write thread. */
1806 vty_event(VTY_WRITE
, 1, stdio_vty
);
1807 vty_event(VTY_READ
, 0, stdio_vty
);
1810 void vty_stdio_close(void)
1814 vty_close(stdio_vty
);
1817 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1821 /* refuse creating two vtys on stdio */
1825 vty
= stdio_vty
= vty_new_init(0);
1826 stdio_vty_atclose
= atclose
;
1829 /* always have stdio vty in a known _unchangeable_ state, don't want
1831 * to have any effect here to make sure scripting this works as intended
1833 vty
->node
= ENABLE_NODE
;
1835 strcpy(vty
->address
, "console");
1841 /* Accept connection from the network. */
1842 static int vty_accept(struct thread
*thread
)
1850 struct access_list
*acl
= NULL
;
1851 char buf
[SU_ADDRSTRLEN
];
1853 accept_sock
= THREAD_FD(thread
);
1855 /* We continue hearing vty socket. */
1856 vty_event(VTY_SERV
, accept_sock
, NULL
);
1858 memset(&su
, 0, sizeof(union sockunion
));
1860 /* We can handle IPv4 or IPv6 socket. */
1861 vty_sock
= sockunion_accept(accept_sock
, &su
);
1863 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1866 set_nonblocking(vty_sock
);
1867 set_cloexec(vty_sock
);
1869 sockunion2hostprefix(&su
, &p
);
1871 /* VTY's accesslist apply. */
1872 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1873 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1874 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1875 zlog_info("Vty connection refused from %s",
1876 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1879 /* continue accepting connections */
1880 vty_event(VTY_SERV
, accept_sock
, NULL
);
1886 /* VTY's ipv6 accesslist apply. */
1887 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1888 if ((acl
= access_list_lookup(AFI_IP6
,
1889 vty_ipv6_accesslist_name
))
1890 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1891 zlog_info("Vty connection refused from %s",
1892 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1895 /* continue accepting connections */
1896 vty_event(VTY_SERV
, accept_sock
, NULL
);
1903 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1906 zlog_info("can't set sockopt to vty_sock : %s",
1907 safe_strerror(errno
));
1909 zlog_info("Vty connection from %s",
1910 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1912 vty_create(vty_sock
, &su
);
1917 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1920 struct addrinfo req
;
1921 struct addrinfo
*ainfo
;
1922 struct addrinfo
*ainfo_save
;
1924 char port_str
[BUFSIZ
];
1926 memset(&req
, 0, sizeof(struct addrinfo
));
1927 req
.ai_flags
= AI_PASSIVE
;
1928 req
.ai_family
= AF_UNSPEC
;
1929 req
.ai_socktype
= SOCK_STREAM
;
1930 sprintf(port_str
, "%d", port
);
1931 port_str
[sizeof(port_str
) - 1] = '\0';
1933 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1936 zlog_err("getaddrinfo failed: %s", gai_strerror(ret
));
1943 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1946 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1947 ainfo
->ai_protocol
);
1951 sockopt_v6only(ainfo
->ai_family
, sock
);
1952 sockopt_reuseaddr(sock
);
1953 sockopt_reuseport(sock
);
1956 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1958 close(sock
); /* Avoid sd leak. */
1962 ret
= listen(sock
, 3);
1964 close(sock
); /* Avoid sd leak. */
1968 vty_event(VTY_SERV
, sock
, NULL
);
1969 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1971 freeaddrinfo(ainfo_save
);
1975 /* For sockaddr_un. */
1978 /* VTY shell UNIX domain socket. */
1979 static void vty_serv_un(const char *path
)
1983 struct sockaddr_un serv
;
1985 struct zprivs_ids_t ids
;
1987 /* First of all, unlink existing socket */
1991 old_mask
= umask(0007);
1993 /* Make UNIX domain socket. */
1994 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1996 zlog_err("Cannot create unix stream socket: %s",
1997 safe_strerror(errno
));
2001 /* Make server socket. */
2002 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2003 serv
.sun_family
= AF_UNIX
;
2004 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2005 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2006 len
= serv
.sun_len
= SUN_LEN(&serv
);
2008 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2009 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2013 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2015 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2016 close(sock
); /* Avoid sd leak. */
2020 ret
= listen(sock
, 5);
2022 zlog_err("listen(fd %d) failed: %s", sock
,
2023 safe_strerror(errno
));
2024 close(sock
); /* Avoid sd leak. */
2030 zprivs_get_ids(&ids
);
2032 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2033 earlier for the case when we don't need to chown the file
2034 type casting it here to make a compare */
2035 if ((int)ids
.gid_vty
> 0) {
2036 /* set group of socket */
2037 if (chown(path
, -1, ids
.gid_vty
)) {
2038 zlog_err("vty_serv_un: could chown socket, %s",
2039 safe_strerror(errno
));
2043 vty_event(VTYSH_SERV
, sock
, NULL
);
2046 /* #define VTYSH_DEBUG 1 */
2048 static int vtysh_accept(struct thread
*thread
)
2053 struct sockaddr_un client
;
2056 accept_sock
= THREAD_FD(thread
);
2058 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2060 memset(&client
, 0, sizeof(struct sockaddr_un
));
2061 client_len
= sizeof(struct sockaddr_un
);
2063 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2064 (socklen_t
*)&client_len
);
2067 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2071 if (set_nonblocking(sock
) < 0) {
2073 "vtysh_accept: could not set vty socket %d to non-blocking,"
2075 sock
, safe_strerror(errno
));
2082 printf("VTY shell accept\n");
2083 #endif /* VTYSH_DEBUG */
2088 vty
->type
= VTY_SHELL_SERV
;
2089 vty
->node
= VIEW_NODE
;
2091 vty_event(VTYSH_READ
, sock
, vty
);
2096 static int vtysh_flush(struct vty
*vty
)
2098 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2099 case BUFFER_PENDING
:
2100 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2104 0; /* disable monitoring to avoid infinite recursion */
2105 zlog_warn("%s: write error to fd %d, closing", __func__
,
2107 buffer_reset(vty
->obuf
);
2117 static int vtysh_read(struct thread
*thread
)
2123 unsigned char buf
[VTY_READ_BUFSIZ
];
2125 uint8_t header
[4] = {0, 0, 0, 0};
2127 sock
= THREAD_FD(thread
);
2128 vty
= THREAD_ARG(thread
);
2131 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2133 if (ERRNO_IO_RETRY(errno
)) {
2134 vty_event(VTYSH_READ
, sock
, vty
);
2137 vty
->monitor
= 0; /* disable monitoring to avoid
2138 infinite recursion */
2140 "%s: read failed on vtysh client fd %d, closing: %s",
2141 __func__
, sock
, safe_strerror(errno
));
2143 buffer_reset(vty
->obuf
);
2146 printf("close vtysh\n");
2147 #endif /* VTYSH_DEBUG */
2152 printf("line: %.*s\n", nbytes
, buf
);
2153 #endif /* VTYSH_DEBUG */
2155 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2156 /* Clear command line buffer. */
2157 vty
->cp
= vty
->length
= 0;
2159 vty_out(vty
, "%% Command is too long.\n");
2161 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2162 vty
->buf
[vty
->length
++] = *p
;
2164 /* Pass this line to parser. */
2165 ret
= vty_execute(vty
);
2166 /* Note that vty_execute clears the command buffer and resets
2167 vty->length to 0. */
2169 /* Return result. */
2171 printf("result: %d\n", ret
);
2172 printf("vtysh node: %d\n", vty
->node
);
2173 #endif /* VTYSH_DEBUG */
2175 /* hack for asynchronous "write integrated"
2176 * - other commands in "buf" will be ditched
2177 * - input during pending config-write is
2179 if (ret
== CMD_SUSPEND
)
2182 /* warning: watchfrr hardcodes this result write
2185 buffer_put(vty
->obuf
, header
, 4);
2187 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2188 /* Try to flush results; exit if a write
2195 if (vty
->status
== VTY_CLOSE
)
2198 vty_event(VTYSH_READ
, sock
, vty
);
2203 static int vtysh_write(struct thread
*thread
)
2205 struct vty
*vty
= THREAD_ARG(thread
);
2207 vty
->t_write
= NULL
;
2214 /* Determine address family to bind. */
2215 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2217 /* If port is set to 0, do not listen on TCP/IP at all! */
2219 vty_serv_sock_addrinfo(addr
, port
);
2226 /* Close vty interface. Warning: call this only from functions that
2227 will be careful not to access the vty afterwards (since it has
2228 now been freed). This is safest from top-level functions (called
2229 directly by the thread dispatcher). */
2230 void vty_close(struct vty
*vty
)
2233 bool was_stdio
= false;
2235 /* Cancel threads.*/
2237 thread_cancel(vty
->t_read
);
2239 thread_cancel(vty
->t_write
);
2241 thread_cancel(vty
->t_timeout
);
2244 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2246 /* Free input buffer. */
2247 buffer_free(vty
->obuf
);
2249 /* Free command history. */
2250 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2252 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2256 vector_unset(vtyvec
, vty
->fd
);
2258 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2262 * note check is for fd > STDERR_FILENO, not fd != -1.
2263 * We never close stdin/stdout/stderr here, because we may be
2264 * running in foreground mode with logging to stdout. Also,
2265 * additionally, we'd need to replace these fds with /dev/null. */
2266 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2268 if (vty
->fd
> STDERR_FILENO
) {
2274 XFREE(MTYPE_VTY
, vty
->buf
);
2277 XFREE(MTYPE_VTY
, vty
->error_buf
);
2279 /* Check configure. */
2280 vty_config_unlock(vty
);
2283 XFREE(MTYPE_VTY
, vty
);
2289 /* When time out occur output message then close connection. */
2290 static int vty_timeout(struct thread
*thread
)
2294 vty
= THREAD_ARG(thread
);
2295 vty
->t_timeout
= NULL
;
2299 buffer_reset(vty
->obuf
);
2300 vty_out(vty
, "\nVty connection is timed out.\n");
2302 /* Close connection. */
2303 vty
->status
= VTY_CLOSE
;
2309 /* Read up configuration file from file_name. */
2310 static void vty_read_file(FILE *confp
)
2314 unsigned int line_num
= 0;
2317 /* vty_close won't close stderr; if some config command prints
2318 * something it'll end up there. (not ideal; it'd be beter if output
2319 * from a file-load went to logging instead. Also note that if this
2320 * function is called after daemonizing, stderr will be /dev/null.)
2322 * vty->fd will be -1 from vty_new()
2324 vty
->wfd
= STDERR_FILENO
;
2325 vty
->type
= VTY_FILE
;
2326 vty
->node
= CONFIG_NODE
;
2328 /* Execute configuration file */
2329 ret
= config_from_file(vty
, confp
, &line_num
);
2331 /* Flush any previous errors before printing messages below */
2332 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2334 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2335 const char *message
= NULL
;
2339 case CMD_ERR_AMBIGUOUS
:
2340 message
= "Ambiguous command";
2342 case CMD_ERR_NO_MATCH
:
2343 message
= "No such command";
2346 message
= "Command returned Warning";
2348 case CMD_WARNING_CONFIG_FAILED
:
2349 message
= "Command returned Warning Config Failed";
2351 case CMD_ERR_INCOMPLETE
:
2352 message
= "Command returned Incomplete";
2354 case CMD_ERR_EXEED_ARGC_MAX
:
2356 "Command exceeded maximum number of Arguments";
2359 message
= "Command returned unhandled error message";
2363 nl
= strchr(vty
->error_buf
, '\n');
2366 zlog_err("ERROR: %s on config line %u: %s", message
, line_num
,
2373 static FILE *vty_use_backup_config(const char *fullpath
)
2375 char *fullpath_sav
, *fullpath_tmp
;
2381 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2382 strcpy(fullpath_sav
, fullpath
);
2383 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2385 sav
= open(fullpath_sav
, O_RDONLY
);
2391 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2392 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2394 /* Open file to configuration write. */
2395 tmp
= mkstemp(fullpath_tmp
);
2399 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2402 while ((c
= read(sav
, buffer
, 512)) > 0) {
2403 if (write(tmp
, buffer
, c
) <= 0)
2409 if (rename(fullpath_tmp
, fullpath
) == 0)
2410 ret
= fopen(fullpath
, "r");
2412 unlink(fullpath_tmp
);
2417 unlink(fullpath_tmp
);
2427 /* Read up configuration file from file_name. */
2428 void vty_read_config(const char *config_file
, char *config_default_dir
)
2430 char cwd
[MAXPATHLEN
];
2432 const char *fullpath
;
2435 /* If -f flag specified. */
2436 if (config_file
!= NULL
) {
2437 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2438 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2440 "Failure to determine Current Working Directory %d!",
2444 tmp
= XMALLOC(MTYPE_TMP
,
2445 strlen(cwd
) + strlen(config_file
) + 2);
2446 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2449 fullpath
= config_file
;
2451 confp
= fopen(fullpath
, "r");
2453 if (confp
== NULL
) {
2454 zlog_err("%s: failed to open configuration file %s: %s",
2455 __func__
, fullpath
, safe_strerror(errno
));
2457 confp
= vty_use_backup_config(fullpath
);
2460 "WARNING: using backup configuration file!");
2462 zlog_err("can't open configuration file [%s]",
2469 host_config_set(config_default_dir
);
2473 struct stat conf_stat
;
2475 /* !!!!PLEASE LEAVE!!!!
2476 * This is NEEDED for use with vtysh -b, or else you can get
2477 * a real configuration food fight with a lot garbage in the
2478 * merged configuration file it creates coming from the per
2479 * daemon configuration files. This also allows the daemons
2480 * to start if there default configuration file is not
2481 * present or ignore them, as needed when using vtysh -b to
2482 * configure the daemons at boot - MAG
2485 /* Stat for vtysh Zebra.conf, if found startup and wait for
2486 * boot configuration
2489 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2490 ret
= stat(integrate_default
, &conf_stat
);
2492 goto tmp_free_and_out
;
2495 confp
= fopen(config_default_dir
, "r");
2496 if (confp
== NULL
) {
2497 zlog_err("%s: failed to open configuration file %s: %s",
2498 __func__
, config_default_dir
,
2499 safe_strerror(errno
));
2501 confp
= vty_use_backup_config(config_default_dir
);
2504 "WARNING: using backup configuration file!");
2505 fullpath
= config_default_dir
;
2507 zlog_err("can't open configuration file [%s]",
2508 config_default_dir
);
2509 goto tmp_free_and_out
;
2512 fullpath
= config_default_dir
;
2515 vty_read_file(confp
);
2519 host_config_set(fullpath
);
2523 XFREE(MTYPE_TMP
, tmp
);
2526 /* Small utility function which output log to the VTY. */
2527 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2528 struct timestamp_control
*ctl
, va_list va
)
2536 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2537 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2541 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2547 /* Async-signal-safe version of vty_log for fixed strings. */
2548 void vty_log_fixed(char *buf
, size_t len
)
2551 struct iovec iov
[2];
2552 char crlf
[4] = "\r\n";
2554 /* vty may not have been initialised */
2558 iov
[0].iov_base
= buf
;
2559 iov
[0].iov_len
= len
;
2560 iov
[1].iov_base
= crlf
;
2563 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2565 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2566 /* N.B. We don't care about the return code, since
2568 most likely just about to die anyway. */
2569 if (writev(vty
->wfd
, iov
, 2) == -1) {
2570 fprintf(stderr
, "Failure to writev: %d\n",
2577 int vty_config_lock(struct vty
*vty
)
2579 if (vty_config_is_lockless
)
2581 if (vty_config
== 0) {
2588 int vty_config_unlock(struct vty
*vty
)
2590 if (vty_config_is_lockless
)
2592 if (vty_config
== 1 && vty
->config
== 1) {
2599 void vty_config_lockless(void)
2601 vty_config_is_lockless
= 1;
2604 /* Master of the threads. */
2605 static struct thread_master
*vty_master
;
2607 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2609 struct thread
*vty_serv_thread
= NULL
;
2613 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2615 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2619 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2621 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2625 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2629 vty
->t_write
= NULL
;
2630 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2636 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2638 /* Time out treatment. */
2639 if (vty
->v_timeout
) {
2641 thread_cancel(vty
->t_timeout
);
2642 vty
->t_timeout
= NULL
;
2643 thread_add_timer(vty_master
, vty_timeout
, vty
,
2644 vty
->v_timeout
, &vty
->t_timeout
);
2648 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2651 case VTY_TIMEOUT_RESET
:
2652 if (vty
->t_timeout
) {
2653 thread_cancel(vty
->t_timeout
);
2654 vty
->t_timeout
= NULL
;
2656 if (vty
->v_timeout
) {
2657 vty
->t_timeout
= NULL
;
2658 thread_add_timer(vty_master
, vty_timeout
, vty
,
2659 vty
->v_timeout
, &vty
->t_timeout
);
2665 DEFUN_NOSH (config_who
,
2668 "Display who is on vty\n")
2673 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2674 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2675 vty_out(vty
, "%svty[%d] connected from %s.\n",
2676 v
->config
? "*" : " ", i
, v
->address
);
2680 /* Move to vty configuration mode. */
2681 DEFUN_NOSH (line_vty
,
2684 "Configure a terminal line\n"
2685 "Virtual terminal\n")
2687 vty
->node
= VTY_NODE
;
2691 /* Set time out value. */
2692 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2693 const char *sec_str
)
2695 unsigned long timeout
= 0;
2697 /* min_str and sec_str are already checked by parser. So it must be
2698 all digit string. */
2700 timeout
= strtol(min_str
, NULL
, 10);
2704 timeout
+= strtol(sec_str
, NULL
, 10);
2706 vty_timeout_val
= timeout
;
2707 vty
->v_timeout
= timeout
;
2708 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2714 DEFUN (exec_timeout_min
,
2715 exec_timeout_min_cmd
,
2716 "exec-timeout (0-35791)",
2717 "Set timeout value\n"
2718 "Timeout value in minutes\n")
2721 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2724 DEFUN (exec_timeout_sec
,
2725 exec_timeout_sec_cmd
,
2726 "exec-timeout (0-35791) (0-2147483)",
2727 "Set the EXEC timeout\n"
2728 "Timeout in minutes\n"
2729 "Timeout in seconds\n")
2732 int idx_number_2
= 2;
2733 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2734 argv
[idx_number_2
]->arg
);
2737 DEFUN (no_exec_timeout
,
2738 no_exec_timeout_cmd
,
2741 "Set the EXEC timeout\n")
2743 return exec_timeout(vty
, NULL
, NULL
);
2746 /* Set vty access class. */
2747 DEFUN (vty_access_class
,
2748 vty_access_class_cmd
,
2749 "access-class WORD",
2750 "Filter connections based on an IP access list\n"
2754 if (vty_accesslist_name
)
2755 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2757 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2762 /* Clear vty access class. */
2763 DEFUN (no_vty_access_class
,
2764 no_vty_access_class_cmd
,
2765 "no access-class [WORD]",
2767 "Filter connections based on an IP access list\n"
2771 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2772 if (!vty_accesslist_name
2773 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2774 vty_out(vty
, "Access-class is not currently applied to vty\n");
2775 return CMD_WARNING_CONFIG_FAILED
;
2778 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2780 vty_accesslist_name
= NULL
;
2785 /* Set vty access class. */
2786 DEFUN (vty_ipv6_access_class
,
2787 vty_ipv6_access_class_cmd
,
2788 "ipv6 access-class WORD",
2790 "Filter connections based on an IP access list\n"
2791 "IPv6 access list\n")
2794 if (vty_ipv6_accesslist_name
)
2795 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2797 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2802 /* Clear vty access class. */
2803 DEFUN (no_vty_ipv6_access_class
,
2804 no_vty_ipv6_access_class_cmd
,
2805 "no ipv6 access-class [WORD]",
2808 "Filter connections based on an IP access list\n"
2809 "IPv6 access list\n")
2812 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2814 if (!vty_ipv6_accesslist_name
2815 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2817 "IPv6 access-class is not currently applied to vty\n");
2818 return CMD_WARNING_CONFIG_FAILED
;
2821 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2823 vty_ipv6_accesslist_name
= NULL
;
2832 "Enable password checking\n")
2834 no_password_check
= 0;
2838 DEFUN (no_vty_login
,
2842 "Enable password checking\n")
2844 no_password_check
= 1;
2848 DEFUN (service_advanced_vty
,
2849 service_advanced_vty_cmd
,
2850 "service advanced-vty",
2851 "Set up miscellaneous service\n"
2852 "Enable advanced mode vty interface\n")
2858 DEFUN (no_service_advanced_vty
,
2859 no_service_advanced_vty_cmd
,
2860 "no service advanced-vty",
2862 "Set up miscellaneous service\n"
2863 "Enable advanced mode vty interface\n")
2869 DEFUN_NOSH (terminal_monitor
,
2870 terminal_monitor_cmd
,
2872 "Set terminal line parameters\n"
2873 "Copy debug output to the current terminal line\n")
2879 DEFUN_NOSH (terminal_no_monitor
,
2880 terminal_no_monitor_cmd
,
2881 "terminal no monitor",
2882 "Set terminal line parameters\n"
2884 "Copy debug output to the current terminal line\n")
2890 DEFUN_NOSH (no_terminal_monitor
,
2891 no_terminal_monitor_cmd
,
2892 "no terminal monitor",
2894 "Set terminal line parameters\n"
2895 "Copy debug output to the current terminal line\n")
2897 return terminal_no_monitor(self
, vty
, argc
, argv
);
2901 DEFUN_NOSH (show_history
,
2905 "Display the session command history\n")
2909 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2910 if (index
== VTY_MAXHIST
) {
2915 if (vty
->hist
[index
] != NULL
)
2916 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2925 DEFUN (log_commands
,
2929 "Log all commands (can't be unset without restart)\n")
2931 do_log_commands
= 1;
2935 /* Display current configuration. */
2936 static int vty_config_write(struct vty
*vty
)
2938 vty_out(vty
, "line vty\n");
2940 if (vty_accesslist_name
)
2941 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2943 if (vty_ipv6_accesslist_name
)
2944 vty_out(vty
, " ipv6 access-class %s\n",
2945 vty_ipv6_accesslist_name
);
2948 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2949 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2950 vty_timeout_val
% 60);
2953 if (no_password_check
)
2954 vty_out(vty
, " no login\n");
2956 if (do_log_commands
)
2957 vty_out(vty
, "log commands\n");
2959 vty_out(vty
, "!\n");
2964 struct cmd_node vty_node
= {
2965 VTY_NODE
, "%s(config-line)# ", 1,
2968 /* Reset all VTY status. */
2973 struct thread
*vty_serv_thread
;
2975 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2976 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2977 buffer_reset(vty
->obuf
);
2978 vty
->status
= VTY_CLOSE
;
2982 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
2983 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
2985 thread_cancel(vty_serv_thread
);
2986 vector_slot(Vvty_serv_thread
, i
) = NULL
;
2990 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2992 if (vty_accesslist_name
) {
2993 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2994 vty_accesslist_name
= NULL
;
2997 if (vty_ipv6_accesslist_name
) {
2998 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2999 vty_ipv6_accesslist_name
= NULL
;
3003 static void vty_save_cwd(void)
3005 char cwd
[MAXPATHLEN
];
3008 c
= getcwd(cwd
, MAXPATHLEN
);
3012 * At this point if these go wrong, more than likely
3013 * the whole world is coming down around us
3014 * Hence not worrying about it too much.
3016 if (!chdir(SYSCONFDIR
)) {
3017 zlog_err("Failure to chdir to %s, errno: %d",
3021 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3022 zlog_err("Failure to getcwd, errno: %d", errno
);
3027 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3028 strcpy(vty_cwd
, cwd
);
3036 int vty_shell(struct vty
*vty
)
3038 return vty
->type
== VTY_SHELL
? 1 : 0;
3041 int vty_shell_serv(struct vty
*vty
)
3043 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3046 void vty_init_vtysh()
3048 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3051 /* Install vty's own commands like `who' command. */
3052 void vty_init(struct thread_master
*master_thread
)
3054 /* For further configuration read, preserve current directory. */
3057 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3059 vty_master
= master_thread
;
3061 atexit(vty_stdio_atexit
);
3063 /* Initilize server thread vector. */
3064 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3066 /* Install bgp top node. */
3067 install_node(&vty_node
, vty_config_write
);
3069 install_element(VIEW_NODE
, &config_who_cmd
);
3070 install_element(VIEW_NODE
, &show_history_cmd
);
3071 install_element(CONFIG_NODE
, &line_vty_cmd
);
3072 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3073 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3074 install_element(CONFIG_NODE
, &show_history_cmd
);
3075 install_element(CONFIG_NODE
, &log_commands_cmd
);
3076 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3077 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3078 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3080 install_default(VTY_NODE
);
3081 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3082 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3083 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3084 install_element(VTY_NODE
, &vty_access_class_cmd
);
3085 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3086 install_element(VTY_NODE
, &vty_login_cmd
);
3087 install_element(VTY_NODE
, &no_vty_login_cmd
);
3088 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3089 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3092 void vty_terminate(void)
3095 XFREE(MTYPE_TMP
, vty_cwd
);
3097 if (vtyvec
&& Vvty_serv_thread
) {
3099 vector_free(vtyvec
);
3100 vector_free(Vvty_serv_thread
);
3102 Vvty_serv_thread
= NULL
;