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
);
1339 /* Read raw data from socket */
1340 if ((nbytes
= read(vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
1342 if (ERRNO_IO_RETRY(errno
)) {
1343 vty_event(VTY_READ
, vty_sock
, vty
);
1346 vty
->monitor
= 0; /* disable monitoring to avoid
1347 infinite recursion */
1350 "%s: read error on vty client fd %d, closing: %s",
1351 __func__
, vty
->fd
, safe_strerror(errno
));
1352 buffer_reset(vty
->obuf
);
1353 buffer_reset(vty
->lbuf
);
1355 vty
->status
= VTY_CLOSE
;
1358 for (i
= 0; i
< nbytes
; i
++) {
1359 if (buf
[i
] == IAC
) {
1368 if (vty
->iac_sb_in_progress
&& !vty
->iac
) {
1369 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1370 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1376 /* In case of telnet command */
1378 ret
= vty_telnet_option(vty
, buf
+ i
, nbytes
- i
);
1385 if (vty
->status
== VTY_MORE
) {
1390 vty_buffer_reset(vty
);
1392 #if 0 /* More line does not work for "show ip bgp". */
1395 vty
->status
= VTY_MORELINE
;
1404 /* Escape character. */
1405 if (vty
->escape
== VTY_ESCAPE
) {
1406 vty_escape_map(buf
[i
], vty
);
1410 /* Pre-escape status. */
1411 if (vty
->escape
== VTY_PRE_ESCAPE
) {
1414 vty
->escape
= VTY_ESCAPE
;
1417 vty_backward_word(vty
);
1418 vty
->escape
= VTY_NORMAL
;
1421 vty_forward_word(vty
);
1422 vty
->escape
= VTY_NORMAL
;
1425 vty_forward_kill_word(vty
);
1426 vty
->escape
= VTY_NORMAL
;
1430 vty_backward_kill_word(vty
);
1431 vty
->escape
= VTY_NORMAL
;
1434 vty
->escape
= VTY_NORMAL
;
1442 vty_beginning_of_line(vty
);
1445 vty_backward_char(vty
);
1448 vty_stop_input(vty
);
1451 vty_delete_char(vty
);
1454 vty_end_of_line(vty
);
1457 vty_forward_char(vty
);
1461 vty_delete_backward_char(vty
);
1470 vty_previous_line(vty
);
1473 vty_transpose_chars(vty
);
1476 vty_kill_line_from_beginning(vty
);
1479 vty_backward_kill_word(vty
);
1482 vty_end_config(vty
);
1490 vty_complete_command(vty
);
1493 if (vty
->node
== AUTH_NODE
1494 || vty
->node
== AUTH_ENABLE_NODE
)
1495 vty_self_insert(vty
, buf
[i
]);
1497 vty_describe_command(vty
);
1500 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[') {
1501 vty
->escape
= VTY_ESCAPE
;
1504 vty
->escape
= VTY_PRE_ESCAPE
;
1507 if (buf
[i
] > 31 && buf
[i
] < 127)
1508 vty_self_insert(vty
, buf
[i
]);
1514 if (vty
->status
== VTY_CLOSE
)
1517 vty_event(VTY_WRITE
, vty
->wfd
, vty
);
1518 vty_event(VTY_READ
, vty_sock
, vty
);
1523 /* Flush buffer to the vty. */
1524 static int vty_flush(struct thread
*thread
)
1527 buffer_status_t flushrc
;
1528 int vty_sock
= THREAD_FD(thread
);
1529 struct vty
*vty
= THREAD_ARG(thread
);
1531 /* Tempolary disable read thread. */
1532 if (vty
->lines
== 0)
1533 THREAD_OFF(vty
->t_read
);
1535 /* Function execution continue. */
1536 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1538 /* N.B. if width is 0, that means we don't know the window size. */
1539 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1540 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1541 else if (vty
->status
== VTY_MORELINE
)
1542 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1545 flushrc
= buffer_flush_window(
1546 vty
->obuf
, vty_sock
, vty
->width
,
1547 vty
->lines
>= 0 ? vty
->lines
: vty
->height
, erase
, 0);
1551 0; /* disable monitoring to avoid infinite recursion */
1552 zlog_info("buffer_flush failed on vty client fd %d, closing",
1554 buffer_reset(vty
->lbuf
);
1555 buffer_reset(vty
->obuf
);
1559 if (vty
->status
== VTY_CLOSE
)
1562 vty
->status
= VTY_NORMAL
;
1563 if (vty
->lines
== 0)
1564 vty_event(VTY_READ
, vty_sock
, vty
);
1567 case BUFFER_PENDING
:
1568 /* There is more data waiting to be written. */
1569 vty
->status
= VTY_MORE
;
1570 if (vty
->lines
== 0)
1571 vty_event(VTY_WRITE
, vty_sock
, vty
);
1578 /* Allocate new vty struct. */
1579 struct vty
*vty_new(void)
1581 struct vty
*new = XCALLOC(MTYPE_VTY
, sizeof(struct vty
));
1583 new->fd
= new->wfd
= -1;
1585 new->lbuf
= buffer_new(0);
1586 new->obuf
= buffer_new(0); /* Use default buffer size. */
1587 new->buf
= XCALLOC(MTYPE_VTY
, VTY_BUFSIZ
);
1588 new->max
= VTY_BUFSIZ
;
1594 /* allocate and initialise vty */
1595 static struct vty
*vty_new_init(int vty_sock
)
1601 vty
->wfd
= vty_sock
;
1602 vty
->type
= VTY_TERM
;
1603 vty
->node
= AUTH_NODE
;
1608 memset(vty
->hist
, 0, sizeof(vty
->hist
));
1611 vty
->xpath_index
= 0;
1612 memset(vty
->xpath
, 0, sizeof(vty
->xpath
));
1613 vty
->private_config
= false;
1614 vty
->candidate_config
= vty_shared_candidate_config
;
1615 vector_set_index(vtyvec
, vty_sock
, vty
);
1616 vty
->status
= VTY_NORMAL
;
1619 vty
->iac_sb_in_progress
= 0;
1625 /* Create new vty structure. */
1626 static struct vty
*vty_create(int vty_sock
, union sockunion
*su
)
1628 char buf
[SU_ADDRSTRLEN
];
1631 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1633 /* Allocate new vty structure and set up default values. */
1634 vty
= vty_new_init(vty_sock
);
1636 /* configurable parameters not part of basic init */
1637 vty
->v_timeout
= vty_timeout_val
;
1638 strlcpy(vty
->address
, buf
, sizeof(vty
->address
));
1639 if (no_password_check
) {
1641 vty
->node
= ENABLE_NODE
;
1643 vty
->node
= VIEW_NODE
;
1645 if (host
.lines
>= 0)
1646 vty
->lines
= host
.lines
;
1648 if (!no_password_check
) {
1649 /* Vty is not available if password isn't set. */
1650 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
) {
1651 vty_out(vty
, "Vty password is not set.\n");
1652 vty
->status
= VTY_CLOSE
;
1658 /* Say hello to the world. */
1660 if (!no_password_check
)
1661 vty_out(vty
, "\nUser Access Verification\n\n");
1663 /* Setting up terminal. */
1665 vty_will_suppress_go_ahead(vty
);
1667 vty_dont_linemode(vty
);
1668 vty_do_window_size(vty
);
1669 /* vty_dont_lflow_ahead (vty); */
1673 /* Add read/write thread. */
1674 vty_event(VTY_WRITE
, vty_sock
, vty
);
1675 vty_event(VTY_READ
, vty_sock
, vty
);
1680 /* create vty for stdio */
1681 static struct termios stdio_orig_termios
;
1682 static struct vty
*stdio_vty
= NULL
;
1683 static bool stdio_termios
= false;
1684 static void (*stdio_vty_atclose
)(int isexit
);
1686 static void vty_stdio_reset(int isexit
)
1690 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1691 stdio_termios
= false;
1695 if (stdio_vty_atclose
)
1696 stdio_vty_atclose(isexit
);
1697 stdio_vty_atclose
= NULL
;
1701 static void vty_stdio_atexit(void)
1706 void vty_stdio_suspend(void)
1711 THREAD_OFF(stdio_vty
->t_write
);
1712 THREAD_OFF(stdio_vty
->t_read
);
1713 THREAD_OFF(stdio_vty
->t_timeout
);
1716 tcsetattr(0, TCSANOW
, &stdio_orig_termios
);
1717 stdio_termios
= false;
1720 void vty_stdio_resume(void)
1725 if (!tcgetattr(0, &stdio_orig_termios
)) {
1726 struct termios termios
;
1728 termios
= stdio_orig_termios
;
1729 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
| INLCR
1730 | IGNCR
| ICRNL
| IXON
);
1731 termios
.c_oflag
&= ~OPOST
;
1732 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1733 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1734 termios
.c_cflag
|= CS8
;
1735 tcsetattr(0, TCSANOW
, &termios
);
1736 stdio_termios
= true;
1739 vty_prompt(stdio_vty
);
1741 /* Add read/write thread. */
1742 vty_event(VTY_WRITE
, 1, stdio_vty
);
1743 vty_event(VTY_READ
, 0, stdio_vty
);
1746 void vty_stdio_close(void)
1750 vty_close(stdio_vty
);
1753 struct vty
*vty_stdio(void (*atclose
)(int isexit
))
1757 /* refuse creating two vtys on stdio */
1761 vty
= stdio_vty
= vty_new_init(0);
1762 stdio_vty_atclose
= atclose
;
1765 /* always have stdio vty in a known _unchangeable_ state, don't want
1767 * to have any effect here to make sure scripting this works as intended
1769 vty
->node
= ENABLE_NODE
;
1771 strlcpy(vty
->address
, "console", sizeof(vty
->address
));
1777 /* Accept connection from the network. */
1778 static int vty_accept(struct thread
*thread
)
1786 struct access_list
*acl
= NULL
;
1787 char buf
[SU_ADDRSTRLEN
];
1789 accept_sock
= THREAD_FD(thread
);
1791 /* We continue hearing vty socket. */
1792 vty_event(VTY_SERV
, accept_sock
, NULL
);
1794 memset(&su
, 0, sizeof(union sockunion
));
1796 /* We can handle IPv4 or IPv6 socket. */
1797 vty_sock
= sockunion_accept(accept_sock
, &su
);
1799 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
1800 safe_strerror(errno
));
1803 set_nonblocking(vty_sock
);
1804 set_cloexec(vty_sock
);
1806 sockunion2hostprefix(&su
, &p
);
1808 /* VTY's accesslist apply. */
1809 if (p
.family
== AF_INET
&& vty_accesslist_name
) {
1810 if ((acl
= access_list_lookup(AFI_IP
, vty_accesslist_name
))
1811 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1812 zlog_info("Vty connection refused from %s",
1813 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1816 /* continue accepting connections */
1817 vty_event(VTY_SERV
, accept_sock
, NULL
);
1823 /* VTY's ipv6 accesslist apply. */
1824 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
) {
1825 if ((acl
= access_list_lookup(AFI_IP6
,
1826 vty_ipv6_accesslist_name
))
1827 && (access_list_apply(acl
, &p
) == FILTER_DENY
)) {
1828 zlog_info("Vty connection refused from %s",
1829 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1832 /* continue accepting connections */
1833 vty_event(VTY_SERV
, accept_sock
, NULL
);
1840 ret
= setsockopt(vty_sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&on
,
1843 zlog_info("can't set sockopt to vty_sock : %s",
1844 safe_strerror(errno
));
1846 zlog_info("Vty connection from %s",
1847 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
1849 vty_create(vty_sock
, &su
);
1854 static void vty_serv_sock_addrinfo(const char *hostname
, unsigned short port
)
1857 struct addrinfo req
;
1858 struct addrinfo
*ainfo
;
1859 struct addrinfo
*ainfo_save
;
1861 char port_str
[BUFSIZ
];
1863 memset(&req
, 0, sizeof(struct addrinfo
));
1864 req
.ai_flags
= AI_PASSIVE
;
1865 req
.ai_family
= AF_UNSPEC
;
1866 req
.ai_socktype
= SOCK_STREAM
;
1867 sprintf(port_str
, "%d", port
);
1868 port_str
[sizeof(port_str
) - 1] = '\0';
1870 ret
= getaddrinfo(hostname
, port_str
, &req
, &ainfo
);
1873 flog_err_sys(EC_LIB_SYSTEM_CALL
, "getaddrinfo failed: %s",
1881 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
1884 sock
= socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
1885 ainfo
->ai_protocol
);
1889 sockopt_v6only(ainfo
->ai_family
, sock
);
1890 sockopt_reuseaddr(sock
);
1891 sockopt_reuseport(sock
);
1894 ret
= bind(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1896 close(sock
); /* Avoid sd leak. */
1900 ret
= listen(sock
, 3);
1902 close(sock
); /* Avoid sd leak. */
1906 vty_event(VTY_SERV
, sock
, NULL
);
1907 } while ((ainfo
= ainfo
->ai_next
) != NULL
);
1909 freeaddrinfo(ainfo_save
);
1913 /* For sockaddr_un. */
1916 /* VTY shell UNIX domain socket. */
1917 static void vty_serv_un(const char *path
)
1921 struct sockaddr_un serv
;
1923 struct zprivs_ids_t ids
;
1925 /* First of all, unlink existing socket */
1929 old_mask
= umask(0007);
1931 /* Make UNIX domain socket. */
1932 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1934 flog_err_sys(EC_LIB_SOCKET
,
1935 "Cannot create unix stream socket: %s",
1936 safe_strerror(errno
));
1940 /* Make server socket. */
1941 memset(&serv
, 0, sizeof(struct sockaddr_un
));
1942 serv
.sun_family
= AF_UNIX
;
1943 strlcpy(serv
.sun_path
, path
, sizeof(serv
.sun_path
));
1944 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1945 len
= serv
.sun_len
= SUN_LEN(&serv
);
1947 len
= sizeof(serv
.sun_family
) + strlen(serv
.sun_path
);
1948 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1952 ret
= bind(sock
, (struct sockaddr
*)&serv
, len
);
1954 flog_err_sys(EC_LIB_SOCKET
, "Cannot bind path %s: %s", path
,
1955 safe_strerror(errno
));
1956 close(sock
); /* Avoid sd leak. */
1960 ret
= listen(sock
, 5);
1962 flog_err_sys(EC_LIB_SOCKET
, "listen(fd %d) failed: %s", sock
,
1963 safe_strerror(errno
));
1964 close(sock
); /* Avoid sd leak. */
1970 zprivs_get_ids(&ids
);
1972 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1973 earlier for the case when we don't need to chown the file
1974 type casting it here to make a compare */
1975 if ((int)ids
.gid_vty
> 0) {
1976 /* set group of socket */
1977 if (chown(path
, -1, ids
.gid_vty
)) {
1978 flog_err_sys(EC_LIB_SYSTEM_CALL
,
1979 "vty_serv_un: could chown socket, %s",
1980 safe_strerror(errno
));
1984 vty_event(VTYSH_SERV
, sock
, NULL
);
1987 /* #define VTYSH_DEBUG 1 */
1989 static int vtysh_accept(struct thread
*thread
)
1994 struct sockaddr_un client
;
1997 accept_sock
= THREAD_FD(thread
);
1999 vty_event(VTYSH_SERV
, accept_sock
, NULL
);
2001 memset(&client
, 0, sizeof(struct sockaddr_un
));
2002 client_len
= sizeof(struct sockaddr_un
);
2004 sock
= accept(accept_sock
, (struct sockaddr
*)&client
,
2005 (socklen_t
*)&client_len
);
2008 flog_err(EC_LIB_SOCKET
, "can't accept vty socket : %s",
2009 safe_strerror(errno
));
2013 if (set_nonblocking(sock
) < 0) {
2016 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
2017 sock
, safe_strerror(errno
));
2024 printf("VTY shell accept\n");
2025 #endif /* VTYSH_DEBUG */
2030 vty
->type
= VTY_SHELL_SERV
;
2031 vty
->node
= VIEW_NODE
;
2033 vty_event(VTYSH_READ
, sock
, vty
);
2038 static int vtysh_flush(struct vty
*vty
)
2040 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
)) {
2041 case BUFFER_PENDING
:
2042 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2046 0; /* disable monitoring to avoid infinite recursion */
2047 flog_err(EC_LIB_SOCKET
, "%s: write error to fd %d, closing",
2049 buffer_reset(vty
->lbuf
);
2050 buffer_reset(vty
->obuf
);
2060 static int vtysh_read(struct thread
*thread
)
2066 unsigned char buf
[VTY_READ_BUFSIZ
];
2068 uint8_t header
[4] = {0, 0, 0, 0};
2070 sock
= THREAD_FD(thread
);
2071 vty
= THREAD_ARG(thread
);
2073 if ((nbytes
= read(sock
, buf
, VTY_READ_BUFSIZ
)) <= 0) {
2075 if (ERRNO_IO_RETRY(errno
)) {
2076 vty_event(VTYSH_READ
, sock
, vty
);
2079 vty
->monitor
= 0; /* disable monitoring to avoid
2080 infinite recursion */
2083 "%s: read failed on vtysh client fd %d, closing: %s",
2084 __func__
, sock
, safe_strerror(errno
));
2086 buffer_reset(vty
->lbuf
);
2087 buffer_reset(vty
->obuf
);
2090 printf("close vtysh\n");
2091 #endif /* VTYSH_DEBUG */
2096 printf("line: %.*s\n", nbytes
, buf
);
2097 #endif /* VTYSH_DEBUG */
2099 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
) {
2100 /* Clear command line buffer. */
2101 vty
->cp
= vty
->length
= 0;
2103 vty_out(vty
, "%% Command is too long.\n");
2105 for (p
= buf
; p
< buf
+ nbytes
; p
++) {
2106 vty
->buf
[vty
->length
++] = *p
;
2108 /* Pass this line to parser. */
2109 ret
= vty_execute(vty
);
2110 /* Note that vty_execute clears the command buffer and resets
2111 vty->length to 0. */
2113 /* Return result. */
2115 printf("result: %d\n", ret
);
2116 printf("vtysh node: %d\n", vty
->node
);
2117 #endif /* VTYSH_DEBUG */
2119 /* hack for asynchronous "write integrated"
2120 * - other commands in "buf" will be ditched
2121 * - input during pending config-write is
2123 if (ret
== CMD_SUSPEND
)
2126 /* warning: watchfrr hardcodes this result write
2129 buffer_put(vty
->obuf
, header
, 4);
2131 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2132 /* Try to flush results; exit if a write
2139 if (vty
->status
== VTY_CLOSE
)
2142 vty_event(VTYSH_READ
, sock
, vty
);
2147 static int vtysh_write(struct thread
*thread
)
2149 struct vty
*vty
= THREAD_ARG(thread
);
2157 /* Determine address family to bind. */
2158 void vty_serv_sock(const char *addr
, unsigned short port
, const char *path
)
2160 /* If port is set to 0, do not listen on TCP/IP at all! */
2162 vty_serv_sock_addrinfo(addr
, port
);
2169 static void vty_error_delete(void *arg
)
2171 struct vty_error
*ve
= arg
;
2173 XFREE(MTYPE_TMP
, ve
);
2176 /* Close vty interface. Warning: call this only from functions that
2177 will be careful not to access the vty afterwards (since it has
2178 now been freed). This is safest from top-level functions (called
2179 directly by the thread dispatcher). */
2180 void vty_close(struct vty
*vty
)
2183 bool was_stdio
= false;
2185 /* Cancel threads.*/
2186 THREAD_OFF(vty
->t_read
);
2187 THREAD_OFF(vty
->t_write
);
2188 THREAD_OFF(vty
->t_timeout
);
2191 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2193 /* Free input buffer. */
2194 buffer_free(vty
->obuf
);
2195 buffer_free(vty
->lbuf
);
2197 /* Free command history. */
2198 for (i
= 0; i
< VTY_MAXHIST
; i
++) {
2199 XFREE(MTYPE_VTY_HIST
, vty
->hist
[i
]);
2204 vector_unset(vtyvec
, vty
->fd
);
2206 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2210 * note check is for fd > STDERR_FILENO, not fd != -1.
2211 * We never close stdin/stdout/stderr here, because we may be
2212 * running in foreground mode with logging to stdout. Also,
2213 * additionally, we'd need to replace these fds with /dev/null. */
2214 if (vty
->wfd
> STDERR_FILENO
&& vty
->wfd
!= vty
->fd
)
2216 if (vty
->fd
> STDERR_FILENO
)
2218 if (vty
->fd
== STDIN_FILENO
)
2221 XFREE(MTYPE_VTY
, vty
->buf
);
2224 vty
->error
->del
= vty_error_delete
;
2225 list_delete(&vty
->error
);
2228 /* Check configure. */
2229 vty_config_exit(vty
);
2232 XFREE(MTYPE_VTY
, vty
);
2238 /* When time out occur output message then close connection. */
2239 static int vty_timeout(struct thread
*thread
)
2243 vty
= THREAD_ARG(thread
);
2247 buffer_reset(vty
->lbuf
);
2248 buffer_reset(vty
->obuf
);
2249 vty_out(vty
, "\nVty connection is timed out.\n");
2251 /* Close connection. */
2252 vty
->status
= VTY_CLOSE
;
2258 /* Read up configuration file from file_name. */
2259 static void vty_read_file(struct nb_config
*config
, FILE *confp
)
2263 struct vty_error
*ve
;
2264 struct listnode
*node
;
2265 unsigned int line_num
= 0;
2268 /* vty_close won't close stderr; if some config command prints
2269 * something it'll end up there. (not ideal; it'd be beter if output
2270 * from a file-load went to logging instead. Also note that if this
2271 * function is called after daemonizing, stderr will be /dev/null.)
2273 * vty->fd will be -1 from vty_new()
2275 vty
->wfd
= STDERR_FILENO
;
2276 vty
->type
= VTY_FILE
;
2277 vty
->node
= CONFIG_NODE
;
2280 vty
->candidate_config
= config
;
2282 vty
->private_config
= true;
2283 vty
->candidate_config
= nb_config_new(NULL
);
2286 /* Execute configuration file */
2287 ret
= config_from_file(vty
, confp
, &line_num
);
2289 /* Flush any previous errors before printing messages below */
2290 buffer_flush_all(vty
->obuf
, vty
->wfd
);
2292 if (!((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
))) {
2293 const char *message
= NULL
;
2297 case CMD_ERR_AMBIGUOUS
:
2298 message
= "Ambiguous command";
2300 case CMD_ERR_NO_MATCH
:
2301 message
= "No such command";
2304 message
= "Command returned Warning";
2306 case CMD_WARNING_CONFIG_FAILED
:
2307 message
= "Command returned Warning Config Failed";
2309 case CMD_ERR_INCOMPLETE
:
2310 message
= "Command returned Incomplete";
2312 case CMD_ERR_EXEED_ARGC_MAX
:
2314 "Command exceeded maximum number of Arguments";
2317 message
= "Command returned unhandled error message";
2321 for (ALL_LIST_ELEMENTS_RO(vty
->error
, node
, ve
)) {
2322 nl
= strchr(ve
->error_buf
, '\n');
2325 flog_err(EC_LIB_VTY
, "ERROR: %s on config line %u: %s",
2326 message
, ve
->line_num
, ve
->error_buf
);
2331 * Automatically commit the candidate configuration after
2332 * reading the configuration file.
2334 if (config
== NULL
&& vty
->candidate_config
2335 && frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
2336 ret
= nb_candidate_commit(vty
->candidate_config
, NB_CLIENT_CLI
,
2337 vty
, true, "Read configuration file",
2339 if (ret
!= NB_OK
&& ret
!= NB_ERR_NO_CHANGES
)
2340 zlog_err("%s: failed to read configuration file.",
2347 static FILE *vty_use_backup_config(const char *fullpath
)
2349 char *fullpath_sav
, *fullpath_tmp
;
2355 size_t fullpath_sav_sz
= strlen(fullpath
) + strlen(CONF_BACKUP_EXT
) + 1;
2356 fullpath_sav
= malloc(fullpath_sav_sz
);
2357 strlcpy(fullpath_sav
, fullpath
, fullpath_sav_sz
);
2358 strlcat(fullpath_sav
, CONF_BACKUP_EXT
, fullpath_sav_sz
);
2360 sav
= open(fullpath_sav
, O_RDONLY
);
2366 fullpath_tmp
= malloc(strlen(fullpath
) + 8);
2367 sprintf(fullpath_tmp
, "%s.XXXXXX", fullpath
);
2369 /* Open file to configuration write. */
2370 tmp
= mkstemp(fullpath_tmp
);
2374 if (fchmod(tmp
, CONFIGFILE_MASK
) != 0)
2377 while ((c
= read(sav
, buffer
, 512)) > 0) {
2378 if (write(tmp
, buffer
, c
) <= 0)
2384 if (rename(fullpath_tmp
, fullpath
) == 0)
2385 ret
= fopen(fullpath
, "r");
2387 unlink(fullpath_tmp
);
2392 unlink(fullpath_tmp
);
2402 /* Read up configuration file from file_name. */
2403 bool vty_read_config(struct nb_config
*config
, const char *config_file
,
2404 char *config_default_dir
)
2406 char cwd
[MAXPATHLEN
];
2408 const char *fullpath
;
2410 bool read_success
= false;
2412 /* If -f flag specified. */
2413 if (config_file
!= NULL
) {
2414 if (!IS_DIRECTORY_SEP(config_file
[0])) {
2415 if (getcwd(cwd
, MAXPATHLEN
) == NULL
) {
2418 "%s: failure to determine Current Working Directory %d!",
2420 goto tmp_free_and_out
;
2422 tmp
= XMALLOC(MTYPE_TMP
,
2423 strlen(cwd
) + strlen(config_file
) + 2);
2424 sprintf(tmp
, "%s/%s", cwd
, config_file
);
2427 fullpath
= config_file
;
2429 confp
= fopen(fullpath
, "r");
2431 if (confp
== NULL
) {
2433 EC_LIB_BACKUP_CONFIG
,
2434 "%s: failed to open configuration file %s: %s, checking backup",
2435 __func__
, fullpath
, safe_strerror(errno
));
2437 confp
= vty_use_backup_config(fullpath
);
2440 EC_LIB_BACKUP_CONFIG
,
2441 "WARNING: using backup configuration file!");
2445 "%s: can't open configuration file [%s]",
2446 __func__
, config_file
);
2447 goto tmp_free_and_out
;
2452 host_config_set(config_default_dir
);
2456 struct stat conf_stat
;
2458 /* !!!!PLEASE LEAVE!!!!
2459 * This is NEEDED for use with vtysh -b, or else you can get
2460 * a real configuration food fight with a lot garbage in the
2461 * merged configuration file it creates coming from the per
2462 * daemon configuration files. This also allows the daemons
2463 * to start if there default configuration file is not
2464 * present or ignore them, as needed when using vtysh -b to
2465 * configure the daemons at boot - MAG
2468 /* Stat for vtysh Zebra.conf, if found startup and wait for
2469 * boot configuration
2472 if (strstr(config_default_dir
, "vtysh") == NULL
) {
2473 ret
= stat(integrate_default
, &conf_stat
);
2475 read_success
= true;
2476 goto tmp_free_and_out
;
2480 confp
= fopen(config_default_dir
, "r");
2481 if (confp
== NULL
) {
2484 "%s: failed to open configuration file %s: %s, checking backup",
2485 __func__
, config_default_dir
,
2486 safe_strerror(errno
));
2488 confp
= vty_use_backup_config(config_default_dir
);
2491 EC_LIB_BACKUP_CONFIG
,
2492 "WARNING: using backup configuration file!");
2493 fullpath
= config_default_dir
;
2495 flog_err(EC_LIB_VTY
,
2496 "can't open configuration file [%s]",
2497 config_default_dir
);
2498 goto tmp_free_and_out
;
2501 fullpath
= config_default_dir
;
2504 vty_read_file(config
, confp
);
2505 read_success
= true;
2509 host_config_set(fullpath
);
2512 XFREE(MTYPE_TMP
, tmp
);
2514 return read_success
;
2517 /* Small utility function which output log to the VTY. */
2518 void vty_log(const char *level
, const char *proto_str
, const char *msg
,
2519 struct timestamp_control
*ctl
)
2527 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2528 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
)
2530 vty_log_out(vty
, level
, proto_str
, msg
, ctl
);
2533 /* Async-signal-safe version of vty_log for fixed strings. */
2534 void vty_log_fixed(char *buf
, size_t len
)
2537 struct iovec iov
[2];
2538 char crlf
[4] = "\r\n";
2540 /* vty may not have been initialised */
2544 iov
[0].iov_base
= buf
;
2545 iov
[0].iov_len
= len
;
2546 iov
[1].iov_base
= crlf
;
2549 for (i
= 0; i
< vector_active(vtyvec
); i
++) {
2551 if (((vty
= vector_slot(vtyvec
, i
)) != NULL
) && vty
->monitor
)
2552 /* N.B. We don't care about the return code, since
2554 most likely just about to die anyway. */
2555 if (writev(vty
->wfd
, iov
, 2) == -1) {
2556 fprintf(stderr
, "Failure to writev: %d\n",
2563 int vty_config_enter(struct vty
*vty
, bool private_config
, bool exclusive
)
2565 if (exclusive
&& nb_running_lock(NB_CLIENT_CLI
, vty
)) {
2566 vty_out(vty
, "%% Configuration is locked by other client\n");
2570 vty
->node
= CONFIG_NODE
;
2572 vty
->private_config
= private_config
;
2573 vty
->xpath_index
= 0;
2575 pthread_rwlock_rdlock(&running_config
->lock
);
2577 if (private_config
) {
2578 vty
->candidate_config
= nb_config_dup(running_config
);
2579 vty
->candidate_config_base
=
2580 nb_config_dup(running_config
);
2582 "Warning: uncommitted changes will be discarded on exit.\n\n");
2584 vty
->candidate_config
= vty_shared_candidate_config
;
2585 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
)
2586 vty
->candidate_config_base
=
2587 nb_config_dup(running_config
);
2590 pthread_rwlock_unlock(&running_config
->lock
);
2595 void vty_config_exit(struct vty
*vty
)
2597 /* Check if there's a pending confirmed commit. */
2598 if (vty
->t_confirmed_commit_timeout
) {
2600 "WARNING: exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
2601 nb_cli_confirmed_commit_rollback(vty
);
2602 nb_cli_confirmed_commit_clean(vty
);
2605 (void)nb_running_unlock(NB_CLIENT_CLI
, vty
);
2607 if (vty
->candidate_config
) {
2608 if (vty
->private_config
)
2609 nb_config_free(vty
->candidate_config
);
2610 vty
->candidate_config
= NULL
;
2612 if (vty
->candidate_config_base
) {
2613 nb_config_free(vty
->candidate_config_base
);
2614 vty
->candidate_config_base
= NULL
;
2617 vty
->config
= false;
2620 /* Master of the threads. */
2621 static struct thread_master
*vty_master
;
2623 static void vty_event(enum event event
, int sock
, struct vty
*vty
)
2625 struct thread
*vty_serv_thread
= NULL
;
2629 vty_serv_thread
= thread_add_read(vty_master
, vty_accept
, vty
,
2631 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2635 vty_serv_thread
= thread_add_read(vty_master
, vtysh_accept
, vty
,
2637 vector_set_index(Vvty_serv_thread
, sock
, vty_serv_thread
);
2640 thread_add_read(vty_master
, vtysh_read
, vty
, sock
,
2644 thread_add_write(vty_master
, vtysh_write
, vty
, sock
,
2649 thread_add_read(vty_master
, vty_read
, vty
, sock
, &vty
->t_read
);
2651 /* Time out treatment. */
2652 if (vty
->v_timeout
) {
2653 THREAD_OFF(vty
->t_timeout
);
2654 thread_add_timer(vty_master
, vty_timeout
, vty
,
2655 vty
->v_timeout
, &vty
->t_timeout
);
2659 thread_add_write(vty_master
, vty_flush
, vty
, sock
,
2662 case VTY_TIMEOUT_RESET
:
2663 THREAD_OFF(vty
->t_timeout
);
2665 thread_add_timer(vty_master
, vty_timeout
, vty
,
2666 vty
->v_timeout
, &vty
->t_timeout
);
2671 DEFUN_NOSH (config_who
,
2674 "Display who is on vty\n")
2679 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2680 if ((v
= vector_slot(vtyvec
, i
)) != NULL
)
2681 vty_out(vty
, "%svty[%d] connected from %s.\n",
2682 v
->config
? "*" : " ", i
, v
->address
);
2686 /* Move to vty configuration mode. */
2687 DEFUN_NOSH (line_vty
,
2690 "Configure a terminal line\n"
2691 "Virtual terminal\n")
2693 vty
->node
= VTY_NODE
;
2697 /* Set time out value. */
2698 static int exec_timeout(struct vty
*vty
, const char *min_str
,
2699 const char *sec_str
)
2701 unsigned long timeout
= 0;
2703 /* min_str and sec_str are already checked by parser. So it must be
2704 all digit string. */
2706 timeout
= strtol(min_str
, NULL
, 10);
2710 timeout
+= strtol(sec_str
, NULL
, 10);
2712 vty_timeout_val
= timeout
;
2713 vty
->v_timeout
= timeout
;
2714 vty_event(VTY_TIMEOUT_RESET
, 0, vty
);
2720 DEFUN (exec_timeout_min
,
2721 exec_timeout_min_cmd
,
2722 "exec-timeout (0-35791)",
2723 "Set timeout value\n"
2724 "Timeout value in minutes\n")
2727 return exec_timeout(vty
, argv
[idx_number
]->arg
, NULL
);
2730 DEFUN (exec_timeout_sec
,
2731 exec_timeout_sec_cmd
,
2732 "exec-timeout (0-35791) (0-2147483)",
2733 "Set the EXEC timeout\n"
2734 "Timeout in minutes\n"
2735 "Timeout in seconds\n")
2738 int idx_number_2
= 2;
2739 return exec_timeout(vty
, argv
[idx_number
]->arg
,
2740 argv
[idx_number_2
]->arg
);
2743 DEFUN (no_exec_timeout
,
2744 no_exec_timeout_cmd
,
2747 "Set the EXEC timeout\n")
2749 return exec_timeout(vty
, NULL
, NULL
);
2752 /* Set vty access class. */
2753 DEFUN (vty_access_class
,
2754 vty_access_class_cmd
,
2755 "access-class WORD",
2756 "Filter connections based on an IP access list\n"
2760 if (vty_accesslist_name
)
2761 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2763 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2768 /* Clear vty access class. */
2769 DEFUN (no_vty_access_class
,
2770 no_vty_access_class_cmd
,
2771 "no access-class [WORD]",
2773 "Filter connections based on an IP access list\n"
2777 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2778 if (!vty_accesslist_name
2779 || (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
))) {
2780 vty_out(vty
, "Access-class is not currently applied to vty\n");
2781 return CMD_WARNING_CONFIG_FAILED
;
2784 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2786 vty_accesslist_name
= NULL
;
2791 /* Set vty access class. */
2792 DEFUN (vty_ipv6_access_class
,
2793 vty_ipv6_access_class_cmd
,
2794 "ipv6 access-class WORD",
2796 "Filter connections based on an IP access list\n"
2797 "IPv6 access list\n")
2800 if (vty_ipv6_accesslist_name
)
2801 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2803 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2808 /* Clear vty access class. */
2809 DEFUN (no_vty_ipv6_access_class
,
2810 no_vty_ipv6_access_class_cmd
,
2811 "no ipv6 access-class [WORD]",
2814 "Filter connections based on an IP access list\n"
2815 "IPv6 access list\n")
2818 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2820 if (!vty_ipv6_accesslist_name
2821 || (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
))) {
2823 "IPv6 access-class is not currently applied to vty\n");
2824 return CMD_WARNING_CONFIG_FAILED
;
2827 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2829 vty_ipv6_accesslist_name
= NULL
;
2838 "Enable password checking\n")
2840 no_password_check
= 0;
2844 DEFUN (no_vty_login
,
2848 "Enable password checking\n")
2850 no_password_check
= 1;
2854 DEFUN (service_advanced_vty
,
2855 service_advanced_vty_cmd
,
2856 "service advanced-vty",
2857 "Set up miscellaneous service\n"
2858 "Enable advanced mode vty interface\n")
2864 DEFUN (no_service_advanced_vty
,
2865 no_service_advanced_vty_cmd
,
2866 "no service advanced-vty",
2868 "Set up miscellaneous service\n"
2869 "Enable advanced mode vty interface\n")
2875 DEFUN_NOSH (terminal_monitor
,
2876 terminal_monitor_cmd
,
2878 "Set terminal line parameters\n"
2879 "Copy debug output to the current terminal line\n")
2885 DEFUN_NOSH (terminal_no_monitor
,
2886 terminal_no_monitor_cmd
,
2887 "terminal no monitor",
2888 "Set terminal line parameters\n"
2890 "Copy debug output to the current terminal line\n")
2896 DEFUN_NOSH (no_terminal_monitor
,
2897 no_terminal_monitor_cmd
,
2898 "no terminal monitor",
2900 "Set terminal line parameters\n"
2901 "Copy debug output to the current terminal line\n")
2903 return terminal_no_monitor(self
, vty
, argc
, argv
);
2907 DEFUN_NOSH (show_history
,
2911 "Display the session command history\n")
2915 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;) {
2916 if (index
== VTY_MAXHIST
) {
2921 if (vty
->hist
[index
] != NULL
)
2922 vty_out(vty
, " %s\n", vty
->hist
[index
]);
2931 DEFPY (log_commands
,
2933 "[no] log commands",
2936 "Log all commands\n")
2939 if (do_log_commands_perm
) {
2941 "Daemon started with permanent logging turned on for commands, ignoring\n");
2945 do_log_commands
= false;
2947 do_log_commands
= true;
2952 /* Display current configuration. */
2953 static int vty_config_write(struct vty
*vty
)
2955 vty_out(vty
, "line vty\n");
2957 if (vty_accesslist_name
)
2958 vty_out(vty
, " access-class %s\n", vty_accesslist_name
);
2960 if (vty_ipv6_accesslist_name
)
2961 vty_out(vty
, " ipv6 access-class %s\n",
2962 vty_ipv6_accesslist_name
);
2965 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2966 vty_out(vty
, " exec-timeout %ld %ld\n", vty_timeout_val
/ 60,
2967 vty_timeout_val
% 60);
2970 if (no_password_check
)
2971 vty_out(vty
, " no login\n");
2973 if (do_log_commands
)
2974 vty_out(vty
, "log commands\n");
2976 vty_out(vty
, "!\n");
2981 struct cmd_node vty_node
= {
2982 VTY_NODE
, "%s(config-line)# ", 1,
2985 /* Reset all VTY status. */
2986 void vty_reset(void)
2990 struct thread
*vty_serv_thread
;
2992 for (i
= 0; i
< vector_active(vtyvec
); i
++)
2993 if ((vty
= vector_slot(vtyvec
, i
)) != NULL
) {
2994 buffer_reset(vty
->lbuf
);
2995 buffer_reset(vty
->obuf
);
2996 vty
->status
= VTY_CLOSE
;
3000 for (i
= 0; i
< vector_active(Vvty_serv_thread
); i
++)
3001 if ((vty_serv_thread
= vector_slot(Vvty_serv_thread
, i
))
3003 THREAD_OFF(vty_serv_thread
);
3004 vector_slot(Vvty_serv_thread
, i
) = NULL
;
3008 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3010 if (vty_accesslist_name
) {
3011 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3012 vty_accesslist_name
= NULL
;
3015 if (vty_ipv6_accesslist_name
) {
3016 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3017 vty_ipv6_accesslist_name
= NULL
;
3021 static void vty_save_cwd(void)
3025 c
= getcwd(vty_cwd
, sizeof(vty_cwd
));
3029 * At this point if these go wrong, more than likely
3030 * the whole world is coming down around us
3031 * Hence not worrying about it too much.
3033 if (!chdir(SYSCONFDIR
)) {
3034 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3035 "Failure to chdir to %s, errno: %d",
3039 if (getcwd(vty_cwd
, sizeof(vty_cwd
)) == NULL
) {
3040 flog_err_sys(EC_LIB_SYSTEM_CALL
,
3041 "Failure to getcwd, errno: %d", errno
);
3047 char *vty_get_cwd(void)
3052 int vty_shell(struct vty
*vty
)
3054 return vty
->type
== VTY_SHELL
? 1 : 0;
3057 int vty_shell_serv(struct vty
*vty
)
3059 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3062 void vty_init_vtysh(void)
3064 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3067 /* Install vty's own commands like `who' command. */
3068 void vty_init(struct thread_master
*master_thread
, bool do_command_logging
)
3070 /* For further configuration read, preserve current directory. */
3073 vtyvec
= vector_init(VECTOR_MIN_SIZE
);
3075 vty_master
= master_thread
;
3077 atexit(vty_stdio_atexit
);
3079 /* Initilize server thread vector. */
3080 Vvty_serv_thread
= vector_init(VECTOR_MIN_SIZE
);
3082 /* Install bgp top node. */
3083 install_node(&vty_node
, vty_config_write
);
3085 install_element(VIEW_NODE
, &config_who_cmd
);
3086 install_element(VIEW_NODE
, &show_history_cmd
);
3087 install_element(CONFIG_NODE
, &line_vty_cmd
);
3088 install_element(CONFIG_NODE
, &service_advanced_vty_cmd
);
3089 install_element(CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3090 install_element(CONFIG_NODE
, &show_history_cmd
);
3091 install_element(CONFIG_NODE
, &log_commands_cmd
);
3093 if (do_command_logging
) {
3094 do_log_commands
= true;
3095 do_log_commands_perm
= true;
3098 install_element(ENABLE_NODE
, &terminal_monitor_cmd
);
3099 install_element(ENABLE_NODE
, &terminal_no_monitor_cmd
);
3100 install_element(ENABLE_NODE
, &no_terminal_monitor_cmd
);
3102 install_default(VTY_NODE
);
3103 install_element(VTY_NODE
, &exec_timeout_min_cmd
);
3104 install_element(VTY_NODE
, &exec_timeout_sec_cmd
);
3105 install_element(VTY_NODE
, &no_exec_timeout_cmd
);
3106 install_element(VTY_NODE
, &vty_access_class_cmd
);
3107 install_element(VTY_NODE
, &no_vty_access_class_cmd
);
3108 install_element(VTY_NODE
, &vty_login_cmd
);
3109 install_element(VTY_NODE
, &no_vty_login_cmd
);
3110 install_element(VTY_NODE
, &vty_ipv6_access_class_cmd
);
3111 install_element(VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3114 void vty_terminate(void)
3116 memset(vty_cwd
, 0x00, sizeof(vty_cwd
));
3118 if (vtyvec
&& Vvty_serv_thread
) {
3120 vector_free(vtyvec
);
3121 vector_free(Vvty_serv_thread
);
3123 Vvty_serv_thread
= NULL
;