2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <lib/version.h>
30 #include "sockunion.h"
40 #include <arpa/telnet.h>
56 static void vty_event (enum event
, int, struct vty
*);
58 /* Extern host structure from command.c */
59 extern struct host host
;
61 /* Vector which store each vty structure. */
64 /* Vty timeout value. */
65 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
67 /* Vty access-class command */
68 static char *vty_accesslist_name
= NULL
;
70 /* Vty access-calss for IPv6. */
71 static char *vty_ipv6_accesslist_name
= NULL
;
73 /* VTY server thread. */
74 static vector Vvty_serv_thread
;
76 /* Current directory. */
80 static int vty_config
;
82 /* Login password check. */
83 static int no_password_check
= 0;
85 /* Restrict unauthenticated logins? */
86 static const u_char restricted_mode_default
= 0;
87 static u_char restricted_mode
= 0;
89 /* Integrated configuration file path */
90 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
93 /* VTY standard output function. */
95 vty_out (struct vty
*vty
, const char *format
, ...)
105 va_start (args
, format
);
106 vprintf (format
, args
);
111 /* Try to write to initial buffer. */
112 va_start (args
, format
);
113 len
= vsnprintf (buf
, sizeof buf
, format
, args
);
116 /* Initial buffer is not enough. */
117 if (len
< 0 || len
>= size
)
126 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
130 va_start (args
, format
);
131 len
= vsnprintf (p
, size
, format
, args
);
134 if (len
> -1 && len
< size
)
139 /* When initial buffer is enough to store all output. */
143 /* Pointer p must point out buffer. */
144 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
146 /* If p is not different with buf, it is allocated buffer. */
148 XFREE (MTYPE_VTY_OUT_BUF
, p
);
155 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
156 const char *format
, struct timestamp_control
*ctl
, va_list va
)
162 if (!ctl
->already_rendered
)
164 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
165 ctl
->already_rendered
= 1;
167 if (ctl
->len
+1 >= sizeof(buf
))
169 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
174 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
176 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
177 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
180 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
181 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
187 if (write(vty
->fd
, buf
, len
) < 0)
189 if (ERRNO_IO_RETRY(errno
))
190 /* Kernel buffer is full, probably too much debugging output, so just
191 drop the data and ignore. */
193 /* Fatal I/O error. */
194 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
195 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
196 __func__
, vty
->fd
, safe_strerror(errno
));
197 buffer_reset(vty
->obuf
);
198 /* cannot call vty_close, because a parent routine may still try
199 to access the vty struct */
200 vty
->status
= VTY_CLOSE
;
201 shutdown(vty
->fd
, SHUT_RDWR
);
207 /* Output current time to the vty. */
209 vty_time_print (struct vty
*vty
, int cr
)
213 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
215 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
219 vty_out (vty
, "%s\n", buf
);
221 vty_out (vty
, "%s ", buf
);
226 /* Say hello to vty interface. */
228 vty_hello (struct vty
*vty
)
235 f
= fopen (host
.motdfile
, "r");
238 while (fgets (buf
, sizeof (buf
), f
))
241 /* work backwards to ignore trailling isspace() */
242 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
245 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
250 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
253 vty_out (vty
, "%s", host
.motd
);
256 /* Put out prompt and wait input from user. */
258 vty_prompt (struct vty
*vty
)
260 struct utsname names
;
263 if (vty
->type
== VTY_TERM
)
265 hostname
= host
.name
;
269 hostname
= names
.nodename
;
271 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
275 /* Send WILL TELOPT_ECHO to remote server. */
277 vty_will_echo (struct vty
*vty
)
279 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
280 vty_out (vty
, "%s", cmd
);
283 /* Make suppress Go-Ahead telnet option. */
285 vty_will_suppress_go_ahead (struct vty
*vty
)
287 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
288 vty_out (vty
, "%s", cmd
);
291 /* Make don't use linemode over telnet. */
293 vty_dont_linemode (struct vty
*vty
)
295 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
296 vty_out (vty
, "%s", cmd
);
299 /* Use window size. */
301 vty_do_window_size (struct vty
*vty
)
303 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
304 vty_out (vty
, "%s", cmd
);
307 #if 0 /* Currently not used. */
308 /* Make don't use lflow vty interface. */
310 vty_dont_lflow_ahead (struct vty
*vty
)
312 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
313 vty_out (vty
, "%s", cmd
);
317 /* Allocate new vty struct. */
321 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
323 new->obuf
= buffer_new(0); /* Use default buffer size. */
324 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
325 new->max
= VTY_BUFSIZ
;
330 /* Authentication of vty */
332 vty_auth (struct vty
*vty
, char *buf
)
335 enum node_type next_node
= 0;
337 char *crypt (const char *, const char *);
343 passwd
= host
.password_encrypt
;
345 passwd
= host
.password
;
347 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
349 next_node
= VIEW_NODE
;
351 case AUTH_ENABLE_NODE
:
353 passwd
= host
.enable_encrypt
;
355 passwd
= host
.enable
;
356 next_node
= ENABLE_NODE
;
363 fail
= strcmp (crypt(buf
, passwd
), passwd
);
365 fail
= strcmp (buf
, passwd
);
373 vty
->node
= next_node
; /* Success ! */
380 if (vty
->node
== AUTH_NODE
)
382 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
383 vty
->status
= VTY_CLOSE
;
387 /* AUTH_ENABLE_NODE */
389 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
390 vty
->node
= restricted_mode
? RESTRICTED_NODE
: VIEW_NODE
;
396 /* Command execution over the vty interface. */
398 vty_command (struct vty
*vty
, char *buf
)
402 const char *protocolname
;
404 /* Split readline string up into the vector */
405 vline
= cmd_make_strvec (buf
);
410 #ifdef CONSUMED_TIME_CHECK
414 unsigned long realtime
, cputime
;
417 #endif /* CONSUMED_TIME_CHECK */
419 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
421 /* Get the name of the protocol if any */
423 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
425 protocolname
= zlog_proto_names
[ZLOG_NONE
];
427 #ifdef CONSUMED_TIME_CHECK
429 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
431 /* Warn about CPU hog that must be fixed. */
432 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
433 realtime
/1000, cputime
/1000, buf
);
435 #endif /* CONSUMED_TIME_CHECK */
437 if (ret
!= CMD_SUCCESS
)
441 if (vty
->type
== VTY_FILE
)
442 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
444 case CMD_ERR_AMBIGUOUS
:
445 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
447 case CMD_ERR_NO_MATCH
:
448 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
450 case CMD_ERR_INCOMPLETE
:
451 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
454 cmd_free_strvec (vline
);
459 static const char telnet_backward_char
= 0x08;
460 static const char telnet_space_char
= ' ';
462 /* Basic function to write buffer to vty. */
464 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
466 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
469 /* Should we do buffering here ? And make vty_flush (vty) ? */
470 buffer_put (vty
->obuf
, buf
, nbytes
);
473 /* Ensure length of input buffer. Is buffer is short, double it. */
475 vty_ensure (struct vty
*vty
, int length
)
477 if (vty
->max
<= length
)
480 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
484 /* Basic function to insert character into vty. */
486 vty_self_insert (struct vty
*vty
, char c
)
491 vty_ensure (vty
, vty
->length
+ 1);
492 length
= vty
->length
- vty
->cp
;
493 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
494 vty
->buf
[vty
->cp
] = c
;
496 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
497 for (i
= 0; i
< length
; i
++)
498 vty_write (vty
, &telnet_backward_char
, 1);
504 /* Self insert character 'c' in overwrite mode. */
506 vty_self_insert_overwrite (struct vty
*vty
, char c
)
508 vty_ensure (vty
, vty
->length
+ 1);
509 vty
->buf
[vty
->cp
++] = c
;
511 if (vty
->cp
> vty
->length
)
514 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
517 vty_write (vty
, &c
, 1);
520 /* Insert a word into vty interface with overwrite mode. */
522 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
524 int len
= strlen (str
);
525 vty_write (vty
, str
, len
);
526 strcpy (&vty
->buf
[vty
->cp
], str
);
528 vty
->length
= vty
->cp
;
531 /* Forward character. */
533 vty_forward_char (struct vty
*vty
)
535 if (vty
->cp
< vty
->length
)
537 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
542 /* Backward character. */
544 vty_backward_char (struct vty
*vty
)
549 vty_write (vty
, &telnet_backward_char
, 1);
553 /* Move to the beginning of the line. */
555 vty_beginning_of_line (struct vty
*vty
)
558 vty_backward_char (vty
);
561 /* Move to the end of the line. */
563 vty_end_of_line (struct vty
*vty
)
565 while (vty
->cp
< vty
->length
)
566 vty_forward_char (vty
);
569 static void vty_kill_line_from_beginning (struct vty
*);
570 static void vty_redraw_line (struct vty
*);
572 /* Print command line history. This function is called from
573 vty_next_line and vty_previous_line. */
575 vty_history_print (struct vty
*vty
)
579 vty_kill_line_from_beginning (vty
);
581 /* Get previous line from history buffer */
582 length
= strlen (vty
->hist
[vty
->hp
]);
583 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
584 vty
->cp
= vty
->length
= length
;
586 /* Redraw current line */
587 vty_redraw_line (vty
);
590 /* Show next command line history. */
592 vty_next_line (struct vty
*vty
)
596 if (vty
->hp
== vty
->hindex
)
599 /* Try is there history exist or not. */
601 if (try_index
== (VTY_MAXHIST
- 1))
606 /* If there is not history return. */
607 if (vty
->hist
[try_index
] == NULL
)
612 vty_history_print (vty
);
615 /* Show previous command line history. */
617 vty_previous_line (struct vty
*vty
)
623 try_index
= VTY_MAXHIST
- 1;
627 if (vty
->hist
[try_index
] == NULL
)
632 vty_history_print (vty
);
635 /* This function redraw all of the command line character. */
637 vty_redraw_line (struct vty
*vty
)
639 vty_write (vty
, vty
->buf
, vty
->length
);
640 vty
->cp
= vty
->length
;
645 vty_forward_word (struct vty
*vty
)
647 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
648 vty_forward_char (vty
);
650 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
651 vty_forward_char (vty
);
654 /* Backward word without skipping training space. */
656 vty_backward_pure_word (struct vty
*vty
)
658 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
659 vty_backward_char (vty
);
664 vty_backward_word (struct vty
*vty
)
666 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
667 vty_backward_char (vty
);
669 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
670 vty_backward_char (vty
);
673 /* When '^D' is typed at the beginning of the line we move to the down
676 vty_down_level (struct vty
*vty
)
678 vty_out (vty
, "%s", VTY_NEWLINE
);
679 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
684 /* When '^Z' is received from vty, move down to the enable mode. */
686 vty_end_config (struct vty
*vty
)
688 vty_out (vty
, "%s", VTY_NEWLINE
);
694 case RESTRICTED_NODE
:
714 case KEYCHAIN_KEY_NODE
:
717 vty_config_unlock (vty
);
718 vty
->node
= ENABLE_NODE
;
721 /* Unknown node, we have to ignore it. */
729 /* Delete a charcter at the current point. */
731 vty_delete_char (struct vty
*vty
)
736 if (vty
->length
== 0)
738 vty_down_level (vty
);
742 if (vty
->cp
== vty
->length
)
743 return; /* completion need here? */
745 size
= vty
->length
- vty
->cp
;
748 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
749 vty
->buf
[vty
->length
] = '\0';
751 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
754 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
755 vty_write (vty
, &telnet_space_char
, 1);
757 for (i
= 0; i
< size
; i
++)
758 vty_write (vty
, &telnet_backward_char
, 1);
761 /* Delete a character before the point. */
763 vty_delete_backward_char (struct vty
*vty
)
768 vty_backward_char (vty
);
769 vty_delete_char (vty
);
772 /* Kill rest of line from current point. */
774 vty_kill_line (struct vty
*vty
)
779 size
= vty
->length
- vty
->cp
;
784 for (i
= 0; i
< size
; i
++)
785 vty_write (vty
, &telnet_space_char
, 1);
786 for (i
= 0; i
< size
; i
++)
787 vty_write (vty
, &telnet_backward_char
, 1);
789 memset (&vty
->buf
[vty
->cp
], 0, size
);
790 vty
->length
= vty
->cp
;
793 /* Kill line from the beginning. */
795 vty_kill_line_from_beginning (struct vty
*vty
)
797 vty_beginning_of_line (vty
);
801 /* Delete a word before the point. */
803 vty_forward_kill_word (struct vty
*vty
)
805 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
806 vty_delete_char (vty
);
807 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
808 vty_delete_char (vty
);
811 /* Delete a word before the point. */
813 vty_backward_kill_word (struct vty
*vty
)
815 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
816 vty_delete_backward_char (vty
);
817 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
818 vty_delete_backward_char (vty
);
821 /* Transpose chars before or at the point. */
823 vty_transpose_chars (struct vty
*vty
)
827 /* If length is short or point is near by the beginning of line then
829 if (vty
->length
< 2 || vty
->cp
< 1)
832 /* In case of point is located at the end of the line. */
833 if (vty
->cp
== vty
->length
)
835 c1
= vty
->buf
[vty
->cp
- 1];
836 c2
= vty
->buf
[vty
->cp
- 2];
838 vty_backward_char (vty
);
839 vty_backward_char (vty
);
840 vty_self_insert_overwrite (vty
, c1
);
841 vty_self_insert_overwrite (vty
, c2
);
845 c1
= vty
->buf
[vty
->cp
];
846 c2
= vty
->buf
[vty
->cp
- 1];
848 vty_backward_char (vty
);
849 vty_self_insert_overwrite (vty
, c1
);
850 vty_self_insert_overwrite (vty
, c2
);
854 /* Do completion at vty interface. */
856 vty_complete_command (struct vty
*vty
)
860 char **matched
= NULL
;
863 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
866 vline
= cmd_make_strvec (vty
->buf
);
870 /* In case of 'help \t'. */
871 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
872 vector_set (vline
, '\0');
874 matched
= cmd_complete_command (vline
, vty
, &ret
);
876 cmd_free_strvec (vline
);
878 vty_out (vty
, "%s", VTY_NEWLINE
);
881 case CMD_ERR_AMBIGUOUS
:
882 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
884 vty_redraw_line (vty
);
886 case CMD_ERR_NO_MATCH
:
887 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
889 vty_redraw_line (vty
);
891 case CMD_COMPLETE_FULL_MATCH
:
893 vty_redraw_line (vty
);
894 vty_backward_pure_word (vty
);
895 vty_insert_word_overwrite (vty
, matched
[0]);
896 vty_self_insert (vty
, ' ');
897 XFREE (MTYPE_TMP
, matched
[0]);
899 case CMD_COMPLETE_MATCH
:
901 vty_redraw_line (vty
);
902 vty_backward_pure_word (vty
);
903 vty_insert_word_overwrite (vty
, matched
[0]);
904 XFREE (MTYPE_TMP
, matched
[0]);
905 vector_only_index_free (matched
);
908 case CMD_COMPLETE_LIST_MATCH
:
909 for (i
= 0; matched
[i
] != NULL
; i
++)
911 if (i
!= 0 && ((i
% 6) == 0))
912 vty_out (vty
, "%s", VTY_NEWLINE
);
913 vty_out (vty
, "%-10s ", matched
[i
]);
914 XFREE (MTYPE_TMP
, matched
[i
]);
916 vty_out (vty
, "%s", VTY_NEWLINE
);
919 vty_redraw_line (vty
);
921 case CMD_ERR_NOTHING_TODO
:
923 vty_redraw_line (vty
);
929 vector_only_index_free (matched
);
933 vty_describe_fold (struct vty
*vty
, int cmd_width
,
934 unsigned int desc_width
, struct cmd_token
*token
)
940 cmd
= token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
;
944 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
948 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
950 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
952 for (pos
= desc_width
; pos
> 0; pos
--)
953 if (*(p
+ pos
) == ' ')
959 strncpy (buf
, p
, pos
);
961 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
966 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
968 XFREE (MTYPE_TMP
, buf
);
971 /* Describe matched command function. */
973 vty_describe_command (struct vty
*vty
)
978 unsigned int i
, width
, desc_width
;
979 struct cmd_token
*token
, *token_cr
= NULL
;
981 vline
= cmd_make_strvec (vty
->buf
);
983 /* In case of '> ?'. */
986 vline
= vector_init (1);
987 vector_set (vline
, '\0');
990 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
991 vector_set (vline
, '\0');
993 describe
= cmd_describe_command (vline
, vty
, &ret
);
995 vty_out (vty
, "%s", VTY_NEWLINE
);
997 /* Ambiguous error. */
1000 case CMD_ERR_AMBIGUOUS
:
1001 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1004 case CMD_ERR_NO_MATCH
:
1005 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1010 /* Get width of command string. */
1012 for (i
= 0; i
< vector_active (describe
); i
++)
1013 if ((token
= vector_slot (describe
, i
)) != NULL
)
1017 if (token
->cmd
[0] == '\0')
1020 len
= strlen (token
->cmd
);
1021 if (token
->cmd
[0] == '.')
1028 /* Get width of description string. */
1029 desc_width
= vty
->width
- (width
+ 6);
1031 /* Print out description. */
1032 for (i
= 0; i
< vector_active (describe
); i
++)
1033 if ((token
= vector_slot (describe
, i
)) != NULL
)
1035 if (token
->cmd
[0] == '\0')
1038 if (strcmp (token
->cmd
, command_cr
) == 0)
1045 vty_out (vty
, " %-s%s",
1046 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1048 else if (desc_width
>= strlen (token
->desc
))
1049 vty_out (vty
, " %-*s %s%s", width
,
1050 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1051 token
->desc
, VTY_NEWLINE
);
1053 vty_describe_fold (vty
, width
, desc_width
, token
);
1056 vty_out (vty
, " %-*s %s%s", width
1057 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1058 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1062 if ((token
= token_cr
))
1065 vty_out (vty
, " %-s%s",
1066 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1068 else if (desc_width
>= strlen (token
->desc
))
1069 vty_out (vty
, " %-*s %s%s", width
,
1070 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1071 token
->desc
, VTY_NEWLINE
);
1073 vty_describe_fold (vty
, width
, desc_width
, token
);
1077 cmd_free_strvec (vline
);
1079 vector_free (describe
);
1082 vty_redraw_line (vty
);
1086 vty_clear_buf (struct vty
*vty
)
1088 memset (vty
->buf
, 0, vty
->max
);
1091 /* ^C stop current input and do not add command line to the history. */
1093 vty_stop_input (struct vty
*vty
)
1095 vty
->cp
= vty
->length
= 0;
1096 vty_clear_buf (vty
);
1097 vty_out (vty
, "%s", VTY_NEWLINE
);
1103 case RESTRICTED_NODE
:
1104 /* Nothing to do. */
1107 case INTERFACE_NODE
:
1118 case KEYCHAIN_KEY_NODE
:
1121 vty_config_unlock (vty
);
1122 vty
->node
= ENABLE_NODE
;
1125 /* Unknown node, we have to ignore it. */
1130 /* Set history pointer to the latest one. */
1131 vty
->hp
= vty
->hindex
;
1134 /* Add current command line to the history buffer. */
1136 vty_hist_add (struct vty
*vty
)
1140 if (vty
->length
== 0)
1143 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1145 /* Ignore the same string as previous one. */
1146 if (vty
->hist
[index
])
1147 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1149 vty
->hp
= vty
->hindex
;
1153 /* Insert history entry. */
1154 if (vty
->hist
[vty
->hindex
])
1155 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1156 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1158 /* History index rotation. */
1160 if (vty
->hindex
== VTY_MAXHIST
)
1163 vty
->hp
= vty
->hindex
;
1166 /* #define TELNET_OPTION_DEBUG */
1168 /* Get telnet window size. */
1170 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1172 #ifdef TELNET_OPTION_DEBUG
1175 for (i
= 0; i
< nbytes
; i
++)
1180 vty_out (vty
, "IAC ");
1183 vty_out (vty
, "WILL ");
1186 vty_out (vty
, "WONT ");
1189 vty_out (vty
, "DO ");
1192 vty_out (vty
, "DONT ");
1195 vty_out (vty
, "SB ");
1198 vty_out (vty
, "SE ");
1201 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1204 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1207 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1210 vty_out (vty
, "%x ", buf
[i
]);
1214 vty_out (vty
, "%s", VTY_NEWLINE
);
1216 #endif /* TELNET_OPTION_DEBUG */
1222 vty
->iac_sb_in_progress
= 1;
1227 if (!vty
->iac_sb_in_progress
)
1230 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1232 vty
->iac_sb_in_progress
= 0;
1235 switch (vty
->sb_buf
[0])
1238 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1239 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1240 "should send %d characters, but we received %lu",
1241 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1242 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1243 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1244 "too small to handle the telnet NAWS option",
1245 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1248 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1249 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1250 #ifdef TELNET_OPTION_DEBUG
1251 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1252 "width %d, height %d%s",
1253 vty
->width
, vty
->height
, VTY_NEWLINE
);
1258 vty
->iac_sb_in_progress
= 0;
1268 /* Execute current command line. */
1270 vty_execute (struct vty
*vty
)
1279 case AUTH_ENABLE_NODE
:
1280 vty_auth (vty
, vty
->buf
);
1283 ret
= vty_command (vty
, vty
->buf
);
1284 if (vty
->type
== VTY_TERM
)
1289 /* Clear command line buffer. */
1290 vty
->cp
= vty
->length
= 0;
1291 vty_clear_buf (vty
);
1293 if (vty
->status
!= VTY_CLOSE
)
1299 #define CONTROL(X) ((X) - '@')
1300 #define VTY_NORMAL 0
1301 #define VTY_PRE_ESCAPE 1
1302 #define VTY_ESCAPE 2
1304 /* Escape character command map. */
1306 vty_escape_map (unsigned char c
, struct vty
*vty
)
1311 vty_previous_line (vty
);
1314 vty_next_line (vty
);
1317 vty_forward_char (vty
);
1320 vty_backward_char (vty
);
1326 /* Go back to normal mode. */
1327 vty
->escape
= VTY_NORMAL
;
1330 /* Quit print out to the buffer. */
1332 vty_buffer_reset (struct vty
*vty
)
1334 buffer_reset (vty
->obuf
);
1336 vty_redraw_line (vty
);
1339 /* Read data via vty socket. */
1341 vty_read (struct thread
*thread
)
1345 unsigned char buf
[VTY_READ_BUFSIZ
];
1347 int vty_sock
= THREAD_FD (thread
);
1348 struct vty
*vty
= THREAD_ARG (thread
);
1351 /* Read raw data from socket */
1352 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1356 if (ERRNO_IO_RETRY(errno
))
1358 vty_event (VTY_READ
, vty_sock
, vty
);
1361 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1362 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1363 __func__
, vty
->fd
, safe_strerror(errno
));
1365 buffer_reset(vty
->obuf
);
1366 vty
->status
= VTY_CLOSE
;
1369 for (i
= 0; i
< nbytes
; i
++)
1384 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1386 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1387 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1394 /* In case of telnet command */
1396 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1403 if (vty
->status
== VTY_MORE
)
1410 vty_buffer_reset (vty
);
1412 #if 0 /* More line does not work for "show ip bgp". */
1415 vty
->status
= VTY_MORELINE
;
1424 /* Escape character. */
1425 if (vty
->escape
== VTY_ESCAPE
)
1427 vty_escape_map (buf
[i
], vty
);
1431 /* Pre-escape status. */
1432 if (vty
->escape
== VTY_PRE_ESCAPE
)
1437 vty
->escape
= VTY_ESCAPE
;
1440 vty_backward_word (vty
);
1441 vty
->escape
= VTY_NORMAL
;
1444 vty_forward_word (vty
);
1445 vty
->escape
= VTY_NORMAL
;
1448 vty_forward_kill_word (vty
);
1449 vty
->escape
= VTY_NORMAL
;
1453 vty_backward_kill_word (vty
);
1454 vty
->escape
= VTY_NORMAL
;
1457 vty
->escape
= VTY_NORMAL
;
1466 vty_beginning_of_line (vty
);
1469 vty_backward_char (vty
);
1472 vty_stop_input (vty
);
1475 vty_delete_char (vty
);
1478 vty_end_of_line (vty
);
1481 vty_forward_char (vty
);
1485 vty_delete_backward_char (vty
);
1488 vty_kill_line (vty
);
1491 vty_next_line (vty
);
1494 vty_previous_line (vty
);
1497 vty_transpose_chars (vty
);
1500 vty_kill_line_from_beginning (vty
);
1503 vty_backward_kill_word (vty
);
1506 vty_end_config (vty
);
1510 vty_out (vty
, "%s", VTY_NEWLINE
);
1514 vty_complete_command (vty
);
1517 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1518 vty_self_insert (vty
, buf
[i
]);
1520 vty_describe_command (vty
);
1523 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1525 vty
->escape
= VTY_ESCAPE
;
1529 vty
->escape
= VTY_PRE_ESCAPE
;
1532 if (buf
[i
] > 31 && buf
[i
] < 127)
1533 vty_self_insert (vty
, buf
[i
]);
1539 if (vty
->status
== VTY_CLOSE
)
1543 vty_event (VTY_WRITE
, vty_sock
, vty
);
1544 vty_event (VTY_READ
, vty_sock
, vty
);
1549 /* Flush buffer to the vty. */
1551 vty_flush (struct thread
*thread
)
1554 buffer_status_t flushrc
;
1555 int vty_sock
= THREAD_FD (thread
);
1556 struct vty
*vty
= THREAD_ARG (thread
);
1558 vty
->t_write
= NULL
;
1560 /* Tempolary disable read thread. */
1561 if ((vty
->lines
== 0) && vty
->t_read
)
1563 thread_cancel (vty
->t_read
);
1567 /* Function execution continue. */
1568 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1570 /* N.B. if width is 0, that means we don't know the window size. */
1571 if ((vty
->lines
== 0) || (vty
->width
== 0))
1572 flushrc
= buffer_flush_available(vty
->obuf
, vty
->fd
);
1573 else if (vty
->status
== VTY_MORELINE
)
1574 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1577 flushrc
= buffer_flush_window(vty
->obuf
, vty
->fd
, vty
->width
,
1578 vty
->lines
>= 0 ? vty
->lines
:
1584 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1585 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1587 buffer_reset(vty
->obuf
);
1591 if (vty
->status
== VTY_CLOSE
)
1595 vty
->status
= VTY_NORMAL
;
1596 if (vty
->lines
== 0)
1597 vty_event (VTY_READ
, vty_sock
, vty
);
1600 case BUFFER_PENDING
:
1601 /* There is more data waiting to be written. */
1602 vty
->status
= VTY_MORE
;
1603 if (vty
->lines
== 0)
1604 vty_event (VTY_WRITE
, vty_sock
, vty
);
1611 /* Create new vty structure. */
1613 vty_create (int vty_sock
, union sockunion
*su
)
1615 char buf
[SU_ADDRSTRLEN
];
1618 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1620 /* Allocate new vty structure and set up default values. */
1623 vty
->type
= VTY_TERM
;
1624 strcpy (vty
->address
, buf
);
1625 if (no_password_check
)
1627 if (restricted_mode
)
1628 vty
->node
= RESTRICTED_NODE
;
1629 else if (host
.advanced
)
1630 vty
->node
= ENABLE_NODE
;
1632 vty
->node
= VIEW_NODE
;
1635 vty
->node
= AUTH_NODE
;
1638 vty_clear_buf (vty
);
1640 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1643 vector_set_index (vtyvec
, vty_sock
, vty
);
1644 vty
->status
= VTY_NORMAL
;
1645 vty
->v_timeout
= vty_timeout_val
;
1646 if (host
.lines
>= 0)
1647 vty
->lines
= host
.lines
;
1651 vty
->iac_sb_in_progress
= 0;
1654 if (! no_password_check
)
1656 /* Vty is not available if password isn't set. */
1657 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1659 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1660 vty
->status
= VTY_CLOSE
;
1666 /* Say hello to the world. */
1668 if (! no_password_check
)
1669 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1671 /* Setting up terminal. */
1672 vty_will_echo (vty
);
1673 vty_will_suppress_go_ahead (vty
);
1675 vty_dont_linemode (vty
);
1676 vty_do_window_size (vty
);
1677 /* vty_dont_lflow_ahead (vty); */
1681 /* Add read/write thread. */
1682 vty_event (VTY_WRITE
, vty_sock
, vty
);
1683 vty_event (VTY_READ
, vty_sock
, vty
);
1688 /* Accept connection from the network. */
1690 vty_accept (struct thread
*thread
)
1697 struct prefix
*p
= NULL
;
1698 struct access_list
*acl
= NULL
;
1699 char buf
[SU_ADDRSTRLEN
];
1701 accept_sock
= THREAD_FD (thread
);
1703 /* We continue hearing vty socket. */
1704 vty_event (VTY_SERV
, accept_sock
, NULL
);
1706 memset (&su
, 0, sizeof (union sockunion
));
1708 /* We can handle IPv4 or IPv6 socket. */
1709 vty_sock
= sockunion_accept (accept_sock
, &su
);
1712 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1715 set_nonblocking(vty_sock
);
1717 p
= sockunion2hostprefix (&su
);
1719 /* VTY's accesslist apply. */
1720 if (p
->family
== AF_INET
&& vty_accesslist_name
)
1722 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1723 (access_list_apply (acl
, p
) == FILTER_DENY
))
1725 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1726 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1729 /* continue accepting connections */
1730 vty_event (VTY_SERV
, accept_sock
, NULL
);
1739 /* VTY's ipv6 accesslist apply. */
1740 if (p
->family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1742 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1743 (access_list_apply (acl
, p
) == FILTER_DENY
))
1745 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1746 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1749 /* continue accepting connections */
1750 vty_event (VTY_SERV
, accept_sock
, NULL
);
1757 #endif /* HAVE_IPV6 */
1762 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1763 (char *) &on
, sizeof (on
));
1765 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1766 safe_strerror (errno
));
1768 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1769 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1771 vty_create (vty_sock
, &su
);
1776 #if defined(HAVE_IPV6) && !defined(NRL)
1778 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1781 struct addrinfo req
;
1782 struct addrinfo
*ainfo
;
1783 struct addrinfo
*ainfo_save
;
1785 char port_str
[BUFSIZ
];
1787 memset (&req
, 0, sizeof (struct addrinfo
));
1788 req
.ai_flags
= AI_PASSIVE
;
1789 req
.ai_family
= AF_UNSPEC
;
1790 req
.ai_socktype
= SOCK_STREAM
;
1791 sprintf (port_str
, "%d", port
);
1792 port_str
[sizeof (port_str
) - 1] = '\0';
1794 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1798 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1806 if (ainfo
->ai_family
!= AF_INET
1808 && ainfo
->ai_family
!= AF_INET6
1809 #endif /* HAVE_IPV6 */
1813 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1817 sockopt_v6only (ainfo
->ai_family
, sock
);
1818 sockopt_reuseaddr (sock
);
1819 sockopt_reuseport (sock
);
1821 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1824 close (sock
); /* Avoid sd leak. */
1828 ret
= listen (sock
, 3);
1831 close (sock
); /* Avoid sd leak. */
1835 vty_event (VTY_SERV
, sock
, NULL
);
1837 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1839 freeaddrinfo (ainfo_save
);
1841 #else /* HAVE_IPV6 && ! NRL */
1843 /* Make vty server socket. */
1845 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1852 memset (&su
, 0, sizeof (union sockunion
));
1853 su
.sa
.sa_family
= family
;
1858 naddr
=&su
.sin
.sin_addr
;
1862 naddr
=&su
.sin6
.sin6_addr
;
1868 switch(inet_pton(family
,addr
,naddr
))
1871 zlog_err("bad address %s",addr
);
1875 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
1879 /* Make new socket. */
1880 accept_sock
= sockunion_stream_socket (&su
);
1881 if (accept_sock
< 0)
1884 /* This is server, so reuse address. */
1885 sockopt_reuseaddr (accept_sock
);
1886 sockopt_reuseport (accept_sock
);
1888 /* Bind socket to universal address and given port. */
1889 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
1892 zlog_warn("can't bind socket");
1893 close (accept_sock
); /* Avoid sd leak. */
1897 /* Listen socket under queue 3. */
1898 ret
= listen (accept_sock
, 3);
1901 zlog (NULL
, LOG_WARNING
, "can't listen socket");
1902 close (accept_sock
); /* Avoid sd leak. */
1906 /* Add vty server event. */
1907 vty_event (VTY_SERV
, accept_sock
, NULL
);
1909 #endif /* HAVE_IPV6 && ! NRL */
1912 /* For sockaddr_un. */
1915 /* VTY shell UNIX domain socket. */
1917 vty_serv_un (const char *path
)
1921 struct sockaddr_un serv
;
1923 struct zprivs_ids_t ids
;
1925 /* First of all, unlink existing socket */
1929 old_mask
= umask (0007);
1931 /* Make UNIX domain socket. */
1932 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1935 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
1939 /* Make server socket. */
1940 memset (&serv
, 0, sizeof (struct sockaddr_un
));
1941 serv
.sun_family
= AF_UNIX
;
1942 strncpy (serv
.sun_path
, path
, strlen (path
));
1943 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1944 len
= serv
.sun_len
= SUN_LEN(&serv
);
1946 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
1947 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1949 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
1952 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
1953 close (sock
); /* Avoid sd leak. */
1957 ret
= listen (sock
, 5);
1960 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
1961 close (sock
); /* Avoid sd leak. */
1967 zprivs_get_ids(&ids
);
1969 if (ids
.gid_vty
> 0)
1971 /* set group of socket */
1972 if ( chown (path
, -1, ids
.gid_vty
) )
1974 zlog_err ("vty_serv_un: could chown socket, %s",
1975 safe_strerror (errno
) );
1979 vty_event (VTYSH_SERV
, sock
, NULL
);
1982 /* #define VTYSH_DEBUG 1 */
1985 vtysh_accept (struct thread
*thread
)
1990 struct sockaddr_un client
;
1993 accept_sock
= THREAD_FD (thread
);
1995 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
1997 memset (&client
, 0, sizeof (struct sockaddr_un
));
1998 client_len
= sizeof (struct sockaddr_un
);
2000 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
2001 (socklen_t
*) &client_len
);
2005 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
2009 if (set_nonblocking(sock
) < 0)
2011 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2012 " %s, closing", sock
, safe_strerror (errno
));
2018 printf ("VTY shell accept\n");
2019 #endif /* VTYSH_DEBUG */
2023 vty
->type
= VTY_SHELL_SERV
;
2024 vty
->node
= VIEW_NODE
;
2026 vty_event (VTYSH_READ
, sock
, vty
);
2032 vtysh_flush(struct vty
*vty
)
2034 switch (buffer_flush_available(vty
->obuf
, vty
->fd
))
2036 case BUFFER_PENDING
:
2037 vty_event(VTYSH_WRITE
, vty
->fd
, vty
);
2040 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2041 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2042 buffer_reset(vty
->obuf
);
2053 vtysh_read (struct thread
*thread
)
2059 unsigned char buf
[VTY_READ_BUFSIZ
];
2061 u_char header
[4] = {0, 0, 0, 0};
2063 sock
= THREAD_FD (thread
);
2064 vty
= THREAD_ARG (thread
);
2067 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2071 if (ERRNO_IO_RETRY(errno
))
2073 vty_event (VTYSH_READ
, sock
, vty
);
2076 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2077 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2078 __func__
, sock
, safe_strerror(errno
));
2080 buffer_reset(vty
->obuf
);
2083 printf ("close vtysh\n");
2084 #endif /* VTYSH_DEBUG */
2089 printf ("line: %.*s\n", nbytes
, buf
);
2090 #endif /* VTYSH_DEBUG */
2092 for (p
= buf
; p
< buf
+nbytes
; p
++)
2094 vty_ensure(vty
, vty
->length
+1);
2095 vty
->buf
[vty
->length
++] = *p
;
2098 /* Pass this line to parser. */
2099 ret
= vty_execute (vty
);
2100 /* Note that vty_execute clears the command buffer and resets
2101 vty->length to 0. */
2103 /* Return result. */
2105 printf ("result: %d\n", ret
);
2106 printf ("vtysh node: %d\n", vty
->node
);
2107 #endif /* VTYSH_DEBUG */
2110 buffer_put(vty
->obuf
, header
, 4);
2112 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2113 /* Try to flush results; exit if a write error occurs. */
2118 vty_event (VTYSH_READ
, sock
, vty
);
2124 vtysh_write (struct thread
*thread
)
2126 struct vty
*vty
= THREAD_ARG (thread
);
2128 vty
->t_write
= NULL
;
2135 /* Determine address family to bind. */
2137 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2139 /* If port is set to 0, do not listen on TCP/IP at all! */
2145 vty_serv_sock_family (addr
, port
, AF_INET
);
2146 vty_serv_sock_family (addr
, port
, AF_INET6
);
2148 vty_serv_sock_addrinfo (addr
, port
);
2150 #else /* ! HAVE_IPV6 */
2151 vty_serv_sock_family (addr
,port
, AF_INET
);
2152 #endif /* HAVE_IPV6 */
2160 /* Close vty interface. Warning: call this only from functions that
2161 will be careful not to access the vty afterwards (since it has
2162 now been freed). This is safest from top-level functions (called
2163 directly by the thread dispatcher). */
2165 vty_close (struct vty
*vty
)
2169 /* Cancel threads.*/
2171 thread_cancel (vty
->t_read
);
2173 thread_cancel (vty
->t_write
);
2175 thread_cancel (vty
->t_timeout
);
2178 buffer_flush_all (vty
->obuf
, vty
->fd
);
2180 /* Free input buffer. */
2181 buffer_free (vty
->obuf
);
2183 /* Free command history. */
2184 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2186 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2189 vector_unset (vtyvec
, vty
->fd
);
2196 XFREE (MTYPE_VTY
, vty
->buf
);
2198 /* Check configure. */
2199 vty_config_unlock (vty
);
2202 XFREE (MTYPE_VTY
, vty
);
2205 /* When time out occur output message then close connection. */
2207 vty_timeout (struct thread
*thread
)
2211 vty
= THREAD_ARG (thread
);
2212 vty
->t_timeout
= NULL
;
2216 buffer_reset (vty
->obuf
);
2217 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2219 /* Close connection. */
2220 vty
->status
= VTY_CLOSE
;
2226 /* Read up configuration file from file_name. */
2228 vty_read_file (FILE *confp
)
2234 vty
->fd
= 0; /* stdout */
2235 vty
->type
= VTY_TERM
;
2236 vty
->node
= CONFIG_NODE
;
2238 /* Execute configuration file */
2239 ret
= config_from_file (vty
, confp
);
2241 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2245 case CMD_ERR_AMBIGUOUS
:
2246 fprintf (stderr
, "Ambiguous command.\n");
2248 case CMD_ERR_NO_MATCH
:
2249 fprintf (stderr
, "There is no such command.\n");
2252 fprintf (stderr
, "Error occured during reading below line.\n%s\n",
2262 vty_use_backup_config (char *fullpath
)
2264 char *fullpath_sav
, *fullpath_tmp
;
2271 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2272 strcpy (fullpath_sav
, fullpath
);
2273 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2274 if (stat (fullpath_sav
, &buf
) == -1)
2276 free (fullpath_sav
);
2280 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2281 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2283 /* Open file to configuration write. */
2284 tmp
= mkstemp (fullpath_tmp
);
2287 free (fullpath_sav
);
2288 free (fullpath_tmp
);
2292 sav
= open (fullpath_sav
, O_RDONLY
);
2295 unlink (fullpath_tmp
);
2296 free (fullpath_sav
);
2297 free (fullpath_tmp
);
2301 while((c
= read (sav
, buffer
, 512)) > 0)
2302 write (tmp
, buffer
, c
);
2307 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2309 unlink (fullpath_tmp
);
2310 free (fullpath_sav
);
2311 free (fullpath_tmp
);
2315 if (link (fullpath_tmp
, fullpath
) == 0)
2316 ret
= fopen (fullpath
, "r");
2318 unlink (fullpath_tmp
);
2320 free (fullpath_sav
);
2321 free (fullpath_tmp
);
2325 /* Read up configuration file from file_name. */
2327 vty_read_config (char *config_file
,
2328 char *config_default_dir
)
2330 char cwd
[MAXPATHLEN
];
2335 /* If -f flag specified. */
2336 if (config_file
!= NULL
)
2338 if (! IS_DIRECTORY_SEP (config_file
[0]))
2340 getcwd (cwd
, MAXPATHLEN
);
2341 tmp
= XMALLOC (MTYPE_TMP
,
2342 strlen (cwd
) + strlen (config_file
) + 2);
2343 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2347 fullpath
= config_file
;
2349 confp
= fopen (fullpath
, "r");
2353 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2354 __func__
, fullpath
, safe_strerror (errno
));
2356 confp
= vty_use_backup_config (fullpath
);
2358 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2361 fprintf (stderr
, "can't open configuration file [%s]\n",
2370 host_config_set (config_default_dir
);
2374 struct stat conf_stat
;
2376 /* !!!!PLEASE LEAVE!!!!
2377 * This is NEEDED for use with vtysh -b, or else you can get
2378 * a real configuration food fight with a lot garbage in the
2379 * merged configuration file it creates coming from the per
2380 * daemon configuration files. This also allows the daemons
2381 * to start if there default configuration file is not
2382 * present or ignore them, as needed when using vtysh -b to
2383 * configure the daemons at boot - MAG
2386 /* Stat for vtysh Zebra.conf, if found startup and wait for
2387 * boot configuration
2390 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2392 ret
= stat (integrate_default
, &conf_stat
);
2397 confp
= fopen (config_default_dir
, "r");
2400 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2401 __func__
, config_default_dir
, safe_strerror (errno
));
2403 confp
= vty_use_backup_config (config_default_dir
);
2406 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2407 fullpath
= config_default_dir
;
2411 fprintf (stderr
, "can't open configuration file [%s]\n",
2412 config_default_dir
);
2417 fullpath
= config_default_dir
;
2420 vty_read_file (confp
);
2424 host_config_set (fullpath
);
2427 XFREE (MTYPE_TMP
, fullpath
);
2430 /* Small utility function which output log to the VTY. */
2432 vty_log (const char *level
, const char *proto_str
,
2433 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2441 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2442 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2447 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2452 /* Async-signal-safe version of vty_log for fixed strings. */
2454 vty_log_fixed (char *buf
, size_t len
)
2457 struct iovec iov
[2];
2458 char crlf
[4] = "\r\n";
2460 /* vty may not have been initialised */
2464 iov
[0].iov_base
= (void *)buf
;
2465 iov
[0].iov_len
= len
;
2466 iov
[1].iov_base
= crlf
;
2469 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2472 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2473 /* N.B. We don't care about the return code, since process is
2474 most likely just about to die anyway. */
2475 writev(vty
->fd
, iov
, 2);
2480 vty_config_lock (struct vty
*vty
)
2482 if (vty_config
== 0)
2491 vty_config_unlock (struct vty
*vty
)
2493 if (vty_config
== 1 && vty
->config
== 1)
2501 /* Master of the threads. */
2502 static struct thread_master
*master
;
2505 vty_event (enum event event
, int sock
, struct vty
*vty
)
2507 struct thread
*vty_serv_thread
;
2512 vty_serv_thread
= thread_add_read (master
, vty_accept
, vty
, sock
);
2513 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2517 vty_serv_thread
= thread_add_read (master
, vtysh_accept
, vty
, sock
);
2518 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2521 vty
->t_read
= thread_add_read (master
, vtysh_read
, vty
, sock
);
2524 vty
->t_write
= thread_add_write (master
, vtysh_write
, vty
, sock
);
2528 vty
->t_read
= thread_add_read (master
, vty_read
, vty
, sock
);
2530 /* Time out treatment. */
2534 thread_cancel (vty
->t_timeout
);
2536 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2541 vty
->t_write
= thread_add_write (master
, vty_flush
, vty
, sock
);
2543 case VTY_TIMEOUT_RESET
:
2546 thread_cancel (vty
->t_timeout
);
2547 vty
->t_timeout
= NULL
;
2552 thread_add_timer (master
, vty_timeout
, vty
, vty
->v_timeout
);
2561 "Display who is on vty\n")
2566 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2567 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2568 vty_out (vty
, "%svty[%d] connected from %s.%s",
2569 v
->config
? "*" : " ",
2570 i
, v
->address
, VTY_NEWLINE
);
2574 /* Move to vty configuration mode. */
2578 "Configure a terminal line\n"
2579 "Virtual terminal\n")
2581 vty
->node
= VTY_NODE
;
2585 /* Set time out value. */
2587 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2589 unsigned long timeout
= 0;
2591 /* min_str and sec_str are already checked by parser. So it must be
2592 all digit string. */
2595 timeout
= strtol (min_str
, NULL
, 10);
2599 timeout
+= strtol (sec_str
, NULL
, 10);
2601 vty_timeout_val
= timeout
;
2602 vty
->v_timeout
= timeout
;
2603 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2609 DEFUN (exec_timeout_min
,
2610 exec_timeout_min_cmd
,
2611 "exec-timeout <0-35791>",
2612 "Set timeout value\n"
2613 "Timeout value in minutes\n")
2615 return exec_timeout (vty
, argv
[0], NULL
);
2618 DEFUN (exec_timeout_sec
,
2619 exec_timeout_sec_cmd
,
2620 "exec-timeout <0-35791> <0-2147483>",
2621 "Set the EXEC timeout\n"
2622 "Timeout in minutes\n"
2623 "Timeout in seconds\n")
2625 return exec_timeout (vty
, argv
[0], argv
[1]);
2628 DEFUN (no_exec_timeout
,
2629 no_exec_timeout_cmd
,
2632 "Set the EXEC timeout\n")
2634 return exec_timeout (vty
, NULL
, NULL
);
2637 /* Set vty access class. */
2638 DEFUN (vty_access_class
,
2639 vty_access_class_cmd
,
2640 "access-class WORD",
2641 "Filter connections based on an IP access list\n"
2644 if (vty_accesslist_name
)
2645 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2647 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2652 /* Clear vty access class. */
2653 DEFUN (no_vty_access_class
,
2654 no_vty_access_class_cmd
,
2655 "no access-class [WORD]",
2657 "Filter connections based on an IP access list\n"
2660 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2662 vty_out (vty
, "Access-class is not currently applied to vty%s",
2667 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2669 vty_accesslist_name
= NULL
;
2675 /* Set vty access class. */
2676 DEFUN (vty_ipv6_access_class
,
2677 vty_ipv6_access_class_cmd
,
2678 "ipv6 access-class WORD",
2680 "Filter connections based on an IP access list\n"
2681 "IPv6 access list\n")
2683 if (vty_ipv6_accesslist_name
)
2684 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2686 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2691 /* Clear vty access class. */
2692 DEFUN (no_vty_ipv6_access_class
,
2693 no_vty_ipv6_access_class_cmd
,
2694 "no ipv6 access-class [WORD]",
2697 "Filter connections based on an IP access list\n"
2698 "IPv6 access list\n")
2700 if (! vty_ipv6_accesslist_name
||
2701 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2703 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2708 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2710 vty_ipv6_accesslist_name
= NULL
;
2714 #endif /* HAVE_IPV6 */
2720 "Enable password checking\n")
2722 no_password_check
= 0;
2726 DEFUN (no_vty_login
,
2730 "Enable password checking\n")
2732 no_password_check
= 1;
2737 DEFUN (vty_restricted_mode
,
2738 vty_restricted_mode_cmd
,
2739 "anonymous restricted",
2740 "Restrict view commands available in anonymous, unauthenticated vty\n")
2742 restricted_mode
= 1;
2746 DEFUN (vty_no_restricted_mode
,
2747 vty_no_restricted_mode_cmd
,
2748 "no anonymous restricted",
2750 "Enable password checking\n")
2752 restricted_mode
= 0;
2756 DEFUN (service_advanced_vty
,
2757 service_advanced_vty_cmd
,
2758 "service advanced-vty",
2759 "Set up miscellaneous service\n"
2760 "Enable advanced mode vty interface\n")
2766 DEFUN (no_service_advanced_vty
,
2767 no_service_advanced_vty_cmd
,
2768 "no service advanced-vty",
2770 "Set up miscellaneous service\n"
2771 "Enable advanced mode vty interface\n")
2777 DEFUN (terminal_monitor
,
2778 terminal_monitor_cmd
,
2780 "Set terminal line parameters\n"
2781 "Copy debug output to the current terminal line\n")
2787 DEFUN (terminal_no_monitor
,
2788 terminal_no_monitor_cmd
,
2789 "terminal no monitor",
2790 "Set terminal line parameters\n"
2792 "Copy debug output to the current terminal line\n")
2798 ALIAS (terminal_no_monitor
,
2799 no_terminal_monitor_cmd
,
2800 "no terminal monitor",
2802 "Set terminal line parameters\n"
2803 "Copy debug output to the current terminal line\n")
2805 DEFUN (show_history
,
2809 "Display the session command history\n")
2813 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2815 if (index
== VTY_MAXHIST
)
2821 if (vty
->hist
[index
] != NULL
)
2822 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2830 /* Display current configuration. */
2832 vty_config_write (struct vty
*vty
)
2834 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
2836 if (vty_accesslist_name
)
2837 vty_out (vty
, " access-class %s%s",
2838 vty_accesslist_name
, VTY_NEWLINE
);
2840 if (vty_ipv6_accesslist_name
)
2841 vty_out (vty
, " ipv6 access-class %s%s",
2842 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
2845 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
2846 vty_out (vty
, " exec-timeout %ld %ld%s",
2847 vty_timeout_val
/ 60,
2848 vty_timeout_val
% 60, VTY_NEWLINE
);
2851 if (no_password_check
)
2852 vty_out (vty
, " no login%s", VTY_NEWLINE
);
2854 if (restricted_mode
!= restricted_mode_default
)
2856 if (restricted_mode_default
)
2857 vty_out (vty
, " no anonymous restricted%s", VTY_NEWLINE
);
2859 vty_out (vty
, " anonymous restricted%s", VTY_NEWLINE
);
2862 vty_out (vty
, "!%s", VTY_NEWLINE
);
2867 struct cmd_node vty_node
=
2870 "%s(config-line)# ",
2874 /* Reset all VTY status. */
2880 struct thread
*vty_serv_thread
;
2882 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2883 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2885 buffer_reset (vty
->obuf
);
2886 vty
->status
= VTY_CLOSE
;
2890 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
2891 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
2893 thread_cancel (vty_serv_thread
);
2894 vector_slot (Vvty_serv_thread
, i
) = NULL
;
2898 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
2900 if (vty_accesslist_name
)
2902 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2903 vty_accesslist_name
= NULL
;
2906 if (vty_ipv6_accesslist_name
)
2908 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2909 vty_ipv6_accesslist_name
= NULL
;
2916 char cwd
[MAXPATHLEN
];
2919 c
= getcwd (cwd
, MAXPATHLEN
);
2924 getcwd (cwd
, MAXPATHLEN
);
2927 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
2928 strcpy (vty_cwd
, cwd
);
2938 vty_shell (struct vty
*vty
)
2940 return vty
->type
== VTY_SHELL
? 1 : 0;
2944 vty_shell_serv (struct vty
*vty
)
2946 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
2952 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2955 /* Install vty's own commands like `who' command. */
2957 vty_init (struct thread_master
*master_thread
)
2959 /* For further configuration read, preserve current directory. */
2962 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
2964 master
= master_thread
;
2966 /* Initilize server thread vector. */
2967 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
2969 /* Install bgp top node. */
2970 install_node (&vty_node
, vty_config_write
);
2972 install_element (RESTRICTED_NODE
, &config_who_cmd
);
2973 install_element (RESTRICTED_NODE
, &show_history_cmd
);
2974 install_element (VIEW_NODE
, &config_who_cmd
);
2975 install_element (VIEW_NODE
, &show_history_cmd
);
2976 install_element (ENABLE_NODE
, &config_who_cmd
);
2977 install_element (CONFIG_NODE
, &line_vty_cmd
);
2978 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
2979 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
2980 install_element (CONFIG_NODE
, &show_history_cmd
);
2981 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
2982 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
2983 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
2984 install_element (ENABLE_NODE
, &show_history_cmd
);
2986 install_default (VTY_NODE
);
2987 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
2988 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
2989 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
2990 install_element (VTY_NODE
, &vty_access_class_cmd
);
2991 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
2992 install_element (VTY_NODE
, &vty_login_cmd
);
2993 install_element (VTY_NODE
, &no_vty_login_cmd
);
2994 install_element (VTY_NODE
, &vty_restricted_mode_cmd
);
2995 install_element (VTY_NODE
, &vty_no_restricted_mode_cmd
);
2997 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
2998 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
2999 #endif /* HAVE_IPV6 */
3003 vty_terminate (void)
3006 XFREE (MTYPE_TMP
, vty_cwd
);
3008 if (vtyvec
&& Vvty_serv_thread
)
3011 vector_free (vtyvec
);
3012 vector_free (Vvty_serv_thread
);