2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <lib/version.h>
30 #include "sockunion.h"
40 #include <arpa/telnet.h>
43 DEFINE_MTYPE_STATIC(LIB
, VTY
, "VTY")
44 DEFINE_MTYPE_STATIC(LIB
, VTY_OUT_BUF
, "VTY output buffer")
45 DEFINE_MTYPE_STATIC(LIB
, VTY_HIST
, "VTY history")
61 static void vty_event (enum event
, int, struct vty
*);
63 /* Extern host structure from command.c */
64 extern struct host host
;
66 /* Vector which store each vty structure. */
69 /* Vty timeout value. */
70 static unsigned long vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
72 /* Vty access-class command */
73 static char *vty_accesslist_name
= NULL
;
75 /* Vty access-calss for IPv6. */
76 static char *vty_ipv6_accesslist_name
= NULL
;
78 /* VTY server thread. */
79 static vector Vvty_serv_thread
;
81 /* Current directory. */
85 static int vty_config
;
87 /* Login password check. */
88 static int no_password_check
= 0;
90 /* Restrict unauthenticated logins? */
91 static const u_char restricted_mode_default
= 0;
92 static u_char restricted_mode
= 0;
94 /* Integrated configuration file path */
95 char integrate_default
[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG
;
97 static int do_log_commands
= 0;
99 /* VTY standard output function. */
101 vty_out (struct vty
*vty
, const char *format
, ...)
111 va_start (args
, format
);
112 vprintf (format
, args
);
117 /* Try to write to initial buffer. */
118 va_start (args
, format
);
119 len
= vsnprintf (buf
, sizeof(buf
), format
, args
);
122 /* Initial buffer is not enough. */
123 if (len
< 0 || len
>= size
)
132 p
= XREALLOC (MTYPE_VTY_OUT_BUF
, p
, size
);
136 va_start (args
, format
);
137 len
= vsnprintf (p
, size
, format
, args
);
140 if (len
> -1 && len
< size
)
145 /* When initial buffer is enough to store all output. */
149 /* Pointer p must point out buffer. */
150 buffer_put (vty
->obuf
, (u_char
*) p
, len
);
152 /* If p is not different with buf, it is allocated buffer. */
154 XFREE (MTYPE_VTY_OUT_BUF
, p
);
161 vty_log_out (struct vty
*vty
, const char *level
, const char *proto_str
,
162 const char *format
, struct timestamp_control
*ctl
, va_list va
)
168 if (!ctl
->already_rendered
)
170 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
171 ctl
->already_rendered
= 1;
173 if (ctl
->len
+1 >= sizeof(buf
))
175 memcpy(buf
, ctl
->buf
, len
= ctl
->len
);
180 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: %s: ", level
, proto_str
);
182 ret
= snprintf(buf
+len
, sizeof(buf
)-len
, "%s: ", proto_str
);
183 if ((ret
< 0) || ((size_t)(len
+= ret
) >= sizeof(buf
)))
186 if (((ret
= vsnprintf(buf
+len
, sizeof(buf
)-len
, format
, va
)) < 0) ||
187 ((size_t)((len
+= ret
)+2) > sizeof(buf
)))
193 if (write(vty
->wfd
, buf
, len
) < 0)
195 if (ERRNO_IO_RETRY(errno
))
196 /* Kernel buffer is full, probably too much debugging output, so just
197 drop the data and ignore. */
199 /* Fatal I/O error. */
200 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
201 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
202 __func__
, vty
->fd
, safe_strerror(errno
));
203 buffer_reset(vty
->obuf
);
204 /* cannot call vty_close, because a parent routine may still try
205 to access the vty struct */
206 vty
->status
= VTY_CLOSE
;
207 shutdown(vty
->fd
, SHUT_RDWR
);
213 /* Output current time to the vty. */
215 vty_time_print (struct vty
*vty
, int cr
)
217 char buf
[QUAGGA_TIMESTAMP_LEN
];
219 if (quagga_timestamp(0, buf
, sizeof(buf
)) == 0)
221 zlog (NULL
, LOG_INFO
, "quagga_timestamp error");
225 vty_out (vty
, "%s\n", buf
);
227 vty_out (vty
, "%s ", buf
);
232 /* Say hello to vty interface. */
234 vty_hello (struct vty
*vty
)
241 f
= fopen (host
.motdfile
, "r");
244 while (fgets (buf
, sizeof (buf
), f
))
247 /* work backwards to ignore trailling isspace() */
248 for (s
= buf
+ strlen (buf
); (s
> buf
) && isspace ((int)*(s
- 1));
251 vty_out (vty
, "%s%s", buf
, VTY_NEWLINE
);
256 vty_out (vty
, "MOTD file not found%s", VTY_NEWLINE
);
259 vty_out (vty
, "%s", host
.motd
);
262 /* Put out prompt and wait input from user. */
264 vty_prompt (struct vty
*vty
)
266 struct utsname names
;
269 if (vty
->type
== VTY_TERM
)
271 hostname
= host
.name
;
275 hostname
= names
.nodename
;
277 vty_out (vty
, cmd_prompt (vty
->node
), hostname
);
281 /* Send WILL TELOPT_ECHO to remote server. */
283 vty_will_echo (struct vty
*vty
)
285 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_ECHO
, '\0' };
286 vty_out (vty
, "%s", cmd
);
289 /* Make suppress Go-Ahead telnet option. */
291 vty_will_suppress_go_ahead (struct vty
*vty
)
293 unsigned char cmd
[] = { IAC
, WILL
, TELOPT_SGA
, '\0' };
294 vty_out (vty
, "%s", cmd
);
297 /* Make don't use linemode over telnet. */
299 vty_dont_linemode (struct vty
*vty
)
301 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LINEMODE
, '\0' };
302 vty_out (vty
, "%s", cmd
);
305 /* Use window size. */
307 vty_do_window_size (struct vty
*vty
)
309 unsigned char cmd
[] = { IAC
, DO
, TELOPT_NAWS
, '\0' };
310 vty_out (vty
, "%s", cmd
);
313 #if 0 /* Currently not used. */
314 /* Make don't use lflow vty interface. */
316 vty_dont_lflow_ahead (struct vty
*vty
)
318 unsigned char cmd
[] = { IAC
, DONT
, TELOPT_LFLOW
, '\0' };
319 vty_out (vty
, "%s", cmd
);
323 /* Allocate new vty struct. */
327 struct vty
*new = XCALLOC (MTYPE_VTY
, sizeof (struct vty
));
329 new->obuf
= buffer_new(0); /* Use default buffer size. */
330 new->buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
331 new->error_buf
= XCALLOC (MTYPE_VTY
, VTY_BUFSIZ
);
332 new->max
= VTY_BUFSIZ
;
337 /* Authentication of vty */
339 vty_auth (struct vty
*vty
, char *buf
)
342 enum node_type next_node
= 0;
344 char *crypt (const char *, const char *);
350 passwd
= host
.password_encrypt
;
352 passwd
= host
.password
;
354 next_node
= host
.enable
? VIEW_NODE
: ENABLE_NODE
;
356 next_node
= VIEW_NODE
;
358 case AUTH_ENABLE_NODE
:
360 passwd
= host
.enable_encrypt
;
362 passwd
= host
.enable
;
363 next_node
= ENABLE_NODE
;
370 fail
= strcmp (crypt(buf
, passwd
), passwd
);
372 fail
= strcmp (buf
, passwd
);
380 vty
->node
= next_node
; /* Success ! */
387 if (vty
->node
== AUTH_NODE
)
389 vty_out (vty
, "%% Bad passwords, too many failures!%s", VTY_NEWLINE
);
390 vty
->status
= VTY_CLOSE
;
394 /* AUTH_ENABLE_NODE */
396 vty_out (vty
, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE
);
397 vty
->node
= restricted_mode
? RESTRICTED_NODE
: VIEW_NODE
;
403 /* Command execution over the vty interface. */
405 vty_command (struct vty
*vty
, char *buf
)
409 const char *protocolname
;
413 * Log non empty command lines
419 /* Skip white spaces. */
420 while (isspace ((int) *cp
) && *cp
!= '\0')
423 if (cp
!= NULL
&& *cp
!= '\0')
426 char vty_str
[VTY_BUFSIZ
];
427 char prompt_str
[VTY_BUFSIZ
];
429 /* format the base vty info */
430 snprintf(vty_str
, sizeof(vty_str
), "vty[??]@%s", vty
->address
);
432 for (i
= 0; i
< vector_active (vtyvec
); i
++)
433 if (vty
== vector_slot (vtyvec
, i
))
435 snprintf(vty_str
, sizeof(vty_str
), "vty[%d]@%s",
440 /* format the prompt */
441 snprintf(prompt_str
, sizeof(prompt_str
), cmd_prompt (vty
->node
), vty_str
);
443 /* now log the command */
444 zlog(NULL
, LOG_ERR
, "%s%s", prompt_str
, buf
);
446 /* Split readline string up into the vector */
447 vline
= cmd_make_strvec (buf
);
452 #ifdef CONSUMED_TIME_CHECK
456 unsigned long realtime
, cputime
;
459 #endif /* CONSUMED_TIME_CHECK */
461 ret
= cmd_execute_command (vline
, vty
, NULL
, 0);
463 /* Get the name of the protocol if any */
465 protocolname
= zlog_proto_names
[zlog_default
->protocol
];
467 protocolname
= zlog_proto_names
[ZLOG_NONE
];
469 #ifdef CONSUMED_TIME_CHECK
471 if ((realtime
= thread_consumed_time(&after
, &before
, &cputime
)) >
473 /* Warn about CPU hog that must be fixed. */
474 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
475 realtime
/1000, cputime
/1000, buf
);
477 #endif /* CONSUMED_TIME_CHECK */
479 if (ret
!= CMD_SUCCESS
)
483 if (vty
->type
== VTY_FILE
)
484 vty_out (vty
, "Warning...%s", VTY_NEWLINE
);
486 case CMD_ERR_AMBIGUOUS
:
487 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
489 case CMD_ERR_NO_MATCH
:
490 vty_out (vty
, "%% [%s] Unknown command: %s%s", protocolname
, buf
, VTY_NEWLINE
);
492 case CMD_ERR_INCOMPLETE
:
493 vty_out (vty
, "%% Command incomplete.%s", VTY_NEWLINE
);
496 cmd_free_strvec (vline
);
501 static const char telnet_backward_char
= 0x08;
502 static const char telnet_space_char
= ' ';
504 /* Basic function to write buffer to vty. */
506 vty_write (struct vty
*vty
, const char *buf
, size_t nbytes
)
508 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
511 /* Should we do buffering here ? And make vty_flush (vty) ? */
512 buffer_put (vty
->obuf
, buf
, nbytes
);
515 /* Ensure length of input buffer. Is buffer is short, double it. */
517 vty_ensure (struct vty
*vty
, int length
)
519 if (vty
->max
<= length
)
522 vty
->buf
= XREALLOC (MTYPE_VTY
, vty
->buf
, vty
->max
);
523 vty
->error_buf
= XREALLOC (MTYPE_VTY
, vty
->error_buf
, vty
->max
);
527 /* Basic function to insert character into vty. */
529 vty_self_insert (struct vty
*vty
, char c
)
534 vty_ensure (vty
, vty
->length
+ 1);
535 length
= vty
->length
- vty
->cp
;
536 memmove (&vty
->buf
[vty
->cp
+ 1], &vty
->buf
[vty
->cp
], length
);
537 vty
->buf
[vty
->cp
] = c
;
539 vty_write (vty
, &vty
->buf
[vty
->cp
], length
+ 1);
540 for (i
= 0; i
< length
; i
++)
541 vty_write (vty
, &telnet_backward_char
, 1);
547 /* Self insert character 'c' in overwrite mode. */
549 vty_self_insert_overwrite (struct vty
*vty
, char c
)
551 vty_ensure (vty
, vty
->length
+ 1);
552 vty
->buf
[vty
->cp
++] = c
;
554 if (vty
->cp
> vty
->length
)
557 if ((vty
->node
== AUTH_NODE
) || (vty
->node
== AUTH_ENABLE_NODE
))
560 vty_write (vty
, &c
, 1);
563 /* Insert a word into vty interface with overwrite mode. */
565 vty_insert_word_overwrite (struct vty
*vty
, char *str
)
567 int len
= strlen (str
);
568 vty_write (vty
, str
, len
);
569 strcpy (&vty
->buf
[vty
->cp
], str
);
571 vty
->length
= vty
->cp
;
574 /* Forward character. */
576 vty_forward_char (struct vty
*vty
)
578 if (vty
->cp
< vty
->length
)
580 vty_write (vty
, &vty
->buf
[vty
->cp
], 1);
585 /* Backward character. */
587 vty_backward_char (struct vty
*vty
)
592 vty_write (vty
, &telnet_backward_char
, 1);
596 /* Move to the beginning of the line. */
598 vty_beginning_of_line (struct vty
*vty
)
601 vty_backward_char (vty
);
604 /* Move to the end of the line. */
606 vty_end_of_line (struct vty
*vty
)
608 while (vty
->cp
< vty
->length
)
609 vty_forward_char (vty
);
612 static void vty_kill_line_from_beginning (struct vty
*);
613 static void vty_redraw_line (struct vty
*);
615 /* Print command line history. This function is called from
616 vty_next_line and vty_previous_line. */
618 vty_history_print (struct vty
*vty
)
622 vty_kill_line_from_beginning (vty
);
624 /* Get previous line from history buffer */
625 length
= strlen (vty
->hist
[vty
->hp
]);
626 memcpy (vty
->buf
, vty
->hist
[vty
->hp
], length
);
627 vty
->cp
= vty
->length
= length
;
629 /* Redraw current line */
630 vty_redraw_line (vty
);
633 /* Show next command line history. */
635 vty_next_line (struct vty
*vty
)
639 if (vty
->hp
== vty
->hindex
)
642 /* Try is there history exist or not. */
644 if (try_index
== (VTY_MAXHIST
- 1))
649 /* If there is not history return. */
650 if (vty
->hist
[try_index
] == NULL
)
655 vty_history_print (vty
);
658 /* Show previous command line history. */
660 vty_previous_line (struct vty
*vty
)
666 try_index
= VTY_MAXHIST
- 1;
670 if (vty
->hist
[try_index
] == NULL
)
675 vty_history_print (vty
);
678 /* This function redraw all of the command line character. */
680 vty_redraw_line (struct vty
*vty
)
682 vty_write (vty
, vty
->buf
, vty
->length
);
683 vty
->cp
= vty
->length
;
688 vty_forward_word (struct vty
*vty
)
690 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
691 vty_forward_char (vty
);
693 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
694 vty_forward_char (vty
);
697 /* Backward word without skipping training space. */
699 vty_backward_pure_word (struct vty
*vty
)
701 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
702 vty_backward_char (vty
);
707 vty_backward_word (struct vty
*vty
)
709 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
710 vty_backward_char (vty
);
712 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
713 vty_backward_char (vty
);
716 /* When '^D' is typed at the beginning of the line we move to the down
719 vty_down_level (struct vty
*vty
)
721 vty_out (vty
, "%s", VTY_NEWLINE
);
722 (*config_exit_cmd
.func
)(NULL
, vty
, 0, NULL
);
727 /* When '^Z' is received from vty, move down to the enable mode. */
729 vty_end_config (struct vty
*vty
)
731 vty_out (vty
, "%s", VTY_NEWLINE
);
737 case RESTRICTED_NODE
:
749 case BGP_ENCAPV6_NODE
:
750 case BGP_VNC_DEFAULTS_NODE
:
751 case BGP_VNC_NVE_GROUP_NODE
:
752 case BGP_VNC_L2_GROUP_NODE
:
763 case LDP_IPV4_IFACE_NODE
:
764 case LDP_IPV6_IFACE_NODE
:
766 case LDP_PSEUDOWIRE_NODE
:
769 case KEYCHAIN_KEY_NODE
:
773 vty_config_unlock (vty
);
774 vty
->node
= ENABLE_NODE
;
777 /* Unknown node, we have to ignore it. */
785 /* Delete a charcter at the current point. */
787 vty_delete_char (struct vty
*vty
)
792 if (vty
->length
== 0)
794 vty_down_level (vty
);
798 if (vty
->cp
== vty
->length
)
799 return; /* completion need here? */
801 size
= vty
->length
- vty
->cp
;
804 memmove (&vty
->buf
[vty
->cp
], &vty
->buf
[vty
->cp
+ 1], size
- 1);
805 vty
->buf
[vty
->length
] = '\0';
807 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
810 vty_write (vty
, &vty
->buf
[vty
->cp
], size
- 1);
811 vty_write (vty
, &telnet_space_char
, 1);
813 for (i
= 0; i
< size
; i
++)
814 vty_write (vty
, &telnet_backward_char
, 1);
817 /* Delete a character before the point. */
819 vty_delete_backward_char (struct vty
*vty
)
824 vty_backward_char (vty
);
825 vty_delete_char (vty
);
828 /* Kill rest of line from current point. */
830 vty_kill_line (struct vty
*vty
)
835 size
= vty
->length
- vty
->cp
;
840 for (i
= 0; i
< size
; i
++)
841 vty_write (vty
, &telnet_space_char
, 1);
842 for (i
= 0; i
< size
; i
++)
843 vty_write (vty
, &telnet_backward_char
, 1);
845 memset (&vty
->buf
[vty
->cp
], 0, size
);
846 vty
->length
= vty
->cp
;
849 /* Kill line from the beginning. */
851 vty_kill_line_from_beginning (struct vty
*vty
)
853 vty_beginning_of_line (vty
);
857 /* Delete a word before the point. */
859 vty_forward_kill_word (struct vty
*vty
)
861 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] == ' ')
862 vty_delete_char (vty
);
863 while (vty
->cp
!= vty
->length
&& vty
->buf
[vty
->cp
] != ' ')
864 vty_delete_char (vty
);
867 /* Delete a word before the point. */
869 vty_backward_kill_word (struct vty
*vty
)
871 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] == ' ')
872 vty_delete_backward_char (vty
);
873 while (vty
->cp
> 0 && vty
->buf
[vty
->cp
- 1] != ' ')
874 vty_delete_backward_char (vty
);
877 /* Transpose chars before or at the point. */
879 vty_transpose_chars (struct vty
*vty
)
883 /* If length is short or point is near by the beginning of line then
885 if (vty
->length
< 2 || vty
->cp
< 1)
888 /* In case of point is located at the end of the line. */
889 if (vty
->cp
== vty
->length
)
891 c1
= vty
->buf
[vty
->cp
- 1];
892 c2
= vty
->buf
[vty
->cp
- 2];
894 vty_backward_char (vty
);
895 vty_backward_char (vty
);
896 vty_self_insert_overwrite (vty
, c1
);
897 vty_self_insert_overwrite (vty
, c2
);
901 c1
= vty
->buf
[vty
->cp
];
902 c2
= vty
->buf
[vty
->cp
- 1];
904 vty_backward_char (vty
);
905 vty_self_insert_overwrite (vty
, c1
);
906 vty_self_insert_overwrite (vty
, c2
);
910 /* Do completion at vty interface. */
912 vty_complete_command (struct vty
*vty
)
916 char **matched
= NULL
;
919 if (vty
->node
== AUTH_NODE
|| vty
->node
== AUTH_ENABLE_NODE
)
922 vline
= cmd_make_strvec (vty
->buf
);
926 /* In case of 'help \t'. */
927 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
928 vector_set (vline
, NULL
);
930 matched
= cmd_complete_command_lib (vline
, vty
, &ret
, 1);
932 cmd_free_strvec (vline
);
934 vty_out (vty
, "%s", VTY_NEWLINE
);
937 case CMD_ERR_AMBIGUOUS
:
938 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
940 vty_redraw_line (vty
);
942 case CMD_ERR_NO_MATCH
:
943 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
945 vty_redraw_line (vty
);
947 case CMD_COMPLETE_FULL_MATCH
:
949 vty_redraw_line (vty
);
950 vty_backward_pure_word (vty
);
951 vty_insert_word_overwrite (vty
, matched
[0]);
952 vty_self_insert (vty
, ' ');
953 XFREE (MTYPE_TMP
, matched
[0]);
955 case CMD_COMPLETE_MATCH
:
957 vty_redraw_line (vty
);
958 vty_backward_pure_word (vty
);
959 vty_insert_word_overwrite (vty
, matched
[0]);
960 XFREE (MTYPE_TMP
, matched
[0]);
961 vector_only_index_free (matched
);
964 case CMD_COMPLETE_LIST_MATCH
:
965 for (i
= 0; matched
[i
] != NULL
; i
++)
967 if (i
!= 0 && ((i
% 6) == 0))
968 vty_out (vty
, "%s", VTY_NEWLINE
);
969 vty_out (vty
, "%-10s ", matched
[i
]);
970 XFREE (MTYPE_TMP
, matched
[i
]);
972 vty_out (vty
, "%s", VTY_NEWLINE
);
975 vty_redraw_line (vty
);
977 case CMD_ERR_NOTHING_TODO
:
979 vty_redraw_line (vty
);
985 vector_only_index_free (matched
);
989 vty_describe_fold (struct vty
*vty
, int cmd_width
,
990 unsigned int desc_width
, struct cmd_token
*token
)
996 cmd
= token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
;
1000 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, token
->desc
, VTY_NEWLINE
);
1004 buf
= XCALLOC (MTYPE_TMP
, strlen (token
->desc
) + 1);
1006 for (p
= token
->desc
; strlen (p
) > desc_width
; p
+= pos
+ 1)
1008 for (pos
= desc_width
; pos
> 0; pos
--)
1009 if (*(p
+ pos
) == ' ')
1015 strncpy (buf
, p
, pos
);
1017 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, buf
, VTY_NEWLINE
);
1022 vty_out (vty
, " %-*s %s%s", cmd_width
, cmd
, p
, VTY_NEWLINE
);
1024 XFREE (MTYPE_TMP
, buf
);
1027 /* Describe matched command function. */
1029 vty_describe_command (struct vty
*vty
)
1034 unsigned int i
, width
, desc_width
;
1035 struct cmd_token
*token
, *token_cr
= NULL
;
1037 vline
= cmd_make_strvec (vty
->buf
);
1039 /* In case of '> ?'. */
1042 vline
= vector_init (1);
1043 vector_set (vline
, NULL
);
1046 if (isspace ((int) vty
->buf
[vty
->length
- 1]))
1047 vector_set (vline
, NULL
);
1049 describe
= cmd_describe_command (vline
, vty
, &ret
);
1051 vty_out (vty
, "%s", VTY_NEWLINE
);
1053 /* Ambiguous error. */
1056 case CMD_ERR_AMBIGUOUS
:
1057 vty_out (vty
, "%% Ambiguous command.%s", VTY_NEWLINE
);
1060 case CMD_ERR_NO_MATCH
:
1061 vty_out (vty
, "%% There is no matched command.%s", VTY_NEWLINE
);
1066 /* Get width of command string. */
1068 for (i
= 0; i
< vector_active (describe
); i
++)
1069 if ((token
= vector_slot (describe
, i
)) != NULL
)
1073 if (token
->cmd
[0] == '\0')
1076 len
= strlen (token
->cmd
);
1077 if (token
->cmd
[0] == '.')
1084 /* Get width of description string. */
1085 desc_width
= vty
->width
- (width
+ 6);
1087 /* Print out description. */
1088 for (i
= 0; i
< vector_active (describe
); i
++)
1089 if ((token
= vector_slot (describe
, i
)) != NULL
)
1091 if (token
->cmd
[0] == '\0')
1094 if (strcmp (token
->cmd
, command_cr
) == 0)
1101 vty_out (vty
, " %-s%s",
1102 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1104 else if (desc_width
>= strlen (token
->desc
))
1105 vty_out (vty
, " %-*s %s%s", width
,
1106 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1107 token
->desc
, VTY_NEWLINE
);
1109 vty_describe_fold (vty
, width
, desc_width
, token
);
1112 vty_out (vty
, " %-*s %s%s", width
1113 desc
->cmd
[0] == '.' ? desc
->cmd
+ 1 : desc
->cmd
,
1114 desc
->str
? desc
->str
: "", VTY_NEWLINE
);
1118 if ((token
= token_cr
))
1121 vty_out (vty
, " %-s%s",
1122 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1124 else if (desc_width
>= strlen (token
->desc
))
1125 vty_out (vty
, " %-*s %s%s", width
,
1126 token
->cmd
[0] == '.' ? token
->cmd
+ 1 : token
->cmd
,
1127 token
->desc
, VTY_NEWLINE
);
1129 vty_describe_fold (vty
, width
, desc_width
, token
);
1133 cmd_free_strvec (vline
);
1135 vector_free (describe
);
1138 vty_redraw_line (vty
);
1142 vty_clear_buf (struct vty
*vty
)
1144 memset (vty
->buf
, 0, vty
->max
);
1147 /* ^C stop current input and do not add command line to the history. */
1149 vty_stop_input (struct vty
*vty
)
1151 vty
->cp
= vty
->length
= 0;
1152 vty_clear_buf (vty
);
1153 vty_out (vty
, "%s", VTY_NEWLINE
);
1159 case RESTRICTED_NODE
:
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
)
1719 if (restricted_mode
)
1720 vty
->node
= RESTRICTED_NODE
;
1721 else if (host
.advanced
)
1722 vty
->node
= ENABLE_NODE
;
1724 vty
->node
= VIEW_NODE
;
1726 if (host
.lines
>= 0)
1727 vty
->lines
= host
.lines
;
1729 if (! no_password_check
)
1731 /* Vty is not available if password isn't set. */
1732 if (host
.password
== NULL
&& host
.password_encrypt
== NULL
)
1734 vty_out (vty
, "Vty password is not set.%s", VTY_NEWLINE
);
1735 vty
->status
= VTY_CLOSE
;
1741 /* Say hello to the world. */
1743 if (! no_password_check
)
1744 vty_out (vty
, "%sUser Access Verification%s%s", VTY_NEWLINE
, VTY_NEWLINE
, VTY_NEWLINE
);
1746 /* Setting up terminal. */
1747 vty_will_echo (vty
);
1748 vty_will_suppress_go_ahead (vty
);
1750 vty_dont_linemode (vty
);
1751 vty_do_window_size (vty
);
1752 /* vty_dont_lflow_ahead (vty); */
1756 /* Add read/write thread. */
1757 vty_event (VTY_WRITE
, vty_sock
, vty
);
1758 vty_event (VTY_READ
, vty_sock
, vty
);
1763 /* create vty for stdio */
1764 static struct termios stdio_orig_termios
;
1765 static struct vty
*stdio_vty
= NULL
;
1766 static void (*stdio_vty_atclose
)(void);
1769 vty_stdio_reset (void)
1773 tcsetattr (0, TCSANOW
, &stdio_orig_termios
);
1776 if (stdio_vty_atclose
)
1777 stdio_vty_atclose ();
1778 stdio_vty_atclose
= NULL
;
1783 vty_stdio (void (*atclose
)())
1786 struct termios termios
;
1788 /* refuse creating two vtys on stdio */
1792 vty
= stdio_vty
= vty_new_init (0);
1793 stdio_vty_atclose
= atclose
;
1796 /* always have stdio vty in a known _unchangeable_ state, don't want config
1797 * to have any effect here to make sure scripting this works as intended */
1798 vty
->node
= ENABLE_NODE
;
1800 strcpy (vty
->address
, "console");
1802 if (!tcgetattr (0, &stdio_orig_termios
))
1804 termios
= stdio_orig_termios
;
1805 termios
.c_iflag
&= ~(IGNBRK
| BRKINT
| PARMRK
| ISTRIP
1806 | INLCR
| IGNCR
| ICRNL
| IXON
);
1807 termios
.c_oflag
&= ~OPOST
;
1808 termios
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| ISIG
| IEXTEN
);
1809 termios
.c_cflag
&= ~(CSIZE
| PARENB
);
1810 termios
.c_cflag
|= CS8
;
1811 tcsetattr (0, TCSANOW
, &termios
);
1816 /* Add read/write thread. */
1817 vty_event (VTY_WRITE
, 1, vty
);
1818 vty_event (VTY_READ
, 0, vty
);
1823 /* Accept connection from the network. */
1825 vty_accept (struct thread
*thread
)
1833 struct access_list
*acl
= NULL
;
1834 char buf
[SU_ADDRSTRLEN
];
1836 accept_sock
= THREAD_FD (thread
);
1838 /* We continue hearing vty socket. */
1839 vty_event (VTY_SERV
, accept_sock
, NULL
);
1841 memset (&su
, 0, sizeof (union sockunion
));
1843 /* We can handle IPv4 or IPv6 socket. */
1844 vty_sock
= sockunion_accept (accept_sock
, &su
);
1847 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno
));
1850 set_nonblocking(vty_sock
);
1852 sockunion2hostprefix (&su
, &p
);
1854 /* VTY's accesslist apply. */
1855 if (p
.family
== AF_INET
&& vty_accesslist_name
)
1857 if ((acl
= access_list_lookup (AFI_IP
, vty_accesslist_name
)) &&
1858 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1860 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1861 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1864 /* continue accepting connections */
1865 vty_event (VTY_SERV
, accept_sock
, NULL
);
1872 /* VTY's ipv6 accesslist apply. */
1873 if (p
.family
== AF_INET6
&& vty_ipv6_accesslist_name
)
1875 if ((acl
= access_list_lookup (AFI_IP6
, vty_ipv6_accesslist_name
)) &&
1876 (access_list_apply (acl
, &p
) == FILTER_DENY
))
1878 zlog (NULL
, LOG_INFO
, "Vty connection refused from %s",
1879 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1882 /* continue accepting connections */
1883 vty_event (VTY_SERV
, accept_sock
, NULL
);
1888 #endif /* HAVE_IPV6 */
1891 ret
= setsockopt (vty_sock
, IPPROTO_TCP
, TCP_NODELAY
,
1892 (char *) &on
, sizeof (on
));
1894 zlog (NULL
, LOG_INFO
, "can't set sockopt to vty_sock : %s",
1895 safe_strerror (errno
));
1897 zlog (NULL
, LOG_INFO
, "Vty connection from %s",
1898 sockunion2str (&su
, buf
, SU_ADDRSTRLEN
));
1900 vty_create (vty_sock
, &su
);
1907 vty_serv_sock_addrinfo (const char *hostname
, unsigned short port
)
1910 struct addrinfo req
;
1911 struct addrinfo
*ainfo
;
1912 struct addrinfo
*ainfo_save
;
1914 char port_str
[BUFSIZ
];
1916 memset (&req
, 0, sizeof (struct addrinfo
));
1917 req
.ai_flags
= AI_PASSIVE
;
1918 req
.ai_family
= AF_UNSPEC
;
1919 req
.ai_socktype
= SOCK_STREAM
;
1920 sprintf (port_str
, "%d", port
);
1921 port_str
[sizeof (port_str
) - 1] = '\0';
1923 ret
= getaddrinfo (hostname
, port_str
, &req
, &ainfo
);
1927 fprintf (stderr
, "getaddrinfo failed: %s\n", gai_strerror (ret
));
1935 if (ainfo
->ai_family
!= AF_INET
1937 && ainfo
->ai_family
!= AF_INET6
1938 #endif /* HAVE_IPV6 */
1942 sock
= socket (ainfo
->ai_family
, ainfo
->ai_socktype
, ainfo
->ai_protocol
);
1946 sockopt_v6only (ainfo
->ai_family
, sock
);
1947 sockopt_reuseaddr (sock
);
1948 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
);
2017 /* Bind socket to universal address and given port. */
2018 ret
= sockunion_bind (accept_sock
, &su
, port
, naddr
);
2021 zlog_warn("can't bind socket");
2022 close (accept_sock
); /* Avoid sd leak. */
2026 /* Listen socket under queue 3. */
2027 ret
= listen (accept_sock
, 3);
2030 zlog (NULL
, LOG_WARNING
, "can't listen socket");
2031 close (accept_sock
); /* Avoid sd leak. */
2035 /* Add vty server event. */
2036 vty_event (VTY_SERV
, accept_sock
, NULL
);
2038 #endif /* HAVE_IPV6 */
2041 /* For sockaddr_un. */
2044 /* VTY shell UNIX domain socket. */
2046 vty_serv_un (const char *path
)
2050 struct sockaddr_un serv
;
2052 struct zprivs_ids_t ids
;
2054 /* First of all, unlink existing socket */
2058 old_mask
= umask (0007);
2060 /* Make UNIX domain socket. */
2061 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
2064 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno
));
2068 /* Make server socket. */
2069 memset (&serv
, 0, sizeof (struct sockaddr_un
));
2070 serv
.sun_family
= AF_UNIX
;
2071 strncpy (serv
.sun_path
, path
, strlen (path
));
2072 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2073 len
= serv
.sun_len
= SUN_LEN(&serv
);
2075 len
= sizeof (serv
.sun_family
) + strlen (serv
.sun_path
);
2076 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2078 ret
= bind (sock
, (struct sockaddr
*) &serv
, len
);
2081 zlog_err("Cannot bind path %s: %s", path
, safe_strerror(errno
));
2082 close (sock
); /* Avoid sd leak. */
2086 ret
= listen (sock
, 5);
2089 zlog_err("listen(fd %d) failed: %s", sock
, safe_strerror(errno
));
2090 close (sock
); /* Avoid sd leak. */
2096 zprivs_get_ids(&ids
);
2098 if (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
));
2147 printf ("VTY shell accept\n");
2148 #endif /* VTYSH_DEBUG */
2153 vty
->type
= VTY_SHELL_SERV
;
2154 vty
->node
= VIEW_NODE
;
2156 vty_event (VTYSH_READ
, sock
, vty
);
2162 vtysh_flush(struct vty
*vty
)
2164 switch (buffer_flush_available(vty
->obuf
, vty
->wfd
))
2166 case BUFFER_PENDING
:
2167 vty_event(VTYSH_WRITE
, vty
->wfd
, vty
);
2170 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2171 zlog_warn("%s: write error to fd %d, closing", __func__
, vty
->fd
);
2172 buffer_reset(vty
->obuf
);
2183 vtysh_read (struct thread
*thread
)
2189 unsigned char buf
[VTY_READ_BUFSIZ
];
2191 u_char header
[4] = {0, 0, 0, 0};
2193 sock
= THREAD_FD (thread
);
2194 vty
= THREAD_ARG (thread
);
2197 if ((nbytes
= read (sock
, buf
, VTY_READ_BUFSIZ
)) <= 0)
2201 if (ERRNO_IO_RETRY(errno
))
2203 vty_event (VTYSH_READ
, sock
, vty
);
2206 vty
->monitor
= 0; /* disable monitoring to avoid infinite recursion */
2207 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2208 __func__
, sock
, safe_strerror(errno
));
2210 buffer_reset(vty
->obuf
);
2213 printf ("close vtysh\n");
2214 #endif /* VTYSH_DEBUG */
2219 printf ("line: %.*s\n", nbytes
, buf
);
2220 #endif /* VTYSH_DEBUG */
2222 for (p
= buf
; p
< buf
+nbytes
; p
++)
2224 vty_ensure(vty
, vty
->length
+1);
2225 vty
->buf
[vty
->length
++] = *p
;
2228 /* Pass this line to parser. */
2229 ret
= vty_execute (vty
);
2230 /* Note that vty_execute clears the command buffer and resets
2231 vty->length to 0. */
2233 /* Return result. */
2235 printf ("result: %d\n", ret
);
2236 printf ("vtysh node: %d\n", vty
->node
);
2237 #endif /* VTYSH_DEBUG */
2240 buffer_put(vty
->obuf
, header
, 4);
2242 if (!vty
->t_write
&& (vtysh_flush(vty
) < 0))
2243 /* Try to flush results; exit if a write error occurs. */
2248 vty_event (VTYSH_READ
, sock
, vty
);
2254 vtysh_write (struct thread
*thread
)
2256 struct vty
*vty
= THREAD_ARG (thread
);
2258 vty
->t_write
= NULL
;
2265 /* Determine address family to bind. */
2267 vty_serv_sock (const char *addr
, unsigned short port
, const char *path
)
2269 /* If port is set to 0, do not listen on TCP/IP at all! */
2274 vty_serv_sock_addrinfo (addr
, port
);
2275 #else /* ! HAVE_IPV6 */
2276 vty_serv_sock_family (addr
,port
, AF_INET
);
2277 #endif /* HAVE_IPV6 */
2285 /* Close vty interface. Warning: call this only from functions that
2286 will be careful not to access the vty afterwards (since it has
2287 now been freed). This is safest from top-level functions (called
2288 directly by the thread dispatcher). */
2290 vty_close (struct vty
*vty
)
2294 /* Cancel threads.*/
2296 thread_cancel (vty
->t_read
);
2298 thread_cancel (vty
->t_write
);
2300 thread_cancel (vty
->t_timeout
);
2303 buffer_flush_all (vty
->obuf
, vty
->wfd
);
2305 /* Free input buffer. */
2306 buffer_free (vty
->obuf
);
2308 /* Free command history. */
2309 for (i
= 0; i
< VTY_MAXHIST
; i
++)
2311 XFREE (MTYPE_VTY_HIST
, vty
->hist
[i
]);
2314 vector_unset (vtyvec
, vty
->fd
);
2323 XFREE (MTYPE_VTY
, vty
->buf
);
2326 XFREE (MTYPE_VTY
, vty
->error_buf
);
2328 /* Check configure. */
2329 vty_config_unlock (vty
);
2332 XFREE (MTYPE_VTY
, vty
);
2335 /* When time out occur output message then close connection. */
2337 vty_timeout (struct thread
*thread
)
2341 vty
= THREAD_ARG (thread
);
2342 vty
->t_timeout
= NULL
;
2346 buffer_reset (vty
->obuf
);
2347 vty_out (vty
, "%sVty connection is timed out.%s", VTY_NEWLINE
, VTY_NEWLINE
);
2349 /* Close connection. */
2350 vty
->status
= VTY_CLOSE
;
2356 /* Read up configuration file from file_name. */
2358 vty_read_file (FILE *confp
)
2362 unsigned int line_num
= 0;
2365 vty
->wfd
= dup(STDERR_FILENO
); /* vty_close() will close this */
2368 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2369 vty
->wfd
= STDOUT_FILENO
;
2371 vty
->fd
= STDIN_FILENO
;
2372 vty
->type
= VTY_FILE
;
2373 vty
->node
= CONFIG_NODE
;
2375 /* Execute configuration file */
2376 ret
= config_from_file (vty
, confp
, &line_num
);
2378 /* Flush any previous errors before printing messages below */
2379 buffer_flush_all (vty
->obuf
, vty
->fd
);
2381 if ( !((ret
== CMD_SUCCESS
) || (ret
== CMD_ERR_NOTHING_TODO
)) )
2385 case CMD_ERR_AMBIGUOUS
:
2386 fprintf (stderr
, "*** Error reading config: Ambiguous command.\n");
2388 case CMD_ERR_NO_MATCH
:
2389 fprintf (stderr
, "*** Error reading config: There is no such command.\n");
2392 fprintf (stderr
, "*** Error occured processing line %u, below:\n%s\n",
2393 line_num
, vty
->error_buf
);
2400 vty_use_backup_config (char *fullpath
)
2402 char *fullpath_sav
, *fullpath_tmp
;
2409 fullpath_sav
= malloc (strlen (fullpath
) + strlen (CONF_BACKUP_EXT
) + 1);
2410 strcpy (fullpath_sav
, fullpath
);
2411 strcat (fullpath_sav
, CONF_BACKUP_EXT
);
2412 if (stat (fullpath_sav
, &buf
) == -1)
2414 free (fullpath_sav
);
2418 fullpath_tmp
= malloc (strlen (fullpath
) + 8);
2419 sprintf (fullpath_tmp
, "%s.XXXXXX", fullpath
);
2421 /* Open file to configuration write. */
2422 tmp
= mkstemp (fullpath_tmp
);
2425 free (fullpath_sav
);
2426 free (fullpath_tmp
);
2430 sav
= open (fullpath_sav
, O_RDONLY
);
2433 unlink (fullpath_tmp
);
2434 free (fullpath_sav
);
2435 free (fullpath_tmp
);
2439 while((c
= read (sav
, buffer
, 512)) > 0)
2441 if (write (tmp
, buffer
, c
) <= 0)
2443 free (fullpath_sav
);
2444 free (fullpath_tmp
);
2453 if (chmod(fullpath_tmp
, CONFIGFILE_MASK
) != 0)
2455 unlink (fullpath_tmp
);
2456 free (fullpath_sav
);
2457 free (fullpath_tmp
);
2461 if (link (fullpath_tmp
, fullpath
) == 0)
2462 ret
= fopen (fullpath
, "r");
2464 unlink (fullpath_tmp
);
2466 free (fullpath_sav
);
2467 free (fullpath_tmp
);
2471 /* Read up configuration file from file_name. */
2473 vty_read_config (char *config_file
,
2474 char *config_default_dir
)
2476 char cwd
[MAXPATHLEN
];
2481 /* If -f flag specified. */
2482 if (config_file
!= NULL
)
2484 if (! IS_DIRECTORY_SEP (config_file
[0]))
2486 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
2488 fprintf (stderr
, "Failure to determine Current Working Directory %d!\n", errno
);
2491 tmp
= XMALLOC (MTYPE_TMP
,
2492 strlen (cwd
) + strlen (config_file
) + 2);
2493 sprintf (tmp
, "%s/%s", cwd
, config_file
);
2497 fullpath
= config_file
;
2499 confp
= fopen (fullpath
, "r");
2503 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2504 __func__
, fullpath
, safe_strerror (errno
));
2506 confp
= vty_use_backup_config (fullpath
);
2508 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2511 fprintf (stderr
, "can't open configuration file [%s]\n",
2520 host_config_set (config_default_dir
);
2524 struct stat conf_stat
;
2526 /* !!!!PLEASE LEAVE!!!!
2527 * This is NEEDED for use with vtysh -b, or else you can get
2528 * a real configuration food fight with a lot garbage in the
2529 * merged configuration file it creates coming from the per
2530 * daemon configuration files. This also allows the daemons
2531 * to start if there default configuration file is not
2532 * present or ignore them, as needed when using vtysh -b to
2533 * configure the daemons at boot - MAG
2536 /* Stat for vtysh Zebra.conf, if found startup and wait for
2537 * boot configuration
2540 if ( strstr(config_default_dir
, "vtysh") == NULL
)
2542 ret
= stat (integrate_default
, &conf_stat
);
2544 goto tmp_free_and_out
;
2547 confp
= fopen (config_default_dir
, "r");
2550 fprintf (stderr
, "%s: failed to open configuration file %s: %s\n",
2551 __func__
, config_default_dir
, safe_strerror (errno
));
2553 confp
= vty_use_backup_config (config_default_dir
);
2556 fprintf (stderr
, "WARNING: using backup configuration file!\n");
2557 fullpath
= config_default_dir
;
2561 fprintf (stderr
, "can't open configuration file [%s]\n",
2562 config_default_dir
);
2563 goto tmp_free_and_out
;
2567 fullpath
= config_default_dir
;
2570 vty_read_file (confp
);
2574 host_config_set (fullpath
);
2578 XFREE (MTYPE_TMP
, fullpath
);
2581 /* Small utility function which output log to the VTY. */
2583 vty_log (const char *level
, const char *proto_str
,
2584 const char *format
, struct timestamp_control
*ctl
, va_list va
)
2592 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2593 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
2598 vty_log_out (vty
, level
, proto_str
, format
, ctl
, ac
);
2603 /* Async-signal-safe version of vty_log for fixed strings. */
2605 vty_log_fixed (char *buf
, size_t len
)
2608 struct iovec iov
[2];
2609 char crlf
[4] = "\r\n";
2611 /* vty may not have been initialised */
2615 iov
[0].iov_base
= buf
;
2616 iov
[0].iov_len
= len
;
2617 iov
[1].iov_base
= crlf
;
2620 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2623 if (((vty
= vector_slot (vtyvec
, i
)) != NULL
) && vty
->monitor
)
2624 /* N.B. We don't care about the return code, since process is
2625 most likely just about to die anyway. */
2626 if (writev(vty
->wfd
, iov
, 2) == -1)
2628 fprintf(stderr
, "Failure to writev: %d\n", errno
);
2635 vty_config_lock (struct vty
*vty
)
2637 if (vty_config
== 0)
2646 vty_config_unlock (struct vty
*vty
)
2648 if (vty_config
== 1 && vty
->config
== 1)
2656 /* Master of the threads. */
2657 static struct thread_master
*vty_master
;
2660 vty_event (enum event event
, int sock
, struct vty
*vty
)
2662 struct thread
*vty_serv_thread
;
2667 vty_serv_thread
= thread_add_read (vty_master
, vty_accept
, vty
, sock
);
2668 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2672 vty_serv_thread
= thread_add_read (vty_master
, vtysh_accept
, vty
, sock
);
2673 vector_set_index (Vvty_serv_thread
, sock
, vty_serv_thread
);
2676 vty
->t_read
= thread_add_read (vty_master
, vtysh_read
, vty
, sock
);
2679 vty
->t_write
= thread_add_write (vty_master
, vtysh_write
, vty
, sock
);
2683 vty
->t_read
= thread_add_read (vty_master
, vty_read
, vty
, sock
);
2685 /* Time out treatment. */
2689 thread_cancel (vty
->t_timeout
);
2691 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2696 vty
->t_write
= thread_add_write (vty_master
, vty_flush
, vty
, sock
);
2698 case VTY_TIMEOUT_RESET
:
2701 thread_cancel (vty
->t_timeout
);
2702 vty
->t_timeout
= NULL
;
2707 thread_add_timer (vty_master
, vty_timeout
, vty
, vty
->v_timeout
);
2716 "Display who is on vty\n")
2721 for (i
= 0; i
< vector_active (vtyvec
); i
++)
2722 if ((v
= vector_slot (vtyvec
, i
)) != NULL
)
2723 vty_out (vty
, "%svty[%d] connected from %s.%s",
2724 v
->config
? "*" : " ",
2725 i
, v
->address
, VTY_NEWLINE
);
2729 /* Move to vty configuration mode. */
2733 "Configure a terminal line\n"
2734 "Virtual terminal\n")
2736 vty
->node
= VTY_NODE
;
2740 /* Set time out value. */
2742 exec_timeout (struct vty
*vty
, const char *min_str
, const char *sec_str
)
2744 unsigned long timeout
= 0;
2746 /* min_str and sec_str are already checked by parser. So it must be
2747 all digit string. */
2750 timeout
= strtol (min_str
, NULL
, 10);
2754 timeout
+= strtol (sec_str
, NULL
, 10);
2756 vty_timeout_val
= timeout
;
2757 vty
->v_timeout
= timeout
;
2758 vty_event (VTY_TIMEOUT_RESET
, 0, vty
);
2764 DEFUN (exec_timeout_min
,
2765 exec_timeout_min_cmd
,
2766 "exec-timeout <0-35791>",
2767 "Set timeout value\n"
2768 "Timeout value in minutes\n")
2770 return exec_timeout (vty
, argv
[0], NULL
);
2773 DEFUN (exec_timeout_sec
,
2774 exec_timeout_sec_cmd
,
2775 "exec-timeout <0-35791> <0-2147483>",
2776 "Set the EXEC timeout\n"
2777 "Timeout in minutes\n"
2778 "Timeout in seconds\n")
2780 return exec_timeout (vty
, argv
[0], argv
[1]);
2783 DEFUN (no_exec_timeout
,
2784 no_exec_timeout_cmd
,
2787 "Set the EXEC timeout\n")
2789 return exec_timeout (vty
, NULL
, NULL
);
2792 /* Set vty access class. */
2793 DEFUN (vty_access_class
,
2794 vty_access_class_cmd
,
2795 "access-class WORD",
2796 "Filter connections based on an IP access list\n"
2799 if (vty_accesslist_name
)
2800 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2802 vty_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2807 /* Clear vty access class. */
2808 DEFUN (no_vty_access_class
,
2809 no_vty_access_class_cmd
,
2810 "no access-class [WORD]",
2812 "Filter connections based on an IP access list\n"
2815 if (! vty_accesslist_name
|| (argc
&& strcmp(vty_accesslist_name
, argv
[0])))
2817 vty_out (vty
, "Access-class is not currently applied to vty%s",
2822 XFREE(MTYPE_VTY
, vty_accesslist_name
);
2824 vty_accesslist_name
= NULL
;
2830 /* Set vty access class. */
2831 DEFUN (vty_ipv6_access_class
,
2832 vty_ipv6_access_class_cmd
,
2833 "ipv6 access-class WORD",
2835 "Filter connections based on an IP access list\n"
2836 "IPv6 access list\n")
2838 if (vty_ipv6_accesslist_name
)
2839 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2841 vty_ipv6_accesslist_name
= XSTRDUP(MTYPE_VTY
, argv
[0]);
2846 /* Clear vty access class. */
2847 DEFUN (no_vty_ipv6_access_class
,
2848 no_vty_ipv6_access_class_cmd
,
2849 "no ipv6 access-class [WORD]",
2852 "Filter connections based on an IP access list\n"
2853 "IPv6 access list\n")
2855 if (! vty_ipv6_accesslist_name
||
2856 (argc
&& strcmp(vty_ipv6_accesslist_name
, argv
[0])))
2858 vty_out (vty
, "IPv6 access-class is not currently applied to vty%s",
2863 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
2865 vty_ipv6_accesslist_name
= NULL
;
2869 #endif /* HAVE_IPV6 */
2875 "Enable password checking\n")
2877 no_password_check
= 0;
2881 DEFUN (no_vty_login
,
2885 "Enable password checking\n")
2887 no_password_check
= 1;
2892 DEFUN (vty_restricted_mode
,
2893 vty_restricted_mode_cmd
,
2894 "anonymous restricted",
2895 "Restrict view commands available in anonymous, unauthenticated vty\n")
2897 restricted_mode
= 1;
2901 DEFUN (vty_no_restricted_mode
,
2902 vty_no_restricted_mode_cmd
,
2903 "no anonymous restricted",
2905 "Enable password checking\n")
2907 restricted_mode
= 0;
2911 DEFUN (service_advanced_vty
,
2912 service_advanced_vty_cmd
,
2913 "service advanced-vty",
2914 "Set up miscellaneous service\n"
2915 "Enable advanced mode vty interface\n")
2921 DEFUN (no_service_advanced_vty
,
2922 no_service_advanced_vty_cmd
,
2923 "no service advanced-vty",
2925 "Set up miscellaneous service\n"
2926 "Enable advanced mode vty interface\n")
2932 DEFUN (terminal_monitor
,
2933 terminal_monitor_cmd
,
2935 "Set terminal line parameters\n"
2936 "Copy debug output to the current terminal line\n")
2942 DEFUN (terminal_no_monitor
,
2943 terminal_no_monitor_cmd
,
2944 "terminal no monitor",
2945 "Set terminal line parameters\n"
2947 "Copy debug output to the current terminal line\n")
2953 ALIAS (terminal_no_monitor
,
2954 no_terminal_monitor_cmd
,
2955 "no terminal monitor",
2957 "Set terminal line parameters\n"
2958 "Copy debug output to the current terminal line\n")
2960 DEFUN (show_history
,
2964 "Display the session command history\n")
2968 for (index
= vty
->hindex
+ 1; index
!= vty
->hindex
;)
2970 if (index
== VTY_MAXHIST
)
2976 if (vty
->hist
[index
] != NULL
)
2977 vty_out (vty
, " %s%s", vty
->hist
[index
], VTY_NEWLINE
);
2986 DEFUN (log_commands
,
2990 "Log all commands (can't be unset without restart)\n")
2992 do_log_commands
= 1;
2996 /* Display current configuration. */
2998 vty_config_write (struct vty
*vty
)
3000 vty_out (vty
, "line vty%s", VTY_NEWLINE
);
3002 if (vty_accesslist_name
)
3003 vty_out (vty
, " access-class %s%s",
3004 vty_accesslist_name
, VTY_NEWLINE
);
3006 if (vty_ipv6_accesslist_name
)
3007 vty_out (vty
, " ipv6 access-class %s%s",
3008 vty_ipv6_accesslist_name
, VTY_NEWLINE
);
3011 if (vty_timeout_val
!= VTY_TIMEOUT_DEFAULT
)
3012 vty_out (vty
, " exec-timeout %ld %ld%s",
3013 vty_timeout_val
/ 60,
3014 vty_timeout_val
% 60, VTY_NEWLINE
);
3017 if (no_password_check
)
3018 vty_out (vty
, " no login%s", VTY_NEWLINE
);
3020 if (restricted_mode
!= restricted_mode_default
)
3022 if (restricted_mode_default
)
3023 vty_out (vty
, " no anonymous restricted%s", VTY_NEWLINE
);
3025 vty_out (vty
, " anonymous restricted%s", VTY_NEWLINE
);
3028 if (do_log_commands
)
3029 vty_out (vty
, "log commands%s", VTY_NEWLINE
);
3031 vty_out (vty
, "!%s", VTY_NEWLINE
);
3036 struct cmd_node vty_node
=
3039 "%s(config-line)# ",
3043 /* Reset all VTY status. */
3049 struct thread
*vty_serv_thread
;
3051 for (i
= 0; i
< vector_active (vtyvec
); i
++)
3052 if ((vty
= vector_slot (vtyvec
, i
)) != NULL
)
3054 buffer_reset (vty
->obuf
);
3055 vty
->status
= VTY_CLOSE
;
3059 for (i
= 0; i
< vector_active (Vvty_serv_thread
); i
++)
3060 if ((vty_serv_thread
= vector_slot (Vvty_serv_thread
, i
)) != NULL
)
3062 thread_cancel (vty_serv_thread
);
3063 vector_slot (Vvty_serv_thread
, i
) = NULL
;
3067 vty_timeout_val
= VTY_TIMEOUT_DEFAULT
;
3069 if (vty_accesslist_name
)
3071 XFREE(MTYPE_VTY
, vty_accesslist_name
);
3072 vty_accesslist_name
= NULL
;
3075 if (vty_ipv6_accesslist_name
)
3077 XFREE(MTYPE_VTY
, vty_ipv6_accesslist_name
);
3078 vty_ipv6_accesslist_name
= NULL
;
3085 char cwd
[MAXPATHLEN
];
3088 c
= getcwd (cwd
, MAXPATHLEN
);
3093 * At this point if these go wrong, more than likely
3094 * the whole world is coming down around us
3095 * Hence not worrying about it too much.
3097 if (!chdir (SYSCONFDIR
))
3099 fprintf(stderr
, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR
, errno
);
3102 if (getcwd (cwd
, MAXPATHLEN
) == NULL
)
3104 fprintf(stderr
, "Failure to getcwd, errno: %d\n", errno
);
3109 vty_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
3110 strcpy (vty_cwd
, cwd
);
3120 vty_shell (struct vty
*vty
)
3122 return vty
->type
== VTY_SHELL
? 1 : 0;
3126 vty_shell_serv (struct vty
*vty
)
3128 return vty
->type
== VTY_SHELL_SERV
? 1 : 0;
3134 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3137 /* Install vty's own commands like `who' command. */
3139 vty_init (struct thread_master
*master_thread
)
3141 /* For further configuration read, preserve current directory. */
3144 vtyvec
= vector_init (VECTOR_MIN_SIZE
);
3146 vty_master
= master_thread
;
3148 atexit (vty_stdio_reset
);
3150 /* Initilize server thread vector. */
3151 Vvty_serv_thread
= vector_init (VECTOR_MIN_SIZE
);
3153 /* Install bgp top node. */
3154 install_node (&vty_node
, vty_config_write
);
3156 install_element (RESTRICTED_NODE
, &config_who_cmd
);
3157 install_element (RESTRICTED_NODE
, &show_history_cmd
);
3158 install_element (VIEW_NODE
, &config_who_cmd
);
3159 install_element (VIEW_NODE
, &show_history_cmd
);
3160 install_element (ENABLE_NODE
, &config_who_cmd
);
3161 install_element (CONFIG_NODE
, &line_vty_cmd
);
3162 install_element (CONFIG_NODE
, &service_advanced_vty_cmd
);
3163 install_element (CONFIG_NODE
, &no_service_advanced_vty_cmd
);
3164 install_element (CONFIG_NODE
, &show_history_cmd
);
3165 install_element (CONFIG_NODE
, &log_commands_cmd
);
3166 install_element (ENABLE_NODE
, &terminal_monitor_cmd
);
3167 install_element (ENABLE_NODE
, &terminal_no_monitor_cmd
);
3168 install_element (ENABLE_NODE
, &no_terminal_monitor_cmd
);
3169 install_element (ENABLE_NODE
, &show_history_cmd
);
3171 install_default (VTY_NODE
);
3172 install_element (VTY_NODE
, &exec_timeout_min_cmd
);
3173 install_element (VTY_NODE
, &exec_timeout_sec_cmd
);
3174 install_element (VTY_NODE
, &no_exec_timeout_cmd
);
3175 install_element (VTY_NODE
, &vty_access_class_cmd
);
3176 install_element (VTY_NODE
, &no_vty_access_class_cmd
);
3177 install_element (VTY_NODE
, &vty_login_cmd
);
3178 install_element (VTY_NODE
, &no_vty_login_cmd
);
3179 install_element (VTY_NODE
, &vty_restricted_mode_cmd
);
3180 install_element (VTY_NODE
, &vty_no_restricted_mode_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
);
3201 /* Utility functions to get arguments from commands generated
3202 by the xml2cli.pl script. */
3204 vty_get_arg_value (struct vty_arg
*args
[], const char *arg
)
3208 if (strcmp ((*args
)->name
, arg
) == 0)
3209 return (*args
)->value
;
3216 vty_get_arg (struct vty_arg
*args
[], const char *arg
)
3220 if (strcmp ((*args
)->name
, arg
) == 0)