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 VTY_BUFSIZ 4096
59 static void vty_event (enum event, int, struct vty *);
61 /* Extern host structure from command.c */
62 extern struct host host;
64 /* Vector which store each vty structure. */
67 /* Vty timeout value. */
68 static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
70 /* Vty access-class command */
71 static char *vty_accesslist_name = NULL;
73 /* Vty access-calss for IPv6. */
74 static char *vty_ipv6_accesslist_name = NULL;
76 /* VTY server thread. */
77 static vector Vvty_serv_thread;
79 /* Current directory. */
83 static int vty_config;
85 /* Login password check. */
86 static int no_password_check = 0;
88 /* Restrict unauthenticated logins? */
89 static const u_char restricted_mode_default = 0;
90 static u_char restricted_mode = 0;
92 /* Integrated configuration file path */
93 char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
95 static int do_log_commands = 0;
98 vty_buf_assert (struct vty *vty)
100 assert (vty->cp <= vty->length);
101 assert (vty->length < vty->max);
102 assert (vty->buf[vty->length] == '\0');
105 /* Sanity/safety wrappers around access to vty->buf */
107 vty_buf_put (struct vty *vty, char c)
109 vty_buf_assert (vty);
110 vty->buf[vty->cp] = c;
111 vty->buf[vty->max - 1] = '\0';
114 /* VTY standard output function. */
116 vty_out (struct vty *vty, const char *format, ...)
126 va_start (args, format);
127 vprintf (format, args);
132 /* Try to write to initial buffer. */
133 va_start (args, format);
134 len = vsnprintf (buf, sizeof(buf), format, args);
137 /* Initial buffer is not enough. */
138 if (len < 0 || len >= size)
147 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
151 va_start (args, format);
152 len = vsnprintf (p, size, format, args);
155 if (len > -1 && len < size)
160 /* When initial buffer is enough to store all output. */
164 /* Pointer p must point out buffer. */
165 buffer_put (vty->obuf, (u_char *) p, len);
167 /* If p is not different with buf, it is allocated buffer. */
169 XFREE (MTYPE_VTY_OUT_BUF, p);
176 vty_log_out (struct vty *vty, const char *level, const char *proto_str,
177 const char *format, struct timestamp_control *ctl, va_list va)
183 if (!ctl->already_rendered)
185 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
186 ctl->already_rendered = 1;
188 if (ctl->len+1 >= sizeof(buf))
190 memcpy(buf, ctl->buf, len = ctl->len);
195 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
197 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
198 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
201 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
202 ((size_t)((len += ret)+2) > sizeof(buf)))
208 if (write(vty->wfd, buf, len) < 0)
210 if (ERRNO_IO_RETRY(errno))
211 /* Kernel buffer is full, probably too much debugging output, so just
212 drop the data and ignore. */
214 /* Fatal I/O error. */
215 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
216 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
217 __func__, vty->fd, safe_strerror(errno));
218 buffer_reset(vty->obuf);
219 /* cannot call vty_close, because a parent routine may still try
220 to access the vty struct */
221 vty->status = VTY_CLOSE;
222 shutdown(vty->fd, SHUT_RDWR);
228 /* Output current time to the vty. */
230 vty_time_print (struct vty *vty, int cr)
232 char buf[QUAGGA_TIMESTAMP_LEN];
234 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
236 zlog (NULL, LOG_INFO, "quagga_timestamp error");
240 vty_out (vty, "%s\n", buf);
242 vty_out (vty, "%s ", buf);
247 /* Say hello to vty interface. */
249 vty_hello (struct vty *vty)
256 f = fopen (host.motdfile, "r");
259 while (fgets (buf, sizeof (buf), f))
262 /* work backwards to ignore trailling isspace() */
263 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
266 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
271 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
274 vty_out (vty, "%s", host.motd);
277 /* Put out prompt and wait input from user. */
279 vty_prompt (struct vty *vty)
281 struct utsname names;
284 if (vty->type == VTY_TERM)
286 hostname = host.name;
290 hostname = names.nodename;
292 vty_out (vty, cmd_prompt (vty->node), hostname);
296 /* Send WILL TELOPT_ECHO to remote server. */
298 vty_will_echo (struct vty *vty)
300 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
301 vty_out (vty, "%s", cmd);
304 /* Make suppress Go-Ahead telnet option. */
306 vty_will_suppress_go_ahead (struct vty *vty)
308 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
309 vty_out (vty, "%s", cmd);
312 /* Make don't use linemode over telnet. */
314 vty_dont_linemode (struct vty *vty)
316 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
317 vty_out (vty, "%s", cmd);
320 /* Use window size. */
322 vty_do_window_size (struct vty *vty)
324 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
325 vty_out (vty, "%s", cmd);
328 #if 0 /* Currently not used. */
329 /* Make don't use lflow vty interface. */
331 vty_dont_lflow_ahead (struct vty *vty)
333 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
334 vty_out (vty, "%s", cmd);
338 /* Allocate new vty struct. */
342 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
344 new->obuf = buffer_new(0); /* Use default buffer size. */
345 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
346 new->max = VTY_BUFSIZ;
351 /* Authentication of vty */
353 vty_auth (struct vty *vty, char *buf)
356 enum node_type next_node = 0;
358 char *crypt (const char *, const char *);
364 passwd = host.password_encrypt;
366 passwd = host.password;
368 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
370 next_node = VIEW_NODE;
372 case AUTH_ENABLE_NODE:
374 passwd = host.enable_encrypt;
376 passwd = host.enable;
377 next_node = ENABLE_NODE;
384 fail = strcmp (crypt(buf, passwd), passwd);
386 fail = strcmp (buf, passwd);
394 vty->node = next_node; /* Success ! */
401 if (vty->node == AUTH_NODE)
403 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
404 vty->status = VTY_CLOSE;
408 /* AUTH_ENABLE_NODE */
410 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
411 vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
417 /* Command execution over the vty interface. */
419 vty_command (struct vty *vty, char *buf)
423 const char *protocolname;
427 * Log non empty command lines
433 /* Skip white spaces. */
434 while (isspace ((int) *cp) && *cp != '\0')
437 if (cp != NULL && *cp != '\0')
440 char vty_str[VTY_BUFSIZ];
441 char prompt_str[VTY_BUFSIZ];
443 /* format the base vty info */
444 snprintf(vty_str, sizeof(vty_str), "vty[??]@%s", vty->address);
446 for (i = 0; i < vector_active (vtyvec); i++)
447 if (vty == vector_slot (vtyvec, i))
449 snprintf(vty_str, sizeof(vty_str), "vty[%d]@%s",
454 /* format the prompt */
455 snprintf(prompt_str, sizeof(prompt_str), cmd_prompt (vty->node), vty_str);
457 /* now log the command */
458 zlog(NULL, LOG_ERR, "%s%s", prompt_str, buf);
460 /* Split readline string up into the vector */
461 vline = cmd_make_strvec (buf);
466 #ifdef CONSUMED_TIME_CHECK
470 unsigned long realtime, cputime;
473 #endif /* CONSUMED_TIME_CHECK */
475 ret = cmd_execute_command (vline, vty, NULL, 0);
477 /* Get the name of the protocol if any */
479 protocolname = zlog_proto_names[zlog_default->protocol];
481 protocolname = zlog_proto_names[ZLOG_NONE];
483 #ifdef CONSUMED_TIME_CHECK
485 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
487 /* Warn about CPU hog that must be fixed. */
488 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
489 realtime/1000, cputime/1000, buf);
491 #endif /* CONSUMED_TIME_CHECK */
493 if (ret != CMD_SUCCESS)
497 if (vty->type == VTY_FILE)
498 vty_out (vty, "Warning...%s", VTY_NEWLINE);
500 case CMD_ERR_AMBIGUOUS:
501 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
503 case CMD_ERR_NO_MATCH:
504 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
506 case CMD_ERR_INCOMPLETE:
507 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
510 cmd_free_strvec (vline);
515 static const char telnet_backward_char = 0x08;
516 static const char telnet_space_char = ' ';
518 /* Basic function to write buffer to vty. */
520 vty_write (struct vty *vty, const char *buf, size_t nbytes)
522 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
525 /* Should we do buffering here ? And make vty_flush (vty) ? */
526 buffer_put (vty->obuf, buf, nbytes);
529 /* Basic function to insert character into vty. */
531 vty_self_insert (struct vty *vty, char c)
536 vty_buf_assert (vty);
538 /* length is sans nul, max is with */
539 if (vty->length + 1 >= vty->max)
542 length = vty->length - vty->cp;
543 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
545 vty->buf[vty->length] = '\0';
547 vty_buf_put (vty, c);
549 vty_write (vty, &vty->buf[vty->cp], length + 1);
550 for (i = 0; i < length; i++)
551 vty_write (vty, &telnet_backward_char, 1);
555 vty_buf_assert (vty);
558 /* Self insert character 'c' in overwrite mode. */
560 vty_self_insert_overwrite (struct vty *vty, char c)
562 vty_buf_assert (vty);
564 if (vty->cp == vty->length)
566 vty_self_insert (vty, c);
570 vty_buf_put (vty, c);
573 vty_buf_assert (vty);
575 vty_write (vty, &c, 1);
579 * Insert a string into vty->buf at the current cursor position.
581 * If the resultant string would be larger than VTY_BUFSIZ it is
585 vty_insert_word_overwrite (struct vty *vty, char *str)
587 vty_buf_assert (vty);
589 size_t nwrite = MIN ((int) strlen (str), vty->max - vty->cp - 1);
590 memcpy (&vty->buf[vty->cp], str, nwrite);
592 vty->length = vty->cp;
593 vty->buf[vty->length] = '\0';
594 vty_buf_assert (vty);
596 vty_write (vty, str, nwrite);
599 /* Forward character. */
601 vty_forward_char (struct vty *vty)
603 vty_buf_assert (vty);
605 if (vty->cp < vty->length)
607 vty_write (vty, &vty->buf[vty->cp], 1);
611 vty_buf_assert (vty);
614 /* Backward character. */
616 vty_backward_char (struct vty *vty)
618 vty_buf_assert (vty);
623 vty_write (vty, &telnet_backward_char, 1);
626 vty_buf_assert (vty);
629 /* Move to the beginning of the line. */
631 vty_beginning_of_line (struct vty *vty)
634 vty_backward_char (vty);
637 /* Move to the end of the line. */
639 vty_end_of_line (struct vty *vty)
641 while (vty->cp < vty->length)
642 vty_forward_char (vty);
645 static void vty_kill_line_from_beginning (struct vty *);
646 static void vty_redraw_line (struct vty *);
648 /* Print command line history. This function is called from
649 vty_next_line and vty_previous_line. */
651 vty_history_print (struct vty *vty)
655 vty_kill_line_from_beginning (vty);
657 /* Get previous line from history buffer */
658 length = strlen (vty->hist[vty->hp]);
659 memcpy (vty->buf, vty->hist[vty->hp], length);
660 vty->cp = vty->length = length;
661 vty->buf[vty->length] = '\0';
662 vty_buf_assert (vty);
664 /* Redraw current line */
665 vty_redraw_line (vty);
668 /* Show next command line history. */
670 vty_next_line (struct vty *vty)
674 if (vty->hp == vty->hindex)
677 /* Try is there history exist or not. */
679 if (try_index == (VTY_MAXHIST - 1))
684 /* If there is not history return. */
685 if (vty->hist[try_index] == NULL)
690 vty_history_print (vty);
693 /* Show previous command line history. */
695 vty_previous_line (struct vty *vty)
701 try_index = VTY_MAXHIST - 1;
705 if (vty->hist[try_index] == NULL)
710 vty_history_print (vty);
713 /* This function redraw all of the command line character. */
715 vty_redraw_line (struct vty *vty)
717 vty_write (vty, vty->buf, vty->length);
718 vty->cp = vty->length;
720 vty_buf_assert (vty);
725 vty_forward_word (struct vty *vty)
727 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
728 vty_forward_char (vty);
730 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
731 vty_forward_char (vty);
734 /* Backward word without skipping training space. */
736 vty_backward_pure_word (struct vty *vty)
738 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
739 vty_backward_char (vty);
744 vty_backward_word (struct vty *vty)
746 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
747 vty_backward_char (vty);
749 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
750 vty_backward_char (vty);
753 /* When '^D' is typed at the beginning of the line we move to the down
756 vty_down_level (struct vty *vty)
758 vty_out (vty, "%s", VTY_NEWLINE);
759 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
764 /* When '^Z' is received from vty, move down to the enable mode. */
766 vty_end_config (struct vty *vty)
768 vty_out (vty, "%s", VTY_NEWLINE);
774 case RESTRICTED_NODE:
787 case BGP_ENCAPV6_NODE:
797 case KEYCHAIN_KEY_NODE:
801 vty_config_unlock (vty);
802 vty->node = ENABLE_NODE;
805 /* Unknown node, we have to ignore it. */
813 /* Delete a charcter at the current point. */
815 vty_delete_char (struct vty *vty)
820 if (vty->length == 0)
822 vty_down_level (vty);
826 if (vty->cp == vty->length)
827 return; /* completion need here? */
829 vty_buf_assert (vty);
831 size = vty->length - vty->cp;
834 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
835 vty->buf[vty->length] = '\0';
837 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
840 vty_write (vty, &vty->buf[vty->cp], size - 1);
841 vty_write (vty, &telnet_space_char, 1);
843 for (i = 0; i < size; i++)
844 vty_write (vty, &telnet_backward_char, 1);
847 /* Delete a character before the point. */
849 vty_delete_backward_char (struct vty *vty)
854 vty_backward_char (vty);
855 vty_delete_char (vty);
858 /* Kill rest of line from current point. */
860 vty_kill_line (struct vty *vty)
865 size = vty->length - vty->cp;
870 for (i = 0; i < size; i++)
871 vty_write (vty, &telnet_space_char, 1);
872 for (i = 0; i < size; i++)
873 vty_write (vty, &telnet_backward_char, 1);
875 memset (&vty->buf[vty->cp], 0, size);
876 vty->length = vty->cp;
877 vty_buf_assert (vty);
880 /* Kill line from the beginning. */
882 vty_kill_line_from_beginning (struct vty *vty)
884 vty_beginning_of_line (vty);
888 /* Delete a word before the point. */
890 vty_forward_kill_word (struct vty *vty)
892 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
893 vty_delete_char (vty);
894 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
895 vty_delete_char (vty);
898 /* Delete a word before the point. */
900 vty_backward_kill_word (struct vty *vty)
902 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
903 vty_delete_backward_char (vty);
904 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
905 vty_delete_backward_char (vty);
908 /* Transpose chars before or at the point. */
910 vty_transpose_chars (struct vty *vty)
914 /* If length is short or point is near by the beginning of line then
916 if (vty->length < 2 || vty->cp < 1)
919 /* In case of point is located at the end of the line. */
920 if (vty->cp == vty->length)
922 c1 = vty->buf[vty->cp - 1];
923 c2 = vty->buf[vty->cp - 2];
925 vty_backward_char (vty);
926 vty_backward_char (vty);
927 vty_self_insert_overwrite (vty, c1);
928 vty_self_insert_overwrite (vty, c2);
932 c1 = vty->buf[vty->cp];
933 c2 = vty->buf[vty->cp - 1];
935 vty_backward_char (vty);
936 vty_self_insert_overwrite (vty, c1);
937 vty_self_insert_overwrite (vty, c2);
941 /* Do completion at vty interface. */
943 vty_complete_command (struct vty *vty)
947 char **matched = NULL;
950 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
953 vline = cmd_make_strvec (vty->buf);
957 /* In case of 'help \t'. */
958 if (isspace ((int) vty->buf[vty->length - 1]))
959 vector_set (vline, NULL);
961 matched = cmd_complete_command_lib (vline, vty, &ret, 1);
963 cmd_free_strvec (vline);
965 vty_out (vty, "%s", VTY_NEWLINE);
968 case CMD_ERR_AMBIGUOUS:
969 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
971 vty_redraw_line (vty);
973 case CMD_ERR_NO_MATCH:
974 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
976 vty_redraw_line (vty);
978 case CMD_COMPLETE_FULL_MATCH:
980 vty_redraw_line (vty);
981 vty_backward_pure_word (vty);
982 vty_insert_word_overwrite (vty, matched[0]);
983 vty_self_insert (vty, ' ');
984 XFREE (MTYPE_TMP, matched[0]);
986 case CMD_COMPLETE_MATCH:
988 vty_redraw_line (vty);
989 vty_backward_pure_word (vty);
990 vty_insert_word_overwrite (vty, matched[0]);
991 XFREE (MTYPE_TMP, matched[0]);
992 vector_only_index_free (matched);
995 case CMD_COMPLETE_LIST_MATCH:
996 for (i = 0; matched[i] != NULL; i++)
998 if (i != 0 && ((i % 6) == 0))
999 vty_out (vty, "%s", VTY_NEWLINE);
1000 vty_out (vty, "%-10s ", matched[i]);
1001 XFREE (MTYPE_TMP, matched[i]);
1003 vty_out (vty, "%s", VTY_NEWLINE);
1006 vty_redraw_line (vty);
1008 case CMD_ERR_NOTHING_TODO:
1010 vty_redraw_line (vty);
1016 vector_only_index_free (matched);
1020 vty_describe_fold (struct vty *vty, int cmd_width,
1021 unsigned int desc_width, struct cmd_token *token)
1024 const char *cmd, *p;
1027 cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
1029 if (desc_width <= 0)
1031 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
1035 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
1037 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
1039 for (pos = desc_width; pos > 0; pos--)
1040 if (*(p + pos) == ' ')
1046 strncpy (buf, p, pos);
1048 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
1053 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
1055 XFREE (MTYPE_TMP, buf);
1058 /* Describe matched command function. */
1060 vty_describe_command (struct vty *vty)
1065 unsigned int i, width, desc_width;
1066 struct cmd_token *token, *token_cr = NULL;
1068 vline = cmd_make_strvec (vty->buf);
1070 /* In case of '> ?'. */
1073 vline = vector_init (1);
1074 vector_set (vline, NULL);
1077 if (isspace ((int) vty->buf[vty->length - 1]))
1078 vector_set (vline, NULL);
1080 describe = cmd_describe_command (vline, vty, &ret);
1082 vty_out (vty, "%s", VTY_NEWLINE);
1084 /* Ambiguous error. */
1087 case CMD_ERR_AMBIGUOUS:
1088 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
1091 case CMD_ERR_NO_MATCH:
1092 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
1097 /* Get width of command string. */
1099 for (i = 0; i < vector_active (describe); i++)
1100 if ((token = vector_slot (describe, i)) != NULL)
1104 if (token->cmd[0] == '\0')
1107 len = strlen (token->cmd);
1108 if (token->cmd[0] == '.')
1115 /* Get width of description string. */
1116 desc_width = vty->width - (width + 6);
1118 /* Print out description. */
1119 for (i = 0; i < vector_active (describe); i++)
1120 if ((token = vector_slot (describe, i)) != NULL)
1122 if (token->cmd[0] == '\0')
1125 if (strcmp (token->cmd, command_cr) == 0)
1132 vty_out (vty, " %-s%s",
1133 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1135 else if (desc_width >= strlen (token->desc))
1136 vty_out (vty, " %-*s %s%s", width,
1137 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1138 token->desc, VTY_NEWLINE);
1140 vty_describe_fold (vty, width, desc_width, token);
1143 vty_out (vty, " %-*s %s%s", width
1144 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1145 desc->str ? desc->str : "", VTY_NEWLINE);
1149 if ((token = token_cr))
1152 vty_out (vty, " %-s%s",
1153 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1155 else if (desc_width >= strlen (token->desc))
1156 vty_out (vty, " %-*s %s%s", width,
1157 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1158 token->desc, VTY_NEWLINE);
1160 vty_describe_fold (vty, width, desc_width, token);
1164 cmd_free_strvec (vline);
1166 vector_free (describe);
1169 vty_redraw_line (vty);
1173 vty_clear_buf (struct vty *vty)
1175 memset (vty->buf, 0, vty->max);
1178 /* ^C stop current input and do not add command line to the history. */
1180 vty_stop_input (struct vty *vty)
1182 vty->cp = vty->length = 0;
1183 vty_clear_buf (vty);
1184 vty_out (vty, "%s", VTY_NEWLINE);
1190 case RESTRICTED_NODE:
1191 /* Nothing to do. */
1194 case INTERFACE_NODE:
1205 case KEYCHAIN_KEY_NODE:
1209 vty_config_unlock (vty);
1210 vty->node = ENABLE_NODE;
1213 /* Unknown node, we have to ignore it. */
1218 /* Set history pointer to the latest one. */
1219 vty->hp = vty->hindex;
1222 /* Add current command line to the history buffer. */
1224 vty_hist_add (struct vty *vty)
1228 if (vty->length == 0)
1231 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1233 /* Ignore the same string as previous one. */
1234 if (vty->hist[index])
1235 if (strcmp (vty->buf, vty->hist[index]) == 0)
1237 vty->hp = vty->hindex;
1241 /* Insert history entry. */
1242 if (vty->hist[vty->hindex])
1243 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1244 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1246 /* History index rotation. */
1248 if (vty->hindex == VTY_MAXHIST)
1251 vty->hp = vty->hindex;
1254 /* #define TELNET_OPTION_DEBUG */
1256 /* Get telnet window size. */
1258 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1260 #ifdef TELNET_OPTION_DEBUG
1263 for (i = 0; i < nbytes; i++)
1268 vty_out (vty, "IAC ");
1271 vty_out (vty, "WILL ");
1274 vty_out (vty, "WONT ");
1277 vty_out (vty, "DO ");
1280 vty_out (vty, "DONT ");
1283 vty_out (vty, "SB ");
1286 vty_out (vty, "SE ");
1289 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1292 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1295 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1298 vty_out (vty, "%x ", buf[i]);
1302 vty_out (vty, "%s", VTY_NEWLINE);
1304 #endif /* TELNET_OPTION_DEBUG */
1310 vty->iac_sb_in_progress = 1;
1315 if (!vty->iac_sb_in_progress)
1318 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
1320 vty->iac_sb_in_progress = 0;
1323 switch (vty->sb_buf[0])
1326 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1327 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1328 "should send %d characters, but we received %lu",
1329 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1330 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1331 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1332 "too small to handle the telnet NAWS option",
1333 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1336 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1337 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1338 #ifdef TELNET_OPTION_DEBUG
1339 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1340 "width %d, height %d%s",
1341 vty->width, vty->height, VTY_NEWLINE);
1346 vty->iac_sb_in_progress = 0;
1356 /* Execute current command line. */
1358 vty_execute (struct vty *vty)
1367 case AUTH_ENABLE_NODE:
1368 vty_auth (vty, vty->buf);
1371 ret = vty_command (vty, vty->buf);
1372 if (vty->type == VTY_TERM)
1377 /* Clear command line buffer. */
1378 vty->cp = vty->length = 0;
1379 vty_clear_buf (vty);
1381 if (vty->status != VTY_CLOSE )
1387 #define CONTROL(X) ((X) - '@')
1388 #define VTY_NORMAL 0
1389 #define VTY_PRE_ESCAPE 1 /* Esc seen */
1390 #define VTY_ESCAPE 2 /* ANSI terminal escape (Esc-[) seen */
1391 #define VTY_LITERAL 3 /* Next char taken as literal */
1393 /* Escape character command map. */
1395 vty_escape_map (unsigned char c, struct vty *vty)
1400 vty_previous_line (vty);
1403 vty_next_line (vty);
1406 vty_forward_char (vty);
1409 vty_backward_char (vty);
1415 /* Go back to normal mode. */
1416 vty->escape = VTY_NORMAL;
1419 /* Quit print out to the buffer. */
1421 vty_buffer_reset (struct vty *vty)
1423 buffer_reset (vty->obuf);
1425 vty_redraw_line (vty);
1428 /* Read data via vty socket. */
1430 vty_read (struct thread *thread)
1434 unsigned char buf[VTY_READ_BUFSIZ];
1436 int vty_sock = THREAD_FD (thread);
1437 struct vty *vty = THREAD_ARG (thread);
1440 /* Read raw data from socket */
1441 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1445 if (ERRNO_IO_RETRY(errno))
1447 vty_event (VTY_READ, vty_sock, vty);
1450 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1451 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1452 __func__, vty->fd, safe_strerror(errno));
1453 buffer_reset(vty->obuf);
1455 vty->status = VTY_CLOSE;
1458 for (i = 0; i < nbytes; i++)
1473 if (vty->iac_sb_in_progress && !vty->iac)
1475 if (vty->sb_len < sizeof(vty->sb_buf))
1476 vty->sb_buf[vty->sb_len] = buf[i];
1483 /* In case of telnet command */
1485 ret = vty_telnet_option (vty, buf + i, nbytes - i);
1492 if (vty->status == VTY_MORE)
1499 vty_buffer_reset (vty);
1501 #if 0 /* More line does not work for "show ip bgp". */
1504 vty->status = VTY_MORELINE;
1513 /* Escape character. */
1514 if (vty->escape == VTY_ESCAPE)
1516 vty_escape_map (buf[i], vty);
1520 if (vty->escape == VTY_LITERAL)
1522 vty_self_insert (vty, buf[i]);
1523 vty->escape = VTY_NORMAL;
1527 /* Pre-escape status. */
1528 if (vty->escape == VTY_PRE_ESCAPE)
1533 vty->escape = VTY_ESCAPE;
1536 vty_backward_word (vty);
1537 vty->escape = VTY_NORMAL;
1540 vty_forward_word (vty);
1541 vty->escape = VTY_NORMAL;
1544 vty_forward_kill_word (vty);
1545 vty->escape = VTY_NORMAL;
1549 vty_backward_kill_word (vty);
1550 vty->escape = VTY_NORMAL;
1553 vty->escape = VTY_NORMAL;
1562 vty_beginning_of_line (vty);
1565 vty_backward_char (vty);
1568 vty_stop_input (vty);
1571 vty_delete_char (vty);
1574 vty_end_of_line (vty);
1577 vty_forward_char (vty);
1581 vty_delete_backward_char (vty);
1584 vty_kill_line (vty);
1587 vty_next_line (vty);
1590 vty_previous_line (vty);
1593 vty_transpose_chars (vty);
1596 vty_kill_line_from_beginning (vty);
1599 vty->escape = VTY_LITERAL;
1602 vty_backward_kill_word (vty);
1605 vty_end_config (vty);
1609 vty_out (vty, "%s", VTY_NEWLINE);
1613 vty_complete_command (vty);
1616 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1617 vty_self_insert (vty, buf[i]);
1619 vty_describe_command (vty);
1622 if (i + 1 < nbytes && buf[i + 1] == '[')
1624 vty->escape = VTY_ESCAPE;
1628 vty->escape = VTY_PRE_ESCAPE;
1631 if (buf[i] > 31 && buf[i] < 127)
1632 vty_self_insert (vty, buf[i]);
1638 if (vty->status == VTY_CLOSE)
1642 vty_event (VTY_WRITE, vty->wfd, vty);
1643 vty_event (VTY_READ, vty_sock, vty);
1648 /* Flush buffer to the vty. */
1650 vty_flush (struct thread *thread)
1653 buffer_status_t flushrc;
1654 int vty_sock = THREAD_FD (thread);
1655 struct vty *vty = THREAD_ARG (thread);
1657 vty->t_write = NULL;
1659 /* Tempolary disable read thread. */
1660 if ((vty->lines == 0) && vty->t_read)
1662 thread_cancel (vty->t_read);
1666 /* Function execution continue. */
1667 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1669 /* N.B. if width is 0, that means we don't know the window size. */
1670 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
1671 flushrc = buffer_flush_available(vty->obuf, vty_sock);
1672 else if (vty->status == VTY_MORELINE)
1673 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1676 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1677 vty->lines >= 0 ? vty->lines :
1683 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1684 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1686 buffer_reset(vty->obuf);
1690 if (vty->status == VTY_CLOSE)
1694 vty->status = VTY_NORMAL;
1695 if (vty->lines == 0)
1696 vty_event (VTY_READ, vty_sock, vty);
1699 case BUFFER_PENDING:
1700 /* There is more data waiting to be written. */
1701 vty->status = VTY_MORE;
1702 if (vty->lines == 0)
1703 vty_event (VTY_WRITE, vty_sock, vty);
1710 /* allocate and initialise vty */
1712 vty_new_init (int vty_sock)
1718 vty->wfd = vty_sock;
1719 vty->type = VTY_TERM;
1720 vty->node = AUTH_NODE;
1723 vty_clear_buf (vty);
1725 memset (vty->hist, 0, sizeof (vty->hist));
1728 vector_set_index (vtyvec, vty_sock, vty);
1729 vty->status = VTY_NORMAL;
1732 vty->iac_sb_in_progress = 0;
1738 /* Create new vty structure. */
1740 vty_create (int vty_sock, union sockunion *su)
1742 char buf[SU_ADDRSTRLEN];
1745 sockunion2str(su, buf, SU_ADDRSTRLEN);
1747 /* Allocate new vty structure and set up default values. */
1748 vty = vty_new_init (vty_sock);
1750 /* configurable parameters not part of basic init */
1751 vty->v_timeout = vty_timeout_val;
1752 strcpy (vty->address, buf);
1753 if (no_password_check)
1755 if (restricted_mode)
1756 vty->node = RESTRICTED_NODE;
1757 else if (host.advanced)
1758 vty->node = ENABLE_NODE;
1760 vty->node = VIEW_NODE;
1762 if (host.lines >= 0)
1763 vty->lines = host.lines;
1765 if (! no_password_check)
1767 /* Vty is not available if password isn't set. */
1768 if (host.password == NULL && host.password_encrypt == NULL)
1770 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1771 vty->status = VTY_CLOSE;
1777 /* Say hello to the world. */
1779 if (! no_password_check)
1780 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1782 /* Setting up terminal. */
1783 vty_will_echo (vty);
1784 vty_will_suppress_go_ahead (vty);
1786 vty_dont_linemode (vty);
1787 vty_do_window_size (vty);
1788 /* vty_dont_lflow_ahead (vty); */
1792 /* Add read/write thread. */
1793 vty_event (VTY_WRITE, vty_sock, vty);
1794 vty_event (VTY_READ, vty_sock, vty);
1799 /* create vty for stdio */
1800 static struct termios stdio_orig_termios;
1801 static struct vty *stdio_vty = NULL;
1802 static void (*stdio_vty_atclose)(void);
1805 vty_stdio_reset (void)
1809 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1812 if (stdio_vty_atclose)
1813 stdio_vty_atclose ();
1814 stdio_vty_atclose = NULL;
1819 vty_stdio (void (*atclose)())
1822 struct termios termios;
1824 /* refuse creating two vtys on stdio */
1828 vty = stdio_vty = vty_new_init (0);
1829 stdio_vty_atclose = atclose;
1832 /* always have stdio vty in a known _unchangeable_ state, don't want config
1833 * to have any effect here to make sure scripting this works as intended */
1834 vty->node = ENABLE_NODE;
1836 strcpy (vty->address, "console");
1838 if (!tcgetattr (0, &stdio_orig_termios))
1840 termios = stdio_orig_termios;
1841 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1842 | INLCR | IGNCR | ICRNL | IXON);
1843 termios.c_oflag &= ~OPOST;
1844 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1845 termios.c_cflag &= ~(CSIZE | PARENB);
1846 termios.c_cflag |= CS8;
1847 tcsetattr (0, TCSANOW, &termios);
1852 /* Add read/write thread. */
1853 vty_event (VTY_WRITE, 1, vty);
1854 vty_event (VTY_READ, 0, vty);
1859 /* Accept connection from the network. */
1861 vty_accept (struct thread *thread)
1869 struct access_list *acl = NULL;
1870 char buf[SU_ADDRSTRLEN];
1872 accept_sock = THREAD_FD (thread);
1874 /* We continue hearing vty socket. */
1875 vty_event (VTY_SERV, accept_sock, NULL);
1877 memset (&su, 0, sizeof (union sockunion));
1879 /* We can handle IPv4 or IPv6 socket. */
1880 vty_sock = sockunion_accept (accept_sock, &su);
1883 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1886 set_nonblocking(vty_sock);
1888 sockunion2hostprefix (&su, &p);
1890 /* VTY's accesslist apply. */
1891 if (p.family == AF_INET && vty_accesslist_name)
1893 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1894 (access_list_apply (acl, &p) == FILTER_DENY))
1896 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1897 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1900 /* continue accepting connections */
1901 vty_event (VTY_SERV, accept_sock, NULL);
1908 /* VTY's ipv6 accesslist apply. */
1909 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
1911 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1912 (access_list_apply (acl, &p) == FILTER_DENY))
1914 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1915 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1918 /* continue accepting connections */
1919 vty_event (VTY_SERV, accept_sock, NULL);
1924 #endif /* HAVE_IPV6 */
1927 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1928 (char *) &on, sizeof (on));
1930 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
1931 safe_strerror (errno));
1933 zlog (NULL, LOG_INFO, "Vty connection from %s",
1934 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1936 vty_create (vty_sock, &su);
1943 vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1946 struct addrinfo req;
1947 struct addrinfo *ainfo;
1948 struct addrinfo *ainfo_save;
1950 char port_str[BUFSIZ];
1952 memset (&req, 0, sizeof (struct addrinfo));
1953 req.ai_flags = AI_PASSIVE;
1954 req.ai_family = AF_UNSPEC;
1955 req.ai_socktype = SOCK_STREAM;
1956 sprintf (port_str, "%d", port);
1957 port_str[sizeof (port_str) - 1] = '\0';
1959 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1963 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1971 if (ainfo->ai_family != AF_INET
1973 && ainfo->ai_family != AF_INET6
1974 #endif /* HAVE_IPV6 */
1978 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1982 sockopt_v6only (ainfo->ai_family, sock);
1983 sockopt_reuseaddr (sock);
1984 sockopt_reuseport (sock);
1986 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1989 close (sock); /* Avoid sd leak. */
1993 ret = listen (sock, 3);
1996 close (sock); /* Avoid sd leak. */
2000 vty_event (VTY_SERV, sock, NULL);
2002 while ((ainfo = ainfo->ai_next) != NULL);
2004 freeaddrinfo (ainfo_save);
2006 #else /* HAVE_IPV6 */
2008 /* Make vty server socket. */
2010 vty_serv_sock_family (const char* addr, unsigned short port, int family)
2017 memset (&su, 0, sizeof (union sockunion));
2018 su.sa.sa_family = family;
2023 naddr=&su.sin.sin_addr;
2027 naddr=&su.sin6.sin6_addr;
2033 switch(inet_pton(family,addr,naddr))
2036 zlog_err("bad address %s",addr);
2040 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
2044 /* Make new socket. */
2045 accept_sock = sockunion_stream_socket (&su);
2046 if (accept_sock < 0)
2049 /* This is server, so reuse address. */
2050 sockopt_reuseaddr (accept_sock);
2051 sockopt_reuseport (accept_sock);
2053 /* Bind socket to universal address and given port. */
2054 ret = sockunion_bind (accept_sock, &su, port, naddr);
2057 zlog_warn("can't bind socket");
2058 close (accept_sock); /* Avoid sd leak. */
2062 /* Listen socket under queue 3. */
2063 ret = listen (accept_sock, 3);
2066 zlog (NULL, LOG_WARNING, "can't listen socket");
2067 close (accept_sock); /* Avoid sd leak. */
2071 /* Add vty server event. */
2072 vty_event (VTY_SERV, accept_sock, NULL);
2074 #endif /* HAVE_IPV6 */
2077 /* For sockaddr_un. */
2080 /* VTY shell UNIX domain socket. */
2082 vty_serv_un (const char *path)
2086 struct sockaddr_un serv;
2088 struct zprivs_ids_t ids;
2090 /* First of all, unlink existing socket */
2094 old_mask = umask (0007);
2096 /* Make UNIX domain socket. */
2097 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2100 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
2104 /* Make server socket. */
2105 memset (&serv, 0, sizeof (struct sockaddr_un));
2106 serv.sun_family = AF_UNIX;
2107 strncpy (serv.sun_path, path, strlen (path));
2108 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2109 len = serv.sun_len = SUN_LEN(&serv);
2111 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
2112 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2114 ret = bind (sock, (struct sockaddr *) &serv, len);
2117 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
2118 close (sock); /* Avoid sd leak. */
2122 ret = listen (sock, 5);
2125 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
2126 close (sock); /* Avoid sd leak. */
2132 zprivs_get_ids(&ids);
2134 if (ids.gid_vty > 0)
2136 /* set group of socket */
2137 if ( chown (path, -1, ids.gid_vty) )
2139 zlog_err ("vty_serv_un: could chown socket, %s",
2140 safe_strerror (errno) );
2144 vty_event (VTYSH_SERV, sock, NULL);
2147 /* #define VTYSH_DEBUG 1 */
2150 vtysh_accept (struct thread *thread)
2155 struct sockaddr_un client;
2158 accept_sock = THREAD_FD (thread);
2160 vty_event (VTYSH_SERV, accept_sock, NULL);
2162 memset (&client, 0, sizeof (struct sockaddr_un));
2163 client_len = sizeof (struct sockaddr_un);
2165 sock = accept (accept_sock, (struct sockaddr *) &client,
2166 (socklen_t *) &client_len);
2170 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
2174 if (set_nonblocking(sock) < 0)
2176 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2177 " %s, closing", sock, safe_strerror (errno));
2183 printf ("VTY shell accept\n");
2184 #endif /* VTYSH_DEBUG */
2189 vty->type = VTY_SHELL_SERV;
2190 vty->node = VIEW_NODE;
2192 vty_event (VTYSH_READ, sock, vty);
2198 vtysh_flush(struct vty *vty)
2200 switch (buffer_flush_available(vty->obuf, vty->wfd))
2202 case BUFFER_PENDING:
2203 vty_event(VTYSH_WRITE, vty->wfd, vty);
2206 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2207 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2208 buffer_reset(vty->obuf);
2219 vtysh_read (struct thread *thread)
2225 unsigned char buf[VTY_READ_BUFSIZ];
2227 u_char header[4] = {0, 0, 0, 0};
2229 sock = THREAD_FD (thread);
2230 vty = THREAD_ARG (thread);
2233 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
2237 if (ERRNO_IO_RETRY(errno))
2239 vty_event (VTYSH_READ, sock, vty);
2242 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2243 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2244 __func__, sock, safe_strerror(errno));
2246 buffer_reset(vty->obuf);
2249 printf ("close vtysh\n");
2250 #endif /* VTYSH_DEBUG */
2255 printf ("line: %.*s\n", nbytes, buf);
2256 #endif /* VTYSH_DEBUG */
2258 if (vty->length + nbytes >= vty->max)
2260 /* Clear command line buffer. */
2261 vty->cp = vty->length = 0;
2262 vty_clear_buf (vty);
2263 vty_out (vty, "%% Command is too long.%s", VTY_NEWLINE);
2267 for (p = buf; p < buf+nbytes; p++)
2269 vty->buf[vty->length++] = *p;
2273 /* Pass this line to parser. */
2274 ret = vty_execute (vty);
2275 /* Note that vty_execute clears the command buffer and resets
2276 vty->length to 0. */
2278 /* Return result. */
2280 printf ("result: %d\n", ret);
2281 printf ("vtysh node: %d\n", vty->node);
2282 #endif /* VTYSH_DEBUG */
2285 buffer_put(vty->obuf, header, 4);
2287 if (!vty->t_write && (vtysh_flush(vty) < 0))
2288 /* Try to flush results; exit if a write error occurs. */
2294 vty_event (VTYSH_READ, sock, vty);
2300 vtysh_write (struct thread *thread)
2302 struct vty *vty = THREAD_ARG (thread);
2304 vty->t_write = NULL;
2311 /* Determine address family to bind. */
2313 vty_serv_sock (const char *addr, unsigned short port, const char *path)
2315 /* If port is set to 0, do not listen on TCP/IP at all! */
2320 vty_serv_sock_addrinfo (addr, port);
2321 #else /* ! HAVE_IPV6 */
2322 vty_serv_sock_family (addr,port, AF_INET);
2323 #endif /* HAVE_IPV6 */
2331 /* Close vty interface. Warning: call this only from functions that
2332 will be careful not to access the vty afterwards (since it has
2333 now been freed). This is safest from top-level functions (called
2334 directly by the thread dispatcher). */
2336 vty_close (struct vty *vty)
2340 /* Cancel threads.*/
2342 thread_cancel (vty->t_read);
2344 thread_cancel (vty->t_write);
2346 thread_cancel (vty->t_timeout);
2349 buffer_flush_all (vty->obuf, vty->wfd);
2351 /* Free input buffer. */
2352 buffer_free (vty->obuf);
2354 /* Free command history. */
2355 for (i = 0; i < VTY_MAXHIST; i++)
2357 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2360 vector_unset (vtyvec, vty->fd);
2368 /* Close output fd (except stdout/stderr) */
2373 XFREE (MTYPE_VTY, vty->buf);
2375 /* Check configure. */
2376 vty_config_unlock (vty);
2379 XFREE (MTYPE_VTY, vty);
2382 /* When time out occur output message then close connection. */
2384 vty_timeout (struct thread *thread)
2388 vty = THREAD_ARG (thread);
2389 vty->t_timeout = NULL;
2393 buffer_reset (vty->obuf);
2394 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2396 /* Close connection. */
2397 vty->status = VTY_CLOSE;
2403 /* Read up configuration file from file_name. */
2405 vty_read_file (FILE *confp)
2409 unsigned int line_num = 0;
2412 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2415 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2416 vty->wfd = STDOUT_FILENO;
2418 vty->fd = STDIN_FILENO;
2419 vty->type = VTY_FILE;
2420 vty->node = CONFIG_NODE;
2422 /* Execute configuration file */
2423 ret = config_from_file (vty, confp, &line_num);
2425 /* Flush any previous errors before printing messages below */
2426 buffer_flush_all (vty->obuf, vty->fd);
2428 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
2432 case CMD_ERR_AMBIGUOUS:
2433 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
2435 case CMD_ERR_NO_MATCH:
2436 fprintf (stderr, "*** Error reading config: There is no such command.\n");
2439 fprintf (stderr, "*** Error occurred processing line %u, below:\n%s\n",
2440 line_num, vty->buf);
2449 vty_use_backup_config (char *fullpath)
2451 char *fullpath_sav, *fullpath_tmp;
2458 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2459 strcpy (fullpath_sav, fullpath);
2460 strcat (fullpath_sav, CONF_BACKUP_EXT);
2461 if (stat (fullpath_sav, &buf) == -1)
2463 free (fullpath_sav);
2467 fullpath_tmp = malloc (strlen (fullpath) + 8);
2468 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2470 /* Open file to configuration write. */
2471 tmp = mkstemp (fullpath_tmp);
2474 free (fullpath_sav);
2475 free (fullpath_tmp);
2479 sav = open (fullpath_sav, O_RDONLY);
2482 unlink (fullpath_tmp);
2483 free (fullpath_sav);
2484 free (fullpath_tmp);
2488 while((c = read (sav, buffer, 512)) > 0)
2489 write (tmp, buffer, c);
2494 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2496 unlink (fullpath_tmp);
2497 free (fullpath_sav);
2498 free (fullpath_tmp);
2502 if (link (fullpath_tmp, fullpath) == 0)
2503 ret = fopen (fullpath, "r");
2505 unlink (fullpath_tmp);
2507 free (fullpath_sav);
2508 free (fullpath_tmp);
2512 /* Read up configuration file from file_name. */
2514 vty_read_config (char *config_file,
2515 char *config_default_dir)
2517 char cwd[MAXPATHLEN];
2522 /* If -f flag specified. */
2523 if (config_file != NULL)
2525 if (! IS_DIRECTORY_SEP (config_file[0]))
2527 getcwd (cwd, MAXPATHLEN);
2528 tmp = XMALLOC (MTYPE_TMP,
2529 strlen (cwd) + strlen (config_file) + 2);
2530 sprintf (tmp, "%s/%s", cwd, config_file);
2534 fullpath = config_file;
2536 confp = fopen (fullpath, "r");
2540 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2541 __func__, fullpath, safe_strerror (errno));
2543 confp = vty_use_backup_config (fullpath);
2545 fprintf (stderr, "WARNING: using backup configuration file!\n");
2548 fprintf (stderr, "can't open configuration file [%s]\n",
2558 struct stat conf_stat;
2560 /* !!!!PLEASE LEAVE!!!!
2561 * This is NEEDED for use with vtysh -b, or else you can get
2562 * a real configuration food fight with a lot garbage in the
2563 * merged configuration file it creates coming from the per
2564 * daemon configuration files. This also allows the daemons
2565 * to start if there default configuration file is not
2566 * present or ignore them, as needed when using vtysh -b to
2567 * configure the daemons at boot - MAG
2570 /* Stat for vtysh Zebra.conf, if found startup and wait for
2571 * boot configuration
2574 if ( strstr(config_default_dir, "vtysh") == NULL)
2576 ret = stat (integrate_default, &conf_stat);
2582 confp = fopen (config_default_dir, "r");
2585 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2586 __func__, config_default_dir, safe_strerror (errno));
2588 confp = vty_use_backup_config (config_default_dir);
2591 fprintf (stderr, "WARNING: using backup configuration file!\n");
2592 fullpath = config_default_dir;
2596 fprintf (stderr, "can't open configuration file [%s]\n",
2597 config_default_dir);
2602 fullpath = config_default_dir;
2605 vty_read_file (confp);
2609 host_config_set (fullpath);
2612 XFREE (MTYPE_TMP, fullpath);
2615 /* Small utility function which output log to the VTY. */
2617 vty_log (const char *level, const char *proto_str,
2618 const char *format, struct timestamp_control *ctl, va_list va)
2626 for (i = 0; i < vector_active (vtyvec); i++)
2627 if ((vty = vector_slot (vtyvec, i)) != NULL)
2632 vty_log_out (vty, level, proto_str, format, ctl, ac);
2637 /* Async-signal-safe version of vty_log for fixed strings. */
2639 vty_log_fixed (char *buf, size_t len)
2642 struct iovec iov[2];
2644 /* vty may not have been initialised */
2648 iov[0].iov_base = buf;
2649 iov[0].iov_len = len;
2650 iov[1].iov_base = (void *)"\r\n";
2653 for (i = 0; i < vector_active (vtyvec); i++)
2656 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2657 /* N.B. We don't care about the return code, since process is
2658 most likely just about to die anyway. */
2659 writev(vty->wfd, iov, 2);
2664 vty_config_lock (struct vty *vty)
2666 if (vty_config == 0)
2675 vty_config_unlock (struct vty *vty)
2677 if (vty_config == 1 && vty->config == 1)
2685 /* Master of the threads. */
2686 static struct thread_master *vty_master;
2689 vty_event (enum event event, int sock, struct vty *vty)
2691 struct thread *vty_serv_thread;
2696 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
2697 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2701 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
2702 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2705 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
2708 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
2712 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
2714 /* Time out treatment. */
2718 thread_cancel (vty->t_timeout);
2720 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2725 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
2727 case VTY_TIMEOUT_RESET:
2730 thread_cancel (vty->t_timeout);
2731 vty->t_timeout = NULL;
2736 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2745 "Display who is on vty\n")
2750 for (i = 0; i < vector_active (vtyvec); i++)
2751 if ((v = vector_slot (vtyvec, i)) != NULL)
2752 vty_out (vty, "%svty[%d] connected from %s.%s",
2753 v->config ? "*" : " ",
2754 i, v->address, VTY_NEWLINE);
2758 /* Move to vty configuration mode. */
2762 "Configure a terminal line\n"
2763 "Virtual terminal\n")
2765 vty->node = VTY_NODE;
2769 /* Set time out value. */
2771 exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
2773 unsigned long timeout = 0;
2775 /* min_str and sec_str are already checked by parser. So it must be
2776 all digit string. */
2779 timeout = strtol (min_str, NULL, 10);
2783 timeout += strtol (sec_str, NULL, 10);
2785 vty_timeout_val = timeout;
2786 vty->v_timeout = timeout;
2787 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2793 DEFUN (exec_timeout_min,
2794 exec_timeout_min_cmd,
2795 "exec-timeout <0-35791>",
2796 "Set timeout value\n"
2797 "Timeout value in minutes\n")
2799 return exec_timeout (vty, argv[0], NULL);
2802 DEFUN (exec_timeout_sec,
2803 exec_timeout_sec_cmd,
2804 "exec-timeout <0-35791> <0-2147483>",
2805 "Set the EXEC timeout\n"
2806 "Timeout in minutes\n"
2807 "Timeout in seconds\n")
2809 return exec_timeout (vty, argv[0], argv[1]);
2812 DEFUN (no_exec_timeout,
2813 no_exec_timeout_cmd,
2816 "Set the EXEC timeout\n")
2818 return exec_timeout (vty, NULL, NULL);
2821 /* Set vty access class. */
2822 DEFUN (vty_access_class,
2823 vty_access_class_cmd,
2824 "access-class WORD",
2825 "Filter connections based on an IP access list\n"
2828 if (vty_accesslist_name)
2829 XFREE(MTYPE_VTY, vty_accesslist_name);
2831 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2836 /* Clear vty access class. */
2837 DEFUN (no_vty_access_class,
2838 no_vty_access_class_cmd,
2839 "no access-class [WORD]",
2841 "Filter connections based on an IP access list\n"
2844 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2846 vty_out (vty, "Access-class is not currently applied to vty%s",
2851 XFREE(MTYPE_VTY, vty_accesslist_name);
2853 vty_accesslist_name = NULL;
2859 /* Set vty access class. */
2860 DEFUN (vty_ipv6_access_class,
2861 vty_ipv6_access_class_cmd,
2862 "ipv6 access-class WORD",
2864 "Filter connections based on an IP access list\n"
2865 "IPv6 access list\n")
2867 if (vty_ipv6_accesslist_name)
2868 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2870 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2875 /* Clear vty access class. */
2876 DEFUN (no_vty_ipv6_access_class,
2877 no_vty_ipv6_access_class_cmd,
2878 "no ipv6 access-class [WORD]",
2881 "Filter connections based on an IP access list\n"
2882 "IPv6 access list\n")
2884 if (! vty_ipv6_accesslist_name ||
2885 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2887 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2892 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2894 vty_ipv6_accesslist_name = NULL;
2898 #endif /* HAVE_IPV6 */
2904 "Enable password checking\n")
2906 no_password_check = 0;
2910 DEFUN (no_vty_login,
2914 "Enable password checking\n")
2916 no_password_check = 1;
2921 DEFUN (vty_restricted_mode,
2922 vty_restricted_mode_cmd,
2923 "anonymous restricted",
2924 "Restrict view commands available in anonymous, unauthenticated vty\n")
2926 restricted_mode = 1;
2930 DEFUN (vty_no_restricted_mode,
2931 vty_no_restricted_mode_cmd,
2932 "no anonymous restricted",
2934 "Enable password checking\n")
2936 restricted_mode = 0;
2940 DEFUN (service_advanced_vty,
2941 service_advanced_vty_cmd,
2942 "service advanced-vty",
2943 "Set up miscellaneous service\n"
2944 "Enable advanced mode vty interface\n")
2950 DEFUN (no_service_advanced_vty,
2951 no_service_advanced_vty_cmd,
2952 "no service advanced-vty",
2954 "Set up miscellaneous service\n"
2955 "Enable advanced mode vty interface\n")
2961 DEFUN (terminal_monitor,
2962 terminal_monitor_cmd,
2964 "Set terminal line parameters\n"
2965 "Copy debug output to the current terminal line\n")
2971 DEFUN (terminal_no_monitor,
2972 terminal_no_monitor_cmd,
2973 "terminal no monitor",
2974 "Set terminal line parameters\n"
2976 "Copy debug output to the current terminal line\n")
2982 ALIAS (terminal_no_monitor,
2983 no_terminal_monitor_cmd,
2984 "no terminal monitor",
2986 "Set terminal line parameters\n"
2987 "Copy debug output to the current terminal line\n")
2989 DEFUN (show_history,
2993 "Display the session command history\n")
2997 for (index = vty->hindex + 1; index != vty->hindex;)
2999 if (index == VTY_MAXHIST)
3005 if (vty->hist[index] != NULL)
3006 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
3015 DEFUN (log_commands,
3019 "Log all commands (can't be unset without restart)\n")
3021 do_log_commands = 1;
3025 /* Display current configuration. */
3027 vty_config_write (struct vty *vty)
3029 vty_out (vty, "line vty%s", VTY_NEWLINE);
3031 if (vty_accesslist_name)
3032 vty_out (vty, " access-class %s%s",
3033 vty_accesslist_name, VTY_NEWLINE);
3035 if (vty_ipv6_accesslist_name)
3036 vty_out (vty, " ipv6 access-class %s%s",
3037 vty_ipv6_accesslist_name, VTY_NEWLINE);
3040 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
3041 vty_out (vty, " exec-timeout %ld %ld%s",
3042 vty_timeout_val / 60,
3043 vty_timeout_val % 60, VTY_NEWLINE);
3046 if (no_password_check)
3047 vty_out (vty, " no login%s", VTY_NEWLINE);
3049 if (restricted_mode != restricted_mode_default)
3051 if (restricted_mode_default)
3052 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
3054 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
3057 if (do_log_commands)
3058 vty_out (vty, "log commands%s", VTY_NEWLINE);
3060 vty_out (vty, "!%s", VTY_NEWLINE);
3065 struct cmd_node vty_node =
3068 "%s(config-line)# ",
3072 /* Reset all VTY status. */
3078 struct thread *vty_serv_thread;
3080 for (i = 0; i < vector_active (vtyvec); i++)
3081 if ((vty = vector_slot (vtyvec, i)) != NULL)
3083 buffer_reset (vty->obuf);
3084 vty->status = VTY_CLOSE;
3088 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
3089 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
3091 thread_cancel (vty_serv_thread);
3092 vector_slot (Vvty_serv_thread, i) = NULL;
3096 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3098 if (vty_accesslist_name)
3100 XFREE(MTYPE_VTY, vty_accesslist_name);
3101 vty_accesslist_name = NULL;
3104 if (vty_ipv6_accesslist_name)
3106 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
3107 vty_ipv6_accesslist_name = NULL;
3114 char cwd[MAXPATHLEN];
3117 c = getcwd (cwd, MAXPATHLEN);
3122 getcwd (cwd, MAXPATHLEN);
3125 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3126 strcpy (vty_cwd, cwd);
3136 vty_shell (struct vty *vty)
3138 return vty->type == VTY_SHELL ? 1 : 0;
3142 vty_shell_serv (struct vty *vty)
3144 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3150 vtyvec = vector_init (VECTOR_MIN_SIZE);
3153 /* Install vty's own commands like `who' command. */
3155 vty_init (struct thread_master *master_thread)
3157 /* For further configuration read, preserve current directory. */
3160 vtyvec = vector_init (VECTOR_MIN_SIZE);
3162 vty_master = master_thread;
3164 atexit (vty_stdio_reset);
3166 /* Initilize server thread vector. */
3167 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3169 /* Install bgp top node. */
3170 install_node (&vty_node, vty_config_write);
3172 install_element (RESTRICTED_NODE, &who_cmd);
3173 install_element (RESTRICTED_NODE, &show_history_cmd);
3174 install_element (VIEW_NODE, &who_cmd);
3175 install_element (VIEW_NODE, &show_history_cmd);
3176 install_element (CONFIG_NODE, &line_vty_cmd);
3177 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3178 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3179 install_element (CONFIG_NODE, &show_history_cmd);
3180 install_element (CONFIG_NODE, &log_commands_cmd);
3181 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3182 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
3183 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
3185 install_default (VTY_NODE);
3186 install_element (VTY_NODE, &exec_timeout_min_cmd);
3187 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3188 install_element (VTY_NODE, &no_exec_timeout_cmd);
3189 install_element (VTY_NODE, &vty_access_class_cmd);
3190 install_element (VTY_NODE, &no_vty_access_class_cmd);
3191 install_element (VTY_NODE, &vty_login_cmd);
3192 install_element (VTY_NODE, &no_vty_login_cmd);
3193 install_element (VTY_NODE, &vty_restricted_mode_cmd);
3194 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
3196 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3197 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3198 #endif /* HAVE_IPV6 */
3202 vty_terminate (void)
3205 XFREE (MTYPE_TMP, vty_cwd);
3207 if (vtyvec && Vvty_serv_thread)
3210 vector_free (vtyvec);
3211 vector_free (Vvty_serv_thread);