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
, struct timestamp_control
*ctl
, va_list va
)
158 if (!ctl
->already_rendered
)
160 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
161 ctl
->already_rendered
= 1;
163 if (ctl
->len
+1 >= sizeof(buf
))
165 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
170 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
172 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
173 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
176 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
177 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
183 if (write(vty
->fd
, buf
, len
) < 0)
185 if (ERRNO_IO_RETRY(errno
))
186 /* Kernel buffer is full, probably too much debugging output, so just
187 drop the data and ignore. */
189 /* Fatal I/O error. */
190 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
191 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
192 __func__
, vty
->fd
, safe_strerror(errno
));
193 buffer_reset(vty
->obuf
);
194 /* cannot call vty_close, because a parent routine may still try
195 to access the vty struct */
196 vty
->status
= VTY_CLOSE
;
197 shutdown(vty
->fd
, SHUT_RDWR
);
203 /* Output current time to the vty. */
205 vty_time_print (struct vty
*vty
, int cr
)
209 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
211 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
215 vty_out (vty
, "%s\n", buf
);
217 vty_out (vty
, "%s ", buf
);
222 /* Say hello to vty interface. */
224 vty_hello (struct vty
*vty
)
231 f
= fopen (host
.motdfile
, "r");
234 while (fgets (buf
, sizeof (buf
), f
))
237 /* work backwards to ignore trailling isspace() */
238 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
241 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
246 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
249 vty_out (vty
, host
.motd
);
252 /* Put out prompt and wait input from user. */
254 vty_prompt (struct vty
*vty
)
256 struct utsname names
;
259 if (vty
->type
== VTY_TERM
)
261 hostname
= host
.name
;
265 hostname
= names
.nodename
;
267 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
271 /* Send WILL TELOPT_ECHO to remote server. */
273 vty_will_echo (struct vty
*vty
)
275 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
276 vty_out (vty
, "%s", cmd
);
279 /* Make suppress Go-Ahead telnet option. */
281 vty_will_suppress_go_ahead (struct vty
*vty
)
283 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
284 vty_out (vty
, "%s", cmd
);
287 /* Make don't use linemode over telnet. */
289 vty_dont_linemode (struct vty
*vty
)
291 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
292 vty_out (vty
, "%s", cmd
);
295 /* Use window size. */
297 vty_do_window_size (struct vty
*vty
)
299 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
300 vty_out (vty
, "%s", cmd
);
303 #if 0 /* Currently not used. */
304 /* Make don't use lflow vty interface. */
306 vty_dont_lflow_ahead (struct vty
*vty
)
308 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
309 vty_out (vty
, "%s", cmd
);
313 /* Allocate new vty struct. */
317 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
319 new->obuf
= buffer_new(0); /* Use default buffer size. */
320 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
321 new->max
= VTY_BUFSIZ
;
326 /* Authentication of vty */
328 vty_auth (struct vty
*vty
, char *buf
)
331 enum node_type next_node
= 0;
333 char *crypt (const char *, const char *);
339 passwd
= host
.password_encrypt
;
341 passwd
= host
.password
;
343 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
345 next_node
= VIEW_NODE
;
347 case AUTH_ENABLE_NODE
:
349 passwd
= host
.enable_encrypt
;
351 passwd
= host
.enable
;
352 next_node
= ENABLE_NODE
;
359 fail
= strcmp (crypt(buf
, passwd
), passwd
);
361 fail
= strcmp (buf
, passwd
);
369 vty
->node
= next_node
; /* Success ! */
376 if (vty
->node
== AUTH_NODE
)
378 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
379 vty
->status
= VTY_CLOSE
;
383 /* AUTH_ENABLE_NODE */
385 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
386 vty
->node
= VIEW_NODE
;
392 /* Command execution over the vty interface. */
394 vty_command (struct vty
*vty
, char *buf
)
398 const char *protocolname
;
400 /* Split readline string up into the vector */
401 vline
= cmd_make_strvec (buf
);
406 #ifdef CONSUMED_TIME_CHECK
410 unsigned long realtime
, cputime
;
413 #endif /* CONSUMED_TIME_CHECK */
415 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
417 /* Get the name of the protocol if any */
419 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
421 protocolname
= zlog_proto_names
[ZLOG_NONE
];
423 #ifdef CONSUMED_TIME_CHECK
425 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
427 /* Warn about CPU hog that must be fixed. */
428 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
429 realtime
/1000, cputime
/1000, buf
);
431 #endif /* CONSUMED_TIME_CHECK */
433 if (ret
!= CMD_SUCCESS
)
437 if (vty
->type
== VTY_FILE
)
438 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
440 case CMD_ERR_AMBIGUOUS
:
441 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
443 case CMD_ERR_NO_MATCH
:
444 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
446 case CMD_ERR_INCOMPLETE
:
447 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
450 cmd_free_strvec (vline
);
455 static const char telnet_backward_char
= 0x08;
456 static const char telnet_space_char
= ' ';
458 /* Basic function to write buffer to vty. */
460 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
462 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
465 /* Should we do buffering here ? And make vty_flush (vty) ? */
466 buffer_put (vty
->obuf
, buf
, nbytes
);
469 /* Ensure length of input buffer. Is buffer is short, double it. */
471 vty_ensure (struct vty
*vty
, int length
)
473 if (vty
->max
<= length
)
476 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
480 /* Basic function to insert character into vty. */
482 vty_self_insert (struct vty
*vty
, char c
)
487 vty_ensure (vty
, vty
->length
+ 1);
488 length
= vty
->length
- vty
->cp
;
489 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
490 vty
->buf
[vty
->cp
] = c
;
492 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
493 for (i
= 0; i
< length
; i
++)
494 vty_write (vty
, &telnet_backward_char
, 1);
500 /* Self insert character 'c' in overwrite mode. */
502 vty_self_insert_overwrite (struct vty
*vty
, char c
)
504 vty_ensure (vty
, vty
->length
+ 1);
505 vty
->buf
[vty
->cp
++] = c
;
507 if (vty
->cp
> vty
->length
)
510 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
513 vty_write (vty
, &c
, 1);
516 /* Insert a word into vty interface with overwrite mode. */
518 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
520 int len
= strlen (str
);
521 vty_write (vty
, str
, len
);
522 strcpy (&vty
->buf
[vty
->cp
], str
);
524 vty
->length
= vty
->cp
;
527 /* Forward character. */
529 vty_forward_char (struct vty
*vty
)
531 if (vty
->cp
< vty
->length
)
533 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
538 /* Backward character. */
540 vty_backward_char (struct vty
*vty
)
545 vty_write (vty
, &telnet_backward_char
, 1);
549 /* Move to the beginning of the line. */
551 vty_beginning_of_line (struct vty
*vty
)
554 vty_backward_char (vty
);
557 /* Move to the end of the line. */
559 vty_end_of_line (struct vty
*vty
)
561 while (vty
->cp
< vty
->length
)
562 vty_forward_char (vty
);
565 static void vty_kill_line_from_beginning (struct vty
*);
566 static void vty_redraw_line (struct vty
*);
568 /* Print command line history. This function is called from
569 vty_next_line and vty_previous_line. */
571 vty_history_print (struct vty
*vty
)
575 vty_kill_line_from_beginning (vty
);
577 /* Get previous line from history buffer */
578 length
= strlen (vty
->hist
[vty
->hp
]);
579 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
580 vty
->cp
= vty
->length
= length
;
582 /* Redraw current line */
583 vty_redraw_line (vty
);
586 /* Show next command line history. */
588 vty_next_line (struct vty
*vty
)
592 if (vty
->hp
== vty
->hindex
)
595 /* Try is there history exist or not. */
597 if (try_index
== (VTY_MAXHIST
- 1))
602 /* If there is not history return. */
603 if (vty
->hist
[try_index
] == NULL
)
608 vty_history_print (vty
);
611 /* Show previous command line history. */
613 vty_previous_line (struct vty
*vty
)
619 try_index
= VTY_MAXHIST
- 1;
623 if (vty
->hist
[try_index
] == NULL
)
628 vty_history_print (vty
);
631 /* This function redraw all of the command line character. */
633 vty_redraw_line (struct vty
*vty
)
635 vty_write (vty
, vty
->buf
, vty
->length
);
636 vty
->cp
= vty
->length
;
641 vty_forward_word (struct vty
*vty
)
643 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
644 vty_forward_char (vty
);
646 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
647 vty_forward_char (vty
);
650 /* Backward word without skipping training space. */
652 vty_backward_pure_word (struct vty
*vty
)
654 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
655 vty_backward_char (vty
);
660 vty_backward_word (struct vty
*vty
)
662 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
663 vty_backward_char (vty
);
665 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
666 vty_backward_char (vty
);
669 /* When '^D' is typed at the beginning of the line we move to the down
672 vty_down_level (struct vty
*vty
)
674 vty_out (vty
, "%s", VTY_NEWLINE
);
675 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
680 /* When '^Z' is received from vty, move down to the enable mode. */
682 vty_end_config (struct vty
*vty
)
684 vty_out (vty
, "%s", VTY_NEWLINE
);
708 case KEYCHAIN_KEY_NODE
:
711 vty_config_unlock (vty
);
712 vty
->node
= ENABLE_NODE
;
715 /* Unknown node, we have to ignore it. */
723 /* Delete a charcter at the current point. */
725 vty_delete_char (struct vty
*vty
)
730 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
733 if (vty
->length
== 0)
735 vty_down_level (vty
);
739 if (vty
->cp
== vty
->length
)
740 return; /* completion need here? */
742 size
= vty
->length
- vty
->cp
;
745 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
746 vty
->buf
[vty
->length
] = '\0';
748 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
749 vty_write (vty
, &telnet_space_char
, 1);
751 for (i
= 0; i
< size
; i
++)
752 vty_write (vty
, &telnet_backward_char
, 1);
755 /* Delete a character before the point. */
757 vty_delete_backward_char (struct vty
*vty
)
762 vty_backward_char (vty
);
763 vty_delete_char (vty
);
766 /* Kill rest of line from current point. */
768 vty_kill_line (struct vty
*vty
)
773 size
= vty
->length
- vty
->cp
;
778 for (i
= 0; i
< size
; i
++)
779 vty_write (vty
, &telnet_space_char
, 1);
780 for (i
= 0; i
< size
; i
++)
781 vty_write (vty
, &telnet_backward_char
, 1);
783 memset (&vty
->buf
[vty
->cp
], 0, size
);
784 vty
->length
= vty
->cp
;
787 /* Kill line from the beginning. */
789 vty_kill_line_from_beginning (struct vty
*vty
)
791 vty_beginning_of_line (vty
);
795 /* Delete a word before the point. */
797 vty_forward_kill_word (struct vty
*vty
)
799 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
800 vty_delete_char (vty
);
801 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
802 vty_delete_char (vty
);
805 /* Delete a word before the point. */
807 vty_backward_kill_word (struct vty
*vty
)
809 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
810 vty_delete_backward_char (vty
);
811 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
812 vty_delete_backward_char (vty
);
815 /* Transpose chars before or at the point. */
817 vty_transpose_chars (struct vty
*vty
)
821 /* If length is short or point is near by the beginning of line then
823 if (vty
->length
< 2 || vty
->cp
< 1)
826 /* In case of point is located at the end of the line. */
827 if (vty
->cp
== vty
->length
)
829 c1
= vty
->buf
[vty
->cp
- 1];
830 c2
= vty
->buf
[vty
->cp
- 2];
832 vty_backward_char (vty
);
833 vty_backward_char (vty
);
834 vty_self_insert_overwrite (vty
, c1
);
835 vty_self_insert_overwrite (vty
, c2
);
839 c1
= vty
->buf
[vty
->cp
];
840 c2
= vty
->buf
[vty
->cp
- 1];
842 vty_backward_char (vty
);
843 vty_self_insert_overwrite (vty
, c1
);
844 vty_self_insert_overwrite (vty
, c2
);
848 /* Do completion at vty interface. */
850 vty_complete_command (struct vty
*vty
)
854 char **matched
= NULL
;
857 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
860 vline
= cmd_make_strvec (vty
->buf
);
864 /* In case of 'help \t'. */
865 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
866 vector_set (vline
, '\0');
868 matched
= cmd_complete_command (vline
, vty
, &ret
);
870 cmd_free_strvec (vline
);
872 vty_out (vty
, "%s", VTY_NEWLINE
);
875 case CMD_ERR_AMBIGUOUS
:
876 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
878 vty_redraw_line (vty
);
880 case CMD_ERR_NO_MATCH
:
881 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
883 vty_redraw_line (vty
);
885 case CMD_COMPLETE_FULL_MATCH
:
887 vty_redraw_line (vty
);
888 vty_backward_pure_word (vty
);
889 vty_insert_word_overwrite (vty
, matched
[0]);
890 vty_self_insert (vty
, ' ');
891 XFREE (MTYPE_TMP
, matched
[0]);
893 case CMD_COMPLETE_MATCH
:
895 vty_redraw_line (vty
);
896 vty_backward_pure_word (vty
);
897 vty_insert_word_overwrite (vty
, matched
[0]);
898 XFREE (MTYPE_TMP
, matched
[0]);
899 vector_only_index_free (matched
);
902 case CMD_COMPLETE_LIST_MATCH
:
903 for (i
= 0; matched
[i
] != NULL
; i
++)
905 if (i
!= 0 && ((i
% 6) == 0))
906 vty_out (vty
, "%s", VTY_NEWLINE
);
907 vty_out (vty
, "%-10s ", matched
[i
]);
908 XFREE (MTYPE_TMP
, matched
[i
]);
910 vty_out (vty
, "%s", VTY_NEWLINE
);
913 vty_redraw_line (vty
);
915 case CMD_ERR_NOTHING_TODO
:
917 vty_redraw_line (vty
);
923 vector_only_index_free (matched
);
927 vty_describe_fold (struct vty
*vty
, int cmd_width
,
928 unsigned int desc_width
, struct desc
*desc
)
934 cmd
= desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
;
938 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, desc
->str
, VTY_NEWLINE
);
942 buf
= XCALLOC (MTYPE_TMP
, strlen (desc
->str
) + 1);
944 for (p
= desc
->str
; strlen (p
) > desc_width
; p
+= pos
+ 1)
946 for (pos
= desc_width
; pos
> 0; pos
--)
947 if (*(p
+ pos
) == ' ')
953 strncpy (buf
, p
, pos
);
955 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
960 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
962 XFREE (MTYPE_TMP
, buf
);
965 /* Describe matched command function. */
967 vty_describe_command (struct vty
*vty
)
972 unsigned int i
, width
, desc_width
;
973 struct desc
*desc
, *desc_cr
= NULL
;
975 vline
= cmd_make_strvec (vty
->buf
);
977 /* In case of '> ?'. */
980 vline
= vector_init (1);
981 vector_set (vline
, '\0');
984 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
985 vector_set (vline
, '\0');
987 describe
= cmd_describe_command (vline
, vty
, &ret
);
989 vty_out (vty
, "%s", VTY_NEWLINE
);
991 /* Ambiguous error. */
994 case CMD_ERR_AMBIGUOUS
:
995 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
998 case CMD_ERR_NO_MATCH
:
999 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1004 /* Get width of command string. */
1006 for (i
= 0; i
< vector_active (describe
); i
++)
1007 if ((desc
= vector_slot (describe
, i
)) != NULL
)
1011 if (desc
->cmd
[0] == '\0')
1014 len
= strlen (desc
->cmd
);
1015 if (desc
->cmd
[0] == '.')
1022 /* Get width of description string. */
1023 desc_width
= vty
->width
- (width
+ 6);
1025 /* Print out description. */
1026 for (i
= 0; i
< vector_active (describe
); i
++)
1027 if ((desc
= vector_slot (describe
, i
)) != NULL
)
1029 if (desc
->cmd
[0] == '\0')
1032 if (strcmp (desc
->cmd
, "<cr>") == 0)
1039 vty_out (vty
, " %-s%s",
1040 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1042 else if (desc_width
>= strlen (desc
->str
))
1043 vty_out (vty
, " %-*s %s%s", width
,
1044 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1045 desc
->str
, VTY_NEWLINE
);
1047 vty_describe_fold (vty
, width
, desc_width
, desc
);
1050 vty_out (vty
, " %-*s %s%s", width
1051 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1052 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1056 if ((desc
= desc_cr
))
1059 vty_out (vty
, " %-s%s",
1060 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1062 else if (desc_width
>= strlen (desc
->str
))
1063 vty_out (vty
, " %-*s %s%s", width
,
1064 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1065 desc
->str
, VTY_NEWLINE
);
1067 vty_describe_fold (vty
, width
, desc_width
, desc
);
1071 cmd_free_strvec (vline
);
1073 vector_free (describe
);
1076 vty_redraw_line (vty
);
1080 vty_clear_buf (struct vty
*vty
)
1082 memset (vty
->buf
, 0, vty
->max
);
1085 /* ^C stop current input and do not add command line to the history. */
1087 vty_stop_input (struct vty
*vty
)
1089 vty
->cp
= vty
->length
= 0;
1090 vty_clear_buf (vty
);
1091 vty_out (vty
, "%s", VTY_NEWLINE
);
1097 /* Nothing to do. */
1100 case INTERFACE_NODE
:
1110 case KEYCHAIN_KEY_NODE
:
1113 vty_config_unlock (vty
);
1114 vty
->node
= ENABLE_NODE
;
1117 /* Unknown node, we have to ignore it. */
1122 /* Set history pointer to the latest one. */
1123 vty
->hp
= vty
->hindex
;
1126 /* Add current command line to the history buffer. */
1128 vty_hist_add (struct vty
*vty
)
1132 if (vty
->length
== 0)
1135 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1137 /* Ignore the same string as previous one. */
1138 if (vty
->hist
[index
])
1139 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1141 vty
->hp
= vty
->hindex
;
1145 /* Insert history entry. */
1146 if (vty
->hist
[vty
->hindex
])
1147 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1148 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1150 /* History index rotation. */
1152 if (vty
->hindex
== VTY_MAXHIST
)
1155 vty
->hp
= vty
->hindex
;
1158 /* #define TELNET_OPTION_DEBUG */
1160 /* Get telnet window size. */
1162 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1164 #ifdef TELNET_OPTION_DEBUG
1167 for (i
= 0; i
< nbytes
; i
++)
1172 vty_out (vty
, "IAC ");
1175 vty_out (vty
, "WILL ");
1178 vty_out (vty
, "WONT ");
1181 vty_out (vty
, "DO ");
1184 vty_out (vty
, "DONT ");
1187 vty_out (vty
, "SB ");
1190 vty_out (vty
, "SE ");
1193 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1196 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1199 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1202 vty_out (vty
, "%x ", buf
[i
]);
1206 vty_out (vty
, "%s", VTY_NEWLINE
);
1208 #endif /* TELNET_OPTION_DEBUG */
1214 vty
->iac_sb_in_progress
= 1;
1219 if (!vty
->iac_sb_in_progress
)
1222 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1224 vty
->iac_sb_in_progress
= 0;
1227 switch (vty
->sb_buf
[0])
1230 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1231 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1232 "should send %d characters, but we received %lu",
1233 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1234 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1235 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1236 "too small to handle the telnet NAWS option",
1237 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1240 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1241 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1242 #ifdef TELNET_OPTION_DEBUG
1243 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1244 "width %d, height %d%s",
1245 vty
->width
, vty
->height
, VTY_NEWLINE
);
1250 vty
->iac_sb_in_progress
= 0;
1260 /* Execute current command line. */
1262 vty_execute (struct vty
*vty
)
1271 case AUTH_ENABLE_NODE
:
1272 vty_auth (vty
, vty
->buf
);
1275 ret
= vty_command (vty
, vty
->buf
);
1276 if (vty
->type
== VTY_TERM
)
1281 /* Clear command line buffer. */
1282 vty
->cp
= vty
->length
= 0;
1283 vty_clear_buf (vty
);
1285 if (vty
->status
!= VTY_CLOSE
)
1291 #define CONTROL(X) ((X) - '@')
1292 #define VTY_NORMAL 0
1293 #define VTY_PRE_ESCAPE 1
1294 #define VTY_ESCAPE 2
1296 /* Escape character command map. */
1298 vty_escape_map (unsigned char c
, struct vty
*vty
)
1303 vty_previous_line (vty
);
1306 vty_next_line (vty
);
1309 vty_forward_char (vty
);
1312 vty_backward_char (vty
);
1318 /* Go back to normal mode. */
1319 vty
->escape
= VTY_NORMAL
;
1322 /* Quit print out to the buffer. */
1324 vty_buffer_reset (struct vty
*vty
)
1326 buffer_reset (vty
->obuf
);
1328 vty_redraw_line (vty
);
1331 /* Read data via vty socket. */
1333 vty_read (struct thread
*thread
)
1337 unsigned char buf
[VTY_READ_BUFSIZ
];
1339 int vty_sock
= THREAD_FD (thread
);
1340 struct vty
*vty
= THREAD_ARG (thread
);
1343 /* Read raw data from socket */
1344 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1348 if (ERRNO_IO_RETRY(errno
))
1350 vty_event (VTY_READ
, vty_sock
, vty
);
1353 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1354 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1355 __func__
, vty
->fd
, safe_strerror(errno
));
1357 buffer_reset(vty
->obuf
);
1358 vty
->status
= VTY_CLOSE
;
1361 for (i
= 0; i
< nbytes
; i
++)
1376 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1378 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1379 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1386 /* In case of telnet command */
1388 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1395 if (vty
->status
== VTY_MORE
)
1402 vty_buffer_reset (vty
);
1404 #if 0 /* More line does not work for "show ip bgp". */
1407 vty
->status
= VTY_MORELINE
;
1416 /* Escape character. */
1417 if (vty
->escape
== VTY_ESCAPE
)
1419 vty_escape_map (buf
[i
], vty
);
1423 /* Pre-escape status. */
1424 if (vty
->escape
== VTY_PRE_ESCAPE
)
1429 vty
->escape
= VTY_ESCAPE
;
1432 vty_backward_word (vty
);
1433 vty
->escape
= VTY_NORMAL
;
1436 vty_forward_word (vty
);
1437 vty
->escape
= VTY_NORMAL
;
1440 vty_forward_kill_word (vty
);
1441 vty
->escape
= VTY_NORMAL
;
1445 vty_backward_kill_word (vty
);
1446 vty
->escape
= VTY_NORMAL
;
1449 vty
->escape
= VTY_NORMAL
;
1458 vty_beginning_of_line (vty
);
1461 vty_backward_char (vty
);
1464 vty_stop_input (vty
);
1467 vty_delete_char (vty
);
1470 vty_end_of_line (vty
);
1473 vty_forward_char (vty
);
1477 vty_delete_backward_char (vty
);
1480 vty_kill_line (vty
);
1483 vty_next_line (vty
);
1486 vty_previous_line (vty
);
1489 vty_transpose_chars (vty
);
1492 vty_kill_line_from_beginning (vty
);
1495 vty_backward_kill_word (vty
);
1498 vty_end_config (vty
);
1502 vty_out (vty
, "%s", VTY_NEWLINE
);
1506 vty_complete_command (vty
);
1509 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1510 vty_self_insert (vty
, buf
[i
]);
1512 vty_describe_command (vty
);
1515 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1517 vty
->escape
= VTY_ESCAPE
;
1521 vty
->escape
= VTY_PRE_ESCAPE
;
1524 if (buf
[i
] > 31 && buf
[i
] < 127)
1525 vty_self_insert (vty
, buf
[i
]);
1531 if (vty
->status
== VTY_CLOSE
)
1535 vty_event (VTY_WRITE
, vty_sock
, vty
);
1536 vty_event (VTY_READ
, vty_sock
, vty
);
1541 /* Flush buffer to the vty. */
1543 vty_flush (struct thread
*thread
)
1546 buffer_status_t flushrc
;
1547 int vty_sock
= THREAD_FD (thread
);
1548 struct vty
*vty
= THREAD_ARG (thread
);
1550 vty
->t_write
= NULL
;
1552 /* Tempolary disable read thread. */
1553 if ((vty
->lines
== 0) && vty
->t_read
)
1555 thread_cancel (vty
->t_read
);
1559 /* Function execution continue. */
1560 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1562 /* N.B. if width is 0, that means we don't know the window size. */
1563 if ((vty
->lines
== 0) || (vty
->width
== 0))
1564 flushrc
= buffer_flush_available(vty
->obuf
, vty
->fd
);
1565 else if (vty
->status
== VTY_MORELINE
)
1566 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1569 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1570 vty
->lines
>= 0 ? vty
->lines
:
1576 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1577 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1579 buffer_reset(vty
->obuf
);
1583 if (vty
->status
== VTY_CLOSE
)
1587 vty
->status
= VTY_NORMAL
;
1588 if (vty
->lines
== 0)
1589 vty_event (VTY_READ
, vty_sock
, vty
);
1592 case BUFFER_PENDING
:
1593 /* There is more data waiting to be written. */
1594 vty
->status
= VTY_MORE
;
1595 if (vty
->lines
== 0)
1596 vty_event (VTY_WRITE
, vty_sock
, vty
);
1603 /* Create new vty structure. */
1605 vty_create (int vty_sock
, union sockunion
*su
)
1609 /* Allocate new vty structure and set up default values. */
1612 vty
->type
= VTY_TERM
;
1613 vty
->address
= sockunion_su2str (su
);
1614 if (no_password_check
)
1617 vty
->node
= ENABLE_NODE
;
1619 vty
->node
= VIEW_NODE
;
1622 vty
->node
= AUTH_NODE
;
1625 vty_clear_buf (vty
);
1627 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1630 vector_set_index (vtyvec
, vty_sock
, vty
);
1631 vty
->status
= VTY_NORMAL
;
1632 vty
->v_timeout
= vty_timeout_val
;
1633 if (host
.lines
>= 0)
1634 vty
->lines
= host
.lines
;
1638 vty
->iac_sb_in_progress
= 0;
1641 if (! no_password_check
)
1643 /* Vty is not available if password isn't set. */
1644 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1646 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1647 vty
->status
= VTY_CLOSE
;
1653 /* Say hello to the world. */
1655 if (! no_password_check
)
1656 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1658 /* Setting up terminal. */
1659 vty_will_echo (vty
);
1660 vty_will_suppress_go_ahead (vty
);
1662 vty_dont_linemode (vty
);
1663 vty_do_window_size (vty
);
1664 /* vty_dont_lflow_ahead (vty); */
1668 /* Add read/write thread. */
1669 vty_event (VTY_WRITE
, vty_sock
, vty
);
1670 vty_event (VTY_READ
, vty_sock
, vty
);
1675 /* Accept connection from the network. */
1677 vty_accept (struct thread
*thread
)
1685 struct prefix
*p
= NULL
;
1686 struct access_list
*acl
= NULL
;
1688 accept_sock
= THREAD_FD (thread
);
1690 /* We continue hearing vty socket. */
1691 vty_event (VTY_SERV
, accept_sock
, NULL
);
1693 memset (&su
, 0, sizeof (union sockunion
));
1695 /* We can handle IPv4 or IPv6 socket. */
1696 vty_sock
= sockunion_accept (accept_sock
, &su
);
1699 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1702 set_nonblocking(vty_sock
);
1704 p
= sockunion2hostprefix (&su
);
1706 /* VTY's accesslist apply. */
1707 if (p
->family
== AF_INET
&& vty_accesslist_name
)
1709 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1710 (access_list_apply (acl
, p
) == FILTER_DENY
))
1713 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1714 (buf
= sockunion_su2str (&su
)));
1718 /* continue accepting connections */
1719 vty_event (VTY_SERV
, accept_sock
, NULL
);
1728 /* VTY's ipv6 accesslist apply. */
1729 if (p
->family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1731 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1732 (access_list_apply (acl
, p
) == FILTER_DENY
))
1735 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1736 (buf
= sockunion_su2str (&su
)));
1740 /* continue accepting connections */
1741 vty_event (VTY_SERV
, accept_sock
, NULL
);
1748 #endif /* HAVE_IPV6 */
1753 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1754 (char *) &on
, sizeof (on
));
1756 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1757 safe_strerror (errno
));
1759 vty
= vty_create (vty_sock
, &su
);
1764 #if defined(HAVE_IPV6) && !defined(NRL)
1766 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1769 struct addrinfo req
;
1770 struct addrinfo
*ainfo
;
1771 struct addrinfo
*ainfo_save
;
1773 char port_str
[BUFSIZ
];
1775 memset (&req
, 0, sizeof (struct addrinfo
));
1776 req
.ai_flags
= AI_PASSIVE
;
1777 req
.ai_family
= AF_UNSPEC
;
1778 req
.ai_socktype
= SOCK_STREAM
;
1779 sprintf (port_str
, "%d", port
);
1780 port_str
[sizeof (port_str
) - 1] = '\0';
1782 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1786 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1794 if (ainfo
->ai_family
!= AF_INET
1796 && ainfo
->ai_family
!= AF_INET6
1797 #endif /* HAVE_IPV6 */
1801 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1805 sockopt_reuseaddr (sock
);
1806 sockopt_reuseport (sock
);
1808 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1811 close (sock
); /* Avoid sd leak. */
1815 ret
= listen (sock
, 3);
1818 close (sock
); /* Avoid sd leak. */
1822 vty_event (VTY_SERV
, sock
, NULL
);
1824 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1826 freeaddrinfo (ainfo_save
);
1828 #endif /* HAVE_IPV6 && ! NRL */
1830 /* Make vty server socket. */
1832 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1839 memset (&su
, 0, sizeof (union sockunion
));
1840 su
.sa
.sa_family
= family
;
1845 naddr
=&su
.sin
.sin_addr
;
1848 naddr
=&su
.sin6
.sin6_addr
;
1853 switch(inet_pton(family
,addr
,naddr
))
1856 zlog_err("bad address %s",addr
);
1860 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
1864 /* Make new socket. */
1865 accept_sock
= sockunion_stream_socket (&su
);
1866 if (accept_sock
< 0)
1869 /* This is server, so reuse address. */
1870 sockopt_reuseaddr (accept_sock
);
1871 sockopt_reuseport (accept_sock
);
1873 /* Bind socket to universal address and given port. */
1874 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
1877 zlog_warn("can't bind socket");
1878 close (accept_sock
); /* Avoid sd leak. */
1882 /* Listen socket under queue 3. */
1883 ret
= listen (accept_sock
, 3);
1886 zlog (NULL
, LOG_WARNING
, "can't listen socket");
1887 close (accept_sock
); /* Avoid sd leak. */
1891 /* Add vty server event. */
1892 vty_event (VTY_SERV
, accept_sock
, NULL
);
1896 /* For sockaddr_un. */
1899 /* VTY shell UNIX domain socket. */
1901 vty_serv_un (const char *path
)
1905 struct sockaddr_un serv
;
1907 struct zprivs_ids_t ids
;
1909 /* First of all, unlink existing socket */
1913 old_mask
= umask (0007);
1915 /* Make UNIX domain socket. */
1916 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1919 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
1923 /* Make server socket. */
1924 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1925 serv
.sun_family
= AF_UNIX
;
1926 strncpy (serv
.sun_path
, path
, strlen (path
));
1927 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1928 len
= serv
.sun_len
= SUN_LEN(&serv
);
1930 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1931 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1933 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1936 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1937 close (sock
); /* Avoid sd leak. */
1941 ret
= listen (sock
, 5);
1944 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
1945 close (sock
); /* Avoid sd leak. */
1951 zprivs_get_ids(&ids
);
1953 if (ids
.gid_vty
> 0)
1955 /* set group of socket */
1956 if ( chown (path
, -1, ids
.gid_vty
) )
1958 zlog_err ("vty_serv_un: could chown socket, %s",
1959 safe_strerror (errno
) );
1963 vty_event (VTYSH_SERV
, sock
, NULL
);
1966 /* #define VTYSH_DEBUG 1 */
1969 vtysh_accept (struct thread
*thread
)
1974 struct sockaddr_un client
;
1977 accept_sock
= THREAD_FD (thread
);
1979 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
1981 memset (&client
, 0, sizeof (struct sockaddr_un
));
1982 client_len
= sizeof (struct sockaddr_un
);
1984 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
1985 (socklen_t
*) &client_len
);
1989 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1993 if (set_nonblocking(sock
) < 0)
1995 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
1996 " %s, closing", sock
, safe_strerror (errno
));
2002 printf ("VTY shell accept\n");
2003 #endif /* VTYSH_DEBUG */
2007 vty
->type
= VTY_SHELL_SERV
;
2008 vty
->node
= VIEW_NODE
;
2010 vty_event (VTYSH_READ
, sock
, vty
);
2016 vtysh_flush(struct vty
*vty
)
2018 switch (buffer_flush_available(vty
->obuf
, vty
->fd
))
2020 case BUFFER_PENDING
:
2021 vty_event(VTYSH_WRITE
, vty
->fd
, vty
);
2024 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2025 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2026 buffer_reset(vty
->obuf
);
2037 vtysh_read (struct thread
*thread
)
2043 unsigned char buf
[VTY_READ_BUFSIZ
];
2045 u_char header
[4] = {0, 0, 0, 0};
2047 sock
= THREAD_FD (thread
);
2048 vty
= THREAD_ARG (thread
);
2051 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2055 if (ERRNO_IO_RETRY(errno
))
2057 vty_event (VTYSH_READ
, sock
, vty
);
2060 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2061 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2062 __func__
, sock
, safe_strerror(errno
));
2064 buffer_reset(vty
->obuf
);
2067 printf ("close vtysh\n");
2068 #endif /* VTYSH_DEBUG */
2073 printf ("line: %.*s\n", nbytes
, buf
);
2074 #endif /* VTYSH_DEBUG */
2076 for (p
= buf
; p
< buf
+nbytes
; p
++)
2078 vty_ensure(vty
, vty
->length
+1);
2079 vty
->buf
[vty
->length
++] = *p
;
2082 /* Pass this line to parser. */
2083 ret
= vty_execute (vty
);
2084 /* Note that vty_execute clears the command buffer and resets
2085 vty->length to 0. */
2087 /* Return result. */
2089 printf ("result: %d\n", ret
);
2090 printf ("vtysh node: %d\n", vty
->node
);
2091 #endif /* VTYSH_DEBUG */
2094 buffer_put(vty
->obuf
, header
, 4);
2096 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2097 /* Try to flush results; exit if a write error occurs. */
2102 vty_event (VTYSH_READ
, sock
, vty
);
2108 vtysh_write (struct thread
*thread
)
2110 struct vty
*vty
= THREAD_ARG (thread
);
2112 vty
->t_write
= NULL
;
2119 /* Determine address family to bind. */
2121 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2123 /* If port is set to 0, do not listen on TCP/IP at all! */
2129 vty_serv_sock_family (addr
, port
, AF_INET
);
2130 vty_serv_sock_family (addr
, port
, AF_INET6
);
2132 vty_serv_sock_addrinfo (addr
, port
);
2134 #else /* ! HAVE_IPV6 */
2135 vty_serv_sock_family (addr
,port
, AF_INET
);
2136 #endif /* HAVE_IPV6 */
2144 /* Close vty interface. Warning: call this only from functions that
2145 will be careful not to access the vty afterwards (since it has
2146 now been freed). This is safest from top-level functions (called
2147 directly by the thread dispatcher). */
2149 vty_close (struct vty
*vty
)
2153 /* Cancel threads.*/
2155 thread_cancel (vty
->t_read
);
2157 thread_cancel (vty
->t_write
);
2159 thread_cancel (vty
->t_timeout
);
2162 buffer_flush_all (vty
->obuf
, vty
->fd
);
2164 /* Free input buffer. */
2165 buffer_free (vty
->obuf
);
2167 /* Free command history. */
2168 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2170 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2173 vector_unset (vtyvec
, vty
->fd
);
2180 XFREE (MTYPE_TMP
, vty
->address
);
2182 XFREE (MTYPE_VTY
, vty
->buf
);
2184 /* Check configure. */
2185 vty_config_unlock (vty
);
2188 XFREE (MTYPE_VTY
, vty
);
2191 /* When time out occur output message then close connection. */
2193 vty_timeout (struct thread
*thread
)
2197 vty
= THREAD_ARG (thread
);
2198 vty
->t_timeout
= NULL
;
2202 buffer_reset (vty
->obuf
);
2203 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2205 /* Close connection. */
2206 vty
->status
= VTY_CLOSE
;
2212 /* Read up configuration file from file_name. */
2214 vty_read_file (FILE *confp
)
2220 vty
->fd
= 0; /* stdout */
2221 vty
->type
= VTY_TERM
;
2222 vty
->node
= CONFIG_NODE
;
2224 /* Execute configuration file */
2225 ret
= config_from_file (vty
, confp
);
2227 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2231 case CMD_ERR_AMBIGUOUS
:
2232 fprintf (stderr
, "Ambiguous command.\n");
2234 case CMD_ERR_NO_MATCH
:
2235 fprintf (stderr
, "There is no such command.\n");
2238 fprintf (stderr
, "Error occured during reading below line.\n%s\n",
2248 vty_use_backup_config (char *fullpath
)
2250 char *fullpath_sav
, *fullpath_tmp
;
2257 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2258 strcpy (fullpath_sav
, fullpath
);
2259 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2260 if (stat (fullpath_sav
, &buf
) == -1)
2262 free (fullpath_sav
);
2266 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2267 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2269 /* Open file to configuration write. */
2270 tmp
= mkstemp (fullpath_tmp
);
2273 free (fullpath_sav
);
2274 free (fullpath_tmp
);
2278 sav
= open (fullpath_sav
, O_RDONLY
);
2281 unlink (fullpath_tmp
);
2282 free (fullpath_sav
);
2283 free (fullpath_tmp
);
2287 while((c
= read (sav
, buffer
, 512)) > 0)
2288 write (tmp
, buffer
, c
);
2293 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2295 unlink (fullpath_tmp
);
2296 free (fullpath_sav
);
2297 free (fullpath_tmp
);
2301 if (link (fullpath_tmp
, fullpath
) == 0)
2302 ret
= fopen (fullpath
, "r");
2304 unlink (fullpath_tmp
);
2306 free (fullpath_sav
);
2307 free (fullpath_tmp
);
2311 /* Read up configuration file from file_name. */
2313 vty_read_config (char *config_file
,
2314 char *config_default_dir
)
2316 char cwd
[MAXPATHLEN
];
2321 /* If -f flag specified. */
2322 if (config_file
!= NULL
)
2324 if (! IS_DIRECTORY_SEP (config_file
[0]))
2326 getcwd (cwd
, MAXPATHLEN
);
2327 tmp
= XMALLOC (MTYPE_TMP
,
2328 strlen (cwd
) + strlen (config_file
) + 2);
2329 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2333 fullpath
= config_file
;
2335 confp
= fopen (fullpath
, "r");
2339 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2340 __func__
, fullpath
, safe_strerror (errno
));
2342 confp
= vty_use_backup_config (fullpath
);
2344 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2347 fprintf (stderr
, "can't open configuration file [%s]\n",
2357 struct stat conf_stat
;
2359 /* !!!!PLEASE LEAVE!!!!
2360 * This is NEEDED for use with vtysh -b, or else you can get
2361 * a real configuration food fight with a lot garbage in the
2362 * merged configuration file it creates coming from the per
2363 * daemon configuration files. This also allows the daemons
2364 * to start if there default configuration file is not
2365 * present or ignore them, as needed when using vtysh -b to
2366 * configure the daemons at boot - MAG
2369 /* Stat for vtysh Zebra.conf, if found startup and wait for
2370 * boot configuration
2373 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2375 ret
= stat (integrate_default
, &conf_stat
);
2381 confp
= fopen (config_default_dir
, "r");
2384 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2385 __func__
, config_default_dir
, safe_strerror (errno
));
2387 confp
= vty_use_backup_config (config_default_dir
);
2390 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2391 fullpath
= config_default_dir
;
2395 fprintf (stderr
, "can't open configuration file [%s]\n",
2396 config_default_dir
);
2401 fullpath
= config_default_dir
;
2404 vty_read_file (confp
);
2408 host_config_set (fullpath
);
2411 XFREE (MTYPE_TMP
, fullpath
);
2414 /* Small utility function which output log to the VTY. */
2416 vty_log (const char *level
, const char *proto_str
,
2417 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2425 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2426 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2431 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2436 /* Async-signal-safe version of vty_log for fixed strings. */
2438 vty_log_fixed (const char *buf
, size_t len
)
2441 struct iovec iov
[2];
2443 /* vty may not have been initialised */
2447 iov
[0].iov_base
= (void *)buf
;
2448 iov
[0].iov_len
= len
;
2449 iov
[1].iov_base
= (void *)"\r\n";
2452 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2455 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2456 /* N.B. We don't care about the return code, since process is
2457 most likely just about to die anyway. */
2458 writev(vty
->fd
, iov
, 2);
2463 vty_config_lock (struct vty
*vty
)
2465 if (vty_config
== 0)
2474 vty_config_unlock (struct vty
*vty
)
2476 if (vty_config
== 1 && vty
->config
== 1)
2484 /* Master of the threads. */
2485 static struct thread_master
*master
;
2488 vty_event (enum event event
, int sock
, struct vty
*vty
)
2490 struct thread
*vty_serv_thread
;
2495 vty_serv_thread
= thread_add_read (master
, vty_accept
, vty
, sock
);
2496 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2500 thread_add_read (master
, vtysh_accept
, vty
, sock
);
2503 vty
->t_read
= thread_add_read (master
, vtysh_read
, vty
, sock
);
2506 vty
->t_write
= thread_add_write (master
, vtysh_write
, vty
, sock
);
2510 vty
->t_read
= thread_add_read (master
, vty_read
, vty
, sock
);
2512 /* Time out treatment. */
2516 thread_cancel (vty
->t_timeout
);
2518 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2523 vty
->t_write
= thread_add_write (master
, vty_flush
, vty
, sock
);
2525 case VTY_TIMEOUT_RESET
:
2528 thread_cancel (vty
->t_timeout
);
2529 vty
->t_timeout
= NULL
;
2534 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2543 "Display who is on vty\n")
2548 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2549 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2550 vty_out (vty
, "%svty[%d] connected from %s.%s",
2551 v
->config
? "*" : " ",
2552 i
, v
->address
, VTY_NEWLINE
);
2556 /* Move to vty configuration mode. */
2560 "Configure a terminal line\n"
2561 "Virtual terminal\n")
2563 vty
->node
= VTY_NODE
;
2567 /* Set time out value. */
2569 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2571 unsigned long timeout
= 0;
2573 /* min_str and sec_str are already checked by parser. So it must be
2574 all digit string. */
2577 timeout
= strtol (min_str
, NULL
, 10);
2581 timeout
+= strtol (sec_str
, NULL
, 10);
2583 vty_timeout_val
= timeout
;
2584 vty
->v_timeout
= timeout
;
2585 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2591 DEFUN (exec_timeout_min
,
2592 exec_timeout_min_cmd
,
2593 "exec-timeout <0-35791>",
2594 "Set timeout value\n"
2595 "Timeout value in minutes\n")
2597 return exec_timeout (vty
, argv
[0], NULL
);
2600 DEFUN (exec_timeout_sec
,
2601 exec_timeout_sec_cmd
,
2602 "exec-timeout <0-35791> <0-2147483>",
2603 "Set the EXEC timeout\n"
2604 "Timeout in minutes\n"
2605 "Timeout in seconds\n")
2607 return exec_timeout (vty
, argv
[0], argv
[1]);
2610 DEFUN (no_exec_timeout
,
2611 no_exec_timeout_cmd
,
2614 "Set the EXEC timeout\n")
2616 return exec_timeout (vty
, NULL
, NULL
);
2619 /* Set vty access class. */
2620 DEFUN (vty_access_class
,
2621 vty_access_class_cmd
,
2622 "access-class WORD",
2623 "Filter connections based on an IP access list\n"
2626 if (vty_accesslist_name
)
2627 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2629 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2634 /* Clear vty access class. */
2635 DEFUN (no_vty_access_class
,
2636 no_vty_access_class_cmd
,
2637 "no access-class [WORD]",
2639 "Filter connections based on an IP access list\n"
2642 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2644 vty_out (vty
, "Access-class is not currently applied to vty%s",
2649 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2651 vty_accesslist_name
= NULL
;
2657 /* Set vty access class. */
2658 DEFUN (vty_ipv6_access_class
,
2659 vty_ipv6_access_class_cmd
,
2660 "ipv6 access-class WORD",
2662 "Filter connections based on an IP access list\n"
2663 "IPv6 access list\n")
2665 if (vty_ipv6_accesslist_name
)
2666 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2668 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2673 /* Clear vty access class. */
2674 DEFUN (no_vty_ipv6_access_class
,
2675 no_vty_ipv6_access_class_cmd
,
2676 "no ipv6 access-class [WORD]",
2679 "Filter connections based on an IP access list\n"
2680 "IPv6 access list\n")
2682 if (! vty_ipv6_accesslist_name
||
2683 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2685 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2690 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2692 vty_ipv6_accesslist_name
= NULL
;
2696 #endif /* HAVE_IPV6 */
2702 "Enable password checking\n")
2704 no_password_check
= 0;
2708 DEFUN (no_vty_login
,
2712 "Enable password checking\n")
2714 no_password_check
= 1;
2718 DEFUN (service_advanced_vty
,
2719 service_advanced_vty_cmd
,
2720 "service advanced-vty",
2721 "Set up miscellaneous service\n"
2722 "Enable advanced mode vty interface\n")
2728 DEFUN (no_service_advanced_vty
,
2729 no_service_advanced_vty_cmd
,
2730 "no service advanced-vty",
2732 "Set up miscellaneous service\n"
2733 "Enable advanced mode vty interface\n")
2739 DEFUN (terminal_monitor
,
2740 terminal_monitor_cmd
,
2742 "Set terminal line parameters\n"
2743 "Copy debug output to the current terminal line\n")
2749 DEFUN (terminal_no_monitor
,
2750 terminal_no_monitor_cmd
,
2751 "terminal no monitor",
2752 "Set terminal line parameters\n"
2754 "Copy debug output to the current terminal line\n")
2760 ALIAS (terminal_no_monitor
,
2761 no_terminal_monitor_cmd
,
2762 "no terminal monitor",
2764 "Set terminal line parameters\n"
2765 "Copy debug output to the current terminal line\n")
2767 DEFUN (show_history
,
2771 "Display the session command history\n")
2775 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2777 if (index
== VTY_MAXHIST
)
2783 if (vty
->hist
[index
] != NULL
)
2784 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2792 /* Display current configuration. */
2794 vty_config_write (struct vty
*vty
)
2796 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2798 if (vty_accesslist_name
)
2799 vty_out (vty
, " access-class %s%s",
2800 vty_accesslist_name
, VTY_NEWLINE
);
2802 if (vty_ipv6_accesslist_name
)
2803 vty_out (vty
, " ipv6 access-class %s%s",
2804 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
2807 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2808 vty_out (vty
, " exec-timeout %ld %ld%s",
2809 vty_timeout_val
/ 60,
2810 vty_timeout_val
% 60, VTY_NEWLINE
);
2813 if (no_password_check
)
2814 vty_out (vty
, " no login%s", VTY_NEWLINE
);
2816 vty_out (vty
, "!%s", VTY_NEWLINE
);
2821 struct cmd_node vty_node
=
2824 "%s(config-line)# ",
2828 /* Reset all VTY status. */
2834 struct thread
*vty_serv_thread
;
2836 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2837 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2839 buffer_reset (vty
->obuf
);
2840 vty
->status
= VTY_CLOSE
;
2844 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
2845 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2847 thread_cancel (vty_serv_thread
);
2848 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2852 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2854 if (vty_accesslist_name
)
2856 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2857 vty_accesslist_name
= NULL
;
2860 if (vty_ipv6_accesslist_name
)
2862 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2863 vty_ipv6_accesslist_name
= NULL
;
2870 char cwd
[MAXPATHLEN
];
2873 c
= getcwd (cwd
, MAXPATHLEN
);
2878 getcwd (cwd
, MAXPATHLEN
);
2881 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
2882 strcpy (vty_cwd
, cwd
);
2892 vty_shell (struct vty
*vty
)
2894 return vty
->type
== VTY_SHELL
? 1 : 0;
2898 vty_shell_serv (struct vty
*vty
)
2900 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2906 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2909 /* Install vty's own commands like `who' command. */
2911 vty_init (struct thread_master
*master_thread
)
2913 /* For further configuration read, preserve current directory. */
2916 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2918 master
= master_thread
;
2920 /* Initilize server thread vector. */
2921 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
2923 /* Install bgp top node. */
2924 install_node (&vty_node
, vty_config_write
);
2926 install_element (VIEW_NODE
, &config_who_cmd
);
2927 install_element (VIEW_NODE
, &show_history_cmd
);
2928 install_element (ENABLE_NODE
, &config_who_cmd
);
2929 install_element (CONFIG_NODE
, &line_vty_cmd
);
2930 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
2931 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
2932 install_element (CONFIG_NODE
, &show_history_cmd
);
2933 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
2934 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
2935 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
2936 install_element (ENABLE_NODE
, &show_history_cmd
);
2938 install_default (VTY_NODE
);
2939 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
2940 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
2941 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
2942 install_element (VTY_NODE
, &vty_access_class_cmd
);
2943 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
2944 install_element (VTY_NODE
, &vty_login_cmd
);
2945 install_element (VTY_NODE
, &no_vty_login_cmd
);
2947 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
2948 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
2949 #endif /* HAVE_IPV6 */