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 vector Vvty_serv_thread
;
76 /* Current directory. */
80 static int vty_config
;
82 /* Login password check. */
83 static int no_password_check
= 0;
85 /* Integrated configuration file path */
86 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
89 /* VTY standard output function. */
91 vty_out (struct vty
*vty
, const char *format
, ...)
101 va_start (args
, format
);
102 vprintf (format
, args
);
107 /* Try to write to initial buffer. */
108 va_start (args
, format
);
109 len
= vsnprintf (buf
, sizeof buf
, format
, args
);
112 /* Initial buffer is not enough. */
113 if (len
< 0 || len
>= size
)
122 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
126 va_start (args
, format
);
127 len
= vsnprintf (p
, size
, format
, args
);
130 if (len
> -1 && len
< size
)
135 /* When initial buffer is enough to store all output. */
139 /* Pointer p must point out buffer. */
140 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
142 /* If p is not different with buf, it is allocated buffer. */
144 XFREE (MTYPE_VTY_OUT_BUF
, p
);
151 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
152 const char *format
, va_list va
)
159 len
= snprintf(buf
, sizeof(buf
), "%s: %s: ", level
, proto_str
);
161 len
= snprintf(buf
, sizeof(buf
), "%s: ", proto_str
);
162 if ((len
< 0) || ((size_t)len
>= sizeof(buf
)))
165 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
166 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
172 if (write(vty
->fd
, buf
, len
) < 0)
174 if (ERRNO_IO_RETRY(errno
))
175 /* Kernel buffer is full, probably too much debugging output, so just
176 drop the data and ignore. */
178 /* Fatal I/O error. */
179 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
180 __func__
, vty
->fd
, safe_strerror(errno
));
181 buffer_reset(vty
->obuf
);
188 /* Output current time to the vty. */
190 vty_time_print (struct vty
*vty
, int cr
)
199 tm
= localtime (&clock
);
201 ret
= strftime (buf
, TIME_BUF
, "%Y/%m/%d %H:%M:%S", tm
);
204 zlog (NULL
, LOG_INFO
, "strftime error");
208 vty_out (vty
, "%s\n", buf
);
210 vty_out (vty
, "%s ", buf
);
215 /* Say hello to vty interface. */
217 vty_hello (struct vty
*vty
)
224 f
= fopen (host
.motdfile
, "r");
227 while (fgets (buf
, sizeof (buf
), f
))
230 /* work backwards to ignore trailling isspace() */
231 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace (*(s
- 1));
234 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
239 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
242 vty_out (vty
, host
.motd
);
245 /* Put out prompt and wait input from user. */
247 vty_prompt (struct vty
*vty
)
249 struct utsname names
;
252 if (vty
->type
== VTY_TERM
)
254 hostname
= host
.name
;
258 hostname
= names
.nodename
;
260 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
264 /* Send WILL TELOPT_ECHO to remote server. */
266 vty_will_echo (struct vty
*vty
)
268 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
269 vty_out (vty
, "%s", cmd
);
272 /* Make suppress Go-Ahead telnet option. */
274 vty_will_suppress_go_ahead (struct vty
*vty
)
276 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
277 vty_out (vty
, "%s", cmd
);
280 /* Make don't use linemode over telnet. */
282 vty_dont_linemode (struct vty
*vty
)
284 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
285 vty_out (vty
, "%s", cmd
);
288 /* Use window size. */
290 vty_do_window_size (struct vty
*vty
)
292 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
293 vty_out (vty
, "%s", cmd
);
296 #if 0 /* Currently not used. */
297 /* Make don't use lflow vty interface. */
299 vty_dont_lflow_ahead (struct vty
*vty
)
301 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
302 vty_out (vty
, "%s", cmd
);
306 /* Allocate new vty struct. */
310 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
312 new->obuf
= buffer_new(0); /* Use default buffer size. */
313 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
314 new->max
= VTY_BUFSIZ
;
319 /* Authentication of vty */
321 vty_auth (struct vty
*vty
, char *buf
)
324 enum node_type next_node
= 0;
326 char *crypt (const char *, const char *);
332 passwd
= host
.password_encrypt
;
334 passwd
= host
.password
;
336 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
338 next_node
= VIEW_NODE
;
340 case AUTH_ENABLE_NODE
:
342 passwd
= host
.enable_encrypt
;
344 passwd
= host
.enable
;
345 next_node
= ENABLE_NODE
;
352 fail
= strcmp (crypt(buf
, passwd
), passwd
);
354 fail
= strcmp (buf
, passwd
);
362 vty
->node
= next_node
; /* Success ! */
369 if (vty
->node
== AUTH_NODE
)
371 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
372 vty
->status
= VTY_CLOSE
;
376 /* AUTH_ENABLE_NODE */
378 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
379 vty
->node
= VIEW_NODE
;
385 /* Command execution over the vty interface. */
387 vty_command (struct vty
*vty
, char *buf
)
392 /* Split readline string up into the vector */
393 vline
= cmd_make_strvec (buf
);
398 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
400 if (ret
!= CMD_SUCCESS
)
404 if (vty
->type
== VTY_FILE
)
405 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
407 case CMD_ERR_AMBIGUOUS
:
408 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
410 case CMD_ERR_NO_MATCH
:
411 vty_out (vty
, "%% Unknown command.%s", VTY_NEWLINE
);
413 case CMD_ERR_INCOMPLETE
:
414 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
417 cmd_free_strvec (vline
);
422 static const char telnet_backward_char
= 0x08;
423 static const char telnet_space_char
= ' ';
425 /* Basic function to write buffer to vty. */
427 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
429 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
432 /* Should we do buffering here ? And make vty_flush (vty) ? */
433 buffer_put (vty
->obuf
, buf
, nbytes
);
436 /* Ensure length of input buffer. Is buffer is short, double it. */
438 vty_ensure (struct vty
*vty
, int length
)
440 if (vty
->max
<= length
)
443 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
447 /* Basic function to insert character into vty. */
449 vty_self_insert (struct vty
*vty
, char c
)
454 vty_ensure (vty
, vty
->length
+ 1);
455 length
= vty
->length
- vty
->cp
;
456 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
457 vty
->buf
[vty
->cp
] = c
;
459 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
460 for (i
= 0; i
< length
; i
++)
461 vty_write (vty
, &telnet_backward_char
, 1);
467 /* Self insert character 'c' in overwrite mode. */
469 vty_self_insert_overwrite (struct vty
*vty
, char c
)
471 vty_ensure (vty
, vty
->length
+ 1);
472 vty
->buf
[vty
->cp
++] = c
;
474 if (vty
->cp
> vty
->length
)
477 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
480 vty_write (vty
, &c
, 1);
483 /* Insert a word into vty interface with overwrite mode. */
485 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
487 int len
= strlen (str
);
488 vty_write (vty
, str
, len
);
489 strcpy (&vty
->buf
[vty
->cp
], str
);
491 vty
->length
= vty
->cp
;
494 /* Forward character. */
496 vty_forward_char (struct vty
*vty
)
498 if (vty
->cp
< vty
->length
)
500 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
505 /* Backward character. */
507 vty_backward_char (struct vty
*vty
)
512 vty_write (vty
, &telnet_backward_char
, 1);
516 /* Move to the beginning of the line. */
518 vty_beginning_of_line (struct vty
*vty
)
521 vty_backward_char (vty
);
524 /* Move to the end of the line. */
526 vty_end_of_line (struct vty
*vty
)
528 while (vty
->cp
< vty
->length
)
529 vty_forward_char (vty
);
532 static void vty_kill_line_from_beginning (struct vty
*);
533 static void vty_redraw_line (struct vty
*);
535 /* Print command line history. This function is called from
536 vty_next_line and vty_previous_line. */
538 vty_history_print (struct vty
*vty
)
542 vty_kill_line_from_beginning (vty
);
544 /* Get previous line from history buffer */
545 length
= strlen (vty
->hist
[vty
->hp
]);
546 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
547 vty
->cp
= vty
->length
= length
;
549 /* Redraw current line */
550 vty_redraw_line (vty
);
553 /* Show next command line history. */
555 vty_next_line (struct vty
*vty
)
559 if (vty
->hp
== vty
->hindex
)
562 /* Try is there history exist or not. */
564 if (try_index
== (VTY_MAXHIST
- 1))
569 /* If there is not history return. */
570 if (vty
->hist
[try_index
] == NULL
)
575 vty_history_print (vty
);
578 /* Show previous command line history. */
580 vty_previous_line (struct vty
*vty
)
586 try_index
= VTY_MAXHIST
- 1;
590 if (vty
->hist
[try_index
] == NULL
)
595 vty_history_print (vty
);
598 /* This function redraw all of the command line character. */
600 vty_redraw_line (struct vty
*vty
)
602 vty_write (vty
, vty
->buf
, vty
->length
);
603 vty
->cp
= vty
->length
;
608 vty_forward_word (struct vty
*vty
)
610 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
611 vty_forward_char (vty
);
613 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
614 vty_forward_char (vty
);
617 /* Backward word without skipping training space. */
619 vty_backward_pure_word (struct vty
*vty
)
621 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
622 vty_backward_char (vty
);
627 vty_backward_word (struct vty
*vty
)
629 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
630 vty_backward_char (vty
);
632 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
633 vty_backward_char (vty
);
636 /* When '^D' is typed at the beginning of the line we move to the down
639 vty_down_level (struct vty
*vty
)
641 vty_out (vty
, "%s", VTY_NEWLINE
);
642 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
647 /* When '^Z' is received from vty, move down to the enable mode. */
649 vty_end_config (struct vty
*vty
)
651 vty_out (vty
, "%s", VTY_NEWLINE
);
674 case KEYCHAIN_KEY_NODE
:
677 vty_config_unlock (vty
);
678 vty
->node
= ENABLE_NODE
;
681 /* Unknown node, we have to ignore it. */
689 /* Delete a charcter at the current point. */
691 vty_delete_char (struct vty
*vty
)
696 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
699 if (vty
->length
== 0)
701 vty_down_level (vty
);
705 if (vty
->cp
== vty
->length
)
706 return; /* completion need here? */
708 size
= vty
->length
- vty
->cp
;
711 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
712 vty
->buf
[vty
->length
] = '\0';
714 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
715 vty_write (vty
, &telnet_space_char
, 1);
717 for (i
= 0; i
< size
; i
++)
718 vty_write (vty
, &telnet_backward_char
, 1);
721 /* Delete a character before the point. */
723 vty_delete_backward_char (struct vty
*vty
)
728 vty_backward_char (vty
);
729 vty_delete_char (vty
);
732 /* Kill rest of line from current point. */
734 vty_kill_line (struct vty
*vty
)
739 size
= vty
->length
- vty
->cp
;
744 for (i
= 0; i
< size
; i
++)
745 vty_write (vty
, &telnet_space_char
, 1);
746 for (i
= 0; i
< size
; i
++)
747 vty_write (vty
, &telnet_backward_char
, 1);
749 memset (&vty
->buf
[vty
->cp
], 0, size
);
750 vty
->length
= vty
->cp
;
753 /* Kill line from the beginning. */
755 vty_kill_line_from_beginning (struct vty
*vty
)
757 vty_beginning_of_line (vty
);
761 /* Delete a word before the point. */
763 vty_forward_kill_word (struct vty
*vty
)
765 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
766 vty_delete_char (vty
);
767 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
768 vty_delete_char (vty
);
771 /* Delete a word before the point. */
773 vty_backward_kill_word (struct vty
*vty
)
775 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
776 vty_delete_backward_char (vty
);
777 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
778 vty_delete_backward_char (vty
);
781 /* Transpose chars before or at the point. */
783 vty_transpose_chars (struct vty
*vty
)
787 /* If length is short or point is near by the beginning of line then
789 if (vty
->length
< 2 || vty
->cp
< 1)
792 /* In case of point is located at the end of the line. */
793 if (vty
->cp
== vty
->length
)
795 c1
= vty
->buf
[vty
->cp
- 1];
796 c2
= vty
->buf
[vty
->cp
- 2];
798 vty_backward_char (vty
);
799 vty_backward_char (vty
);
800 vty_self_insert_overwrite (vty
, c1
);
801 vty_self_insert_overwrite (vty
, c2
);
805 c1
= vty
->buf
[vty
->cp
];
806 c2
= vty
->buf
[vty
->cp
- 1];
808 vty_backward_char (vty
);
809 vty_self_insert_overwrite (vty
, c1
);
810 vty_self_insert_overwrite (vty
, c2
);
814 /* Do completion at vty interface. */
816 vty_complete_command (struct vty
*vty
)
820 char **matched
= NULL
;
823 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
826 vline
= cmd_make_strvec (vty
->buf
);
830 /* In case of 'help \t'. */
831 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
832 vector_set (vline
, '\0');
834 matched
= cmd_complete_command (vline
, vty
, &ret
);
836 cmd_free_strvec (vline
);
838 vty_out (vty
, "%s", VTY_NEWLINE
);
841 case CMD_ERR_AMBIGUOUS
:
842 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
844 vty_redraw_line (vty
);
846 case CMD_ERR_NO_MATCH
:
847 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
849 vty_redraw_line (vty
);
851 case CMD_COMPLETE_FULL_MATCH
:
853 vty_redraw_line (vty
);
854 vty_backward_pure_word (vty
);
855 vty_insert_word_overwrite (vty
, matched
[0]);
856 vty_self_insert (vty
, ' ');
857 XFREE (MTYPE_TMP
, matched
[0]);
859 case CMD_COMPLETE_MATCH
:
861 vty_redraw_line (vty
);
862 vty_backward_pure_word (vty
);
863 vty_insert_word_overwrite (vty
, matched
[0]);
864 XFREE (MTYPE_TMP
, matched
[0]);
865 vector_only_index_free (matched
);
868 case CMD_COMPLETE_LIST_MATCH
:
869 for (i
= 0; matched
[i
] != NULL
; i
++)
871 if (i
!= 0 && ((i
% 6) == 0))
872 vty_out (vty
, "%s", VTY_NEWLINE
);
873 vty_out (vty
, "%-10s ", matched
[i
]);
874 XFREE (MTYPE_TMP
, matched
[i
]);
876 vty_out (vty
, "%s", VTY_NEWLINE
);
879 vty_redraw_line (vty
);
881 case CMD_ERR_NOTHING_TODO
:
883 vty_redraw_line (vty
);
889 vector_only_index_free (matched
);
893 vty_describe_fold (struct vty
*vty
, int cmd_width
,
894 unsigned int desc_width
, struct desc
*desc
)
900 cmd
= desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
;
904 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, desc
->str
, VTY_NEWLINE
);
908 buf
= XCALLOC (MTYPE_TMP
, strlen (desc
->str
) + 1);
910 for (p
= desc
->str
; strlen (p
) > desc_width
; p
+= pos
+ 1)
912 for (pos
= desc_width
; pos
> 0; pos
--)
913 if (*(p
+ pos
) == ' ')
919 strncpy (buf
, p
, pos
);
921 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
926 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
928 XFREE (MTYPE_TMP
, buf
);
931 /* Describe matched command function. */
933 vty_describe_command (struct vty
*vty
)
938 unsigned int i
, width
, desc_width
;
939 struct desc
*desc
, *desc_cr
= NULL
;
941 vline
= cmd_make_strvec (vty
->buf
);
943 /* In case of '> ?'. */
946 vline
= vector_init (1);
947 vector_set (vline
, '\0');
950 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
951 vector_set (vline
, '\0');
953 describe
= cmd_describe_command (vline
, vty
, &ret
);
955 vty_out (vty
, "%s", VTY_NEWLINE
);
957 /* Ambiguous error. */
960 case CMD_ERR_AMBIGUOUS
:
961 cmd_free_strvec (vline
);
962 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
964 vty_redraw_line (vty
);
967 case CMD_ERR_NO_MATCH
:
968 cmd_free_strvec (vline
);
969 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
971 vty_redraw_line (vty
);
976 /* Get width of command string. */
978 for (i
= 0; i
< vector_active (describe
); i
++)
979 if ((desc
= vector_slot (describe
, i
)) != NULL
)
983 if (desc
->cmd
[0] == '\0')
986 len
= strlen (desc
->cmd
);
987 if (desc
->cmd
[0] == '.')
994 /* Get width of description string. */
995 desc_width
= vty
->width
- (width
+ 6);
997 /* Print out description. */
998 for (i
= 0; i
< vector_active (describe
); i
++)
999 if ((desc
= vector_slot (describe
, i
)) != NULL
)
1001 if (desc
->cmd
[0] == '\0')
1004 if (strcmp (desc
->cmd
, "<cr>") == 0)
1011 vty_out (vty
, " %-s%s",
1012 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1014 else if (desc_width
>= strlen (desc
->str
))
1015 vty_out (vty
, " %-*s %s%s", width
,
1016 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1017 desc
->str
, VTY_NEWLINE
);
1019 vty_describe_fold (vty
, width
, desc_width
, desc
);
1022 vty_out (vty
, " %-*s %s%s", width
1023 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1024 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1028 if ((desc
= desc_cr
))
1031 vty_out (vty
, " %-s%s",
1032 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1034 else if (desc_width
>= strlen (desc
->str
))
1035 vty_out (vty
, " %-*s %s%s", width
,
1036 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1037 desc
->str
, VTY_NEWLINE
);
1039 vty_describe_fold (vty
, width
, desc_width
, desc
);
1042 cmd_free_strvec (vline
);
1043 vector_free (describe
);
1046 vty_redraw_line (vty
);
1050 vty_clear_buf (struct vty
*vty
)
1052 memset (vty
->buf
, 0, vty
->max
);
1055 /* ^C stop current input and do not add command line to the history. */
1057 vty_stop_input (struct vty
*vty
)
1059 vty
->cp
= vty
->length
= 0;
1060 vty_clear_buf (vty
);
1061 vty_out (vty
, "%s", VTY_NEWLINE
);
1067 /* Nothing to do. */
1070 case INTERFACE_NODE
:
1080 case KEYCHAIN_KEY_NODE
:
1083 vty_config_unlock (vty
);
1084 vty
->node
= ENABLE_NODE
;
1087 /* Unknown node, we have to ignore it. */
1092 /* Set history pointer to the latest one. */
1093 vty
->hp
= vty
->hindex
;
1096 /* Add current command line to the history buffer. */
1098 vty_hist_add (struct vty
*vty
)
1102 if (vty
->length
== 0)
1105 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1107 /* Ignore the same string as previous one. */
1108 if (vty
->hist
[index
])
1109 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1111 vty
->hp
= vty
->hindex
;
1115 /* Insert history entry. */
1116 if (vty
->hist
[vty
->hindex
])
1117 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1118 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1120 /* History index rotation. */
1122 if (vty
->hindex
== VTY_MAXHIST
)
1125 vty
->hp
= vty
->hindex
;
1128 /* #define TELNET_OPTION_DEBUG */
1130 /* Get telnet window size. */
1132 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1134 #ifdef TELNET_OPTION_DEBUG
1137 for (i
= 0; i
< nbytes
; i
++)
1142 vty_out (vty
, "IAC ");
1145 vty_out (vty
, "WILL ");
1148 vty_out (vty
, "WONT ");
1151 vty_out (vty
, "DO ");
1154 vty_out (vty
, "DONT ");
1157 vty_out (vty
, "SB ");
1160 vty_out (vty
, "SE ");
1163 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1166 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1169 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1172 vty_out (vty
, "%x ", buf
[i
]);
1176 vty_out (vty
, "%s", VTY_NEWLINE
);
1178 #endif /* TELNET_OPTION_DEBUG */
1184 vty
->iac_sb_in_progress
= 1;
1189 if (!vty
->iac_sb_in_progress
)
1192 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1194 vty
->iac_sb_in_progress
= 0;
1197 switch (vty
->sb_buf
[0])
1200 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1201 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1202 "should send %d characters, but we received %lu",
1203 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1204 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1205 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1206 "too small to handle the telnet NAWS option",
1207 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1210 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1211 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1212 #ifdef TELNET_OPTION_DEBUG
1213 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1214 "width %d, height %d%s",
1215 vty
->width
, vty
->height
, VTY_NEWLINE
);
1220 vty
->iac_sb_in_progress
= 0;
1230 /* Execute current command line. */
1232 vty_execute (struct vty
*vty
)
1241 case AUTH_ENABLE_NODE
:
1242 vty_auth (vty
, vty
->buf
);
1245 ret
= vty_command (vty
, vty
->buf
);
1246 if (vty
->type
== VTY_TERM
)
1251 /* Clear command line buffer. */
1252 vty
->cp
= vty
->length
= 0;
1253 vty_clear_buf (vty
);
1255 if (vty
->status
!= VTY_CLOSE
)
1261 #define CONTROL(X) ((X) - '@')
1262 #define VTY_NORMAL 0
1263 #define VTY_PRE_ESCAPE 1
1264 #define VTY_ESCAPE 2
1266 /* Escape character command map. */
1268 vty_escape_map (unsigned char c
, struct vty
*vty
)
1273 vty_previous_line (vty
);
1276 vty_next_line (vty
);
1279 vty_forward_char (vty
);
1282 vty_backward_char (vty
);
1288 /* Go back to normal mode. */
1289 vty
->escape
= VTY_NORMAL
;
1292 /* Quit print out to the buffer. */
1294 vty_buffer_reset (struct vty
*vty
)
1296 buffer_reset (vty
->obuf
);
1298 vty_redraw_line (vty
);
1301 /* Read data via vty socket. */
1303 vty_read (struct thread
*thread
)
1307 unsigned char buf
[VTY_READ_BUFSIZ
];
1309 int vty_sock
= THREAD_FD (thread
);
1310 struct vty
*vty
= THREAD_ARG (thread
);
1313 /* Read raw data from socket */
1314 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1318 if (ERRNO_IO_RETRY(errno
))
1320 vty_event (VTY_READ
, vty_sock
, vty
);
1323 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1324 __func__
, vty
->fd
, safe_strerror(errno
));
1326 buffer_reset(vty
->obuf
);
1327 vty
->status
= VTY_CLOSE
;
1330 for (i
= 0; i
< nbytes
; i
++)
1345 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1347 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1348 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1355 /* In case of telnet command */
1357 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1364 if (vty
->status
== VTY_MORE
)
1371 vty_buffer_reset (vty
);
1373 #if 0 /* More line does not work for "show ip bgp". */
1376 vty
->status
= VTY_MORELINE
;
1385 /* Escape character. */
1386 if (vty
->escape
== VTY_ESCAPE
)
1388 vty_escape_map (buf
[i
], vty
);
1392 /* Pre-escape status. */
1393 if (vty
->escape
== VTY_PRE_ESCAPE
)
1398 vty
->escape
= VTY_ESCAPE
;
1401 vty_backward_word (vty
);
1402 vty
->escape
= VTY_NORMAL
;
1405 vty_forward_word (vty
);
1406 vty
->escape
= VTY_NORMAL
;
1409 vty_forward_kill_word (vty
);
1410 vty
->escape
= VTY_NORMAL
;
1414 vty_backward_kill_word (vty
);
1415 vty
->escape
= VTY_NORMAL
;
1418 vty
->escape
= VTY_NORMAL
;
1427 vty_beginning_of_line (vty
);
1430 vty_backward_char (vty
);
1433 vty_stop_input (vty
);
1436 vty_delete_char (vty
);
1439 vty_end_of_line (vty
);
1442 vty_forward_char (vty
);
1446 vty_delete_backward_char (vty
);
1449 vty_kill_line (vty
);
1452 vty_next_line (vty
);
1455 vty_previous_line (vty
);
1458 vty_transpose_chars (vty
);
1461 vty_kill_line_from_beginning (vty
);
1464 vty_backward_kill_word (vty
);
1467 vty_end_config (vty
);
1471 vty_out (vty
, "%s", VTY_NEWLINE
);
1475 vty_complete_command (vty
);
1478 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1479 vty_self_insert (vty
, buf
[i
]);
1481 vty_describe_command (vty
);
1484 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1486 vty
->escape
= VTY_ESCAPE
;
1490 vty
->escape
= VTY_PRE_ESCAPE
;
1493 if (buf
[i
] > 31 && buf
[i
] < 127)
1494 vty_self_insert (vty
, buf
[i
]);
1500 if (vty
->status
== VTY_CLOSE
)
1504 vty_event (VTY_WRITE
, vty_sock
, vty
);
1505 vty_event (VTY_READ
, vty_sock
, vty
);
1510 /* Flush buffer to the vty. */
1512 vty_flush (struct thread
*thread
)
1515 buffer_status_t flushrc
;
1516 int vty_sock
= THREAD_FD (thread
);
1517 struct vty
*vty
= THREAD_ARG (thread
);
1519 vty
->t_write
= NULL
;
1521 /* Tempolary disable read thread. */
1522 if ((vty
->lines
== 0) && vty
->t_read
)
1524 thread_cancel (vty
->t_read
);
1528 /* Function execution continue. */
1529 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1531 /* N.B. if width is 0, that means we don't know the window size. */
1532 if ((vty
->lines
== 0) || (vty
->width
== 0))
1533 flushrc
= buffer_flush_available(vty
->obuf
, vty
->fd
);
1534 else if (vty
->status
== VTY_MORELINE
)
1535 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1538 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1539 vty
->lines
>= 0 ? vty
->lines
:
1545 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1547 buffer_reset(vty
->obuf
);
1551 if (vty
->status
== VTY_CLOSE
)
1555 vty
->status
= VTY_NORMAL
;
1556 if (vty
->lines
== 0)
1557 vty_event (VTY_READ
, vty_sock
, vty
);
1560 case BUFFER_PENDING
:
1561 /* There is more data waiting to be written. */
1562 vty
->status
= VTY_MORE
;
1563 if (vty
->lines
== 0)
1564 vty_event (VTY_WRITE
, vty_sock
, vty
);
1571 /* Create new vty structure. */
1573 vty_create (int vty_sock
, union sockunion
*su
)
1577 /* Allocate new vty structure and set up default values. */
1580 vty
->type
= VTY_TERM
;
1581 vty
->address
= sockunion_su2str (su
);
1582 if (no_password_check
)
1585 vty
->node
= ENABLE_NODE
;
1587 vty
->node
= VIEW_NODE
;
1590 vty
->node
= AUTH_NODE
;
1593 vty_clear_buf (vty
);
1595 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1598 vector_set_index (vtyvec
, vty_sock
, vty
);
1599 vty
->status
= VTY_NORMAL
;
1600 vty
->v_timeout
= vty_timeout_val
;
1601 if (host
.lines
>= 0)
1602 vty
->lines
= host
.lines
;
1606 vty
->iac_sb_in_progress
= 0;
1609 if (! no_password_check
)
1611 /* Vty is not available if password isn't set. */
1612 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1614 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1615 vty
->status
= VTY_CLOSE
;
1621 /* Say hello to the world. */
1623 if (! no_password_check
)
1624 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1626 /* Setting up terminal. */
1627 vty_will_echo (vty
);
1628 vty_will_suppress_go_ahead (vty
);
1630 vty_dont_linemode (vty
);
1631 vty_do_window_size (vty
);
1632 /* vty_dont_lflow_ahead (vty); */
1636 /* Add read/write thread. */
1637 vty_event (VTY_WRITE
, vty_sock
, vty
);
1638 vty_event (VTY_READ
, vty_sock
, vty
);
1643 /* Accept connection from the network. */
1645 vty_accept (struct thread
*thread
)
1653 struct prefix
*p
= NULL
;
1654 struct access_list
*acl
= NULL
;
1656 accept_sock
= THREAD_FD (thread
);
1658 /* We continue hearing vty socket. */
1659 vty_event (VTY_SERV
, accept_sock
, NULL
);
1661 memset (&su
, 0, sizeof (union sockunion
));
1663 /* We can handle IPv4 or IPv6 socket. */
1664 vty_sock
= sockunion_accept (accept_sock
, &su
);
1667 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1670 set_nonblocking(vty_sock
);
1672 p
= sockunion2hostprefix (&su
);
1674 /* VTY's accesslist apply. */
1675 if (p
->family
== AF_INET
&& vty_accesslist_name
)
1677 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1678 (access_list_apply (acl
, p
) == FILTER_DENY
))
1681 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1682 (buf
= sockunion_su2str (&su
)));
1686 /* continue accepting connections */
1687 vty_event (VTY_SERV
, accept_sock
, NULL
);
1696 /* VTY's ipv6 accesslist apply. */
1697 if (p
->family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1699 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1700 (access_list_apply (acl
, p
) == FILTER_DENY
))
1703 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1704 (buf
= sockunion_su2str (&su
)));
1708 /* continue accepting connections */
1709 vty_event (VTY_SERV
, accept_sock
, NULL
);
1716 #endif /* HAVE_IPV6 */
1721 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1722 (char *) &on
, sizeof (on
));
1724 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1725 safe_strerror (errno
));
1727 vty
= vty_create (vty_sock
, &su
);
1732 #if defined(HAVE_IPV6) && !defined(NRL)
1734 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1737 struct addrinfo req
;
1738 struct addrinfo
*ainfo
;
1739 struct addrinfo
*ainfo_save
;
1741 char port_str
[BUFSIZ
];
1743 memset (&req
, 0, sizeof (struct addrinfo
));
1744 req
.ai_flags
= AI_PASSIVE
;
1745 req
.ai_family
= AF_UNSPEC
;
1746 req
.ai_socktype
= SOCK_STREAM
;
1747 sprintf (port_str
, "%d", port
);
1748 port_str
[sizeof (port_str
) - 1] = '\0';
1750 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1754 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1762 if (ainfo
->ai_family
!= AF_INET
1764 && ainfo
->ai_family
!= AF_INET6
1765 #endif /* HAVE_IPV6 */
1769 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1773 sockopt_reuseaddr (sock
);
1774 sockopt_reuseport (sock
);
1776 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1779 close (sock
); /* Avoid sd leak. */
1783 ret
= listen (sock
, 3);
1786 close (sock
); /* Avoid sd leak. */
1790 vty_event (VTY_SERV
, sock
, NULL
);
1792 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1794 freeaddrinfo (ainfo_save
);
1796 #endif /* HAVE_IPV6 && ! NRL */
1798 /* Make vty server socket. */
1800 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1807 memset (&su
, 0, sizeof (union sockunion
));
1808 su
.sa
.sa_family
= family
;
1813 naddr
=&su
.sin
.sin_addr
;
1816 naddr
=&su
.sin6
.sin6_addr
;
1821 switch(inet_pton(family
,addr
,naddr
))
1824 zlog_err("bad address %s",addr
);
1828 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
1832 /* Make new socket. */
1833 accept_sock
= sockunion_stream_socket (&su
);
1834 if (accept_sock
< 0)
1837 /* This is server, so reuse address. */
1838 sockopt_reuseaddr (accept_sock
);
1839 sockopt_reuseport (accept_sock
);
1841 /* Bind socket to universal address and given port. */
1842 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
1845 zlog_warn("can't bind socket");
1846 close (accept_sock
); /* Avoid sd leak. */
1850 /* Listen socket under queue 3. */
1851 ret
= listen (accept_sock
, 3);
1854 zlog (NULL
, LOG_WARNING
, "can't listen socket");
1855 close (accept_sock
); /* Avoid sd leak. */
1859 /* Add vty server event. */
1860 vty_event (VTY_SERV
, accept_sock
, NULL
);
1864 /* For sockaddr_un. */
1867 /* VTY shell UNIX domain socket. */
1869 vty_serv_un (const char *path
)
1873 struct sockaddr_un serv
;
1875 struct zprivs_ids_t ids
;
1877 /* First of all, unlink existing socket */
1881 old_mask
= umask (0007);
1883 /* Make UNIX domain socket. */
1884 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1887 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
1891 /* Make server socket. */
1892 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1893 serv
.sun_family
= AF_UNIX
;
1894 strncpy (serv
.sun_path
, path
, strlen (path
));
1896 len
= serv
.sun_len
= SUN_LEN(&serv
);
1898 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1899 #endif /* HAVE_SUN_LEN */
1901 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1904 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1905 close (sock
); /* Avoid sd leak. */
1909 ret
= listen (sock
, 5);
1912 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
1913 close (sock
); /* Avoid sd leak. */
1919 zprivs_get_ids(&ids
);
1921 if (ids
.gid_vty
> 0)
1923 /* set group of socket */
1924 if ( chown (path
, -1, ids
.gid_vty
) )
1926 zlog_err ("vty_serv_un: could chown socket, %s",
1927 safe_strerror (errno
) );
1931 vty_event (VTYSH_SERV
, sock
, NULL
);
1934 /* #define VTYSH_DEBUG 1 */
1937 vtysh_accept (struct thread
*thread
)
1942 struct sockaddr_un client
;
1945 accept_sock
= THREAD_FD (thread
);
1947 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
1949 memset (&client
, 0, sizeof (struct sockaddr_un
));
1950 client_len
= sizeof (struct sockaddr_un
);
1952 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
1953 (socklen_t
*) &client_len
);
1957 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1961 if (set_nonblocking(sock
) < 0)
1963 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
1964 " %s, closing", sock
, safe_strerror (errno
));
1970 printf ("VTY shell accept\n");
1971 #endif /* VTYSH_DEBUG */
1975 vty
->type
= VTY_SHELL_SERV
;
1976 vty
->node
= VIEW_NODE
;
1978 vty_event (VTYSH_READ
, sock
, vty
);
1984 vtysh_flush(struct vty
*vty
)
1986 switch (buffer_flush_available(vty
->obuf
, vty
->fd
))
1988 case BUFFER_PENDING
:
1989 vty_event(VTYSH_WRITE
, vty
->fd
, vty
);
1992 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
1993 buffer_reset(vty
->obuf
);
2004 vtysh_read (struct thread
*thread
)
2010 unsigned char buf
[VTY_READ_BUFSIZ
];
2012 u_char header
[4] = {0, 0, 0, 0};
2014 sock
= THREAD_FD (thread
);
2015 vty
= THREAD_ARG (thread
);
2018 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2022 if (ERRNO_IO_RETRY(errno
))
2024 vty_event (VTYSH_READ
, sock
, vty
);
2027 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2028 __func__
, sock
, safe_strerror(errno
));
2030 buffer_reset(vty
->obuf
);
2033 printf ("close vtysh\n");
2034 #endif /* VTYSH_DEBUG */
2039 printf ("line: %.*s\n", nbytes
, buf
);
2040 #endif /* VTYSH_DEBUG */
2042 for (p
= buf
; p
< buf
+nbytes
; p
++)
2044 vty_ensure(vty
, vty
->length
+1);
2045 vty
->buf
[vty
->length
++] = *p
;
2048 /* Pass this line to parser. */
2049 ret
= vty_execute (vty
);
2050 /* Note that vty_execute clears the command buffer and resets
2051 vty->length to 0. */
2053 /* Return result. */
2055 printf ("result: %d\n", ret
);
2056 printf ("vtysh node: %d\n", vty
->node
);
2057 #endif /* VTYSH_DEBUG */
2060 buffer_put(vty
->obuf
, header
, 4);
2062 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2063 /* Try to flush results; exit if a write error occurs. */
2068 vty_event (VTYSH_READ
, sock
, vty
);
2074 vtysh_write (struct thread
*thread
)
2076 struct vty
*vty
= THREAD_ARG (thread
);
2078 vty
->t_write
= NULL
;
2085 /* Determine address family to bind. */
2087 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2089 /* If port is set to 0, do not listen on TCP/IP at all! */
2095 vty_serv_sock_family (addr
, port
, AF_INET
);
2096 vty_serv_sock_family (addr
, port
, AF_INET6
);
2098 vty_serv_sock_addrinfo (addr
, port
);
2100 #else /* ! HAVE_IPV6 */
2101 vty_serv_sock_family (addr
,port
, AF_INET
);
2102 #endif /* HAVE_IPV6 */
2110 /* Close vty interface. */
2112 vty_close (struct vty
*vty
)
2116 /* Cancel threads.*/
2118 thread_cancel (vty
->t_read
);
2120 thread_cancel (vty
->t_write
);
2122 thread_cancel (vty
->t_timeout
);
2125 buffer_flush_all (vty
->obuf
, vty
->fd
);
2127 /* Free input buffer. */
2128 buffer_free (vty
->obuf
);
2130 /* Free command history. */
2131 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2133 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2136 vector_unset (vtyvec
, vty
->fd
);
2143 XFREE (0, vty
->address
);
2145 XFREE (MTYPE_VTY
, vty
->buf
);
2147 /* Check configure. */
2148 vty_config_unlock (vty
);
2151 XFREE (MTYPE_VTY
, vty
);
2154 /* When time out occur output message then close connection. */
2156 vty_timeout (struct thread
*thread
)
2160 vty
= THREAD_ARG (thread
);
2161 vty
->t_timeout
= NULL
;
2165 buffer_reset (vty
->obuf
);
2166 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2168 /* Close connection. */
2169 vty
->status
= VTY_CLOSE
;
2175 /* Read up configuration file from file_name. */
2177 vty_read_file (FILE *confp
)
2183 vty
->fd
= 0; /* stdout */
2184 vty
->type
= VTY_TERM
;
2185 vty
->node
= CONFIG_NODE
;
2187 /* Execute configuration file */
2188 ret
= config_from_file (vty
, confp
);
2190 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2194 case CMD_ERR_AMBIGUOUS
:
2195 fprintf (stderr
, "Ambiguous command.\n");
2197 case CMD_ERR_NO_MATCH
:
2198 fprintf (stderr
, "There is no such command.\n");
2201 fprintf (stderr
, "Error occured during reading below line.\n%s\n",
2211 vty_use_backup_config (char *fullpath
)
2213 char *fullpath_sav
, *fullpath_tmp
;
2220 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2221 strcpy (fullpath_sav
, fullpath
);
2222 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2223 if (stat (fullpath_sav
, &buf
) == -1)
2225 free (fullpath_sav
);
2229 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2230 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2232 /* Open file to configuration write. */
2233 tmp
= mkstemp (fullpath_tmp
);
2236 free (fullpath_sav
);
2237 free (fullpath_tmp
);
2241 sav
= open (fullpath_sav
, O_RDONLY
);
2244 unlink (fullpath_tmp
);
2245 free (fullpath_sav
);
2246 free (fullpath_tmp
);
2250 while((c
= read (sav
, buffer
, 512)) > 0)
2251 write (tmp
, buffer
, c
);
2256 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2258 unlink (fullpath_tmp
);
2259 free (fullpath_sav
);
2260 free (fullpath_tmp
);
2264 if (link (fullpath_tmp
, fullpath
) == 0)
2265 ret
= fopen (fullpath
, "r");
2267 unlink (fullpath_tmp
);
2269 free (fullpath_sav
);
2270 free (fullpath_tmp
);
2274 /* Read up configuration file from file_name. */
2276 vty_read_config (char *config_file
,
2277 char *config_default_dir
)
2279 char cwd
[MAXPATHLEN
];
2283 /* If -f flag specified. */
2284 if (config_file
!= NULL
)
2286 if (! IS_DIRECTORY_SEP (config_file
[0]))
2288 getcwd (cwd
, MAXPATHLEN
);
2289 fullpath
= XMALLOC (MTYPE_TMP
,
2290 strlen (cwd
) + strlen (config_file
) + 2);
2291 sprintf (fullpath
, "%s/%s", cwd
, config_file
);
2294 fullpath
= config_file
;
2296 confp
= fopen (fullpath
, "r");
2300 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2301 __func__
, fullpath
, safe_strerror (errno
));
2303 confp
= vty_use_backup_config (fullpath
);
2305 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2308 fprintf (stderr
, "can't open configuration file [%s]\n",
2318 struct stat conf_stat
;
2320 /* !!!!PLEASE LEAVE!!!!
2321 * This is NEEDED for use with vtysh -b, or else you can get
2322 * a real configuration food fight with a lot garbage in the
2323 * merged configuration file it creates coming from the per
2324 * daemon configuration files. This also allows the daemons
2325 * to start if there default configuration file is not
2326 * present or ignore them, as needed when using vtysh -b to
2327 * configure the daemons at boot - MAG
2330 /* Stat for vtysh Zebra.conf, if found startup and wait for
2331 * boot configuration
2334 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2336 ret
= stat (integrate_default
, &conf_stat
);
2342 confp
= fopen (config_default_dir
, "r");
2345 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2346 __func__
, config_default_dir
, safe_strerror (errno
));
2348 confp
= vty_use_backup_config (config_default_dir
);
2351 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2352 fullpath
= config_default_dir
;
2356 fprintf (stderr
, "can't open configuration file [%s]\n",
2357 config_default_dir
);
2362 fullpath
= config_default_dir
;
2365 vty_read_file (confp
);
2369 host_config_set (fullpath
);
2372 /* Small utility function which output log to the VTY. */
2374 vty_log (const char *level
, const char *proto_str
,
2375 const char *format
, va_list va
)
2380 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2381 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2386 vty_log_out (vty
, level
, proto_str
, format
, ac
);
2391 /* Async-signal-safe version of vty_log for fixed strings. */
2393 vty_log_fixed (const char *buf
, size_t len
)
2396 struct iovec iov
[2];
2398 iov
[0].iov_base
= (void *)buf
;
2399 iov
[0].iov_len
= len
;
2400 iov
[1].iov_base
= (void *)"\r\n";
2403 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2406 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2407 /* N.B. We don't care about the return code, since process is
2408 most likely just about to die anyway. */
2409 writev(vty
->fd
, iov
, 2);
2414 vty_config_lock (struct vty
*vty
)
2416 if (vty_config
== 0)
2425 vty_config_unlock (struct vty
*vty
)
2427 if (vty_config
== 1 && vty
->config
== 1)
2435 /* Master of the threads. */
2436 static struct thread_master
*master
;
2439 vty_event (enum event event
, int sock
, struct vty
*vty
)
2441 struct thread
*vty_serv_thread
;
2446 vty_serv_thread
= thread_add_read (master
, vty_accept
, vty
, sock
);
2447 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2451 thread_add_read (master
, vtysh_accept
, vty
, sock
);
2454 vty
->t_read
= thread_add_read (master
, vtysh_read
, vty
, sock
);
2457 vty
->t_write
= thread_add_write (master
, vtysh_write
, vty
, sock
);
2461 vty
->t_read
= thread_add_read (master
, vty_read
, vty
, sock
);
2463 /* Time out treatment. */
2467 thread_cancel (vty
->t_timeout
);
2469 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2474 vty
->t_write
= thread_add_write (master
, vty_flush
, vty
, sock
);
2476 case VTY_TIMEOUT_RESET
:
2479 thread_cancel (vty
->t_timeout
);
2480 vty
->t_timeout
= NULL
;
2485 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2494 "Display who is on vty\n")
2499 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2500 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2501 vty_out (vty
, "%svty[%d] connected from %s.%s",
2502 v
->config
? "*" : " ",
2503 i
, v
->address
, VTY_NEWLINE
);
2507 /* Move to vty configuration mode. */
2511 "Configure a terminal line\n"
2512 "Virtual terminal\n")
2514 vty
->node
= VTY_NODE
;
2518 /* Set time out value. */
2520 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2522 unsigned long timeout
= 0;
2524 /* min_str and sec_str are already checked by parser. So it must be
2525 all digit string. */
2528 timeout
= strtol (min_str
, NULL
, 10);
2532 timeout
+= strtol (sec_str
, NULL
, 10);
2534 vty_timeout_val
= timeout
;
2535 vty
->v_timeout
= timeout
;
2536 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2542 DEFUN (exec_timeout_min
,
2543 exec_timeout_min_cmd
,
2544 "exec-timeout <0-35791>",
2545 "Set timeout value\n"
2546 "Timeout value in minutes\n")
2548 return exec_timeout (vty
, argv
[0], NULL
);
2551 DEFUN (exec_timeout_sec
,
2552 exec_timeout_sec_cmd
,
2553 "exec-timeout <0-35791> <0-2147483>",
2554 "Set the EXEC timeout\n"
2555 "Timeout in minutes\n"
2556 "Timeout in seconds\n")
2558 return exec_timeout (vty
, argv
[0], argv
[1]);
2561 DEFUN (no_exec_timeout
,
2562 no_exec_timeout_cmd
,
2565 "Set the EXEC timeout\n")
2567 return exec_timeout (vty
, NULL
, NULL
);
2570 /* Set vty access class. */
2571 DEFUN (vty_access_class
,
2572 vty_access_class_cmd
,
2573 "access-class WORD",
2574 "Filter connections based on an IP access list\n"
2577 if (vty_accesslist_name
)
2578 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2580 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2585 /* Clear vty access class. */
2586 DEFUN (no_vty_access_class
,
2587 no_vty_access_class_cmd
,
2588 "no access-class [WORD]",
2590 "Filter connections based on an IP access list\n"
2593 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2595 vty_out (vty
, "Access-class is not currently applied to vty%s",
2600 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2602 vty_accesslist_name
= NULL
;
2608 /* Set vty access class. */
2609 DEFUN (vty_ipv6_access_class
,
2610 vty_ipv6_access_class_cmd
,
2611 "ipv6 access-class WORD",
2613 "Filter connections based on an IP access list\n"
2614 "IPv6 access list\n")
2616 if (vty_ipv6_accesslist_name
)
2617 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2619 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2624 /* Clear vty access class. */
2625 DEFUN (no_vty_ipv6_access_class
,
2626 no_vty_ipv6_access_class_cmd
,
2627 "no ipv6 access-class [WORD]",
2630 "Filter connections based on an IP access list\n"
2631 "IPv6 access list\n")
2633 if (! vty_ipv6_accesslist_name
||
2634 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2636 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2641 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2643 vty_ipv6_accesslist_name
= NULL
;
2647 #endif /* HAVE_IPV6 */
2653 "Enable password checking\n")
2655 no_password_check
= 0;
2659 DEFUN (no_vty_login
,
2663 "Enable password checking\n")
2665 no_password_check
= 1;
2669 DEFUN (service_advanced_vty
,
2670 service_advanced_vty_cmd
,
2671 "service advanced-vty",
2672 "Set up miscellaneous service\n"
2673 "Enable advanced mode vty interface\n")
2679 DEFUN (no_service_advanced_vty
,
2680 no_service_advanced_vty_cmd
,
2681 "no service advanced-vty",
2683 "Set up miscellaneous service\n"
2684 "Enable advanced mode vty interface\n")
2690 DEFUN (terminal_monitor
,
2691 terminal_monitor_cmd
,
2693 "Set terminal line parameters\n"
2694 "Copy debug output to the current terminal line\n")
2700 DEFUN (terminal_no_monitor
,
2701 terminal_no_monitor_cmd
,
2702 "terminal no monitor",
2703 "Set terminal line parameters\n"
2705 "Copy debug output to the current terminal line\n")
2711 DEFUN (show_history
,
2715 "Display the session command history\n")
2719 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2721 if (index
== VTY_MAXHIST
)
2727 if (vty
->hist
[index
] != NULL
)
2728 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2736 /* Display current configuration. */
2738 vty_config_write (struct vty
*vty
)
2740 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2742 if (vty_accesslist_name
)
2743 vty_out (vty
, " access-class %s%s",
2744 vty_accesslist_name
, VTY_NEWLINE
);
2746 if (vty_ipv6_accesslist_name
)
2747 vty_out (vty
, " ipv6 access-class %s%s",
2748 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
2751 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2752 vty_out (vty
, " exec-timeout %ld %ld%s",
2753 vty_timeout_val
/ 60,
2754 vty_timeout_val
% 60, VTY_NEWLINE
);
2757 if (no_password_check
)
2758 vty_out (vty
, " no login%s", VTY_NEWLINE
);
2760 vty_out (vty
, "!%s", VTY_NEWLINE
);
2765 struct cmd_node vty_node
=
2768 "%s(config-line)# ",
2772 /* Reset all VTY status. */
2778 struct thread
*vty_serv_thread
;
2780 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2781 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2783 buffer_reset (vty
->obuf
);
2784 vty
->status
= VTY_CLOSE
;
2788 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
2789 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2791 thread_cancel (vty_serv_thread
);
2792 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2796 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2798 if (vty_accesslist_name
)
2800 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2801 vty_accesslist_name
= NULL
;
2804 if (vty_ipv6_accesslist_name
)
2806 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2807 vty_ipv6_accesslist_name
= NULL
;
2814 char cwd
[MAXPATHLEN
];
2817 c
= getcwd (cwd
, MAXPATHLEN
);
2822 getcwd (cwd
, MAXPATHLEN
);
2825 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
2826 strcpy (vty_cwd
, cwd
);
2836 vty_shell (struct vty
*vty
)
2838 return vty
->type
== VTY_SHELL
? 1 : 0;
2842 vty_shell_serv (struct vty
*vty
)
2844 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2850 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2853 /* Install vty's own commands like `who' command. */
2855 vty_init (struct thread_master
*master_thread
)
2857 /* For further configuration read, preserve current directory. */
2860 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2862 master
= master_thread
;
2864 /* Initilize server thread vector. */
2865 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
2867 /* Install bgp top node. */
2868 install_node (&vty_node
, vty_config_write
);
2870 install_element (VIEW_NODE
, &config_who_cmd
);
2871 install_element (VIEW_NODE
, &show_history_cmd
);
2872 install_element (ENABLE_NODE
, &config_who_cmd
);
2873 install_element (CONFIG_NODE
, &line_vty_cmd
);
2874 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
2875 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
2876 install_element (CONFIG_NODE
, &show_history_cmd
);
2877 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
2878 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
2879 install_element (ENABLE_NODE
, &show_history_cmd
);
2881 install_default (VTY_NODE
);
2882 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
2883 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
2884 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
2885 install_element (VTY_NODE
, &vty_access_class_cmd
);
2886 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
2887 install_element (VTY_NODE
, &vty_login_cmd
);
2888 install_element (VTY_NODE
, &no_vty_login_cmd
);
2890 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
2891 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
2892 #endif /* HAVE_IPV6 */