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"
39 #include <arpa/telnet.h>
42 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
43 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
44 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
60 static void vty_event (enum event
, int, struct vty
*);
62 /* Extern host structure from command.c */
63 extern struct host host
;
65 /* Vector which store each vty structure. */
68 /* Vty timeout value. */
69 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
71 /* Vty access-class command */
72 static char *vty_accesslist_name
= NULL
;
74 /* Vty access-calss for IPv6. */
75 static char *vty_ipv6_accesslist_name
= NULL
;
77 /* VTY server thread. */
78 static vector Vvty_serv_thread
;
80 /* Current directory. */
84 static int vty_config
;
85 static int vty_config_is_lockless
= 0;
87 /* Login password check. */
88 static int no_password_check
= 0;
90 /* Integrated configuration file path */
91 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
93 static int do_log_commands
= 0;
95 /* VTY standard output function. */
97 vty_out (struct vty
*vty
, const char *format
, ...)
107 va_start (args
, format
);
108 vprintf (format
, args
);
113 /* Try to write to initial buffer. */
114 va_start (args
, format
);
115 len
= vsnprintf (buf
, sizeof(buf
), format
, args
);
118 /* Initial buffer is not enough. */
119 if (len
< 0 || len
>= size
)
128 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
132 va_start (args
, format
);
133 len
= vsnprintf (p
, size
, format
, args
);
136 if (len
> -1 && len
< size
)
141 /* When initial buffer is enough to store all output. */
145 /* Pointer p must point out buffer. */
146 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
148 /* If p is not different with buf, it is allocated buffer. */
150 XFREE (MTYPE_VTY_OUT_BUF
, p
);
157 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
158 const char *format
, struct timestamp_control
*ctl
, va_list va
)
164 if (!ctl
->already_rendered
)
166 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
167 ctl
->already_rendered
= 1;
169 if (ctl
->len
+1 >= sizeof(buf
))
171 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
176 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
178 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
179 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
182 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
183 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
189 if (write(vty
->wfd
, buf
, len
) < 0)
191 if (ERRNO_IO_RETRY(errno
))
192 /* Kernel buffer is full, probably too much debugging output, so just
193 drop the data and ignore. */
195 /* Fatal I/O error. */
196 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
197 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
198 __func__
, vty
->fd
, safe_strerror(errno
));
199 buffer_reset(vty
->obuf
);
200 /* cannot call vty_close, because a parent routine may still try
201 to access the vty struct */
202 vty
->status
= VTY_CLOSE
;
203 shutdown(vty
->fd
, SHUT_RDWR
);
209 /* Output current time to the vty. */
211 vty_time_print (struct vty
*vty
, int cr
)
213 char buf
[QUAGGA_TIMESTAMP_LEN
];
215 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
217 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
221 vty_out (vty
, "%s\n", buf
);
223 vty_out (vty
, "%s ", buf
);
228 /* Say hello to vty interface. */
230 vty_hello (struct vty
*vty
)
237 f
= fopen (host
.motdfile
, "r");
240 while (fgets (buf
, sizeof (buf
), f
))
243 /* work backwards to ignore trailling isspace() */
244 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
247 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
252 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
255 vty_out (vty
, "%s", host
.motd
);
258 /* Put out prompt and wait input from user. */
260 vty_prompt (struct vty
*vty
)
262 struct utsname names
;
265 if (vty
->type
== VTY_TERM
)
267 hostname
= host
.name
;
271 hostname
= names
.nodename
;
273 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
277 /* Send WILL TELOPT_ECHO to remote server. */
279 vty_will_echo (struct vty
*vty
)
281 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
282 vty_out (vty
, "%s", cmd
);
285 /* Make suppress Go-Ahead telnet option. */
287 vty_will_suppress_go_ahead (struct vty
*vty
)
289 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
290 vty_out (vty
, "%s", cmd
);
293 /* Make don't use linemode over telnet. */
295 vty_dont_linemode (struct vty
*vty
)
297 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
298 vty_out (vty
, "%s", cmd
);
301 /* Use window size. */
303 vty_do_window_size (struct vty
*vty
)
305 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
306 vty_out (vty
, "%s", cmd
);
309 #if 0 /* Currently not used. */
310 /* Make don't use lflow vty interface. */
312 vty_dont_lflow_ahead (struct vty
*vty
)
314 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
315 vty_out (vty
, "%s", cmd
);
319 /* Allocate new vty struct. */
323 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
325 new->obuf
= buffer_new(0); /* Use default buffer size. */
326 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
327 new->error_buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
328 new->max
= VTY_BUFSIZ
;
333 /* Authentication of vty */
335 vty_auth (struct vty
*vty
, char *buf
)
338 enum node_type next_node
= 0;
340 char *crypt (const char *, const char *);
346 passwd
= host
.password_encrypt
;
348 passwd
= host
.password
;
350 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
352 next_node
= VIEW_NODE
;
354 case AUTH_ENABLE_NODE
:
356 passwd
= host
.enable_encrypt
;
358 passwd
= host
.enable
;
359 next_node
= ENABLE_NODE
;
366 fail
= strcmp (crypt(buf
, passwd
), passwd
);
368 fail
= strcmp (buf
, passwd
);
376 vty
->node
= next_node
; /* Success ! */
383 if (vty
->node
== AUTH_NODE
)
385 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
386 vty
->status
= VTY_CLOSE
;
390 /* AUTH_ENABLE_NODE */
392 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
393 vty
->status
= VTY_CLOSE
;
399 /* Command execution over the vty interface. */
401 vty_command (struct vty
*vty
, char *buf
)
405 const char *protocolname
;
409 * Log non empty command lines
415 /* Skip white spaces. */
416 while (isspace ((int) *cp
) && *cp
!= '\0')
419 if (cp
!= NULL
&& *cp
!= '\0')
422 char vty_str
[VTY_BUFSIZ
];
423 char prompt_str
[VTY_BUFSIZ
];
425 /* format the base vty info */
426 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
428 for (i
= 0; i
< vector_active (vtyvec
); i
++)
429 if (vty
== vector_slot (vtyvec
, i
))
431 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
436 /* format the prompt */
437 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt (vty
->node
), vty_str
);
439 /* now log the command */
440 zlog(NULL
, LOG_ERR
, "%s%s", prompt_str
, buf
);
442 /* Split readline string up into the vector */
443 vline
= cmd_make_strvec (buf
);
448 #ifdef CONSUMED_TIME_CHECK
452 unsigned long realtime
, cputime
;
455 #endif /* CONSUMED_TIME_CHECK */
457 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
459 /* Get the name of the protocol if any */
461 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
463 protocolname
= zlog_proto_names
[ZLOG_NONE
];
465 #ifdef CONSUMED_TIME_CHECK
467 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
469 /* Warn about CPU hog that must be fixed. */
470 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
471 realtime
/1000, cputime
/1000, buf
);
473 #endif /* CONSUMED_TIME_CHECK */
475 if (ret
!= CMD_SUCCESS
)
479 if (vty
->type
== VTY_FILE
)
480 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
482 case CMD_ERR_AMBIGUOUS
:
483 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
485 case CMD_ERR_NO_MATCH
:
486 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
488 case CMD_ERR_INCOMPLETE
:
489 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
492 cmd_free_strvec (vline
);
497 static const char telnet_backward_char
= 0x08;
498 static const char telnet_space_char
= ' ';
500 /* Basic function to write buffer to vty. */
502 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
504 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
507 /* Should we do buffering here ? And make vty_flush (vty) ? */
508 buffer_put (vty
->obuf
, buf
, nbytes
);
511 /* Ensure length of input buffer. Is buffer is short, double it. */
513 vty_ensure (struct vty
*vty
, int length
)
515 if (vty
->max
<= length
)
518 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
519 vty
->error_buf
= XREALLOC (MTYPE_VTY
, vty
->error_buf
, vty
->max
);
523 /* Basic function to insert character into vty. */
525 vty_self_insert (struct vty
*vty
, char c
)
530 vty_ensure (vty
, vty
->length
+ 1);
531 length
= vty
->length
- vty
->cp
;
532 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
533 vty
->buf
[vty
->cp
] = c
;
535 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
536 for (i
= 0; i
< length
; i
++)
537 vty_write (vty
, &telnet_backward_char
, 1);
543 /* Self insert character 'c' in overwrite mode. */
545 vty_self_insert_overwrite (struct vty
*vty
, char c
)
547 vty_ensure (vty
, vty
->length
+ 1);
548 vty
->buf
[vty
->cp
++] = c
;
550 if (vty
->cp
> vty
->length
)
553 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
556 vty_write (vty
, &c
, 1);
559 /* Insert a word into vty interface with overwrite mode. */
561 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
563 int len
= strlen (str
);
564 vty_write (vty
, str
, len
);
565 strcpy (&vty
->buf
[vty
->cp
], str
);
567 vty
->length
= vty
->cp
;
570 /* Forward character. */
572 vty_forward_char (struct vty
*vty
)
574 if (vty
->cp
< vty
->length
)
576 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
581 /* Backward character. */
583 vty_backward_char (struct vty
*vty
)
588 vty_write (vty
, &telnet_backward_char
, 1);
592 /* Move to the beginning of the line. */
594 vty_beginning_of_line (struct vty
*vty
)
597 vty_backward_char (vty
);
600 /* Move to the end of the line. */
602 vty_end_of_line (struct vty
*vty
)
604 while (vty
->cp
< vty
->length
)
605 vty_forward_char (vty
);
608 static void vty_kill_line_from_beginning (struct vty
*);
609 static void vty_redraw_line (struct vty
*);
611 /* Print command line history. This function is called from
612 vty_next_line and vty_previous_line. */
614 vty_history_print (struct vty
*vty
)
618 vty_kill_line_from_beginning (vty
);
620 /* Get previous line from history buffer */
621 length
= strlen (vty
->hist
[vty
->hp
]);
622 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
623 vty
->cp
= vty
->length
= length
;
625 /* Redraw current line */
626 vty_redraw_line (vty
);
629 /* Show next command line history. */
631 vty_next_line (struct vty
*vty
)
635 if (vty
->hp
== vty
->hindex
)
638 /* Try is there history exist or not. */
640 if (try_index
== (VTY_MAXHIST
- 1))
645 /* If there is not history return. */
646 if (vty
->hist
[try_index
] == NULL
)
651 vty_history_print (vty
);
654 /* Show previous command line history. */
656 vty_previous_line (struct vty
*vty
)
662 try_index
= VTY_MAXHIST
- 1;
666 if (vty
->hist
[try_index
] == NULL
)
671 vty_history_print (vty
);
674 /* This function redraw all of the command line character. */
676 vty_redraw_line (struct vty
*vty
)
678 vty_write (vty
, vty
->buf
, vty
->length
);
679 vty
->cp
= vty
->length
;
684 vty_forward_word (struct vty
*vty
)
686 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
687 vty_forward_char (vty
);
689 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
690 vty_forward_char (vty
);
693 /* Backward word without skipping training space. */
695 vty_backward_pure_word (struct vty
*vty
)
697 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
698 vty_backward_char (vty
);
703 vty_backward_word (struct vty
*vty
)
705 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
706 vty_backward_char (vty
);
708 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
709 vty_backward_char (vty
);
712 /* When '^D' is typed at the beginning of the line we move to the down
715 vty_down_level (struct vty
*vty
)
717 vty_out (vty
, "%s", VTY_NEWLINE
);
723 /* When '^Z' is received from vty, move down to the enable mode. */
725 vty_end_config (struct vty
*vty
)
727 vty_out (vty
, "%s", VTY_NEWLINE
);
744 case BGP_ENCAPV6_NODE
:
745 case BGP_VNC_DEFAULTS_NODE
:
746 case BGP_VNC_NVE_GROUP_NODE
:
747 case BGP_VNC_L2_GROUP_NODE
:
758 case LDP_IPV4_IFACE_NODE
:
759 case LDP_IPV6_IFACE_NODE
:
761 case LDP_PSEUDOWIRE_NODE
:
764 case KEYCHAIN_KEY_NODE
:
768 vty_config_unlock (vty
);
769 vty
->node
= ENABLE_NODE
;
772 /* Unknown node, we have to ignore it. */
780 /* Delete a charcter at the current point. */
782 vty_delete_char (struct vty
*vty
)
787 if (vty
->length
== 0)
789 vty_down_level (vty
);
793 if (vty
->cp
== vty
->length
)
794 return; /* completion need here? */
796 size
= vty
->length
- vty
->cp
;
799 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
800 vty
->buf
[vty
->length
] = '\0';
802 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
805 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
806 vty_write (vty
, &telnet_space_char
, 1);
808 for (i
= 0; i
< size
; i
++)
809 vty_write (vty
, &telnet_backward_char
, 1);
812 /* Delete a character before the point. */
814 vty_delete_backward_char (struct vty
*vty
)
819 vty_backward_char (vty
);
820 vty_delete_char (vty
);
823 /* Kill rest of line from current point. */
825 vty_kill_line (struct vty
*vty
)
830 size
= vty
->length
- vty
->cp
;
835 for (i
= 0; i
< size
; i
++)
836 vty_write (vty
, &telnet_space_char
, 1);
837 for (i
= 0; i
< size
; i
++)
838 vty_write (vty
, &telnet_backward_char
, 1);
840 memset (&vty
->buf
[vty
->cp
], 0, size
);
841 vty
->length
= vty
->cp
;
844 /* Kill line from the beginning. */
846 vty_kill_line_from_beginning (struct vty
*vty
)
848 vty_beginning_of_line (vty
);
852 /* Delete a word before the point. */
854 vty_forward_kill_word (struct vty
*vty
)
856 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
857 vty_delete_char (vty
);
858 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
859 vty_delete_char (vty
);
862 /* Delete a word before the point. */
864 vty_backward_kill_word (struct vty
*vty
)
866 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
867 vty_delete_backward_char (vty
);
868 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
869 vty_delete_backward_char (vty
);
872 /* Transpose chars before or at the point. */
874 vty_transpose_chars (struct vty
*vty
)
878 /* If length is short or point is near by the beginning of line then
880 if (vty
->length
< 2 || vty
->cp
< 1)
883 /* In case of point is located at the end of the line. */
884 if (vty
->cp
== vty
->length
)
886 c1
= vty
->buf
[vty
->cp
- 1];
887 c2
= vty
->buf
[vty
->cp
- 2];
889 vty_backward_char (vty
);
890 vty_backward_char (vty
);
891 vty_self_insert_overwrite (vty
, c1
);
892 vty_self_insert_overwrite (vty
, c2
);
896 c1
= vty
->buf
[vty
->cp
];
897 c2
= vty
->buf
[vty
->cp
- 1];
899 vty_backward_char (vty
);
900 vty_self_insert_overwrite (vty
, c1
);
901 vty_self_insert_overwrite (vty
, c2
);
905 /* Do completion at vty interface. */
907 vty_complete_command (struct vty
*vty
)
911 char **matched
= NULL
;
914 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
917 vline
= cmd_make_strvec (vty
->buf
);
921 /* In case of 'help \t'. */
922 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
923 vector_set (vline
, NULL
);
925 matched
= cmd_complete_command (vline
, vty
, &ret
);
927 cmd_free_strvec (vline
);
929 vty_out (vty
, "%s", VTY_NEWLINE
);
932 case CMD_ERR_AMBIGUOUS
:
933 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
935 vty_redraw_line (vty
);
937 case CMD_ERR_NO_MATCH
:
938 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
940 vty_redraw_line (vty
);
942 case CMD_COMPLETE_FULL_MATCH
:
945 /* 2016-11-28 equinox -- need to debug, SEGV here */
946 vty_out (vty
, "%% CLI BUG: FULL_MATCH with NULL str%s", VTY_NEWLINE
);
948 vty_redraw_line (vty
);
952 vty_redraw_line (vty
);
953 vty_backward_pure_word (vty
);
954 vty_insert_word_overwrite (vty
, matched
[0]);
955 vty_self_insert (vty
, ' ');
956 XFREE (MTYPE_TMP
, matched
[0]);
958 case CMD_COMPLETE_MATCH
:
960 vty_redraw_line (vty
);
961 vty_backward_pure_word (vty
);
962 vty_insert_word_overwrite (vty
, matched
[0]);
963 XFREE (MTYPE_TMP
, matched
[0]);
964 vector_only_index_free (matched
);
967 case CMD_COMPLETE_LIST_MATCH
:
968 for (i
= 0; matched
[i
] != NULL
; i
++)
970 if (i
!= 0 && ((i
% 6) == 0))
971 vty_out (vty
, "%s", VTY_NEWLINE
);
972 vty_out (vty
, "%-10s ", matched
[i
]);
973 XFREE (MTYPE_TMP
, matched
[i
]);
975 vty_out (vty
, "%s", VTY_NEWLINE
);
978 vty_redraw_line (vty
);
980 case CMD_ERR_NOTHING_TODO
:
982 vty_redraw_line (vty
);
988 vector_only_index_free (matched
);
992 vty_describe_fold (struct vty
*vty
, int cmd_width
,
993 unsigned int desc_width
, struct cmd_token
*token
)
1001 if (desc_width
<= 0)
1003 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
1007 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
1009 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
1011 for (pos
= desc_width
; pos
> 0; pos
--)
1012 if (*(p
+ pos
) == ' ')
1018 strncpy (buf
, p
, pos
);
1020 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
1025 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
1027 XFREE (MTYPE_TMP
, buf
);
1030 /* Describe matched command function. */
1032 vty_describe_command (struct vty
*vty
)
1037 unsigned int i
, width
, desc_width
;
1038 struct cmd_token
*token
, *token_cr
= NULL
;
1040 vline
= cmd_make_strvec (vty
->buf
);
1042 /* In case of '> ?'. */
1045 vline
= vector_init (1);
1046 vector_set (vline
, NULL
);
1049 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
1050 vector_set (vline
, NULL
);
1052 describe
= cmd_describe_command (vline
, vty
, &ret
);
1054 vty_out (vty
, "%s", VTY_NEWLINE
);
1056 /* Ambiguous error. */
1059 case CMD_ERR_AMBIGUOUS
:
1060 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1063 case CMD_ERR_NO_MATCH
:
1064 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1069 /* Get width of command string. */
1071 for (i
= 0; i
< vector_active (describe
); i
++)
1072 if ((token
= vector_slot (describe
, i
)) != NULL
)
1076 if (token
->text
[0] == '\0')
1079 len
= strlen (token
->text
);
1085 /* Get width of description string. */
1086 desc_width
= vty
->width
- (width
+ 6);
1088 /* Print out description. */
1089 for (i
= 0; i
< vector_active (describe
); i
++)
1090 if ((token
= vector_slot (describe
, i
)) != NULL
)
1092 if (token
->text
[0] == '\0')
1095 if (strcmp (token
->text
, CMD_CR_TEXT
) == 0)
1102 vty_out (vty
, " %-s%s",
1105 else if (desc_width
>= strlen (token
->desc
))
1106 vty_out (vty
, " %-*s %s%s", width
,
1108 token
->desc
, VTY_NEWLINE
);
1110 vty_describe_fold (vty
, width
, desc_width
, token
);
1113 vty_out (vty
, " %-*s %s%s", width
1114 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1115 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1119 if ((token
= token_cr
))
1122 vty_out (vty
, " %-s%s",
1125 else if (desc_width
>= strlen (token
->desc
))
1126 vty_out (vty
, " %-*s %s%s", width
,
1128 token
->desc
, VTY_NEWLINE
);
1130 vty_describe_fold (vty
, width
, desc_width
, token
);
1134 cmd_free_strvec (vline
);
1136 vector_free (describe
);
1139 vty_redraw_line (vty
);
1143 vty_clear_buf (struct vty
*vty
)
1145 memset (vty
->buf
, 0, vty
->max
);
1148 /* ^C stop current input and do not add command line to the history. */
1150 vty_stop_input (struct vty
*vty
)
1152 vty
->cp
= vty
->length
= 0;
1153 vty_clear_buf (vty
);
1154 vty_out (vty
, "%s", VTY_NEWLINE
);
1160 /* Nothing to do. */
1163 case INTERFACE_NODE
:
1174 case LDP_IPV4_IFACE_NODE
:
1175 case LDP_IPV6_IFACE_NODE
:
1176 case LDP_L2VPN_NODE
:
1177 case LDP_PSEUDOWIRE_NODE
:
1180 case KEYCHAIN_KEY_NODE
:
1184 vty_config_unlock (vty
);
1185 vty
->node
= ENABLE_NODE
;
1188 /* Unknown node, we have to ignore it. */
1193 /* Set history pointer to the latest one. */
1194 vty
->hp
= vty
->hindex
;
1197 /* Add current command line to the history buffer. */
1199 vty_hist_add (struct vty
*vty
)
1203 if (vty
->length
== 0)
1206 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1208 /* Ignore the same string as previous one. */
1209 if (vty
->hist
[index
])
1210 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1212 vty
->hp
= vty
->hindex
;
1216 /* Insert history entry. */
1217 if (vty
->hist
[vty
->hindex
])
1218 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1219 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1221 /* History index rotation. */
1223 if (vty
->hindex
== VTY_MAXHIST
)
1226 vty
->hp
= vty
->hindex
;
1229 /* #define TELNET_OPTION_DEBUG */
1231 /* Get telnet window size. */
1233 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1235 #ifdef TELNET_OPTION_DEBUG
1238 for (i
= 0; i
< nbytes
; i
++)
1243 vty_out (vty
, "IAC ");
1246 vty_out (vty
, "WILL ");
1249 vty_out (vty
, "WONT ");
1252 vty_out (vty
, "DO ");
1255 vty_out (vty
, "DONT ");
1258 vty_out (vty
, "SB ");
1261 vty_out (vty
, "SE ");
1264 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1267 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1270 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1273 vty_out (vty
, "%x ", buf
[i
]);
1277 vty_out (vty
, "%s", VTY_NEWLINE
);
1279 #endif /* TELNET_OPTION_DEBUG */
1285 vty
->iac_sb_in_progress
= 1;
1290 if (!vty
->iac_sb_in_progress
)
1293 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1295 vty
->iac_sb_in_progress
= 0;
1298 switch (vty
->sb_buf
[0])
1301 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1302 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1303 "should send %d characters, but we received %lu",
1304 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1305 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1306 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1307 "too small to handle the telnet NAWS option",
1308 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1311 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1312 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1313 #ifdef TELNET_OPTION_DEBUG
1314 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1315 "width %d, height %d%s",
1316 vty
->width
, vty
->height
, VTY_NEWLINE
);
1321 vty
->iac_sb_in_progress
= 0;
1331 /* Execute current command line. */
1333 vty_execute (struct vty
*vty
)
1342 case AUTH_ENABLE_NODE
:
1343 vty_auth (vty
, vty
->buf
);
1346 ret
= vty_command (vty
, vty
->buf
);
1347 if (vty
->type
== VTY_TERM
)
1352 /* Clear command line buffer. */
1353 vty
->cp
= vty
->length
= 0;
1354 vty_clear_buf (vty
);
1356 if (vty
->status
!= VTY_CLOSE
)
1362 #define CONTROL(X) ((X) - '@')
1363 #define VTY_NORMAL 0
1364 #define VTY_PRE_ESCAPE 1
1365 #define VTY_ESCAPE 2
1367 /* Escape character command map. */
1369 vty_escape_map (unsigned char c
, struct vty
*vty
)
1374 vty_previous_line (vty
);
1377 vty_next_line (vty
);
1380 vty_forward_char (vty
);
1383 vty_backward_char (vty
);
1389 /* Go back to normal mode. */
1390 vty
->escape
= VTY_NORMAL
;
1393 /* Quit print out to the buffer. */
1395 vty_buffer_reset (struct vty
*vty
)
1397 buffer_reset (vty
->obuf
);
1399 vty_redraw_line (vty
);
1402 /* Read data via vty socket. */
1404 vty_read (struct thread
*thread
)
1408 unsigned char buf
[VTY_READ_BUFSIZ
];
1410 int vty_sock
= THREAD_FD (thread
);
1411 struct vty
*vty
= THREAD_ARG (thread
);
1414 /* Read raw data from socket */
1415 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1419 if (ERRNO_IO_RETRY(errno
))
1421 vty_event (VTY_READ
, vty_sock
, vty
);
1424 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1425 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1426 __func__
, vty
->fd
, safe_strerror(errno
));
1427 buffer_reset(vty
->obuf
);
1429 vty
->status
= VTY_CLOSE
;
1432 for (i
= 0; i
< nbytes
; i
++)
1447 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1449 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1450 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1457 /* In case of telnet command */
1459 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1466 if (vty
->status
== VTY_MORE
)
1473 vty_buffer_reset (vty
);
1475 #if 0 /* More line does not work for "show ip bgp". */
1478 vty
->status
= VTY_MORELINE
;
1487 /* Escape character. */
1488 if (vty
->escape
== VTY_ESCAPE
)
1490 vty_escape_map (buf
[i
], vty
);
1494 /* Pre-escape status. */
1495 if (vty
->escape
== VTY_PRE_ESCAPE
)
1500 vty
->escape
= VTY_ESCAPE
;
1503 vty_backward_word (vty
);
1504 vty
->escape
= VTY_NORMAL
;
1507 vty_forward_word (vty
);
1508 vty
->escape
= VTY_NORMAL
;
1511 vty_forward_kill_word (vty
);
1512 vty
->escape
= VTY_NORMAL
;
1516 vty_backward_kill_word (vty
);
1517 vty
->escape
= VTY_NORMAL
;
1520 vty
->escape
= VTY_NORMAL
;
1529 vty_beginning_of_line (vty
);
1532 vty_backward_char (vty
);
1535 vty_stop_input (vty
);
1538 vty_delete_char (vty
);
1541 vty_end_of_line (vty
);
1544 vty_forward_char (vty
);
1548 vty_delete_backward_char (vty
);
1551 vty_kill_line (vty
);
1554 vty_next_line (vty
);
1557 vty_previous_line (vty
);
1560 vty_transpose_chars (vty
);
1563 vty_kill_line_from_beginning (vty
);
1566 vty_backward_kill_word (vty
);
1569 vty_end_config (vty
);
1573 vty_out (vty
, "%s", VTY_NEWLINE
);
1577 vty_complete_command (vty
);
1580 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1581 vty_self_insert (vty
, buf
[i
]);
1583 vty_describe_command (vty
);
1586 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1588 vty
->escape
= VTY_ESCAPE
;
1592 vty
->escape
= VTY_PRE_ESCAPE
;
1595 if (buf
[i
] > 31 && buf
[i
] < 127)
1596 vty_self_insert (vty
, buf
[i
]);
1602 if (vty
->status
== VTY_CLOSE
)
1606 vty_event (VTY_WRITE
, vty
->wfd
, vty
);
1607 vty_event (VTY_READ
, vty_sock
, vty
);
1612 /* Flush buffer to the vty. */
1614 vty_flush (struct thread
*thread
)
1617 buffer_status_t flushrc
;
1618 int vty_sock
= THREAD_FD (thread
);
1619 struct vty
*vty
= THREAD_ARG (thread
);
1621 vty
->t_write
= NULL
;
1623 /* Tempolary disable read thread. */
1624 if ((vty
->lines
== 0) && vty
->t_read
)
1626 thread_cancel (vty
->t_read
);
1630 /* Function execution continue. */
1631 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1633 /* N.B. if width is 0, that means we don't know the window size. */
1634 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1635 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1636 else if (vty
->status
== VTY_MORELINE
)
1637 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1640 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1641 vty
->lines
>= 0 ? vty
->lines
:
1647 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1648 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1650 buffer_reset(vty
->obuf
);
1654 if (vty
->status
== VTY_CLOSE
)
1658 vty
->status
= VTY_NORMAL
;
1659 if (vty
->lines
== 0)
1660 vty_event (VTY_READ
, vty_sock
, vty
);
1663 case BUFFER_PENDING
:
1664 /* There is more data waiting to be written. */
1665 vty
->status
= VTY_MORE
;
1666 if (vty
->lines
== 0)
1667 vty_event (VTY_WRITE
, vty_sock
, vty
);
1674 /* allocate and initialise vty */
1676 vty_new_init (int vty_sock
)
1682 vty
->wfd
= vty_sock
;
1683 vty
->type
= VTY_TERM
;
1684 vty
->node
= AUTH_NODE
;
1687 vty_clear_buf (vty
);
1689 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1692 vector_set_index (vtyvec
, vty_sock
, vty
);
1693 vty
->status
= VTY_NORMAL
;
1696 vty
->iac_sb_in_progress
= 0;
1702 /* Create new vty structure. */
1704 vty_create (int vty_sock
, union sockunion
*su
)
1706 char buf
[SU_ADDRSTRLEN
];
1709 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1711 /* Allocate new vty structure and set up default values. */
1712 vty
= vty_new_init (vty_sock
);
1714 /* configurable parameters not part of basic init */
1715 vty
->v_timeout
= vty_timeout_val
;
1716 strcpy (vty
->address
, buf
);
1717 if (no_password_check
)
1720 vty
->node
= ENABLE_NODE
;
1722 vty
->node
= VIEW_NODE
;
1724 if (host
.lines
>= 0)
1725 vty
->lines
= host
.lines
;
1727 if (! no_password_check
)
1729 /* Vty is not available if password isn't set. */
1730 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1732 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1733 vty
->status
= VTY_CLOSE
;
1739 /* Say hello to the world. */
1741 if (! no_password_check
)
1742 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1744 /* Setting up terminal. */
1745 vty_will_echo (vty
);
1746 vty_will_suppress_go_ahead (vty
);
1748 vty_dont_linemode (vty
);
1749 vty_do_window_size (vty
);
1750 /* vty_dont_lflow_ahead (vty); */
1754 /* Add read/write thread. */
1755 vty_event (VTY_WRITE
, vty_sock
, vty
);
1756 vty_event (VTY_READ
, vty_sock
, vty
);
1761 /* create vty for stdio */
1762 static struct termios stdio_orig_termios
;
1763 static struct vty
*stdio_vty
= NULL
;
1764 static void (*stdio_vty_atclose
)(void);
1767 vty_stdio_reset (void)
1771 tcsetattr (0, TCSANOW
, &stdio_orig_termios
);
1774 if (stdio_vty_atclose
)
1775 stdio_vty_atclose ();
1776 stdio_vty_atclose
= NULL
;
1781 vty_stdio (void (*atclose
)())
1784 struct termios termios
;
1786 /* refuse creating two vtys on stdio */
1790 vty
= stdio_vty
= vty_new_init (0);
1791 stdio_vty_atclose
= atclose
;
1794 /* always have stdio vty in a known _unchangeable_ state, don't want config
1795 * to have any effect here to make sure scripting this works as intended */
1796 vty
->node
= ENABLE_NODE
;
1798 strcpy (vty
->address
, "console");
1800 if (!tcgetattr (0, &stdio_orig_termios
))
1802 termios
= stdio_orig_termios
;
1803 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
1804 | INLCR
| IGNCR
| ICRNL
| IXON
);
1805 termios
.c_oflag
&= ~OPOST
;
1806 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
1807 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1808 termios
.c_cflag
|= CS8
;
1809 tcsetattr (0, TCSANOW
, &termios
);
1814 /* Add read/write thread. */
1815 vty_event (VTY_WRITE
, 1, vty
);
1816 vty_event (VTY_READ
, 0, vty
);
1821 /* Accept connection from the network. */
1823 vty_accept (struct thread
*thread
)
1831 struct access_list
*acl
= NULL
;
1832 char buf
[SU_ADDRSTRLEN
];
1834 accept_sock
= THREAD_FD (thread
);
1836 /* We continue hearing vty socket. */
1837 vty_event (VTY_SERV
, accept_sock
, NULL
);
1839 memset (&su
, 0, sizeof (union sockunion
));
1841 /* We can handle IPv4 or IPv6 socket. */
1842 vty_sock
= sockunion_accept (accept_sock
, &su
);
1845 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1848 set_nonblocking(vty_sock
);
1849 set_cloexec(vty_sock
);
1851 sockunion2hostprefix (&su
, &p
);
1853 /* VTY's accesslist apply. */
1854 if (p
.family
== AF_INET
&& vty_accesslist_name
)
1856 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1857 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1859 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1860 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1863 /* continue accepting connections */
1864 vty_event (VTY_SERV
, accept_sock
, NULL
);
1871 /* VTY's ipv6 accesslist apply. */
1872 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1874 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1875 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1877 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1878 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1881 /* continue accepting connections */
1882 vty_event (VTY_SERV
, accept_sock
, NULL
);
1887 #endif /* HAVE_IPV6 */
1890 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1891 (char *) &on
, sizeof (on
));
1893 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1894 safe_strerror (errno
));
1896 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1897 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1899 vty_create (vty_sock
, &su
);
1906 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1909 struct addrinfo req
;
1910 struct addrinfo
*ainfo
;
1911 struct addrinfo
*ainfo_save
;
1913 char port_str
[BUFSIZ
];
1915 memset (&req
, 0, sizeof (struct addrinfo
));
1916 req
.ai_flags
= AI_PASSIVE
;
1917 req
.ai_family
= AF_UNSPEC
;
1918 req
.ai_socktype
= SOCK_STREAM
;
1919 sprintf (port_str
, "%d", port
);
1920 port_str
[sizeof (port_str
) - 1] = '\0';
1922 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1926 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1934 if (ainfo
->ai_family
!= AF_INET
1936 && ainfo
->ai_family
!= AF_INET6
1937 #endif /* HAVE_IPV6 */
1941 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1945 sockopt_v6only (ainfo
->ai_family
, sock
);
1946 sockopt_reuseaddr (sock
);
1947 sockopt_reuseport (sock
);
1950 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1953 close (sock
); /* Avoid sd leak. */
1957 ret
= listen (sock
, 3);
1960 close (sock
); /* Avoid sd leak. */
1964 vty_event (VTY_SERV
, sock
, NULL
);
1966 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1968 freeaddrinfo (ainfo_save
);
1970 #else /* HAVE_IPV6 */
1972 /* Make vty server socket. */
1974 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1981 memset (&su
, 0, sizeof (union sockunion
));
1982 su
.sa
.sa_family
= family
;
1987 naddr
=&su
.sin
.sin_addr
;
1991 naddr
=&su
.sin6
.sin6_addr
;
1997 switch(inet_pton(family
,addr
,naddr
))
2000 zlog_err("bad address %s",addr
);
2004 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
2008 /* Make new socket. */
2009 accept_sock
= sockunion_stream_socket (&su
);
2010 if (accept_sock
< 0)
2013 /* This is server, so reuse address. */
2014 sockopt_reuseaddr (accept_sock
);
2015 sockopt_reuseport (accept_sock
);
2016 set_cloexec (accept_sock
);
2018 /* Bind socket to universal address and given port. */
2019 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
2022 zlog_warn("can't bind socket");
2023 close (accept_sock
); /* Avoid sd leak. */
2027 /* Listen socket under queue 3. */
2028 ret
= listen (accept_sock
, 3);
2031 zlog (NULL
, LOG_WARNING
, "can't listen socket");
2032 close (accept_sock
); /* Avoid sd leak. */
2036 /* Add vty server event. */
2037 vty_event (VTY_SERV
, accept_sock
, NULL
);
2039 #endif /* HAVE_IPV6 */
2042 /* For sockaddr_un. */
2045 /* VTY shell UNIX domain socket. */
2047 vty_serv_un (const char *path
)
2051 struct sockaddr_un serv
;
2053 struct zprivs_ids_t ids
;
2055 /* First of all, unlink existing socket */
2059 old_mask
= umask (0007);
2061 /* Make UNIX domain socket. */
2062 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2065 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
2069 /* Make server socket. */
2070 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2071 serv
.sun_family
= AF_UNIX
;
2072 strncpy (serv
.sun_path
, path
, strlen (path
));
2073 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2074 len
= serv
.sun_len
= SUN_LEN(&serv
);
2076 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2077 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2081 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2084 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2085 close (sock
); /* Avoid sd leak. */
2089 ret
= listen (sock
, 5);
2092 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
2093 close (sock
); /* Avoid sd leak. */
2099 zprivs_get_ids(&ids
);
2101 if (ids
.gid_vty
> 0)
2103 /* set group of socket */
2104 if ( chown (path
, -1, ids
.gid_vty
) )
2106 zlog_err ("vty_serv_un: could chown socket, %s",
2107 safe_strerror (errno
) );
2111 vty_event (VTYSH_SERV
, sock
, NULL
);
2114 /* #define VTYSH_DEBUG 1 */
2117 vtysh_accept (struct thread
*thread
)
2122 struct sockaddr_un client
;
2125 accept_sock
= THREAD_FD (thread
);
2127 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
2129 memset (&client
, 0, sizeof (struct sockaddr_un
));
2130 client_len
= sizeof (struct sockaddr_un
);
2132 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
2133 (socklen_t
*) &client_len
);
2137 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
2141 if (set_nonblocking(sock
) < 0)
2143 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2144 " %s, closing", sock
, safe_strerror (errno
));
2151 printf ("VTY shell accept\n");
2152 #endif /* VTYSH_DEBUG */
2157 vty
->type
= VTY_SHELL_SERV
;
2158 vty
->node
= VIEW_NODE
;
2160 vty_event (VTYSH_READ
, sock
, vty
);
2166 vtysh_flush(struct vty
*vty
)
2168 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
))
2170 case BUFFER_PENDING
:
2171 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2174 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2175 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2176 buffer_reset(vty
->obuf
);
2187 vtysh_read (struct thread
*thread
)
2193 unsigned char buf
[VTY_READ_BUFSIZ
];
2195 u_char header
[4] = {0, 0, 0, 0};
2197 sock
= THREAD_FD (thread
);
2198 vty
= THREAD_ARG (thread
);
2201 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2205 if (ERRNO_IO_RETRY(errno
))
2207 vty_event (VTYSH_READ
, sock
, vty
);
2210 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2211 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2212 __func__
, sock
, safe_strerror(errno
));
2214 buffer_reset(vty
->obuf
);
2217 printf ("close vtysh\n");
2218 #endif /* VTYSH_DEBUG */
2223 printf ("line: %.*s\n", nbytes
, buf
);
2224 #endif /* VTYSH_DEBUG */
2226 for (p
= buf
; p
< buf
+nbytes
; p
++)
2228 vty_ensure(vty
, vty
->length
+1);
2229 vty
->buf
[vty
->length
++] = *p
;
2232 /* Pass this line to parser. */
2233 ret
= vty_execute (vty
);
2234 /* Note that vty_execute clears the command buffer and resets
2235 vty->length to 0. */
2237 /* Return result. */
2239 printf ("result: %d\n", ret
);
2240 printf ("vtysh node: %d\n", vty
->node
);
2241 #endif /* VTYSH_DEBUG */
2243 /* hack for asynchronous "write integrated"
2244 * - other commands in "buf" will be ditched
2245 * - input during pending config-write is "unsupported" */
2246 if (ret
== CMD_SUSPEND
)
2249 /* warning: watchquagga hardcodes this result write */
2251 buffer_put(vty
->obuf
, header
, 4);
2253 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2254 /* Try to flush results; exit if a write error occurs. */
2259 vty_event (VTYSH_READ
, sock
, vty
);
2265 vtysh_write (struct thread
*thread
)
2267 struct vty
*vty
= THREAD_ARG (thread
);
2269 vty
->t_write
= NULL
;
2276 /* Determine address family to bind. */
2278 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2280 /* If port is set to 0, do not listen on TCP/IP at all! */
2285 vty_serv_sock_addrinfo (addr
, port
);
2286 #else /* ! HAVE_IPV6 */
2287 vty_serv_sock_family (addr
,port
, AF_INET
);
2288 #endif /* HAVE_IPV6 */
2296 /* Close vty interface. Warning: call this only from functions that
2297 will be careful not to access the vty afterwards (since it has
2298 now been freed). This is safest from top-level functions (called
2299 directly by the thread dispatcher). */
2301 vty_close (struct vty
*vty
)
2305 /* Cancel threads.*/
2307 thread_cancel (vty
->t_read
);
2309 thread_cancel (vty
->t_write
);
2311 thread_cancel (vty
->t_timeout
);
2314 buffer_flush_all (vty
->obuf
, vty
->wfd
);
2316 /* Free input buffer. */
2317 buffer_free (vty
->obuf
);
2319 /* Free command history. */
2320 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2322 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2325 vector_unset (vtyvec
, vty
->fd
);
2334 XFREE (MTYPE_VTY
, vty
->buf
);
2337 XFREE (MTYPE_VTY
, vty
->error_buf
);
2339 /* Check configure. */
2340 vty_config_unlock (vty
);
2343 XFREE (MTYPE_VTY
, vty
);
2346 /* When time out occur output message then close connection. */
2348 vty_timeout (struct thread
*thread
)
2352 vty
= THREAD_ARG (thread
);
2353 vty
->t_timeout
= NULL
;
2357 buffer_reset (vty
->obuf
);
2358 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2360 /* Close connection. */
2361 vty
->status
= VTY_CLOSE
;
2367 /* Read up configuration file from file_name. */
2369 vty_read_file (FILE *confp
)
2373 unsigned int line_num
= 0;
2376 vty
->wfd
= dup(STDERR_FILENO
); /* vty_close() will close this */
2379 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2380 vty
->wfd
= STDOUT_FILENO
;
2382 vty
->fd
= STDIN_FILENO
;
2383 vty
->type
= VTY_FILE
;
2384 vty
->node
= CONFIG_NODE
;
2386 /* Execute configuration file */
2387 ret
= config_from_file (vty
, confp
, &line_num
);
2389 /* Flush any previous errors before printing messages below */
2390 buffer_flush_all (vty
->obuf
, vty
->fd
);
2392 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2394 const char *message
= NULL
;
2397 case CMD_ERR_AMBIGUOUS
:
2398 message
= "*** Error reading config: Ambiguous command.";
2400 case CMD_ERR_NO_MATCH
:
2401 message
= "*** Error reading config: There is no such command.";
2404 fprintf (stderr
, "%s\n", message
);
2405 zlog_err ("%s", message
);
2406 fprintf (stderr
, "*** Error occurred processing line %u, below:\n%s\n",
2407 line_num
, vty
->error_buf
);
2408 zlog_err ("*** Error occurred processing line %u, below:\n%s",
2409 line_num
, vty
->error_buf
);
2416 vty_use_backup_config (char *fullpath
)
2418 char *fullpath_sav
, *fullpath_tmp
;
2425 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2426 strcpy (fullpath_sav
, fullpath
);
2427 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2428 if (stat (fullpath_sav
, &buf
) == -1)
2430 free (fullpath_sav
);
2434 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2435 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2437 /* Open file to configuration write. */
2438 tmp
= mkstemp (fullpath_tmp
);
2441 free (fullpath_sav
);
2442 free (fullpath_tmp
);
2446 sav
= open (fullpath_sav
, O_RDONLY
);
2449 unlink (fullpath_tmp
);
2450 free (fullpath_sav
);
2451 free (fullpath_tmp
);
2455 while((c
= read (sav
, buffer
, 512)) > 0)
2457 if (write (tmp
, buffer
, c
) <= 0)
2459 free (fullpath_sav
);
2460 free (fullpath_tmp
);
2469 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2471 unlink (fullpath_tmp
);
2472 free (fullpath_sav
);
2473 free (fullpath_tmp
);
2477 if (link (fullpath_tmp
, fullpath
) == 0)
2478 ret
= fopen (fullpath
, "r");
2480 unlink (fullpath_tmp
);
2482 free (fullpath_sav
);
2483 free (fullpath_tmp
);
2487 /* Read up configuration file from file_name. */
2489 vty_read_config (char *config_file
,
2490 char *config_default_dir
)
2492 char cwd
[MAXPATHLEN
];
2497 /* If -f flag specified. */
2498 if (config_file
!= NULL
)
2500 if (! IS_DIRECTORY_SEP (config_file
[0]))
2502 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2504 fprintf (stderr
, "Failure to determine Current Working Directory %d!\n", errno
);
2507 tmp
= XMALLOC (MTYPE_TMP
,
2508 strlen (cwd
) + strlen (config_file
) + 2);
2509 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2513 fullpath
= config_file
;
2515 confp
= fopen (fullpath
, "r");
2519 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2520 __func__
, fullpath
, safe_strerror (errno
));
2522 confp
= vty_use_backup_config (fullpath
);
2524 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2527 fprintf (stderr
, "can't open configuration file [%s]\n",
2536 host_config_set (config_default_dir
);
2540 struct stat conf_stat
;
2542 /* !!!!PLEASE LEAVE!!!!
2543 * This is NEEDED for use with vtysh -b, or else you can get
2544 * a real configuration food fight with a lot garbage in the
2545 * merged configuration file it creates coming from the per
2546 * daemon configuration files. This also allows the daemons
2547 * to start if there default configuration file is not
2548 * present or ignore them, as needed when using vtysh -b to
2549 * configure the daemons at boot - MAG
2552 /* Stat for vtysh Zebra.conf, if found startup and wait for
2553 * boot configuration
2556 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2558 ret
= stat (integrate_default
, &conf_stat
);
2560 goto tmp_free_and_out
;
2563 confp
= fopen (config_default_dir
, "r");
2566 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2567 __func__
, config_default_dir
, safe_strerror (errno
));
2569 confp
= vty_use_backup_config (config_default_dir
);
2572 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2573 fullpath
= config_default_dir
;
2577 fprintf (stderr
, "can't open configuration file [%s]\n",
2578 config_default_dir
);
2579 goto tmp_free_and_out
;
2583 fullpath
= config_default_dir
;
2586 vty_read_file (confp
);
2590 host_config_set (fullpath
);
2594 XFREE (MTYPE_TMP
, fullpath
);
2597 /* Small utility function which output log to the VTY. */
2599 vty_log (const char *level
, const char *proto_str
,
2600 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2608 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2609 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2614 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2619 /* Async-signal-safe version of vty_log for fixed strings. */
2621 vty_log_fixed (char *buf
, size_t len
)
2624 struct iovec iov
[2];
2625 char crlf
[4] = "\r\n";
2627 /* vty may not have been initialised */
2631 iov
[0].iov_base
= buf
;
2632 iov
[0].iov_len
= len
;
2633 iov
[1].iov_base
= crlf
;
2636 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2639 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2640 /* N.B. We don't care about the return code, since process is
2641 most likely just about to die anyway. */
2642 if (writev(vty
->wfd
, iov
, 2) == -1)
2644 fprintf(stderr
, "Failure to writev: %d\n", errno
);
2651 vty_config_lock (struct vty
*vty
)
2653 if (vty_config_is_lockless
)
2655 if (vty_config
== 0)
2664 vty_config_unlock (struct vty
*vty
)
2666 if (vty_config_is_lockless
)
2668 if (vty_config
== 1 && vty
->config
== 1)
2677 vty_config_lockless (void)
2679 vty_config_is_lockless
= 1;
2682 /* Master of the threads. */
2683 static struct thread_master
*vty_master
;
2686 vty_event (enum event event
, int sock
, struct vty
*vty
)
2688 struct thread
*vty_serv_thread
;
2693 vty_serv_thread
= thread_add_read (vty_master
, vty_accept
, vty
, sock
);
2694 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2698 vty_serv_thread
= thread_add_read (vty_master
, vtysh_accept
, vty
, sock
);
2699 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2702 vty
->t_read
= thread_add_read (vty_master
, vtysh_read
, vty
, sock
);
2705 vty
->t_write
= thread_add_write (vty_master
, vtysh_write
, vty
, sock
);
2709 vty
->t_read
= thread_add_read (vty_master
, vty_read
, vty
, sock
);
2711 /* Time out treatment. */
2715 thread_cancel (vty
->t_timeout
);
2717 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2722 vty
->t_write
= thread_add_write (vty_master
, vty_flush
, vty
, sock
);
2724 case VTY_TIMEOUT_RESET
:
2727 thread_cancel (vty
->t_timeout
);
2728 vty
->t_timeout
= NULL
;
2733 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2742 "Display who is on vty\n")
2747 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2748 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2749 vty_out (vty
, "%svty[%d] connected from %s.%s",
2750 v
->config
? "*" : " ",
2751 i
, v
->address
, VTY_NEWLINE
);
2755 /* Move to vty configuration mode. */
2759 "Configure a terminal line\n"
2760 "Virtual terminal\n")
2762 vty
->node
= VTY_NODE
;
2766 /* Set time out value. */
2768 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2770 unsigned long timeout
= 0;
2772 /* min_str and sec_str are already checked by parser. So it must be
2773 all digit string. */
2776 timeout
= strtol (min_str
, NULL
, 10);
2780 timeout
+= strtol (sec_str
, NULL
, 10);
2782 vty_timeout_val
= timeout
;
2783 vty
->v_timeout
= timeout
;
2784 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2790 DEFUN (exec_timeout_min
,
2791 exec_timeout_min_cmd
,
2792 "exec-timeout (0-35791)",
2793 "Set timeout value\n"
2794 "Timeout value in minutes\n")
2797 return exec_timeout (vty
, argv
[idx_number
]->arg
, NULL
);
2800 DEFUN (exec_timeout_sec
,
2801 exec_timeout_sec_cmd
,
2802 "exec-timeout (0-35791) (0-2147483)",
2803 "Set the EXEC timeout\n"
2804 "Timeout in minutes\n"
2805 "Timeout in seconds\n")
2808 int idx_number_2
= 2;
2809 return exec_timeout (vty
, argv
[idx_number
]->arg
, argv
[idx_number_2
]->arg
);
2812 DEFUN (no_exec_timeout
,
2813 no_exec_timeout_cmd
,
2816 "Set the EXEC timeout\n")
2818 return exec_timeout (vty
, NULL
, NULL
);
2821 /* Set vty access class. */
2822 DEFUN (vty_access_class
,
2823 vty_access_class_cmd
,
2824 "access-class WORD",
2825 "Filter connections based on an IP access list\n"
2829 if (vty_accesslist_name
)
2830 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2832 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2837 /* Clear vty access class. */
2838 DEFUN (no_vty_access_class
,
2839 no_vty_access_class_cmd
,
2840 "no access-class [WORD]",
2842 "Filter connections based on an IP access list\n"
2846 const char *accesslist
= (argc
== 3) ? argv
[idx_word
]->arg
: NULL
;
2847 if (! vty_accesslist_name
|| (argc
== 3 && strcmp(vty_accesslist_name
, accesslist
)))
2849 vty_out (vty
, "Access-class is not currently applied to vty%s",
2854 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2856 vty_accesslist_name
= NULL
;
2862 /* Set vty access class. */
2863 DEFUN (vty_ipv6_access_class
,
2864 vty_ipv6_access_class_cmd
,
2865 "ipv6 access-class WORD",
2867 "Filter connections based on an IP access list\n"
2868 "IPv6 access list\n")
2871 if (vty_ipv6_accesslist_name
)
2872 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2874 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[idx_word
]->arg
);
2879 /* Clear vty access class. */
2880 DEFUN (no_vty_ipv6_access_class
,
2881 no_vty_ipv6_access_class_cmd
,
2882 "no ipv6 access-class [WORD]",
2885 "Filter connections based on an IP access list\n"
2886 "IPv6 access list\n")
2889 const char *accesslist
= (argc
== 4) ? argv
[idx_word
]->arg
: NULL
;
2891 if (! vty_ipv6_accesslist_name
||
2892 (argc
== 4 && strcmp(vty_ipv6_accesslist_name
, accesslist
)))
2894 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2899 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2901 vty_ipv6_accesslist_name
= NULL
;
2905 #endif /* HAVE_IPV6 */
2911 "Enable password checking\n")
2913 no_password_check
= 0;
2917 DEFUN (no_vty_login
,
2921 "Enable password checking\n")
2923 no_password_check
= 1;
2927 DEFUN (service_advanced_vty
,
2928 service_advanced_vty_cmd
,
2929 "service advanced-vty",
2930 "Set up miscellaneous service\n"
2931 "Enable advanced mode vty interface\n")
2937 DEFUN (no_service_advanced_vty
,
2938 no_service_advanced_vty_cmd
,
2939 "no service advanced-vty",
2941 "Set up miscellaneous service\n"
2942 "Enable advanced mode vty interface\n")
2948 DEFUN (terminal_monitor
,
2949 terminal_monitor_cmd
,
2951 "Set terminal line parameters\n"
2952 "Copy debug output to the current terminal line\n")
2958 DEFUN (terminal_no_monitor
,
2959 terminal_no_monitor_cmd
,
2960 "terminal no monitor",
2961 "Set terminal line parameters\n"
2963 "Copy debug output to the current terminal line\n")
2969 DEFUN (no_terminal_monitor
,
2970 no_terminal_monitor_cmd
,
2971 "no terminal monitor",
2973 "Set terminal line parameters\n"
2974 "Copy debug output to the current terminal line\n")
2976 return terminal_no_monitor (self
, vty
, argc
, argv
);
2980 DEFUN (show_history
,
2984 "Display the session command history\n")
2988 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2990 if (index
== VTY_MAXHIST
)
2996 if (vty
->hist
[index
] != NULL
)
2997 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
3006 DEFUN (log_commands
,
3010 "Log all commands (can't be unset without restart)\n")
3012 do_log_commands
= 1;
3016 /* Display current configuration. */
3018 vty_config_write (struct vty
*vty
)
3020 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
3022 if (vty_accesslist_name
)
3023 vty_out (vty
, " access-class %s%s",
3024 vty_accesslist_name
, VTY_NEWLINE
);
3026 if (vty_ipv6_accesslist_name
)
3027 vty_out (vty
, " ipv6 access-class %s%s",
3028 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
3031 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3032 vty_out (vty
, " exec-timeout %ld %ld%s",
3033 vty_timeout_val
/ 60,
3034 vty_timeout_val
% 60, VTY_NEWLINE
);
3037 if (no_password_check
)
3038 vty_out (vty
, " no login%s", VTY_NEWLINE
);
3040 if (do_log_commands
)
3041 vty_out (vty
, "log commands%s", VTY_NEWLINE
);
3043 vty_out (vty
, "!%s", VTY_NEWLINE
);
3048 struct cmd_node vty_node
=
3051 "%s(config-line)# ",
3055 /* Reset all VTY status. */
3061 struct thread
*vty_serv_thread
;
3063 for (i
= 0; i
< vector_active (vtyvec
); i
++)
3064 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
3066 buffer_reset (vty
->obuf
);
3067 vty
->status
= VTY_CLOSE
;
3071 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
3072 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
3074 thread_cancel (vty_serv_thread
);
3075 vector_slot (Vvty_serv_thread
, i
) = NULL
;
3079 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3081 if (vty_accesslist_name
)
3083 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3084 vty_accesslist_name
= NULL
;
3087 if (vty_ipv6_accesslist_name
)
3089 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3090 vty_ipv6_accesslist_name
= NULL
;
3097 char cwd
[MAXPATHLEN
];
3100 c
= getcwd (cwd
, MAXPATHLEN
);
3105 * At this point if these go wrong, more than likely
3106 * the whole world is coming down around us
3107 * Hence not worrying about it too much.
3109 if (!chdir (SYSCONFDIR
))
3111 fprintf(stderr
, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR
, errno
);
3114 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
3116 fprintf(stderr
, "Failure to getcwd, errno: %d\n", errno
);
3121 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
3122 strcpy (vty_cwd
, cwd
);
3132 vty_shell (struct vty
*vty
)
3134 return vty
->type
== VTY_SHELL
? 1 : 0;
3138 vty_shell_serv (struct vty
*vty
)
3140 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3146 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3149 /* Install vty's own commands like `who' command. */
3151 vty_init (struct thread_master
*master_thread
)
3153 /* For further configuration read, preserve current directory. */
3156 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3158 vty_master
= master_thread
;
3160 atexit (vty_stdio_reset
);
3162 /* Initilize server thread vector. */
3163 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
3165 /* Install bgp top node. */
3166 install_node (&vty_node
, vty_config_write
);
3168 install_element (VIEW_NODE
, &config_who_cmd
);
3169 install_element (VIEW_NODE
, &show_history_cmd
);
3170 install_element (CONFIG_NODE
, &line_vty_cmd
);
3171 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
3172 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3173 install_element (CONFIG_NODE
, &show_history_cmd
);
3174 install_element (CONFIG_NODE
, &log_commands_cmd
);
3175 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
3176 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
3177 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
3179 install_default (VTY_NODE
);
3180 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
3181 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
3182 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
3183 install_element (VTY_NODE
, &vty_access_class_cmd
);
3184 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
3185 install_element (VTY_NODE
, &vty_login_cmd
);
3186 install_element (VTY_NODE
, &no_vty_login_cmd
);
3188 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
3189 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3190 #endif /* HAVE_IPV6 */
3194 vty_terminate (void)
3197 XFREE (MTYPE_TMP
, vty_cwd
);
3199 if (vtyvec
&& Vvty_serv_thread
)
3202 vector_free (vtyvec
);
3203 vector_free (Vvty_serv_thread
);
3207 /* Utility functions to get arguments from commands generated
3208 by the xml2cli.pl script. */
3210 vty_get_arg_value (struct vty_arg
*args
[], const char *arg
)
3214 if (strcmp ((*args
)->name
, arg
) == 0)
3215 return (*args
)->value
;
3222 vty_get_arg (struct vty_arg
*args
[], const char *arg
)
3226 if (strcmp ((*args
)->name
, arg
) == 0)