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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <lib/version.h>
30 #include "sockunion.h"
40 #include <arpa/telnet.h>
56 static void vty_event (enum event
, int, struct vty
*);
58 /* Extern host structure from command.c */
59 extern struct host host
;
61 /* Vector which store each vty structure. */
64 /* Vty timeout value. */
65 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
67 /* Vty access-class command */
68 static char *vty_accesslist_name
= NULL
;
70 /* Vty access-calss for IPv6. */
71 static char *vty_ipv6_accesslist_name
= NULL
;
73 /* VTY server thread. */
74 static vector Vvty_serv_thread
;
76 /* Current directory. */
80 static int vty_config
;
82 /* Login password check. */
83 static int no_password_check
= 0;
85 /* Restrict unauthenticated logins? */
86 static const u_char restricted_mode_default
= 0;
87 static u_char restricted_mode
= 0;
89 /* Integrated configuration file path */
90 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
93 /* VTY standard output function. */
95 vty_out (struct vty
*vty
, const char *format
, ...)
105 va_start (args
, format
);
106 vprintf (format
, args
);
111 /* Try to write to initial buffer. */
112 va_start (args
, format
);
113 len
= vsnprintf (buf
, sizeof buf
, format
, args
);
116 /* Initial buffer is not enough. */
117 if (len
< 0 || len
>= size
)
126 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
130 va_start (args
, format
);
131 len
= vsnprintf (p
, size
, format
, args
);
134 if (len
> -1 && len
< size
)
139 /* When initial buffer is enough to store all output. */
143 /* Pointer p must point out buffer. */
144 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
146 /* If p is not different with buf, it is allocated buffer. */
148 XFREE (MTYPE_VTY_OUT_BUF
, p
);
155 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
156 const char *format
, struct timestamp_control
*ctl
, va_list va
)
162 if (!ctl
->already_rendered
)
164 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
165 ctl
->already_rendered
= 1;
167 if (ctl
->len
+1 >= sizeof(buf
))
169 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
174 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
176 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
177 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
180 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
181 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
187 if (write(vty
->fd
, buf
, len
) < 0)
189 if (ERRNO_IO_RETRY(errno
))
190 /* Kernel buffer is full, probably too much debugging output, so just
191 drop the data and ignore. */
193 /* Fatal I/O error. */
194 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
195 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
196 __func__
, vty
->fd
, safe_strerror(errno
));
197 buffer_reset(vty
->obuf
);
198 /* cannot call vty_close, because a parent routine may still try
199 to access the vty struct */
200 vty
->status
= VTY_CLOSE
;
201 shutdown(vty
->fd
, SHUT_RDWR
);
207 /* Output current time to the vty. */
209 vty_time_print (struct vty
*vty
, int cr
)
211 char buf
[QUAGGA_TIMESTAMP_LEN
];
213 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
215 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
219 vty_out (vty
, "%s\n", buf
);
221 vty_out (vty
, "%s ", buf
);
226 /* Say hello to vty interface. */
228 vty_hello (struct vty
*vty
)
235 f
= fopen (host
.motdfile
, "r");
238 while (fgets (buf
, sizeof (buf
), f
))
241 /* work backwards to ignore trailling isspace() */
242 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
245 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
250 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
253 vty_out (vty
, "%s", host
.motd
);
256 /* Put out prompt and wait input from user. */
258 vty_prompt (struct vty
*vty
)
260 struct utsname names
;
263 if (vty
->type
== VTY_TERM
)
265 hostname
= host
.name
;
269 hostname
= names
.nodename
;
271 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
275 /* Send WILL TELOPT_ECHO to remote server. */
277 vty_will_echo (struct vty
*vty
)
279 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
280 vty_out (vty
, "%s", cmd
);
283 /* Make suppress Go-Ahead telnet option. */
285 vty_will_suppress_go_ahead (struct vty
*vty
)
287 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
288 vty_out (vty
, "%s", cmd
);
291 /* Make don't use linemode over telnet. */
293 vty_dont_linemode (struct vty
*vty
)
295 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
296 vty_out (vty
, "%s", cmd
);
299 /* Use window size. */
301 vty_do_window_size (struct vty
*vty
)
303 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
304 vty_out (vty
, "%s", cmd
);
307 #if 0 /* Currently not used. */
308 /* Make don't use lflow vty interface. */
310 vty_dont_lflow_ahead (struct vty
*vty
)
312 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
313 vty_out (vty
, "%s", cmd
);
317 /* Allocate new vty struct. */
321 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
323 new->obuf
= buffer_new(0); /* Use default buffer size. */
324 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
325 new->error_buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
326 new->max
= VTY_BUFSIZ
;
331 /* Authentication of vty */
333 vty_auth (struct vty
*vty
, char *buf
)
336 enum node_type next_node
= 0;
338 char *crypt (const char *, const char *);
344 passwd
= host
.password_encrypt
;
346 passwd
= host
.password
;
348 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
350 next_node
= VIEW_NODE
;
352 case AUTH_ENABLE_NODE
:
354 passwd
= host
.enable_encrypt
;
356 passwd
= host
.enable
;
357 next_node
= ENABLE_NODE
;
364 fail
= strcmp (crypt(buf
, passwd
), passwd
);
366 fail
= strcmp (buf
, passwd
);
374 vty
->node
= next_node
; /* Success ! */
381 if (vty
->node
== AUTH_NODE
)
383 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
384 vty
->status
= VTY_CLOSE
;
388 /* AUTH_ENABLE_NODE */
390 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
391 vty
->node
= restricted_mode
? RESTRICTED_NODE
: VIEW_NODE
;
397 /* Command execution over the vty interface. */
399 vty_command (struct vty
*vty
, char *buf
)
403 const char *protocolname
;
405 /* Split readline string up into the vector */
406 vline
= cmd_make_strvec (buf
);
411 #ifdef CONSUMED_TIME_CHECK
415 unsigned long realtime
, cputime
;
418 #endif /* CONSUMED_TIME_CHECK */
420 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
422 /* Get the name of the protocol if any */
424 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
426 protocolname
= zlog_proto_names
[ZLOG_NONE
];
428 #ifdef CONSUMED_TIME_CHECK
430 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
432 /* Warn about CPU hog that must be fixed. */
433 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
434 realtime
/1000, cputime
/1000, buf
);
436 #endif /* CONSUMED_TIME_CHECK */
438 if (ret
!= CMD_SUCCESS
)
442 if (vty
->type
== VTY_FILE
)
443 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
445 case CMD_ERR_AMBIGUOUS
:
446 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
448 case CMD_ERR_NO_MATCH
:
449 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
451 case CMD_ERR_INCOMPLETE
:
452 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
455 cmd_free_strvec (vline
);
460 static const char telnet_backward_char
= 0x08;
461 static const char telnet_space_char
= ' ';
463 /* Basic function to write buffer to vty. */
465 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
467 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
470 /* Should we do buffering here ? And make vty_flush (vty) ? */
471 buffer_put (vty
->obuf
, buf
, nbytes
);
474 /* Ensure length of input buffer. Is buffer is short, double it. */
476 vty_ensure (struct vty
*vty
, int length
)
478 if (vty
->max
<= length
)
481 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
482 vty
->error_buf
= XREALLOC (MTYPE_VTY
, vty
->error_buf
, vty
->max
);
486 /* Basic function to insert character into vty. */
488 vty_self_insert (struct vty
*vty
, char c
)
493 vty_ensure (vty
, vty
->length
+ 1);
494 length
= vty
->length
- vty
->cp
;
495 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
496 vty
->buf
[vty
->cp
] = c
;
498 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
499 for (i
= 0; i
< length
; i
++)
500 vty_write (vty
, &telnet_backward_char
, 1);
506 /* Self insert character 'c' in overwrite mode. */
508 vty_self_insert_overwrite (struct vty
*vty
, char c
)
510 vty_ensure (vty
, vty
->length
+ 1);
511 vty
->buf
[vty
->cp
++] = c
;
513 if (vty
->cp
> vty
->length
)
516 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
519 vty_write (vty
, &c
, 1);
522 /* Insert a word into vty interface with overwrite mode. */
524 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
526 int len
= strlen (str
);
527 vty_write (vty
, str
, len
);
528 strcpy (&vty
->buf
[vty
->cp
], str
);
530 vty
->length
= vty
->cp
;
533 /* Forward character. */
535 vty_forward_char (struct vty
*vty
)
537 if (vty
->cp
< vty
->length
)
539 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
544 /* Backward character. */
546 vty_backward_char (struct vty
*vty
)
551 vty_write (vty
, &telnet_backward_char
, 1);
555 /* Move to the beginning of the line. */
557 vty_beginning_of_line (struct vty
*vty
)
560 vty_backward_char (vty
);
563 /* Move to the end of the line. */
565 vty_end_of_line (struct vty
*vty
)
567 while (vty
->cp
< vty
->length
)
568 vty_forward_char (vty
);
571 static void vty_kill_line_from_beginning (struct vty
*);
572 static void vty_redraw_line (struct vty
*);
574 /* Print command line history. This function is called from
575 vty_next_line and vty_previous_line. */
577 vty_history_print (struct vty
*vty
)
581 vty_kill_line_from_beginning (vty
);
583 /* Get previous line from history buffer */
584 length
= strlen (vty
->hist
[vty
->hp
]);
585 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
586 vty
->cp
= vty
->length
= length
;
588 /* Redraw current line */
589 vty_redraw_line (vty
);
592 /* Show next command line history. */
594 vty_next_line (struct vty
*vty
)
598 if (vty
->hp
== vty
->hindex
)
601 /* Try is there history exist or not. */
603 if (try_index
== (VTY_MAXHIST
- 1))
608 /* If there is not history return. */
609 if (vty
->hist
[try_index
] == NULL
)
614 vty_history_print (vty
);
617 /* Show previous command line history. */
619 vty_previous_line (struct vty
*vty
)
625 try_index
= VTY_MAXHIST
- 1;
629 if (vty
->hist
[try_index
] == NULL
)
634 vty_history_print (vty
);
637 /* This function redraw all of the command line character. */
639 vty_redraw_line (struct vty
*vty
)
641 vty_write (vty
, vty
->buf
, vty
->length
);
642 vty
->cp
= vty
->length
;
647 vty_forward_word (struct vty
*vty
)
649 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
650 vty_forward_char (vty
);
652 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
653 vty_forward_char (vty
);
656 /* Backward word without skipping training space. */
658 vty_backward_pure_word (struct vty
*vty
)
660 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
661 vty_backward_char (vty
);
666 vty_backward_word (struct vty
*vty
)
668 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
669 vty_backward_char (vty
);
671 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
672 vty_backward_char (vty
);
675 /* When '^D' is typed at the beginning of the line we move to the down
678 vty_down_level (struct vty
*vty
)
680 vty_out (vty
, "%s", VTY_NEWLINE
);
681 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
686 /* When '^Z' is received from vty, move down to the enable mode. */
688 vty_end_config (struct vty
*vty
)
690 vty_out (vty
, "%s", VTY_NEWLINE
);
696 case RESTRICTED_NODE
:
716 case KEYCHAIN_KEY_NODE
:
720 vty_config_unlock (vty
);
721 vty
->node
= ENABLE_NODE
;
724 /* Unknown node, we have to ignore it. */
732 /* Delete a charcter at the current point. */
734 vty_delete_char (struct vty
*vty
)
739 if (vty
->length
== 0)
741 vty_down_level (vty
);
745 if (vty
->cp
== vty
->length
)
746 return; /* completion need here? */
748 size
= vty
->length
- vty
->cp
;
751 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
752 vty
->buf
[vty
->length
] = '\0';
754 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
757 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
758 vty_write (vty
, &telnet_space_char
, 1);
760 for (i
= 0; i
< size
; i
++)
761 vty_write (vty
, &telnet_backward_char
, 1);
764 /* Delete a character before the point. */
766 vty_delete_backward_char (struct vty
*vty
)
771 vty_backward_char (vty
);
772 vty_delete_char (vty
);
775 /* Kill rest of line from current point. */
777 vty_kill_line (struct vty
*vty
)
782 size
= vty
->length
- vty
->cp
;
787 for (i
= 0; i
< size
; i
++)
788 vty_write (vty
, &telnet_space_char
, 1);
789 for (i
= 0; i
< size
; i
++)
790 vty_write (vty
, &telnet_backward_char
, 1);
792 memset (&vty
->buf
[vty
->cp
], 0, size
);
793 vty
->length
= vty
->cp
;
796 /* Kill line from the beginning. */
798 vty_kill_line_from_beginning (struct vty
*vty
)
800 vty_beginning_of_line (vty
);
804 /* Delete a word before the point. */
806 vty_forward_kill_word (struct vty
*vty
)
808 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
809 vty_delete_char (vty
);
810 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
811 vty_delete_char (vty
);
814 /* Delete a word before the point. */
816 vty_backward_kill_word (struct vty
*vty
)
818 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
819 vty_delete_backward_char (vty
);
820 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
821 vty_delete_backward_char (vty
);
824 /* Transpose chars before or at the point. */
826 vty_transpose_chars (struct vty
*vty
)
830 /* If length is short or point is near by the beginning of line then
832 if (vty
->length
< 2 || vty
->cp
< 1)
835 /* In case of point is located at the end of the line. */
836 if (vty
->cp
== vty
->length
)
838 c1
= vty
->buf
[vty
->cp
- 1];
839 c2
= vty
->buf
[vty
->cp
- 2];
841 vty_backward_char (vty
);
842 vty_backward_char (vty
);
843 vty_self_insert_overwrite (vty
, c1
);
844 vty_self_insert_overwrite (vty
, c2
);
848 c1
= vty
->buf
[vty
->cp
];
849 c2
= vty
->buf
[vty
->cp
- 1];
851 vty_backward_char (vty
);
852 vty_self_insert_overwrite (vty
, c1
);
853 vty_self_insert_overwrite (vty
, c2
);
857 /* Do completion at vty interface. */
859 vty_complete_command (struct vty
*vty
)
863 char **matched
= NULL
;
866 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
869 vline
= cmd_make_strvec (vty
->buf
);
873 /* In case of 'help \t'. */
874 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
875 vector_set (vline
, NULL
);
877 matched
= cmd_complete_command_lib (vline
, vty
, &ret
, 1);
879 cmd_free_strvec (vline
);
881 vty_out (vty
, "%s", VTY_NEWLINE
);
884 case CMD_ERR_AMBIGUOUS
:
885 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
887 vty_redraw_line (vty
);
889 case CMD_ERR_NO_MATCH
:
890 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
892 vty_redraw_line (vty
);
894 case CMD_COMPLETE_FULL_MATCH
:
896 vty_redraw_line (vty
);
897 vty_backward_pure_word (vty
);
898 vty_insert_word_overwrite (vty
, matched
[0]);
899 vty_self_insert (vty
, ' ');
900 XFREE (MTYPE_TMP
, matched
[0]);
902 case CMD_COMPLETE_MATCH
:
904 vty_redraw_line (vty
);
905 vty_backward_pure_word (vty
);
906 vty_insert_word_overwrite (vty
, matched
[0]);
907 XFREE (MTYPE_TMP
, matched
[0]);
908 vector_only_index_free (matched
);
911 case CMD_COMPLETE_LIST_MATCH
:
912 for (i
= 0; matched
[i
] != NULL
; i
++)
914 if (i
!= 0 && ((i
% 6) == 0))
915 vty_out (vty
, "%s", VTY_NEWLINE
);
916 vty_out (vty
, "%-10s ", matched
[i
]);
917 XFREE (MTYPE_TMP
, matched
[i
]);
919 vty_out (vty
, "%s", VTY_NEWLINE
);
922 vty_redraw_line (vty
);
924 case CMD_ERR_NOTHING_TODO
:
926 vty_redraw_line (vty
);
932 vector_only_index_free (matched
);
936 vty_describe_fold (struct vty
*vty
, int cmd_width
,
937 unsigned int desc_width
, struct cmd_token
*token
)
943 cmd
= token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
;
947 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
951 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
953 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
955 for (pos
= desc_width
; pos
> 0; pos
--)
956 if (*(p
+ pos
) == ' ')
962 strncpy (buf
, p
, pos
);
964 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
969 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
971 XFREE (MTYPE_TMP
, buf
);
974 /* Describe matched command function. */
976 vty_describe_command (struct vty
*vty
)
981 unsigned int i
, width
, desc_width
;
982 struct cmd_token
*token
, *token_cr
= NULL
;
984 vline
= cmd_make_strvec (vty
->buf
);
986 /* In case of '> ?'. */
989 vline
= vector_init (1);
990 vector_set (vline
, NULL
);
993 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
994 vector_set (vline
, NULL
);
996 describe
= cmd_describe_command (vline
, vty
, &ret
);
998 vty_out (vty
, "%s", VTY_NEWLINE
);
1000 /* Ambiguous error. */
1003 case CMD_ERR_AMBIGUOUS
:
1004 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1007 case CMD_ERR_NO_MATCH
:
1008 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1013 /* Get width of command string. */
1015 for (i
= 0; i
< vector_active (describe
); i
++)
1016 if ((token
= vector_slot (describe
, i
)) != NULL
)
1020 if (token
->cmd
[0] == '\0')
1023 len
= strlen (token
->cmd
);
1024 if (token
->cmd
[0] == '.')
1031 /* Get width of description string. */
1032 desc_width
= vty
->width
- (width
+ 6);
1034 /* Print out description. */
1035 for (i
= 0; i
< vector_active (describe
); i
++)
1036 if ((token
= vector_slot (describe
, i
)) != NULL
)
1038 if (token
->cmd
[0] == '\0')
1041 if (strcmp (token
->cmd
, command_cr
) == 0)
1048 vty_out (vty
, " %-s%s",
1049 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1051 else if (desc_width
>= strlen (token
->desc
))
1052 vty_out (vty
, " %-*s %s%s", width
,
1053 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1054 token
->desc
, VTY_NEWLINE
);
1056 vty_describe_fold (vty
, width
, desc_width
, token
);
1059 vty_out (vty
, " %-*s %s%s", width
1060 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1061 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1065 if ((token
= token_cr
))
1068 vty_out (vty
, " %-s%s",
1069 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1071 else if (desc_width
>= strlen (token
->desc
))
1072 vty_out (vty
, " %-*s %s%s", width
,
1073 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1074 token
->desc
, VTY_NEWLINE
);
1076 vty_describe_fold (vty
, width
, desc_width
, token
);
1080 cmd_free_strvec (vline
);
1082 vector_free (describe
);
1085 vty_redraw_line (vty
);
1089 vty_clear_buf (struct vty
*vty
)
1091 memset (vty
->buf
, 0, vty
->max
);
1094 /* ^C stop current input and do not add command line to the history. */
1096 vty_stop_input (struct vty
*vty
)
1098 vty
->cp
= vty
->length
= 0;
1099 vty_clear_buf (vty
);
1100 vty_out (vty
, "%s", VTY_NEWLINE
);
1106 case RESTRICTED_NODE
:
1107 /* Nothing to do. */
1110 case INTERFACE_NODE
:
1120 case KEYCHAIN_KEY_NODE
:
1124 vty_config_unlock (vty
);
1125 vty
->node
= ENABLE_NODE
;
1128 /* Unknown node, we have to ignore it. */
1133 /* Set history pointer to the latest one. */
1134 vty
->hp
= vty
->hindex
;
1137 /* Add current command line to the history buffer. */
1139 vty_hist_add (struct vty
*vty
)
1143 if (vty
->length
== 0)
1146 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1148 /* Ignore the same string as previous one. */
1149 if (vty
->hist
[index
])
1150 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1152 vty
->hp
= vty
->hindex
;
1156 /* Insert history entry. */
1157 if (vty
->hist
[vty
->hindex
])
1158 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1159 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1161 /* History index rotation. */
1163 if (vty
->hindex
== VTY_MAXHIST
)
1166 vty
->hp
= vty
->hindex
;
1169 /* #define TELNET_OPTION_DEBUG */
1171 /* Get telnet window size. */
1173 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1175 #ifdef TELNET_OPTION_DEBUG
1178 for (i
= 0; i
< nbytes
; i
++)
1183 vty_out (vty
, "IAC ");
1186 vty_out (vty
, "WILL ");
1189 vty_out (vty
, "WONT ");
1192 vty_out (vty
, "DO ");
1195 vty_out (vty
, "DONT ");
1198 vty_out (vty
, "SB ");
1201 vty_out (vty
, "SE ");
1204 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1207 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1210 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1213 vty_out (vty
, "%x ", buf
[i
]);
1217 vty_out (vty
, "%s", VTY_NEWLINE
);
1219 #endif /* TELNET_OPTION_DEBUG */
1225 vty
->iac_sb_in_progress
= 1;
1230 if (!vty
->iac_sb_in_progress
)
1233 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1235 vty
->iac_sb_in_progress
= 0;
1238 switch (vty
->sb_buf
[0])
1241 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1242 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1243 "should send %d characters, but we received %lu",
1244 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1245 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1246 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1247 "too small to handle the telnet NAWS option",
1248 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1251 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1252 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1253 #ifdef TELNET_OPTION_DEBUG
1254 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1255 "width %d, height %d%s",
1256 vty
->width
, vty
->height
, VTY_NEWLINE
);
1261 vty
->iac_sb_in_progress
= 0;
1271 /* Execute current command line. */
1273 vty_execute (struct vty
*vty
)
1282 case AUTH_ENABLE_NODE
:
1283 vty_auth (vty
, vty
->buf
);
1286 ret
= vty_command (vty
, vty
->buf
);
1287 if (vty
->type
== VTY_TERM
)
1292 /* Clear command line buffer. */
1293 vty
->cp
= vty
->length
= 0;
1294 vty_clear_buf (vty
);
1296 if (vty
->status
!= VTY_CLOSE
)
1302 #define CONTROL(X) ((X) - '@')
1303 #define VTY_NORMAL 0
1304 #define VTY_PRE_ESCAPE 1
1305 #define VTY_ESCAPE 2
1307 /* Escape character command map. */
1309 vty_escape_map (unsigned char c
, struct vty
*vty
)
1314 vty_previous_line (vty
);
1317 vty_next_line (vty
);
1320 vty_forward_char (vty
);
1323 vty_backward_char (vty
);
1329 /* Go back to normal mode. */
1330 vty
->escape
= VTY_NORMAL
;
1333 /* Quit print out to the buffer. */
1335 vty_buffer_reset (struct vty
*vty
)
1337 buffer_reset (vty
->obuf
);
1339 vty_redraw_line (vty
);
1342 /* Read data via vty socket. */
1344 vty_read (struct thread
*thread
)
1348 unsigned char buf
[VTY_READ_BUFSIZ
];
1350 int vty_sock
= THREAD_FD (thread
);
1351 struct vty
*vty
= THREAD_ARG (thread
);
1354 /* Read raw data from socket */
1355 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1359 if (ERRNO_IO_RETRY(errno
))
1361 vty_event (VTY_READ
, vty_sock
, vty
);
1364 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1365 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1366 __func__
, vty
->fd
, safe_strerror(errno
));
1368 buffer_reset(vty
->obuf
);
1369 vty
->status
= VTY_CLOSE
;
1372 for (i
= 0; i
< nbytes
; i
++)
1387 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1389 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1390 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1397 /* In case of telnet command */
1399 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1406 if (vty
->status
== VTY_MORE
)
1413 vty_buffer_reset (vty
);
1415 #if 0 /* More line does not work for "show ip bgp". */
1418 vty
->status
= VTY_MORELINE
;
1427 /* Escape character. */
1428 if (vty
->escape
== VTY_ESCAPE
)
1430 vty_escape_map (buf
[i
], vty
);
1434 /* Pre-escape status. */
1435 if (vty
->escape
== VTY_PRE_ESCAPE
)
1440 vty
->escape
= VTY_ESCAPE
;
1443 vty_backward_word (vty
);
1444 vty
->escape
= VTY_NORMAL
;
1447 vty_forward_word (vty
);
1448 vty
->escape
= VTY_NORMAL
;
1451 vty_forward_kill_word (vty
);
1452 vty
->escape
= VTY_NORMAL
;
1456 vty_backward_kill_word (vty
);
1457 vty
->escape
= VTY_NORMAL
;
1460 vty
->escape
= VTY_NORMAL
;
1469 vty_beginning_of_line (vty
);
1472 vty_backward_char (vty
);
1475 vty_stop_input (vty
);
1478 vty_delete_char (vty
);
1481 vty_end_of_line (vty
);
1484 vty_forward_char (vty
);
1488 vty_delete_backward_char (vty
);
1491 vty_kill_line (vty
);
1494 vty_next_line (vty
);
1497 vty_previous_line (vty
);
1500 vty_transpose_chars (vty
);
1503 vty_kill_line_from_beginning (vty
);
1506 vty_backward_kill_word (vty
);
1509 vty_end_config (vty
);
1513 vty_out (vty
, "%s", VTY_NEWLINE
);
1517 vty_complete_command (vty
);
1520 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1521 vty_self_insert (vty
, buf
[i
]);
1523 vty_describe_command (vty
);
1526 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1528 vty
->escape
= VTY_ESCAPE
;
1532 vty
->escape
= VTY_PRE_ESCAPE
;
1535 if (buf
[i
] > 31 && buf
[i
] < 127)
1536 vty_self_insert (vty
, buf
[i
]);
1542 if (vty
->status
== VTY_CLOSE
)
1546 vty_event (VTY_WRITE
, vty_sock
, vty
);
1547 vty_event (VTY_READ
, vty_sock
, vty
);
1552 /* Flush buffer to the vty. */
1554 vty_flush (struct thread
*thread
)
1557 buffer_status_t flushrc
;
1558 int vty_sock
= THREAD_FD (thread
);
1559 struct vty
*vty
= THREAD_ARG (thread
);
1561 vty
->t_write
= NULL
;
1563 /* Tempolary disable read thread. */
1564 if ((vty
->lines
== 0) && vty
->t_read
)
1566 thread_cancel (vty
->t_read
);
1570 /* Function execution continue. */
1571 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1573 /* N.B. if width is 0, that means we don't know the window size. */
1574 if ((vty
->lines
== 0) || (vty
->width
== 0))
1575 flushrc
= buffer_flush_available(vty
->obuf
, vty
->fd
);
1576 else if (vty
->status
== VTY_MORELINE
)
1577 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1580 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1581 vty
->lines
>= 0 ? vty
->lines
:
1587 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1588 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1590 buffer_reset(vty
->obuf
);
1594 if (vty
->status
== VTY_CLOSE
)
1598 vty
->status
= VTY_NORMAL
;
1599 if (vty
->lines
== 0)
1600 vty_event (VTY_READ
, vty_sock
, vty
);
1603 case BUFFER_PENDING
:
1604 /* There is more data waiting to be written. */
1605 vty
->status
= VTY_MORE
;
1606 if (vty
->lines
== 0)
1607 vty_event (VTY_WRITE
, vty_sock
, vty
);
1614 /* Create new vty structure. */
1616 vty_create (int vty_sock
, union sockunion
*su
)
1618 char buf
[SU_ADDRSTRLEN
];
1621 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1623 /* Allocate new vty structure and set up default values. */
1626 vty
->type
= VTY_TERM
;
1627 strcpy (vty
->address
, buf
);
1628 if (no_password_check
)
1630 if (restricted_mode
)
1631 vty
->node
= RESTRICTED_NODE
;
1632 else if (host
.advanced
)
1633 vty
->node
= ENABLE_NODE
;
1635 vty
->node
= VIEW_NODE
;
1638 vty
->node
= AUTH_NODE
;
1641 vty_clear_buf (vty
);
1643 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1646 vector_set_index (vtyvec
, vty_sock
, vty
);
1647 vty
->status
= VTY_NORMAL
;
1648 vty
->v_timeout
= vty_timeout_val
;
1649 if (host
.lines
>= 0)
1650 vty
->lines
= host
.lines
;
1654 vty
->iac_sb_in_progress
= 0;
1657 if (! no_password_check
)
1659 /* Vty is not available if password isn't set. */
1660 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1662 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1663 vty
->status
= VTY_CLOSE
;
1669 /* Say hello to the world. */
1671 if (! no_password_check
)
1672 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1674 /* Setting up terminal. */
1675 vty_will_echo (vty
);
1676 vty_will_suppress_go_ahead (vty
);
1678 vty_dont_linemode (vty
);
1679 vty_do_window_size (vty
);
1680 /* vty_dont_lflow_ahead (vty); */
1684 /* Add read/write thread. */
1685 vty_event (VTY_WRITE
, vty_sock
, vty
);
1686 vty_event (VTY_READ
, vty_sock
, vty
);
1691 /* Accept connection from the network. */
1693 vty_accept (struct thread
*thread
)
1700 struct prefix
*p
= NULL
;
1701 struct access_list
*acl
= NULL
;
1702 char buf
[SU_ADDRSTRLEN
];
1704 accept_sock
= THREAD_FD (thread
);
1706 /* We continue hearing vty socket. */
1707 vty_event (VTY_SERV
, accept_sock
, NULL
);
1709 memset (&su
, 0, sizeof (union sockunion
));
1711 /* We can handle IPv4 or IPv6 socket. */
1712 vty_sock
= sockunion_accept (accept_sock
, &su
);
1715 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1718 set_nonblocking(vty_sock
);
1720 p
= sockunion2hostprefix (&su
);
1722 /* VTY's accesslist apply. */
1723 if (p
->family
== AF_INET
&& vty_accesslist_name
)
1725 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1726 (access_list_apply (acl
, p
) == FILTER_DENY
))
1728 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1729 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1732 /* continue accepting connections */
1733 vty_event (VTY_SERV
, accept_sock
, NULL
);
1742 /* VTY's ipv6 accesslist apply. */
1743 if (p
->family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1745 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1746 (access_list_apply (acl
, p
) == FILTER_DENY
))
1748 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1749 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1752 /* continue accepting connections */
1753 vty_event (VTY_SERV
, accept_sock
, NULL
);
1760 #endif /* HAVE_IPV6 */
1765 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1766 (char *) &on
, sizeof (on
));
1768 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1769 safe_strerror (errno
));
1771 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1772 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1774 vty_create (vty_sock
, &su
);
1781 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1784 struct addrinfo req
;
1785 struct addrinfo
*ainfo
;
1786 struct addrinfo
*ainfo_save
;
1788 char port_str
[BUFSIZ
];
1790 memset (&req
, 0, sizeof (struct addrinfo
));
1791 req
.ai_flags
= AI_PASSIVE
;
1792 req
.ai_family
= AF_UNSPEC
;
1793 req
.ai_socktype
= SOCK_STREAM
;
1794 sprintf (port_str
, "%d", port
);
1795 port_str
[sizeof (port_str
) - 1] = '\0';
1797 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1801 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1809 if (ainfo
->ai_family
!= AF_INET
1811 && ainfo
->ai_family
!= AF_INET6
1812 #endif /* HAVE_IPV6 */
1816 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1820 sockopt_v6only (ainfo
->ai_family
, sock
);
1821 sockopt_reuseaddr (sock
);
1822 sockopt_reuseport (sock
);
1824 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1827 close (sock
); /* Avoid sd leak. */
1831 ret
= listen (sock
, 3);
1834 close (sock
); /* Avoid sd leak. */
1838 vty_event (VTY_SERV
, sock
, NULL
);
1840 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1842 freeaddrinfo (ainfo_save
);
1844 #else /* HAVE_IPV6 */
1846 /* Make vty server socket. */
1848 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1855 memset (&su
, 0, sizeof (union sockunion
));
1856 su
.sa
.sa_family
= family
;
1861 naddr
=&su
.sin
.sin_addr
;
1865 naddr
=&su
.sin6
.sin6_addr
;
1871 switch(inet_pton(family
,addr
,naddr
))
1874 zlog_err("bad address %s",addr
);
1878 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
1882 /* Make new socket. */
1883 accept_sock
= sockunion_stream_socket (&su
);
1884 if (accept_sock
< 0)
1887 /* This is server, so reuse address. */
1888 sockopt_reuseaddr (accept_sock
);
1889 sockopt_reuseport (accept_sock
);
1891 /* Bind socket to universal address and given port. */
1892 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
1895 zlog_warn("can't bind socket");
1896 close (accept_sock
); /* Avoid sd leak. */
1900 /* Listen socket under queue 3. */
1901 ret
= listen (accept_sock
, 3);
1904 zlog (NULL
, LOG_WARNING
, "can't listen socket");
1905 close (accept_sock
); /* Avoid sd leak. */
1909 /* Add vty server event. */
1910 vty_event (VTY_SERV
, accept_sock
, NULL
);
1912 #endif /* HAVE_IPV6 */
1915 /* For sockaddr_un. */
1918 /* VTY shell UNIX domain socket. */
1920 vty_serv_un (const char *path
)
1924 struct sockaddr_un serv
;
1926 struct zprivs_ids_t ids
;
1928 /* First of all, unlink existing socket */
1932 old_mask
= umask (0007);
1934 /* Make UNIX domain socket. */
1935 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1938 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
1942 /* Make server socket. */
1943 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1944 serv
.sun_family
= AF_UNIX
;
1945 strncpy (serv
.sun_path
, path
, strlen (path
));
1946 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1947 len
= serv
.sun_len
= SUN_LEN(&serv
);
1949 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1950 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1952 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1955 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1956 close (sock
); /* Avoid sd leak. */
1960 ret
= listen (sock
, 5);
1963 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
1964 close (sock
); /* Avoid sd leak. */
1970 zprivs_get_ids(&ids
);
1972 if (ids
.gid_vty
> 0)
1974 /* set group of socket */
1975 if ( chown (path
, -1, ids
.gid_vty
) )
1977 zlog_err ("vty_serv_un: could chown socket, %s",
1978 safe_strerror (errno
) );
1982 vty_event (VTYSH_SERV
, sock
, NULL
);
1985 /* #define VTYSH_DEBUG 1 */
1988 vtysh_accept (struct thread
*thread
)
1993 struct sockaddr_un client
;
1996 accept_sock
= THREAD_FD (thread
);
1998 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
2000 memset (&client
, 0, sizeof (struct sockaddr_un
));
2001 client_len
= sizeof (struct sockaddr_un
);
2003 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
2004 (socklen_t
*) &client_len
);
2008 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
2012 if (set_nonblocking(sock
) < 0)
2014 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2015 " %s, closing", sock
, safe_strerror (errno
));
2021 printf ("VTY shell accept\n");
2022 #endif /* VTYSH_DEBUG */
2026 vty
->type
= VTY_SHELL_SERV
;
2027 vty
->node
= VIEW_NODE
;
2029 vty_event (VTYSH_READ
, sock
, vty
);
2035 vtysh_flush(struct vty
*vty
)
2037 switch (buffer_flush_available(vty
->obuf
, vty
->fd
))
2039 case BUFFER_PENDING
:
2040 vty_event(VTYSH_WRITE
, vty
->fd
, vty
);
2043 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2044 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2045 buffer_reset(vty
->obuf
);
2056 vtysh_read (struct thread
*thread
)
2062 unsigned char buf
[VTY_READ_BUFSIZ
];
2064 u_char header
[4] = {0, 0, 0, 0};
2066 sock
= THREAD_FD (thread
);
2067 vty
= THREAD_ARG (thread
);
2070 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2074 if (ERRNO_IO_RETRY(errno
))
2076 vty_event (VTYSH_READ
, sock
, vty
);
2079 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2080 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2081 __func__
, sock
, safe_strerror(errno
));
2083 buffer_reset(vty
->obuf
);
2086 printf ("close vtysh\n");
2087 #endif /* VTYSH_DEBUG */
2092 printf ("line: %.*s\n", nbytes
, buf
);
2093 #endif /* VTYSH_DEBUG */
2095 for (p
= buf
; p
< buf
+nbytes
; p
++)
2097 vty_ensure(vty
, vty
->length
+1);
2098 vty
->buf
[vty
->length
++] = *p
;
2101 /* Pass this line to parser. */
2102 ret
= vty_execute (vty
);
2103 /* Note that vty_execute clears the command buffer and resets
2104 vty->length to 0. */
2106 /* Return result. */
2108 printf ("result: %d\n", ret
);
2109 printf ("vtysh node: %d\n", vty
->node
);
2110 #endif /* VTYSH_DEBUG */
2113 buffer_put(vty
->obuf
, header
, 4);
2115 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2116 /* Try to flush results; exit if a write error occurs. */
2121 vty_event (VTYSH_READ
, sock
, vty
);
2127 vtysh_write (struct thread
*thread
)
2129 struct vty
*vty
= THREAD_ARG (thread
);
2131 vty
->t_write
= NULL
;
2138 /* Determine address family to bind. */
2140 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2142 /* If port is set to 0, do not listen on TCP/IP at all! */
2147 vty_serv_sock_addrinfo (addr
, port
);
2148 #else /* ! HAVE_IPV6 */
2149 vty_serv_sock_family (addr
,port
, AF_INET
);
2150 #endif /* HAVE_IPV6 */
2158 /* Close vty interface. Warning: call this only from functions that
2159 will be careful not to access the vty afterwards (since it has
2160 now been freed). This is safest from top-level functions (called
2161 directly by the thread dispatcher). */
2163 vty_close (struct vty
*vty
)
2167 /* Cancel threads.*/
2169 thread_cancel (vty
->t_read
);
2171 thread_cancel (vty
->t_write
);
2173 thread_cancel (vty
->t_timeout
);
2176 buffer_flush_all (vty
->obuf
, vty
->fd
);
2178 /* Free input buffer. */
2179 buffer_free (vty
->obuf
);
2181 /* Free command history. */
2182 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2184 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2187 vector_unset (vtyvec
, vty
->fd
);
2194 XFREE (MTYPE_VTY
, vty
->buf
);
2197 XFREE (MTYPE_VTY
, vty
->error_buf
);
2199 /* Check configure. */
2200 vty_config_unlock (vty
);
2203 XFREE (MTYPE_VTY
, vty
);
2206 /* When time out occur output message then close connection. */
2208 vty_timeout (struct thread
*thread
)
2212 vty
= THREAD_ARG (thread
);
2213 vty
->t_timeout
= NULL
;
2217 buffer_reset (vty
->obuf
);
2218 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2220 /* Close connection. */
2221 vty
->status
= VTY_CLOSE
;
2227 /* Read up configuration file from file_name. */
2229 vty_read_file (FILE *confp
)
2233 unsigned int line_num
= 0;
2236 vty
->fd
= dup(STDERR_FILENO
); /* vty_close() will close this */
2239 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2240 vty
->fd
= STDOUT_FILENO
;
2242 vty
->type
= VTY_FILE
;
2243 vty
->node
= CONFIG_NODE
;
2245 /* Execute configuration file */
2246 ret
= config_from_file (vty
, confp
, &line_num
);
2248 /* Flush any previous errors before printing messages below */
2249 buffer_flush_all (vty
->obuf
, vty
->fd
);
2251 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2255 case CMD_ERR_AMBIGUOUS
:
2256 fprintf (stderr
, "*** Error reading config: Ambiguous command.\n");
2258 case CMD_ERR_NO_MATCH
:
2259 fprintf (stderr
, "*** Error reading config: There is no such command.\n");
2262 fprintf (stderr
, "*** Error occured processing line %u, below:\n%s\n",
2263 line_num
, vty
->error_buf
);
2270 vty_use_backup_config (char *fullpath
)
2272 char *fullpath_sav
, *fullpath_tmp
;
2279 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2280 strcpy (fullpath_sav
, fullpath
);
2281 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2282 if (stat (fullpath_sav
, &buf
) == -1)
2284 free (fullpath_sav
);
2288 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2289 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2291 /* Open file to configuration write. */
2292 tmp
= mkstemp (fullpath_tmp
);
2295 free (fullpath_sav
);
2296 free (fullpath_tmp
);
2300 sav
= open (fullpath_sav
, O_RDONLY
);
2303 unlink (fullpath_tmp
);
2304 free (fullpath_sav
);
2305 free (fullpath_tmp
);
2309 while((c
= read (sav
, buffer
, 512)) > 0)
2311 if (write (tmp
, buffer
, c
) <= 0)
2313 free (fullpath_sav
);
2314 free (fullpath_tmp
);
2323 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2325 unlink (fullpath_tmp
);
2326 free (fullpath_sav
);
2327 free (fullpath_tmp
);
2331 if (link (fullpath_tmp
, fullpath
) == 0)
2332 ret
= fopen (fullpath
, "r");
2334 unlink (fullpath_tmp
);
2336 free (fullpath_sav
);
2337 free (fullpath_tmp
);
2341 /* Read up configuration file from file_name. */
2343 vty_read_config (char *config_file
,
2344 char *config_default_dir
)
2346 char cwd
[MAXPATHLEN
];
2351 /* If -f flag specified. */
2352 if (config_file
!= NULL
)
2354 if (! IS_DIRECTORY_SEP (config_file
[0]))
2356 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2358 fprintf (stderr
, "Failure to determine Current Working Directory %d!\n", errno
);
2361 tmp
= XMALLOC (MTYPE_TMP
,
2362 strlen (cwd
) + strlen (config_file
) + 2);
2363 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2367 fullpath
= config_file
;
2369 confp
= fopen (fullpath
, "r");
2373 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2374 __func__
, fullpath
, safe_strerror (errno
));
2376 confp
= vty_use_backup_config (fullpath
);
2378 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2381 fprintf (stderr
, "can't open configuration file [%s]\n",
2390 host_config_set (config_default_dir
);
2394 struct stat conf_stat
;
2396 /* !!!!PLEASE LEAVE!!!!
2397 * This is NEEDED for use with vtysh -b, or else you can get
2398 * a real configuration food fight with a lot garbage in the
2399 * merged configuration file it creates coming from the per
2400 * daemon configuration files. This also allows the daemons
2401 * to start if there default configuration file is not
2402 * present or ignore them, as needed when using vtysh -b to
2403 * configure the daemons at boot - MAG
2406 /* Stat for vtysh Zebra.conf, if found startup and wait for
2407 * boot configuration
2410 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2412 ret
= stat (integrate_default
, &conf_stat
);
2414 goto tmp_free_and_out
;
2417 confp
= fopen (config_default_dir
, "r");
2420 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2421 __func__
, config_default_dir
, safe_strerror (errno
));
2423 confp
= vty_use_backup_config (config_default_dir
);
2426 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2427 fullpath
= config_default_dir
;
2431 fprintf (stderr
, "can't open configuration file [%s]\n",
2432 config_default_dir
);
2433 goto tmp_free_and_out
;
2437 fullpath
= config_default_dir
;
2440 vty_read_file (confp
);
2444 host_config_set (fullpath
);
2448 XFREE (MTYPE_TMP
, fullpath
);
2451 /* Small utility function which output log to the VTY. */
2453 vty_log (const char *level
, const char *proto_str
,
2454 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2462 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2463 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2468 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2473 /* Async-signal-safe version of vty_log for fixed strings. */
2475 vty_log_fixed (char *buf
, size_t len
)
2478 struct iovec iov
[2];
2479 char crlf
[4] = "\r\n";
2481 /* vty may not have been initialised */
2485 iov
[0].iov_base
= buf
;
2486 iov
[0].iov_len
= len
;
2487 iov
[1].iov_base
= crlf
;
2490 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2493 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2494 /* N.B. We don't care about the return code, since process is
2495 most likely just about to die anyway. */
2496 if (writev(vty
->fd
, iov
, 2) == -1)
2498 fprintf(stderr
, "Failure to writev: %d\n", errno
);
2505 vty_config_lock (struct vty
*vty
)
2507 if (vty_config
== 0)
2516 vty_config_unlock (struct vty
*vty
)
2518 if (vty_config
== 1 && vty
->config
== 1)
2526 /* Master of the threads. */
2527 static struct thread_master
*vty_master
;
2530 vty_event (enum event event
, int sock
, struct vty
*vty
)
2532 struct thread
*vty_serv_thread
;
2537 vty_serv_thread
= thread_add_read (vty_master
, vty_accept
, vty
, sock
);
2538 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2542 vty_serv_thread
= thread_add_read (vty_master
, vtysh_accept
, vty
, sock
);
2543 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2546 vty
->t_read
= thread_add_read (vty_master
, vtysh_read
, vty
, sock
);
2549 vty
->t_write
= thread_add_write (vty_master
, vtysh_write
, vty
, sock
);
2553 vty
->t_read
= thread_add_read (vty_master
, vty_read
, vty
, sock
);
2555 /* Time out treatment. */
2559 thread_cancel (vty
->t_timeout
);
2561 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2566 vty
->t_write
= thread_add_write (vty_master
, vty_flush
, vty
, sock
);
2568 case VTY_TIMEOUT_RESET
:
2571 thread_cancel (vty
->t_timeout
);
2572 vty
->t_timeout
= NULL
;
2577 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2586 "Display who is on vty\n")
2591 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2592 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2593 vty_out (vty
, "%svty[%d] connected from %s.%s",
2594 v
->config
? "*" : " ",
2595 i
, v
->address
, VTY_NEWLINE
);
2599 /* Move to vty configuration mode. */
2603 "Configure a terminal line\n"
2604 "Virtual terminal\n")
2606 vty
->node
= VTY_NODE
;
2610 /* Set time out value. */
2612 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2614 unsigned long timeout
= 0;
2616 /* min_str and sec_str are already checked by parser. So it must be
2617 all digit string. */
2620 timeout
= strtol (min_str
, NULL
, 10);
2624 timeout
+= strtol (sec_str
, NULL
, 10);
2626 vty_timeout_val
= timeout
;
2627 vty
->v_timeout
= timeout
;
2628 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2634 DEFUN (exec_timeout_min
,
2635 exec_timeout_min_cmd
,
2636 "exec-timeout <0-35791>",
2637 "Set timeout value\n"
2638 "Timeout value in minutes\n")
2640 return exec_timeout (vty
, argv
[0], NULL
);
2643 DEFUN (exec_timeout_sec
,
2644 exec_timeout_sec_cmd
,
2645 "exec-timeout <0-35791> <0-2147483>",
2646 "Set the EXEC timeout\n"
2647 "Timeout in minutes\n"
2648 "Timeout in seconds\n")
2650 return exec_timeout (vty
, argv
[0], argv
[1]);
2653 DEFUN (no_exec_timeout
,
2654 no_exec_timeout_cmd
,
2657 "Set the EXEC timeout\n")
2659 return exec_timeout (vty
, NULL
, NULL
);
2662 /* Set vty access class. */
2663 DEFUN (vty_access_class
,
2664 vty_access_class_cmd
,
2665 "access-class WORD",
2666 "Filter connections based on an IP access list\n"
2669 if (vty_accesslist_name
)
2670 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2672 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2677 /* Clear vty access class. */
2678 DEFUN (no_vty_access_class
,
2679 no_vty_access_class_cmd
,
2680 "no access-class [WORD]",
2682 "Filter connections based on an IP access list\n"
2685 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2687 vty_out (vty
, "Access-class is not currently applied to vty%s",
2692 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2694 vty_accesslist_name
= NULL
;
2700 /* Set vty access class. */
2701 DEFUN (vty_ipv6_access_class
,
2702 vty_ipv6_access_class_cmd
,
2703 "ipv6 access-class WORD",
2705 "Filter connections based on an IP access list\n"
2706 "IPv6 access list\n")
2708 if (vty_ipv6_accesslist_name
)
2709 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2711 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2716 /* Clear vty access class. */
2717 DEFUN (no_vty_ipv6_access_class
,
2718 no_vty_ipv6_access_class_cmd
,
2719 "no ipv6 access-class [WORD]",
2722 "Filter connections based on an IP access list\n"
2723 "IPv6 access list\n")
2725 if (! vty_ipv6_accesslist_name
||
2726 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2728 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2733 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2735 vty_ipv6_accesslist_name
= NULL
;
2739 #endif /* HAVE_IPV6 */
2745 "Enable password checking\n")
2747 no_password_check
= 0;
2751 DEFUN (no_vty_login
,
2755 "Enable password checking\n")
2757 no_password_check
= 1;
2762 DEFUN (vty_restricted_mode
,
2763 vty_restricted_mode_cmd
,
2764 "anonymous restricted",
2765 "Restrict view commands available in anonymous, unauthenticated vty\n")
2767 restricted_mode
= 1;
2771 DEFUN (vty_no_restricted_mode
,
2772 vty_no_restricted_mode_cmd
,
2773 "no anonymous restricted",
2775 "Enable password checking\n")
2777 restricted_mode
= 0;
2781 DEFUN (service_advanced_vty
,
2782 service_advanced_vty_cmd
,
2783 "service advanced-vty",
2784 "Set up miscellaneous service\n"
2785 "Enable advanced mode vty interface\n")
2791 DEFUN (no_service_advanced_vty
,
2792 no_service_advanced_vty_cmd
,
2793 "no service advanced-vty",
2795 "Set up miscellaneous service\n"
2796 "Enable advanced mode vty interface\n")
2802 DEFUN (terminal_monitor
,
2803 terminal_monitor_cmd
,
2805 "Set terminal line parameters\n"
2806 "Copy debug output to the current terminal line\n")
2812 DEFUN (terminal_no_monitor
,
2813 terminal_no_monitor_cmd
,
2814 "terminal no monitor",
2815 "Set terminal line parameters\n"
2817 "Copy debug output to the current terminal line\n")
2823 ALIAS (terminal_no_monitor
,
2824 no_terminal_monitor_cmd
,
2825 "no terminal monitor",
2827 "Set terminal line parameters\n"
2828 "Copy debug output to the current terminal line\n")
2830 DEFUN (show_history
,
2834 "Display the session command history\n")
2838 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2840 if (index
== VTY_MAXHIST
)
2846 if (vty
->hist
[index
] != NULL
)
2847 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2855 /* Display current configuration. */
2857 vty_config_write (struct vty
*vty
)
2859 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2861 if (vty_accesslist_name
)
2862 vty_out (vty
, " access-class %s%s",
2863 vty_accesslist_name
, VTY_NEWLINE
);
2865 if (vty_ipv6_accesslist_name
)
2866 vty_out (vty
, " ipv6 access-class %s%s",
2867 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
2870 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2871 vty_out (vty
, " exec-timeout %ld %ld%s",
2872 vty_timeout_val
/ 60,
2873 vty_timeout_val
% 60, VTY_NEWLINE
);
2876 if (no_password_check
)
2877 vty_out (vty
, " no login%s", VTY_NEWLINE
);
2879 if (restricted_mode
!= restricted_mode_default
)
2881 if (restricted_mode_default
)
2882 vty_out (vty
, " no anonymous restricted%s", VTY_NEWLINE
);
2884 vty_out (vty
, " anonymous restricted%s", VTY_NEWLINE
);
2887 vty_out (vty
, "!%s", VTY_NEWLINE
);
2892 struct cmd_node vty_node
=
2895 "%s(config-line)# ",
2899 /* Reset all VTY status. */
2905 struct thread
*vty_serv_thread
;
2907 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2908 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2910 buffer_reset (vty
->obuf
);
2911 vty
->status
= VTY_CLOSE
;
2915 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
2916 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2918 thread_cancel (vty_serv_thread
);
2919 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2923 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2925 if (vty_accesslist_name
)
2927 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2928 vty_accesslist_name
= NULL
;
2931 if (vty_ipv6_accesslist_name
)
2933 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2934 vty_ipv6_accesslist_name
= NULL
;
2941 char cwd
[MAXPATHLEN
];
2944 c
= getcwd (cwd
, MAXPATHLEN
);
2949 * At this point if these go wrong, more than likely
2950 * the whole world is coming down around us
2951 * Hence not worrying about it too much.
2953 if (!chdir (SYSCONFDIR
))
2955 fprintf(stderr
, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR
, errno
);
2958 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2960 fprintf(stderr
, "Failure to getcwd, errno: %d\n", errno
);
2965 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
2966 strcpy (vty_cwd
, cwd
);
2976 vty_shell (struct vty
*vty
)
2978 return vty
->type
== VTY_SHELL
? 1 : 0;
2982 vty_shell_serv (struct vty
*vty
)
2984 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2990 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2993 /* Install vty's own commands like `who' command. */
2995 vty_init (struct thread_master
*master_thread
)
2997 /* For further configuration read, preserve current directory. */
3000 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3002 vty_master
= master_thread
;
3004 /* Initilize server thread vector. */
3005 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
3007 /* Install bgp top node. */
3008 install_node (&vty_node
, vty_config_write
);
3010 install_element (RESTRICTED_NODE
, &config_who_cmd
);
3011 install_element (RESTRICTED_NODE
, &show_history_cmd
);
3012 install_element (VIEW_NODE
, &config_who_cmd
);
3013 install_element (VIEW_NODE
, &show_history_cmd
);
3014 install_element (ENABLE_NODE
, &config_who_cmd
);
3015 install_element (CONFIG_NODE
, &line_vty_cmd
);
3016 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
3017 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3018 install_element (CONFIG_NODE
, &show_history_cmd
);
3019 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
3020 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
3021 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
3022 install_element (ENABLE_NODE
, &show_history_cmd
);
3024 install_default (VTY_NODE
);
3025 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
3026 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
3027 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
3028 install_element (VTY_NODE
, &vty_access_class_cmd
);
3029 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
3030 install_element (VTY_NODE
, &vty_login_cmd
);
3031 install_element (VTY_NODE
, &no_vty_login_cmd
);
3032 install_element (VTY_NODE
, &vty_restricted_mode_cmd
);
3033 install_element (VTY_NODE
, &vty_no_restricted_mode_cmd
);
3035 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
3036 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3037 #endif /* HAVE_IPV6 */
3041 vty_terminate (void)
3044 XFREE (MTYPE_TMP
, vty_cwd
);
3046 if (vtyvec
&& Vvty_serv_thread
)
3049 vector_free (vtyvec
);
3050 vector_free (Vvty_serv_thread
);