Bug #362 is fixed now.
[jleu-quagga.git] / lib / vty.c
blobccf6640690957345ea20be785f6eee9a8d228694
1 /*
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
10 * later version.
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
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "linklist.h"
26 #include "thread.h"
27 #include "buffer.h"
28 #include <lib/version.h>
29 #include "command.h"
30 #include "sockunion.h"
31 #include "memory.h"
32 #include "str.h"
33 #include "log.h"
34 #include "prefix.h"
35 #include "filter.h"
36 #include "vty.h"
37 #include "privs.h"
38 #include "network.h"
40 #include <arpa/telnet.h>
42 /* Vty events */
43 enum event
45 VTY_SERV,
46 VTY_READ,
47 VTY_WRITE,
48 VTY_TIMEOUT_RESET,
49 #ifdef VTYSH
50 VTYSH_SERV,
51 VTYSH_READ,
52 VTYSH_WRITE
53 #endif /* VTYSH */
56 static void vty_event (enum event, int, struct vty *);
58 /* Extern host structure from command.c */
59 extern struct host host;
61 /* Vector which store each vty structure. */
62 static vector vtyvec;
64 /* Vty timeout value. */
65 static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
67 /* Vty access-class command */
68 static char *vty_accesslist_name = NULL;
70 /* Vty access-calss for IPv6. */
71 static char *vty_ipv6_accesslist_name = NULL;
73 /* VTY server thread. */
74 vector Vvty_serv_thread;
76 /* Current directory. */
77 char *vty_cwd = NULL;
79 /* Configure lock. */
80 static int vty_config;
82 /* Login password check. */
83 static int no_password_check = 0;
85 /* Integrated configuration file path */
86 char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
89 /* VTY standard output function. */
90 int
91 vty_out (struct vty *vty, const char *format, ...)
93 va_list args;
94 int len = 0;
95 int size = 1024;
96 char buf[1024];
97 char *p = NULL;
99 if (vty_shell (vty))
101 va_start (args, format);
102 vprintf (format, args);
103 va_end (args);
105 else
107 /* Try to write to initial buffer. */
108 va_start (args, format);
109 len = vsnprintf (buf, sizeof buf, format, args);
110 va_end (args);
112 /* Initial buffer is not enough. */
113 if (len < 0 || len >= size)
115 while (1)
117 if (len > -1)
118 size = len + 1;
119 else
120 size = size * 2;
122 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
123 if (! p)
124 return -1;
126 va_start (args, format);
127 len = vsnprintf (p, size, format, args);
128 va_end (args);
130 if (len > -1 && len < size)
131 break;
135 /* When initial buffer is enough to store all output. */
136 if (! p)
137 p = buf;
139 /* Pointer p must point out buffer. */
140 buffer_put (vty->obuf, (u_char *) p, len);
142 /* If p is not different with buf, it is allocated buffer. */
143 if (p != buf)
144 XFREE (MTYPE_VTY_OUT_BUF, p);
147 return len;
150 static int
151 vty_log_out (struct vty *vty, const char *level, const char *proto_str,
152 const char *format, struct timestamp_control *ctl, va_list va)
154 int ret;
155 int len;
156 char buf[1024];
158 if (!ctl->already_rendered)
160 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
161 ctl->already_rendered = 1;
163 if (ctl->len+1 >= sizeof(buf))
164 return -1;
165 memcpy(buf, ctl->buf, len = ctl->len);
166 buf[len++] = ' ';
167 buf[len] = '\0';
169 if (level)
170 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
171 else
172 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
173 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
174 return -1;
176 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
177 ((size_t)((len += ret)+2) > sizeof(buf)))
178 return -1;
180 buf[len++] = '\r';
181 buf[len++] = '\n';
183 if (write(vty->fd, buf, len) < 0)
185 if (ERRNO_IO_RETRY(errno))
186 /* Kernel buffer is full, probably too much debugging output, so just
187 drop the data and ignore. */
188 return -1;
189 /* Fatal I/O error. */
190 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
191 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
192 __func__, vty->fd, safe_strerror(errno));
193 buffer_reset(vty->obuf);
194 /* cannot call vty_close, because a parent routine may still try
195 to access the vty struct */
196 vty->status = VTY_CLOSE;
197 shutdown(vty->fd, SHUT_RDWR);
198 return -1;
200 return 0;
203 /* Output current time to the vty. */
204 void
205 vty_time_print (struct vty *vty, int cr)
207 char buf [25];
209 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
211 zlog (NULL, LOG_INFO, "quagga_timestamp error");
212 return;
214 if (cr)
215 vty_out (vty, "%s\n", buf);
216 else
217 vty_out (vty, "%s ", buf);
219 return;
222 /* Say hello to vty interface. */
223 void
224 vty_hello (struct vty *vty)
226 if (host.motdfile)
228 FILE *f;
229 char buf[4096];
231 f = fopen (host.motdfile, "r");
232 if (f)
234 while (fgets (buf, sizeof (buf), f))
236 char *s;
237 /* work backwards to ignore trailling isspace() */
238 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
239 s--);
240 *s = '\0';
241 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
243 fclose (f);
245 else
246 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
248 else if (host.motd)
249 vty_out (vty, host.motd);
252 /* Put out prompt and wait input from user. */
253 static void
254 vty_prompt (struct vty *vty)
256 struct utsname names;
257 const char*hostname;
259 if (vty->type == VTY_TERM)
261 hostname = host.name;
262 if (!hostname)
264 uname (&names);
265 hostname = names.nodename;
267 vty_out (vty, cmd_prompt (vty->node), hostname);
271 /* Send WILL TELOPT_ECHO to remote server. */
272 static void
273 vty_will_echo (struct vty *vty)
275 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
276 vty_out (vty, "%s", cmd);
279 /* Make suppress Go-Ahead telnet option. */
280 static void
281 vty_will_suppress_go_ahead (struct vty *vty)
283 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
284 vty_out (vty, "%s", cmd);
287 /* Make don't use linemode over telnet. */
288 static void
289 vty_dont_linemode (struct vty *vty)
291 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
292 vty_out (vty, "%s", cmd);
295 /* Use window size. */
296 static void
297 vty_do_window_size (struct vty *vty)
299 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
300 vty_out (vty, "%s", cmd);
303 #if 0 /* Currently not used. */
304 /* Make don't use lflow vty interface. */
305 static void
306 vty_dont_lflow_ahead (struct vty *vty)
308 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
309 vty_out (vty, "%s", cmd);
311 #endif /* 0 */
313 /* Allocate new vty struct. */
314 struct vty *
315 vty_new ()
317 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
319 new->obuf = buffer_new(0); /* Use default buffer size. */
320 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
321 new->max = VTY_BUFSIZ;
323 return new;
326 /* Authentication of vty */
327 static void
328 vty_auth (struct vty *vty, char *buf)
330 char *passwd = NULL;
331 enum node_type next_node = 0;
332 int fail;
333 char *crypt (const char *, const char *);
335 switch (vty->node)
337 case AUTH_NODE:
338 if (host.encrypt)
339 passwd = host.password_encrypt;
340 else
341 passwd = host.password;
342 if (host.advanced)
343 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
344 else
345 next_node = VIEW_NODE;
346 break;
347 case AUTH_ENABLE_NODE:
348 if (host.encrypt)
349 passwd = host.enable_encrypt;
350 else
351 passwd = host.enable;
352 next_node = ENABLE_NODE;
353 break;
356 if (passwd)
358 if (host.encrypt)
359 fail = strcmp (crypt(buf, passwd), passwd);
360 else
361 fail = strcmp (buf, passwd);
363 else
364 fail = 1;
366 if (! fail)
368 vty->fail = 0;
369 vty->node = next_node; /* Success ! */
371 else
373 vty->fail++;
374 if (vty->fail >= 3)
376 if (vty->node == AUTH_NODE)
378 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
379 vty->status = VTY_CLOSE;
381 else
383 /* AUTH_ENABLE_NODE */
384 vty->fail = 0;
385 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
386 vty->node = VIEW_NODE;
392 /* Command execution over the vty interface. */
393 static int
394 vty_command (struct vty *vty, char *buf)
396 int ret;
397 vector vline;
398 const char *protocolname;
400 /* Split readline string up into the vector */
401 vline = cmd_make_strvec (buf);
403 if (vline == NULL)
404 return CMD_SUCCESS;
406 #ifdef CONSUMED_TIME_CHECK
408 RUSAGE_T before;
409 RUSAGE_T after;
410 unsigned long realtime, cputime;
412 GETRUSAGE(&before);
413 #endif /* CONSUMED_TIME_CHECK */
415 ret = cmd_execute_command (vline, vty, NULL, 0);
417 /* Get the name of the protocol if any */
418 if (zlog_default)
419 protocolname = zlog_proto_names[zlog_default->protocol];
420 else
421 protocolname = zlog_proto_names[ZLOG_NONE];
423 #ifdef CONSUMED_TIME_CHECK
424 GETRUSAGE(&after);
425 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
426 CONSUMED_TIME_CHECK)
427 /* Warn about CPU hog that must be fixed. */
428 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
429 realtime/1000, cputime/1000, buf);
431 #endif /* CONSUMED_TIME_CHECK */
433 if (ret != CMD_SUCCESS)
434 switch (ret)
436 case CMD_WARNING:
437 if (vty->type == VTY_FILE)
438 vty_out (vty, "Warning...%s", VTY_NEWLINE);
439 break;
440 case CMD_ERR_AMBIGUOUS:
441 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
442 break;
443 case CMD_ERR_NO_MATCH:
444 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
445 break;
446 case CMD_ERR_INCOMPLETE:
447 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
448 break;
450 cmd_free_strvec (vline);
452 return ret;
455 static const char telnet_backward_char = 0x08;
456 static const char telnet_space_char = ' ';
458 /* Basic function to write buffer to vty. */
459 static void
460 vty_write (struct vty *vty, const char *buf, size_t nbytes)
462 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
463 return;
465 /* Should we do buffering here ? And make vty_flush (vty) ? */
466 buffer_put (vty->obuf, buf, nbytes);
469 /* Ensure length of input buffer. Is buffer is short, double it. */
470 static void
471 vty_ensure (struct vty *vty, int length)
473 if (vty->max <= length)
475 vty->max *= 2;
476 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
480 /* Basic function to insert character into vty. */
481 static void
482 vty_self_insert (struct vty *vty, char c)
484 int i;
485 int length;
487 vty_ensure (vty, vty->length + 1);
488 length = vty->length - vty->cp;
489 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
490 vty->buf[vty->cp] = c;
492 vty_write (vty, &vty->buf[vty->cp], length + 1);
493 for (i = 0; i < length; i++)
494 vty_write (vty, &telnet_backward_char, 1);
496 vty->cp++;
497 vty->length++;
500 /* Self insert character 'c' in overwrite mode. */
501 static void
502 vty_self_insert_overwrite (struct vty *vty, char c)
504 vty_ensure (vty, vty->length + 1);
505 vty->buf[vty->cp++] = c;
507 if (vty->cp > vty->length)
508 vty->length++;
510 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
511 return;
513 vty_write (vty, &c, 1);
516 /* Insert a word into vty interface with overwrite mode. */
517 static void
518 vty_insert_word_overwrite (struct vty *vty, char *str)
520 int len = strlen (str);
521 vty_write (vty, str, len);
522 strcpy (&vty->buf[vty->cp], str);
523 vty->cp += len;
524 vty->length = vty->cp;
527 /* Forward character. */
528 static void
529 vty_forward_char (struct vty *vty)
531 if (vty->cp < vty->length)
533 vty_write (vty, &vty->buf[vty->cp], 1);
534 vty->cp++;
538 /* Backward character. */
539 static void
540 vty_backward_char (struct vty *vty)
542 if (vty->cp > 0)
544 vty->cp--;
545 vty_write (vty, &telnet_backward_char, 1);
549 /* Move to the beginning of the line. */
550 static void
551 vty_beginning_of_line (struct vty *vty)
553 while (vty->cp)
554 vty_backward_char (vty);
557 /* Move to the end of the line. */
558 static void
559 vty_end_of_line (struct vty *vty)
561 while (vty->cp < vty->length)
562 vty_forward_char (vty);
565 static void vty_kill_line_from_beginning (struct vty *);
566 static void vty_redraw_line (struct vty *);
568 /* Print command line history. This function is called from
569 vty_next_line and vty_previous_line. */
570 static void
571 vty_history_print (struct vty *vty)
573 int length;
575 vty_kill_line_from_beginning (vty);
577 /* Get previous line from history buffer */
578 length = strlen (vty->hist[vty->hp]);
579 memcpy (vty->buf, vty->hist[vty->hp], length);
580 vty->cp = vty->length = length;
582 /* Redraw current line */
583 vty_redraw_line (vty);
586 /* Show next command line history. */
587 static void
588 vty_next_line (struct vty *vty)
590 int try_index;
592 if (vty->hp == vty->hindex)
593 return;
595 /* Try is there history exist or not. */
596 try_index = vty->hp;
597 if (try_index == (VTY_MAXHIST - 1))
598 try_index = 0;
599 else
600 try_index++;
602 /* If there is not history return. */
603 if (vty->hist[try_index] == NULL)
604 return;
605 else
606 vty->hp = try_index;
608 vty_history_print (vty);
611 /* Show previous command line history. */
612 static void
613 vty_previous_line (struct vty *vty)
615 int try_index;
617 try_index = vty->hp;
618 if (try_index == 0)
619 try_index = VTY_MAXHIST - 1;
620 else
621 try_index--;
623 if (vty->hist[try_index] == NULL)
624 return;
625 else
626 vty->hp = try_index;
628 vty_history_print (vty);
631 /* This function redraw all of the command line character. */
632 static void
633 vty_redraw_line (struct vty *vty)
635 vty_write (vty, vty->buf, vty->length);
636 vty->cp = vty->length;
639 /* Forward word. */
640 static void
641 vty_forward_word (struct vty *vty)
643 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
644 vty_forward_char (vty);
646 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
647 vty_forward_char (vty);
650 /* Backward word without skipping training space. */
651 static void
652 vty_backward_pure_word (struct vty *vty)
654 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
655 vty_backward_char (vty);
658 /* Backward word. */
659 static void
660 vty_backward_word (struct vty *vty)
662 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
663 vty_backward_char (vty);
665 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
666 vty_backward_char (vty);
669 /* When '^D' is typed at the beginning of the line we move to the down
670 level. */
671 static void
672 vty_down_level (struct vty *vty)
674 vty_out (vty, "%s", VTY_NEWLINE);
675 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
676 vty_prompt (vty);
677 vty->cp = 0;
680 /* When '^Z' is received from vty, move down to the enable mode. */
681 static void
682 vty_end_config (struct vty *vty)
684 vty_out (vty, "%s", VTY_NEWLINE);
686 switch (vty->node)
688 case VIEW_NODE:
689 case ENABLE_NODE:
690 /* Nothing to do. */
691 break;
692 case CONFIG_NODE:
693 case INTERFACE_NODE:
694 case ZEBRA_NODE:
695 case RIP_NODE:
696 case RIPNG_NODE:
697 case BGP_NODE:
698 case BGP_VPNV4_NODE:
699 case BGP_IPV4_NODE:
700 case BGP_IPV4M_NODE:
701 case BGP_IPV6_NODE:
702 case BGP_IPV6M_NODE:
703 case RMAP_NODE:
704 case OSPF_NODE:
705 case OSPF6_NODE:
706 case ISIS_NODE:
707 case KEYCHAIN_NODE:
708 case KEYCHAIN_KEY_NODE:
709 case MASC_NODE:
710 case VTY_NODE:
711 vty_config_unlock (vty);
712 vty->node = ENABLE_NODE;
713 break;
714 default:
715 /* Unknown node, we have to ignore it. */
716 break;
719 vty_prompt (vty);
720 vty->cp = 0;
723 /* Delete a charcter at the current point. */
724 static void
725 vty_delete_char (struct vty *vty)
727 int i;
728 int size;
730 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
731 return;
733 if (vty->length == 0)
735 vty_down_level (vty);
736 return;
739 if (vty->cp == vty->length)
740 return; /* completion need here? */
742 size = vty->length - vty->cp;
744 vty->length--;
745 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
746 vty->buf[vty->length] = '\0';
748 vty_write (vty, &vty->buf[vty->cp], size - 1);
749 vty_write (vty, &telnet_space_char, 1);
751 for (i = 0; i < size; i++)
752 vty_write (vty, &telnet_backward_char, 1);
755 /* Delete a character before the point. */
756 static void
757 vty_delete_backward_char (struct vty *vty)
759 if (vty->cp == 0)
760 return;
762 vty_backward_char (vty);
763 vty_delete_char (vty);
766 /* Kill rest of line from current point. */
767 static void
768 vty_kill_line (struct vty *vty)
770 int i;
771 int size;
773 size = vty->length - vty->cp;
775 if (size == 0)
776 return;
778 for (i = 0; i < size; i++)
779 vty_write (vty, &telnet_space_char, 1);
780 for (i = 0; i < size; i++)
781 vty_write (vty, &telnet_backward_char, 1);
783 memset (&vty->buf[vty->cp], 0, size);
784 vty->length = vty->cp;
787 /* Kill line from the beginning. */
788 static void
789 vty_kill_line_from_beginning (struct vty *vty)
791 vty_beginning_of_line (vty);
792 vty_kill_line (vty);
795 /* Delete a word before the point. */
796 static void
797 vty_forward_kill_word (struct vty *vty)
799 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
800 vty_delete_char (vty);
801 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
802 vty_delete_char (vty);
805 /* Delete a word before the point. */
806 static void
807 vty_backward_kill_word (struct vty *vty)
809 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
810 vty_delete_backward_char (vty);
811 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
812 vty_delete_backward_char (vty);
815 /* Transpose chars before or at the point. */
816 static void
817 vty_transpose_chars (struct vty *vty)
819 char c1, c2;
821 /* If length is short or point is near by the beginning of line then
822 return. */
823 if (vty->length < 2 || vty->cp < 1)
824 return;
826 /* In case of point is located at the end of the line. */
827 if (vty->cp == vty->length)
829 c1 = vty->buf[vty->cp - 1];
830 c2 = vty->buf[vty->cp - 2];
832 vty_backward_char (vty);
833 vty_backward_char (vty);
834 vty_self_insert_overwrite (vty, c1);
835 vty_self_insert_overwrite (vty, c2);
837 else
839 c1 = vty->buf[vty->cp];
840 c2 = vty->buf[vty->cp - 1];
842 vty_backward_char (vty);
843 vty_self_insert_overwrite (vty, c1);
844 vty_self_insert_overwrite (vty, c2);
848 /* Do completion at vty interface. */
849 static void
850 vty_complete_command (struct vty *vty)
852 int i;
853 int ret;
854 char **matched = NULL;
855 vector vline;
857 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
858 return;
860 vline = cmd_make_strvec (vty->buf);
861 if (vline == NULL)
862 return;
864 /* In case of 'help \t'. */
865 if (isspace ((int) vty->buf[vty->length - 1]))
866 vector_set (vline, '\0');
868 matched = cmd_complete_command (vline, vty, &ret);
870 cmd_free_strvec (vline);
872 vty_out (vty, "%s", VTY_NEWLINE);
873 switch (ret)
875 case CMD_ERR_AMBIGUOUS:
876 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
877 vty_prompt (vty);
878 vty_redraw_line (vty);
879 break;
880 case CMD_ERR_NO_MATCH:
881 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
882 vty_prompt (vty);
883 vty_redraw_line (vty);
884 break;
885 case CMD_COMPLETE_FULL_MATCH:
886 vty_prompt (vty);
887 vty_redraw_line (vty);
888 vty_backward_pure_word (vty);
889 vty_insert_word_overwrite (vty, matched[0]);
890 vty_self_insert (vty, ' ');
891 XFREE (MTYPE_TMP, matched[0]);
892 break;
893 case CMD_COMPLETE_MATCH:
894 vty_prompt (vty);
895 vty_redraw_line (vty);
896 vty_backward_pure_word (vty);
897 vty_insert_word_overwrite (vty, matched[0]);
898 XFREE (MTYPE_TMP, matched[0]);
899 vector_only_index_free (matched);
900 return;
901 break;
902 case CMD_COMPLETE_LIST_MATCH:
903 for (i = 0; matched[i] != NULL; i++)
905 if (i != 0 && ((i % 6) == 0))
906 vty_out (vty, "%s", VTY_NEWLINE);
907 vty_out (vty, "%-10s ", matched[i]);
908 XFREE (MTYPE_TMP, matched[i]);
910 vty_out (vty, "%s", VTY_NEWLINE);
912 vty_prompt (vty);
913 vty_redraw_line (vty);
914 break;
915 case CMD_ERR_NOTHING_TODO:
916 vty_prompt (vty);
917 vty_redraw_line (vty);
918 break;
919 default:
920 break;
922 if (matched)
923 vector_only_index_free (matched);
926 static void
927 vty_describe_fold (struct vty *vty, int cmd_width,
928 unsigned int desc_width, struct desc *desc)
930 char *buf;
931 const char *cmd, *p;
932 int pos;
934 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
936 if (desc_width <= 0)
938 vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
939 return;
942 buf = XCALLOC (MTYPE_TMP, strlen (desc->str) + 1);
944 for (p = desc->str; strlen (p) > desc_width; p += pos + 1)
946 for (pos = desc_width; pos > 0; pos--)
947 if (*(p + pos) == ' ')
948 break;
950 if (pos == 0)
951 break;
953 strncpy (buf, p, pos);
954 buf[pos] = '\0';
955 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
957 cmd = "";
960 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
962 XFREE (MTYPE_TMP, buf);
965 /* Describe matched command function. */
966 static void
967 vty_describe_command (struct vty *vty)
969 int ret;
970 vector vline;
971 vector describe;
972 unsigned int i, width, desc_width;
973 struct desc *desc, *desc_cr = NULL;
975 vline = cmd_make_strvec (vty->buf);
977 /* In case of '> ?'. */
978 if (vline == NULL)
980 vline = vector_init (1);
981 vector_set (vline, '\0');
983 else
984 if (isspace ((int) vty->buf[vty->length - 1]))
985 vector_set (vline, '\0');
987 describe = cmd_describe_command (vline, vty, &ret);
989 vty_out (vty, "%s", VTY_NEWLINE);
991 /* Ambiguous error. */
992 switch (ret)
994 case CMD_ERR_AMBIGUOUS:
995 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
996 goto out;
997 break;
998 case CMD_ERR_NO_MATCH:
999 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
1000 goto out;
1001 break;
1004 /* Get width of command string. */
1005 width = 0;
1006 for (i = 0; i < vector_active (describe); i++)
1007 if ((desc = vector_slot (describe, i)) != NULL)
1009 unsigned int len;
1011 if (desc->cmd[0] == '\0')
1012 continue;
1014 len = strlen (desc->cmd);
1015 if (desc->cmd[0] == '.')
1016 len--;
1018 if (width < len)
1019 width = len;
1022 /* Get width of description string. */
1023 desc_width = vty->width - (width + 6);
1025 /* Print out description. */
1026 for (i = 0; i < vector_active (describe); i++)
1027 if ((desc = vector_slot (describe, i)) != NULL)
1029 if (desc->cmd[0] == '\0')
1030 continue;
1032 if (strcmp (desc->cmd, "<cr>") == 0)
1034 desc_cr = desc;
1035 continue;
1038 if (!desc->str)
1039 vty_out (vty, " %-s%s",
1040 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1041 VTY_NEWLINE);
1042 else if (desc_width >= strlen (desc->str))
1043 vty_out (vty, " %-*s %s%s", width,
1044 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1045 desc->str, VTY_NEWLINE);
1046 else
1047 vty_describe_fold (vty, width, desc_width, desc);
1049 #if 0
1050 vty_out (vty, " %-*s %s%s", width
1051 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1052 desc->str ? desc->str : "", VTY_NEWLINE);
1053 #endif /* 0 */
1056 if ((desc = desc_cr))
1058 if (!desc->str)
1059 vty_out (vty, " %-s%s",
1060 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1061 VTY_NEWLINE);
1062 else if (desc_width >= strlen (desc->str))
1063 vty_out (vty, " %-*s %s%s", width,
1064 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1065 desc->str, VTY_NEWLINE);
1066 else
1067 vty_describe_fold (vty, width, desc_width, desc);
1070 out:
1071 cmd_free_strvec (vline);
1072 if (describe)
1073 vector_free (describe);
1075 vty_prompt (vty);
1076 vty_redraw_line (vty);
1079 static void
1080 vty_clear_buf (struct vty *vty)
1082 memset (vty->buf, 0, vty->max);
1085 /* ^C stop current input and do not add command line to the history. */
1086 static void
1087 vty_stop_input (struct vty *vty)
1089 vty->cp = vty->length = 0;
1090 vty_clear_buf (vty);
1091 vty_out (vty, "%s", VTY_NEWLINE);
1093 switch (vty->node)
1095 case VIEW_NODE:
1096 case ENABLE_NODE:
1097 /* Nothing to do. */
1098 break;
1099 case CONFIG_NODE:
1100 case INTERFACE_NODE:
1101 case ZEBRA_NODE:
1102 case RIP_NODE:
1103 case RIPNG_NODE:
1104 case BGP_NODE:
1105 case RMAP_NODE:
1106 case OSPF_NODE:
1107 case OSPF6_NODE:
1108 case ISIS_NODE:
1109 case KEYCHAIN_NODE:
1110 case KEYCHAIN_KEY_NODE:
1111 case MASC_NODE:
1112 case VTY_NODE:
1113 vty_config_unlock (vty);
1114 vty->node = ENABLE_NODE;
1115 break;
1116 default:
1117 /* Unknown node, we have to ignore it. */
1118 break;
1120 vty_prompt (vty);
1122 /* Set history pointer to the latest one. */
1123 vty->hp = vty->hindex;
1126 /* Add current command line to the history buffer. */
1127 static void
1128 vty_hist_add (struct vty *vty)
1130 int index;
1132 if (vty->length == 0)
1133 return;
1135 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1137 /* Ignore the same string as previous one. */
1138 if (vty->hist[index])
1139 if (strcmp (vty->buf, vty->hist[index]) == 0)
1141 vty->hp = vty->hindex;
1142 return;
1145 /* Insert history entry. */
1146 if (vty->hist[vty->hindex])
1147 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1148 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1150 /* History index rotation. */
1151 vty->hindex++;
1152 if (vty->hindex == VTY_MAXHIST)
1153 vty->hindex = 0;
1155 vty->hp = vty->hindex;
1158 /* #define TELNET_OPTION_DEBUG */
1160 /* Get telnet window size. */
1161 static int
1162 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1164 #ifdef TELNET_OPTION_DEBUG
1165 int i;
1167 for (i = 0; i < nbytes; i++)
1169 switch (buf[i])
1171 case IAC:
1172 vty_out (vty, "IAC ");
1173 break;
1174 case WILL:
1175 vty_out (vty, "WILL ");
1176 break;
1177 case WONT:
1178 vty_out (vty, "WONT ");
1179 break;
1180 case DO:
1181 vty_out (vty, "DO ");
1182 break;
1183 case DONT:
1184 vty_out (vty, "DONT ");
1185 break;
1186 case SB:
1187 vty_out (vty, "SB ");
1188 break;
1189 case SE:
1190 vty_out (vty, "SE ");
1191 break;
1192 case TELOPT_ECHO:
1193 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1194 break;
1195 case TELOPT_SGA:
1196 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1197 break;
1198 case TELOPT_NAWS:
1199 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1200 break;
1201 default:
1202 vty_out (vty, "%x ", buf[i]);
1203 break;
1206 vty_out (vty, "%s", VTY_NEWLINE);
1208 #endif /* TELNET_OPTION_DEBUG */
1210 switch (buf[0])
1212 case SB:
1213 vty->sb_len = 0;
1214 vty->iac_sb_in_progress = 1;
1215 return 0;
1216 break;
1217 case SE:
1219 if (!vty->iac_sb_in_progress)
1220 return 0;
1222 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
1224 vty->iac_sb_in_progress = 0;
1225 return 0;
1227 switch (vty->sb_buf[0])
1229 case TELOPT_NAWS:
1230 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1231 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1232 "should send %d characters, but we received %lu",
1233 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1234 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1235 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1236 "too small to handle the telnet NAWS option",
1237 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1238 else
1240 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1241 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1242 #ifdef TELNET_OPTION_DEBUG
1243 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1244 "width %d, height %d%s",
1245 vty->width, vty->height, VTY_NEWLINE);
1246 #endif
1248 break;
1250 vty->iac_sb_in_progress = 0;
1251 return 0;
1252 break;
1254 default:
1255 break;
1257 return 1;
1260 /* Execute current command line. */
1261 static int
1262 vty_execute (struct vty *vty)
1264 int ret;
1266 ret = CMD_SUCCESS;
1268 switch (vty->node)
1270 case AUTH_NODE:
1271 case AUTH_ENABLE_NODE:
1272 vty_auth (vty, vty->buf);
1273 break;
1274 default:
1275 ret = vty_command (vty, vty->buf);
1276 if (vty->type == VTY_TERM)
1277 vty_hist_add (vty);
1278 break;
1281 /* Clear command line buffer. */
1282 vty->cp = vty->length = 0;
1283 vty_clear_buf (vty);
1285 if (vty->status != VTY_CLOSE )
1286 vty_prompt (vty);
1288 return ret;
1291 #define CONTROL(X) ((X) - '@')
1292 #define VTY_NORMAL 0
1293 #define VTY_PRE_ESCAPE 1
1294 #define VTY_ESCAPE 2
1296 /* Escape character command map. */
1297 static void
1298 vty_escape_map (unsigned char c, struct vty *vty)
1300 switch (c)
1302 case ('A'):
1303 vty_previous_line (vty);
1304 break;
1305 case ('B'):
1306 vty_next_line (vty);
1307 break;
1308 case ('C'):
1309 vty_forward_char (vty);
1310 break;
1311 case ('D'):
1312 vty_backward_char (vty);
1313 break;
1314 default:
1315 break;
1318 /* Go back to normal mode. */
1319 vty->escape = VTY_NORMAL;
1322 /* Quit print out to the buffer. */
1323 static void
1324 vty_buffer_reset (struct vty *vty)
1326 buffer_reset (vty->obuf);
1327 vty_prompt (vty);
1328 vty_redraw_line (vty);
1331 /* Read data via vty socket. */
1332 static int
1333 vty_read (struct thread *thread)
1335 int i;
1336 int nbytes;
1337 unsigned char buf[VTY_READ_BUFSIZ];
1339 int vty_sock = THREAD_FD (thread);
1340 struct vty *vty = THREAD_ARG (thread);
1341 vty->t_read = NULL;
1343 /* Read raw data from socket */
1344 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1346 if (nbytes < 0)
1348 if (ERRNO_IO_RETRY(errno))
1350 vty_event (VTY_READ, vty_sock, vty);
1351 return 0;
1353 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1354 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1355 __func__, vty->fd, safe_strerror(errno));
1357 buffer_reset(vty->obuf);
1358 vty->status = VTY_CLOSE;
1361 for (i = 0; i < nbytes; i++)
1363 if (buf[i] == IAC)
1365 if (!vty->iac)
1367 vty->iac = 1;
1368 continue;
1370 else
1372 vty->iac = 0;
1376 if (vty->iac_sb_in_progress && !vty->iac)
1378 if (vty->sb_len < sizeof(vty->sb_buf))
1379 vty->sb_buf[vty->sb_len] = buf[i];
1380 vty->sb_len++;
1381 continue;
1384 if (vty->iac)
1386 /* In case of telnet command */
1387 int ret = 0;
1388 ret = vty_telnet_option (vty, buf + i, nbytes - i);
1389 vty->iac = 0;
1390 i += ret;
1391 continue;
1395 if (vty->status == VTY_MORE)
1397 switch (buf[i])
1399 case CONTROL('C'):
1400 case 'q':
1401 case 'Q':
1402 vty_buffer_reset (vty);
1403 break;
1404 #if 0 /* More line does not work for "show ip bgp". */
1405 case '\n':
1406 case '\r':
1407 vty->status = VTY_MORELINE;
1408 break;
1409 #endif
1410 default:
1411 break;
1413 continue;
1416 /* Escape character. */
1417 if (vty->escape == VTY_ESCAPE)
1419 vty_escape_map (buf[i], vty);
1420 continue;
1423 /* Pre-escape status. */
1424 if (vty->escape == VTY_PRE_ESCAPE)
1426 switch (buf[i])
1428 case '[':
1429 vty->escape = VTY_ESCAPE;
1430 break;
1431 case 'b':
1432 vty_backward_word (vty);
1433 vty->escape = VTY_NORMAL;
1434 break;
1435 case 'f':
1436 vty_forward_word (vty);
1437 vty->escape = VTY_NORMAL;
1438 break;
1439 case 'd':
1440 vty_forward_kill_word (vty);
1441 vty->escape = VTY_NORMAL;
1442 break;
1443 case CONTROL('H'):
1444 case 0x7f:
1445 vty_backward_kill_word (vty);
1446 vty->escape = VTY_NORMAL;
1447 break;
1448 default:
1449 vty->escape = VTY_NORMAL;
1450 break;
1452 continue;
1455 switch (buf[i])
1457 case CONTROL('A'):
1458 vty_beginning_of_line (vty);
1459 break;
1460 case CONTROL('B'):
1461 vty_backward_char (vty);
1462 break;
1463 case CONTROL('C'):
1464 vty_stop_input (vty);
1465 break;
1466 case CONTROL('D'):
1467 vty_delete_char (vty);
1468 break;
1469 case CONTROL('E'):
1470 vty_end_of_line (vty);
1471 break;
1472 case CONTROL('F'):
1473 vty_forward_char (vty);
1474 break;
1475 case CONTROL('H'):
1476 case 0x7f:
1477 vty_delete_backward_char (vty);
1478 break;
1479 case CONTROL('K'):
1480 vty_kill_line (vty);
1481 break;
1482 case CONTROL('N'):
1483 vty_next_line (vty);
1484 break;
1485 case CONTROL('P'):
1486 vty_previous_line (vty);
1487 break;
1488 case CONTROL('T'):
1489 vty_transpose_chars (vty);
1490 break;
1491 case CONTROL('U'):
1492 vty_kill_line_from_beginning (vty);
1493 break;
1494 case CONTROL('W'):
1495 vty_backward_kill_word (vty);
1496 break;
1497 case CONTROL('Z'):
1498 vty_end_config (vty);
1499 break;
1500 case '\n':
1501 case '\r':
1502 vty_out (vty, "%s", VTY_NEWLINE);
1503 vty_execute (vty);
1504 break;
1505 case '\t':
1506 vty_complete_command (vty);
1507 break;
1508 case '?':
1509 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1510 vty_self_insert (vty, buf[i]);
1511 else
1512 vty_describe_command (vty);
1513 break;
1514 case '\033':
1515 if (i + 1 < nbytes && buf[i + 1] == '[')
1517 vty->escape = VTY_ESCAPE;
1518 i++;
1520 else
1521 vty->escape = VTY_PRE_ESCAPE;
1522 break;
1523 default:
1524 if (buf[i] > 31 && buf[i] < 127)
1525 vty_self_insert (vty, buf[i]);
1526 break;
1530 /* Check status. */
1531 if (vty->status == VTY_CLOSE)
1532 vty_close (vty);
1533 else
1535 vty_event (VTY_WRITE, vty_sock, vty);
1536 vty_event (VTY_READ, vty_sock, vty);
1538 return 0;
1541 /* Flush buffer to the vty. */
1542 static int
1543 vty_flush (struct thread *thread)
1545 int erase;
1546 buffer_status_t flushrc;
1547 int vty_sock = THREAD_FD (thread);
1548 struct vty *vty = THREAD_ARG (thread);
1550 vty->t_write = NULL;
1552 /* Tempolary disable read thread. */
1553 if ((vty->lines == 0) && vty->t_read)
1555 thread_cancel (vty->t_read);
1556 vty->t_read = NULL;
1559 /* Function execution continue. */
1560 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1562 /* N.B. if width is 0, that means we don't know the window size. */
1563 if ((vty->lines == 0) || (vty->width == 0))
1564 flushrc = buffer_flush_available(vty->obuf, vty->fd);
1565 else if (vty->status == VTY_MORELINE)
1566 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1567 1, erase, 0);
1568 else
1569 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1570 vty->lines >= 0 ? vty->lines :
1571 vty->height,
1572 erase, 0);
1573 switch (flushrc)
1575 case BUFFER_ERROR:
1576 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1577 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1578 vty->fd);
1579 buffer_reset(vty->obuf);
1580 vty_close(vty);
1581 return 0;
1582 case BUFFER_EMPTY:
1583 if (vty->status == VTY_CLOSE)
1584 vty_close (vty);
1585 else
1587 vty->status = VTY_NORMAL;
1588 if (vty->lines == 0)
1589 vty_event (VTY_READ, vty_sock, vty);
1591 break;
1592 case BUFFER_PENDING:
1593 /* There is more data waiting to be written. */
1594 vty->status = VTY_MORE;
1595 if (vty->lines == 0)
1596 vty_event (VTY_WRITE, vty_sock, vty);
1597 break;
1600 return 0;
1603 /* Create new vty structure. */
1604 static struct vty *
1605 vty_create (int vty_sock, union sockunion *su)
1607 struct vty *vty;
1609 /* Allocate new vty structure and set up default values. */
1610 vty = vty_new ();
1611 vty->fd = vty_sock;
1612 vty->type = VTY_TERM;
1613 vty->address = sockunion_su2str (su);
1614 if (no_password_check)
1616 if (host.advanced)
1617 vty->node = ENABLE_NODE;
1618 else
1619 vty->node = VIEW_NODE;
1621 else
1622 vty->node = AUTH_NODE;
1623 vty->fail = 0;
1624 vty->cp = 0;
1625 vty_clear_buf (vty);
1626 vty->length = 0;
1627 memset (vty->hist, 0, sizeof (vty->hist));
1628 vty->hp = 0;
1629 vty->hindex = 0;
1630 vector_set_index (vtyvec, vty_sock, vty);
1631 vty->status = VTY_NORMAL;
1632 vty->v_timeout = vty_timeout_val;
1633 if (host.lines >= 0)
1634 vty->lines = host.lines;
1635 else
1636 vty->lines = -1;
1637 vty->iac = 0;
1638 vty->iac_sb_in_progress = 0;
1639 vty->sb_len = 0;
1641 if (! no_password_check)
1643 /* Vty is not available if password isn't set. */
1644 if (host.password == NULL && host.password_encrypt == NULL)
1646 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1647 vty->status = VTY_CLOSE;
1648 vty_close (vty);
1649 return NULL;
1653 /* Say hello to the world. */
1654 vty_hello (vty);
1655 if (! no_password_check)
1656 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1658 /* Setting up terminal. */
1659 vty_will_echo (vty);
1660 vty_will_suppress_go_ahead (vty);
1662 vty_dont_linemode (vty);
1663 vty_do_window_size (vty);
1664 /* vty_dont_lflow_ahead (vty); */
1666 vty_prompt (vty);
1668 /* Add read/write thread. */
1669 vty_event (VTY_WRITE, vty_sock, vty);
1670 vty_event (VTY_READ, vty_sock, vty);
1672 return vty;
1675 /* Accept connection from the network. */
1676 static int
1677 vty_accept (struct thread *thread)
1679 int vty_sock;
1680 struct vty *vty;
1681 union sockunion su;
1682 int ret;
1683 unsigned int on;
1684 int accept_sock;
1685 struct prefix *p = NULL;
1686 struct access_list *acl = NULL;
1688 accept_sock = THREAD_FD (thread);
1690 /* We continue hearing vty socket. */
1691 vty_event (VTY_SERV, accept_sock, NULL);
1693 memset (&su, 0, sizeof (union sockunion));
1695 /* We can handle IPv4 or IPv6 socket. */
1696 vty_sock = sockunion_accept (accept_sock, &su);
1697 if (vty_sock < 0)
1699 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1700 return -1;
1702 set_nonblocking(vty_sock);
1704 p = sockunion2hostprefix (&su);
1706 /* VTY's accesslist apply. */
1707 if (p->family == AF_INET && vty_accesslist_name)
1709 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1710 (access_list_apply (acl, p) == FILTER_DENY))
1712 char *buf;
1713 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1714 (buf = sockunion_su2str (&su)));
1715 free (buf);
1716 close (vty_sock);
1718 /* continue accepting connections */
1719 vty_event (VTY_SERV, accept_sock, NULL);
1721 prefix_free (p);
1723 return 0;
1727 #ifdef HAVE_IPV6
1728 /* VTY's ipv6 accesslist apply. */
1729 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1731 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1732 (access_list_apply (acl, p) == FILTER_DENY))
1734 char *buf;
1735 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1736 (buf = sockunion_su2str (&su)));
1737 free (buf);
1738 close (vty_sock);
1740 /* continue accepting connections */
1741 vty_event (VTY_SERV, accept_sock, NULL);
1743 prefix_free (p);
1745 return 0;
1748 #endif /* HAVE_IPV6 */
1750 prefix_free (p);
1752 on = 1;
1753 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1754 (char *) &on, sizeof (on));
1755 if (ret < 0)
1756 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
1757 safe_strerror (errno));
1759 vty = vty_create (vty_sock, &su);
1761 return 0;
1764 #if defined(HAVE_IPV6) && !defined(NRL)
1765 static void
1766 vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1768 int ret;
1769 struct addrinfo req;
1770 struct addrinfo *ainfo;
1771 struct addrinfo *ainfo_save;
1772 int sock;
1773 char port_str[BUFSIZ];
1775 memset (&req, 0, sizeof (struct addrinfo));
1776 req.ai_flags = AI_PASSIVE;
1777 req.ai_family = AF_UNSPEC;
1778 req.ai_socktype = SOCK_STREAM;
1779 sprintf (port_str, "%d", port);
1780 port_str[sizeof (port_str) - 1] = '\0';
1782 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1784 if (ret != 0)
1786 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1787 exit (1);
1790 ainfo_save = ainfo;
1794 if (ainfo->ai_family != AF_INET
1795 #ifdef HAVE_IPV6
1796 && ainfo->ai_family != AF_INET6
1797 #endif /* HAVE_IPV6 */
1799 continue;
1801 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1802 if (sock < 0)
1803 continue;
1805 sockopt_reuseaddr (sock);
1806 sockopt_reuseport (sock);
1808 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1809 if (ret < 0)
1811 close (sock); /* Avoid sd leak. */
1812 continue;
1815 ret = listen (sock, 3);
1816 if (ret < 0)
1818 close (sock); /* Avoid sd leak. */
1819 continue;
1822 vty_event (VTY_SERV, sock, NULL);
1824 while ((ainfo = ainfo->ai_next) != NULL);
1826 freeaddrinfo (ainfo_save);
1828 #endif /* HAVE_IPV6 && ! NRL */
1830 /* Make vty server socket. */
1831 static void
1832 vty_serv_sock_family (const char* addr, unsigned short port, int family)
1834 int ret;
1835 union sockunion su;
1836 int accept_sock;
1837 void* naddr=NULL;
1839 memset (&su, 0, sizeof (union sockunion));
1840 su.sa.sa_family = family;
1841 if(addr)
1842 switch(family)
1844 case AF_INET:
1845 naddr=&su.sin.sin_addr;
1846 #ifdef HAVE_IPV6
1847 case AF_INET6:
1848 naddr=&su.sin6.sin6_addr;
1849 #endif
1852 if(naddr)
1853 switch(inet_pton(family,addr,naddr))
1855 case -1:
1856 zlog_err("bad address %s",addr);
1857 naddr=NULL;
1858 break;
1859 case 0:
1860 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
1861 naddr=NULL;
1864 /* Make new socket. */
1865 accept_sock = sockunion_stream_socket (&su);
1866 if (accept_sock < 0)
1867 return;
1869 /* This is server, so reuse address. */
1870 sockopt_reuseaddr (accept_sock);
1871 sockopt_reuseport (accept_sock);
1873 /* Bind socket to universal address and given port. */
1874 ret = sockunion_bind (accept_sock, &su, port, naddr);
1875 if (ret < 0)
1877 zlog_warn("can't bind socket");
1878 close (accept_sock); /* Avoid sd leak. */
1879 return;
1882 /* Listen socket under queue 3. */
1883 ret = listen (accept_sock, 3);
1884 if (ret < 0)
1886 zlog (NULL, LOG_WARNING, "can't listen socket");
1887 close (accept_sock); /* Avoid sd leak. */
1888 return;
1891 /* Add vty server event. */
1892 vty_event (VTY_SERV, accept_sock, NULL);
1895 #ifdef VTYSH
1896 /* For sockaddr_un. */
1897 #include <sys/un.h>
1899 /* VTY shell UNIX domain socket. */
1900 static void
1901 vty_serv_un (const char *path)
1903 int ret;
1904 int sock, len;
1905 struct sockaddr_un serv;
1906 mode_t old_mask;
1907 struct zprivs_ids_t ids;
1909 /* First of all, unlink existing socket */
1910 unlink (path);
1912 /* Set umask */
1913 old_mask = umask (0007);
1915 /* Make UNIX domain socket. */
1916 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1917 if (sock < 0)
1919 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
1920 return;
1923 /* Make server socket. */
1924 memset (&serv, 0, sizeof (struct sockaddr_un));
1925 serv.sun_family = AF_UNIX;
1926 strncpy (serv.sun_path, path, strlen (path));
1927 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1928 len = serv.sun_len = SUN_LEN(&serv);
1929 #else
1930 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1931 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
1933 ret = bind (sock, (struct sockaddr *) &serv, len);
1934 if (ret < 0)
1936 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
1937 close (sock); /* Avoid sd leak. */
1938 return;
1941 ret = listen (sock, 5);
1942 if (ret < 0)
1944 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
1945 close (sock); /* Avoid sd leak. */
1946 return;
1949 umask (old_mask);
1951 zprivs_get_ids(&ids);
1953 if (ids.gid_vty > 0)
1955 /* set group of socket */
1956 if ( chown (path, -1, ids.gid_vty) )
1958 zlog_err ("vty_serv_un: could chown socket, %s",
1959 safe_strerror (errno) );
1963 vty_event (VTYSH_SERV, sock, NULL);
1966 /* #define VTYSH_DEBUG 1 */
1968 static int
1969 vtysh_accept (struct thread *thread)
1971 int accept_sock;
1972 int sock;
1973 int client_len;
1974 struct sockaddr_un client;
1975 struct vty *vty;
1977 accept_sock = THREAD_FD (thread);
1979 vty_event (VTYSH_SERV, accept_sock, NULL);
1981 memset (&client, 0, sizeof (struct sockaddr_un));
1982 client_len = sizeof (struct sockaddr_un);
1984 sock = accept (accept_sock, (struct sockaddr *) &client,
1985 (socklen_t *) &client_len);
1987 if (sock < 0)
1989 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1990 return -1;
1993 if (set_nonblocking(sock) < 0)
1995 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
1996 " %s, closing", sock, safe_strerror (errno));
1997 close (sock);
1998 return -1;
2001 #ifdef VTYSH_DEBUG
2002 printf ("VTY shell accept\n");
2003 #endif /* VTYSH_DEBUG */
2005 vty = vty_new ();
2006 vty->fd = sock;
2007 vty->type = VTY_SHELL_SERV;
2008 vty->node = VIEW_NODE;
2010 vty_event (VTYSH_READ, sock, vty);
2012 return 0;
2015 static int
2016 vtysh_flush(struct vty *vty)
2018 switch (buffer_flush_available(vty->obuf, vty->fd))
2020 case BUFFER_PENDING:
2021 vty_event(VTYSH_WRITE, vty->fd, vty);
2022 break;
2023 case BUFFER_ERROR:
2024 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2025 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2026 buffer_reset(vty->obuf);
2027 vty_close(vty);
2028 return -1;
2029 break;
2030 case BUFFER_EMPTY:
2031 break;
2033 return 0;
2036 static int
2037 vtysh_read (struct thread *thread)
2039 int ret;
2040 int sock;
2041 int nbytes;
2042 struct vty *vty;
2043 unsigned char buf[VTY_READ_BUFSIZ];
2044 unsigned char *p;
2045 u_char header[4] = {0, 0, 0, 0};
2047 sock = THREAD_FD (thread);
2048 vty = THREAD_ARG (thread);
2049 vty->t_read = NULL;
2051 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
2053 if (nbytes < 0)
2055 if (ERRNO_IO_RETRY(errno))
2057 vty_event (VTYSH_READ, sock, vty);
2058 return 0;
2060 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2061 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2062 __func__, sock, safe_strerror(errno));
2064 buffer_reset(vty->obuf);
2065 vty_close (vty);
2066 #ifdef VTYSH_DEBUG
2067 printf ("close vtysh\n");
2068 #endif /* VTYSH_DEBUG */
2069 return 0;
2072 #ifdef VTYSH_DEBUG
2073 printf ("line: %.*s\n", nbytes, buf);
2074 #endif /* VTYSH_DEBUG */
2076 for (p = buf; p < buf+nbytes; p++)
2078 vty_ensure(vty, vty->length+1);
2079 vty->buf[vty->length++] = *p;
2080 if (*p == '\0')
2082 /* Pass this line to parser. */
2083 ret = vty_execute (vty);
2084 /* Note that vty_execute clears the command buffer and resets
2085 vty->length to 0. */
2087 /* Return result. */
2088 #ifdef VTYSH_DEBUG
2089 printf ("result: %d\n", ret);
2090 printf ("vtysh node: %d\n", vty->node);
2091 #endif /* VTYSH_DEBUG */
2093 header[3] = ret;
2094 buffer_put(vty->obuf, header, 4);
2096 if (!vty->t_write && (vtysh_flush(vty) < 0))
2097 /* Try to flush results; exit if a write error occurs. */
2098 return 0;
2102 vty_event (VTYSH_READ, sock, vty);
2104 return 0;
2107 static int
2108 vtysh_write (struct thread *thread)
2110 struct vty *vty = THREAD_ARG (thread);
2112 vty->t_write = NULL;
2113 vtysh_flush(vty);
2114 return 0;
2117 #endif /* VTYSH */
2119 /* Determine address family to bind. */
2120 void
2121 vty_serv_sock (const char *addr, unsigned short port, const char *path)
2123 /* If port is set to 0, do not listen on TCP/IP at all! */
2124 if (port)
2127 #ifdef HAVE_IPV6
2128 #ifdef NRL
2129 vty_serv_sock_family (addr, port, AF_INET);
2130 vty_serv_sock_family (addr, port, AF_INET6);
2131 #else /* ! NRL */
2132 vty_serv_sock_addrinfo (addr, port);
2133 #endif /* NRL*/
2134 #else /* ! HAVE_IPV6 */
2135 vty_serv_sock_family (addr,port, AF_INET);
2136 #endif /* HAVE_IPV6 */
2139 #ifdef VTYSH
2140 vty_serv_un (path);
2141 #endif /* VTYSH */
2144 /* Close vty interface. Warning: call this only from functions that
2145 will be careful not to access the vty afterwards (since it has
2146 now been freed). This is safest from top-level functions (called
2147 directly by the thread dispatcher). */
2148 void
2149 vty_close (struct vty *vty)
2151 int i;
2153 /* Cancel threads.*/
2154 if (vty->t_read)
2155 thread_cancel (vty->t_read);
2156 if (vty->t_write)
2157 thread_cancel (vty->t_write);
2158 if (vty->t_timeout)
2159 thread_cancel (vty->t_timeout);
2161 /* Flush buffer. */
2162 buffer_flush_all (vty->obuf, vty->fd);
2164 /* Free input buffer. */
2165 buffer_free (vty->obuf);
2167 /* Free command history. */
2168 for (i = 0; i < VTY_MAXHIST; i++)
2169 if (vty->hist[i])
2170 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2172 /* Unset vector. */
2173 vector_unset (vtyvec, vty->fd);
2175 /* Close socket. */
2176 if (vty->fd > 0)
2177 close (vty->fd);
2179 if (vty->address)
2180 XFREE (MTYPE_TMP, vty->address);
2181 if (vty->buf)
2182 XFREE (MTYPE_VTY, vty->buf);
2184 /* Check configure. */
2185 vty_config_unlock (vty);
2187 /* OK free vty. */
2188 XFREE (MTYPE_VTY, vty);
2191 /* When time out occur output message then close connection. */
2192 static int
2193 vty_timeout (struct thread *thread)
2195 struct vty *vty;
2197 vty = THREAD_ARG (thread);
2198 vty->t_timeout = NULL;
2199 vty->v_timeout = 0;
2201 /* Clear buffer*/
2202 buffer_reset (vty->obuf);
2203 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2205 /* Close connection. */
2206 vty->status = VTY_CLOSE;
2207 vty_close (vty);
2209 return 0;
2212 /* Read up configuration file from file_name. */
2213 static void
2214 vty_read_file (FILE *confp)
2216 int ret;
2217 struct vty *vty;
2219 vty = vty_new ();
2220 vty->fd = 0; /* stdout */
2221 vty->type = VTY_TERM;
2222 vty->node = CONFIG_NODE;
2224 /* Execute configuration file */
2225 ret = config_from_file (vty, confp);
2227 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
2229 switch (ret)
2231 case CMD_ERR_AMBIGUOUS:
2232 fprintf (stderr, "Ambiguous command.\n");
2233 break;
2234 case CMD_ERR_NO_MATCH:
2235 fprintf (stderr, "There is no such command.\n");
2236 break;
2238 fprintf (stderr, "Error occured during reading below line.\n%s\n",
2239 vty->buf);
2240 vty_close (vty);
2241 exit (1);
2244 vty_close (vty);
2247 static FILE *
2248 vty_use_backup_config (char *fullpath)
2250 char *fullpath_sav, *fullpath_tmp;
2251 FILE *ret = NULL;
2252 struct stat buf;
2253 int tmp, sav;
2254 int c;
2255 char buffer[512];
2257 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2258 strcpy (fullpath_sav, fullpath);
2259 strcat (fullpath_sav, CONF_BACKUP_EXT);
2260 if (stat (fullpath_sav, &buf) == -1)
2262 free (fullpath_sav);
2263 return NULL;
2266 fullpath_tmp = malloc (strlen (fullpath) + 8);
2267 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2269 /* Open file to configuration write. */
2270 tmp = mkstemp (fullpath_tmp);
2271 if (tmp < 0)
2273 free (fullpath_sav);
2274 free (fullpath_tmp);
2275 return NULL;
2278 sav = open (fullpath_sav, O_RDONLY);
2279 if (sav < 0)
2281 unlink (fullpath_tmp);
2282 free (fullpath_sav);
2283 free (fullpath_tmp);
2284 return NULL;
2287 while((c = read (sav, buffer, 512)) > 0)
2288 write (tmp, buffer, c);
2290 close (sav);
2291 close (tmp);
2293 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2295 unlink (fullpath_tmp);
2296 free (fullpath_sav);
2297 free (fullpath_tmp);
2298 return NULL;
2301 if (link (fullpath_tmp, fullpath) == 0)
2302 ret = fopen (fullpath, "r");
2304 unlink (fullpath_tmp);
2306 free (fullpath_sav);
2307 free (fullpath_tmp);
2308 return ret;
2311 /* Read up configuration file from file_name. */
2312 void
2313 vty_read_config (char *config_file,
2314 char *config_default_dir)
2316 char cwd[MAXPATHLEN];
2317 FILE *confp = NULL;
2318 char *fullpath;
2319 char *tmp = NULL;
2321 /* If -f flag specified. */
2322 if (config_file != NULL)
2324 if (! IS_DIRECTORY_SEP (config_file[0]))
2326 getcwd (cwd, MAXPATHLEN);
2327 tmp = XMALLOC (MTYPE_TMP,
2328 strlen (cwd) + strlen (config_file) + 2);
2329 sprintf (tmp, "%s/%s", cwd, config_file);
2330 fullpath = tmp;
2332 else
2333 fullpath = config_file;
2335 confp = fopen (fullpath, "r");
2337 if (confp == NULL)
2339 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2340 __func__, fullpath, safe_strerror (errno));
2342 confp = vty_use_backup_config (fullpath);
2343 if (confp)
2344 fprintf (stderr, "WARNING: using backup configuration file!\n");
2345 else
2347 fprintf (stderr, "can't open configuration file [%s]\n",
2348 config_file);
2349 exit(1);
2353 else
2355 #ifdef VTYSH
2356 int ret;
2357 struct stat conf_stat;
2359 /* !!!!PLEASE LEAVE!!!!
2360 * This is NEEDED for use with vtysh -b, or else you can get
2361 * a real configuration food fight with a lot garbage in the
2362 * merged configuration file it creates coming from the per
2363 * daemon configuration files. This also allows the daemons
2364 * to start if there default configuration file is not
2365 * present or ignore them, as needed when using vtysh -b to
2366 * configure the daemons at boot - MAG
2369 /* Stat for vtysh Zebra.conf, if found startup and wait for
2370 * boot configuration
2373 if ( strstr(config_default_dir, "vtysh") == NULL)
2375 ret = stat (integrate_default, &conf_stat);
2376 if (ret >= 0)
2377 return;
2379 #endif /* VTYSH */
2381 confp = fopen (config_default_dir, "r");
2382 if (confp == NULL)
2384 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2385 __func__, config_default_dir, safe_strerror (errno));
2387 confp = vty_use_backup_config (config_default_dir);
2388 if (confp)
2390 fprintf (stderr, "WARNING: using backup configuration file!\n");
2391 fullpath = config_default_dir;
2393 else
2395 fprintf (stderr, "can't open configuration file [%s]\n",
2396 config_default_dir);
2397 exit (1);
2400 else
2401 fullpath = config_default_dir;
2404 vty_read_file (confp);
2406 fclose (confp);
2408 host_config_set (fullpath);
2410 if (tmp)
2411 XFREE (MTYPE_TMP, fullpath);
2414 /* Small utility function which output log to the VTY. */
2415 void
2416 vty_log (const char *level, const char *proto_str,
2417 const char *format, struct timestamp_control *ctl, va_list va)
2419 unsigned int i;
2420 struct vty *vty;
2422 if (!vtyvec)
2423 return;
2425 for (i = 0; i < vector_active (vtyvec); i++)
2426 if ((vty = vector_slot (vtyvec, i)) != NULL)
2427 if (vty->monitor)
2429 va_list ac;
2430 va_copy(ac, va);
2431 vty_log_out (vty, level, proto_str, format, ctl, ac);
2432 va_end(ac);
2436 /* Async-signal-safe version of vty_log for fixed strings. */
2437 void
2438 vty_log_fixed (const char *buf, size_t len)
2440 unsigned int i;
2441 struct iovec iov[2];
2443 /* vty may not have been initialised */
2444 if (!vtyvec)
2445 return;
2447 iov[0].iov_base = (void *)buf;
2448 iov[0].iov_len = len;
2449 iov[1].iov_base = (void *)"\r\n";
2450 iov[1].iov_len = 2;
2452 for (i = 0; i < vector_active (vtyvec); i++)
2454 struct vty *vty;
2455 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2456 /* N.B. We don't care about the return code, since process is
2457 most likely just about to die anyway. */
2458 writev(vty->fd, iov, 2);
2463 vty_config_lock (struct vty *vty)
2465 if (vty_config == 0)
2467 vty->config = 1;
2468 vty_config = 1;
2470 return vty->config;
2474 vty_config_unlock (struct vty *vty)
2476 if (vty_config == 1 && vty->config == 1)
2478 vty->config = 0;
2479 vty_config = 0;
2481 return vty->config;
2484 /* Master of the threads. */
2485 static struct thread_master *master;
2487 static void
2488 vty_event (enum event event, int sock, struct vty *vty)
2490 struct thread *vty_serv_thread;
2492 switch (event)
2494 case VTY_SERV:
2495 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2496 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2497 break;
2498 #ifdef VTYSH
2499 case VTYSH_SERV:
2500 thread_add_read (master, vtysh_accept, vty, sock);
2501 break;
2502 case VTYSH_READ:
2503 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2504 break;
2505 case VTYSH_WRITE:
2506 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
2507 break;
2508 #endif /* VTYSH */
2509 case VTY_READ:
2510 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2512 /* Time out treatment. */
2513 if (vty->v_timeout)
2515 if (vty->t_timeout)
2516 thread_cancel (vty->t_timeout);
2517 vty->t_timeout =
2518 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2520 break;
2521 case VTY_WRITE:
2522 if (! vty->t_write)
2523 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2524 break;
2525 case VTY_TIMEOUT_RESET:
2526 if (vty->t_timeout)
2528 thread_cancel (vty->t_timeout);
2529 vty->t_timeout = NULL;
2531 if (vty->v_timeout)
2533 vty->t_timeout =
2534 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2536 break;
2540 DEFUN (config_who,
2541 config_who_cmd,
2542 "who",
2543 "Display who is on vty\n")
2545 unsigned int i;
2546 struct vty *v;
2548 for (i = 0; i < vector_active (vtyvec); i++)
2549 if ((v = vector_slot (vtyvec, i)) != NULL)
2550 vty_out (vty, "%svty[%d] connected from %s.%s",
2551 v->config ? "*" : " ",
2552 i, v->address, VTY_NEWLINE);
2553 return CMD_SUCCESS;
2556 /* Move to vty configuration mode. */
2557 DEFUN (line_vty,
2558 line_vty_cmd,
2559 "line vty",
2560 "Configure a terminal line\n"
2561 "Virtual terminal\n")
2563 vty->node = VTY_NODE;
2564 return CMD_SUCCESS;
2567 /* Set time out value. */
2568 static int
2569 exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
2571 unsigned long timeout = 0;
2573 /* min_str and sec_str are already checked by parser. So it must be
2574 all digit string. */
2575 if (min_str)
2577 timeout = strtol (min_str, NULL, 10);
2578 timeout *= 60;
2580 if (sec_str)
2581 timeout += strtol (sec_str, NULL, 10);
2583 vty_timeout_val = timeout;
2584 vty->v_timeout = timeout;
2585 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2588 return CMD_SUCCESS;
2591 DEFUN (exec_timeout_min,
2592 exec_timeout_min_cmd,
2593 "exec-timeout <0-35791>",
2594 "Set timeout value\n"
2595 "Timeout value in minutes\n")
2597 return exec_timeout (vty, argv[0], NULL);
2600 DEFUN (exec_timeout_sec,
2601 exec_timeout_sec_cmd,
2602 "exec-timeout <0-35791> <0-2147483>",
2603 "Set the EXEC timeout\n"
2604 "Timeout in minutes\n"
2605 "Timeout in seconds\n")
2607 return exec_timeout (vty, argv[0], argv[1]);
2610 DEFUN (no_exec_timeout,
2611 no_exec_timeout_cmd,
2612 "no exec-timeout",
2613 NO_STR
2614 "Set the EXEC timeout\n")
2616 return exec_timeout (vty, NULL, NULL);
2619 /* Set vty access class. */
2620 DEFUN (vty_access_class,
2621 vty_access_class_cmd,
2622 "access-class WORD",
2623 "Filter connections based on an IP access list\n"
2624 "IP access list\n")
2626 if (vty_accesslist_name)
2627 XFREE(MTYPE_VTY, vty_accesslist_name);
2629 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2631 return CMD_SUCCESS;
2634 /* Clear vty access class. */
2635 DEFUN (no_vty_access_class,
2636 no_vty_access_class_cmd,
2637 "no access-class [WORD]",
2638 NO_STR
2639 "Filter connections based on an IP access list\n"
2640 "IP access list\n")
2642 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2644 vty_out (vty, "Access-class is not currently applied to vty%s",
2645 VTY_NEWLINE);
2646 return CMD_WARNING;
2649 XFREE(MTYPE_VTY, vty_accesslist_name);
2651 vty_accesslist_name = NULL;
2653 return CMD_SUCCESS;
2656 #ifdef HAVE_IPV6
2657 /* Set vty access class. */
2658 DEFUN (vty_ipv6_access_class,
2659 vty_ipv6_access_class_cmd,
2660 "ipv6 access-class WORD",
2661 IPV6_STR
2662 "Filter connections based on an IP access list\n"
2663 "IPv6 access list\n")
2665 if (vty_ipv6_accesslist_name)
2666 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2668 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2670 return CMD_SUCCESS;
2673 /* Clear vty access class. */
2674 DEFUN (no_vty_ipv6_access_class,
2675 no_vty_ipv6_access_class_cmd,
2676 "no ipv6 access-class [WORD]",
2677 NO_STR
2678 IPV6_STR
2679 "Filter connections based on an IP access list\n"
2680 "IPv6 access list\n")
2682 if (! vty_ipv6_accesslist_name ||
2683 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2685 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2686 VTY_NEWLINE);
2687 return CMD_WARNING;
2690 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2692 vty_ipv6_accesslist_name = NULL;
2694 return CMD_SUCCESS;
2696 #endif /* HAVE_IPV6 */
2698 /* vty login. */
2699 DEFUN (vty_login,
2700 vty_login_cmd,
2701 "login",
2702 "Enable password checking\n")
2704 no_password_check = 0;
2705 return CMD_SUCCESS;
2708 DEFUN (no_vty_login,
2709 no_vty_login_cmd,
2710 "no login",
2711 NO_STR
2712 "Enable password checking\n")
2714 no_password_check = 1;
2715 return CMD_SUCCESS;
2718 DEFUN (service_advanced_vty,
2719 service_advanced_vty_cmd,
2720 "service advanced-vty",
2721 "Set up miscellaneous service\n"
2722 "Enable advanced mode vty interface\n")
2724 host.advanced = 1;
2725 return CMD_SUCCESS;
2728 DEFUN (no_service_advanced_vty,
2729 no_service_advanced_vty_cmd,
2730 "no service advanced-vty",
2731 NO_STR
2732 "Set up miscellaneous service\n"
2733 "Enable advanced mode vty interface\n")
2735 host.advanced = 0;
2736 return CMD_SUCCESS;
2739 DEFUN (terminal_monitor,
2740 terminal_monitor_cmd,
2741 "terminal monitor",
2742 "Set terminal line parameters\n"
2743 "Copy debug output to the current terminal line\n")
2745 vty->monitor = 1;
2746 return CMD_SUCCESS;
2749 DEFUN (terminal_no_monitor,
2750 terminal_no_monitor_cmd,
2751 "terminal no monitor",
2752 "Set terminal line parameters\n"
2753 NO_STR
2754 "Copy debug output to the current terminal line\n")
2756 vty->monitor = 0;
2757 return CMD_SUCCESS;
2760 ALIAS (terminal_no_monitor,
2761 no_terminal_monitor_cmd,
2762 "no terminal monitor",
2763 NO_STR
2764 "Set terminal line parameters\n"
2765 "Copy debug output to the current terminal line\n")
2767 DEFUN (show_history,
2768 show_history_cmd,
2769 "show history",
2770 SHOW_STR
2771 "Display the session command history\n")
2773 int index;
2775 for (index = vty->hindex + 1; index != vty->hindex;)
2777 if (index == VTY_MAXHIST)
2779 index = 0;
2780 continue;
2783 if (vty->hist[index] != NULL)
2784 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2786 index++;
2789 return CMD_SUCCESS;
2792 /* Display current configuration. */
2793 static int
2794 vty_config_write (struct vty *vty)
2796 vty_out (vty, "line vty%s", VTY_NEWLINE);
2798 if (vty_accesslist_name)
2799 vty_out (vty, " access-class %s%s",
2800 vty_accesslist_name, VTY_NEWLINE);
2802 if (vty_ipv6_accesslist_name)
2803 vty_out (vty, " ipv6 access-class %s%s",
2804 vty_ipv6_accesslist_name, VTY_NEWLINE);
2806 /* exec-timeout */
2807 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2808 vty_out (vty, " exec-timeout %ld %ld%s",
2809 vty_timeout_val / 60,
2810 vty_timeout_val % 60, VTY_NEWLINE);
2812 /* login */
2813 if (no_password_check)
2814 vty_out (vty, " no login%s", VTY_NEWLINE);
2816 vty_out (vty, "!%s", VTY_NEWLINE);
2818 return CMD_SUCCESS;
2821 struct cmd_node vty_node =
2823 VTY_NODE,
2824 "%s(config-line)# ",
2828 /* Reset all VTY status. */
2829 void
2830 vty_reset ()
2832 unsigned int i;
2833 struct vty *vty;
2834 struct thread *vty_serv_thread;
2836 for (i = 0; i < vector_active (vtyvec); i++)
2837 if ((vty = vector_slot (vtyvec, i)) != NULL)
2839 buffer_reset (vty->obuf);
2840 vty->status = VTY_CLOSE;
2841 vty_close (vty);
2844 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
2845 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2847 thread_cancel (vty_serv_thread);
2848 vector_slot (Vvty_serv_thread, i) = NULL;
2849 close (i);
2852 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2854 if (vty_accesslist_name)
2856 XFREE(MTYPE_VTY, vty_accesslist_name);
2857 vty_accesslist_name = NULL;
2860 if (vty_ipv6_accesslist_name)
2862 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2863 vty_ipv6_accesslist_name = NULL;
2867 static void
2868 vty_save_cwd (void)
2870 char cwd[MAXPATHLEN];
2871 char *c;
2873 c = getcwd (cwd, MAXPATHLEN);
2875 if (!c)
2877 chdir (SYSCONFDIR);
2878 getcwd (cwd, MAXPATHLEN);
2881 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2882 strcpy (vty_cwd, cwd);
2885 char *
2886 vty_get_cwd ()
2888 return vty_cwd;
2892 vty_shell (struct vty *vty)
2894 return vty->type == VTY_SHELL ? 1 : 0;
2898 vty_shell_serv (struct vty *vty)
2900 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2903 void
2904 vty_init_vtysh ()
2906 vtyvec = vector_init (VECTOR_MIN_SIZE);
2909 /* Install vty's own commands like `who' command. */
2910 void
2911 vty_init (struct thread_master *master_thread)
2913 /* For further configuration read, preserve current directory. */
2914 vty_save_cwd ();
2916 vtyvec = vector_init (VECTOR_MIN_SIZE);
2918 master = master_thread;
2920 /* Initilize server thread vector. */
2921 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
2923 /* Install bgp top node. */
2924 install_node (&vty_node, vty_config_write);
2926 install_element (VIEW_NODE, &config_who_cmd);
2927 install_element (VIEW_NODE, &show_history_cmd);
2928 install_element (ENABLE_NODE, &config_who_cmd);
2929 install_element (CONFIG_NODE, &line_vty_cmd);
2930 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
2931 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
2932 install_element (CONFIG_NODE, &show_history_cmd);
2933 install_element (ENABLE_NODE, &terminal_monitor_cmd);
2934 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
2935 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
2936 install_element (ENABLE_NODE, &show_history_cmd);
2938 install_default (VTY_NODE);
2939 install_element (VTY_NODE, &exec_timeout_min_cmd);
2940 install_element (VTY_NODE, &exec_timeout_sec_cmd);
2941 install_element (VTY_NODE, &no_exec_timeout_cmd);
2942 install_element (VTY_NODE, &vty_access_class_cmd);
2943 install_element (VTY_NODE, &no_vty_access_class_cmd);
2944 install_element (VTY_NODE, &vty_login_cmd);
2945 install_element (VTY_NODE, &no_vty_login_cmd);
2946 #ifdef HAVE_IPV6
2947 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
2948 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
2949 #endif /* HAVE_IPV6 */