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
)
123 if (!regexp
&& vty
->filter
) {
124 regfree(&vty
->include
);
129 errcode
= regcomp(&vty
->include
, regexp
,
130 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
133 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
134 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
142 /* VTY standard output function. */
143 int vty_out(struct vty
*vty
, const char *format
, ...)
152 if (vty
->frame_pos
) {
154 vty_out(vty
, "%s", vty
->frame
);
157 /* Try to write to initial buffer. */
158 va_start(args
, format
);
159 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
162 /* Initial buffer is not enough. */
163 if (len
< 0 || len
>= size
) {
170 p
= XREALLOC(MTYPE_VTY_OUT_BUF
, p
, size
);
174 va_start(args
, format
);
175 len
= vsnprintf(p
, size
, format
, args
);
178 if (len
> -1 && len
< size
)
183 /* When initial buffer is enough to store all output. */
189 vector lines
= frrstr_split_vec(buf
, "\n");
191 frrstr_filter_vec(lines
, &vty
->include
);
192 vector_compact(lines
);
195 * Consider the string "foo\n". If the regex is an empty string
196 * and the line ended with a newline, then the vector will look
202 * If the regex isn't empty, the vector will look like:
206 * In this case we'd like to preserve the newline, so we add
207 * the empty string [1] as in the first example.
209 if (buf
[strlen(buf
) - 1] == '\n' && vector_active(lines
) > 0
210 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
211 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
213 filtered
= frrstr_join_vec(lines
, "\n");
214 frrstr_strvec_free(lines
);
221 /* print with crlf replacement */
222 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
226 fprintf(vty
->of
, "%s", filtered
);
232 /* print without crlf replacement */
233 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
238 XFREE(MTYPE_TMP
, filtered
);
240 /* If p is not different with buf, it is allocated buffer. */
242 XFREE(MTYPE_VTY_OUT_BUF
, p
);
247 static int vty_log_out(struct vty
*vty
, const char *level
,
248 const char *proto_str
, const char *format
,
249 struct timestamp_control
*ctl
, va_list va
)
255 if (!ctl
->already_rendered
) {
256 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
258 ctl
->already_rendered
= 1;
260 if (ctl
->len
+ 1 >= sizeof(buf
))
262 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
267 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
270 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
271 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
274 if (((ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, va
)) < 0)
275 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
281 if (write(vty
->wfd
, buf
, len
) < 0) {
282 if (ERRNO_IO_RETRY(errno
))
283 /* Kernel buffer is full, probably too much debugging
285 drop the data and ignore. */
287 /* Fatal I/O error. */
289 0; /* disable monitoring to avoid infinite recursion */
290 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
291 __func__
, vty
->fd
, safe_strerror(errno
));
292 buffer_reset(vty
->obuf
);
293 /* cannot call vty_close, because a parent routine may still try
294 to access the vty struct */
295 vty
->status
= VTY_CLOSE
;
296 shutdown(vty
->fd
, SHUT_RDWR
);
302 /* Output current time to the vty. */
303 void vty_time_print(struct vty
*vty
, int cr
)
305 char buf
[QUAGGA_TIMESTAMP_LEN
];
307 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
308 zlog_info("quagga_timestamp error");
312 vty_out(vty
, "%s\n", buf
);
314 vty_out(vty
, "%s ", buf
);
319 /* Say hello to vty interface. */
320 void vty_hello(struct vty
*vty
)
326 f
= fopen(host
.motdfile
, "r");
328 while (fgets(buf
, sizeof(buf
), f
)) {
330 /* work backwards to ignore trailling isspace()
332 for (s
= buf
+ strlen(buf
);
333 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
336 vty_out(vty
, "%s\n", buf
);
340 vty_out(vty
, "MOTD file not found\n");
341 } else if (host
.motd
)
342 vty_out(vty
, "%s", host
.motd
);
345 /* Put out prompt and wait input from user. */
346 static void vty_prompt(struct vty
*vty
)
348 if (vty
->type
== VTY_TERM
) {
349 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
353 /* Send WILL TELOPT_ECHO to remote server. */
354 static void vty_will_echo(struct vty
*vty
)
356 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
357 vty_out(vty
, "%s", cmd
);
360 /* Make suppress Go-Ahead telnet option. */
361 static void vty_will_suppress_go_ahead(struct vty
*vty
)
363 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
364 vty_out(vty
, "%s", cmd
);
367 /* Make don't use linemode over telnet. */
368 static void vty_dont_linemode(struct vty
*vty
)
370 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
371 vty_out(vty
, "%s", cmd
);
374 /* Use window size. */
375 static void vty_do_window_size(struct vty
*vty
)
377 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
378 vty_out(vty
, "%s", cmd
);
381 #if 0 /* Currently not used. */
382 /* Make don't use lflow vty interface. */
384 vty_dont_lflow_ahead (struct vty
*vty
)
386 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
387 vty_out (vty
, "%s", cmd
);
391 /* Authentication of vty */
392 static void vty_auth(struct vty
*vty
, char *buf
)
395 enum node_type next_node
= 0;
397 char *crypt(const char *, const char *);
402 passwd
= host
.password_encrypt
;
404 passwd
= host
.password
;
406 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
408 next_node
= VIEW_NODE
;
410 case AUTH_ENABLE_NODE
:
412 passwd
= host
.enable_encrypt
;
414 passwd
= host
.enable
;
415 next_node
= ENABLE_NODE
;
421 fail
= strcmp(crypt(buf
, passwd
), passwd
);
423 fail
= strcmp(buf
, passwd
);
429 vty
->node
= next_node
; /* Success ! */
432 if (vty
->fail
>= 3) {
433 if (vty
->node
== AUTH_NODE
) {
435 "%% Bad passwords, too many failures!\n");
436 vty
->status
= VTY_CLOSE
;
438 /* AUTH_ENABLE_NODE */
441 "%% Bad enable passwords, too many failures!\n");
442 vty
->status
= VTY_CLOSE
;
448 /* Command execution over the vty interface. */
449 static int vty_command(struct vty
*vty
, char *buf
)
452 const char *protocolname
;
456 * Log non empty command lines
461 /* Skip white spaces. */
462 while (isspace((int)*cp
) && *cp
!= '\0')
465 if (cp
!= NULL
&& *cp
!= '\0') {
467 char vty_str
[VTY_BUFSIZ
];
468 char prompt_str
[VTY_BUFSIZ
];
470 /* format the base vty info */
471 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
473 for (i
= 0; i
< vector_active(vtyvec
); i
++)
474 if (vty
== vector_slot(vtyvec
, i
)) {
475 snprintf(vty_str
, sizeof(vty_str
),
476 "vty[%d]@%s", i
, vty
->address
);
480 /* format the prompt */
481 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
484 /* now log the command */
485 zlog_err("%s%s", prompt_str
, buf
);
488 #ifdef CONSUMED_TIME_CHECK
492 unsigned long realtime
, cputime
;
495 #endif /* CONSUMED_TIME_CHECK */
497 ret
= cmd_execute(vty
, buf
, NULL
, 0);
499 /* Get the name of the protocol if any */
500 protocolname
= frr_protoname
;
502 #ifdef CONSUMED_TIME_CHECK
504 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
505 > CONSUMED_TIME_CHECK
)
506 /* Warn about CPU hog that must be fixed. */
508 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
509 realtime
/ 1000, cputime
/ 1000, buf
);
511 #endif /* CONSUMED_TIME_CHECK */
513 if (ret
!= CMD_SUCCESS
)
516 if (vty
->type
== VTY_FILE
)
517 vty_out(vty
, "Warning...\n");
519 case CMD_ERR_AMBIGUOUS
:
520 vty_out(vty
, "%% Ambiguous command.\n");
522 case CMD_ERR_NO_MATCH
:
523 vty_out(vty
, "%% [%s] Unknown command: %s\n",
526 case CMD_ERR_INCOMPLETE
:
527 vty_out(vty
, "%% Command incomplete.\n");
534 static const char telnet_backward_char
= 0x08;
535 static const char telnet_space_char
= ' ';
537 /* Basic function to write buffer to vty. */
538 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
540 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
543 /* Should we do buffering here ? And make vty_flush (vty) ? */
544 buffer_put(vty
->obuf
, buf
, nbytes
);
547 /* Basic function to insert character into vty. */
548 static void vty_self_insert(struct vty
*vty
, char c
)
553 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
556 length
= vty
->length
- vty
->cp
;
557 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
558 vty
->buf
[vty
->cp
] = c
;
560 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
561 for (i
= 0; i
< length
; i
++)
562 vty_write(vty
, &telnet_backward_char
, 1);
567 vty
->buf
[vty
->length
] = '\0';
570 /* Self insert character 'c' in overwrite mode. */
571 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
573 if (vty
->cp
== vty
->length
) {
574 vty_self_insert(vty
, c
);
578 vty
->buf
[vty
->cp
++] = c
;
579 vty_write(vty
, &c
, 1);
583 * Insert a string into vty->buf at the current cursor position.
585 * If the resultant string would be larger than VTY_BUFSIZ it is
588 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
590 if (vty
->cp
== VTY_BUFSIZ
)
593 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
594 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
596 vty
->length
= MAX(vty
->cp
, vty
->length
);
597 vty
->buf
[vty
->length
] = '\0';
598 vty_write(vty
, str
, nwrite
);
601 /* Forward character. */
602 static void vty_forward_char(struct vty
*vty
)
604 if (vty
->cp
< vty
->length
) {
605 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
610 /* Backward character. */
611 static void vty_backward_char(struct vty
*vty
)
615 vty_write(vty
, &telnet_backward_char
, 1);
619 /* Move to the beginning of the line. */
620 static void vty_beginning_of_line(struct vty
*vty
)
623 vty_backward_char(vty
);
626 /* Move to the end of the line. */
627 static void vty_end_of_line(struct vty
*vty
)
629 while (vty
->cp
< vty
->length
)
630 vty_forward_char(vty
);
633 static void vty_kill_line_from_beginning(struct vty
*);
634 static void vty_redraw_line(struct vty
*);
636 /* Print command line history. This function is called from
637 vty_next_line and vty_previous_line. */
638 static void vty_history_print(struct vty
*vty
)
642 vty_kill_line_from_beginning(vty
);
644 /* Get previous line from history buffer */
645 length
= strlen(vty
->hist
[vty
->hp
]);
646 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
647 vty
->cp
= vty
->length
= length
;
648 vty
->buf
[vty
->length
] = '\0';
650 /* Redraw current line */
651 vty_redraw_line(vty
);
654 /* Show next command line history. */
655 static void vty_next_line(struct vty
*vty
)
659 if (vty
->hp
== vty
->hindex
)
662 /* Try is there history exist or not. */
664 if (try_index
== (VTY_MAXHIST
- 1))
669 /* If there is not history return. */
670 if (vty
->hist
[try_index
] == NULL
)
675 vty_history_print(vty
);
678 /* Show previous command line history. */
679 static void vty_previous_line(struct vty
*vty
)
685 try_index
= VTY_MAXHIST
- 1;
689 if (vty
->hist
[try_index
] == NULL
)
694 vty_history_print(vty
);
697 /* This function redraw all of the command line character. */
698 static void vty_redraw_line(struct vty
*vty
)
700 vty_write(vty
, vty
->buf
, vty
->length
);
701 vty
->cp
= vty
->length
;
705 static void vty_forward_word(struct vty
*vty
)
707 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
708 vty_forward_char(vty
);
710 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
711 vty_forward_char(vty
);
714 /* Backward word without skipping training space. */
715 static void vty_backward_pure_word(struct vty
*vty
)
717 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
718 vty_backward_char(vty
);
722 static void vty_backward_word(struct vty
*vty
)
724 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
725 vty_backward_char(vty
);
727 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
728 vty_backward_char(vty
);
731 /* When '^D' is typed at the beginning of the line we move to the down
733 static void vty_down_level(struct vty
*vty
)
741 /* When '^Z' is received from vty, move down to the enable mode. */
742 static void vty_end_config(struct vty
*vty
)
761 case BGP_VRF_POLICY_NODE
:
762 case BGP_VNC_DEFAULTS_NODE
:
763 case BGP_VNC_NVE_GROUP_NODE
:
764 case BGP_VNC_L2_GROUP_NODE
:
779 case LDP_IPV4_IFACE_NODE
:
780 case LDP_IPV6_IFACE_NODE
:
782 case LDP_PSEUDOWIRE_NODE
:
785 case KEYCHAIN_KEY_NODE
:
787 case BGP_EVPN_VNI_NODE
:
788 vty_config_unlock(vty
);
789 vty
->node
= ENABLE_NODE
;
792 /* Unknown node, we have to ignore it. */
800 /* Delete a charcter at the current point. */
801 static void vty_delete_char(struct vty
*vty
)
806 if (vty
->length
== 0) {
811 if (vty
->cp
== vty
->length
)
812 return; /* completion need here? */
814 size
= vty
->length
- vty
->cp
;
817 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
818 vty
->buf
[vty
->length
] = '\0';
820 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
823 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
824 vty_write(vty
, &telnet_space_char
, 1);
826 for (i
= 0; i
< size
; i
++)
827 vty_write(vty
, &telnet_backward_char
, 1);
830 /* Delete a character before the point. */
831 static void vty_delete_backward_char(struct vty
*vty
)
836 vty_backward_char(vty
);
837 vty_delete_char(vty
);
840 /* Kill rest of line from current point. */
841 static void vty_kill_line(struct vty
*vty
)
846 size
= vty
->length
- vty
->cp
;
851 for (i
= 0; i
< size
; i
++)
852 vty_write(vty
, &telnet_space_char
, 1);
853 for (i
= 0; i
< size
; i
++)
854 vty_write(vty
, &telnet_backward_char
, 1);
856 memset(&vty
->buf
[vty
->cp
], 0, size
);
857 vty
->length
= vty
->cp
;
860 /* Kill line from the beginning. */
861 static void vty_kill_line_from_beginning(struct vty
*vty
)
863 vty_beginning_of_line(vty
);
867 /* Delete a word before the point. */
868 static void vty_forward_kill_word(struct vty
*vty
)
870 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
871 vty_delete_char(vty
);
872 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
873 vty_delete_char(vty
);
876 /* Delete a word before the point. */
877 static void vty_backward_kill_word(struct vty
*vty
)
879 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
880 vty_delete_backward_char(vty
);
881 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
882 vty_delete_backward_char(vty
);
885 /* Transpose chars before or at the point. */
886 static void vty_transpose_chars(struct vty
*vty
)
890 /* If length is short or point is near by the beginning of line then
892 if (vty
->length
< 2 || vty
->cp
< 1)
895 /* In case of point is located at the end of the line. */
896 if (vty
->cp
== vty
->length
) {
897 c1
= vty
->buf
[vty
->cp
- 1];
898 c2
= vty
->buf
[vty
->cp
- 2];
900 vty_backward_char(vty
);
901 vty_backward_char(vty
);
902 vty_self_insert_overwrite(vty
, c1
);
903 vty_self_insert_overwrite(vty
, c2
);
905 c1
= vty
->buf
[vty
->cp
];
906 c2
= vty
->buf
[vty
->cp
- 1];
908 vty_backward_char(vty
);
909 vty_self_insert_overwrite(vty
, c1
);
910 vty_self_insert_overwrite(vty
, c2
);
914 /* Do completion at vty interface. */
915 static void vty_complete_command(struct vty
*vty
)
919 char **matched
= NULL
;
922 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
925 vline
= cmd_make_strvec(vty
->buf
);
929 /* In case of 'help \t'. */
930 if (isspace((int)vty
->buf
[vty
->length
- 1]))
931 vector_set(vline
, NULL
);
933 matched
= cmd_complete_command(vline
, vty
, &ret
);
935 cmd_free_strvec(vline
);
939 case CMD_ERR_AMBIGUOUS
:
940 vty_out(vty
, "%% Ambiguous command.\n");
942 vty_redraw_line(vty
);
944 case CMD_ERR_NO_MATCH
:
945 /* vty_out (vty, "%% There is no matched command.\n"); */
947 vty_redraw_line(vty
);
949 case CMD_COMPLETE_FULL_MATCH
:
951 /* 2016-11-28 equinox -- need to debug, SEGV here */
952 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
954 vty_redraw_line(vty
);
958 vty_redraw_line(vty
);
959 vty_backward_pure_word(vty
);
960 vty_insert_word_overwrite(vty
, matched
[0]);
961 vty_self_insert(vty
, ' ');
962 XFREE(MTYPE_COMPLETION
, matched
[0]);
964 case CMD_COMPLETE_MATCH
:
966 vty_redraw_line(vty
);
967 vty_backward_pure_word(vty
);
968 vty_insert_word_overwrite(vty
, matched
[0]);
969 XFREE(MTYPE_COMPLETION
, matched
[0]);
971 case CMD_COMPLETE_LIST_MATCH
:
972 for (i
= 0; matched
[i
] != NULL
; i
++) {
973 if (i
!= 0 && ((i
% 6) == 0))
975 vty_out(vty
, "%-10s ", matched
[i
]);
976 XFREE(MTYPE_COMPLETION
, matched
[i
]);
981 vty_redraw_line(vty
);
983 case CMD_ERR_NOTHING_TODO
:
985 vty_redraw_line(vty
);
991 XFREE(MTYPE_TMP
, matched
);
994 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
995 unsigned int desc_width
, struct cmd_token
*token
)
1003 if (desc_width
<= 0) {
1004 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
1008 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
1010 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
1011 for (pos
= desc_width
; pos
> 0; pos
--)
1012 if (*(p
+ pos
) == ' ')
1018 strncpy(buf
, p
, pos
);
1020 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
1025 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
1027 XFREE(MTYPE_TMP
, buf
);
1030 /* Describe matched command function. */
1031 static void vty_describe_command(struct vty
*vty
)
1036 unsigned int i
, width
, desc_width
;
1037 struct cmd_token
*token
, *token_cr
= NULL
;
1039 vline
= cmd_make_strvec(vty
->buf
);
1041 /* In case of '> ?'. */
1042 if (vline
== NULL
) {
1043 vline
= vector_init(1);
1044 vector_set(vline
, NULL
);
1045 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1046 vector_set(vline
, NULL
);
1048 describe
= cmd_describe_command(vline
, vty
, &ret
);
1052 /* Ambiguous error. */
1054 case CMD_ERR_AMBIGUOUS
:
1055 vty_out(vty
, "%% Ambiguous command.\n");
1058 case CMD_ERR_NO_MATCH
:
1059 vty_out(vty
, "%% There is no matched command.\n");
1064 /* Get width of command string. */
1066 for (i
= 0; i
< vector_active(describe
); i
++)
1067 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1070 if (token
->text
[0] == '\0')
1073 len
= strlen(token
->text
);
1079 /* Get width of description string. */
1080 desc_width
= vty
->width
- (width
+ 6);
1082 /* Print out description. */
1083 for (i
= 0; i
< vector_active(describe
); i
++)
1084 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1085 if (token
->text
[0] == '\0')
1088 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1094 vty_out(vty
, " %-s\n", token
->text
);
1095 else if (desc_width
>= strlen(token
->desc
))
1096 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1099 vty_describe_fold(vty
, width
, desc_width
,
1102 if (IS_VARYING_TOKEN(token
->type
)) {
1103 const char *ref
= vector_slot(
1104 vline
, vector_active(vline
) - 1);
1106 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1107 cmd_variable_complete(token
, ref
, varcomps
);
1109 if (vector_active(varcomps
) > 0) {
1110 char *ac
= cmd_variable_comp2str(
1111 varcomps
, vty
->width
);
1112 vty_out(vty
, "%s\n", ac
);
1113 XFREE(MTYPE_TMP
, ac
);
1116 vector_free(varcomps
);
1119 vty_out (vty
, " %-*s %s\n", width
1120 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1121 desc
->str
? desc
->str
: "");
1125 if ((token
= token_cr
)) {
1127 vty_out(vty
, " %-s\n", token
->text
);
1128 else if (desc_width
>= strlen(token
->desc
))
1129 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1132 vty_describe_fold(vty
, width
, desc_width
, token
);
1136 cmd_free_strvec(vline
);
1138 vector_free(describe
);
1141 vty_redraw_line(vty
);
1144 static void vty_clear_buf(struct vty
*vty
)
1146 memset(vty
->buf
, 0, vty
->max
);
1149 /* ^C stop current input and do not add command line to the history. */
1150 static void vty_stop_input(struct vty
*vty
)
1152 vty
->cp
= vty
->length
= 0;
1156 switch (vty
->node
) {
1159 /* Nothing to do. */
1162 case INTERFACE_NODE
:
1176 case LDP_IPV4_IFACE_NODE
:
1177 case LDP_IPV6_IFACE_NODE
:
1178 case LDP_L2VPN_NODE
:
1179 case LDP_PSEUDOWIRE_NODE
:
1182 case KEYCHAIN_KEY_NODE
:
1184 vty_config_unlock(vty
);
1185 vty
->node
= ENABLE_NODE
;
1188 /* Unknown node, we have to ignore it. */
1193 /* Set history pointer to the latest one. */
1194 vty
->hp
= vty
->hindex
;
1197 /* Add current command line to the history buffer. */
1198 static void vty_hist_add(struct vty
*vty
)
1202 if (vty
->length
== 0)
1205 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1207 /* Ignore the same string as previous one. */
1208 if (vty
->hist
[index
])
1209 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1210 vty
->hp
= vty
->hindex
;
1214 /* Insert history entry. */
1215 if (vty
->hist
[vty
->hindex
])
1216 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1217 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1219 /* History index rotation. */
1221 if (vty
->hindex
== VTY_MAXHIST
)
1224 vty
->hp
= vty
->hindex
;
1227 /* #define TELNET_OPTION_DEBUG */
1229 /* Get telnet window size. */
1230 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1232 #ifdef TELNET_OPTION_DEBUG
1235 for (i
= 0; i
< nbytes
; i
++) {
1238 vty_out(vty
, "IAC ");
1241 vty_out(vty
, "WILL ");
1244 vty_out(vty
, "WONT ");
1247 vty_out(vty
, "DO ");
1250 vty_out(vty
, "DONT ");
1253 vty_out(vty
, "SB ");
1256 vty_out(vty
, "SE ");
1259 vty_out(vty
, "TELOPT_ECHO \n");
1262 vty_out(vty
, "TELOPT_SGA \n");
1265 vty_out(vty
, "TELOPT_NAWS \n");
1268 vty_out(vty
, "%x ", buf
[i
]);
1274 #endif /* TELNET_OPTION_DEBUG */
1279 vty
->iac_sb_in_progress
= 1;
1283 if (!vty
->iac_sb_in_progress
)
1286 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1287 vty
->iac_sb_in_progress
= 0;
1290 switch (vty
->sb_buf
[0]) {
1292 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1294 "RFC 1073 violation detected: telnet NAWS option "
1295 "should send %d characters, but we received %lu",
1297 (unsigned long)vty
->sb_len
);
1298 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1300 "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1301 "too small to handle the telnet NAWS option",
1302 (unsigned long)sizeof(vty
->sb_buf
),
1303 TELNET_NAWS_SB_LEN
);
1305 vty
->width
= ((vty
->sb_buf
[1] << 8)
1307 vty
->height
= ((vty
->sb_buf
[3] << 8)
1309 #ifdef TELNET_OPTION_DEBUG
1311 "TELNET NAWS window size negotiation completed: "
1312 "width %d, height %d\n",
1313 vty
->width
, vty
->height
);
1318 vty
->iac_sb_in_progress
= 0;
1328 /* Execute current command line. */
1329 static int vty_execute(struct vty
*vty
)
1335 switch (vty
->node
) {
1337 case AUTH_ENABLE_NODE
:
1338 vty_auth(vty
, vty
->buf
);
1341 ret
= vty_command(vty
, vty
->buf
);
1342 if (vty
->type
== VTY_TERM
)
1347 /* Clear command line buffer. */
1348 vty
->cp
= vty
->length
= 0;
1351 if (vty
->status
!= VTY_CLOSE
)
1357 #define CONTROL(X) ((X) - '@')
1358 #define VTY_NORMAL 0
1359 #define VTY_PRE_ESCAPE 1
1360 #define VTY_ESCAPE 2
1362 /* Escape character command map. */
1363 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1367 vty_previous_line(vty
);
1373 vty_forward_char(vty
);
1376 vty_backward_char(vty
);
1382 /* Go back to normal mode. */
1383 vty
->escape
= VTY_NORMAL
;
1386 /* Quit print out to the buffer. */
1387 static void vty_buffer_reset(struct vty
*vty
)
1389 buffer_reset(vty
->obuf
);
1391 vty_redraw_line(vty
);
1394 /* Read data via vty socket. */
1395 static int vty_read(struct thread
*thread
)
1399 unsigned char buf
[VTY_READ_BUFSIZ
];
1401 int vty_sock
= THREAD_FD(thread
);
1402 struct vty
*vty
= THREAD_ARG(thread
);
1405 /* Read raw data from socket */
1406 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1408 if (ERRNO_IO_RETRY(errno
)) {
1409 vty_event(VTY_READ
, vty_sock
, vty
);
1412 vty
->monitor
= 0; /* disable monitoring to avoid
1413 infinite recursion */
1415 "%s: read error on vty client fd %d, closing: %s",
1416 __func__
, vty
->fd
, safe_strerror(errno
));
1417 buffer_reset(vty
->obuf
);
1419 vty
->status
= VTY_CLOSE
;
1422 for (i
= 0; i
< nbytes
; i
++) {
1423 if (buf
[i
] == IAC
) {
1432 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1433 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1434 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1440 /* In case of telnet command */
1442 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1449 if (vty
->status
== VTY_MORE
) {
1454 vty_buffer_reset(vty
);
1456 #if 0 /* More line does not work for "show ip bgp". */
1459 vty
->status
= VTY_MORELINE
;
1468 /* Escape character. */
1469 if (vty
->escape
== VTY_ESCAPE
) {
1470 vty_escape_map(buf
[i
], vty
);
1474 /* Pre-escape status. */
1475 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1478 vty
->escape
= VTY_ESCAPE
;
1481 vty_backward_word(vty
);
1482 vty
->escape
= VTY_NORMAL
;
1485 vty_forward_word(vty
);
1486 vty
->escape
= VTY_NORMAL
;
1489 vty_forward_kill_word(vty
);
1490 vty
->escape
= VTY_NORMAL
;
1494 vty_backward_kill_word(vty
);
1495 vty
->escape
= VTY_NORMAL
;
1498 vty
->escape
= VTY_NORMAL
;
1506 vty_beginning_of_line(vty
);
1509 vty_backward_char(vty
);
1512 vty_stop_input(vty
);
1515 vty_delete_char(vty
);
1518 vty_end_of_line(vty
);
1521 vty_forward_char(vty
);
1525 vty_delete_backward_char(vty
);
1534 vty_previous_line(vty
);
1537 vty_transpose_chars(vty
);
1540 vty_kill_line_from_beginning(vty
);
1543 vty_backward_kill_word(vty
);
1546 vty_end_config(vty
);
1554 vty_complete_command(vty
);
1557 if (vty
->node
== AUTH_NODE
1558 || vty
->node
== AUTH_ENABLE_NODE
)
1559 vty_self_insert(vty
, buf
[i
]);
1561 vty_describe_command(vty
);
1564 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1565 vty
->escape
= VTY_ESCAPE
;
1568 vty
->escape
= VTY_PRE_ESCAPE
;
1571 if (buf
[i
] > 31 && buf
[i
] < 127)
1572 vty_self_insert(vty
, buf
[i
]);
1578 if (vty
->status
== VTY_CLOSE
)
1581 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1582 vty_event(VTY_READ
, vty_sock
, vty
);
1587 /* Flush buffer to the vty. */
1588 static int vty_flush(struct thread
*thread
)
1591 buffer_status_t flushrc
;
1592 int vty_sock
= THREAD_FD(thread
);
1593 struct vty
*vty
= THREAD_ARG(thread
);
1595 vty
->t_write
= NULL
;
1597 /* Tempolary disable read thread. */
1598 if ((vty
->lines
== 0) && vty
->t_read
) {
1599 thread_cancel(vty
->t_read
);
1603 /* Function execution continue. */
1604 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1606 /* N.B. if width is 0, that means we don't know the window size. */
1607 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1608 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1609 else if (vty
->status
== VTY_MORELINE
)
1610 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1613 flushrc
= buffer_flush_window(
1614 vty
->obuf
, vty_sock
, vty
->width
,
1615 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1619 0; /* disable monitoring to avoid infinite recursion */
1620 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1622 buffer_reset(vty
->obuf
);
1626 if (vty
->status
== VTY_CLOSE
)
1629 vty
->status
= VTY_NORMAL
;
1630 if (vty
->lines
== 0)
1631 vty_event(VTY_READ
, vty_sock
, vty
);
1634 case BUFFER_PENDING
:
1635 /* There is more data waiting to be written. */
1636 vty
->status
= VTY_MORE
;
1637 if (vty
->lines
== 0)
1638 vty_event(VTY_WRITE
, vty_sock
, vty
);
1645 /* Allocate new vty struct. */
1646 struct vty
*vty_new()
1648 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1650 new->fd
= new->wfd
= -1;
1652 new->obuf
= buffer_new(0); /* Use default buffer size. */
1653 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1654 new->error_buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1655 new->max
= VTY_BUFSIZ
;
1661 /* allocate and initialise vty */
1662 static struct vty
*vty_new_init(int vty_sock
)
1668 vty
->wfd
= vty_sock
;
1669 vty
->type
= VTY_TERM
;
1670 vty
->node
= AUTH_NODE
;
1675 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1678 vector_set_index(vtyvec
, vty_sock
, vty
);
1679 vty
->status
= VTY_NORMAL
;
1682 vty
->iac_sb_in_progress
= 0;
1688 /* Create new vty structure. */
1689 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1691 char buf
[SU_ADDRSTRLEN
];
1694 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1696 /* Allocate new vty structure and set up default values. */
1697 vty
= vty_new_init(vty_sock
);
1699 /* configurable parameters not part of basic init */
1700 vty
->v_timeout
= vty_timeout_val
;
1701 strcpy(vty
->address
, buf
);
1702 if (no_password_check
) {
1704 vty
->node
= ENABLE_NODE
;
1706 vty
->node
= VIEW_NODE
;
1708 if (host
.lines
>= 0)
1709 vty
->lines
= host
.lines
;
1711 if (!no_password_check
) {
1712 /* Vty is not available if password isn't set. */
1713 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1714 vty_out(vty
, "Vty password is not set.\n");
1715 vty
->status
= VTY_CLOSE
;
1721 /* Say hello to the world. */
1723 if (!no_password_check
)
1724 vty_out(vty
, "\nUser Access Verification\n\n");
1726 /* Setting up terminal. */
1728 vty_will_suppress_go_ahead(vty
);
1730 vty_dont_linemode(vty
);
1731 vty_do_window_size(vty
);
1732 /* vty_dont_lflow_ahead (vty); */
1736 /* Add read/write thread. */
1737 vty_event(VTY_WRITE
, vty_sock
, vty
);
1738 vty_event(VTY_READ
, vty_sock
, vty
);
1743 /* create vty for stdio */
1744 static struct termios stdio_orig_termios
;
1745 static struct vty
*stdio_vty
= NULL
;
1746 static bool stdio_termios
= false;
1747 static void (*stdio_vty_atclose
)(int isexit
);
1749 static void vty_stdio_reset(int isexit
)
1753 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1754 stdio_termios
= false;
1758 if (stdio_vty_atclose
)
1759 stdio_vty_atclose(isexit
);
1760 stdio_vty_atclose
= NULL
;
1764 static void vty_stdio_atexit(void)
1769 void vty_stdio_suspend(void)
1774 if (stdio_vty
->t_write
)
1775 thread_cancel(stdio_vty
->t_write
);
1776 if (stdio_vty
->t_read
)
1777 thread_cancel(stdio_vty
->t_read
);
1778 if (stdio_vty
->t_timeout
)
1779 thread_cancel(stdio_vty
->t_timeout
);
1782 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1783 stdio_termios
= false;
1786 void vty_stdio_resume(void)
1791 if (!tcgetattr(0, &stdio_orig_termios
)) {
1792 struct termios termios
;
1794 termios
= stdio_orig_termios
;
1795 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1796 | IGNCR
| ICRNL
| IXON
);
1797 termios
.c_oflag
&= ~OPOST
;
1798 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1799 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1800 termios
.c_cflag
|= CS8
;
1801 tcsetattr(0, TCSANOW
, &termios
);
1802 stdio_termios
= true;
1805 vty_prompt(stdio_vty
);
1807 /* Add read/write thread. */
1808 vty_event(VTY_WRITE
, 1, stdio_vty
);
1809 vty_event(VTY_READ
, 0, stdio_vty
);
1812 void vty_stdio_close(void)
1816 vty_close(stdio_vty
);
1819 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1823 /* refuse creating two vtys on stdio */
1827 vty
= stdio_vty
= vty_new_init(0);
1828 stdio_vty_atclose
= atclose
;
1831 /* always have stdio vty in a known _unchangeable_ state, don't want
1833 * to have any effect here to make sure scripting this works as intended
1835 vty
->node
= ENABLE_NODE
;
1837 strcpy(vty
->address
, "console");
1843 /* Accept connection from the network. */
1844 static int vty_accept(struct thread
*thread
)
1852 struct access_list
*acl
= NULL
;
1853 char buf
[SU_ADDRSTRLEN
];
1855 accept_sock
= THREAD_FD(thread
);
1857 /* We continue hearing vty socket. */
1858 vty_event(VTY_SERV
, accept_sock
, NULL
);
1860 memset(&su
, 0, sizeof(union sockunion
));
1862 /* We can handle IPv4 or IPv6 socket. */
1863 vty_sock
= sockunion_accept(accept_sock
, &su
);
1865 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
1868 set_nonblocking(vty_sock
);
1869 set_cloexec(vty_sock
);
1871 sockunion2hostprefix(&su
, &p
);
1873 /* VTY's accesslist apply. */
1874 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1875 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1876 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1877 zlog_info("Vty connection refused from %s",
1878 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1881 /* continue accepting connections */
1882 vty_event(VTY_SERV
, accept_sock
, NULL
);
1888 /* VTY's ipv6 accesslist apply. */
1889 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1890 if ((acl
= access_list_lookup(AFI_IP6
,
1891 vty_ipv6_accesslist_name
))
1892 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1893 zlog_info("Vty connection refused from %s",
1894 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1897 /* continue accepting connections */
1898 vty_event(VTY_SERV
, accept_sock
, NULL
);
1905 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1908 zlog_info("can't set sockopt to vty_sock : %s",
1909 safe_strerror(errno
));
1911 zlog_info("Vty connection from %s",
1912 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1914 vty_create(vty_sock
, &su
);
1919 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1922 struct addrinfo req
;
1923 struct addrinfo
*ainfo
;
1924 struct addrinfo
*ainfo_save
;
1926 char port_str
[BUFSIZ
];
1928 memset(&req
, 0, sizeof(struct addrinfo
));
1929 req
.ai_flags
= AI_PASSIVE
;
1930 req
.ai_family
= AF_UNSPEC
;
1931 req
.ai_socktype
= SOCK_STREAM
;
1932 sprintf(port_str
, "%d", port
);
1933 port_str
[sizeof(port_str
) - 1] = '\0';
1935 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1938 zlog_err("getaddrinfo failed: %s", gai_strerror(ret
));
1945 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1948 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1949 ainfo
->ai_protocol
);
1953 sockopt_v6only(ainfo
->ai_family
, sock
);
1954 sockopt_reuseaddr(sock
);
1955 sockopt_reuseport(sock
);
1958 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1960 close(sock
); /* Avoid sd leak. */
1964 ret
= listen(sock
, 3);
1966 close(sock
); /* Avoid sd leak. */
1970 vty_event(VTY_SERV
, sock
, NULL
);
1971 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1973 freeaddrinfo(ainfo_save
);
1977 /* For sockaddr_un. */
1980 /* VTY shell UNIX domain socket. */
1981 static void vty_serv_un(const char *path
)
1985 struct sockaddr_un serv
;
1987 struct zprivs_ids_t ids
;
1989 /* First of all, unlink existing socket */
1993 old_mask
= umask(0007);
1995 /* Make UNIX domain socket. */
1996 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1998 zlog_err("Cannot create unix stream socket: %s",
1999 safe_strerror(errno
));
2003 /* Make server socket. */
2004 memset(&serv
, 0, sizeof(struct sockaddr_un
));
2005 serv
.sun_family
= AF_UNIX
;
2006 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
2007 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2008 len
= serv
.sun_len
= SUN_LEN(&serv
);
2010 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
2011 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2015 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
2017 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2018 close(sock
); /* Avoid sd leak. */
2022 ret
= listen(sock
, 5);
2024 zlog_err("listen(fd %d) failed: %s", sock
,
2025 safe_strerror(errno
));
2026 close(sock
); /* Avoid sd leak. */
2032 zprivs_get_ids(&ids
);
2034 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2035 earlier for the case when we don't need to chown the file
2036 type casting it here to make a compare */
2037 if ((int)ids
.gid_vty
> 0) {
2038 /* set group of socket */
2039 if (chown(path
, -1, ids
.gid_vty
)) {
2040 zlog_err("vty_serv_un: could chown socket, %s",
2041 safe_strerror(errno
));
2045 vty_event(VTYSH_SERV
, sock
, NULL
);
2048 /* #define VTYSH_DEBUG 1 */
2050 static int vtysh_accept(struct thread
*thread
)
2055 struct sockaddr_un client
;
2058 accept_sock
= THREAD_FD(thread
);
2060 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2062 memset(&client
, 0, sizeof(struct sockaddr_un
));
2063 client_len
= sizeof(struct sockaddr_un
);
2065 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2066 (socklen_t
*)&client_len
);
2069 zlog_warn("can't accept vty socket : %s", safe_strerror(errno
));
2073 if (set_nonblocking(sock
) < 0) {
2075 "vtysh_accept: could not set vty socket %d to non-blocking,"
2077 sock
, safe_strerror(errno
));
2084 printf("VTY shell accept\n");
2085 #endif /* VTYSH_DEBUG */
2090 vty
->type
= VTY_SHELL_SERV
;
2091 vty
->node
= VIEW_NODE
;
2093 vty_event(VTYSH_READ
, sock
, vty
);
2098 static int vtysh_flush(struct vty
*vty
)
2100 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2101 case BUFFER_PENDING
:
2102 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2106 0; /* disable monitoring to avoid infinite recursion */
2107 zlog_warn("%s: write error to fd %d, closing", __func__
,
2109 buffer_reset(vty
->obuf
);
2119 static int vtysh_read(struct thread
*thread
)
2125 unsigned char buf
[VTY_READ_BUFSIZ
];
2127 uint8_t header
[4] = {0, 0, 0, 0};
2129 sock
= THREAD_FD(thread
);
2130 vty
= THREAD_ARG(thread
);
2133 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2135 if (ERRNO_IO_RETRY(errno
)) {
2136 vty_event(VTYSH_READ
, sock
, vty
);
2139 vty
->monitor
= 0; /* disable monitoring to avoid
2140 infinite recursion */
2142 "%s: read failed on vtysh client fd %d, closing: %s",
2143 __func__
, sock
, safe_strerror(errno
));
2145 buffer_reset(vty
->obuf
);
2148 printf("close vtysh\n");
2149 #endif /* VTYSH_DEBUG */
2154 printf("line: %.*s\n", nbytes
, buf
);
2155 #endif /* VTYSH_DEBUG */
2157 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2158 /* Clear command line buffer. */
2159 vty
->cp
= vty
->length
= 0;
2161 vty_out(vty
, "%% Command is too long.\n");
2163 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2164 vty
->buf
[vty
->length
++] = *p
;
2166 /* Pass this line to parser. */
2167 ret
= vty_execute(vty
);
2168 /* Note that vty_execute clears the command buffer and resets
2169 vty->length to 0. */
2171 /* Return result. */
2173 printf("result: %d\n", ret
);
2174 printf("vtysh node: %d\n", vty
->node
);
2175 #endif /* VTYSH_DEBUG */
2177 /* hack for asynchronous "write integrated"
2178 * - other commands in "buf" will be ditched
2179 * - input during pending config-write is
2181 if (ret
== CMD_SUSPEND
)
2184 /* warning: watchfrr hardcodes this result write
2187 buffer_put(vty
->obuf
, header
, 4);
2189 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2190 /* Try to flush results; exit if a write
2197 if (vty
->status
== VTY_CLOSE
)
2200 vty_event(VTYSH_READ
, sock
, vty
);
2205 static int vtysh_write(struct thread
*thread
)
2207 struct vty
*vty
= THREAD_ARG(thread
);
2209 vty
->t_write
= NULL
;
2216 /* Determine address family to bind. */
2217 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2219 /* If port is set to 0, do not listen on TCP/IP at all! */
2221 vty_serv_sock_addrinfo(addr
, port
);
2228 /* Close vty interface. Warning: call this only from functions that
2229 will be careful not to access the vty afterwards (since it has
2230 now been freed). This is safest from top-level functions (called
2231 directly by the thread dispatcher). */
2232 void vty_close(struct vty
*vty
)
2235 bool was_stdio
= false;
2237 /* Cancel threads.*/
2239 thread_cancel(vty
->t_read
);
2241 thread_cancel(vty
->t_write
);
2243 thread_cancel(vty
->t_timeout
);
2246 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2248 /* Free input buffer. */
2249 buffer_free(vty
->obuf
);
2251 /* Free command history. */
2252 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2254 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2258 vector_unset(vtyvec
, vty
->fd
);
2260 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2264 * note check is for fd > STDERR_FILENO, not fd != -1.
2265 * We never close stdin/stdout/stderr here, because we may be
2266 * running in foreground mode with logging to stdout. Also,
2267 * additionally, we'd need to replace these fds with /dev/null. */
2268 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2270 if (vty
->fd
> STDERR_FILENO
) {
2276 XFREE(MTYPE_VTY
, vty
->buf
);
2279 XFREE(MTYPE_VTY
, vty
->error_buf
);
2281 /* Check configure. */
2282 vty_config_unlock(vty
);
2285 XFREE(MTYPE_VTY
, vty
);
2291 /* When time out occur output message then close connection. */
2292 static int vty_timeout(struct thread
*thread
)
2296 vty
= THREAD_ARG(thread
);
2297 vty
->t_timeout
= NULL
;
2301 buffer_reset(vty
->obuf
);
2302 vty_out(vty
, "\nVty connection is timed out.\n");
2304 /* Close connection. */
2305 vty
->status
= VTY_CLOSE
;
2311 /* Read up configuration file from file_name. */
2312 static void vty_read_file(FILE *confp
)
2316 unsigned int line_num
= 0;
2319 /* vty_close won't close stderr; if some config command prints
2320 * something it'll end up there. (not ideal; it'd be beter if output
2321 * from a file-load went to logging instead. Also note that if this
2322 * function is called after daemonizing, stderr will be /dev/null.)
2324 * vty->fd will be -1 from vty_new()
2326 vty
->wfd
= STDERR_FILENO
;
2327 vty
->type
= VTY_FILE
;
2328 vty
->node
= CONFIG_NODE
;
2330 /* Execute configuration file */
2331 ret
= config_from_file(vty
, confp
, &line_num
);
2333 /* Flush any previous errors before printing messages below */
2334 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2336 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2337 const char *message
= NULL
;
2341 case CMD_ERR_AMBIGUOUS
:
2342 message
= "Ambiguous command";
2344 case CMD_ERR_NO_MATCH
:
2345 message
= "No such command";
2348 message
= "Command returned Warning";
2350 case CMD_WARNING_CONFIG_FAILED
:
2351 message
= "Command returned Warning Config Failed";
2353 case CMD_ERR_INCOMPLETE
:
2354 message
= "Command returned Incomplete";
2356 case CMD_ERR_EXEED_ARGC_MAX
:
2358 "Command exceeded maximum number of Arguments";
2361 message
= "Command returned unhandled error message";
2365 nl
= strchr(vty
->error_buf
, '\n');
2368 zlog_err("ERROR: %s on config line %u: %s", message
, line_num
,
2375 static FILE *vty_use_backup_config(const char *fullpath
)
2377 char *fullpath_sav
, *fullpath_tmp
;
2383 fullpath_sav
= malloc(strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1);
2384 strcpy(fullpath_sav
, fullpath
);
2385 strcat(fullpath_sav
, CONF_BACKUP_EXT
);
2387 sav
= open(fullpath_sav
, O_RDONLY
);
2393 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2394 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2396 /* Open file to configuration write. */
2397 tmp
= mkstemp(fullpath_tmp
);
2401 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2404 while ((c
= read(sav
, buffer
, 512)) > 0) {
2405 if (write(tmp
, buffer
, c
) <= 0)
2411 if (rename(fullpath_tmp
, fullpath
) == 0)
2412 ret
= fopen(fullpath
, "r");
2414 unlink(fullpath_tmp
);
2419 unlink(fullpath_tmp
);
2429 /* Read up configuration file from file_name. */
2430 void vty_read_config(const char *config_file
, char *config_default_dir
)
2432 char cwd
[MAXPATHLEN
];
2434 const char *fullpath
;
2437 /* If -f flag specified. */
2438 if (config_file
!= NULL
) {
2439 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2440 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2442 "Failure to determine Current Working Directory %d!",
2446 tmp
= XMALLOC(MTYPE_TMP
,
2447 strlen(cwd
) + strlen(config_file
) + 2);
2448 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2451 fullpath
= config_file
;
2453 confp
= fopen(fullpath
, "r");
2455 if (confp
== NULL
) {
2456 zlog_err("%s: failed to open configuration file %s: %s",
2457 __func__
, fullpath
, safe_strerror(errno
));
2459 confp
= vty_use_backup_config(fullpath
);
2462 "WARNING: using backup configuration file!");
2464 zlog_err("can't open configuration file [%s]",
2471 host_config_set(config_default_dir
);
2475 struct stat conf_stat
;
2477 /* !!!!PLEASE LEAVE!!!!
2478 * This is NEEDED for use with vtysh -b, or else you can get
2479 * a real configuration food fight with a lot garbage in the
2480 * merged configuration file it creates coming from the per
2481 * daemon configuration files. This also allows the daemons
2482 * to start if there default configuration file is not
2483 * present or ignore them, as needed when using vtysh -b to
2484 * configure the daemons at boot - MAG
2487 /* Stat for vtysh Zebra.conf, if found startup and wait for
2488 * boot configuration
2491 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2492 ret
= stat(integrate_default
, &conf_stat
);
2494 goto tmp_free_and_out
;
2497 confp
= fopen(config_default_dir
, "r");
2498 if (confp
== NULL
) {
2499 zlog_err("%s: failed to open configuration file %s: %s",
2500 __func__
, config_default_dir
,
2501 safe_strerror(errno
));
2503 confp
= vty_use_backup_config(config_default_dir
);
2506 "WARNING: using backup configuration file!");
2507 fullpath
= config_default_dir
;
2509 zlog_err("can't open configuration file [%s]",
2510 config_default_dir
);
2511 goto tmp_free_and_out
;
2514 fullpath
= config_default_dir
;
2517 vty_read_file(confp
);
2521 host_config_set(fullpath
);
2525 XFREE(MTYPE_TMP
, tmp
);
2528 /* Small utility function which output log to the VTY. */
2529 void vty_log(const char *level
, const char *proto_str
, const char *format
,
2530 struct timestamp_control
*ctl
, va_list va
)
2538 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2539 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2543 vty_log_out(vty
, level
, proto_str
, format
, ctl
,
2549 /* Async-signal-safe version of vty_log for fixed strings. */
2550 void vty_log_fixed(char *buf
, size_t len
)
2553 struct iovec iov
[2];
2554 char crlf
[4] = "\r\n";
2556 /* vty may not have been initialised */
2560 iov
[0].iov_base
= buf
;
2561 iov
[0].iov_len
= len
;
2562 iov
[1].iov_base
= crlf
;
2565 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2567 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2568 /* N.B. We don't care about the return code, since
2570 most likely just about to die anyway. */
2571 if (writev(vty
->wfd
, iov
, 2) == -1) {
2572 fprintf(stderr
, "Failure to writev: %d\n",
2579 int vty_config_lock(struct vty
*vty
)
2581 if (vty_config_is_lockless
)
2583 if (vty_config
== 0) {
2590 int vty_config_unlock(struct vty
*vty
)
2592 if (vty_config_is_lockless
)
2594 if (vty_config
== 1 && vty
->config
== 1) {
2601 void vty_config_lockless(void)
2603 vty_config_is_lockless
= 1;
2606 /* Master of the threads. */
2607 static struct thread_master
*vty_master
;
2609 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2611 struct thread
*vty_serv_thread
= NULL
;
2615 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2617 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2621 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2623 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2627 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2631 vty
->t_write
= NULL
;
2632 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2638 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2640 /* Time out treatment. */
2641 if (vty
->v_timeout
) {
2643 thread_cancel(vty
->t_timeout
);
2644 vty
->t_timeout
= NULL
;
2645 thread_add_timer(vty_master
, vty_timeout
, vty
,
2646 vty
->v_timeout
, &vty
->t_timeout
);
2650 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2653 case VTY_TIMEOUT_RESET
:
2654 if (vty
->t_timeout
) {
2655 thread_cancel(vty
->t_timeout
);
2656 vty
->t_timeout
= NULL
;
2658 if (vty
->v_timeout
) {
2659 vty
->t_timeout
= NULL
;
2660 thread_add_timer(vty_master
, vty_timeout
, vty
,
2661 vty
->v_timeout
, &vty
->t_timeout
);
2667 DEFUN_NOSH (config_who
,
2670 "Display who is on vty\n")
2675 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2676 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2677 vty_out(vty
, "%svty[%d] connected from %s.\n",
2678 v
->config
? "*" : " ", i
, v
->address
);
2682 /* Move to vty configuration mode. */
2683 DEFUN_NOSH (line_vty
,
2686 "Configure a terminal line\n"
2687 "Virtual terminal\n")
2689 vty
->node
= VTY_NODE
;
2693 /* Set time out value. */
2694 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2695 const char *sec_str
)
2697 unsigned long timeout
= 0;
2699 /* min_str and sec_str are already checked by parser. So it must be
2700 all digit string. */
2702 timeout
= strtol(min_str
, NULL
, 10);
2706 timeout
+= strtol(sec_str
, NULL
, 10);
2708 vty_timeout_val
= timeout
;
2709 vty
->v_timeout
= timeout
;
2710 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2716 DEFUN (exec_timeout_min
,
2717 exec_timeout_min_cmd
,
2718 "exec-timeout (0-35791)",
2719 "Set timeout value\n"
2720 "Timeout value in minutes\n")
2723 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2726 DEFUN (exec_timeout_sec
,
2727 exec_timeout_sec_cmd
,
2728 "exec-timeout (0-35791) (0-2147483)",
2729 "Set the EXEC timeout\n"
2730 "Timeout in minutes\n"
2731 "Timeout in seconds\n")
2734 int idx_number_2
= 2;
2735 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2736 argv
[idx_number_2
]->arg
);
2739 DEFUN (no_exec_timeout
,
2740 no_exec_timeout_cmd
,
2743 "Set the EXEC timeout\n")
2745 return exec_timeout(vty
, NULL
, NULL
);
2748 /* Set vty access class. */
2749 DEFUN (vty_access_class
,
2750 vty_access_class_cmd
,
2751 "access-class WORD",
2752 "Filter connections based on an IP access list\n"
2756 if (vty_accesslist_name
)
2757 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2759 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2764 /* Clear vty access class. */
2765 DEFUN (no_vty_access_class
,
2766 no_vty_access_class_cmd
,
2767 "no access-class [WORD]",
2769 "Filter connections based on an IP access list\n"
2773 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2774 if (!vty_accesslist_name
2775 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2776 vty_out(vty
, "Access-class is not currently applied to vty\n");
2777 return CMD_WARNING_CONFIG_FAILED
;
2780 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2782 vty_accesslist_name
= NULL
;
2787 /* Set vty access class. */
2788 DEFUN (vty_ipv6_access_class
,
2789 vty_ipv6_access_class_cmd
,
2790 "ipv6 access-class WORD",
2792 "Filter connections based on an IP access list\n"
2793 "IPv6 access list\n")
2796 if (vty_ipv6_accesslist_name
)
2797 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2799 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2804 /* Clear vty access class. */
2805 DEFUN (no_vty_ipv6_access_class
,
2806 no_vty_ipv6_access_class_cmd
,
2807 "no ipv6 access-class [WORD]",
2810 "Filter connections based on an IP access list\n"
2811 "IPv6 access list\n")
2814 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2816 if (!vty_ipv6_accesslist_name
2817 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2819 "IPv6 access-class is not currently applied to vty\n");
2820 return CMD_WARNING_CONFIG_FAILED
;
2823 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2825 vty_ipv6_accesslist_name
= NULL
;
2834 "Enable password checking\n")
2836 no_password_check
= 0;
2840 DEFUN (no_vty_login
,
2844 "Enable password checking\n")
2846 no_password_check
= 1;
2850 DEFUN (service_advanced_vty
,
2851 service_advanced_vty_cmd
,
2852 "service advanced-vty",
2853 "Set up miscellaneous service\n"
2854 "Enable advanced mode vty interface\n")
2860 DEFUN (no_service_advanced_vty
,
2861 no_service_advanced_vty_cmd
,
2862 "no service advanced-vty",
2864 "Set up miscellaneous service\n"
2865 "Enable advanced mode vty interface\n")
2871 DEFUN_NOSH (terminal_monitor
,
2872 terminal_monitor_cmd
,
2874 "Set terminal line parameters\n"
2875 "Copy debug output to the current terminal line\n")
2881 DEFUN_NOSH (terminal_no_monitor
,
2882 terminal_no_monitor_cmd
,
2883 "terminal no monitor",
2884 "Set terminal line parameters\n"
2886 "Copy debug output to the current terminal line\n")
2892 DEFUN_NOSH (no_terminal_monitor
,
2893 no_terminal_monitor_cmd
,
2894 "no terminal monitor",
2896 "Set terminal line parameters\n"
2897 "Copy debug output to the current terminal line\n")
2899 return terminal_no_monitor(self
, vty
, argc
, argv
);
2903 DEFUN_NOSH (show_history
,
2907 "Display the session command history\n")
2911 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2912 if (index
== VTY_MAXHIST
) {
2917 if (vty
->hist
[index
] != NULL
)
2918 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2927 DEFUN (log_commands
,
2931 "Log all commands (can't be unset without restart)\n")
2933 do_log_commands
= 1;
2937 /* Display current configuration. */
2938 static int vty_config_write(struct vty
*vty
)
2940 vty_out(vty
, "line vty\n");
2942 if (vty_accesslist_name
)
2943 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2945 if (vty_ipv6_accesslist_name
)
2946 vty_out(vty
, " ipv6 access-class %s\n",
2947 vty_ipv6_accesslist_name
);
2950 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2951 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2952 vty_timeout_val
% 60);
2955 if (no_password_check
)
2956 vty_out(vty
, " no login\n");
2958 if (do_log_commands
)
2959 vty_out(vty
, "log commands\n");
2961 vty_out(vty
, "!\n");
2966 struct cmd_node vty_node
= {
2967 VTY_NODE
, "%s(config-line)# ", 1,
2970 /* Reset all VTY status. */
2975 struct thread
*vty_serv_thread
;
2977 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2978 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2979 buffer_reset(vty
->obuf
);
2980 vty
->status
= VTY_CLOSE
;
2984 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
2985 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
2987 thread_cancel(vty_serv_thread
);
2988 vector_slot(Vvty_serv_thread
, i
) = NULL
;
2992 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2994 if (vty_accesslist_name
) {
2995 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2996 vty_accesslist_name
= NULL
;
2999 if (vty_ipv6_accesslist_name
) {
3000 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3001 vty_ipv6_accesslist_name
= NULL
;
3005 static void vty_save_cwd(void)
3007 char cwd
[MAXPATHLEN
];
3010 c
= getcwd(cwd
, MAXPATHLEN
);
3014 * At this point if these go wrong, more than likely
3015 * the whole world is coming down around us
3016 * Hence not worrying about it too much.
3018 if (!chdir(SYSCONFDIR
)) {
3019 zlog_err("Failure to chdir to %s, errno: %d",
3023 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
3024 zlog_err("Failure to getcwd, errno: %d", errno
);
3029 vty_cwd
= XMALLOC(MTYPE_TMP
, strlen(cwd
) + 1);
3030 strcpy(vty_cwd
, cwd
);
3038 int vty_shell(struct vty
*vty
)
3040 return vty
->type
== VTY_SHELL
? 1 : 0;
3043 int vty_shell_serv(struct vty
*vty
)
3045 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3048 void vty_init_vtysh()
3050 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3053 /* Install vty's own commands like `who' command. */
3054 void vty_init(struct thread_master
*master_thread
)
3056 /* For further configuration read, preserve current directory. */
3059 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3061 vty_master
= master_thread
;
3063 atexit(vty_stdio_atexit
);
3065 /* Initilize server thread vector. */
3066 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3068 /* Install bgp top node. */
3069 install_node(&vty_node
, vty_config_write
);
3071 install_element(VIEW_NODE
, &config_who_cmd
);
3072 install_element(VIEW_NODE
, &show_history_cmd
);
3073 install_element(CONFIG_NODE
, &line_vty_cmd
);
3074 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3075 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3076 install_element(CONFIG_NODE
, &show_history_cmd
);
3077 install_element(CONFIG_NODE
, &log_commands_cmd
);
3078 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3079 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3080 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3082 install_default(VTY_NODE
);
3083 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3084 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3085 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3086 install_element(VTY_NODE
, &vty_access_class_cmd
);
3087 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3088 install_element(VTY_NODE
, &vty_login_cmd
);
3089 install_element(VTY_NODE
, &no_vty_login_cmd
);
3090 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3091 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3094 void vty_terminate(void)
3097 XFREE(MTYPE_TMP
, vty_cwd
);
3099 if (vtyvec
&& Vvty_serv_thread
) {
3101 vector_free(vtyvec
);
3102 vector_free(Vvty_serv_thread
);
3104 Vvty_serv_thread
= NULL
;