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 /* Basic function to insert character into vty. */
513 vty_self_insert (struct vty
*vty
, char c
)
518 if (vty
->length
+ 1 >= VTY_BUFSIZ
)
521 length
= vty
->length
- vty
->cp
;
522 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
523 vty
->buf
[vty
->cp
] = c
;
525 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
526 for (i
= 0; i
< length
; i
++)
527 vty_write (vty
, &telnet_backward_char
, 1);
532 vty
->buf
[vty
->length
] = '\0';
535 /* Self insert character 'c' in overwrite mode. */
537 vty_self_insert_overwrite (struct vty
*vty
, char c
)
539 if (vty
->cp
== vty
->length
)
541 vty_self_insert (vty
, c
);
545 vty
->buf
[vty
->cp
++] = c
;
546 vty_write (vty
, &c
, 1);
550 * Insert a string into vty->buf at the current cursor position.
552 * If the resultant string would be larger than VTY_BUFSIZ it is
556 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
558 if (vty
->cp
== VTY_BUFSIZ
)
561 size_t nwrite
= MIN ((int) strlen (str
), VTY_BUFSIZ
- vty
->cp
- 1);
562 memcpy (&vty
->buf
[vty
->cp
], str
, nwrite
);
564 vty
->length
= MAX (vty
->cp
, vty
->length
);
565 vty
->buf
[vty
->length
] = '\0';
566 vty_write (vty
, str
, nwrite
);
569 /* Forward character. */
571 vty_forward_char (struct vty
*vty
)
573 if (vty
->cp
< vty
->length
)
575 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
580 /* Backward character. */
582 vty_backward_char (struct vty
*vty
)
587 vty_write (vty
, &telnet_backward_char
, 1);
591 /* Move to the beginning of the line. */
593 vty_beginning_of_line (struct vty
*vty
)
596 vty_backward_char (vty
);
599 /* Move to the end of the line. */
601 vty_end_of_line (struct vty
*vty
)
603 while (vty
->cp
< vty
->length
)
604 vty_forward_char (vty
);
607 static void vty_kill_line_from_beginning (struct vty
*);
608 static void vty_redraw_line (struct vty
*);
610 /* Print command line history. This function is called from
611 vty_next_line and vty_previous_line. */
613 vty_history_print (struct vty
*vty
)
617 vty_kill_line_from_beginning (vty
);
619 /* Get previous line from history buffer */
620 length
= strlen (vty
->hist
[vty
->hp
]);
621 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
622 vty
->cp
= vty
->length
= length
;
623 vty
->buf
[vty
->length
] = '\0';
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
);
718 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
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_lib (vline
, vty
, &ret
, 1);
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
:
944 vty_redraw_line (vty
);
945 vty_backward_pure_word (vty
);
946 vty_insert_word_overwrite (vty
, matched
[0]);
947 vty_self_insert (vty
, ' ');
948 XFREE (MTYPE_TMP
, matched
[0]);
950 case CMD_COMPLETE_MATCH
:
952 vty_redraw_line (vty
);
953 vty_backward_pure_word (vty
);
954 vty_insert_word_overwrite (vty
, matched
[0]);
955 XFREE (MTYPE_TMP
, matched
[0]);
956 vector_only_index_free (matched
);
959 case CMD_COMPLETE_LIST_MATCH
:
960 for (i
= 0; matched
[i
] != NULL
; i
++)
962 if (i
!= 0 && ((i
% 6) == 0))
963 vty_out (vty
, "%s", VTY_NEWLINE
);
964 vty_out (vty
, "%-10s ", matched
[i
]);
965 XFREE (MTYPE_TMP
, matched
[i
]);
967 vty_out (vty
, "%s", VTY_NEWLINE
);
970 vty_redraw_line (vty
);
972 case CMD_ERR_NOTHING_TODO
:
974 vty_redraw_line (vty
);
980 vector_only_index_free (matched
);
984 vty_describe_fold (struct vty
*vty
, int cmd_width
,
985 unsigned int desc_width
, struct cmd_token
*token
)
991 cmd
= token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
;
995 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
999 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
1001 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
1003 for (pos
= desc_width
; pos
> 0; pos
--)
1004 if (*(p
+ pos
) == ' ')
1010 strncpy (buf
, p
, pos
);
1012 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
1017 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
1019 XFREE (MTYPE_TMP
, buf
);
1022 /* Describe matched command function. */
1024 vty_describe_command (struct vty
*vty
)
1029 unsigned int i
, width
, desc_width
;
1030 struct cmd_token
*token
, *token_cr
= NULL
;
1032 vline
= cmd_make_strvec (vty
->buf
);
1034 /* In case of '> ?'. */
1037 vline
= vector_init (1);
1038 vector_set (vline
, NULL
);
1041 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
1042 vector_set (vline
, NULL
);
1044 describe
= cmd_describe_command (vline
, vty
, &ret
);
1046 vty_out (vty
, "%s", VTY_NEWLINE
);
1048 /* Ambiguous error. */
1051 case CMD_ERR_AMBIGUOUS
:
1052 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1055 case CMD_ERR_NO_MATCH
:
1056 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1061 /* Get width of command string. */
1063 for (i
= 0; i
< vector_active (describe
); i
++)
1064 if ((token
= vector_slot (describe
, i
)) != NULL
)
1068 if (token
->cmd
[0] == '\0')
1071 len
= strlen (token
->cmd
);
1072 if (token
->cmd
[0] == '.')
1079 /* Get width of description string. */
1080 desc_width
= vty
->width
- (width
+ 6);
1082 /* Print out description. */
1083 for (i
= 0; i
< vector_active (describe
); i
++)
1084 if ((token
= vector_slot (describe
, i
)) != NULL
)
1086 if (token
->cmd
[0] == '\0')
1089 if (strcmp (token
->cmd
, command_cr
) == 0)
1096 vty_out (vty
, " %-s%s",
1097 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1099 else if (desc_width
>= strlen (token
->desc
))
1100 vty_out (vty
, " %-*s %s%s", width
,
1101 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1102 token
->desc
, VTY_NEWLINE
);
1104 vty_describe_fold (vty
, width
, desc_width
, token
);
1107 vty_out (vty
, " %-*s %s%s", width
1108 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1109 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1113 if ((token
= token_cr
))
1116 vty_out (vty
, " %-s%s",
1117 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1119 else if (desc_width
>= strlen (token
->desc
))
1120 vty_out (vty
, " %-*s %s%s", width
,
1121 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1122 token
->desc
, VTY_NEWLINE
);
1124 vty_describe_fold (vty
, width
, desc_width
, token
);
1128 cmd_free_strvec (vline
);
1130 vector_free (describe
);
1133 vty_redraw_line (vty
);
1137 vty_clear_buf (struct vty
*vty
)
1139 memset (vty
->buf
, 0, vty
->max
);
1142 /* ^C stop current input and do not add command line to the history. */
1144 vty_stop_input (struct vty
*vty
)
1146 vty
->cp
= vty
->length
= 0;
1147 vty_clear_buf (vty
);
1148 vty_out (vty
, "%s", VTY_NEWLINE
);
1154 /* Nothing to do. */
1157 case INTERFACE_NODE
:
1168 case LDP_IPV4_IFACE_NODE
:
1169 case LDP_IPV6_IFACE_NODE
:
1170 case LDP_L2VPN_NODE
:
1171 case LDP_PSEUDOWIRE_NODE
:
1174 case KEYCHAIN_KEY_NODE
:
1178 vty_config_unlock (vty
);
1179 vty
->node
= ENABLE_NODE
;
1182 /* Unknown node, we have to ignore it. */
1187 /* Set history pointer to the latest one. */
1188 vty
->hp
= vty
->hindex
;
1191 /* Add current command line to the history buffer. */
1193 vty_hist_add (struct vty
*vty
)
1197 if (vty
->length
== 0)
1200 index
= vty
->hindex
? vty
->hindex
- 1 : VTY_MAXHIST
- 1;
1202 /* Ignore the same string as previous one. */
1203 if (vty
->hist
[index
])
1204 if (strcmp (vty
->buf
, vty
->hist
[index
]) == 0)
1206 vty
->hp
= vty
->hindex
;
1210 /* Insert history entry. */
1211 if (vty
->hist
[vty
->hindex
])
1212 XFREE (MTYPE_VTY_HIST
, vty
->hist
[vty
->hindex
]);
1213 vty
->hist
[vty
->hindex
] = XSTRDUP (MTYPE_VTY_HIST
, vty
->buf
);
1215 /* History index rotation. */
1217 if (vty
->hindex
== VTY_MAXHIST
)
1220 vty
->hp
= vty
->hindex
;
1223 /* #define TELNET_OPTION_DEBUG */
1225 /* Get telnet window size. */
1227 vty_telnet_option (struct vty
*vty
, unsigned char *buf
, int nbytes
)
1229 #ifdef TELNET_OPTION_DEBUG
1232 for (i
= 0; i
< nbytes
; i
++)
1237 vty_out (vty
, "IAC ");
1240 vty_out (vty
, "WILL ");
1243 vty_out (vty
, "WONT ");
1246 vty_out (vty
, "DO ");
1249 vty_out (vty
, "DONT ");
1252 vty_out (vty
, "SB ");
1255 vty_out (vty
, "SE ");
1258 vty_out (vty
, "TELOPT_ECHO %s", VTY_NEWLINE
);
1261 vty_out (vty
, "TELOPT_SGA %s", VTY_NEWLINE
);
1264 vty_out (vty
, "TELOPT_NAWS %s", VTY_NEWLINE
);
1267 vty_out (vty
, "%x ", buf
[i
]);
1271 vty_out (vty
, "%s", VTY_NEWLINE
);
1273 #endif /* TELNET_OPTION_DEBUG */
1279 vty
->iac_sb_in_progress
= 1;
1284 if (!vty
->iac_sb_in_progress
)
1287 if ((vty
->sb_len
== 0) || (vty
->sb_buf
[0] == '\0'))
1289 vty
->iac_sb_in_progress
= 0;
1292 switch (vty
->sb_buf
[0])
1295 if (vty
->sb_len
!= TELNET_NAWS_SB_LEN
)
1296 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1297 "should send %d characters, but we received %lu",
1298 TELNET_NAWS_SB_LEN
, (u_long
)vty
->sb_len
);
1299 else if (sizeof(vty
->sb_buf
) < TELNET_NAWS_SB_LEN
)
1300 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1301 "too small to handle the telnet NAWS option",
1302 (u_long
)sizeof(vty
->sb_buf
), TELNET_NAWS_SB_LEN
);
1305 vty
->width
= ((vty
->sb_buf
[1] << 8)|vty
->sb_buf
[2]);
1306 vty
->height
= ((vty
->sb_buf
[3] << 8)|vty
->sb_buf
[4]);
1307 #ifdef TELNET_OPTION_DEBUG
1308 vty_out(vty
, "TELNET NAWS window size negotiation completed: "
1309 "width %d, height %d%s",
1310 vty
->width
, vty
->height
, VTY_NEWLINE
);
1315 vty
->iac_sb_in_progress
= 0;
1325 /* Execute current command line. */
1327 vty_execute (struct vty
*vty
)
1336 case AUTH_ENABLE_NODE
:
1337 vty_auth (vty
, vty
->buf
);
1340 ret
= vty_command (vty
, vty
->buf
);
1341 if (vty
->type
== VTY_TERM
)
1346 /* Clear command line buffer. */
1347 vty
->cp
= vty
->length
= 0;
1348 vty_clear_buf (vty
);
1350 if (vty
->status
!= VTY_CLOSE
)
1356 #define CONTROL(X) ((X) - '@')
1357 #define VTY_NORMAL 0
1358 #define VTY_PRE_ESCAPE 1
1359 #define VTY_ESCAPE 2
1361 /* Escape character command map. */
1363 vty_escape_map (unsigned char c
, struct vty
*vty
)
1368 vty_previous_line (vty
);
1371 vty_next_line (vty
);
1374 vty_forward_char (vty
);
1377 vty_backward_char (vty
);
1383 /* Go back to normal mode. */
1384 vty
->escape
= VTY_NORMAL
;
1387 /* Quit print out to the buffer. */
1389 vty_buffer_reset (struct vty
*vty
)
1391 buffer_reset (vty
->obuf
);
1393 vty_redraw_line (vty
);
1396 /* Read data via vty socket. */
1398 vty_read (struct thread
*thread
)
1402 unsigned char buf
[VTY_READ_BUFSIZ
];
1404 int vty_sock
= THREAD_FD (thread
);
1405 struct vty
*vty
= THREAD_ARG (thread
);
1408 /* Read raw data from socket */
1409 if ((nbytes
= read (vty
->fd
, buf
, VTY_READ_BUFSIZ
)) <= 0)
1413 if (ERRNO_IO_RETRY(errno
))
1415 vty_event (VTY_READ
, vty_sock
, vty
);
1418 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1419 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1420 __func__
, vty
->fd
, safe_strerror(errno
));
1421 buffer_reset(vty
->obuf
);
1423 vty
->status
= VTY_CLOSE
;
1426 for (i
= 0; i
< nbytes
; i
++)
1441 if (vty
->iac_sb_in_progress
&& !vty
->iac
)
1443 if (vty
->sb_len
< sizeof(vty
->sb_buf
))
1444 vty
->sb_buf
[vty
->sb_len
] = buf
[i
];
1451 /* In case of telnet command */
1453 ret
= vty_telnet_option (vty
, buf
+ i
, nbytes
- i
);
1460 if (vty
->status
== VTY_MORE
)
1467 vty_buffer_reset (vty
);
1469 #if 0 /* More line does not work for "show ip bgp". */
1472 vty
->status
= VTY_MORELINE
;
1481 /* Escape character. */
1482 if (vty
->escape
== VTY_ESCAPE
)
1484 vty_escape_map (buf
[i
], vty
);
1488 /* Pre-escape status. */
1489 if (vty
->escape
== VTY_PRE_ESCAPE
)
1494 vty
->escape
= VTY_ESCAPE
;
1497 vty_backward_word (vty
);
1498 vty
->escape
= VTY_NORMAL
;
1501 vty_forward_word (vty
);
1502 vty
->escape
= VTY_NORMAL
;
1505 vty_forward_kill_word (vty
);
1506 vty
->escape
= VTY_NORMAL
;
1510 vty_backward_kill_word (vty
);
1511 vty
->escape
= VTY_NORMAL
;
1514 vty
->escape
= VTY_NORMAL
;
1523 vty_beginning_of_line (vty
);
1526 vty_backward_char (vty
);
1529 vty_stop_input (vty
);
1532 vty_delete_char (vty
);
1535 vty_end_of_line (vty
);
1538 vty_forward_char (vty
);
1542 vty_delete_backward_char (vty
);
1545 vty_kill_line (vty
);
1548 vty_next_line (vty
);
1551 vty_previous_line (vty
);
1554 vty_transpose_chars (vty
);
1557 vty_kill_line_from_beginning (vty
);
1560 vty_backward_kill_word (vty
);
1563 vty_end_config (vty
);
1567 vty_out (vty
, "%s", VTY_NEWLINE
);
1571 vty_complete_command (vty
);
1574 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
1575 vty_self_insert (vty
, buf
[i
]);
1577 vty_describe_command (vty
);
1580 if (i
+ 1 < nbytes
&& buf
[i
+ 1] == '[')
1582 vty
->escape
= VTY_ESCAPE
;
1586 vty
->escape
= VTY_PRE_ESCAPE
;
1589 if (buf
[i
] > 31 && buf
[i
] < 127)
1590 vty_self_insert (vty
, buf
[i
]);
1596 if (vty
->status
== VTY_CLOSE
)
1600 vty_event (VTY_WRITE
, vty
->wfd
, vty
);
1601 vty_event (VTY_READ
, vty_sock
, vty
);
1606 /* Flush buffer to the vty. */
1608 vty_flush (struct thread
*thread
)
1611 buffer_status_t flushrc
;
1612 int vty_sock
= THREAD_FD (thread
);
1613 struct vty
*vty
= THREAD_ARG (thread
);
1615 vty
->t_write
= NULL
;
1617 /* Tempolary disable read thread. */
1618 if ((vty
->lines
== 0) && vty
->t_read
)
1620 thread_cancel (vty
->t_read
);
1624 /* Function execution continue. */
1625 erase
= ((vty
->status
== VTY_MORE
|| vty
->status
== VTY_MORELINE
));
1627 /* N.B. if width is 0, that means we don't know the window size. */
1628 if ((vty
->lines
== 0) || (vty
->width
== 0) || (vty
->height
== 0))
1629 flushrc
= buffer_flush_available(vty
->obuf
, vty_sock
);
1630 else if (vty
->status
== VTY_MORELINE
)
1631 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1634 flushrc
= buffer_flush_window(vty
->obuf
, vty_sock
, vty
->width
,
1635 vty
->lines
>= 0 ? vty
->lines
:
1641 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
1642 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1644 buffer_reset(vty
->obuf
);
1648 if (vty
->status
== VTY_CLOSE
)
1652 vty
->status
= VTY_NORMAL
;
1653 if (vty
->lines
== 0)
1654 vty_event (VTY_READ
, vty_sock
, vty
);
1657 case BUFFER_PENDING
:
1658 /* There is more data waiting to be written. */
1659 vty
->status
= VTY_MORE
;
1660 if (vty
->lines
== 0)
1661 vty_event (VTY_WRITE
, vty_sock
, vty
);
1668 /* allocate and initialise vty */
1670 vty_new_init (int vty_sock
)
1676 vty
->wfd
= vty_sock
;
1677 vty
->type
= VTY_TERM
;
1678 vty
->node
= AUTH_NODE
;
1681 vty_clear_buf (vty
);
1683 memset (vty
->hist
, 0, sizeof (vty
->hist
));
1686 vector_set_index (vtyvec
, vty_sock
, vty
);
1687 vty
->status
= VTY_NORMAL
;
1690 vty
->iac_sb_in_progress
= 0;
1696 /* Create new vty structure. */
1698 vty_create (int vty_sock
, union sockunion
*su
)
1700 char buf
[SU_ADDRSTRLEN
];
1703 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1705 /* Allocate new vty structure and set up default values. */
1706 vty
= vty_new_init (vty_sock
);
1708 /* configurable parameters not part of basic init */
1709 vty
->v_timeout
= vty_timeout_val
;
1710 strcpy (vty
->address
, buf
);
1711 if (no_password_check
)
1714 vty
->node
= ENABLE_NODE
;
1716 vty
->node
= VIEW_NODE
;
1718 if (host
.lines
>= 0)
1719 vty
->lines
= host
.lines
;
1721 if (! no_password_check
)
1723 /* Vty is not available if password isn't set. */
1724 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1726 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1727 vty
->status
= VTY_CLOSE
;
1733 /* Say hello to the world. */
1735 if (! no_password_check
)
1736 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1738 /* Setting up terminal. */
1739 vty_will_echo (vty
);
1740 vty_will_suppress_go_ahead (vty
);
1742 vty_dont_linemode (vty
);
1743 vty_do_window_size (vty
);
1744 /* vty_dont_lflow_ahead (vty); */
1748 /* Add read/write thread. */
1749 vty_event (VTY_WRITE
, vty_sock
, vty
);
1750 vty_event (VTY_READ
, vty_sock
, vty
);
1755 /* create vty for stdio */
1756 static struct termios stdio_orig_termios
;
1757 static struct vty
*stdio_vty
= NULL
;
1758 static void (*stdio_vty_atclose
)(void);
1761 vty_stdio_reset (void)
1765 tcsetattr (0, TCSANOW
, &stdio_orig_termios
);
1768 if (stdio_vty_atclose
)
1769 stdio_vty_atclose ();
1770 stdio_vty_atclose
= NULL
;
1775 vty_stdio (void (*atclose
)())
1778 struct termios termios
;
1780 /* refuse creating two vtys on stdio */
1784 vty
= stdio_vty
= vty_new_init (0);
1785 stdio_vty_atclose
= atclose
;
1788 /* always have stdio vty in a known _unchangeable_ state, don't want config
1789 * to have any effect here to make sure scripting this works as intended */
1790 vty
->node
= ENABLE_NODE
;
1792 strcpy (vty
->address
, "console");
1794 if (!tcgetattr (0, &stdio_orig_termios
))
1796 termios
= stdio_orig_termios
;
1797 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
1798 | INLCR
| IGNCR
| ICRNL
| IXON
);
1799 termios
.c_oflag
&= ~OPOST
;
1800 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
1801 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1802 termios
.c_cflag
|= CS8
;
1803 tcsetattr (0, TCSANOW
, &termios
);
1808 /* Add read/write thread. */
1809 vty_event (VTY_WRITE
, 1, vty
);
1810 vty_event (VTY_READ
, 0, vty
);
1815 /* Accept connection from the network. */
1817 vty_accept (struct thread
*thread
)
1825 struct access_list
*acl
= NULL
;
1826 char buf
[SU_ADDRSTRLEN
];
1828 accept_sock
= THREAD_FD (thread
);
1830 /* We continue hearing vty socket. */
1831 vty_event (VTY_SERV
, accept_sock
, NULL
);
1833 memset (&su
, 0, sizeof (union sockunion
));
1835 /* We can handle IPv4 or IPv6 socket. */
1836 vty_sock
= sockunion_accept (accept_sock
, &su
);
1839 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1842 set_nonblocking(vty_sock
);
1843 set_cloexec(vty_sock
);
1845 sockunion2hostprefix (&su
, &p
);
1847 /* VTY's accesslist apply. */
1848 if (p
.family
== AF_INET
&& vty_accesslist_name
)
1850 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1851 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1853 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1854 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1857 /* continue accepting connections */
1858 vty_event (VTY_SERV
, accept_sock
, NULL
);
1865 /* VTY's ipv6 accesslist apply. */
1866 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1868 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1869 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1871 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1872 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1875 /* continue accepting connections */
1876 vty_event (VTY_SERV
, accept_sock
, NULL
);
1881 #endif /* HAVE_IPV6 */
1884 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1885 (char *) &on
, sizeof (on
));
1887 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1888 safe_strerror (errno
));
1890 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1891 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1893 vty_create (vty_sock
, &su
);
1900 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1903 struct addrinfo req
;
1904 struct addrinfo
*ainfo
;
1905 struct addrinfo
*ainfo_save
;
1907 char port_str
[BUFSIZ
];
1909 memset (&req
, 0, sizeof (struct addrinfo
));
1910 req
.ai_flags
= AI_PASSIVE
;
1911 req
.ai_family
= AF_UNSPEC
;
1912 req
.ai_socktype
= SOCK_STREAM
;
1913 sprintf (port_str
, "%d", port
);
1914 port_str
[sizeof (port_str
) - 1] = '\0';
1916 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1920 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1928 if (ainfo
->ai_family
!= AF_INET
1930 && ainfo
->ai_family
!= AF_INET6
1931 #endif /* HAVE_IPV6 */
1935 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1939 sockopt_v6only (ainfo
->ai_family
, sock
);
1940 sockopt_reuseaddr (sock
);
1941 sockopt_reuseport (sock
);
1944 ret
= bind (sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
);
1947 close (sock
); /* Avoid sd leak. */
1951 ret
= listen (sock
, 3);
1954 close (sock
); /* Avoid sd leak. */
1958 vty_event (VTY_SERV
, sock
, NULL
);
1960 while ((ainfo
= ainfo
->ai_next
) != NULL
);
1962 freeaddrinfo (ainfo_save
);
1964 #else /* HAVE_IPV6 */
1966 /* Make vty server socket. */
1968 vty_serv_sock_family (const char* addr
, unsigned short port
, int family
)
1975 memset (&su
, 0, sizeof (union sockunion
));
1976 su
.sa
.sa_family
= family
;
1981 naddr
=&su
.sin
.sin_addr
;
1985 naddr
=&su
.sin6
.sin6_addr
;
1991 switch(inet_pton(family
,addr
,naddr
))
1994 zlog_err("bad address %s",addr
);
1998 zlog_err("error translating address %s: %s",addr
,safe_strerror(errno
));
2002 /* Make new socket. */
2003 accept_sock
= sockunion_stream_socket (&su
);
2004 if (accept_sock
< 0)
2007 /* This is server, so reuse address. */
2008 sockopt_reuseaddr (accept_sock
);
2009 sockopt_reuseport (accept_sock
);
2010 set_cloexec (accept_sock
);
2012 /* Bind socket to universal address and given port. */
2013 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
2016 zlog_warn("can't bind socket");
2017 close (accept_sock
); /* Avoid sd leak. */
2021 /* Listen socket under queue 3. */
2022 ret
= listen (accept_sock
, 3);
2025 zlog (NULL
, LOG_WARNING
, "can't listen socket");
2026 close (accept_sock
); /* Avoid sd leak. */
2030 /* Add vty server event. */
2031 vty_event (VTY_SERV
, accept_sock
, NULL
);
2033 #endif /* HAVE_IPV6 */
2036 /* For sockaddr_un. */
2039 /* VTY shell UNIX domain socket. */
2041 vty_serv_un (const char *path
)
2045 struct sockaddr_un serv
;
2047 struct zprivs_ids_t ids
;
2049 /* First of all, unlink existing socket */
2053 old_mask
= umask (0007);
2055 /* Make UNIX domain socket. */
2056 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2059 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
2063 /* Make server socket. */
2064 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2065 serv
.sun_family
= AF_UNIX
;
2066 strncpy (serv
.sun_path
, path
, strlen (path
));
2067 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2068 len
= serv
.sun_len
= SUN_LEN(&serv
);
2070 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2071 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2075 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2078 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2079 close (sock
); /* Avoid sd leak. */
2083 ret
= listen (sock
, 5);
2086 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
2087 close (sock
); /* Avoid sd leak. */
2093 zprivs_get_ids(&ids
);
2095 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2096 earlier for the case when we don't need to chown the file
2097 type casting it here to make a compare */
2098 if ((int)ids
.gid_vty
> 0)
2100 /* set group of socket */
2101 if ( chown (path
, -1, ids
.gid_vty
) )
2103 zlog_err ("vty_serv_un: could chown socket, %s",
2104 safe_strerror (errno
) );
2108 vty_event (VTYSH_SERV
, sock
, NULL
);
2111 /* #define VTYSH_DEBUG 1 */
2114 vtysh_accept (struct thread
*thread
)
2119 struct sockaddr_un client
;
2122 accept_sock
= THREAD_FD (thread
);
2124 vty_event (VTYSH_SERV
, accept_sock
, NULL
);
2126 memset (&client
, 0, sizeof (struct sockaddr_un
));
2127 client_len
= sizeof (struct sockaddr_un
);
2129 sock
= accept (accept_sock
, (struct sockaddr
*) &client
,
2130 (socklen_t
*) &client_len
);
2134 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
2138 if (set_nonblocking(sock
) < 0)
2140 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2141 " %s, closing", sock
, safe_strerror (errno
));
2148 printf ("VTY shell accept\n");
2149 #endif /* VTYSH_DEBUG */
2154 vty
->type
= VTY_SHELL_SERV
;
2155 vty
->node
= VIEW_NODE
;
2157 vty_event (VTYSH_READ
, sock
, vty
);
2163 vtysh_flush(struct vty
*vty
)
2165 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
))
2167 case BUFFER_PENDING
:
2168 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2171 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2172 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2173 buffer_reset(vty
->obuf
);
2184 vtysh_read (struct thread
*thread
)
2190 unsigned char buf
[VTY_READ_BUFSIZ
];
2192 u_char header
[4] = {0, 0, 0, 0};
2194 sock
= THREAD_FD (thread
);
2195 vty
= THREAD_ARG (thread
);
2198 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2202 if (ERRNO_IO_RETRY(errno
))
2204 vty_event (VTYSH_READ
, sock
, vty
);
2207 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2208 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2209 __func__
, sock
, safe_strerror(errno
));
2211 buffer_reset(vty
->obuf
);
2214 printf ("close vtysh\n");
2215 #endif /* VTYSH_DEBUG */
2220 printf ("line: %.*s\n", nbytes
, buf
);
2221 #endif /* VTYSH_DEBUG */
2223 if (vty
->length
+ nbytes
>= VTY_BUFSIZ
)
2225 /* Clear command line buffer. */
2226 vty
->cp
= vty
->length
= 0;
2227 vty_clear_buf (vty
);
2228 vty_out (vty
, "%% Command is too long.%s", VTY_NEWLINE
);
2232 for (p
= buf
; p
< buf
+nbytes
; p
++)
2234 vty
->buf
[vty
->length
++] = *p
;
2237 /* Pass this line to parser. */
2238 ret
= vty_execute (vty
);
2239 /* Note that vty_execute clears the command buffer and resets
2240 vty->length to 0. */
2242 /* Return result. */
2244 printf ("result: %d\n", ret
);
2245 printf ("vtysh node: %d\n", vty
->node
);
2246 #endif /* VTYSH_DEBUG */
2248 /* hack for asynchronous "write integrated"
2249 * - other commands in "buf" will be ditched
2250 * - input during pending config-write is "unsupported" */
2251 if (ret
== CMD_SUSPEND
)
2254 /* warning: watchquagga hardcodes this result write */
2256 buffer_put(vty
->obuf
, header
, 4);
2258 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2259 /* Try to flush results; exit if a write error occurs. */
2265 vty_event (VTYSH_READ
, sock
, vty
);
2271 vtysh_write (struct thread
*thread
)
2273 struct vty
*vty
= THREAD_ARG (thread
);
2275 vty
->t_write
= NULL
;
2282 /* Determine address family to bind. */
2284 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2286 /* If port is set to 0, do not listen on TCP/IP at all! */
2291 vty_serv_sock_addrinfo (addr
, port
);
2292 #else /* ! HAVE_IPV6 */
2293 vty_serv_sock_family (addr
,port
, AF_INET
);
2294 #endif /* HAVE_IPV6 */
2302 /* Close vty interface. Warning: call this only from functions that
2303 will be careful not to access the vty afterwards (since it has
2304 now been freed). This is safest from top-level functions (called
2305 directly by the thread dispatcher). */
2307 vty_close (struct vty
*vty
)
2311 /* Cancel threads.*/
2313 thread_cancel (vty
->t_read
);
2315 thread_cancel (vty
->t_write
);
2317 thread_cancel (vty
->t_timeout
);
2320 buffer_flush_all (vty
->obuf
, vty
->wfd
);
2322 /* Free input buffer. */
2323 buffer_free (vty
->obuf
);
2325 /* Free command history. */
2326 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2328 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2331 vector_unset (vtyvec
, vty
->fd
);
2333 if (vty
->wfd
> 0 && vty
->type
== VTY_FILE
)
2340 if (vty
->wfd
> 0 && vty
->wfd
!= vty
->fd
)
2347 XFREE (MTYPE_VTY
, vty
->buf
);
2350 XFREE (MTYPE_VTY
, vty
->error_buf
);
2352 /* Check configure. */
2353 vty_config_unlock (vty
);
2356 XFREE (MTYPE_VTY
, vty
);
2359 /* When time out occur output message then close connection. */
2361 vty_timeout (struct thread
*thread
)
2365 vty
= THREAD_ARG (thread
);
2366 vty
->t_timeout
= NULL
;
2370 buffer_reset (vty
->obuf
);
2371 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2373 /* Close connection. */
2374 vty
->status
= VTY_CLOSE
;
2380 /* Read up configuration file from file_name. */
2382 vty_read_file (FILE *confp
)
2386 unsigned int line_num
= 0;
2389 vty
->wfd
= dup(STDERR_FILENO
); /* vty_close() will close this */
2392 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2393 vty
->wfd
= STDOUT_FILENO
;
2395 vty
->fd
= STDIN_FILENO
;
2396 vty
->type
= VTY_FILE
;
2397 vty
->node
= CONFIG_NODE
;
2399 /* Execute configuration file */
2400 ret
= config_from_file (vty
, confp
, &line_num
);
2402 /* Flush any previous errors before printing messages below */
2403 buffer_flush_all (vty
->obuf
, vty
->fd
);
2405 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2409 case CMD_ERR_AMBIGUOUS
:
2410 fprintf (stderr
, "*** Error reading config: Ambiguous command.\n");
2412 case CMD_ERR_NO_MATCH
:
2413 fprintf (stderr
, "*** Error reading config: There is no such command.\n");
2416 fprintf (stderr
, "*** Error occured processing line %u, below:\n%s\n",
2417 line_num
, vty
->error_buf
);
2424 vty_use_backup_config (char *fullpath
)
2426 char *fullpath_sav
, *fullpath_tmp
;
2433 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2434 strcpy (fullpath_sav
, fullpath
);
2435 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2436 if (stat (fullpath_sav
, &buf
) == -1)
2438 free (fullpath_sav
);
2442 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2443 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2445 /* Open file to configuration write. */
2446 tmp
= mkstemp (fullpath_tmp
);
2449 free (fullpath_sav
);
2450 free (fullpath_tmp
);
2454 sav
= open (fullpath_sav
, O_RDONLY
);
2457 unlink (fullpath_tmp
);
2458 free (fullpath_sav
);
2459 free (fullpath_tmp
);
2463 while((c
= read (sav
, buffer
, 512)) > 0)
2465 if (write (tmp
, buffer
, c
) <= 0)
2467 free (fullpath_sav
);
2468 free (fullpath_tmp
);
2477 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2479 unlink (fullpath_tmp
);
2480 free (fullpath_sav
);
2481 free (fullpath_tmp
);
2485 if (link (fullpath_tmp
, fullpath
) == 0)
2486 ret
= fopen (fullpath
, "r");
2488 unlink (fullpath_tmp
);
2490 free (fullpath_sav
);
2491 free (fullpath_tmp
);
2495 /* Read up configuration file from file_name. */
2497 vty_read_config (char *config_file
,
2498 char *config_default_dir
)
2500 char cwd
[MAXPATHLEN
];
2505 /* If -f flag specified. */
2506 if (config_file
!= NULL
)
2508 if (! IS_DIRECTORY_SEP (config_file
[0]))
2510 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2512 fprintf (stderr
, "Failure to determine Current Working Directory %d!\n", errno
);
2515 tmp
= XMALLOC (MTYPE_TMP
,
2516 strlen (cwd
) + strlen (config_file
) + 2);
2517 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2521 fullpath
= config_file
;
2523 confp
= fopen (fullpath
, "r");
2527 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2528 __func__
, fullpath
, safe_strerror (errno
));
2530 confp
= vty_use_backup_config (fullpath
);
2532 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2535 fprintf (stderr
, "can't open configuration file [%s]\n",
2544 host_config_set (config_default_dir
);
2548 struct stat conf_stat
;
2550 /* !!!!PLEASE LEAVE!!!!
2551 * This is NEEDED for use with vtysh -b, or else you can get
2552 * a real configuration food fight with a lot garbage in the
2553 * merged configuration file it creates coming from the per
2554 * daemon configuration files. This also allows the daemons
2555 * to start if there default configuration file is not
2556 * present or ignore them, as needed when using vtysh -b to
2557 * configure the daemons at boot - MAG
2560 /* Stat for vtysh Zebra.conf, if found startup and wait for
2561 * boot configuration
2564 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2566 ret
= stat (integrate_default
, &conf_stat
);
2568 goto tmp_free_and_out
;
2571 confp
= fopen (config_default_dir
, "r");
2574 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2575 __func__
, config_default_dir
, safe_strerror (errno
));
2577 confp
= vty_use_backup_config (config_default_dir
);
2580 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2581 fullpath
= config_default_dir
;
2585 fprintf (stderr
, "can't open configuration file [%s]\n",
2586 config_default_dir
);
2587 goto tmp_free_and_out
;
2591 fullpath
= config_default_dir
;
2594 vty_read_file (confp
);
2598 host_config_set (fullpath
);
2602 XFREE (MTYPE_TMP
, fullpath
);
2605 /* Small utility function which output log to the VTY. */
2607 vty_log (const char *level
, const char *proto_str
,
2608 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2616 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2617 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2622 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2627 /* Async-signal-safe version of vty_log for fixed strings. */
2629 vty_log_fixed (char *buf
, size_t len
)
2632 struct iovec iov
[2];
2633 char crlf
[4] = "\r\n";
2635 /* vty may not have been initialised */
2639 iov
[0].iov_base
= buf
;
2640 iov
[0].iov_len
= len
;
2641 iov
[1].iov_base
= crlf
;
2644 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2647 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2648 /* N.B. We don't care about the return code, since process is
2649 most likely just about to die anyway. */
2650 if (writev(vty
->wfd
, iov
, 2) == -1)
2652 fprintf(stderr
, "Failure to writev: %d\n", errno
);
2659 vty_config_lock (struct vty
*vty
)
2661 if (vty_config_is_lockless
)
2663 if (vty_config
== 0)
2672 vty_config_unlock (struct vty
*vty
)
2674 if (vty_config_is_lockless
)
2676 if (vty_config
== 1 && vty
->config
== 1)
2685 vty_config_lockless (void)
2687 vty_config_is_lockless
= 1;
2690 /* Master of the threads. */
2691 static struct thread_master
*vty_master
;
2694 vty_event (enum event event
, int sock
, struct vty
*vty
)
2696 struct thread
*vty_serv_thread
;
2701 vty_serv_thread
= thread_add_read (vty_master
, vty_accept
, vty
, sock
);
2702 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2706 vty_serv_thread
= thread_add_read (vty_master
, vtysh_accept
, vty
, sock
);
2707 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2710 vty
->t_read
= thread_add_read (vty_master
, vtysh_read
, vty
, sock
);
2713 vty
->t_write
= thread_add_write (vty_master
, vtysh_write
, vty
, sock
);
2717 vty
->t_read
= thread_add_read (vty_master
, vty_read
, vty
, sock
);
2719 /* Time out treatment. */
2723 thread_cancel (vty
->t_timeout
);
2725 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2730 vty
->t_write
= thread_add_write (vty_master
, vty_flush
, vty
, sock
);
2732 case VTY_TIMEOUT_RESET
:
2735 thread_cancel (vty
->t_timeout
);
2736 vty
->t_timeout
= NULL
;
2741 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2750 "Display who is on vty\n")
2755 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2756 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2757 vty_out (vty
, "%svty[%d] connected from %s.%s",
2758 v
->config
? "*" : " ",
2759 i
, v
->address
, VTY_NEWLINE
);
2763 /* Move to vty configuration mode. */
2767 "Configure a terminal line\n"
2768 "Virtual terminal\n")
2770 vty
->node
= VTY_NODE
;
2774 /* Set time out value. */
2776 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2778 unsigned long timeout
= 0;
2780 /* min_str and sec_str are already checked by parser. So it must be
2781 all digit string. */
2784 timeout
= strtol (min_str
, NULL
, 10);
2788 timeout
+= strtol (sec_str
, NULL
, 10);
2790 vty_timeout_val
= timeout
;
2791 vty
->v_timeout
= timeout
;
2792 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2798 DEFUN (exec_timeout_min
,
2799 exec_timeout_min_cmd
,
2800 "exec-timeout <0-35791>",
2801 "Set timeout value\n"
2802 "Timeout value in minutes\n")
2804 return exec_timeout (vty
, argv
[0], NULL
);
2807 DEFUN (exec_timeout_sec
,
2808 exec_timeout_sec_cmd
,
2809 "exec-timeout <0-35791> <0-2147483>",
2810 "Set the EXEC timeout\n"
2811 "Timeout in minutes\n"
2812 "Timeout in seconds\n")
2814 return exec_timeout (vty
, argv
[0], argv
[1]);
2817 DEFUN (no_exec_timeout
,
2818 no_exec_timeout_cmd
,
2821 "Set the EXEC timeout\n")
2823 return exec_timeout (vty
, NULL
, NULL
);
2826 /* Set vty access class. */
2827 DEFUN (vty_access_class
,
2828 vty_access_class_cmd
,
2829 "access-class WORD",
2830 "Filter connections based on an IP access list\n"
2833 if (vty_accesslist_name
)
2834 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2836 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2841 /* Clear vty access class. */
2842 DEFUN (no_vty_access_class
,
2843 no_vty_access_class_cmd
,
2844 "no access-class [WORD]",
2846 "Filter connections based on an IP access list\n"
2849 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2851 vty_out (vty
, "Access-class is not currently applied to vty%s",
2856 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2858 vty_accesslist_name
= NULL
;
2864 /* Set vty access class. */
2865 DEFUN (vty_ipv6_access_class
,
2866 vty_ipv6_access_class_cmd
,
2867 "ipv6 access-class WORD",
2869 "Filter connections based on an IP access list\n"
2870 "IPv6 access list\n")
2872 if (vty_ipv6_accesslist_name
)
2873 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2875 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2880 /* Clear vty access class. */
2881 DEFUN (no_vty_ipv6_access_class
,
2882 no_vty_ipv6_access_class_cmd
,
2883 "no ipv6 access-class [WORD]",
2886 "Filter connections based on an IP access list\n"
2887 "IPv6 access list\n")
2889 if (! vty_ipv6_accesslist_name
||
2890 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2892 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2897 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2899 vty_ipv6_accesslist_name
= NULL
;
2903 #endif /* HAVE_IPV6 */
2909 "Enable password checking\n")
2911 no_password_check
= 0;
2915 DEFUN (no_vty_login
,
2919 "Enable password checking\n")
2921 no_password_check
= 1;
2925 DEFUN (service_advanced_vty
,
2926 service_advanced_vty_cmd
,
2927 "service advanced-vty",
2928 "Set up miscellaneous service\n"
2929 "Enable advanced mode vty interface\n")
2935 DEFUN (no_service_advanced_vty
,
2936 no_service_advanced_vty_cmd
,
2937 "no service advanced-vty",
2939 "Set up miscellaneous service\n"
2940 "Enable advanced mode vty interface\n")
2946 DEFUN (terminal_monitor
,
2947 terminal_monitor_cmd
,
2949 "Set terminal line parameters\n"
2950 "Copy debug output to the current terminal line\n")
2956 DEFUN (terminal_no_monitor
,
2957 terminal_no_monitor_cmd
,
2958 "terminal no monitor",
2959 "Set terminal line parameters\n"
2961 "Copy debug output to the current terminal line\n")
2967 ALIAS (terminal_no_monitor
,
2968 no_terminal_monitor_cmd
,
2969 "no terminal monitor",
2971 "Set terminal line parameters\n"
2972 "Copy debug output to the current terminal line\n")
2974 DEFUN (show_history
,
2978 "Display the session command history\n")
2982 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2984 if (index
== VTY_MAXHIST
)
2990 if (vty
->hist
[index
] != NULL
)
2991 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
3000 DEFUN (log_commands
,
3004 "Log all commands (can't be unset without restart)\n")
3006 do_log_commands
= 1;
3010 /* Display current configuration. */
3012 vty_config_write (struct vty
*vty
)
3014 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
3016 if (vty_accesslist_name
)
3017 vty_out (vty
, " access-class %s%s",
3018 vty_accesslist_name
, VTY_NEWLINE
);
3020 if (vty_ipv6_accesslist_name
)
3021 vty_out (vty
, " ipv6 access-class %s%s",
3022 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
3025 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3026 vty_out (vty
, " exec-timeout %ld %ld%s",
3027 vty_timeout_val
/ 60,
3028 vty_timeout_val
% 60, VTY_NEWLINE
);
3031 if (no_password_check
)
3032 vty_out (vty
, " no login%s", VTY_NEWLINE
);
3034 if (do_log_commands
)
3035 vty_out (vty
, "log commands%s", VTY_NEWLINE
);
3037 vty_out (vty
, "!%s", VTY_NEWLINE
);
3042 struct cmd_node vty_node
=
3045 "%s(config-line)# ",
3049 /* Reset all VTY status. */
3055 struct thread
*vty_serv_thread
;
3057 for (i
= 0; i
< vector_active (vtyvec
); i
++)
3058 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
3060 buffer_reset (vty
->obuf
);
3061 vty
->status
= VTY_CLOSE
;
3065 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
3066 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
3068 thread_cancel (vty_serv_thread
);
3069 vector_slot (Vvty_serv_thread
, i
) = NULL
;
3073 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3075 if (vty_accesslist_name
)
3077 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3078 vty_accesslist_name
= NULL
;
3081 if (vty_ipv6_accesslist_name
)
3083 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3084 vty_ipv6_accesslist_name
= NULL
;
3091 char cwd
[MAXPATHLEN
];
3094 c
= getcwd (cwd
, MAXPATHLEN
);
3099 * At this point if these go wrong, more than likely
3100 * the whole world is coming down around us
3101 * Hence not worrying about it too much.
3103 if (!chdir (SYSCONFDIR
))
3105 fprintf(stderr
, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR
, errno
);
3108 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
3110 fprintf(stderr
, "Failure to getcwd, errno: %d\n", errno
);
3115 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
3116 strcpy (vty_cwd
, cwd
);
3126 vty_shell (struct vty
*vty
)
3128 return vty
->type
== VTY_SHELL
? 1 : 0;
3132 vty_shell_serv (struct vty
*vty
)
3134 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3140 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3143 /* Install vty's own commands like `who' command. */
3145 vty_init (struct thread_master
*master_thread
)
3147 /* For further configuration read, preserve current directory. */
3150 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3152 vty_master
= master_thread
;
3154 atexit (vty_stdio_reset
);
3156 /* Initilize server thread vector. */
3157 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
3159 /* Install bgp top node. */
3160 install_node (&vty_node
, vty_config_write
);
3162 install_element (VIEW_NODE
, &config_who_cmd
);
3163 install_element (VIEW_NODE
, &show_history_cmd
);
3164 install_element (CONFIG_NODE
, &line_vty_cmd
);
3165 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
3166 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3167 install_element (CONFIG_NODE
, &show_history_cmd
);
3168 install_element (CONFIG_NODE
, &log_commands_cmd
);
3169 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
3170 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
3171 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
3173 install_default (VTY_NODE
);
3174 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
3175 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
3176 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
3177 install_element (VTY_NODE
, &vty_access_class_cmd
);
3178 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
3179 install_element (VTY_NODE
, &vty_login_cmd
);
3180 install_element (VTY_NODE
, &no_vty_login_cmd
);
3182 install_element (VTY_NODE
, &vty_ipv6_access_class_cmd
);
3183 install_element (VTY_NODE
, &no_vty_ipv6_access_class_cmd
);
3184 #endif /* HAVE_IPV6 */
3188 vty_terminate (void)
3191 XFREE (MTYPE_TMP
, vty_cwd
);
3193 if (vtyvec
&& Vvty_serv_thread
)
3196 vector_free (vtyvec
);
3197 vector_free (Vvty_serv_thread
);
3199 Vvty_serv_thread
= NULL
;
3203 /* Utility functions to get arguments from commands generated
3204 by the xml2cli.pl script. */
3206 vty_get_arg_value (struct vty_arg
*args
[], const char *arg
)
3210 if (strcmp ((*args
)->name
, arg
) == 0)
3211 return (*args
)->value
;
3218 vty_get_arg (struct vty_arg
*args
[], const char *arg
)
3222 if (strcmp ((*args
)->name
, arg
) == 0)