blob: e6bcb61ae478db1e8805e6818c310e667b0dde67 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
paulb21b19c2003-06-15 01:28:29 +000026#include "thread.h"
paul718e3742002-12-13 20:15:29 +000027#include "buffer.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "command.h"
30#include "sockunion.h"
paul718e3742002-12-13 20:15:29 +000031#include "memory.h"
32#include "str.h"
33#include "log.h"
34#include "prefix.h"
35#include "filter.h"
paulb21b19c2003-06-15 01:28:29 +000036#include "vty.h"
pauledd7c242003-06-04 13:59:38 +000037#include "privs.h"
paul718e3742002-12-13 20:15:29 +000038
39/* Vty events */
40enum event
41{
42 VTY_SERV,
43 VTY_READ,
44 VTY_WRITE,
45 VTY_TIMEOUT_RESET,
46#ifdef VTYSH
47 VTYSH_SERV,
ajs49ff6d92004-11-04 19:26:16 +000048 VTYSH_READ,
49 VTYSH_WRITE
paul718e3742002-12-13 20:15:29 +000050#endif /* VTYSH */
51};
52
ajs49ff6d92004-11-04 19:26:16 +000053/* Minimum size of output buffers; to be rounded up to multiple of pagesize. */
54#define VTY_OBUF_SIZE 4096
55
paul718e3742002-12-13 20:15:29 +000056static void vty_event (enum event, int, struct vty *);
57
58/* Extern host structure from command.c */
59extern struct host host;
60
61/* Vector which store each vty structure. */
62static vector vtyvec;
63
64/* Vty timeout value. */
65static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
66
67/* Vty access-class command */
68static char *vty_accesslist_name = NULL;
69
70/* Vty access-calss for IPv6. */
71static char *vty_ipv6_accesslist_name = NULL;
72
73/* VTY server thread. */
74vector Vvty_serv_thread;
75
76/* Current directory. */
77char *vty_cwd = NULL;
78
79/* Configure lock. */
80static int vty_config;
81
82/* Login password check. */
83static int no_password_check = 0;
84
85/* Integrated configuration file path */
86char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
87
88
89/* VTY standard output function. */
90int
91vty_out (struct vty *vty, const char *format, ...)
92{
93 va_list args;
94 int len = 0;
95 int size = 1024;
96 char buf[1024];
97 char *p = NULL;
98
99 va_start (args, format);
100
101 if (vty_shell (vty))
102 vprintf (format, args);
103 else
104 {
105 /* Try to write to initial buffer. */
106 len = vsnprintf (buf, sizeof buf, format, args);
107
108 /* Initial buffer is not enough. */
109 if (len < 0 || len >= size)
110 {
111 while (1)
112 {
113 if (len > -1)
114 size = len + 1;
115 else
116 size = size * 2;
117
118 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
119 if (! p)
120 return -1;
121
122 len = vsnprintf (p, size, format, args);
123
124 if (len > -1 && len < size)
125 break;
126 }
127 }
128
129 /* When initial buffer is enough to store all output. */
130 if (! p)
131 p = buf;
132
133 /* Pointer p must point out buffer. */
ajs49ff6d92004-11-04 19:26:16 +0000134 buffer_write (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000135
136 /* If p is not different with buf, it is allocated buffer. */
137 if (p != buf)
138 XFREE (MTYPE_VTY_OUT_BUF, p);
139 }
140
141 va_end (args);
142
143 return len;
144}
145
146int
147vty_log_out (struct vty *vty, const char *proto_str, const char *format,
148 va_list va)
149{
150 int len;
151 char buf[1024];
152
153 snprintf (buf, sizeof buf, "%s: ", proto_str);
154 write (vty->fd, buf, strlen (proto_str) + 2);
155
156 len = vsnprintf (buf, sizeof buf, format, va);
157 if (len < 0)
158 return -1;
159 write (vty->fd, (u_char *)buf, len);
160
161 snprintf (buf, sizeof buf, "\r\n");
162 write (vty->fd, buf, 2);
163
164 return len;
165}
166
167/* Output current time to the vty. */
168void
169vty_time_print (struct vty *vty, int cr)
170{
171 time_t clock;
172 struct tm *tm;
173#define TIME_BUF 25
174 char buf [TIME_BUF];
175 int ret;
176
177 time (&clock);
178 tm = localtime (&clock);
179
180 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
181 if (ret == 0)
182 {
183 zlog (NULL, LOG_INFO, "strftime error");
184 return;
185 }
186 if (cr)
187 vty_out (vty, "%s\n", buf);
188 else
189 vty_out (vty, "%s ", buf);
190
191 return;
192}
193
194/* Say hello to vty interface. */
195void
196vty_hello (struct vty *vty)
197{
198 if (host.motd)
199 vty_out (vty, host.motd);
200}
201
202/* Put out prompt and wait input from user. */
203static void
204vty_prompt (struct vty *vty)
205{
206 struct utsname names;
207 const char*hostname;
208
209 if (vty->type == VTY_TERM)
210 {
211 hostname = host.name;
212 if (!hostname)
213 {
214 uname (&names);
215 hostname = names.nodename;
216 }
217 vty_out (vty, cmd_prompt (vty->node), hostname);
218 }
219}
220
221/* Send WILL TELOPT_ECHO to remote server. */
222void
223vty_will_echo (struct vty *vty)
224{
paul02ff83c2004-06-11 11:27:03 +0000225 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000226 vty_out (vty, "%s", cmd);
227}
228
229/* Make suppress Go-Ahead telnet option. */
230static void
231vty_will_suppress_go_ahead (struct vty *vty)
232{
paul02ff83c2004-06-11 11:27:03 +0000233 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000234 vty_out (vty, "%s", cmd);
235}
236
237/* Make don't use linemode over telnet. */
238static void
239vty_dont_linemode (struct vty *vty)
240{
paul02ff83c2004-06-11 11:27:03 +0000241 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000242 vty_out (vty, "%s", cmd);
243}
244
245/* Use window size. */
246static void
247vty_do_window_size (struct vty *vty)
248{
paul02ff83c2004-06-11 11:27:03 +0000249 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000250 vty_out (vty, "%s", cmd);
251}
252
253#if 0 /* Currently not used. */
254/* Make don't use lflow vty interface. */
255static void
256vty_dont_lflow_ahead (struct vty *vty)
257{
paul02ff83c2004-06-11 11:27:03 +0000258 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000259 vty_out (vty, "%s", cmd);
260}
261#endif /* 0 */
262
263/* Allocate new vty struct. */
264struct vty *
265vty_new ()
266{
267 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
ajs49ff6d92004-11-04 19:26:16 +0000268 int pgsz = getpagesize();
paul718e3742002-12-13 20:15:29 +0000269
ajs49ff6d92004-11-04 19:26:16 +0000270 new->obuf = (struct buffer *) buffer_new ((((VTY_OBUF_SIZE-1)/pgsz)+1)*pgsz);
paul718e3742002-12-13 20:15:29 +0000271 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
272 new->max = VTY_BUFSIZ;
273 new->sb_buffer = NULL;
274
275 return new;
276}
277
278/* Authentication of vty */
279static void
280vty_auth (struct vty *vty, char *buf)
281{
282 char *passwd = NULL;
283 enum node_type next_node = 0;
284 int fail;
285 char *crypt (const char *, const char *);
286
287 switch (vty->node)
288 {
289 case AUTH_NODE:
290 if (host.encrypt)
291 passwd = host.password_encrypt;
292 else
293 passwd = host.password;
294 if (host.advanced)
295 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
296 else
297 next_node = VIEW_NODE;
298 break;
299 case AUTH_ENABLE_NODE:
300 if (host.encrypt)
301 passwd = host.enable_encrypt;
302 else
303 passwd = host.enable;
304 next_node = ENABLE_NODE;
305 break;
306 }
307
308 if (passwd)
309 {
310 if (host.encrypt)
311 fail = strcmp (crypt(buf, passwd), passwd);
312 else
313 fail = strcmp (buf, passwd);
314 }
315 else
316 fail = 1;
317
318 if (! fail)
319 {
320 vty->fail = 0;
321 vty->node = next_node; /* Success ! */
322 }
323 else
324 {
325 vty->fail++;
326 if (vty->fail >= 3)
327 {
328 if (vty->node == AUTH_NODE)
329 {
330 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
331 vty->status = VTY_CLOSE;
332 }
333 else
334 {
335 /* AUTH_ENABLE_NODE */
336 vty->fail = 0;
337 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
338 vty->node = VIEW_NODE;
339 }
340 }
341 }
342}
343
344/* Command execution over the vty interface. */
345int
346vty_command (struct vty *vty, char *buf)
347{
348 int ret;
349 vector vline;
350
351 /* Split readline string up into the vector */
352 vline = cmd_make_strvec (buf);
353
354 if (vline == NULL)
355 return CMD_SUCCESS;
356
357 ret = cmd_execute_command (vline, vty, NULL);
358
359 if (ret != CMD_SUCCESS)
360 switch (ret)
361 {
362 case CMD_WARNING:
363 if (vty->type == VTY_FILE)
364 vty_out (vty, "Warning...%s", VTY_NEWLINE);
365 break;
366 case CMD_ERR_AMBIGUOUS:
367 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
368 break;
369 case CMD_ERR_NO_MATCH:
370 vty_out (vty, "%% Unknown command.%s", VTY_NEWLINE);
371 break;
372 case CMD_ERR_INCOMPLETE:
373 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
374 break;
375 }
376 cmd_free_strvec (vline);
377
378 return ret;
379}
380
381char telnet_backward_char = 0x08;
382char telnet_space_char = ' ';
383
384/* Basic function to write buffer to vty. */
385static void
386vty_write (struct vty *vty, char *buf, size_t nbytes)
387{
388 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
389 return;
390
391 /* Should we do buffering here ? And make vty_flush (vty) ? */
392 buffer_write (vty->obuf, (u_char *)buf, nbytes);
393}
394
395/* Ensure length of input buffer. Is buffer is short, double it. */
396static void
397vty_ensure (struct vty *vty, int length)
398{
399 if (vty->max <= length)
400 {
401 vty->max *= 2;
402 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
403 }
404}
405
406/* Basic function to insert character into vty. */
407static void
408vty_self_insert (struct vty *vty, char c)
409{
410 int i;
411 int length;
412
413 vty_ensure (vty, vty->length + 1);
414 length = vty->length - vty->cp;
415 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
416 vty->buf[vty->cp] = c;
417
418 vty_write (vty, &vty->buf[vty->cp], length + 1);
419 for (i = 0; i < length; i++)
420 vty_write (vty, &telnet_backward_char, 1);
421
422 vty->cp++;
423 vty->length++;
424}
425
426/* Self insert character 'c' in overwrite mode. */
427static void
428vty_self_insert_overwrite (struct vty *vty, char c)
429{
430 vty_ensure (vty, vty->length + 1);
431 vty->buf[vty->cp++] = c;
432
433 if (vty->cp > vty->length)
434 vty->length++;
435
436 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
437 return;
438
439 vty_write (vty, &c, 1);
440}
441
442/* Insert a word into vty interface with overwrite mode. */
443static void
444vty_insert_word_overwrite (struct vty *vty, char *str)
445{
446 int len = strlen (str);
447 vty_write (vty, str, len);
448 strcpy (&vty->buf[vty->cp], str);
449 vty->cp += len;
450 vty->length = vty->cp;
451}
452
453/* Forward character. */
454static void
455vty_forward_char (struct vty *vty)
456{
457 if (vty->cp < vty->length)
458 {
459 vty_write (vty, &vty->buf[vty->cp], 1);
460 vty->cp++;
461 }
462}
463
464/* Backward character. */
465static void
466vty_backward_char (struct vty *vty)
467{
468 if (vty->cp > 0)
469 {
470 vty->cp--;
471 vty_write (vty, &telnet_backward_char, 1);
472 }
473}
474
475/* Move to the beginning of the line. */
476static void
477vty_beginning_of_line (struct vty *vty)
478{
479 while (vty->cp)
480 vty_backward_char (vty);
481}
482
483/* Move to the end of the line. */
484static void
485vty_end_of_line (struct vty *vty)
486{
487 while (vty->cp < vty->length)
488 vty_forward_char (vty);
489}
490
491static void vty_kill_line_from_beginning (struct vty *);
492static void vty_redraw_line (struct vty *);
493
494/* Print command line history. This function is called from
495 vty_next_line and vty_previous_line. */
496static void
497vty_history_print (struct vty *vty)
498{
499 int length;
500
501 vty_kill_line_from_beginning (vty);
502
503 /* Get previous line from history buffer */
504 length = strlen (vty->hist[vty->hp]);
505 memcpy (vty->buf, vty->hist[vty->hp], length);
506 vty->cp = vty->length = length;
507
508 /* Redraw current line */
509 vty_redraw_line (vty);
510}
511
512/* Show next command line history. */
513void
514vty_next_line (struct vty *vty)
515{
516 int try_index;
517
518 if (vty->hp == vty->hindex)
519 return;
520
521 /* Try is there history exist or not. */
522 try_index = vty->hp;
523 if (try_index == (VTY_MAXHIST - 1))
524 try_index = 0;
525 else
526 try_index++;
527
528 /* If there is not history return. */
529 if (vty->hist[try_index] == NULL)
530 return;
531 else
532 vty->hp = try_index;
533
534 vty_history_print (vty);
535}
536
537/* Show previous command line history. */
538void
539vty_previous_line (struct vty *vty)
540{
541 int try_index;
542
543 try_index = vty->hp;
544 if (try_index == 0)
545 try_index = VTY_MAXHIST - 1;
546 else
547 try_index--;
548
549 if (vty->hist[try_index] == NULL)
550 return;
551 else
552 vty->hp = try_index;
553
554 vty_history_print (vty);
555}
556
557/* This function redraw all of the command line character. */
558static void
559vty_redraw_line (struct vty *vty)
560{
561 vty_write (vty, vty->buf, vty->length);
562 vty->cp = vty->length;
563}
564
565/* Forward word. */
566static void
567vty_forward_word (struct vty *vty)
568{
569 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
570 vty_forward_char (vty);
571
572 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
573 vty_forward_char (vty);
574}
575
576/* Backward word without skipping training space. */
577static void
578vty_backward_pure_word (struct vty *vty)
579{
580 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
581 vty_backward_char (vty);
582}
583
584/* Backward word. */
585static void
586vty_backward_word (struct vty *vty)
587{
588 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
589 vty_backward_char (vty);
590
591 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
592 vty_backward_char (vty);
593}
594
595/* When '^D' is typed at the beginning of the line we move to the down
596 level. */
597static void
598vty_down_level (struct vty *vty)
599{
600 vty_out (vty, "%s", VTY_NEWLINE);
601 config_exit (NULL, vty, 0, NULL);
602 vty_prompt (vty);
603 vty->cp = 0;
604}
605
606/* When '^Z' is received from vty, move down to the enable mode. */
607void
608vty_end_config (struct vty *vty)
609{
610 vty_out (vty, "%s", VTY_NEWLINE);
611
612 switch (vty->node)
613 {
614 case VIEW_NODE:
615 case ENABLE_NODE:
616 /* Nothing to do. */
617 break;
618 case CONFIG_NODE:
619 case INTERFACE_NODE:
620 case ZEBRA_NODE:
621 case RIP_NODE:
622 case RIPNG_NODE:
623 case BGP_NODE:
624 case BGP_VPNV4_NODE:
625 case BGP_IPV4_NODE:
626 case BGP_IPV4M_NODE:
627 case BGP_IPV6_NODE:
628 case RMAP_NODE:
629 case OSPF_NODE:
630 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000631 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000632 case KEYCHAIN_NODE:
633 case KEYCHAIN_KEY_NODE:
634 case MASC_NODE:
635 case VTY_NODE:
636 vty_config_unlock (vty);
637 vty->node = ENABLE_NODE;
638 break;
639 default:
640 /* Unknown node, we have to ignore it. */
641 break;
642 }
643
644 vty_prompt (vty);
645 vty->cp = 0;
646}
647
648/* Delete a charcter at the current point. */
649static void
650vty_delete_char (struct vty *vty)
651{
652 int i;
653 int size;
654
655 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
656 return;
657
658 if (vty->length == 0)
659 {
660 vty_down_level (vty);
661 return;
662 }
663
664 if (vty->cp == vty->length)
665 return; /* completion need here? */
666
667 size = vty->length - vty->cp;
668
669 vty->length--;
670 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
671 vty->buf[vty->length] = '\0';
672
673 vty_write (vty, &vty->buf[vty->cp], size - 1);
674 vty_write (vty, &telnet_space_char, 1);
675
676 for (i = 0; i < size; i++)
677 vty_write (vty, &telnet_backward_char, 1);
678}
679
680/* Delete a character before the point. */
681static void
682vty_delete_backward_char (struct vty *vty)
683{
684 if (vty->cp == 0)
685 return;
686
687 vty_backward_char (vty);
688 vty_delete_char (vty);
689}
690
691/* Kill rest of line from current point. */
692static void
693vty_kill_line (struct vty *vty)
694{
695 int i;
696 int size;
697
698 size = vty->length - vty->cp;
699
700 if (size == 0)
701 return;
702
703 for (i = 0; i < size; i++)
704 vty_write (vty, &telnet_space_char, 1);
705 for (i = 0; i < size; i++)
706 vty_write (vty, &telnet_backward_char, 1);
707
708 memset (&vty->buf[vty->cp], 0, size);
709 vty->length = vty->cp;
710}
711
712/* Kill line from the beginning. */
713static void
714vty_kill_line_from_beginning (struct vty *vty)
715{
716 vty_beginning_of_line (vty);
717 vty_kill_line (vty);
718}
719
720/* Delete a word before the point. */
721static void
722vty_forward_kill_word (struct vty *vty)
723{
724 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
725 vty_delete_char (vty);
726 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
727 vty_delete_char (vty);
728}
729
730/* Delete a word before the point. */
731static void
732vty_backward_kill_word (struct vty *vty)
733{
734 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
735 vty_delete_backward_char (vty);
736 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
737 vty_delete_backward_char (vty);
738}
739
740/* Transpose chars before or at the point. */
741static void
742vty_transpose_chars (struct vty *vty)
743{
744 char c1, c2;
745
746 /* If length is short or point is near by the beginning of line then
747 return. */
748 if (vty->length < 2 || vty->cp < 1)
749 return;
750
751 /* In case of point is located at the end of the line. */
752 if (vty->cp == vty->length)
753 {
754 c1 = vty->buf[vty->cp - 1];
755 c2 = vty->buf[vty->cp - 2];
756
757 vty_backward_char (vty);
758 vty_backward_char (vty);
759 vty_self_insert_overwrite (vty, c1);
760 vty_self_insert_overwrite (vty, c2);
761 }
762 else
763 {
764 c1 = vty->buf[vty->cp];
765 c2 = vty->buf[vty->cp - 1];
766
767 vty_backward_char (vty);
768 vty_self_insert_overwrite (vty, c1);
769 vty_self_insert_overwrite (vty, c2);
770 }
771}
772
773/* Do completion at vty interface. */
774static void
775vty_complete_command (struct vty *vty)
776{
777 int i;
778 int ret;
779 char **matched = NULL;
780 vector vline;
781
782 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
783 return;
784
785 vline = cmd_make_strvec (vty->buf);
786 if (vline == NULL)
787 return;
788
789 /* In case of 'help \t'. */
790 if (isspace ((int) vty->buf[vty->length - 1]))
791 vector_set (vline, '\0');
792
793 matched = cmd_complete_command (vline, vty, &ret);
794
795 cmd_free_strvec (vline);
796
797 vty_out (vty, "%s", VTY_NEWLINE);
798 switch (ret)
799 {
800 case CMD_ERR_AMBIGUOUS:
801 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
802 vty_prompt (vty);
803 vty_redraw_line (vty);
804 break;
805 case CMD_ERR_NO_MATCH:
806 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
807 vty_prompt (vty);
808 vty_redraw_line (vty);
809 break;
810 case CMD_COMPLETE_FULL_MATCH:
811 vty_prompt (vty);
812 vty_redraw_line (vty);
813 vty_backward_pure_word (vty);
814 vty_insert_word_overwrite (vty, matched[0]);
815 vty_self_insert (vty, ' ');
816 XFREE (MTYPE_TMP, matched[0]);
817 break;
818 case CMD_COMPLETE_MATCH:
819 vty_prompt (vty);
820 vty_redraw_line (vty);
821 vty_backward_pure_word (vty);
822 vty_insert_word_overwrite (vty, matched[0]);
823 XFREE (MTYPE_TMP, matched[0]);
824 vector_only_index_free (matched);
825 return;
826 break;
827 case CMD_COMPLETE_LIST_MATCH:
828 for (i = 0; matched[i] != NULL; i++)
829 {
830 if (i != 0 && ((i % 6) == 0))
831 vty_out (vty, "%s", VTY_NEWLINE);
832 vty_out (vty, "%-10s ", matched[i]);
833 XFREE (MTYPE_TMP, matched[i]);
834 }
835 vty_out (vty, "%s", VTY_NEWLINE);
836
837 vty_prompt (vty);
838 vty_redraw_line (vty);
839 break;
840 case CMD_ERR_NOTHING_TODO:
841 vty_prompt (vty);
842 vty_redraw_line (vty);
843 break;
844 default:
845 break;
846 }
847 if (matched)
848 vector_only_index_free (matched);
849}
850
851void
852vty_describe_fold (struct vty *vty, int cmd_width,
hasso8c328f12004-10-05 21:01:23 +0000853 unsigned int desc_width, struct desc *desc)
paul718e3742002-12-13 20:15:29 +0000854{
hasso8c328f12004-10-05 21:01:23 +0000855 char *buf;
856 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000857 int pos;
858
859 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
860
861 if (desc_width <= 0)
862 {
863 vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
864 return;
865 }
866
867 buf = XCALLOC (MTYPE_TMP, strlen (desc->str) + 1);
868
869 for (p = desc->str; strlen (p) > desc_width; p += pos + 1)
870 {
871 for (pos = desc_width; pos > 0; pos--)
872 if (*(p + pos) == ' ')
873 break;
874
875 if (pos == 0)
876 break;
877
878 strncpy (buf, p, pos);
879 buf[pos] = '\0';
880 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
881
882 cmd = "";
883 }
884
885 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
886
887 XFREE (MTYPE_TMP, buf);
888}
889
890/* Describe matched command function. */
891static void
892vty_describe_command (struct vty *vty)
893{
894 int ret;
895 vector vline;
896 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000897 unsigned int i, width, desc_width;
paul718e3742002-12-13 20:15:29 +0000898 struct desc *desc, *desc_cr = NULL;
899
900 vline = cmd_make_strvec (vty->buf);
901
902 /* In case of '> ?'. */
903 if (vline == NULL)
904 {
905 vline = vector_init (1);
906 vector_set (vline, '\0');
907 }
908 else
909 if (isspace ((int) vty->buf[vty->length - 1]))
910 vector_set (vline, '\0');
911
912 describe = cmd_describe_command (vline, vty, &ret);
913
914 vty_out (vty, "%s", VTY_NEWLINE);
915
916 /* Ambiguous error. */
917 switch (ret)
918 {
919 case CMD_ERR_AMBIGUOUS:
920 cmd_free_strvec (vline);
921 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
922 vty_prompt (vty);
923 vty_redraw_line (vty);
924 return;
925 break;
926 case CMD_ERR_NO_MATCH:
927 cmd_free_strvec (vline);
928 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
929 vty_prompt (vty);
930 vty_redraw_line (vty);
931 return;
932 break;
933 }
934
935 /* Get width of command string. */
936 width = 0;
937 for (i = 0; i < vector_max (describe); i++)
938 if ((desc = vector_slot (describe, i)) != NULL)
939 {
hasso8c328f12004-10-05 21:01:23 +0000940 unsigned int len;
paul718e3742002-12-13 20:15:29 +0000941
942 if (desc->cmd[0] == '\0')
943 continue;
944
945 len = strlen (desc->cmd);
946 if (desc->cmd[0] == '.')
947 len--;
948
949 if (width < len)
950 width = len;
951 }
952
953 /* Get width of description string. */
954 desc_width = vty->width - (width + 6);
955
956 /* Print out description. */
957 for (i = 0; i < vector_max (describe); i++)
958 if ((desc = vector_slot (describe, i)) != NULL)
959 {
960 if (desc->cmd[0] == '\0')
961 continue;
962
963 if (strcmp (desc->cmd, "<cr>") == 0)
964 {
965 desc_cr = desc;
966 continue;
967 }
968
969 if (!desc->str)
970 vty_out (vty, " %-s%s",
971 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
972 VTY_NEWLINE);
973 else if (desc_width >= strlen (desc->str))
974 vty_out (vty, " %-*s %s%s", width,
975 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
976 desc->str, VTY_NEWLINE);
977 else
978 vty_describe_fold (vty, width, desc_width, desc);
979
980#if 0
981 vty_out (vty, " %-*s %s%s", width
982 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
983 desc->str ? desc->str : "", VTY_NEWLINE);
984#endif /* 0 */
985 }
986
987 if ((desc = desc_cr))
988 {
989 if (!desc->str)
990 vty_out (vty, " %-s%s",
991 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
992 VTY_NEWLINE);
993 else if (desc_width >= strlen (desc->str))
994 vty_out (vty, " %-*s %s%s", width,
995 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
996 desc->str, VTY_NEWLINE);
997 else
998 vty_describe_fold (vty, width, desc_width, desc);
999 }
1000
1001 cmd_free_strvec (vline);
1002 vector_free (describe);
1003
1004 vty_prompt (vty);
1005 vty_redraw_line (vty);
1006}
1007
1008void
1009vty_clear_buf (struct vty *vty)
1010{
1011 memset (vty->buf, 0, vty->max);
1012}
1013
1014/* ^C stop current input and do not add command line to the history. */
1015static void
1016vty_stop_input (struct vty *vty)
1017{
1018 vty->cp = vty->length = 0;
1019 vty_clear_buf (vty);
1020 vty_out (vty, "%s", VTY_NEWLINE);
1021
1022 switch (vty->node)
1023 {
1024 case VIEW_NODE:
1025 case ENABLE_NODE:
1026 /* Nothing to do. */
1027 break;
1028 case CONFIG_NODE:
1029 case INTERFACE_NODE:
1030 case ZEBRA_NODE:
1031 case RIP_NODE:
1032 case RIPNG_NODE:
1033 case BGP_NODE:
1034 case RMAP_NODE:
1035 case OSPF_NODE:
1036 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001037 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001038 case KEYCHAIN_NODE:
1039 case KEYCHAIN_KEY_NODE:
1040 case MASC_NODE:
1041 case VTY_NODE:
1042 vty_config_unlock (vty);
1043 vty->node = ENABLE_NODE;
1044 break;
1045 default:
1046 /* Unknown node, we have to ignore it. */
1047 break;
1048 }
1049 vty_prompt (vty);
1050
1051 /* Set history pointer to the latest one. */
1052 vty->hp = vty->hindex;
1053}
1054
1055/* Add current command line to the history buffer. */
1056static void
1057vty_hist_add (struct vty *vty)
1058{
1059 int index;
1060
1061 if (vty->length == 0)
1062 return;
1063
1064 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1065
1066 /* Ignore the same string as previous one. */
1067 if (vty->hist[index])
1068 if (strcmp (vty->buf, vty->hist[index]) == 0)
1069 {
1070 vty->hp = vty->hindex;
1071 return;
1072 }
1073
1074 /* Insert history entry. */
1075 if (vty->hist[vty->hindex])
1076 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1077 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1078
1079 /* History index rotation. */
1080 vty->hindex++;
1081 if (vty->hindex == VTY_MAXHIST)
1082 vty->hindex = 0;
1083
1084 vty->hp = vty->hindex;
1085}
1086
1087/* #define TELNET_OPTION_DEBUG */
1088
1089/* Get telnet window size. */
1090static int
1091vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1092{
1093#ifdef TELNET_OPTION_DEBUG
1094 int i;
1095
1096 for (i = 0; i < nbytes; i++)
1097 {
1098 switch (buf[i])
1099 {
1100 case IAC:
1101 vty_out (vty, "IAC ");
1102 break;
1103 case WILL:
1104 vty_out (vty, "WILL ");
1105 break;
1106 case WONT:
1107 vty_out (vty, "WONT ");
1108 break;
1109 case DO:
1110 vty_out (vty, "DO ");
1111 break;
1112 case DONT:
1113 vty_out (vty, "DONT ");
1114 break;
1115 case SB:
1116 vty_out (vty, "SB ");
1117 break;
1118 case SE:
1119 vty_out (vty, "SE ");
1120 break;
1121 case TELOPT_ECHO:
1122 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1123 break;
1124 case TELOPT_SGA:
1125 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1126 break;
1127 case TELOPT_NAWS:
1128 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1129 break;
1130 default:
1131 vty_out (vty, "%x ", buf[i]);
1132 break;
1133 }
1134 }
1135 vty_out (vty, "%s", VTY_NEWLINE);
1136
1137#endif /* TELNET_OPTION_DEBUG */
1138
1139 switch (buf[0])
1140 {
1141 case SB:
1142 buffer_reset(vty->sb_buffer);
1143 vty->iac_sb_in_progress = 1;
1144 return 0;
1145 break;
1146 case SE:
1147 {
paul5b8c1b02003-10-15 23:08:55 +00001148 char *buffer;
1149 int length;
paul718e3742002-12-13 20:15:29 +00001150
1151 if (!vty->iac_sb_in_progress)
1152 return 0;
1153
paul5b8c1b02003-10-15 23:08:55 +00001154 buffer = (char *)vty->sb_buffer->head->data;
1155 length = vty->sb_buffer->length;
1156
1157 if (buffer == NULL)
1158 return 0;
1159
paul718e3742002-12-13 20:15:29 +00001160 if (buffer[0] == '\0')
1161 {
1162 vty->iac_sb_in_progress = 0;
1163 return 0;
1164 }
1165 switch (buffer[0])
1166 {
1167 case TELOPT_NAWS:
1168 if (length < 5)
1169 break;
1170 vty->width = buffer[2];
1171 vty->height = vty->lines >= 0 ? vty->lines : buffer[4];
1172 break;
1173 }
1174 vty->iac_sb_in_progress = 0;
1175 return 0;
1176 break;
1177 }
1178 default:
1179 break;
1180 }
1181 return 1;
1182}
1183
1184/* Execute current command line. */
1185static int
1186vty_execute (struct vty *vty)
1187{
1188 int ret;
1189
1190 ret = CMD_SUCCESS;
1191
1192 switch (vty->node)
1193 {
1194 case AUTH_NODE:
1195 case AUTH_ENABLE_NODE:
1196 vty_auth (vty, vty->buf);
1197 break;
1198 default:
1199 ret = vty_command (vty, vty->buf);
1200 if (vty->type == VTY_TERM)
1201 vty_hist_add (vty);
1202 break;
1203 }
1204
1205 /* Clear command line buffer. */
1206 vty->cp = vty->length = 0;
1207 vty_clear_buf (vty);
1208
1209 if (vty->status != VTY_CLOSE
1210 && vty->status != VTY_START
1211 && vty->status != VTY_CONTINUE)
1212 vty_prompt (vty);
1213
1214 return ret;
1215}
1216
1217#define CONTROL(X) ((X) - '@')
1218#define VTY_NORMAL 0
1219#define VTY_PRE_ESCAPE 1
1220#define VTY_ESCAPE 2
1221
1222/* Escape character command map. */
1223static void
1224vty_escape_map (unsigned char c, struct vty *vty)
1225{
1226 switch (c)
1227 {
1228 case ('A'):
1229 vty_previous_line (vty);
1230 break;
1231 case ('B'):
1232 vty_next_line (vty);
1233 break;
1234 case ('C'):
1235 vty_forward_char (vty);
1236 break;
1237 case ('D'):
1238 vty_backward_char (vty);
1239 break;
1240 default:
1241 break;
1242 }
1243
1244 /* Go back to normal mode. */
1245 vty->escape = VTY_NORMAL;
1246}
1247
1248/* Quit print out to the buffer. */
1249static void
1250vty_buffer_reset (struct vty *vty)
1251{
1252 buffer_reset (vty->obuf);
1253 vty_prompt (vty);
1254 vty_redraw_line (vty);
1255}
1256
1257/* Read data via vty socket. */
1258static int
1259vty_read (struct thread *thread)
1260{
1261 int i;
paul718e3742002-12-13 20:15:29 +00001262 int nbytes;
1263 unsigned char buf[VTY_READ_BUFSIZ];
1264
1265 int vty_sock = THREAD_FD (thread);
1266 struct vty *vty = THREAD_ARG (thread);
1267 vty->t_read = NULL;
1268
1269 /* Read raw data from socket */
1270 nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ);
1271 if (nbytes <= 0)
1272 vty->status = VTY_CLOSE;
1273
1274 for (i = 0; i < nbytes; i++)
1275 {
1276 if (buf[i] == IAC)
1277 {
1278 if (!vty->iac)
1279 {
1280 vty->iac = 1;
1281 continue;
1282 }
1283 else
1284 {
1285 vty->iac = 0;
1286 }
1287 }
1288
1289 if (vty->iac_sb_in_progress && !vty->iac)
1290 {
1291 buffer_putc(vty->sb_buffer, buf[i]);
1292 continue;
1293 }
1294
1295 if (vty->iac)
1296 {
1297 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001298 int ret = 0;
paule9372532003-10-26 21:36:07 +00001299 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001300 vty->iac = 0;
1301 i += ret;
1302 continue;
1303 }
paul5b8c1b02003-10-15 23:08:55 +00001304
paul718e3742002-12-13 20:15:29 +00001305
1306 if (vty->status == VTY_MORE)
1307 {
1308 switch (buf[i])
1309 {
1310 case CONTROL('C'):
1311 case 'q':
1312 case 'Q':
1313 if (vty->output_func)
1314 (*vty->output_func) (vty, 1);
1315 vty_buffer_reset (vty);
1316 break;
1317#if 0 /* More line does not work for "show ip bgp". */
1318 case '\n':
1319 case '\r':
1320 vty->status = VTY_MORELINE;
1321 break;
1322#endif
1323 default:
1324 if (vty->output_func)
1325 (*vty->output_func) (vty, 0);
1326 break;
1327 }
1328 continue;
1329 }
1330
1331 /* Escape character. */
1332 if (vty->escape == VTY_ESCAPE)
1333 {
1334 vty_escape_map (buf[i], vty);
1335 continue;
1336 }
1337
1338 /* Pre-escape status. */
1339 if (vty->escape == VTY_PRE_ESCAPE)
1340 {
1341 switch (buf[i])
1342 {
1343 case '[':
1344 vty->escape = VTY_ESCAPE;
1345 break;
1346 case 'b':
1347 vty_backward_word (vty);
1348 vty->escape = VTY_NORMAL;
1349 break;
1350 case 'f':
1351 vty_forward_word (vty);
1352 vty->escape = VTY_NORMAL;
1353 break;
1354 case 'd':
1355 vty_forward_kill_word (vty);
1356 vty->escape = VTY_NORMAL;
1357 break;
1358 case CONTROL('H'):
1359 case 0x7f:
1360 vty_backward_kill_word (vty);
1361 vty->escape = VTY_NORMAL;
1362 break;
1363 default:
1364 vty->escape = VTY_NORMAL;
1365 break;
1366 }
1367 continue;
1368 }
1369
1370 switch (buf[i])
1371 {
1372 case CONTROL('A'):
1373 vty_beginning_of_line (vty);
1374 break;
1375 case CONTROL('B'):
1376 vty_backward_char (vty);
1377 break;
1378 case CONTROL('C'):
1379 vty_stop_input (vty);
1380 break;
1381 case CONTROL('D'):
1382 vty_delete_char (vty);
1383 break;
1384 case CONTROL('E'):
1385 vty_end_of_line (vty);
1386 break;
1387 case CONTROL('F'):
1388 vty_forward_char (vty);
1389 break;
1390 case CONTROL('H'):
1391 case 0x7f:
1392 vty_delete_backward_char (vty);
1393 break;
1394 case CONTROL('K'):
1395 vty_kill_line (vty);
1396 break;
1397 case CONTROL('N'):
1398 vty_next_line (vty);
1399 break;
1400 case CONTROL('P'):
1401 vty_previous_line (vty);
1402 break;
1403 case CONTROL('T'):
1404 vty_transpose_chars (vty);
1405 break;
1406 case CONTROL('U'):
1407 vty_kill_line_from_beginning (vty);
1408 break;
1409 case CONTROL('W'):
1410 vty_backward_kill_word (vty);
1411 break;
1412 case CONTROL('Z'):
1413 vty_end_config (vty);
1414 break;
1415 case '\n':
1416 case '\r':
1417 vty_out (vty, "%s", VTY_NEWLINE);
1418 vty_execute (vty);
1419 break;
1420 case '\t':
1421 vty_complete_command (vty);
1422 break;
1423 case '?':
1424 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1425 vty_self_insert (vty, buf[i]);
1426 else
1427 vty_describe_command (vty);
1428 break;
1429 case '\033':
1430 if (i + 1 < nbytes && buf[i + 1] == '[')
1431 {
1432 vty->escape = VTY_ESCAPE;
1433 i++;
1434 }
1435 else
1436 vty->escape = VTY_PRE_ESCAPE;
1437 break;
1438 default:
1439 if (buf[i] > 31 && buf[i] < 127)
1440 vty_self_insert (vty, buf[i]);
1441 break;
1442 }
1443 }
1444
1445 /* Check status. */
1446 if (vty->status == VTY_CLOSE)
1447 vty_close (vty);
1448 else
1449 {
1450 vty_event (VTY_WRITE, vty_sock, vty);
1451 vty_event (VTY_READ, vty_sock, vty);
1452 }
1453 return 0;
1454}
1455
1456/* Flush buffer to the vty. */
1457static int
1458vty_flush (struct thread *thread)
1459{
1460 int erase;
1461 int dont_more;
1462 int vty_sock = THREAD_FD (thread);
1463 struct vty *vty = THREAD_ARG (thread);
1464 vty->t_write = NULL;
1465
1466 /* Tempolary disable read thread. */
1467 if (vty->lines == 0)
1468 if (vty->t_read)
1469 {
1470 thread_cancel (vty->t_read);
1471 vty->t_read = NULL;
1472 }
1473
1474 /* Function execution continue. */
1475 if (vty->status == VTY_START || vty->status == VTY_CONTINUE)
1476 {
1477 if (vty->status == VTY_CONTINUE)
1478 erase = 1;
1479 else
1480 erase = 0;
1481
1482 if (vty->output_func == NULL)
1483 dont_more = 1;
1484 else
1485 dont_more = 0;
1486
1487 if (vty->lines == 0)
1488 {
1489 erase = 0;
1490 dont_more = 1;
1491 }
1492
1493 buffer_flush_vty_all (vty->obuf, vty->fd, erase, dont_more);
1494
1495 if (vty->status == VTY_CLOSE)
1496 {
1497 vty_close (vty);
1498 return 0;
1499 }
1500
1501 if (vty->output_func == NULL)
1502 {
1503 vty->status = VTY_NORMAL;
1504 vty_prompt (vty);
1505 vty_event (VTY_WRITE, vty_sock, vty);
1506 }
1507 else
1508 vty->status = VTY_MORE;
1509
1510 if (vty->lines == 0)
1511 {
1512 if (vty->output_func == NULL)
1513 vty_event (VTY_READ, vty_sock, vty);
1514 else
1515 {
1516 if (vty->output_func)
1517 (*vty->output_func) (vty, 0);
1518 vty_event (VTY_WRITE, vty_sock, vty);
1519 }
1520 }
1521 }
1522 else
1523 {
1524 if (vty->status == VTY_MORE || vty->status == VTY_MORELINE)
1525 erase = 1;
1526 else
1527 erase = 0;
1528
1529 if (vty->lines == 0)
1530 buffer_flush_window (vty->obuf, vty->fd, vty->width, 25, 0, 1);
1531 else if (vty->status == VTY_MORELINE)
1532 buffer_flush_window (vty->obuf, vty->fd, vty->width, 1, erase, 0);
1533 else
1534 buffer_flush_window (vty->obuf, vty->fd, vty->width,
1535 vty->lines >= 0 ? vty->lines : vty->height,
1536 erase, 0);
1537
1538 if (buffer_empty (vty->obuf))
1539 {
1540 if (vty->status == VTY_CLOSE)
1541 vty_close (vty);
1542 else
1543 {
1544 vty->status = VTY_NORMAL;
1545
1546 if (vty->lines == 0)
1547 vty_event (VTY_READ, vty_sock, vty);
1548 }
1549 }
1550 else
1551 {
1552 vty->status = VTY_MORE;
1553
1554 if (vty->lines == 0)
1555 vty_event (VTY_WRITE, vty_sock, vty);
1556 }
1557 }
1558
1559 return 0;
1560}
1561
1562/* Create new vty structure. */
1563struct vty *
1564vty_create (int vty_sock, union sockunion *su)
1565{
1566 struct vty *vty;
1567
1568 /* Allocate new vty structure and set up default values. */
1569 vty = vty_new ();
1570 vty->fd = vty_sock;
1571 vty->type = VTY_TERM;
1572 vty->address = sockunion_su2str (su);
1573 if (no_password_check)
1574 {
1575 if (host.advanced)
1576 vty->node = ENABLE_NODE;
1577 else
1578 vty->node = VIEW_NODE;
1579 }
1580 else
1581 vty->node = AUTH_NODE;
1582 vty->fail = 0;
1583 vty->cp = 0;
1584 vty_clear_buf (vty);
1585 vty->length = 0;
1586 memset (vty->hist, 0, sizeof (vty->hist));
1587 vty->hp = 0;
1588 vty->hindex = 0;
1589 vector_set_index (vtyvec, vty_sock, vty);
1590 vty->status = VTY_NORMAL;
1591 vty->v_timeout = vty_timeout_val;
1592 if (host.lines >= 0)
1593 vty->lines = host.lines;
1594 else
1595 vty->lines = -1;
1596 vty->iac = 0;
1597 vty->iac_sb_in_progress = 0;
1598 vty->sb_buffer = buffer_new (1024);
1599
1600 if (! no_password_check)
1601 {
1602 /* Vty is not available if password isn't set. */
1603 if (host.password == NULL && host.password_encrypt == NULL)
1604 {
1605 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1606 vty->status = VTY_CLOSE;
1607 vty_close (vty);
1608 return NULL;
1609 }
1610 }
1611
1612 /* Say hello to the world. */
1613 vty_hello (vty);
1614 if (! no_password_check)
1615 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1616
1617 /* Setting up terminal. */
1618 vty_will_echo (vty);
1619 vty_will_suppress_go_ahead (vty);
1620
1621 vty_dont_linemode (vty);
1622 vty_do_window_size (vty);
1623 /* vty_dont_lflow_ahead (vty); */
1624
1625 vty_prompt (vty);
1626
1627 /* Add read/write thread. */
1628 vty_event (VTY_WRITE, vty_sock, vty);
1629 vty_event (VTY_READ, vty_sock, vty);
1630
1631 return vty;
1632}
1633
1634/* Accept connection from the network. */
1635static int
1636vty_accept (struct thread *thread)
1637{
1638 int vty_sock;
1639 struct vty *vty;
1640 union sockunion su;
1641 int ret;
1642 unsigned int on;
1643 int accept_sock;
1644 struct prefix *p = NULL;
1645 struct access_list *acl = NULL;
1646
1647 accept_sock = THREAD_FD (thread);
1648
1649 /* We continue hearing vty socket. */
1650 vty_event (VTY_SERV, accept_sock, NULL);
1651
1652 memset (&su, 0, sizeof (union sockunion));
1653
1654 /* We can handle IPv4 or IPv6 socket. */
1655 vty_sock = sockunion_accept (accept_sock, &su);
1656 if (vty_sock < 0)
1657 {
1658 zlog_warn ("can't accept vty socket : %s", strerror (errno));
1659 return -1;
1660 }
1661
1662 p = sockunion2hostprefix (&su);
1663
1664 /* VTY's accesslist apply. */
1665 if (p->family == AF_INET && vty_accesslist_name)
1666 {
1667 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1668 (access_list_apply (acl, p) == FILTER_DENY))
1669 {
1670 char *buf;
1671 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1672 (buf = sockunion_su2str (&su)));
1673 free (buf);
1674 close (vty_sock);
1675
1676 /* continue accepting connections */
1677 vty_event (VTY_SERV, accept_sock, NULL);
1678
1679 prefix_free (p);
1680
1681 return 0;
1682 }
1683 }
1684
1685#ifdef HAVE_IPV6
1686 /* VTY's ipv6 accesslist apply. */
1687 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1688 {
1689 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1690 (access_list_apply (acl, p) == FILTER_DENY))
1691 {
1692 char *buf;
1693 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1694 (buf = sockunion_su2str (&su)));
1695 free (buf);
1696 close (vty_sock);
1697
1698 /* continue accepting connections */
1699 vty_event (VTY_SERV, accept_sock, NULL);
1700
1701 prefix_free (p);
1702
1703 return 0;
1704 }
1705 }
1706#endif /* HAVE_IPV6 */
1707
1708 prefix_free (p);
1709
1710 on = 1;
1711 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1712 (char *) &on, sizeof (on));
1713 if (ret < 0)
1714 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
1715 strerror (errno));
1716
1717 vty = vty_create (vty_sock, &su);
1718
1719 return 0;
1720}
1721
1722#if defined(HAVE_IPV6) && !defined(NRL)
1723void
1724vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1725{
1726 int ret;
1727 struct addrinfo req;
1728 struct addrinfo *ainfo;
1729 struct addrinfo *ainfo_save;
1730 int sock;
1731 char port_str[BUFSIZ];
1732
1733 memset (&req, 0, sizeof (struct addrinfo));
1734 req.ai_flags = AI_PASSIVE;
1735 req.ai_family = AF_UNSPEC;
1736 req.ai_socktype = SOCK_STREAM;
1737 sprintf (port_str, "%d", port);
1738 port_str[sizeof (port_str) - 1] = '\0';
1739
1740 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1741
1742 if (ret != 0)
1743 {
1744 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1745 exit (1);
1746 }
1747
1748 ainfo_save = ainfo;
1749
1750 do
1751 {
1752 if (ainfo->ai_family != AF_INET
1753#ifdef HAVE_IPV6
1754 && ainfo->ai_family != AF_INET6
1755#endif /* HAVE_IPV6 */
1756 )
1757 continue;
1758
1759 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1760 if (sock < 0)
1761 continue;
1762
1763 sockopt_reuseaddr (sock);
1764 sockopt_reuseport (sock);
1765
1766 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1767 if (ret < 0)
1768 {
1769 close (sock); /* Avoid sd leak. */
1770 continue;
1771 }
1772
1773 ret = listen (sock, 3);
1774 if (ret < 0)
1775 {
1776 close (sock); /* Avoid sd leak. */
1777 continue;
1778 }
1779
1780 vty_event (VTY_SERV, sock, NULL);
1781 }
1782 while ((ainfo = ainfo->ai_next) != NULL);
1783
1784 freeaddrinfo (ainfo_save);
1785}
1786#endif /* HAVE_IPV6 && ! NRL */
1787
1788/* Make vty server socket. */
1789void
paul29db05b2003-05-08 20:10:22 +00001790vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001791{
1792 int ret;
1793 union sockunion su;
1794 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001795 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001796
1797 memset (&su, 0, sizeof (union sockunion));
1798 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001799 if(addr)
1800 switch(family)
1801 {
1802 case AF_INET:
1803 naddr=&su.sin.sin_addr;
1804#ifdef HAVE_IPV6
1805 case AF_INET6:
1806 naddr=&su.sin6.sin6_addr;
1807#endif
1808 }
1809
1810 if(naddr)
1811 switch(inet_pton(family,addr,naddr))
1812 {
1813 case -1:
1814 zlog_err("bad address %s",addr);
1815 naddr=NULL;
1816 break;
1817 case 0:
1818 zlog_err("error translating address %s: %s",addr,strerror(errno));
1819 naddr=NULL;
1820 }
paul718e3742002-12-13 20:15:29 +00001821
1822 /* Make new socket. */
1823 accept_sock = sockunion_stream_socket (&su);
1824 if (accept_sock < 0)
1825 return;
1826
1827 /* This is server, so reuse address. */
1828 sockopt_reuseaddr (accept_sock);
1829 sockopt_reuseport (accept_sock);
1830
1831 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001832 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001833 if (ret < 0)
1834 {
paul29db05b2003-05-08 20:10:22 +00001835 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001836 close (accept_sock); /* Avoid sd leak. */
1837 return;
1838 }
1839
1840 /* Listen socket under queue 3. */
1841 ret = listen (accept_sock, 3);
1842 if (ret < 0)
1843 {
1844 zlog (NULL, LOG_WARNING, "can't listen socket");
1845 close (accept_sock); /* Avoid sd leak. */
1846 return;
1847 }
1848
1849 /* Add vty server event. */
1850 vty_event (VTY_SERV, accept_sock, NULL);
1851}
1852
1853#ifdef VTYSH
1854/* For sockaddr_un. */
1855#include <sys/un.h>
1856
1857/* VTY shell UNIX domain socket. */
1858void
hasso6ad96ea2004-10-07 19:33:46 +00001859vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001860{
1861 int ret;
paul75e15fe2004-10-31 02:13:09 +00001862 int sock, len;
paul718e3742002-12-13 20:15:29 +00001863 struct sockaddr_un serv;
1864 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001865 struct zprivs_ids_t ids;
1866
paul718e3742002-12-13 20:15:29 +00001867 /* First of all, unlink existing socket */
1868 unlink (path);
1869
1870 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001871 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001872
1873 /* Make UNIX domain socket. */
1874 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1875 if (sock < 0)
1876 {
1877 perror ("sock");
1878 return;
1879 }
1880
1881 /* Make server socket. */
1882 memset (&serv, 0, sizeof (struct sockaddr_un));
1883 serv.sun_family = AF_UNIX;
1884 strncpy (serv.sun_path, path, strlen (path));
1885#ifdef HAVE_SUN_LEN
1886 len = serv.sun_len = SUN_LEN(&serv);
1887#else
1888 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1889#endif /* HAVE_SUN_LEN */
1890
1891 ret = bind (sock, (struct sockaddr *) &serv, len);
1892 if (ret < 0)
1893 {
1894 perror ("bind");
1895 close (sock); /* Avoid sd leak. */
1896 return;
1897 }
1898
1899 ret = listen (sock, 5);
1900 if (ret < 0)
1901 {
1902 perror ("listen");
1903 close (sock); /* Avoid sd leak. */
1904 return;
1905 }
1906
1907 umask (old_mask);
1908
pauledd7c242003-06-04 13:59:38 +00001909 zprivs_get_ids(&ids);
1910
1911 if (ids.gid_vty > 0)
1912 {
1913 /* set group of socket */
1914 if ( chown (path, -1, ids.gid_vty) )
1915 {
1916 zlog_err ("vty_serv_un: could chown socket, %s",
1917 strerror (errno) );
1918 }
1919 }
1920
paul718e3742002-12-13 20:15:29 +00001921 vty_event (VTYSH_SERV, sock, NULL);
1922}
1923
1924/* #define VTYSH_DEBUG 1 */
1925
1926static int
1927vtysh_accept (struct thread *thread)
1928{
1929 int accept_sock;
1930 int sock;
1931 int client_len;
paul75e15fe2004-10-31 02:13:09 +00001932 int flags;
paul718e3742002-12-13 20:15:29 +00001933 struct sockaddr_un client;
1934 struct vty *vty;
1935
1936 accept_sock = THREAD_FD (thread);
1937
1938 vty_event (VTYSH_SERV, accept_sock, NULL);
1939
1940 memset (&client, 0, sizeof (struct sockaddr_un));
1941 client_len = sizeof (struct sockaddr_un);
1942
hassoe473b032004-09-26 16:08:11 +00001943 sock = accept (accept_sock, (struct sockaddr *) &client,
1944 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00001945
1946 if (sock < 0)
1947 {
1948 zlog_warn ("can't accept vty socket : %s", strerror (errno));
1949 return -1;
1950 }
1951
pauldccfb192004-10-29 08:29:36 +00001952 /* set to non-blocking*/
1953 if ( ((flags = fcntl (sock, F_GETFL)) == -1)
1954 || (fcntl (sock, F_SETFL, flags|O_NONBLOCK) == -1) )
paul75e15fe2004-10-31 02:13:09 +00001955 {
1956 zlog_warn ("vtysh_accept: could not set vty socket to non-blocking,"
1957 " %s, closing", strerror (errno));
1958 close (sock);
1959 return -1;
1960 }
pauldccfb192004-10-29 08:29:36 +00001961
paul718e3742002-12-13 20:15:29 +00001962#ifdef VTYSH_DEBUG
1963 printf ("VTY shell accept\n");
1964#endif /* VTYSH_DEBUG */
1965
1966 vty = vty_new ();
1967 vty->fd = sock;
1968 vty->type = VTY_SHELL_SERV;
1969 vty->node = VIEW_NODE;
1970
1971 vty_event (VTYSH_READ, sock, vty);
1972
1973 return 0;
1974}
1975
1976static int
1977vtysh_read (struct thread *thread)
1978{
1979 int ret;
1980 int sock;
1981 int nbytes;
1982 struct vty *vty;
1983 unsigned char buf[VTY_READ_BUFSIZ];
1984 u_char header[4] = {0, 0, 0, 0};
1985
1986 sock = THREAD_FD (thread);
1987 vty = THREAD_ARG (thread);
1988 vty->t_read = NULL;
1989
1990 nbytes = read (sock, buf, VTY_READ_BUFSIZ);
1991 if (nbytes <= 0)
1992 {
1993 vty_close (vty);
1994#ifdef VTYSH_DEBUG
1995 printf ("close vtysh\n");
1996#endif /* VTYSH_DEBUG */
1997 return 0;
1998 }
1999
2000#ifdef VTYSH_DEBUG
2001 printf ("line: %s\n", buf);
2002#endif /* VTYSH_DEBUG */
2003
2004 vty_ensure (vty, nbytes);
2005 memcpy (vty->buf, buf, nbytes);
2006
2007 /* Pass this line to parser. */
2008 ret = vty_execute (vty);
2009
2010 vty_clear_buf (vty);
2011
2012 /* Return result. */
2013#ifdef VTYSH_DEBUG
2014 printf ("result: %d\n", ret);
2015 printf ("vtysh node: %d\n", vty->node);
2016#endif /* VTYSH_DEBUG */
2017
2018 header[3] = ret;
ajs49ff6d92004-11-04 19:26:16 +00002019 buffer_write(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002020
ajs49ff6d92004-11-04 19:26:16 +00002021 if (!vty->t_write && buffer_flush_available(vty->obuf, vty->fd))
2022 vty_event (VTYSH_WRITE, vty->fd, vty);
paul718e3742002-12-13 20:15:29 +00002023 vty_event (VTYSH_READ, sock, vty);
2024
2025 return 0;
2026}
ajs49ff6d92004-11-04 19:26:16 +00002027
2028static int
2029vtysh_write (struct thread *thread)
2030{
2031 struct vty *vty = THREAD_ARG (thread);
2032
2033 vty->t_write = NULL;
2034 if (buffer_flush_available(vty->obuf, vty->fd))
2035 vty_event (VTYSH_WRITE, vty->fd, vty);
2036}
2037
paul718e3742002-12-13 20:15:29 +00002038#endif /* VTYSH */
2039
2040/* Determine address family to bind. */
2041void
hasso6ad96ea2004-10-07 19:33:46 +00002042vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002043{
2044 /* If port is set to 0, do not listen on TCP/IP at all! */
2045 if (port)
2046 {
2047
2048#ifdef HAVE_IPV6
2049#ifdef NRL
paul29db05b2003-05-08 20:10:22 +00002050 vty_serv_sock_family (addr, port, AF_INET);
2051 vty_serv_sock_family (addr, port, AF_INET6);
paul718e3742002-12-13 20:15:29 +00002052#else /* ! NRL */
paul29db05b2003-05-08 20:10:22 +00002053 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002054#endif /* NRL*/
2055#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002056 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002057#endif /* HAVE_IPV6 */
2058 }
2059
2060#ifdef VTYSH
2061 vty_serv_un (path);
2062#endif /* VTYSH */
2063}
2064
2065/* Close vty interface. */
2066void
2067vty_close (struct vty *vty)
2068{
2069 int i;
2070
2071 /* Cancel threads.*/
2072 if (vty->t_read)
2073 thread_cancel (vty->t_read);
2074 if (vty->t_write)
2075 thread_cancel (vty->t_write);
2076 if (vty->t_timeout)
2077 thread_cancel (vty->t_timeout);
2078 if (vty->t_output)
2079 thread_cancel (vty->t_output);
2080
2081 /* Flush buffer. */
2082 if (! buffer_empty (vty->obuf))
2083 buffer_flush_all (vty->obuf, vty->fd);
2084
2085 /* Free input buffer. */
2086 buffer_free (vty->obuf);
2087
2088 /* Free SB buffer. */
2089 if (vty->sb_buffer)
2090 buffer_free (vty->sb_buffer);
2091
2092 /* Free command history. */
2093 for (i = 0; i < VTY_MAXHIST; i++)
2094 if (vty->hist[i])
2095 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2096
2097 /* Unset vector. */
2098 vector_unset (vtyvec, vty->fd);
2099
2100 /* Close socket. */
2101 if (vty->fd > 0)
2102 close (vty->fd);
2103
2104 if (vty->address)
2105 XFREE (0, vty->address);
2106 if (vty->buf)
2107 XFREE (MTYPE_VTY, vty->buf);
2108
2109 /* Check configure. */
2110 vty_config_unlock (vty);
2111
2112 /* OK free vty. */
2113 XFREE (MTYPE_VTY, vty);
2114}
2115
2116/* When time out occur output message then close connection. */
2117static int
2118vty_timeout (struct thread *thread)
2119{
2120 struct vty *vty;
2121
2122 vty = THREAD_ARG (thread);
2123 vty->t_timeout = NULL;
2124 vty->v_timeout = 0;
2125
2126 /* Clear buffer*/
2127 buffer_reset (vty->obuf);
2128 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2129
2130 /* Close connection. */
2131 vty->status = VTY_CLOSE;
2132 vty_close (vty);
2133
2134 return 0;
2135}
2136
2137/* Read up configuration file from file_name. */
2138static void
2139vty_read_file (FILE *confp)
2140{
2141 int ret;
2142 struct vty *vty;
2143
2144 vty = vty_new ();
2145 vty->fd = 0; /* stdout */
2146 vty->type = VTY_TERM;
2147 vty->node = CONFIG_NODE;
2148
2149 /* Execute configuration file */
2150 ret = config_from_file (vty, confp);
2151
paul7021c422003-07-15 12:52:22 +00002152 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002153 {
2154 switch (ret)
paul7021c422003-07-15 12:52:22 +00002155 {
2156 case CMD_ERR_AMBIGUOUS:
2157 fprintf (stderr, "Ambiguous command.\n");
2158 break;
2159 case CMD_ERR_NO_MATCH:
2160 fprintf (stderr, "There is no such command.\n");
2161 break;
2162 }
paul718e3742002-12-13 20:15:29 +00002163 fprintf (stderr, "Error occured during reading below line.\n%s\n",
2164 vty->buf);
2165 vty_close (vty);
2166 exit (1);
2167 }
2168
2169 vty_close (vty);
2170}
2171
2172FILE *
2173vty_use_backup_config (char *fullpath)
2174{
2175 char *fullpath_sav, *fullpath_tmp;
2176 FILE *ret = NULL;
2177 struct stat buf;
2178 int tmp, sav;
2179 int c;
2180 char buffer[512];
2181
2182 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2183 strcpy (fullpath_sav, fullpath);
2184 strcat (fullpath_sav, CONF_BACKUP_EXT);
2185 if (stat (fullpath_sav, &buf) == -1)
2186 {
2187 free (fullpath_sav);
2188 return NULL;
2189 }
2190
2191 fullpath_tmp = malloc (strlen (fullpath) + 8);
2192 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2193
2194 /* Open file to configuration write. */
2195 tmp = mkstemp (fullpath_tmp);
2196 if (tmp < 0)
2197 {
2198 free (fullpath_sav);
2199 free (fullpath_tmp);
2200 return NULL;
2201 }
2202
2203 sav = open (fullpath_sav, O_RDONLY);
2204 if (sav < 0)
2205 {
gdt3dbf9962003-12-22 20:18:18 +00002206 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002207 free (fullpath_sav);
2208 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002209 return NULL;
2210 }
2211
2212 while((c = read (sav, buffer, 512)) > 0)
2213 write (tmp, buffer, c);
2214
2215 close (sav);
2216 close (tmp);
2217
gdtaa593d52003-12-22 20:15:53 +00002218 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2219 {
gdt3dbf9962003-12-22 20:18:18 +00002220 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002221 free (fullpath_sav);
2222 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002223 return NULL;
2224 }
2225
paul718e3742002-12-13 20:15:29 +00002226 if (link (fullpath_tmp, fullpath) == 0)
2227 ret = fopen (fullpath, "r");
2228
2229 unlink (fullpath_tmp);
2230
2231 free (fullpath_sav);
2232 free (fullpath_tmp);
2233 return fopen (fullpath, "r");
2234}
2235
2236/* Read up configuration file from file_name. */
2237void
2238vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002239 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002240{
paulccc92352003-10-22 02:49:38 +00002241 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002242 FILE *confp = NULL;
2243 char *fullpath;
2244
2245 /* If -f flag specified. */
2246 if (config_file != NULL)
2247 {
2248 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002249 {
2250 getcwd (cwd, MAXPATHLEN);
2251 fullpath = XMALLOC (MTYPE_TMP,
2252 strlen (cwd) + strlen (config_file) + 2);
2253 sprintf (fullpath, "%s/%s", cwd, config_file);
2254 }
paul718e3742002-12-13 20:15:29 +00002255 else
hasso320ec102004-06-20 19:54:37 +00002256 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002257
2258 confp = fopen (fullpath, "r");
2259
2260 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002261 {
2262 confp = vty_use_backup_config (fullpath);
2263 if (confp)
2264 fprintf (stderr, "WARNING: using backup configuration file!\n");
2265 else
2266 {
2267 fprintf (stderr, "can't open configuration file [%s]\n",
2268 config_file);
2269 exit(1);
2270 }
2271 }
paul718e3742002-12-13 20:15:29 +00002272 }
2273 else
2274 {
paul718e3742002-12-13 20:15:29 +00002275#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002276 int ret;
2277 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002278
hasso320ec102004-06-20 19:54:37 +00002279 /* !!!!PLEASE LEAVE!!!!
2280 * This is NEEDED for use with vtysh -b, or else you can get
2281 * a real configuration food fight with a lot garbage in the
2282 * merged configuration file it creates coming from the per
2283 * daemon configuration files. This also allows the daemons
2284 * to start if there default configuration file is not
2285 * present or ignore them, as needed when using vtysh -b to
2286 * configure the daemons at boot - MAG
2287 */
paul718e3742002-12-13 20:15:29 +00002288
hasso320ec102004-06-20 19:54:37 +00002289 /* Stat for vtysh Zebra.conf, if found startup and wait for
2290 * boot configuration
2291 */
paul718e3742002-12-13 20:15:29 +00002292
hasso320ec102004-06-20 19:54:37 +00002293 if ( strstr(config_default_dir, "vtysh") == NULL)
2294 {
2295 ret = stat (integrate_default, &conf_stat);
2296 if (ret >= 0)
2297 return;
2298 }
paul718e3742002-12-13 20:15:29 +00002299#endif /* VTYSH */
2300
hasso320ec102004-06-20 19:54:37 +00002301 confp = fopen (config_default_dir, "r");
2302 if (confp == NULL)
2303 {
2304 confp = vty_use_backup_config (config_default_dir);
2305 if (confp)
2306 {
2307 fprintf (stderr, "WARNING: using backup configuration file!\n");
2308 fullpath = config_default_dir;
2309 }
2310 else
2311 {
2312 fprintf (stderr, "can't open configuration file [%s]\n",
2313 config_default_dir);
2314 exit (1);
2315 }
2316 }
paul718e3742002-12-13 20:15:29 +00002317 else
hasso320ec102004-06-20 19:54:37 +00002318 fullpath = config_default_dir;
2319 }
2320
paul718e3742002-12-13 20:15:29 +00002321 vty_read_file (confp);
2322
2323 fclose (confp);
2324
2325 host_config_set (fullpath);
2326}
2327
2328/* Small utility function which output log to the VTY. */
2329void
2330vty_log (const char *proto_str, const char *format, va_list va)
2331{
hasso8c328f12004-10-05 21:01:23 +00002332 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002333 struct vty *vty;
2334
2335 for (i = 0; i < vector_max (vtyvec); i++)
2336 if ((vty = vector_slot (vtyvec, i)) != NULL)
2337 if (vty->monitor)
2338 vty_log_out (vty, proto_str, format, va);
2339}
2340
2341int
2342vty_config_lock (struct vty *vty)
2343{
2344 if (vty_config == 0)
2345 {
2346 vty->config = 1;
2347 vty_config = 1;
2348 }
2349 return vty->config;
2350}
2351
2352int
2353vty_config_unlock (struct vty *vty)
2354{
2355 if (vty_config == 1 && vty->config == 1)
2356 {
2357 vty->config = 0;
2358 vty_config = 0;
2359 }
2360 return vty->config;
2361}
2362
2363/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002364static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002365
2366static void
2367vty_event (enum event event, int sock, struct vty *vty)
2368{
2369 struct thread *vty_serv_thread;
2370
2371 switch (event)
2372 {
2373 case VTY_SERV:
2374 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2375 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2376 break;
2377#ifdef VTYSH
2378 case VTYSH_SERV:
2379 thread_add_read (master, vtysh_accept, vty, sock);
2380 break;
2381 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002382 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2383 break;
2384 case VTYSH_WRITE:
2385 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002386 break;
2387#endif /* VTYSH */
2388 case VTY_READ:
2389 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2390
2391 /* Time out treatment. */
2392 if (vty->v_timeout)
2393 {
2394 if (vty->t_timeout)
2395 thread_cancel (vty->t_timeout);
2396 vty->t_timeout =
2397 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2398 }
2399 break;
2400 case VTY_WRITE:
2401 if (! vty->t_write)
2402 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2403 break;
2404 case VTY_TIMEOUT_RESET:
2405 if (vty->t_timeout)
2406 {
2407 thread_cancel (vty->t_timeout);
2408 vty->t_timeout = NULL;
2409 }
2410 if (vty->v_timeout)
2411 {
2412 vty->t_timeout =
2413 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2414 }
2415 break;
2416 }
2417}
2418
2419DEFUN (config_who,
2420 config_who_cmd,
2421 "who",
2422 "Display who is on vty\n")
2423{
hasso8c328f12004-10-05 21:01:23 +00002424 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002425 struct vty *v;
2426
2427 for (i = 0; i < vector_max (vtyvec); i++)
2428 if ((v = vector_slot (vtyvec, i)) != NULL)
2429 vty_out (vty, "%svty[%d] connected from %s.%s",
2430 v->config ? "*" : " ",
2431 i, v->address, VTY_NEWLINE);
2432 return CMD_SUCCESS;
2433}
2434
2435/* Move to vty configuration mode. */
2436DEFUN (line_vty,
2437 line_vty_cmd,
2438 "line vty",
2439 "Configure a terminal line\n"
2440 "Virtual terminal\n")
2441{
2442 vty->node = VTY_NODE;
2443 return CMD_SUCCESS;
2444}
2445
2446/* Set time out value. */
2447int
paul9035efa2004-10-10 11:56:56 +00002448exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002449{
2450 unsigned long timeout = 0;
2451
2452 /* min_str and sec_str are already checked by parser. So it must be
2453 all digit string. */
2454 if (min_str)
2455 {
2456 timeout = strtol (min_str, NULL, 10);
2457 timeout *= 60;
2458 }
2459 if (sec_str)
2460 timeout += strtol (sec_str, NULL, 10);
2461
2462 vty_timeout_val = timeout;
2463 vty->v_timeout = timeout;
2464 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2465
2466
2467 return CMD_SUCCESS;
2468}
2469
2470DEFUN (exec_timeout_min,
2471 exec_timeout_min_cmd,
2472 "exec-timeout <0-35791>",
2473 "Set timeout value\n"
2474 "Timeout value in minutes\n")
2475{
2476 return exec_timeout (vty, argv[0], NULL);
2477}
2478
2479DEFUN (exec_timeout_sec,
2480 exec_timeout_sec_cmd,
2481 "exec-timeout <0-35791> <0-2147483>",
2482 "Set the EXEC timeout\n"
2483 "Timeout in minutes\n"
2484 "Timeout in seconds\n")
2485{
2486 return exec_timeout (vty, argv[0], argv[1]);
2487}
2488
2489DEFUN (no_exec_timeout,
2490 no_exec_timeout_cmd,
2491 "no exec-timeout",
2492 NO_STR
2493 "Set the EXEC timeout\n")
2494{
2495 return exec_timeout (vty, NULL, NULL);
2496}
2497
2498/* Set vty access class. */
2499DEFUN (vty_access_class,
2500 vty_access_class_cmd,
2501 "access-class WORD",
2502 "Filter connections based on an IP access list\n"
2503 "IP access list\n")
2504{
2505 if (vty_accesslist_name)
2506 XFREE(MTYPE_VTY, vty_accesslist_name);
2507
2508 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2509
2510 return CMD_SUCCESS;
2511}
2512
2513/* Clear vty access class. */
2514DEFUN (no_vty_access_class,
2515 no_vty_access_class_cmd,
2516 "no access-class [WORD]",
2517 NO_STR
2518 "Filter connections based on an IP access list\n"
2519 "IP access list\n")
2520{
2521 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2522 {
2523 vty_out (vty, "Access-class is not currently applied to vty%s",
2524 VTY_NEWLINE);
2525 return CMD_WARNING;
2526 }
2527
2528 XFREE(MTYPE_VTY, vty_accesslist_name);
2529
2530 vty_accesslist_name = NULL;
2531
2532 return CMD_SUCCESS;
2533}
2534
2535#ifdef HAVE_IPV6
2536/* Set vty access class. */
2537DEFUN (vty_ipv6_access_class,
2538 vty_ipv6_access_class_cmd,
2539 "ipv6 access-class WORD",
2540 IPV6_STR
2541 "Filter connections based on an IP access list\n"
2542 "IPv6 access list\n")
2543{
2544 if (vty_ipv6_accesslist_name)
2545 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2546
2547 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2548
2549 return CMD_SUCCESS;
2550}
2551
2552/* Clear vty access class. */
2553DEFUN (no_vty_ipv6_access_class,
2554 no_vty_ipv6_access_class_cmd,
2555 "no ipv6 access-class [WORD]",
2556 NO_STR
2557 IPV6_STR
2558 "Filter connections based on an IP access list\n"
2559 "IPv6 access list\n")
2560{
2561 if (! vty_ipv6_accesslist_name ||
2562 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2563 {
2564 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2565 VTY_NEWLINE);
2566 return CMD_WARNING;
2567 }
2568
2569 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2570
2571 vty_ipv6_accesslist_name = NULL;
2572
2573 return CMD_SUCCESS;
2574}
2575#endif /* HAVE_IPV6 */
2576
2577/* vty login. */
2578DEFUN (vty_login,
2579 vty_login_cmd,
2580 "login",
2581 "Enable password checking\n")
2582{
2583 no_password_check = 0;
2584 return CMD_SUCCESS;
2585}
2586
2587DEFUN (no_vty_login,
2588 no_vty_login_cmd,
2589 "no login",
2590 NO_STR
2591 "Enable password checking\n")
2592{
2593 no_password_check = 1;
2594 return CMD_SUCCESS;
2595}
2596
2597DEFUN (service_advanced_vty,
2598 service_advanced_vty_cmd,
2599 "service advanced-vty",
2600 "Set up miscellaneous service\n"
2601 "Enable advanced mode vty interface\n")
2602{
2603 host.advanced = 1;
2604 return CMD_SUCCESS;
2605}
2606
2607DEFUN (no_service_advanced_vty,
2608 no_service_advanced_vty_cmd,
2609 "no service advanced-vty",
2610 NO_STR
2611 "Set up miscellaneous service\n"
2612 "Enable advanced mode vty interface\n")
2613{
2614 host.advanced = 0;
2615 return CMD_SUCCESS;
2616}
2617
2618DEFUN (terminal_monitor,
2619 terminal_monitor_cmd,
2620 "terminal monitor",
2621 "Set terminal line parameters\n"
2622 "Copy debug output to the current terminal line\n")
2623{
2624 vty->monitor = 1;
2625 return CMD_SUCCESS;
2626}
2627
2628DEFUN (terminal_no_monitor,
2629 terminal_no_monitor_cmd,
2630 "terminal no monitor",
2631 "Set terminal line parameters\n"
2632 NO_STR
2633 "Copy debug output to the current terminal line\n")
2634{
2635 vty->monitor = 0;
2636 return CMD_SUCCESS;
2637}
2638
2639DEFUN (show_history,
2640 show_history_cmd,
2641 "show history",
2642 SHOW_STR
2643 "Display the session command history\n")
2644{
2645 int index;
2646
2647 for (index = vty->hindex + 1; index != vty->hindex;)
2648 {
2649 if (index == VTY_MAXHIST)
2650 {
2651 index = 0;
2652 continue;
2653 }
2654
2655 if (vty->hist[index] != NULL)
2656 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2657
2658 index++;
2659 }
2660
2661 return CMD_SUCCESS;
2662}
2663
2664/* Display current configuration. */
2665int
2666vty_config_write (struct vty *vty)
2667{
2668 vty_out (vty, "line vty%s", VTY_NEWLINE);
2669
2670 if (vty_accesslist_name)
2671 vty_out (vty, " access-class %s%s",
2672 vty_accesslist_name, VTY_NEWLINE);
2673
2674 if (vty_ipv6_accesslist_name)
2675 vty_out (vty, " ipv6 access-class %s%s",
2676 vty_ipv6_accesslist_name, VTY_NEWLINE);
2677
2678 /* exec-timeout */
2679 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2680 vty_out (vty, " exec-timeout %ld %ld%s",
2681 vty_timeout_val / 60,
2682 vty_timeout_val % 60, VTY_NEWLINE);
2683
2684 /* login */
2685 if (no_password_check)
2686 vty_out (vty, " no login%s", VTY_NEWLINE);
2687
2688 vty_out (vty, "!%s", VTY_NEWLINE);
2689
2690 return CMD_SUCCESS;
2691}
2692
2693struct cmd_node vty_node =
2694{
2695 VTY_NODE,
2696 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002697 1,
paul718e3742002-12-13 20:15:29 +00002698};
2699
2700/* Reset all VTY status. */
2701void
2702vty_reset ()
2703{
hasso8c328f12004-10-05 21:01:23 +00002704 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002705 struct vty *vty;
2706 struct thread *vty_serv_thread;
2707
2708 for (i = 0; i < vector_max (vtyvec); i++)
2709 if ((vty = vector_slot (vtyvec, i)) != NULL)
2710 {
2711 buffer_reset (vty->obuf);
2712 vty->status = VTY_CLOSE;
2713 vty_close (vty);
2714 }
2715
2716 for (i = 0; i < vector_max (Vvty_serv_thread); i++)
2717 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2718 {
2719 thread_cancel (vty_serv_thread);
2720 vector_slot (Vvty_serv_thread, i) = NULL;
2721 close (i);
2722 }
2723
2724 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2725
2726 if (vty_accesslist_name)
2727 {
2728 XFREE(MTYPE_VTY, vty_accesslist_name);
2729 vty_accesslist_name = NULL;
2730 }
2731
2732 if (vty_ipv6_accesslist_name)
2733 {
2734 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2735 vty_ipv6_accesslist_name = NULL;
2736 }
2737}
2738
2739/* for ospf6d easy temprary reload function */
2740/* vty_reset + close accept socket */
2741void
2742vty_finish ()
2743{
hasso8c328f12004-10-05 21:01:23 +00002744 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002745 struct vty *vty;
2746 struct thread *vty_serv_thread;
2747
2748 for (i = 0; i < vector_max (vtyvec); i++)
2749 if ((vty = vector_slot (vtyvec, i)) != NULL)
2750 {
2751 buffer_reset (vty->obuf);
2752 vty->status = VTY_CLOSE;
2753 vty_close (vty);
2754 }
2755
2756 for (i = 0; i < vector_max (Vvty_serv_thread); i++)
2757 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2758 {
2759 thread_cancel (vty_serv_thread);
2760 vector_slot (Vvty_serv_thread, i) = NULL;
2761 close (i);
2762 }
2763
2764 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2765
2766 if (vty_accesslist_name)
2767 {
2768 XFREE(MTYPE_VTY, vty_accesslist_name);
2769 vty_accesslist_name = NULL;
2770 }
2771
2772 if (vty_ipv6_accesslist_name)
2773 {
2774 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2775 vty_ipv6_accesslist_name = NULL;
2776 }
2777}
2778
2779void
2780vty_save_cwd ()
2781{
paul79ad2792003-10-15 22:09:28 +00002782 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002783 char *c;
paul718e3742002-12-13 20:15:29 +00002784
paulccc92352003-10-22 02:49:38 +00002785 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002786
paulccc92352003-10-22 02:49:38 +00002787 if (!c)
paul79ad2792003-10-15 22:09:28 +00002788 {
2789 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002790 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002791 }
paul718e3742002-12-13 20:15:29 +00002792
2793 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2794 strcpy (vty_cwd, cwd);
2795}
2796
2797char *
2798vty_get_cwd ()
2799{
2800 return vty_cwd;
2801}
2802
2803int
2804vty_shell (struct vty *vty)
2805{
2806 return vty->type == VTY_SHELL ? 1 : 0;
2807}
2808
2809int
2810vty_shell_serv (struct vty *vty)
2811{
2812 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2813}
2814
2815void
2816vty_init_vtysh ()
2817{
2818 vtyvec = vector_init (VECTOR_MIN_SIZE);
2819}
2820
2821/* Install vty's own commands like `who' command. */
2822void
paulb21b19c2003-06-15 01:28:29 +00002823vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00002824{
2825 /* For further configuration read, preserve current directory. */
2826 vty_save_cwd ();
2827
2828 vtyvec = vector_init (VECTOR_MIN_SIZE);
2829
paulb21b19c2003-06-15 01:28:29 +00002830 master = master_thread;
2831
paul718e3742002-12-13 20:15:29 +00002832 /* Initilize server thread vector. */
2833 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
2834
2835 /* Install bgp top node. */
2836 install_node (&vty_node, vty_config_write);
2837
2838 install_element (VIEW_NODE, &config_who_cmd);
2839 install_element (VIEW_NODE, &show_history_cmd);
2840 install_element (ENABLE_NODE, &config_who_cmd);
2841 install_element (CONFIG_NODE, &line_vty_cmd);
2842 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
2843 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
2844 install_element (CONFIG_NODE, &show_history_cmd);
2845 install_element (ENABLE_NODE, &terminal_monitor_cmd);
2846 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
2847 install_element (ENABLE_NODE, &show_history_cmd);
2848
2849 install_default (VTY_NODE);
2850 install_element (VTY_NODE, &exec_timeout_min_cmd);
2851 install_element (VTY_NODE, &exec_timeout_sec_cmd);
2852 install_element (VTY_NODE, &no_exec_timeout_cmd);
2853 install_element (VTY_NODE, &vty_access_class_cmd);
2854 install_element (VTY_NODE, &no_vty_access_class_cmd);
2855 install_element (VTY_NODE, &vty_login_cmd);
2856 install_element (VTY_NODE, &no_vty_login_cmd);
2857#ifdef HAVE_IPV6
2858 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
2859 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
2860#endif /* HAVE_IPV6 */
2861}