2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lib/version.h>
25 #include <sys/types.h>
33 #include "sockunion.h"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
47 #include <arpa/telnet.h>
50 #ifndef VTYSH_EXTRACT_PL
51 #include "lib/vty_clippy.c"
54 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
55 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
56 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
71 static void vty_event(enum event
, int, struct vty
*);
73 /* Extern host structure from command.c */
74 extern struct host host
;
76 /* Vector which store each vty structure. */
79 /* Vty timeout value. */
80 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
82 /* Vty access-class command */
83 static char *vty_accesslist_name
= NULL
;
85 /* Vty access-calss for IPv6. */
86 static char *vty_ipv6_accesslist_name
= NULL
;
88 /* VTY server thread. */
89 static vector Vvty_serv_thread
;
91 /* Current directory. */
92 char vty_cwd
[MAXPATHLEN
];
94 /* Login password check. */
95 static int no_password_check
= 0;
97 /* Integrated configuration file path */
98 static char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
100 static bool do_log_commands
;
101 static bool do_log_commands_perm
;
103 void vty_frame(struct vty
*vty
, const char *format
, ...)
107 va_start(args
, format
);
108 vsnprintfrr(vty
->frame
+ vty
->frame_pos
,
109 sizeof(vty
->frame
) - vty
->frame_pos
, format
, args
);
110 vty
->frame_pos
= strlen(vty
->frame
);
114 void vty_endframe(struct vty
*vty
, const char *endtext
)
116 if (vty
->frame_pos
== 0 && endtext
)
117 vty_out(vty
, "%s", endtext
);
121 bool vty_set_include(struct vty
*vty
, const char *regexp
)
129 regfree(&vty
->include
);
135 errcode
= regcomp(&vty
->include
, regexp
,
136 REG_EXTENDED
| REG_NEWLINE
| REG_NOSUB
);
139 regerror(ret
, &vty
->include
, errbuf
, sizeof(errbuf
));
140 vty_out(vty
, "%% Regex compilation error: %s", errbuf
);
148 /* VTY standard output function. */
149 int vty_out(struct vty
*vty
, const char *format
, ...)
157 if (vty
->frame_pos
) {
159 vty_out(vty
, "%s", vty
->frame
);
162 va_start(args
, format
);
163 p
= vasnprintfrr(MTYPE_VTY_OUT_BUF
, buf
, sizeof(buf
), format
, args
);
170 vector lines
= frrstr_split_vec(p
, "\n");
172 /* Place first value in the cache */
173 char *firstline
= vector_slot(lines
, 0);
174 buffer_put(vty
->lbuf
, (uint8_t *) firstline
, strlen(firstline
));
176 /* If our split returned more than one entry, time to filter */
177 if (vector_active(lines
) > 1) {
179 * returned string is MTYPE_TMP so it matches the MTYPE
180 * of everything else in the vector
182 char *bstr
= buffer_getstr(vty
->lbuf
);
183 buffer_reset(vty
->lbuf
);
184 XFREE(MTYPE_TMP
, lines
->index
[0]);
185 vector_set_index(lines
, 0, bstr
);
186 frrstr_filter_vec(lines
, &vty
->include
);
187 vector_compact(lines
);
189 * Consider the string "foo\n". If the regex is an empty string
190 * and the line ended with a newline, then the vector will look
196 * If the regex isn't empty, the vector will look like:
200 * In this case we'd like to preserve the newline, so we add
201 * the empty string [1] as in the first example.
203 if (p
[strlen(p
) - 1] == '\n' && vector_active(lines
) > 0
204 && strlen(vector_slot(lines
, vector_active(lines
) - 1)))
205 vector_set(lines
, XSTRDUP(MTYPE_TMP
, ""));
207 filtered
= frrstr_join_vec(lines
, "\n");
213 frrstr_strvec_free(lines
);
224 /* print with crlf replacement */
225 buffer_put_crlf(vty
->obuf
, (uint8_t *)filtered
,
229 fprintf(vty
->of
, "%s", filtered
);
235 /* print without crlf replacement */
236 buffer_put(vty
->obuf
, (uint8_t *)filtered
, strlen(filtered
));
242 if (vty
->filter
&& filtered
)
243 XFREE(MTYPE_TMP
, filtered
);
245 /* If p is not different with buf, it is allocated buffer. */
247 XFREE(MTYPE_VTY_OUT_BUF
, p
);
252 static int vty_log_out(struct vty
*vty
, const char *level
,
253 const char *proto_str
, const char *msg
,
254 struct timestamp_control
*ctl
)
260 if (!ctl
->already_rendered
) {
261 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
263 ctl
->already_rendered
= 1;
265 if (ctl
->len
+ 1 >= sizeof(buf
))
267 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
272 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: ", level
,
275 ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: ", proto_str
);
276 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
279 if (((ret
= snprintf(buf
+ len
, sizeof(buf
) - len
, "%s", msg
)) < 0)
280 || ((size_t)((len
+= ret
) + 2) > sizeof(buf
)))
286 if (write(vty
->wfd
, buf
, len
) < 0) {
287 if (ERRNO_IO_RETRY(errno
))
288 /* Kernel buffer is full, probably too much debugging
290 drop the data and ignore. */
292 /* Fatal I/O error. */
294 0; /* disable monitoring to avoid infinite recursion */
295 flog_err(EC_LIB_SOCKET
,
296 "%s: write failed to vty client fd %d, closing: %s",
297 __func__
, vty
->fd
, safe_strerror(errno
));
298 buffer_reset(vty
->obuf
);
299 buffer_reset(vty
->lbuf
);
300 /* cannot call vty_close, because a parent routine may still try
301 to access the vty struct */
302 vty
->status
= VTY_CLOSE
;
303 shutdown(vty
->fd
, SHUT_RDWR
);
309 /* Output current time to the vty. */
310 void vty_time_print(struct vty
*vty
, int cr
)
312 char buf
[QUAGGA_TIMESTAMP_LEN
];
314 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0) {
315 zlog_info("quagga_timestamp error");
319 vty_out(vty
, "%s\n", buf
);
321 vty_out(vty
, "%s ", buf
);
326 /* Say hello to vty interface. */
327 void vty_hello(struct vty
*vty
)
333 f
= fopen(host
.motdfile
, "r");
335 while (fgets(buf
, sizeof(buf
), f
)) {
337 /* work backwards to ignore trailling isspace()
339 for (s
= buf
+ strlen(buf
);
340 (s
> buf
) && isspace((int)*(s
- 1)); s
--)
343 vty_out(vty
, "%s\n", buf
);
347 vty_out(vty
, "MOTD file not found\n");
348 } else if (host
.motd
)
349 vty_out(vty
, "%s", host
.motd
);
352 /* Put out prompt and wait input from user. */
353 static void vty_prompt(struct vty
*vty
)
355 if (vty
->type
== VTY_TERM
) {
356 vty_out(vty
, cmd_prompt(vty
->node
), cmd_hostname_get());
360 /* Send WILL TELOPT_ECHO to remote server. */
361 static void vty_will_echo(struct vty
*vty
)
363 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_ECHO
, '\0'};
364 vty_out(vty
, "%s", cmd
);
367 /* Make suppress Go-Ahead telnet option. */
368 static void vty_will_suppress_go_ahead(struct vty
*vty
)
370 unsigned char cmd
[] = {IAC
, WILL
, TELOPT_SGA
, '\0'};
371 vty_out(vty
, "%s", cmd
);
374 /* Make don't use linemode over telnet. */
375 static void vty_dont_linemode(struct vty
*vty
)
377 unsigned char cmd
[] = {IAC
, DONT
, TELOPT_LINEMODE
, '\0'};
378 vty_out(vty
, "%s", cmd
);
381 /* Use window size. */
382 static void vty_do_window_size(struct vty
*vty
)
384 unsigned char cmd
[] = {IAC
, DO
, TELOPT_NAWS
, '\0'};
385 vty_out(vty
, "%s", cmd
);
388 #if 0 /* Currently not used. */
389 /* Make don't use lflow vty interface. */
391 vty_dont_lflow_ahead (struct vty
*vty
)
393 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
394 vty_out (vty
, "%s", cmd
);
398 /* Authentication of vty */
399 static void vty_auth(struct vty
*vty
, char *buf
)
402 enum node_type next_node
= 0;
404 char *crypt(const char *, const char *);
409 passwd
= host
.password_encrypt
;
411 passwd
= host
.password
;
413 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
415 next_node
= VIEW_NODE
;
417 case AUTH_ENABLE_NODE
:
419 passwd
= host
.enable_encrypt
;
421 passwd
= host
.enable
;
422 next_node
= ENABLE_NODE
;
428 fail
= strcmp(crypt(buf
, passwd
), passwd
);
430 fail
= strcmp(buf
, passwd
);
436 vty
->node
= next_node
; /* Success ! */
439 if (vty
->fail
>= 3) {
440 if (vty
->node
== AUTH_NODE
) {
442 "%% Bad passwords, too many failures!\n");
443 vty
->status
= VTY_CLOSE
;
445 /* AUTH_ENABLE_NODE */
448 "%% Bad enable passwords, too many failures!\n");
449 vty
->status
= VTY_CLOSE
;
455 /* Command execution over the vty interface. */
456 static int vty_command(struct vty
*vty
, char *buf
)
459 const char *protocolname
;
465 * Log non empty command lines
470 /* Skip white spaces. */
471 while (isspace((int)*cp
) && *cp
!= '\0')
474 if (cp
!= NULL
&& *cp
!= '\0') {
476 char vty_str
[VTY_BUFSIZ
];
477 char prompt_str
[VTY_BUFSIZ
];
479 /* format the base vty info */
480 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
482 for (i
= 0; i
< vector_active(vtyvec
); i
++)
483 if (vty
== vector_slot(vtyvec
, i
)) {
484 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
489 /* format the prompt */
490 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt(vty
->node
),
493 /* now log the command */
494 zlog_notice("%s%s", prompt_str
, buf
);
497 #ifdef CONSUMED_TIME_CHECK
501 unsigned long realtime
, cputime
;
504 #endif /* CONSUMED_TIME_CHECK */
506 ret
= cmd_execute(vty
, buf
, NULL
, 0);
508 /* Get the name of the protocol if any */
509 protocolname
= frr_protoname
;
511 #ifdef CONSUMED_TIME_CHECK
513 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
))
514 > CONSUMED_TIME_CHECK
)
515 /* Warn about CPU hog that must be fixed. */
518 "SLOW COMMAND: command took %lums (cpu time %lums): %s",
519 realtime
/ 1000, cputime
/ 1000, buf
);
521 #endif /* CONSUMED_TIME_CHECK */
523 if (ret
!= CMD_SUCCESS
)
526 if (vty
->type
== VTY_FILE
)
527 vty_out(vty
, "Warning...\n");
529 case CMD_ERR_AMBIGUOUS
:
530 vty_out(vty
, "%% Ambiguous command.\n");
532 case CMD_ERR_NO_MATCH
:
533 vty_out(vty
, "%% [%s] Unknown command: %s\n",
536 case CMD_ERR_INCOMPLETE
:
537 vty_out(vty
, "%% Command incomplete.\n");
544 static const char telnet_backward_char
= 0x08;
545 static const char telnet_space_char
= ' ';
547 /* Basic function to write buffer to vty. */
548 static void vty_write(struct vty
*vty
, const char *buf
, size_t nbytes
)
550 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
553 /* Should we do buffering here ? And make vty_flush (vty) ? */
554 buffer_put(vty
->obuf
, buf
, nbytes
);
557 /* Basic function to insert character into vty. */
558 static void vty_self_insert(struct vty
*vty
, char c
)
563 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
566 length
= vty
->length
- vty
->cp
;
567 memmove(&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
568 vty
->buf
[vty
->cp
] = c
;
570 vty_write(vty
, &vty
->buf
[vty
->cp
], length
+ 1);
571 for (i
= 0; i
< length
; i
++)
572 vty_write(vty
, &telnet_backward_char
, 1);
577 vty
->buf
[vty
->length
] = '\0';
580 /* Self insert character 'c' in overwrite mode. */
581 static void vty_self_insert_overwrite(struct vty
*vty
, char c
)
583 if (vty
->cp
== vty
->length
) {
584 vty_self_insert(vty
, c
);
588 vty
->buf
[vty
->cp
++] = c
;
589 vty_write(vty
, &c
, 1);
593 * Insert a string into vty->buf at the current cursor position.
595 * If the resultant string would be larger than VTY_BUFSIZ it is
598 static void vty_insert_word_overwrite(struct vty
*vty
, char *str
)
600 if (vty
->cp
== VTY_BUFSIZ
)
603 size_t nwrite
= MIN((int)strlen(str
), VTY_BUFSIZ
- vty
->cp
- 1);
604 memcpy(&vty
->buf
[vty
->cp
], str
, nwrite
);
606 vty
->length
= MAX(vty
->cp
, vty
->length
);
607 vty
->buf
[vty
->length
] = '\0';
608 vty_write(vty
, str
, nwrite
);
611 /* Forward character. */
612 static void vty_forward_char(struct vty
*vty
)
614 if (vty
->cp
< vty
->length
) {
615 vty_write(vty
, &vty
->buf
[vty
->cp
], 1);
620 /* Backward character. */
621 static void vty_backward_char(struct vty
*vty
)
625 vty_write(vty
, &telnet_backward_char
, 1);
629 /* Move to the beginning of the line. */
630 static void vty_beginning_of_line(struct vty
*vty
)
633 vty_backward_char(vty
);
636 /* Move to the end of the line. */
637 static void vty_end_of_line(struct vty
*vty
)
639 while (vty
->cp
< vty
->length
)
640 vty_forward_char(vty
);
643 static void vty_kill_line_from_beginning(struct vty
*);
644 static void vty_redraw_line(struct vty
*);
646 /* Print command line history. This function is called from
647 vty_next_line and vty_previous_line. */
648 static void vty_history_print(struct vty
*vty
)
652 vty_kill_line_from_beginning(vty
);
654 /* Get previous line from history buffer */
655 length
= strlen(vty
->hist
[vty
->hp
]);
656 memcpy(vty
->buf
, vty
->hist
[vty
->hp
], length
);
657 vty
->cp
= vty
->length
= length
;
658 vty
->buf
[vty
->length
] = '\0';
660 /* Redraw current line */
661 vty_redraw_line(vty
);
664 /* Show next command line history. */
665 static void vty_next_line(struct vty
*vty
)
669 if (vty
->hp
== vty
->hindex
)
672 /* Try is there history exist or not. */
674 if (try_index
== (VTY_MAXHIST
- 1))
679 /* If there is not history return. */
680 if (vty
->hist
[try_index
] == NULL
)
685 vty_history_print(vty
);
688 /* Show previous command line history. */
689 static void vty_previous_line(struct vty
*vty
)
695 try_index
= VTY_MAXHIST
- 1;
699 if (vty
->hist
[try_index
] == NULL
)
704 vty_history_print(vty
);
707 /* This function redraw all of the command line character. */
708 static void vty_redraw_line(struct vty
*vty
)
710 vty_write(vty
, vty
->buf
, vty
->length
);
711 vty
->cp
= vty
->length
;
715 static void vty_forward_word(struct vty
*vty
)
717 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
718 vty_forward_char(vty
);
720 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
721 vty_forward_char(vty
);
724 /* Backward word without skipping training space. */
725 static void vty_backward_pure_word(struct vty
*vty
)
727 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
728 vty_backward_char(vty
);
732 static void vty_backward_word(struct vty
*vty
)
734 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
735 vty_backward_char(vty
);
737 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
738 vty_backward_char(vty
);
741 /* When '^D' is typed at the beginning of the line we move to the down
743 static void vty_down_level(struct vty
*vty
)
751 /* When '^Z' is received from vty, move down to the enable mode. */
752 static void vty_end_config(struct vty
*vty
)
757 vty_config_exit(vty
);
758 vty
->node
= ENABLE_NODE
;
765 /* Delete a charcter at the current point. */
766 static void vty_delete_char(struct vty
*vty
)
771 if (vty
->length
== 0) {
776 if (vty
->cp
== vty
->length
)
777 return; /* completion need here? */
779 size
= vty
->length
- vty
->cp
;
782 memmove(&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
783 vty
->buf
[vty
->length
] = '\0';
785 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
788 vty_write(vty
, &vty
->buf
[vty
->cp
], size
- 1);
789 vty_write(vty
, &telnet_space_char
, 1);
791 for (i
= 0; i
< size
; i
++)
792 vty_write(vty
, &telnet_backward_char
, 1);
795 /* Delete a character before the point. */
796 static void vty_delete_backward_char(struct vty
*vty
)
801 vty_backward_char(vty
);
802 vty_delete_char(vty
);
805 /* Kill rest of line from current point. */
806 static void vty_kill_line(struct vty
*vty
)
811 size
= vty
->length
- vty
->cp
;
816 for (i
= 0; i
< size
; i
++)
817 vty_write(vty
, &telnet_space_char
, 1);
818 for (i
= 0; i
< size
; i
++)
819 vty_write(vty
, &telnet_backward_char
, 1);
821 memset(&vty
->buf
[vty
->cp
], 0, size
);
822 vty
->length
= vty
->cp
;
825 /* Kill line from the beginning. */
826 static void vty_kill_line_from_beginning(struct vty
*vty
)
828 vty_beginning_of_line(vty
);
832 /* Delete a word before the point. */
833 static void vty_forward_kill_word(struct vty
*vty
)
835 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
836 vty_delete_char(vty
);
837 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
838 vty_delete_char(vty
);
841 /* Delete a word before the point. */
842 static void vty_backward_kill_word(struct vty
*vty
)
844 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
845 vty_delete_backward_char(vty
);
846 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
847 vty_delete_backward_char(vty
);
850 /* Transpose chars before or at the point. */
851 static void vty_transpose_chars(struct vty
*vty
)
855 /* If length is short or point is near by the beginning of line then
857 if (vty
->length
< 2 || vty
->cp
< 1)
860 /* In case of point is located at the end of the line. */
861 if (vty
->cp
== vty
->length
) {
862 c1
= vty
->buf
[vty
->cp
- 1];
863 c2
= vty
->buf
[vty
->cp
- 2];
865 vty_backward_char(vty
);
866 vty_backward_char(vty
);
867 vty_self_insert_overwrite(vty
, c1
);
868 vty_self_insert_overwrite(vty
, c2
);
870 c1
= vty
->buf
[vty
->cp
];
871 c2
= vty
->buf
[vty
->cp
- 1];
873 vty_backward_char(vty
);
874 vty_self_insert_overwrite(vty
, c1
);
875 vty_self_insert_overwrite(vty
, c2
);
879 /* Do completion at vty interface. */
880 static void vty_complete_command(struct vty
*vty
)
884 char **matched
= NULL
;
887 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
890 vline
= cmd_make_strvec(vty
->buf
);
894 /* In case of 'help \t'. */
895 if (isspace((int)vty
->buf
[vty
->length
- 1]))
896 vector_set(vline
, NULL
);
898 matched
= cmd_complete_command(vline
, vty
, &ret
);
900 cmd_free_strvec(vline
);
904 case CMD_ERR_AMBIGUOUS
:
905 vty_out(vty
, "%% Ambiguous command.\n");
907 vty_redraw_line(vty
);
909 case CMD_ERR_NO_MATCH
:
910 /* vty_out (vty, "%% There is no matched command.\n"); */
912 vty_redraw_line(vty
);
914 case CMD_COMPLETE_FULL_MATCH
:
916 /* 2016-11-28 equinox -- need to debug, SEGV here */
917 vty_out(vty
, "%% CLI BUG: FULL_MATCH with NULL str\n");
919 vty_redraw_line(vty
);
923 vty_redraw_line(vty
);
924 vty_backward_pure_word(vty
);
925 vty_insert_word_overwrite(vty
, matched
[0]);
926 vty_self_insert(vty
, ' ');
927 XFREE(MTYPE_COMPLETION
, matched
[0]);
929 case CMD_COMPLETE_MATCH
:
931 vty_redraw_line(vty
);
932 vty_backward_pure_word(vty
);
933 vty_insert_word_overwrite(vty
, matched
[0]);
934 XFREE(MTYPE_COMPLETION
, matched
[0]);
936 case CMD_COMPLETE_LIST_MATCH
:
937 for (i
= 0; matched
[i
] != NULL
; i
++) {
938 if (i
!= 0 && ((i
% 6) == 0))
940 vty_out(vty
, "%-10s ", matched
[i
]);
941 XFREE(MTYPE_COMPLETION
, matched
[i
]);
946 vty_redraw_line(vty
);
948 case CMD_ERR_NOTHING_TODO
:
950 vty_redraw_line(vty
);
955 XFREE(MTYPE_TMP
, matched
);
958 static void vty_describe_fold(struct vty
*vty
, int cmd_width
,
959 unsigned int desc_width
, struct cmd_token
*token
)
967 if (desc_width
<= 0) {
968 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, token
->desc
);
972 buf
= XCALLOC(MTYPE_TMP
, strlen(token
->desc
) + 1);
974 for (p
= token
->desc
; strlen(p
) > desc_width
; p
+= pos
+ 1) {
975 for (pos
= desc_width
; pos
> 0; pos
--)
976 if (*(p
+ pos
) == ' ')
984 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, buf
);
989 vty_out(vty
, " %-*s %s\n", cmd_width
, cmd
, p
);
991 XFREE(MTYPE_TMP
, buf
);
994 /* Describe matched command function. */
995 static void vty_describe_command(struct vty
*vty
)
1000 unsigned int i
, width
, desc_width
;
1001 struct cmd_token
*token
, *token_cr
= NULL
;
1003 vline
= cmd_make_strvec(vty
->buf
);
1005 /* In case of '> ?'. */
1006 if (vline
== NULL
) {
1007 vline
= vector_init(1);
1008 vector_set(vline
, NULL
);
1009 } else if (isspace((int)vty
->buf
[vty
->length
- 1]))
1010 vector_set(vline
, NULL
);
1012 describe
= cmd_describe_command(vline
, vty
, &ret
);
1016 /* Ambiguous error. */
1018 case CMD_ERR_AMBIGUOUS
:
1019 vty_out(vty
, "%% Ambiguous command.\n");
1022 case CMD_ERR_NO_MATCH
:
1023 vty_out(vty
, "%% There is no matched command.\n");
1028 /* Get width of command string. */
1030 for (i
= 0; i
< vector_active(describe
); i
++)
1031 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1034 if (token
->text
[0] == '\0')
1037 len
= strlen(token
->text
);
1043 /* Get width of description string. */
1044 desc_width
= vty
->width
- (width
+ 6);
1046 /* Print out description. */
1047 for (i
= 0; i
< vector_active(describe
); i
++)
1048 if ((token
= vector_slot(describe
, i
)) != NULL
) {
1049 if (token
->text
[0] == '\0')
1052 if (strcmp(token
->text
, CMD_CR_TEXT
) == 0) {
1058 vty_out(vty
, " %-s\n", token
->text
);
1059 else if (desc_width
>= strlen(token
->desc
))
1060 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1063 vty_describe_fold(vty
, width
, desc_width
,
1066 if (IS_VARYING_TOKEN(token
->type
)) {
1067 const char *ref
= vector_slot(
1068 vline
, vector_active(vline
) - 1);
1070 vector varcomps
= vector_init(VECTOR_MIN_SIZE
);
1071 cmd_variable_complete(token
, ref
, varcomps
);
1073 if (vector_active(varcomps
) > 0) {
1074 char *ac
= cmd_variable_comp2str(
1075 varcomps
, vty
->width
);
1076 vty_out(vty
, "%s\n", ac
);
1077 XFREE(MTYPE_TMP
, ac
);
1080 vector_free(varcomps
);
1083 vty_out (vty
, " %-*s %s\n", width
1084 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1085 desc
->str
? desc
->str
: "");
1089 if ((token
= token_cr
)) {
1091 vty_out(vty
, " %-s\n", token
->text
);
1092 else if (desc_width
>= strlen(token
->desc
))
1093 vty_out(vty
, " %-*s %s\n", width
, token
->text
,
1096 vty_describe_fold(vty
, width
, desc_width
, token
);
1100 cmd_free_strvec(vline
);
1102 vector_free(describe
);
1105 vty_redraw_line(vty
);
1108 static void vty_clear_buf(struct vty
*vty
)
1110 memset(vty
->buf
, 0, vty
->max
);
1113 /* ^C stop current input and do not add command line to the history. */
1114 static void vty_stop_input(struct vty
*vty
)
1116 vty
->cp
= vty
->length
= 0;
1121 vty_config_exit(vty
);
1122 vty
->node
= ENABLE_NODE
;
1127 /* Set history pointer to the latest one. */
1128 vty
->hp
= vty
->hindex
;
1131 /* Add current command line to the history buffer. */
1132 static void vty_hist_add(struct vty
*vty
)
1136 if (vty
->length
== 0)
1139 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1141 /* Ignore the same string as previous one. */
1142 if (vty
->hist
[index
])
1143 if (strcmp(vty
->buf
, vty
->hist
[index
]) == 0) {
1144 vty
->hp
= vty
->hindex
;
1148 /* Insert history entry. */
1149 XFREE(MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1150 vty
->hist
[vty
->hindex
] = XSTRDUP(MTYPE_VTY_HIST
, vty
->buf
);
1152 /* History index rotation. */
1154 if (vty
->hindex
== VTY_MAXHIST
)
1157 vty
->hp
= vty
->hindex
;
1160 /* #define TELNET_OPTION_DEBUG */
1162 /* Get telnet window size. */
1163 static int vty_telnet_option(struct vty
*vty
, unsigned char *buf
, int nbytes
)
1165 #ifdef TELNET_OPTION_DEBUG
1168 for (i
= 0; i
< nbytes
; i
++) {
1171 vty_out(vty
, "IAC ");
1174 vty_out(vty
, "WILL ");
1177 vty_out(vty
, "WONT ");
1180 vty_out(vty
, "DO ");
1183 vty_out(vty
, "DONT ");
1186 vty_out(vty
, "SB ");
1189 vty_out(vty
, "SE ");
1192 vty_out(vty
, "TELOPT_ECHO \n");
1195 vty_out(vty
, "TELOPT_SGA \n");
1198 vty_out(vty
, "TELOPT_NAWS \n");
1201 vty_out(vty
, "%x ", buf
[i
]);
1207 #endif /* TELNET_OPTION_DEBUG */
1212 vty
->iac_sb_in_progress
= 1;
1216 if (!vty
->iac_sb_in_progress
)
1219 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0')) {
1220 vty
->iac_sb_in_progress
= 0;
1223 switch (vty
->sb_buf
[0]) {
1225 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1228 "RFC 1073 violation detected: telnet NAWS option "
1229 "should send %d characters, but we received %lu",
1231 (unsigned long)vty
->sb_len
);
1232 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1235 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
1236 (unsigned long)sizeof(vty
->sb_buf
),
1237 TELNET_NAWS_SB_LEN
);
1239 vty
->width
= ((vty
->sb_buf
[1] << 8)
1241 vty
->height
= ((vty
->sb_buf
[3] << 8)
1243 #ifdef TELNET_OPTION_DEBUG
1245 "TELNET NAWS window size negotiation completed: "
1246 "width %d, height %d\n",
1247 vty
->width
, vty
->height
);
1252 vty
->iac_sb_in_progress
= 0;
1262 /* Execute current command line. */
1263 static int vty_execute(struct vty
*vty
)
1269 switch (vty
->node
) {
1271 case AUTH_ENABLE_NODE
:
1272 vty_auth(vty
, vty
->buf
);
1275 ret
= vty_command(vty
, vty
->buf
);
1276 if (vty
->type
== VTY_TERM
)
1281 /* Clear command line buffer. */
1282 vty
->cp
= vty
->length
= 0;
1285 if (vty
->status
!= VTY_CLOSE
)
1291 #define CONTROL(X) ((X) - '@')
1292 #define VTY_NORMAL 0
1293 #define VTY_PRE_ESCAPE 1
1294 #define VTY_ESCAPE 2
1296 /* Escape character command map. */
1297 static void vty_escape_map(unsigned char c
, struct vty
*vty
)
1301 vty_previous_line(vty
);
1307 vty_forward_char(vty
);
1310 vty_backward_char(vty
);
1316 /* Go back to normal mode. */
1317 vty
->escape
= VTY_NORMAL
;
1320 /* Quit print out to the buffer. */
1321 static void vty_buffer_reset(struct vty
*vty
)
1323 buffer_reset(vty
->obuf
);
1324 buffer_reset(vty
->lbuf
);
1326 vty_redraw_line(vty
);
1329 /* Read data via vty socket. */
1330 static int vty_read(struct thread
*thread
)
1334 unsigned char buf
[VTY_READ_BUFSIZ
];
1336 int vty_sock
= THREAD_FD(thread
);
1337 struct vty
*vty
= THREAD_ARG(thread
);
1340 /* Read raw data from socket */
1341 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1343 if (ERRNO_IO_RETRY(errno
)) {
1344 vty_event(VTY_READ
, vty_sock
, vty
);
1347 vty
->monitor
= 0; /* disable monitoring to avoid
1348 infinite recursion */
1351 "%s: read error on vty client fd %d, closing: %s",
1352 __func__
, vty
->fd
, safe_strerror(errno
));
1353 buffer_reset(vty
->obuf
);
1354 buffer_reset(vty
->lbuf
);
1356 vty
->status
= VTY_CLOSE
;
1359 for (i
= 0; i
< nbytes
; i
++) {
1360 if (buf
[i
] == IAC
) {
1369 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1370 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1371 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1377 /* In case of telnet command */
1379 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1386 if (vty
->status
== VTY_MORE
) {
1391 vty_buffer_reset(vty
);
1393 #if 0 /* More line does not work for "show ip bgp". */
1396 vty
->status
= VTY_MORELINE
;
1405 /* Escape character. */
1406 if (vty
->escape
== VTY_ESCAPE
) {
1407 vty_escape_map(buf
[i
], vty
);
1411 /* Pre-escape status. */
1412 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1415 vty
->escape
= VTY_ESCAPE
;
1418 vty_backward_word(vty
);
1419 vty
->escape
= VTY_NORMAL
;
1422 vty_forward_word(vty
);
1423 vty
->escape
= VTY_NORMAL
;
1426 vty_forward_kill_word(vty
);
1427 vty
->escape
= VTY_NORMAL
;
1431 vty_backward_kill_word(vty
);
1432 vty
->escape
= VTY_NORMAL
;
1435 vty
->escape
= VTY_NORMAL
;
1443 vty_beginning_of_line(vty
);
1446 vty_backward_char(vty
);
1449 vty_stop_input(vty
);
1452 vty_delete_char(vty
);
1455 vty_end_of_line(vty
);
1458 vty_forward_char(vty
);
1462 vty_delete_backward_char(vty
);
1471 vty_previous_line(vty
);
1474 vty_transpose_chars(vty
);
1477 vty_kill_line_from_beginning(vty
);
1480 vty_backward_kill_word(vty
);
1483 vty_end_config(vty
);
1491 vty_complete_command(vty
);
1494 if (vty
->node
== AUTH_NODE
1495 || vty
->node
== AUTH_ENABLE_NODE
)
1496 vty_self_insert(vty
, buf
[i
]);
1498 vty_describe_command(vty
);
1501 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1502 vty
->escape
= VTY_ESCAPE
;
1505 vty
->escape
= VTY_PRE_ESCAPE
;
1508 if (buf
[i
] > 31 && buf
[i
] < 127)
1509 vty_self_insert(vty
, buf
[i
]);
1515 if (vty
->status
== VTY_CLOSE
)
1518 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1519 vty_event(VTY_READ
, vty_sock
, vty
);
1524 /* Flush buffer to the vty. */
1525 static int vty_flush(struct thread
*thread
)
1528 buffer_status_t flushrc
;
1529 int vty_sock
= THREAD_FD(thread
);
1530 struct vty
*vty
= THREAD_ARG(thread
);
1532 vty
->t_write
= NULL
;
1534 /* Tempolary disable read thread. */
1535 if ((vty
->lines
== 0) && vty
->t_read
) {
1536 thread_cancel(vty
->t_read
);
1540 /* Function execution continue. */
1541 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1543 /* N.B. if width is 0, that means we don't know the window size. */
1544 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1545 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1546 else if (vty
->status
== VTY_MORELINE
)
1547 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1550 flushrc
= buffer_flush_window(
1551 vty
->obuf
, vty_sock
, vty
->width
,
1552 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1556 0; /* disable monitoring to avoid infinite recursion */
1557 zlog_info("buffer_flush failed on vty client fd %d, closing",
1559 buffer_reset(vty
->lbuf
);
1560 buffer_reset(vty
->obuf
);
1564 if (vty
->status
== VTY_CLOSE
)
1567 vty
->status
= VTY_NORMAL
;
1568 if (vty
->lines
== 0)
1569 vty_event(VTY_READ
, vty_sock
, vty
);
1572 case BUFFER_PENDING
:
1573 /* There is more data waiting to be written. */
1574 vty
->status
= VTY_MORE
;
1575 if (vty
->lines
== 0)
1576 vty_event(VTY_WRITE
, vty_sock
, vty
);
1583 /* Allocate new vty struct. */
1584 struct vty
*vty_new(void)
1586 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1588 new->fd
= new->wfd
= -1;
1590 new->lbuf
= buffer_new(0);
1591 new->obuf
= buffer_new(0); /* Use default buffer size. */
1592 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1593 new->max
= VTY_BUFSIZ
;
1599 /* allocate and initialise vty */
1600 static struct vty
*vty_new_init(int vty_sock
)
1606 vty
->wfd
= vty_sock
;
1607 vty
->type
= VTY_TERM
;
1608 vty
->node
= AUTH_NODE
;
1613 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1616 vty
->xpath_index
= 0;
1617 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1618 vty
->private_config
= false;
1619 vty
->candidate_config
= vty_shared_candidate_config
;
1620 vector_set_index(vtyvec
, vty_sock
, vty
);
1621 vty
->status
= VTY_NORMAL
;
1624 vty
->iac_sb_in_progress
= 0;
1630 /* Create new vty structure. */
1631 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1633 char buf
[SU_ADDRSTRLEN
];
1636 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1638 /* Allocate new vty structure and set up default values. */
1639 vty
= vty_new_init(vty_sock
);
1641 /* configurable parameters not part of basic init */
1642 vty
->v_timeout
= vty_timeout_val
;
1643 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1644 if (no_password_check
) {
1646 vty
->node
= ENABLE_NODE
;
1648 vty
->node
= VIEW_NODE
;
1650 if (host
.lines
>= 0)
1651 vty
->lines
= host
.lines
;
1653 if (!no_password_check
) {
1654 /* Vty is not available if password isn't set. */
1655 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1656 vty_out(vty
, "Vty password is not set.\n");
1657 vty
->status
= VTY_CLOSE
;
1663 /* Say hello to the world. */
1665 if (!no_password_check
)
1666 vty_out(vty
, "\nUser Access Verification\n\n");
1668 /* Setting up terminal. */
1670 vty_will_suppress_go_ahead(vty
);
1672 vty_dont_linemode(vty
);
1673 vty_do_window_size(vty
);
1674 /* vty_dont_lflow_ahead (vty); */
1678 /* Add read/write thread. */
1679 vty_event(VTY_WRITE
, vty_sock
, vty
);
1680 vty_event(VTY_READ
, vty_sock
, vty
);
1685 /* create vty for stdio */
1686 static struct termios stdio_orig_termios
;
1687 static struct vty
*stdio_vty
= NULL
;
1688 static bool stdio_termios
= false;
1689 static void (*stdio_vty_atclose
)(int isexit
);
1691 static void vty_stdio_reset(int isexit
)
1695 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1696 stdio_termios
= false;
1700 if (stdio_vty_atclose
)
1701 stdio_vty_atclose(isexit
);
1702 stdio_vty_atclose
= NULL
;
1706 static void vty_stdio_atexit(void)
1711 void vty_stdio_suspend(void)
1716 if (stdio_vty
->t_write
)
1717 thread_cancel(stdio_vty
->t_write
);
1718 if (stdio_vty
->t_read
)
1719 thread_cancel(stdio_vty
->t_read
);
1720 if (stdio_vty
->t_timeout
)
1721 thread_cancel(stdio_vty
->t_timeout
);
1724 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1725 stdio_termios
= false;
1728 void vty_stdio_resume(void)
1733 if (!tcgetattr(0, &stdio_orig_termios
)) {
1734 struct termios termios
;
1736 termios
= stdio_orig_termios
;
1737 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1738 | IGNCR
| ICRNL
| IXON
);
1739 termios
.c_oflag
&= ~OPOST
;
1740 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1741 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1742 termios
.c_cflag
|= CS8
;
1743 tcsetattr(0, TCSANOW
, &termios
);
1744 stdio_termios
= true;
1747 vty_prompt(stdio_vty
);
1749 /* Add read/write thread. */
1750 vty_event(VTY_WRITE
, 1, stdio_vty
);
1751 vty_event(VTY_READ
, 0, stdio_vty
);
1754 void vty_stdio_close(void)
1758 vty_close(stdio_vty
);
1761 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1765 /* refuse creating two vtys on stdio */
1769 vty
= stdio_vty
= vty_new_init(0);
1770 stdio_vty_atclose
= atclose
;
1773 /* always have stdio vty in a known _unchangeable_ state, don't want
1775 * to have any effect here to make sure scripting this works as intended
1777 vty
->node
= ENABLE_NODE
;
1779 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1785 /* Accept connection from the network. */
1786 static int vty_accept(struct thread
*thread
)
1794 struct access_list
*acl
= NULL
;
1795 char buf
[SU_ADDRSTRLEN
];
1797 accept_sock
= THREAD_FD(thread
);
1799 /* We continue hearing vty socket. */
1800 vty_event(VTY_SERV
, accept_sock
, NULL
);
1802 memset(&su
, 0, sizeof(union sockunion
));
1804 /* We can handle IPv4 or IPv6 socket. */
1805 vty_sock
= sockunion_accept(accept_sock
, &su
);
1807 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1808 safe_strerror(errno
));
1811 set_nonblocking(vty_sock
);
1812 set_cloexec(vty_sock
);
1814 sockunion2hostprefix(&su
, &p
);
1816 /* VTY's accesslist apply. */
1817 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1818 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1819 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1820 zlog_info("Vty connection refused from %s",
1821 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1824 /* continue accepting connections */
1825 vty_event(VTY_SERV
, accept_sock
, NULL
);
1831 /* VTY's ipv6 accesslist apply. */
1832 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1833 if ((acl
= access_list_lookup(AFI_IP6
,
1834 vty_ipv6_accesslist_name
))
1835 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1836 zlog_info("Vty connection refused from %s",
1837 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1840 /* continue accepting connections */
1841 vty_event(VTY_SERV
, accept_sock
, NULL
);
1848 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1851 zlog_info("can't set sockopt to vty_sock : %s",
1852 safe_strerror(errno
));
1854 zlog_info("Vty connection from %s",
1855 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1857 vty_create(vty_sock
, &su
);
1862 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1865 struct addrinfo req
;
1866 struct addrinfo
*ainfo
;
1867 struct addrinfo
*ainfo_save
;
1869 char port_str
[BUFSIZ
];
1871 memset(&req
, 0, sizeof(struct addrinfo
));
1872 req
.ai_flags
= AI_PASSIVE
;
1873 req
.ai_family
= AF_UNSPEC
;
1874 req
.ai_socktype
= SOCK_STREAM
;
1875 sprintf(port_str
, "%d", port
);
1876 port_str
[sizeof(port_str
) - 1] = '\0';
1878 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1881 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1889 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1892 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1893 ainfo
->ai_protocol
);
1897 sockopt_v6only(ainfo
->ai_family
, sock
);
1898 sockopt_reuseaddr(sock
);
1899 sockopt_reuseport(sock
);
1902 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1904 close(sock
); /* Avoid sd leak. */
1908 ret
= listen(sock
, 3);
1910 close(sock
); /* Avoid sd leak. */
1914 vty_event(VTY_SERV
, sock
, NULL
);
1915 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1917 freeaddrinfo(ainfo_save
);
1921 /* For sockaddr_un. */
1924 /* VTY shell UNIX domain socket. */
1925 static void vty_serv_un(const char *path
)
1929 struct sockaddr_un serv
;
1931 struct zprivs_ids_t ids
;
1933 /* First of all, unlink existing socket */
1937 old_mask
= umask(0007);
1939 /* Make UNIX domain socket. */
1940 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1942 flog_err_sys(EC_LIB_SOCKET
,
1943 "Cannot create unix stream socket: %s",
1944 safe_strerror(errno
));
1948 /* Make server socket. */
1949 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1950 serv
.sun_family
= AF_UNIX
;
1951 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1952 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1953 len
= serv
.sun_len
= SUN_LEN(&serv
);
1955 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1956 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1960 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1962 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1963 safe_strerror(errno
));
1964 close(sock
); /* Avoid sd leak. */
1968 ret
= listen(sock
, 5);
1970 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1971 safe_strerror(errno
));
1972 close(sock
); /* Avoid sd leak. */
1978 zprivs_get_ids(&ids
);
1980 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1981 earlier for the case when we don't need to chown the file
1982 type casting it here to make a compare */
1983 if ((int)ids
.gid_vty
> 0) {
1984 /* set group of socket */
1985 if (chown(path
, -1, ids
.gid_vty
)) {
1986 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1987 "vty_serv_un: could chown socket, %s",
1988 safe_strerror(errno
));
1992 vty_event(VTYSH_SERV
, sock
, NULL
);
1995 /* #define VTYSH_DEBUG 1 */
1997 static int vtysh_accept(struct thread
*thread
)
2002 struct sockaddr_un client
;
2005 accept_sock
= THREAD_FD(thread
);
2007 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2009 memset(&client
, 0, sizeof(struct sockaddr_un
));
2010 client_len
= sizeof(struct sockaddr_un
);
2012 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2013 (socklen_t
*)&client_len
);
2016 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2017 safe_strerror(errno
));
2021 if (set_nonblocking(sock
) < 0) {
2024 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2025 sock
, safe_strerror(errno
));
2032 printf("VTY shell accept\n");
2033 #endif /* VTYSH_DEBUG */
2038 vty
->type
= VTY_SHELL_SERV
;
2039 vty
->node
= VIEW_NODE
;
2041 vty_event(VTYSH_READ
, sock
, vty
);
2046 static int vtysh_flush(struct vty
*vty
)
2048 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2049 case BUFFER_PENDING
:
2050 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2054 0; /* disable monitoring to avoid infinite recursion */
2055 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2057 buffer_reset(vty
->lbuf
);
2058 buffer_reset(vty
->obuf
);
2068 static int vtysh_read(struct thread
*thread
)
2074 unsigned char buf
[VTY_READ_BUFSIZ
];
2076 uint8_t header
[4] = {0, 0, 0, 0};
2078 sock
= THREAD_FD(thread
);
2079 vty
= THREAD_ARG(thread
);
2082 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2084 if (ERRNO_IO_RETRY(errno
)) {
2085 vty_event(VTYSH_READ
, sock
, vty
);
2088 vty
->monitor
= 0; /* disable monitoring to avoid
2089 infinite recursion */
2092 "%s: read failed on vtysh client fd %d, closing: %s",
2093 __func__
, sock
, safe_strerror(errno
));
2095 buffer_reset(vty
->lbuf
);
2096 buffer_reset(vty
->obuf
);
2099 printf("close vtysh\n");
2100 #endif /* VTYSH_DEBUG */
2105 printf("line: %.*s\n", nbytes
, buf
);
2106 #endif /* VTYSH_DEBUG */
2108 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2109 /* Clear command line buffer. */
2110 vty
->cp
= vty
->length
= 0;
2112 vty_out(vty
, "%% Command is too long.\n");
2114 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2115 vty
->buf
[vty
->length
++] = *p
;
2117 /* Pass this line to parser. */
2118 ret
= vty_execute(vty
);
2119 /* Note that vty_execute clears the command buffer and resets
2120 vty->length to 0. */
2122 /* Return result. */
2124 printf("result: %d\n", ret
);
2125 printf("vtysh node: %d\n", vty
->node
);
2126 #endif /* VTYSH_DEBUG */
2128 /* hack for asynchronous "write integrated"
2129 * - other commands in "buf" will be ditched
2130 * - input during pending config-write is
2132 if (ret
== CMD_SUSPEND
)
2135 /* warning: watchfrr hardcodes this result write
2138 buffer_put(vty
->obuf
, header
, 4);
2140 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2141 /* Try to flush results; exit if a write
2148 if (vty
->status
== VTY_CLOSE
)
2151 vty_event(VTYSH_READ
, sock
, vty
);
2156 static int vtysh_write(struct thread
*thread
)
2158 struct vty
*vty
= THREAD_ARG(thread
);
2160 vty
->t_write
= NULL
;
2167 /* Determine address family to bind. */
2168 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2170 /* If port is set to 0, do not listen on TCP/IP at all! */
2172 vty_serv_sock_addrinfo(addr
, port
);
2179 static void vty_error_delete(void *arg
)
2181 struct vty_error
*ve
= arg
;
2183 XFREE(MTYPE_TMP
, ve
);
2186 /* Close vty interface. Warning: call this only from functions that
2187 will be careful not to access the vty afterwards (since it has
2188 now been freed). This is safest from top-level functions (called
2189 directly by the thread dispatcher). */
2190 void vty_close(struct vty
*vty
)
2193 bool was_stdio
= false;
2195 /* Cancel threads.*/
2197 thread_cancel(vty
->t_read
);
2199 thread_cancel(vty
->t_write
);
2201 thread_cancel(vty
->t_timeout
);
2204 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2206 /* Free input buffer. */
2207 buffer_free(vty
->obuf
);
2208 buffer_free(vty
->lbuf
);
2210 /* Free command history. */
2211 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2212 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2217 vector_unset(vtyvec
, vty
->fd
);
2219 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2223 * note check is for fd > STDERR_FILENO, not fd != -1.
2224 * We never close stdin/stdout/stderr here, because we may be
2225 * running in foreground mode with logging to stdout. Also,
2226 * additionally, we'd need to replace these fds with /dev/null. */
2227 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2229 if (vty
->fd
> STDERR_FILENO
)
2231 if (vty
->fd
== STDIN_FILENO
)
2234 XFREE(MTYPE_VTY
, vty
->buf
);
2237 vty
->error
->del
= vty_error_delete
;
2238 list_delete(&vty
->error
);
2241 /* Check configure. */
2242 vty_config_exit(vty
);
2245 XFREE(MTYPE_VTY
, vty
);
2251 /* When time out occur output message then close connection. */
2252 static int vty_timeout(struct thread
*thread
)
2256 vty
= THREAD_ARG(thread
);
2257 vty
->t_timeout
= NULL
;
2261 buffer_reset(vty
->lbuf
);
2262 buffer_reset(vty
->obuf
);
2263 vty_out(vty
, "\nVty connection is timed out.\n");
2265 /* Close connection. */
2266 vty
->status
= VTY_CLOSE
;
2272 /* Read up configuration file from file_name. */
2273 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2277 struct vty_error
*ve
;
2278 struct listnode
*node
;
2279 unsigned int line_num
= 0;
2282 /* vty_close won't close stderr; if some config command prints
2283 * something it'll end up there. (not ideal; it'd be beter if output
2284 * from a file-load went to logging instead. Also note that if this
2285 * function is called after daemonizing, stderr will be /dev/null.)
2287 * vty->fd will be -1 from vty_new()
2289 vty
->wfd
= STDERR_FILENO
;
2290 vty
->type
= VTY_FILE
;
2291 vty
->node
= CONFIG_NODE
;
2294 vty
->candidate_config
= config
;
2296 vty
->private_config
= true;
2297 vty
->candidate_config
= nb_config_new(NULL
);
2300 /* Execute configuration file */
2301 ret
= config_from_file(vty
, confp
, &line_num
);
2303 /* Flush any previous errors before printing messages below */
2304 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2306 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2307 const char *message
= NULL
;
2311 case CMD_ERR_AMBIGUOUS
:
2312 message
= "Ambiguous command";
2314 case CMD_ERR_NO_MATCH
:
2315 message
= "No such command";
2318 message
= "Command returned Warning";
2320 case CMD_WARNING_CONFIG_FAILED
:
2321 message
= "Command returned Warning Config Failed";
2323 case CMD_ERR_INCOMPLETE
:
2324 message
= "Command returned Incomplete";
2326 case CMD_ERR_EXEED_ARGC_MAX
:
2328 "Command exceeded maximum number of Arguments";
2331 message
= "Command returned unhandled error message";
2335 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2336 nl
= strchr(ve
->error_buf
, '\n');
2339 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2340 message
, ve
->line_num
, ve
->error_buf
);
2345 * Automatically commit the candidate configuration after
2346 * reading the configuration file.
2348 if (config
== NULL
&& vty
->candidate_config
2349 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2350 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2351 vty
, true, "Read configuration file",
2353 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2354 zlog_err("%s: failed to read configuration file.",
2361 static FILE *vty_use_backup_config(const char *fullpath
)
2363 char *fullpath_sav
, *fullpath_tmp
;
2369 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2370 fullpath_sav
= malloc(fullpath_sav_sz
);
2371 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2372 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2374 sav
= open(fullpath_sav
, O_RDONLY
);
2380 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2381 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2383 /* Open file to configuration write. */
2384 tmp
= mkstemp(fullpath_tmp
);
2388 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2391 while ((c
= read(sav
, buffer
, 512)) > 0) {
2392 if (write(tmp
, buffer
, c
) <= 0)
2398 if (rename(fullpath_tmp
, fullpath
) == 0)
2399 ret
= fopen(fullpath
, "r");
2401 unlink(fullpath_tmp
);
2406 unlink(fullpath_tmp
);
2416 /* Read up configuration file from file_name. */
2417 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2418 char *config_default_dir
)
2420 char cwd
[MAXPATHLEN
];
2422 const char *fullpath
;
2424 bool read_success
= false;
2426 /* If -f flag specified. */
2427 if (config_file
!= NULL
) {
2428 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2429 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2432 "%s: failure to determine Current Working Directory %d!",
2434 goto tmp_free_and_out
;
2436 tmp
= XMALLOC(MTYPE_TMP
,
2437 strlen(cwd
) + strlen(config_file
) + 2);
2438 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2441 fullpath
= config_file
;
2443 confp
= fopen(fullpath
, "r");
2445 if (confp
== NULL
) {
2447 EC_LIB_BACKUP_CONFIG
,
2448 "%s: failed to open configuration file %s: %s, checking backup",
2449 __func__
, fullpath
, safe_strerror(errno
));
2451 confp
= vty_use_backup_config(fullpath
);
2454 EC_LIB_BACKUP_CONFIG
,
2455 "WARNING: using backup configuration file!");
2459 "%s: can't open configuration file [%s]",
2460 __func__
, config_file
);
2461 goto tmp_free_and_out
;
2466 host_config_set(config_default_dir
);
2470 struct stat conf_stat
;
2472 /* !!!!PLEASE LEAVE!!!!
2473 * This is NEEDED for use with vtysh -b, or else you can get
2474 * a real configuration food fight with a lot garbage in the
2475 * merged configuration file it creates coming from the per
2476 * daemon configuration files. This also allows the daemons
2477 * to start if there default configuration file is not
2478 * present or ignore them, as needed when using vtysh -b to
2479 * configure the daemons at boot - MAG
2482 /* Stat for vtysh Zebra.conf, if found startup and wait for
2483 * boot configuration
2486 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2487 ret
= stat(integrate_default
, &conf_stat
);
2489 read_success
= true;
2490 goto tmp_free_and_out
;
2494 confp
= fopen(config_default_dir
, "r");
2495 if (confp
== NULL
) {
2498 "%s: failed to open configuration file %s: %s, checking backup",
2499 __func__
, config_default_dir
,
2500 safe_strerror(errno
));
2502 confp
= vty_use_backup_config(config_default_dir
);
2505 EC_LIB_BACKUP_CONFIG
,
2506 "WARNING: using backup configuration file!");
2507 fullpath
= config_default_dir
;
2509 flog_err(EC_LIB_VTY
,
2510 "can't open configuration file [%s]",
2511 config_default_dir
);
2512 goto tmp_free_and_out
;
2515 fullpath
= config_default_dir
;
2518 vty_read_file(config
, confp
);
2519 read_success
= true;
2523 host_config_set(fullpath
);
2526 XFREE(MTYPE_TMP
, tmp
);
2528 return read_success
;
2531 /* Small utility function which output log to the VTY. */
2532 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2533 struct timestamp_control
*ctl
)
2541 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2542 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2544 vty_log_out(vty
, level
, proto_str
, msg
, 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_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2579 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2580 vty_out(vty
, "%% Configuration is locked by other client\n");
2584 vty
->node
= CONFIG_NODE
;
2586 vty
->private_config
= private_config
;
2587 vty
->xpath_index
= 0;
2589 pthread_rwlock_rdlock(&running_config
->lock
);
2591 if (private_config
) {
2592 vty
->candidate_config
= nb_config_dup(running_config
);
2593 vty
->candidate_config_base
=
2594 nb_config_dup(running_config
);
2596 "Warning: uncommitted changes will be discarded on exit.\n\n");
2598 vty
->candidate_config
= vty_shared_candidate_config
;
2599 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2600 vty
->candidate_config_base
=
2601 nb_config_dup(running_config
);
2604 pthread_rwlock_unlock(&running_config
->lock
);
2609 void vty_config_exit(struct vty
*vty
)
2611 /* Check if there's a pending confirmed commit. */
2612 if (vty
->t_confirmed_commit_timeout
) {
2614 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2615 nb_cli_confirmed_commit_rollback(vty
);
2616 nb_cli_confirmed_commit_clean(vty
);
2619 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2621 if (vty
->candidate_config
) {
2622 if (vty
->private_config
)
2623 nb_config_free(vty
->candidate_config
);
2624 vty
->candidate_config
= NULL
;
2626 if (vty
->candidate_config_base
) {
2627 nb_config_free(vty
->candidate_config_base
);
2628 vty
->candidate_config_base
= NULL
;
2631 vty
->config
= false;
2634 /* Master of the threads. */
2635 static struct thread_master
*vty_master
;
2637 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2639 struct thread
*vty_serv_thread
= NULL
;
2643 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2645 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2649 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2651 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2655 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2659 vty
->t_write
= NULL
;
2660 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2666 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2668 /* Time out treatment. */
2669 if (vty
->v_timeout
) {
2671 thread_cancel(vty
->t_timeout
);
2672 vty
->t_timeout
= NULL
;
2673 thread_add_timer(vty_master
, vty_timeout
, vty
,
2674 vty
->v_timeout
, &vty
->t_timeout
);
2678 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2681 case VTY_TIMEOUT_RESET
:
2682 if (vty
->t_timeout
) {
2683 thread_cancel(vty
->t_timeout
);
2684 vty
->t_timeout
= NULL
;
2686 if (vty
->v_timeout
) {
2687 vty
->t_timeout
= NULL
;
2688 thread_add_timer(vty_master
, vty_timeout
, vty
,
2689 vty
->v_timeout
, &vty
->t_timeout
);
2695 DEFUN_NOSH (config_who
,
2698 "Display who is on vty\n")
2703 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2704 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2705 vty_out(vty
, "%svty[%d] connected from %s.\n",
2706 v
->config
? "*" : " ", i
, v
->address
);
2710 /* Move to vty configuration mode. */
2711 DEFUN_NOSH (line_vty
,
2714 "Configure a terminal line\n"
2715 "Virtual terminal\n")
2717 vty
->node
= VTY_NODE
;
2721 /* Set time out value. */
2722 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2723 const char *sec_str
)
2725 unsigned long timeout
= 0;
2727 /* min_str and sec_str are already checked by parser. So it must be
2728 all digit string. */
2730 timeout
= strtol(min_str
, NULL
, 10);
2734 timeout
+= strtol(sec_str
, NULL
, 10);
2736 vty_timeout_val
= timeout
;
2737 vty
->v_timeout
= timeout
;
2738 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2744 DEFUN (exec_timeout_min
,
2745 exec_timeout_min_cmd
,
2746 "exec-timeout (0-35791)",
2747 "Set timeout value\n"
2748 "Timeout value in minutes\n")
2751 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2754 DEFUN (exec_timeout_sec
,
2755 exec_timeout_sec_cmd
,
2756 "exec-timeout (0-35791) (0-2147483)",
2757 "Set the EXEC timeout\n"
2758 "Timeout in minutes\n"
2759 "Timeout in seconds\n")
2762 int idx_number_2
= 2;
2763 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2764 argv
[idx_number_2
]->arg
);
2767 DEFUN (no_exec_timeout
,
2768 no_exec_timeout_cmd
,
2771 "Set the EXEC timeout\n")
2773 return exec_timeout(vty
, NULL
, NULL
);
2776 /* Set vty access class. */
2777 DEFUN (vty_access_class
,
2778 vty_access_class_cmd
,
2779 "access-class WORD",
2780 "Filter connections based on an IP access list\n"
2784 if (vty_accesslist_name
)
2785 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2787 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2792 /* Clear vty access class. */
2793 DEFUN (no_vty_access_class
,
2794 no_vty_access_class_cmd
,
2795 "no access-class [WORD]",
2797 "Filter connections based on an IP access list\n"
2801 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2802 if (!vty_accesslist_name
2803 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2804 vty_out(vty
, "Access-class is not currently applied to vty\n");
2805 return CMD_WARNING_CONFIG_FAILED
;
2808 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2810 vty_accesslist_name
= NULL
;
2815 /* Set vty access class. */
2816 DEFUN (vty_ipv6_access_class
,
2817 vty_ipv6_access_class_cmd
,
2818 "ipv6 access-class WORD",
2820 "Filter connections based on an IP access list\n"
2821 "IPv6 access list\n")
2824 if (vty_ipv6_accesslist_name
)
2825 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2827 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2832 /* Clear vty access class. */
2833 DEFUN (no_vty_ipv6_access_class
,
2834 no_vty_ipv6_access_class_cmd
,
2835 "no ipv6 access-class [WORD]",
2838 "Filter connections based on an IP access list\n"
2839 "IPv6 access list\n")
2842 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2844 if (!vty_ipv6_accesslist_name
2845 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2847 "IPv6 access-class is not currently applied to vty\n");
2848 return CMD_WARNING_CONFIG_FAILED
;
2851 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2853 vty_ipv6_accesslist_name
= NULL
;
2862 "Enable password checking\n")
2864 no_password_check
= 0;
2868 DEFUN (no_vty_login
,
2872 "Enable password checking\n")
2874 no_password_check
= 1;
2878 DEFUN (service_advanced_vty
,
2879 service_advanced_vty_cmd
,
2880 "service advanced-vty",
2881 "Set up miscellaneous service\n"
2882 "Enable advanced mode vty interface\n")
2888 DEFUN (no_service_advanced_vty
,
2889 no_service_advanced_vty_cmd
,
2890 "no service advanced-vty",
2892 "Set up miscellaneous service\n"
2893 "Enable advanced mode vty interface\n")
2899 DEFUN_NOSH (terminal_monitor
,
2900 terminal_monitor_cmd
,
2902 "Set terminal line parameters\n"
2903 "Copy debug output to the current terminal line\n")
2909 DEFUN_NOSH (terminal_no_monitor
,
2910 terminal_no_monitor_cmd
,
2911 "terminal no monitor",
2912 "Set terminal line parameters\n"
2914 "Copy debug output to the current terminal line\n")
2920 DEFUN_NOSH (no_terminal_monitor
,
2921 no_terminal_monitor_cmd
,
2922 "no terminal monitor",
2924 "Set terminal line parameters\n"
2925 "Copy debug output to the current terminal line\n")
2927 return terminal_no_monitor(self
, vty
, argc
, argv
);
2931 DEFUN_NOSH (show_history
,
2935 "Display the session command history\n")
2939 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2940 if (index
== VTY_MAXHIST
) {
2945 if (vty
->hist
[index
] != NULL
)
2946 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2955 DEFPY (log_commands
,
2957 "[no] log commands",
2960 "Log all commands\n")
2963 if (do_log_commands_perm
) {
2965 "Daemon started with permanent logging turned on for commands, ignoring\n");
2969 do_log_commands
= false;
2971 do_log_commands
= true;
2976 /* Display current configuration. */
2977 static int vty_config_write(struct vty
*vty
)
2979 vty_out(vty
, "line vty\n");
2981 if (vty_accesslist_name
)
2982 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2984 if (vty_ipv6_accesslist_name
)
2985 vty_out(vty
, " ipv6 access-class %s\n",
2986 vty_ipv6_accesslist_name
);
2989 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2990 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2991 vty_timeout_val
% 60);
2994 if (no_password_check
)
2995 vty_out(vty
, " no login\n");
2997 if (do_log_commands
)
2998 vty_out(vty
, "log commands\n");
3000 vty_out(vty
, "!\n");
3005 struct cmd_node vty_node
= {
3006 VTY_NODE
, "%s(config-line)# ", 1,
3009 /* Reset all VTY status. */
3010 void vty_reset(void)
3014 struct thread
*vty_serv_thread
;
3016 for (i
= 0; i
< vector_active(vtyvec
); i
++)
3017 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
3018 buffer_reset(vty
->lbuf
);
3019 buffer_reset(vty
->obuf
);
3020 vty
->status
= VTY_CLOSE
;
3024 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3025 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3027 thread_cancel(vty_serv_thread
);
3028 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3032 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3034 if (vty_accesslist_name
) {
3035 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3036 vty_accesslist_name
= NULL
;
3039 if (vty_ipv6_accesslist_name
) {
3040 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3041 vty_ipv6_accesslist_name
= NULL
;
3045 static void vty_save_cwd(void)
3049 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3053 * At this point if these go wrong, more than likely
3054 * the whole world is coming down around us
3055 * Hence not worrying about it too much.
3057 if (!chdir(SYSCONFDIR
)) {
3058 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3059 "Failure to chdir to %s, errno: %d",
3063 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3064 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3065 "Failure to getcwd, errno: %d", errno
);
3071 char *vty_get_cwd(void)
3076 int vty_shell(struct vty
*vty
)
3078 return vty
->type
== VTY_SHELL
? 1 : 0;
3081 int vty_shell_serv(struct vty
*vty
)
3083 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3086 void vty_init_vtysh(void)
3088 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3091 /* Install vty's own commands like `who' command. */
3092 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3094 /* For further configuration read, preserve current directory. */
3097 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3099 vty_master
= master_thread
;
3101 atexit(vty_stdio_atexit
);
3103 /* Initilize server thread vector. */
3104 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3106 /* Install bgp top node. */
3107 install_node(&vty_node
, vty_config_write
);
3109 install_element(VIEW_NODE
, &config_who_cmd
);
3110 install_element(VIEW_NODE
, &show_history_cmd
);
3111 install_element(CONFIG_NODE
, &line_vty_cmd
);
3112 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3113 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3114 install_element(CONFIG_NODE
, &show_history_cmd
);
3115 install_element(CONFIG_NODE
, &log_commands_cmd
);
3117 if (do_command_logging
) {
3118 do_log_commands
= true;
3119 do_log_commands_perm
= true;
3122 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3123 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3124 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3126 install_default(VTY_NODE
);
3127 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3128 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3129 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3130 install_element(VTY_NODE
, &vty_access_class_cmd
);
3131 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3132 install_element(VTY_NODE
, &vty_login_cmd
);
3133 install_element(VTY_NODE
, &no_vty_login_cmd
);
3134 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3135 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3138 void vty_terminate(void)
3140 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3142 if (vtyvec
&& Vvty_serv_thread
) {
3144 vector_free(vtyvec
);
3145 vector_free(Vvty_serv_thread
);
3147 Vvty_serv_thread
= NULL
;