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"
53 /* Minimum size of output buffers; to be rounded up to multiple of pagesize. */
54 #define VTY_OBUF_SIZE 4096
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_write (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
)
158 snprintf (buf
, sizeof buf
, "%s: %s: ", level
, proto_str
);
160 snprintf (buf
, sizeof buf
, "%s: ", proto_str
);
161 write (vty
->fd
, buf
, strlen (buf
));
163 len
= vsnprintf (buf
, sizeof buf
, format
, va
);
166 write (vty
->fd
, (u_char
*)buf
, len
);
168 snprintf (buf
, sizeof buf
, "\r\n");
169 write (vty
->fd
, buf
, 2);
174 /* Output current time to the vty. */
176 vty_time_print (struct vty
*vty
, int cr
)
185 tm
= localtime (&clock
);
187 ret
= strftime (buf
, TIME_BUF
, "%Y/%m/%d %H:%M:%S", tm
);
190 zlog (NULL
, LOG_INFO
, "strftime error");
194 vty_out (vty
, "%s\n", buf
);
196 vty_out (vty
, "%s ", buf
);
201 /* Say hello to vty interface. */
203 vty_hello (struct vty
*vty
)
206 vty_out (vty
, host
.motd
);
209 /* Put out prompt and wait input from user. */
211 vty_prompt (struct vty
*vty
)
213 struct utsname names
;
216 if (vty
->type
== VTY_TERM
)
218 hostname
= host
.name
;
222 hostname
= names
.nodename
;
224 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
228 /* Send WILL TELOPT_ECHO to remote server. */
230 vty_will_echo (struct vty
*vty
)
232 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
233 vty_out (vty
, "%s", cmd
);
236 /* Make suppress Go-Ahead telnet option. */
238 vty_will_suppress_go_ahead (struct vty
*vty
)
240 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
241 vty_out (vty
, "%s", cmd
);
244 /* Make don't use linemode over telnet. */
246 vty_dont_linemode (struct vty
*vty
)
248 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
249 vty_out (vty
, "%s", cmd
);
252 /* Use window size. */
254 vty_do_window_size (struct vty
*vty
)
256 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
257 vty_out (vty
, "%s", cmd
);
260 #if 0 /* Currently not used. */
261 /* Make don't use lflow vty interface. */
263 vty_dont_lflow_ahead (struct vty
*vty
)
265 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
266 vty_out (vty
, "%s", cmd
);
270 /* Allocate new vty struct. */
274 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
275 int pgsz
= getpagesize();
277 new->obuf
= (struct buffer
*) buffer_new ((((VTY_OBUF_SIZE
-1)/pgsz
)+1)*pgsz
);
278 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
279 new->max
= VTY_BUFSIZ
;
280 new->sb_buffer
= NULL
;
285 /* Authentication of vty */
287 vty_auth (struct vty
*vty
, char *buf
)
290 enum node_type next_node
= 0;
292 char *crypt (const char *, const char *);
298 passwd
= host
.password_encrypt
;
300 passwd
= host
.password
;
302 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
304 next_node
= VIEW_NODE
;
306 case AUTH_ENABLE_NODE
:
308 passwd
= host
.enable_encrypt
;
310 passwd
= host
.enable
;
311 next_node
= ENABLE_NODE
;
318 fail
= strcmp (crypt(buf
, passwd
), passwd
);
320 fail
= strcmp (buf
, passwd
);
328 vty
->node
= next_node
; /* Success ! */
335 if (vty
->node
== AUTH_NODE
)
337 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
338 vty
->status
= VTY_CLOSE
;
342 /* AUTH_ENABLE_NODE */
344 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
345 vty
->node
= VIEW_NODE
;
351 /* Command execution over the vty interface. */
353 vty_command (struct vty
*vty
, char *buf
)
358 /* Split readline string up into the vector */
359 vline
= cmd_make_strvec (buf
);
364 ret
= cmd_execute_command (vline
, vty
, NULL
);
366 if (ret
!= CMD_SUCCESS
)
370 if (vty
->type
== VTY_FILE
)
371 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
373 case CMD_ERR_AMBIGUOUS
:
374 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
376 case CMD_ERR_NO_MATCH
:
377 vty_out (vty
, "%% Unknown command.%s", VTY_NEWLINE
);
379 case CMD_ERR_INCOMPLETE
:
380 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
383 cmd_free_strvec (vline
);
388 char telnet_backward_char
= 0x08;
389 char telnet_space_char
= ' ';
391 /* Basic function to write buffer to vty. */
393 vty_write (struct vty
*vty
, char *buf
, size_t nbytes
)
395 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
398 /* Should we do buffering here ? And make vty_flush (vty) ? */
399 buffer_write (vty
->obuf
, (u_char
*)buf
, nbytes
);
402 /* Ensure length of input buffer. Is buffer is short, double it. */
404 vty_ensure (struct vty
*vty
, int length
)
406 if (vty
->max
<= length
)
409 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
413 /* Basic function to insert character into vty. */
415 vty_self_insert (struct vty
*vty
, char c
)
420 vty_ensure (vty
, vty
->length
+ 1);
421 length
= vty
->length
- vty
->cp
;
422 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
423 vty
->buf
[vty
->cp
] = c
;
425 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
426 for (i
= 0; i
< length
; i
++)
427 vty_write (vty
, &telnet_backward_char
, 1);
433 /* Self insert character 'c' in overwrite mode. */
435 vty_self_insert_overwrite (struct vty
*vty
, char c
)
437 vty_ensure (vty
, vty
->length
+ 1);
438 vty
->buf
[vty
->cp
++] = c
;
440 if (vty
->cp
> vty
->length
)
443 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
446 vty_write (vty
, &c
, 1);
449 /* Insert a word into vty interface with overwrite mode. */
451 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
453 int len
= strlen (str
);
454 vty_write (vty
, str
, len
);
455 strcpy (&vty
->buf
[vty
->cp
], str
);
457 vty
->length
= vty
->cp
;
460 /* Forward character. */
462 vty_forward_char (struct vty
*vty
)
464 if (vty
->cp
< vty
->length
)
466 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
471 /* Backward character. */
473 vty_backward_char (struct vty
*vty
)
478 vty_write (vty
, &telnet_backward_char
, 1);
482 /* Move to the beginning of the line. */
484 vty_beginning_of_line (struct vty
*vty
)
487 vty_backward_char (vty
);
490 /* Move to the end of the line. */
492 vty_end_of_line (struct vty
*vty
)
494 while (vty
->cp
< vty
->length
)
495 vty_forward_char (vty
);
498 static void vty_kill_line_from_beginning (struct vty
*);
499 static void vty_redraw_line (struct vty
*);
501 /* Print command line history. This function is called from
502 vty_next_line and vty_previous_line. */
504 vty_history_print (struct vty
*vty
)
508 vty_kill_line_from_beginning (vty
);
510 /* Get previous line from history buffer */
511 length
= strlen (vty
->hist
[vty
->hp
]);
512 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
513 vty
->cp
= vty
->length
= length
;
515 /* Redraw current line */
516 vty_redraw_line (vty
);
519 /* Show next command line history. */
521 vty_next_line (struct vty
*vty
)
525 if (vty
->hp
== vty
->hindex
)
528 /* Try is there history exist or not. */
530 if (try_index
== (VTY_MAXHIST
- 1))
535 /* If there is not history return. */
536 if (vty
->hist
[try_index
] == NULL
)
541 vty_history_print (vty
);
544 /* Show previous command line history. */
546 vty_previous_line (struct vty
*vty
)
552 try_index
= VTY_MAXHIST
- 1;
556 if (vty
->hist
[try_index
] == NULL
)
561 vty_history_print (vty
);
564 /* This function redraw all of the command line character. */
566 vty_redraw_line (struct vty
*vty
)
568 vty_write (vty
, vty
->buf
, vty
->length
);
569 vty
->cp
= vty
->length
;
574 vty_forward_word (struct vty
*vty
)
576 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
577 vty_forward_char (vty
);
579 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
580 vty_forward_char (vty
);
583 /* Backward word without skipping training space. */
585 vty_backward_pure_word (struct vty
*vty
)
587 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
588 vty_backward_char (vty
);
593 vty_backward_word (struct vty
*vty
)
595 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
596 vty_backward_char (vty
);
598 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
599 vty_backward_char (vty
);
602 /* When '^D' is typed at the beginning of the line we move to the down
605 vty_down_level (struct vty
*vty
)
607 vty_out (vty
, "%s", VTY_NEWLINE
);
608 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
613 /* When '^Z' is received from vty, move down to the enable mode. */
615 vty_end_config (struct vty
*vty
)
617 vty_out (vty
, "%s", VTY_NEWLINE
);
640 case KEYCHAIN_KEY_NODE
:
643 vty_config_unlock (vty
);
644 vty
->node
= ENABLE_NODE
;
647 /* Unknown node, we have to ignore it. */
655 /* Delete a charcter at the current point. */
657 vty_delete_char (struct vty
*vty
)
662 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
665 if (vty
->length
== 0)
667 vty_down_level (vty
);
671 if (vty
->cp
== vty
->length
)
672 return; /* completion need here? */
674 size
= vty
->length
- vty
->cp
;
677 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
678 vty
->buf
[vty
->length
] = '\0';
680 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
681 vty_write (vty
, &telnet_space_char
, 1);
683 for (i
= 0; i
< size
; i
++)
684 vty_write (vty
, &telnet_backward_char
, 1);
687 /* Delete a character before the point. */
689 vty_delete_backward_char (struct vty
*vty
)
694 vty_backward_char (vty
);
695 vty_delete_char (vty
);
698 /* Kill rest of line from current point. */
700 vty_kill_line (struct vty
*vty
)
705 size
= vty
->length
- vty
->cp
;
710 for (i
= 0; i
< size
; i
++)
711 vty_write (vty
, &telnet_space_char
, 1);
712 for (i
= 0; i
< size
; i
++)
713 vty_write (vty
, &telnet_backward_char
, 1);
715 memset (&vty
->buf
[vty
->cp
], 0, size
);
716 vty
->length
= vty
->cp
;
719 /* Kill line from the beginning. */
721 vty_kill_line_from_beginning (struct vty
*vty
)
723 vty_beginning_of_line (vty
);
727 /* Delete a word before the point. */
729 vty_forward_kill_word (struct vty
*vty
)
731 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
732 vty_delete_char (vty
);
733 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
734 vty_delete_char (vty
);
737 /* Delete a word before the point. */
739 vty_backward_kill_word (struct vty
*vty
)
741 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
742 vty_delete_backward_char (vty
);
743 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
744 vty_delete_backward_char (vty
);
747 /* Transpose chars before or at the point. */
749 vty_transpose_chars (struct vty
*vty
)
753 /* If length is short or point is near by the beginning of line then
755 if (vty
->length
< 2 || vty
->cp
< 1)
758 /* In case of point is located at the end of the line. */
759 if (vty
->cp
== vty
->length
)
761 c1
= vty
->buf
[vty
->cp
- 1];
762 c2
= vty
->buf
[vty
->cp
- 2];
764 vty_backward_char (vty
);
765 vty_backward_char (vty
);
766 vty_self_insert_overwrite (vty
, c1
);
767 vty_self_insert_overwrite (vty
, c2
);
771 c1
= vty
->buf
[vty
->cp
];
772 c2
= vty
->buf
[vty
->cp
- 1];
774 vty_backward_char (vty
);
775 vty_self_insert_overwrite (vty
, c1
);
776 vty_self_insert_overwrite (vty
, c2
);
780 /* Do completion at vty interface. */
782 vty_complete_command (struct vty
*vty
)
786 char **matched
= NULL
;
789 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
792 vline
= cmd_make_strvec (vty
->buf
);
796 /* In case of 'help \t'. */
797 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
798 vector_set (vline
, '\0');
800 matched
= cmd_complete_command (vline
, vty
, &ret
);
802 cmd_free_strvec (vline
);
804 vty_out (vty
, "%s", VTY_NEWLINE
);
807 case CMD_ERR_AMBIGUOUS
:
808 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
810 vty_redraw_line (vty
);
812 case CMD_ERR_NO_MATCH
:
813 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
815 vty_redraw_line (vty
);
817 case CMD_COMPLETE_FULL_MATCH
:
819 vty_redraw_line (vty
);
820 vty_backward_pure_word (vty
);
821 vty_insert_word_overwrite (vty
, matched
[0]);
822 vty_self_insert (vty
, ' ');
823 XFREE (MTYPE_TMP
, matched
[0]);
825 case CMD_COMPLETE_MATCH
:
827 vty_redraw_line (vty
);
828 vty_backward_pure_word (vty
);
829 vty_insert_word_overwrite (vty
, matched
[0]);
830 XFREE (MTYPE_TMP
, matched
[0]);
831 vector_only_index_free (matched
);
834 case CMD_COMPLETE_LIST_MATCH
:
835 for (i
= 0; matched
[i
] != NULL
; i
++)
837 if (i
!= 0 && ((i
% 6) == 0))
838 vty_out (vty
, "%s", VTY_NEWLINE
);
839 vty_out (vty
, "%-10s ", matched
[i
]);
840 XFREE (MTYPE_TMP
, matched
[i
]);
842 vty_out (vty
, "%s", VTY_NEWLINE
);
845 vty_redraw_line (vty
);
847 case CMD_ERR_NOTHING_TODO
:
849 vty_redraw_line (vty
);
855 vector_only_index_free (matched
);
859 vty_describe_fold (struct vty
*vty
, int cmd_width
,
860 unsigned int desc_width
, struct desc
*desc
)
866 cmd
= desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
;
870 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, desc
->str
, VTY_NEWLINE
);
874 buf
= XCALLOC (MTYPE_TMP
, strlen (desc
->str
) + 1);
876 for (p
= desc
->str
; strlen (p
) > desc_width
; p
+= pos
+ 1)
878 for (pos
= desc_width
; pos
> 0; pos
--)
879 if (*(p
+ pos
) == ' ')
885 strncpy (buf
, p
, pos
);
887 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
892 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
894 XFREE (MTYPE_TMP
, buf
);
897 /* Describe matched command function. */
899 vty_describe_command (struct vty
*vty
)
904 unsigned int i
, width
, desc_width
;
905 struct desc
*desc
, *desc_cr
= NULL
;
907 vline
= cmd_make_strvec (vty
->buf
);
909 /* In case of '> ?'. */
912 vline
= vector_init (1);
913 vector_set (vline
, '\0');
916 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
917 vector_set (vline
, '\0');
919 describe
= cmd_describe_command (vline
, vty
, &ret
);
921 vty_out (vty
, "%s", VTY_NEWLINE
);
923 /* Ambiguous error. */
926 case CMD_ERR_AMBIGUOUS
:
927 cmd_free_strvec (vline
);
928 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
930 vty_redraw_line (vty
);
933 case CMD_ERR_NO_MATCH
:
934 cmd_free_strvec (vline
);
935 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
937 vty_redraw_line (vty
);
942 /* Get width of command string. */
944 for (i
= 0; i
< vector_max (describe
); i
++)
945 if ((desc
= vector_slot (describe
, i
)) != NULL
)
949 if (desc
->cmd
[0] == '\0')
952 len
= strlen (desc
->cmd
);
953 if (desc
->cmd
[0] == '.')
960 /* Get width of description string. */
961 desc_width
= vty
->width
- (width
+ 6);
963 /* Print out description. */
964 for (i
= 0; i
< vector_max (describe
); i
++)
965 if ((desc
= vector_slot (describe
, i
)) != NULL
)
967 if (desc
->cmd
[0] == '\0')
970 if (strcmp (desc
->cmd
, "<cr>") == 0)
977 vty_out (vty
, " %-s%s",
978 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
980 else if (desc_width
>= strlen (desc
->str
))
981 vty_out (vty
, " %-*s %s%s", width
,
982 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
983 desc
->str
, VTY_NEWLINE
);
985 vty_describe_fold (vty
, width
, desc_width
, desc
);
988 vty_out (vty
, " %-*s %s%s", width
989 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
990 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
994 if ((desc
= desc_cr
))
997 vty_out (vty
, " %-s%s",
998 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1000 else if (desc_width
>= strlen (desc
->str
))
1001 vty_out (vty
, " %-*s %s%s", width
,
1002 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1003 desc
->str
, VTY_NEWLINE
);
1005 vty_describe_fold (vty
, width
, desc_width
, desc
);
1008 cmd_free_strvec (vline
);
1009 vector_free (describe
);
1012 vty_redraw_line (vty
);
1016 vty_clear_buf (struct vty
*vty
)
1018 memset (vty
->buf
, 0, vty
->max
);
1021 /* ^C stop current input and do not add command line to the history. */
1023 vty_stop_input (struct vty
*vty
)
1025 vty
->cp
= vty
->length
= 0;
1026 vty_clear_buf (vty
);
1027 vty_out (vty
, "%s", VTY_NEWLINE
);
1033 /* Nothing to do. */
1036 case INTERFACE_NODE
:
1046 case KEYCHAIN_KEY_NODE
:
1049 vty_config_unlock (vty
);
1050 vty
->node
= ENABLE_NODE
;
1053 /* Unknown node, we have to ignore it. */
1058 /* Set history pointer to the latest one. */
1059 vty
->hp
= vty
->hindex
;
1062 /* Add current command line to the history buffer. */
1064 vty_hist_add (struct vty
*vty
)
1068 if (vty
->length
== 0)
1071 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1073 /* Ignore the same string as previous one. */
1074 if (vty
->hist
[index
])
1075 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1077 vty
->hp
= vty
->hindex
;
1081 /* Insert history entry. */
1082 if (vty
->hist
[vty
->hindex
])
1083 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1084 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1086 /* History index rotation. */
1088 if (vty
->hindex
== VTY_MAXHIST
)
1091 vty
->hp
= vty
->hindex
;
1094 /* #define TELNET_OPTION_DEBUG */
1096 /* Get telnet window size. */
1098 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1100 #ifdef TELNET_OPTION_DEBUG
1103 for (i
= 0; i
< nbytes
; i
++)
1108 vty_out (vty
, "IAC ");
1111 vty_out (vty
, "WILL ");
1114 vty_out (vty
, "WONT ");
1117 vty_out (vty
, "DO ");
1120 vty_out (vty
, "DONT ");
1123 vty_out (vty
, "SB ");
1126 vty_out (vty
, "SE ");
1129 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1132 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1135 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1138 vty_out (vty
, "%x ", buf
[i
]);
1142 vty_out (vty
, "%s", VTY_NEWLINE
);
1144 #endif /* TELNET_OPTION_DEBUG */
1149 buffer_reset(vty
->sb_buffer
);
1150 vty
->iac_sb_in_progress
= 1;
1158 if (!vty
->iac_sb_in_progress
)
1161 buffer
= (char *)vty
->sb_buffer
->head
->data
;
1162 length
= vty
->sb_buffer
->length
;
1167 if (buffer
[0] == '\0')
1169 vty
->iac_sb_in_progress
= 0;
1177 vty
->width
= buffer
[2];
1178 vty
->height
= vty
->lines
>= 0 ? vty
->lines
: buffer
[4];
1181 vty
->iac_sb_in_progress
= 0;
1191 /* Execute current command line. */
1193 vty_execute (struct vty
*vty
)
1202 case AUTH_ENABLE_NODE
:
1203 vty_auth (vty
, vty
->buf
);
1206 ret
= vty_command (vty
, vty
->buf
);
1207 if (vty
->type
== VTY_TERM
)
1212 /* Clear command line buffer. */
1213 vty
->cp
= vty
->length
= 0;
1214 vty_clear_buf (vty
);
1216 if (vty
->status
!= VTY_CLOSE
)
1222 #define CONTROL(X) ((X) - '@')
1223 #define VTY_NORMAL 0
1224 #define VTY_PRE_ESCAPE 1
1225 #define VTY_ESCAPE 2
1227 /* Escape character command map. */
1229 vty_escape_map (unsigned char c
, struct vty
*vty
)
1234 vty_previous_line (vty
);
1237 vty_next_line (vty
);
1240 vty_forward_char (vty
);
1243 vty_backward_char (vty
);
1249 /* Go back to normal mode. */
1250 vty
->escape
= VTY_NORMAL
;
1253 /* Quit print out to the buffer. */
1255 vty_buffer_reset (struct vty
*vty
)
1257 buffer_reset (vty
->obuf
);
1259 vty_redraw_line (vty
);
1262 /* Read data via vty socket. */
1264 vty_read (struct thread
*thread
)
1268 unsigned char buf
[VTY_READ_BUFSIZ
];
1270 int vty_sock
= THREAD_FD (thread
);
1271 struct vty
*vty
= THREAD_ARG (thread
);
1274 /* Read raw data from socket */
1275 nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
);
1277 vty
->status
= VTY_CLOSE
;
1279 for (i
= 0; i
< nbytes
; i
++)
1294 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1296 buffer_putc(vty
->sb_buffer
, buf
[i
]);
1302 /* In case of telnet command */
1304 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1311 if (vty
->status
== VTY_MORE
)
1318 vty_buffer_reset (vty
);
1320 #if 0 /* More line does not work for "show ip bgp". */
1323 vty
->status
= VTY_MORELINE
;
1332 /* Escape character. */
1333 if (vty
->escape
== VTY_ESCAPE
)
1335 vty_escape_map (buf
[i
], vty
);
1339 /* Pre-escape status. */
1340 if (vty
->escape
== VTY_PRE_ESCAPE
)
1345 vty
->escape
= VTY_ESCAPE
;
1348 vty_backward_word (vty
);
1349 vty
->escape
= VTY_NORMAL
;
1352 vty_forward_word (vty
);
1353 vty
->escape
= VTY_NORMAL
;
1356 vty_forward_kill_word (vty
);
1357 vty
->escape
= VTY_NORMAL
;
1361 vty_backward_kill_word (vty
);
1362 vty
->escape
= VTY_NORMAL
;
1365 vty
->escape
= VTY_NORMAL
;
1374 vty_beginning_of_line (vty
);
1377 vty_backward_char (vty
);
1380 vty_stop_input (vty
);
1383 vty_delete_char (vty
);
1386 vty_end_of_line (vty
);
1389 vty_forward_char (vty
);
1393 vty_delete_backward_char (vty
);
1396 vty_kill_line (vty
);
1399 vty_next_line (vty
);
1402 vty_previous_line (vty
);
1405 vty_transpose_chars (vty
);
1408 vty_kill_line_from_beginning (vty
);
1411 vty_backward_kill_word (vty
);
1414 vty_end_config (vty
);
1418 vty_out (vty
, "%s", VTY_NEWLINE
);
1422 vty_complete_command (vty
);
1425 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1426 vty_self_insert (vty
, buf
[i
]);
1428 vty_describe_command (vty
);
1431 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1433 vty
->escape
= VTY_ESCAPE
;
1437 vty
->escape
= VTY_PRE_ESCAPE
;
1440 if (buf
[i
] > 31 && buf
[i
] < 127)
1441 vty_self_insert (vty
, buf
[i
]);
1447 if (vty
->status
== VTY_CLOSE
)
1451 vty_event (VTY_WRITE
, vty_sock
, vty
);
1452 vty_event (VTY_READ
, vty_sock
, vty
);
1457 /* Flush buffer to the vty. */
1459 vty_flush (struct thread
*thread
)
1462 int vty_sock
= THREAD_FD (thread
);
1463 struct vty
*vty
= THREAD_ARG (thread
);
1464 vty
->t_write
= NULL
;
1466 /* Tempolary disable read thread. */
1467 if (vty
->lines
== 0)
1470 thread_cancel (vty
->t_read
);
1474 /* Function execution continue. */
1475 if (vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
)
1480 if (vty
->lines
== 0)
1481 buffer_flush_window (vty
->obuf
, vty
->fd
, vty
->width
, 25, 0, 1);
1482 else if (vty
->status
== VTY_MORELINE
)
1483 buffer_flush_window (vty
->obuf
, vty
->fd
, vty
->width
, 1, erase
, 0);
1485 buffer_flush_window (vty
->obuf
, vty
->fd
, vty
->width
,
1486 vty
->lines
>= 0 ? vty
->lines
: vty
->height
,
1489 if (buffer_empty (vty
->obuf
))
1491 if (vty
->status
== VTY_CLOSE
)
1495 vty
->status
= VTY_NORMAL
;
1497 if (vty
->lines
== 0)
1498 vty_event (VTY_READ
, vty_sock
, vty
);
1503 vty
->status
= VTY_MORE
;
1505 if (vty
->lines
== 0)
1506 vty_event (VTY_WRITE
, vty_sock
, vty
);
1512 /* Create new vty structure. */
1514 vty_create (int vty_sock
, union sockunion
*su
)
1518 /* Allocate new vty structure and set up default values. */
1521 vty
->type
= VTY_TERM
;
1522 vty
->address
= sockunion_su2str (su
);
1523 if (no_password_check
)
1526 vty
->node
= ENABLE_NODE
;
1528 vty
->node
= VIEW_NODE
;
1531 vty
->node
= AUTH_NODE
;
1534 vty_clear_buf (vty
);
1536 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1539 vector_set_index (vtyvec
, vty_sock
, vty
);
1540 vty
->status
= VTY_NORMAL
;
1541 vty
->v_timeout
= vty_timeout_val
;
1542 if (host
.lines
>= 0)
1543 vty
->lines
= host
.lines
;
1547 vty
->iac_sb_in_progress
= 0;
1548 vty
->sb_buffer
= buffer_new (1024);
1550 if (! no_password_check
)
1552 /* Vty is not available if password isn't set. */
1553 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1555 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1556 vty
->status
= VTY_CLOSE
;
1562 /* Say hello to the world. */
1564 if (! no_password_check
)
1565 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1567 /* Setting up terminal. */
1568 vty_will_echo (vty
);
1569 vty_will_suppress_go_ahead (vty
);
1571 vty_dont_linemode (vty
);
1572 vty_do_window_size (vty
);
1573 /* vty_dont_lflow_ahead (vty); */
1577 /* Add read/write thread. */
1578 vty_event (VTY_WRITE
, vty_sock
, vty
);
1579 vty_event (VTY_READ
, vty_sock
, vty
);
1584 /* Accept connection from the network. */
1586 vty_accept (struct thread
*thread
)
1594 struct prefix
*p
= NULL
;
1595 struct access_list
*acl
= NULL
;
1597 accept_sock
= THREAD_FD (thread
);
1599 /* We continue hearing vty socket. */
1600 vty_event (VTY_SERV
, accept_sock
, NULL
);
1602 memset (&su
, 0, sizeof (union sockunion
));
1604 /* We can handle IPv4 or IPv6 socket. */
1605 vty_sock
= sockunion_accept (accept_sock
, &su
);
1608 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1612 p
= sockunion2hostprefix (&su
);
1614 /* VTY's accesslist apply. */
1615 if (p
->family
== AF_INET
&& vty_accesslist_name
)
1617 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1618 (access_list_apply (acl
, p
) == FILTER_DENY
))
1621 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1622 (buf
= sockunion_su2str (&su
)));
1626 /* continue accepting connections */
1627 vty_event (VTY_SERV
, accept_sock
, NULL
);
1636 /* VTY's ipv6 accesslist apply. */
1637 if (p
->family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1639 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1640 (access_list_apply (acl
, p
) == FILTER_DENY
))
1643 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1644 (buf
= sockunion_su2str (&su
)));
1648 /* continue accepting connections */
1649 vty_event (VTY_SERV
, accept_sock
, NULL
);
1656 #endif /* HAVE_IPV6 */
1661 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1662 (char *) &on
, sizeof (on
));
1664 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1665 safe_strerror (errno
));
1667 vty
= vty_create (vty_sock
, &su
);
1672 #if defined(HAVE_IPV6) && !defined(NRL)
1674 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1677 struct addrinfo req
;
1678 struct addrinfo
*ainfo
;
1679 struct addrinfo
*ainfo_save
;
1681 char port_str
[BUFSIZ
];
1683 memset (&req
, 0, sizeof (struct addrinfo
));
1684 req
.ai_flags
= AI_PASSIVE
;
1685 req
.ai_family
= AF_UNSPEC
;
1686 req
.ai_socktype
= SOCK_STREAM
;
1687 sprintf (port_str
, "%d", port
);
1688 port_str
[sizeof (port_str
) - 1] = '\0';
1690 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1694 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1702 if (ainfo
->ai_family
!= AF_INET
1704 && ainfo
->ai_family
!= AF_INET6
1705 #endif /* HAVE_IPV6 */
1709 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1713 sockopt_reuseaddr (sock
);
1714 sockopt_reuseport (sock
);
1716 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1719 close (sock
); /* Avoid sd leak. */
1723 ret
= listen (sock
, 3);
1726 close (sock
); /* Avoid sd leak. */
1730 vty_event (VTY_SERV
, sock
, NULL
);
1732 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1734 freeaddrinfo (ainfo_save
);
1736 #endif /* HAVE_IPV6 && ! NRL */
1738 /* Make vty server socket. */
1740 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1747 memset (&su
, 0, sizeof (union sockunion
));
1748 su
.sa
.sa_family
= family
;
1753 naddr
=&su
.sin
.sin_addr
;
1756 naddr
=&su
.sin6
.sin6_addr
;
1761 switch(inet_pton(family
,addr
,naddr
))
1764 zlog_err("bad address %s",addr
);
1768 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
1772 /* Make new socket. */
1773 accept_sock
= sockunion_stream_socket (&su
);
1774 if (accept_sock
< 0)
1777 /* This is server, so reuse address. */
1778 sockopt_reuseaddr (accept_sock
);
1779 sockopt_reuseport (accept_sock
);
1781 /* Bind socket to universal address and given port. */
1782 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
1785 zlog_warn("can't bind socket");
1786 close (accept_sock
); /* Avoid sd leak. */
1790 /* Listen socket under queue 3. */
1791 ret
= listen (accept_sock
, 3);
1794 zlog (NULL
, LOG_WARNING
, "can't listen socket");
1795 close (accept_sock
); /* Avoid sd leak. */
1799 /* Add vty server event. */
1800 vty_event (VTY_SERV
, accept_sock
, NULL
);
1804 /* For sockaddr_un. */
1807 /* VTY shell UNIX domain socket. */
1809 vty_serv_un (const char *path
)
1813 struct sockaddr_un serv
;
1815 struct zprivs_ids_t ids
;
1817 /* First of all, unlink existing socket */
1821 old_mask
= umask (0007);
1823 /* Make UNIX domain socket. */
1824 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1831 /* Make server socket. */
1832 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1833 serv
.sun_family
= AF_UNIX
;
1834 strncpy (serv
.sun_path
, path
, strlen (path
));
1836 len
= serv
.sun_len
= SUN_LEN(&serv
);
1838 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1839 #endif /* HAVE_SUN_LEN */
1841 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1845 close (sock
); /* Avoid sd leak. */
1849 ret
= listen (sock
, 5);
1853 close (sock
); /* Avoid sd leak. */
1859 zprivs_get_ids(&ids
);
1861 if (ids
.gid_vty
> 0)
1863 /* set group of socket */
1864 if ( chown (path
, -1, ids
.gid_vty
) )
1866 zlog_err ("vty_serv_un: could chown socket, %s",
1867 safe_strerror (errno
) );
1871 vty_event (VTYSH_SERV
, sock
, NULL
);
1874 /* #define VTYSH_DEBUG 1 */
1877 vtysh_accept (struct thread
*thread
)
1883 struct sockaddr_un client
;
1886 accept_sock
= THREAD_FD (thread
);
1888 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
1890 memset (&client
, 0, sizeof (struct sockaddr_un
));
1891 client_len
= sizeof (struct sockaddr_un
);
1893 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
1894 (socklen_t
*) &client_len
);
1898 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1902 /* set to non-blocking*/
1903 if ( ((flags
= fcntl (sock
, F_GETFL
)) == -1)
1904 || (fcntl (sock
, F_SETFL
, flags
|O_NONBLOCK
) == -1) )
1906 zlog_warn ("vtysh_accept: could not set vty socket to non-blocking,"
1907 " %s, closing", safe_strerror (errno
));
1913 printf ("VTY shell accept\n");
1914 #endif /* VTYSH_DEBUG */
1918 vty
->type
= VTY_SHELL_SERV
;
1919 vty
->node
= VIEW_NODE
;
1921 vty_event (VTYSH_READ
, sock
, vty
);
1927 vtysh_read (struct thread
*thread
)
1933 unsigned char buf
[VTY_READ_BUFSIZ
];
1934 u_char header
[4] = {0, 0, 0, 0};
1936 sock
= THREAD_FD (thread
);
1937 vty
= THREAD_ARG (thread
);
1940 nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
);
1945 printf ("close vtysh\n");
1946 #endif /* VTYSH_DEBUG */
1951 printf ("line: %s\n", buf
);
1952 #endif /* VTYSH_DEBUG */
1954 vty_ensure (vty
, nbytes
);
1955 memcpy (vty
->buf
, buf
, nbytes
);
1957 /* Pass this line to parser. */
1958 ret
= vty_execute (vty
);
1960 vty_clear_buf (vty
);
1962 /* Return result. */
1964 printf ("result: %d\n", ret
);
1965 printf ("vtysh node: %d\n", vty
->node
);
1966 #endif /* VTYSH_DEBUG */
1969 buffer_write(vty
->obuf
, header
, 4);
1971 if (!vty
->t_write
&& buffer_flush_available(vty
->obuf
, vty
->fd
))
1972 vty_event (VTYSH_WRITE
, vty
->fd
, vty
);
1973 vty_event (VTYSH_READ
, sock
, vty
);
1979 vtysh_write (struct thread
*thread
)
1981 struct vty
*vty
= THREAD_ARG (thread
);
1983 vty
->t_write
= NULL
;
1984 if (buffer_flush_available(vty
->obuf
, vty
->fd
))
1985 vty_event (VTYSH_WRITE
, vty
->fd
, vty
);
1991 /* Determine address family to bind. */
1993 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
1995 /* If port is set to 0, do not listen on TCP/IP at all! */
2001 vty_serv_sock_family (addr
, port
, AF_INET
);
2002 vty_serv_sock_family (addr
, port
, AF_INET6
);
2004 vty_serv_sock_addrinfo (addr
, port
);
2006 #else /* ! HAVE_IPV6 */
2007 vty_serv_sock_family (addr
,port
, AF_INET
);
2008 #endif /* HAVE_IPV6 */
2016 /* Close vty interface. */
2018 vty_close (struct vty
*vty
)
2022 /* Cancel threads.*/
2024 thread_cancel (vty
->t_read
);
2026 thread_cancel (vty
->t_write
);
2028 thread_cancel (vty
->t_timeout
);
2031 if (! buffer_empty (vty
->obuf
))
2032 buffer_flush_all (vty
->obuf
, vty
->fd
);
2034 /* Free input buffer. */
2035 buffer_free (vty
->obuf
);
2037 /* Free SB buffer. */
2039 buffer_free (vty
->sb_buffer
);
2041 /* Free command history. */
2042 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2044 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2047 vector_unset (vtyvec
, vty
->fd
);
2054 XFREE (0, vty
->address
);
2056 XFREE (MTYPE_VTY
, vty
->buf
);
2058 /* Check configure. */
2059 vty_config_unlock (vty
);
2062 XFREE (MTYPE_VTY
, vty
);
2065 /* When time out occur output message then close connection. */
2067 vty_timeout (struct thread
*thread
)
2071 vty
= THREAD_ARG (thread
);
2072 vty
->t_timeout
= NULL
;
2076 buffer_reset (vty
->obuf
);
2077 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2079 /* Close connection. */
2080 vty
->status
= VTY_CLOSE
;
2086 /* Read up configuration file from file_name. */
2088 vty_read_file (FILE *confp
)
2094 vty
->fd
= 0; /* stdout */
2095 vty
->type
= VTY_TERM
;
2096 vty
->node
= CONFIG_NODE
;
2098 /* Execute configuration file */
2099 ret
= config_from_file (vty
, confp
);
2101 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2105 case CMD_ERR_AMBIGUOUS
:
2106 fprintf (stderr
, "Ambiguous command.\n");
2108 case CMD_ERR_NO_MATCH
:
2109 fprintf (stderr
, "There is no such command.\n");
2112 fprintf (stderr
, "Error occured during reading below line.\n%s\n",
2122 vty_use_backup_config (char *fullpath
)
2124 char *fullpath_sav
, *fullpath_tmp
;
2131 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2132 strcpy (fullpath_sav
, fullpath
);
2133 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2134 if (stat (fullpath_sav
, &buf
) == -1)
2136 free (fullpath_sav
);
2140 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2141 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2143 /* Open file to configuration write. */
2144 tmp
= mkstemp (fullpath_tmp
);
2147 free (fullpath_sav
);
2148 free (fullpath_tmp
);
2152 sav
= open (fullpath_sav
, O_RDONLY
);
2155 unlink (fullpath_tmp
);
2156 free (fullpath_sav
);
2157 free (fullpath_tmp
);
2161 while((c
= read (sav
, buffer
, 512)) > 0)
2162 write (tmp
, buffer
, c
);
2167 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2169 unlink (fullpath_tmp
);
2170 free (fullpath_sav
);
2171 free (fullpath_tmp
);
2175 if (link (fullpath_tmp
, fullpath
) == 0)
2176 ret
= fopen (fullpath
, "r");
2178 unlink (fullpath_tmp
);
2180 free (fullpath_sav
);
2181 free (fullpath_tmp
);
2182 return fopen (fullpath
, "r");
2185 /* Read up configuration file from file_name. */
2187 vty_read_config (char *config_file
,
2188 char *config_default_dir
)
2190 char cwd
[MAXPATHLEN
];
2194 /* If -f flag specified. */
2195 if (config_file
!= NULL
)
2197 if (! IS_DIRECTORY_SEP (config_file
[0]))
2199 getcwd (cwd
, MAXPATHLEN
);
2200 fullpath
= XMALLOC (MTYPE_TMP
,
2201 strlen (cwd
) + strlen (config_file
) + 2);
2202 sprintf (fullpath
, "%s/%s", cwd
, config_file
);
2205 fullpath
= config_file
;
2207 confp
= fopen (fullpath
, "r");
2211 confp
= vty_use_backup_config (fullpath
);
2213 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2216 fprintf (stderr
, "can't open configuration file [%s]\n",
2226 struct stat conf_stat
;
2228 /* !!!!PLEASE LEAVE!!!!
2229 * This is NEEDED for use with vtysh -b, or else you can get
2230 * a real configuration food fight with a lot garbage in the
2231 * merged configuration file it creates coming from the per
2232 * daemon configuration files. This also allows the daemons
2233 * to start if there default configuration file is not
2234 * present or ignore them, as needed when using vtysh -b to
2235 * configure the daemons at boot - MAG
2238 /* Stat for vtysh Zebra.conf, if found startup and wait for
2239 * boot configuration
2242 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2244 ret
= stat (integrate_default
, &conf_stat
);
2250 confp
= fopen (config_default_dir
, "r");
2253 confp
= vty_use_backup_config (config_default_dir
);
2256 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2257 fullpath
= config_default_dir
;
2261 fprintf (stderr
, "can't open configuration file [%s]\n",
2262 config_default_dir
);
2267 fullpath
= config_default_dir
;
2270 vty_read_file (confp
);
2274 host_config_set (fullpath
);
2277 /* Small utility function which output log to the VTY. */
2279 vty_log (const char *level
, const char *proto_str
,
2280 const char *format
, va_list va
)
2285 for (i
= 0; i
< vector_max (vtyvec
); i
++)
2286 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2291 vty_log_out (vty
, level
, proto_str
, format
, ac
);
2296 /* Async-signal-safe version of vty_log for fixed strings. */
2298 vty_log_fixed (const char *buf
, size_t len
)
2302 for (i
= 0; i
< vector_max (vtyvec
); i
++)
2305 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2308 write(vty
->fd
, buf
, len
);
2309 write(vty
->fd
, "\r\n", 2);
2315 vty_config_lock (struct vty
*vty
)
2317 if (vty_config
== 0)
2326 vty_config_unlock (struct vty
*vty
)
2328 if (vty_config
== 1 && vty
->config
== 1)
2336 /* Master of the threads. */
2337 static struct thread_master
*master
;
2340 vty_event (enum event event
, int sock
, struct vty
*vty
)
2342 struct thread
*vty_serv_thread
;
2347 vty_serv_thread
= thread_add_read (master
, vty_accept
, vty
, sock
);
2348 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2352 thread_add_read (master
, vtysh_accept
, vty
, sock
);
2355 vty
->t_read
= thread_add_read (master
, vtysh_read
, vty
, sock
);
2358 vty
->t_write
= thread_add_write (master
, vtysh_write
, vty
, sock
);
2362 vty
->t_read
= thread_add_read (master
, vty_read
, vty
, sock
);
2364 /* Time out treatment. */
2368 thread_cancel (vty
->t_timeout
);
2370 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2375 vty
->t_write
= thread_add_write (master
, vty_flush
, vty
, sock
);
2377 case VTY_TIMEOUT_RESET
:
2380 thread_cancel (vty
->t_timeout
);
2381 vty
->t_timeout
= NULL
;
2386 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2395 "Display who is on vty\n")
2400 for (i
= 0; i
< vector_max (vtyvec
); i
++)
2401 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2402 vty_out (vty
, "%svty[%d] connected from %s.%s",
2403 v
->config
? "*" : " ",
2404 i
, v
->address
, VTY_NEWLINE
);
2408 /* Move to vty configuration mode. */
2412 "Configure a terminal line\n"
2413 "Virtual terminal\n")
2415 vty
->node
= VTY_NODE
;
2419 /* Set time out value. */
2421 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2423 unsigned long timeout
= 0;
2425 /* min_str and sec_str are already checked by parser. So it must be
2426 all digit string. */
2429 timeout
= strtol (min_str
, NULL
, 10);
2433 timeout
+= strtol (sec_str
, NULL
, 10);
2435 vty_timeout_val
= timeout
;
2436 vty
->v_timeout
= timeout
;
2437 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2443 DEFUN (exec_timeout_min
,
2444 exec_timeout_min_cmd
,
2445 "exec-timeout <0-35791>",
2446 "Set timeout value\n"
2447 "Timeout value in minutes\n")
2449 return exec_timeout (vty
, argv
[0], NULL
);
2452 DEFUN (exec_timeout_sec
,
2453 exec_timeout_sec_cmd
,
2454 "exec-timeout <0-35791> <0-2147483>",
2455 "Set the EXEC timeout\n"
2456 "Timeout in minutes\n"
2457 "Timeout in seconds\n")
2459 return exec_timeout (vty
, argv
[0], argv
[1]);
2462 DEFUN (no_exec_timeout
,
2463 no_exec_timeout_cmd
,
2466 "Set the EXEC timeout\n")
2468 return exec_timeout (vty
, NULL
, NULL
);
2471 /* Set vty access class. */
2472 DEFUN (vty_access_class
,
2473 vty_access_class_cmd
,
2474 "access-class WORD",
2475 "Filter connections based on an IP access list\n"
2478 if (vty_accesslist_name
)
2479 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2481 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2486 /* Clear vty access class. */
2487 DEFUN (no_vty_access_class
,
2488 no_vty_access_class_cmd
,
2489 "no access-class [WORD]",
2491 "Filter connections based on an IP access list\n"
2494 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2496 vty_out (vty
, "Access-class is not currently applied to vty%s",
2501 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2503 vty_accesslist_name
= NULL
;
2509 /* Set vty access class. */
2510 DEFUN (vty_ipv6_access_class
,
2511 vty_ipv6_access_class_cmd
,
2512 "ipv6 access-class WORD",
2514 "Filter connections based on an IP access list\n"
2515 "IPv6 access list\n")
2517 if (vty_ipv6_accesslist_name
)
2518 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2520 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2525 /* Clear vty access class. */
2526 DEFUN (no_vty_ipv6_access_class
,
2527 no_vty_ipv6_access_class_cmd
,
2528 "no ipv6 access-class [WORD]",
2531 "Filter connections based on an IP access list\n"
2532 "IPv6 access list\n")
2534 if (! vty_ipv6_accesslist_name
||
2535 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2537 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2542 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2544 vty_ipv6_accesslist_name
= NULL
;
2548 #endif /* HAVE_IPV6 */
2554 "Enable password checking\n")
2556 no_password_check
= 0;
2560 DEFUN (no_vty_login
,
2564 "Enable password checking\n")
2566 no_password_check
= 1;
2570 DEFUN (service_advanced_vty
,
2571 service_advanced_vty_cmd
,
2572 "service advanced-vty",
2573 "Set up miscellaneous service\n"
2574 "Enable advanced mode vty interface\n")
2580 DEFUN (no_service_advanced_vty
,
2581 no_service_advanced_vty_cmd
,
2582 "no service advanced-vty",
2584 "Set up miscellaneous service\n"
2585 "Enable advanced mode vty interface\n")
2591 DEFUN (terminal_monitor
,
2592 terminal_monitor_cmd
,
2594 "Set terminal line parameters\n"
2595 "Copy debug output to the current terminal line\n")
2601 DEFUN (terminal_no_monitor
,
2602 terminal_no_monitor_cmd
,
2603 "terminal no monitor",
2604 "Set terminal line parameters\n"
2606 "Copy debug output to the current terminal line\n")
2612 DEFUN (show_history
,
2616 "Display the session command history\n")
2620 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2622 if (index
== VTY_MAXHIST
)
2628 if (vty
->hist
[index
] != NULL
)
2629 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2637 /* Display current configuration. */
2639 vty_config_write (struct vty
*vty
)
2641 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2643 if (vty_accesslist_name
)
2644 vty_out (vty
, " access-class %s%s",
2645 vty_accesslist_name
, VTY_NEWLINE
);
2647 if (vty_ipv6_accesslist_name
)
2648 vty_out (vty
, " ipv6 access-class %s%s",
2649 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
2652 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2653 vty_out (vty
, " exec-timeout %ld %ld%s",
2654 vty_timeout_val
/ 60,
2655 vty_timeout_val
% 60, VTY_NEWLINE
);
2658 if (no_password_check
)
2659 vty_out (vty
, " no login%s", VTY_NEWLINE
);
2661 vty_out (vty
, "!%s", VTY_NEWLINE
);
2666 struct cmd_node vty_node
=
2669 "%s(config-line)# ",
2673 /* Reset all VTY status. */
2679 struct thread
*vty_serv_thread
;
2681 for (i
= 0; i
< vector_max (vtyvec
); i
++)
2682 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2684 buffer_reset (vty
->obuf
);
2685 vty
->status
= VTY_CLOSE
;
2689 for (i
= 0; i
< vector_max (Vvty_serv_thread
); i
++)
2690 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2692 thread_cancel (vty_serv_thread
);
2693 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2697 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2699 if (vty_accesslist_name
)
2701 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2702 vty_accesslist_name
= NULL
;
2705 if (vty_ipv6_accesslist_name
)
2707 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2708 vty_ipv6_accesslist_name
= NULL
;
2712 /* for ospf6d easy temprary reload function */
2713 /* vty_reset + close accept socket */
2719 struct thread
*vty_serv_thread
;
2721 for (i
= 0; i
< vector_max (vtyvec
); i
++)
2722 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2724 buffer_reset (vty
->obuf
);
2725 vty
->status
= VTY_CLOSE
;
2729 for (i
= 0; i
< vector_max (Vvty_serv_thread
); i
++)
2730 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2732 thread_cancel (vty_serv_thread
);
2733 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2737 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2739 if (vty_accesslist_name
)
2741 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2742 vty_accesslist_name
= NULL
;
2745 if (vty_ipv6_accesslist_name
)
2747 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2748 vty_ipv6_accesslist_name
= NULL
;
2755 char cwd
[MAXPATHLEN
];
2758 c
= getcwd (cwd
, MAXPATHLEN
);
2763 getcwd (cwd
, MAXPATHLEN
);
2766 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
2767 strcpy (vty_cwd
, cwd
);
2777 vty_shell (struct vty
*vty
)
2779 return vty
->type
== VTY_SHELL
? 1 : 0;
2783 vty_shell_serv (struct vty
*vty
)
2785 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2791 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2794 /* Install vty's own commands like `who' command. */
2796 vty_init (struct thread_master
*master_thread
)
2798 /* For further configuration read, preserve current directory. */
2801 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2803 master
= master_thread
;
2805 /* Initilize server thread vector. */
2806 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
2808 /* Install bgp top node. */
2809 install_node (&vty_node
, vty_config_write
);
2811 install_element (VIEW_NODE
, &config_who_cmd
);
2812 install_element (VIEW_NODE
, &show_history_cmd
);
2813 install_element (ENABLE_NODE
, &config_who_cmd
);
2814 install_element (CONFIG_NODE
, &line_vty_cmd
);
2815 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
2816 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
2817 install_element (CONFIG_NODE
, &show_history_cmd
);
2818 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
2819 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
2820 install_element (ENABLE_NODE
, &show_history_cmd
);
2822 install_default (VTY_NODE
);
2823 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
2824 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
2825 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
2826 install_element (VTY_NODE
, &vty_access_class_cmd
);
2827 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
2828 install_element (VTY_NODE
, &vty_login_cmd
);
2829 install_element (VTY_NODE
, &no_vty_login_cmd
);
2831 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
2832 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
2833 #endif /* HAVE_IPV6 */