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