]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.c
lib: add and use set_cloexec()
[mirror_frr.git] / lib / vty.c
1 /*
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"
26 #include "thread.h"
27 #include "buffer.h"
28 #include <lib/version.h>
29 #include "command.h"
30 #include "sockunion.h"
31 #include "memory.h"
32 #include "str.h"
33 #include "log.h"
34 #include "prefix.h"
35 #include "filter.h"
36 #include "vty.h"
37 #include "privs.h"
38 #include "network.h"
39
40 #include <arpa/telnet.h>
41 #include <termios.h>
42
43 DEFINE_MTYPE_STATIC(LIB, VTY, "VTY")
44 DEFINE_MTYPE_STATIC(LIB, VTY_OUT_BUF, "VTY output buffer")
45 DEFINE_MTYPE_STATIC(LIB, VTY_HIST, "VTY history")
46
47 /* Vty events */
48 enum event
49 {
50 VTY_SERV,
51 VTY_READ,
52 VTY_WRITE,
53 VTY_TIMEOUT_RESET,
54 #ifdef VTYSH
55 VTYSH_SERV,
56 VTYSH_READ,
57 VTYSH_WRITE
58 #endif /* VTYSH */
59 };
60
61 static void vty_event (enum event, int, struct vty *);
62
63 /* Extern host structure from command.c */
64 extern struct host host;
65
66 /* Vector which store each vty structure. */
67 static vector vtyvec;
68
69 /* Vty timeout value. */
70 static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
71
72 /* Vty access-class command */
73 static char *vty_accesslist_name = NULL;
74
75 /* Vty access-calss for IPv6. */
76 static char *vty_ipv6_accesslist_name = NULL;
77
78 /* VTY server thread. */
79 static vector Vvty_serv_thread;
80
81 /* Current directory. */
82 char *vty_cwd = NULL;
83
84 /* Configure lock. */
85 static int vty_config;
86 static int vty_config_is_lockless = 0;
87
88 /* Login password check. */
89 static int no_password_check = 0;
90
91 /* Integrated configuration file path */
92 char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
93
94 static int do_log_commands = 0;
95
96 /* VTY standard output function. */
97 int
98 vty_out (struct vty *vty, const char *format, ...)
99 {
100 va_list args;
101 int len = 0;
102 int size = 1024;
103 char buf[1024];
104 char *p = NULL;
105
106 if (vty_shell (vty))
107 {
108 va_start (args, format);
109 vprintf (format, args);
110 va_end (args);
111 }
112 else
113 {
114 /* Try to write to initial buffer. */
115 va_start (args, format);
116 len = vsnprintf (buf, sizeof(buf), format, args);
117 va_end (args);
118
119 /* Initial buffer is not enough. */
120 if (len < 0 || len >= size)
121 {
122 while (1)
123 {
124 if (len > -1)
125 size = len + 1;
126 else
127 size = size * 2;
128
129 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
130 if (! p)
131 return -1;
132
133 va_start (args, format);
134 len = vsnprintf (p, size, format, args);
135 va_end (args);
136
137 if (len > -1 && len < size)
138 break;
139 }
140 }
141
142 /* When initial buffer is enough to store all output. */
143 if (! p)
144 p = buf;
145
146 /* Pointer p must point out buffer. */
147 buffer_put (vty->obuf, (u_char *) p, len);
148
149 /* If p is not different with buf, it is allocated buffer. */
150 if (p != buf)
151 XFREE (MTYPE_VTY_OUT_BUF, p);
152 }
153
154 return len;
155 }
156
157 static int
158 vty_log_out (struct vty *vty, const char *level, const char *proto_str,
159 const char *format, struct timestamp_control *ctl, va_list va)
160 {
161 int ret;
162 int len;
163 char buf[1024];
164
165 if (!ctl->already_rendered)
166 {
167 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
168 ctl->already_rendered = 1;
169 }
170 if (ctl->len+1 >= sizeof(buf))
171 return -1;
172 memcpy(buf, ctl->buf, len = ctl->len);
173 buf[len++] = ' ';
174 buf[len] = '\0';
175
176 if (level)
177 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
178 else
179 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
180 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
181 return -1;
182
183 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
184 ((size_t)((len += ret)+2) > sizeof(buf)))
185 return -1;
186
187 buf[len++] = '\r';
188 buf[len++] = '\n';
189
190 if (write(vty->wfd, buf, len) < 0)
191 {
192 if (ERRNO_IO_RETRY(errno))
193 /* Kernel buffer is full, probably too much debugging output, so just
194 drop the data and ignore. */
195 return -1;
196 /* Fatal I/O error. */
197 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
198 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
199 __func__, vty->fd, safe_strerror(errno));
200 buffer_reset(vty->obuf);
201 /* cannot call vty_close, because a parent routine may still try
202 to access the vty struct */
203 vty->status = VTY_CLOSE;
204 shutdown(vty->fd, SHUT_RDWR);
205 return -1;
206 }
207 return 0;
208 }
209
210 /* Output current time to the vty. */
211 void
212 vty_time_print (struct vty *vty, int cr)
213 {
214 char buf[QUAGGA_TIMESTAMP_LEN];
215
216 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
217 {
218 zlog (NULL, LOG_INFO, "quagga_timestamp error");
219 return;
220 }
221 if (cr)
222 vty_out (vty, "%s\n", buf);
223 else
224 vty_out (vty, "%s ", buf);
225
226 return;
227 }
228
229 /* Say hello to vty interface. */
230 void
231 vty_hello (struct vty *vty)
232 {
233 if (host.motdfile)
234 {
235 FILE *f;
236 char buf[4096];
237
238 f = fopen (host.motdfile, "r");
239 if (f)
240 {
241 while (fgets (buf, sizeof (buf), f))
242 {
243 char *s;
244 /* work backwards to ignore trailling isspace() */
245 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
246 s--);
247 *s = '\0';
248 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
249 }
250 fclose (f);
251 }
252 else
253 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
254 }
255 else if (host.motd)
256 vty_out (vty, "%s", host.motd);
257 }
258
259 /* Put out prompt and wait input from user. */
260 static void
261 vty_prompt (struct vty *vty)
262 {
263 struct utsname names;
264 const char*hostname;
265
266 if (vty->type == VTY_TERM)
267 {
268 hostname = host.name;
269 if (!hostname)
270 {
271 uname (&names);
272 hostname = names.nodename;
273 }
274 vty_out (vty, cmd_prompt (vty->node), hostname);
275 }
276 }
277
278 /* Send WILL TELOPT_ECHO to remote server. */
279 static void
280 vty_will_echo (struct vty *vty)
281 {
282 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
283 vty_out (vty, "%s", cmd);
284 }
285
286 /* Make suppress Go-Ahead telnet option. */
287 static void
288 vty_will_suppress_go_ahead (struct vty *vty)
289 {
290 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
291 vty_out (vty, "%s", cmd);
292 }
293
294 /* Make don't use linemode over telnet. */
295 static void
296 vty_dont_linemode (struct vty *vty)
297 {
298 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
299 vty_out (vty, "%s", cmd);
300 }
301
302 /* Use window size. */
303 static void
304 vty_do_window_size (struct vty *vty)
305 {
306 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
307 vty_out (vty, "%s", cmd);
308 }
309
310 #if 0 /* Currently not used. */
311 /* Make don't use lflow vty interface. */
312 static void
313 vty_dont_lflow_ahead (struct vty *vty)
314 {
315 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
316 vty_out (vty, "%s", cmd);
317 }
318 #endif /* 0 */
319
320 /* Allocate new vty struct. */
321 struct vty *
322 vty_new ()
323 {
324 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
325
326 new->obuf = buffer_new(0); /* Use default buffer size. */
327 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
328 new->error_buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
329 new->max = VTY_BUFSIZ;
330
331 return new;
332 }
333
334 /* Authentication of vty */
335 static void
336 vty_auth (struct vty *vty, char *buf)
337 {
338 char *passwd = NULL;
339 enum node_type next_node = 0;
340 int fail;
341 char *crypt (const char *, const char *);
342
343 switch (vty->node)
344 {
345 case AUTH_NODE:
346 if (host.encrypt)
347 passwd = host.password_encrypt;
348 else
349 passwd = host.password;
350 if (host.advanced)
351 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
352 else
353 next_node = VIEW_NODE;
354 break;
355 case AUTH_ENABLE_NODE:
356 if (host.encrypt)
357 passwd = host.enable_encrypt;
358 else
359 passwd = host.enable;
360 next_node = ENABLE_NODE;
361 break;
362 }
363
364 if (passwd)
365 {
366 if (host.encrypt)
367 fail = strcmp (crypt(buf, passwd), passwd);
368 else
369 fail = strcmp (buf, passwd);
370 }
371 else
372 fail = 1;
373
374 if (! fail)
375 {
376 vty->fail = 0;
377 vty->node = next_node; /* Success ! */
378 }
379 else
380 {
381 vty->fail++;
382 if (vty->fail >= 3)
383 {
384 if (vty->node == AUTH_NODE)
385 {
386 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
387 vty->status = VTY_CLOSE;
388 }
389 else
390 {
391 /* AUTH_ENABLE_NODE */
392 vty->fail = 0;
393 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
394 vty->status = VTY_CLOSE;
395 }
396 }
397 }
398 }
399
400 /* Command execution over the vty interface. */
401 static int
402 vty_command (struct vty *vty, char *buf)
403 {
404 int ret;
405 vector vline;
406 const char *protocolname;
407 char *cp = NULL;
408
409 /*
410 * Log non empty command lines
411 */
412 if (do_log_commands)
413 cp = buf;
414 if (cp != NULL)
415 {
416 /* Skip white spaces. */
417 while (isspace ((int) *cp) && *cp != '\0')
418 cp++;
419 }
420 if (cp != NULL && *cp != '\0')
421 {
422 unsigned i;
423 char vty_str[VTY_BUFSIZ];
424 char prompt_str[VTY_BUFSIZ];
425
426 /* format the base vty info */
427 snprintf(vty_str, sizeof(vty_str), "vty[??]@%s", vty->address);
428 if (vty)
429 for (i = 0; i < vector_active (vtyvec); i++)
430 if (vty == vector_slot (vtyvec, i))
431 {
432 snprintf(vty_str, sizeof(vty_str), "vty[%d]@%s",
433 i, vty->address);
434 break;
435 }
436
437 /* format the prompt */
438 snprintf(prompt_str, sizeof(prompt_str), cmd_prompt (vty->node), vty_str);
439
440 /* now log the command */
441 zlog(NULL, LOG_ERR, "%s%s", prompt_str, buf);
442 }
443 /* Split readline string up into the vector */
444 vline = cmd_make_strvec (buf);
445
446 if (vline == NULL)
447 return CMD_SUCCESS;
448
449 #ifdef CONSUMED_TIME_CHECK
450 {
451 RUSAGE_T before;
452 RUSAGE_T after;
453 unsigned long realtime, cputime;
454
455 GETRUSAGE(&before);
456 #endif /* CONSUMED_TIME_CHECK */
457
458 ret = cmd_execute_command (vline, vty, NULL, 0);
459
460 /* Get the name of the protocol if any */
461 if (zlog_default)
462 protocolname = zlog_proto_names[zlog_default->protocol];
463 else
464 protocolname = zlog_proto_names[ZLOG_NONE];
465
466 #ifdef CONSUMED_TIME_CHECK
467 GETRUSAGE(&after);
468 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
469 CONSUMED_TIME_CHECK)
470 /* Warn about CPU hog that must be fixed. */
471 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
472 realtime/1000, cputime/1000, buf);
473 }
474 #endif /* CONSUMED_TIME_CHECK */
475
476 if (ret != CMD_SUCCESS)
477 switch (ret)
478 {
479 case CMD_WARNING:
480 if (vty->type == VTY_FILE)
481 vty_out (vty, "Warning...%s", VTY_NEWLINE);
482 break;
483 case CMD_ERR_AMBIGUOUS:
484 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
485 break;
486 case CMD_ERR_NO_MATCH:
487 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
488 break;
489 case CMD_ERR_INCOMPLETE:
490 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
491 break;
492 }
493 cmd_free_strvec (vline);
494
495 return ret;
496 }
497
498 static const char telnet_backward_char = 0x08;
499 static const char telnet_space_char = ' ';
500
501 /* Basic function to write buffer to vty. */
502 static void
503 vty_write (struct vty *vty, const char *buf, size_t nbytes)
504 {
505 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
506 return;
507
508 /* Should we do buffering here ? And make vty_flush (vty) ? */
509 buffer_put (vty->obuf, buf, nbytes);
510 }
511
512 /* Ensure length of input buffer. Is buffer is short, double it. */
513 static void
514 vty_ensure (struct vty *vty, int length)
515 {
516 if (vty->max <= length)
517 {
518 vty->max *= 2;
519 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
520 vty->error_buf = XREALLOC (MTYPE_VTY, vty->error_buf, vty->max);
521 }
522 }
523
524 /* Basic function to insert character into vty. */
525 static void
526 vty_self_insert (struct vty *vty, char c)
527 {
528 int i;
529 int length;
530
531 vty_ensure (vty, vty->length + 1);
532 length = vty->length - vty->cp;
533 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
534 vty->buf[vty->cp] = c;
535
536 vty_write (vty, &vty->buf[vty->cp], length + 1);
537 for (i = 0; i < length; i++)
538 vty_write (vty, &telnet_backward_char, 1);
539
540 vty->cp++;
541 vty->length++;
542 }
543
544 /* Self insert character 'c' in overwrite mode. */
545 static void
546 vty_self_insert_overwrite (struct vty *vty, char c)
547 {
548 vty_ensure (vty, vty->length + 1);
549 vty->buf[vty->cp++] = c;
550
551 if (vty->cp > vty->length)
552 vty->length++;
553
554 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
555 return;
556
557 vty_write (vty, &c, 1);
558 }
559
560 /* Insert a word into vty interface with overwrite mode. */
561 static void
562 vty_insert_word_overwrite (struct vty *vty, char *str)
563 {
564 int len = strlen (str);
565 vty_write (vty, str, len);
566 strcpy (&vty->buf[vty->cp], str);
567 vty->cp += len;
568 vty->length = vty->cp;
569 }
570
571 /* Forward character. */
572 static void
573 vty_forward_char (struct vty *vty)
574 {
575 if (vty->cp < vty->length)
576 {
577 vty_write (vty, &vty->buf[vty->cp], 1);
578 vty->cp++;
579 }
580 }
581
582 /* Backward character. */
583 static void
584 vty_backward_char (struct vty *vty)
585 {
586 if (vty->cp > 0)
587 {
588 vty->cp--;
589 vty_write (vty, &telnet_backward_char, 1);
590 }
591 }
592
593 /* Move to the beginning of the line. */
594 static void
595 vty_beginning_of_line (struct vty *vty)
596 {
597 while (vty->cp)
598 vty_backward_char (vty);
599 }
600
601 /* Move to the end of the line. */
602 static void
603 vty_end_of_line (struct vty *vty)
604 {
605 while (vty->cp < vty->length)
606 vty_forward_char (vty);
607 }
608
609 static void vty_kill_line_from_beginning (struct vty *);
610 static void vty_redraw_line (struct vty *);
611
612 /* Print command line history. This function is called from
613 vty_next_line and vty_previous_line. */
614 static void
615 vty_history_print (struct vty *vty)
616 {
617 int length;
618
619 vty_kill_line_from_beginning (vty);
620
621 /* Get previous line from history buffer */
622 length = strlen (vty->hist[vty->hp]);
623 memcpy (vty->buf, vty->hist[vty->hp], length);
624 vty->cp = vty->length = length;
625
626 /* Redraw current line */
627 vty_redraw_line (vty);
628 }
629
630 /* Show next command line history. */
631 static void
632 vty_next_line (struct vty *vty)
633 {
634 int try_index;
635
636 if (vty->hp == vty->hindex)
637 return;
638
639 /* Try is there history exist or not. */
640 try_index = vty->hp;
641 if (try_index == (VTY_MAXHIST - 1))
642 try_index = 0;
643 else
644 try_index++;
645
646 /* If there is not history return. */
647 if (vty->hist[try_index] == NULL)
648 return;
649 else
650 vty->hp = try_index;
651
652 vty_history_print (vty);
653 }
654
655 /* Show previous command line history. */
656 static void
657 vty_previous_line (struct vty *vty)
658 {
659 int try_index;
660
661 try_index = vty->hp;
662 if (try_index == 0)
663 try_index = VTY_MAXHIST - 1;
664 else
665 try_index--;
666
667 if (vty->hist[try_index] == NULL)
668 return;
669 else
670 vty->hp = try_index;
671
672 vty_history_print (vty);
673 }
674
675 /* This function redraw all of the command line character. */
676 static void
677 vty_redraw_line (struct vty *vty)
678 {
679 vty_write (vty, vty->buf, vty->length);
680 vty->cp = vty->length;
681 }
682
683 /* Forward word. */
684 static void
685 vty_forward_word (struct vty *vty)
686 {
687 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
688 vty_forward_char (vty);
689
690 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
691 vty_forward_char (vty);
692 }
693
694 /* Backward word without skipping training space. */
695 static void
696 vty_backward_pure_word (struct vty *vty)
697 {
698 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
699 vty_backward_char (vty);
700 }
701
702 /* Backward word. */
703 static void
704 vty_backward_word (struct vty *vty)
705 {
706 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
707 vty_backward_char (vty);
708
709 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
710 vty_backward_char (vty);
711 }
712
713 /* When '^D' is typed at the beginning of the line we move to the down
714 level. */
715 static void
716 vty_down_level (struct vty *vty)
717 {
718 vty_out (vty, "%s", VTY_NEWLINE);
719 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
720 vty_prompt (vty);
721 vty->cp = 0;
722 }
723
724 /* When '^Z' is received from vty, move down to the enable mode. */
725 static void
726 vty_end_config (struct vty *vty)
727 {
728 vty_out (vty, "%s", VTY_NEWLINE);
729
730 switch (vty->node)
731 {
732 case VIEW_NODE:
733 case ENABLE_NODE:
734 /* Nothing to do. */
735 break;
736 case CONFIG_NODE:
737 case INTERFACE_NODE:
738 case ZEBRA_NODE:
739 case RIP_NODE:
740 case RIPNG_NODE:
741 case BGP_NODE:
742 case BGP_VPNV4_NODE:
743 case BGP_VPNV6_NODE:
744 case BGP_ENCAP_NODE:
745 case BGP_ENCAPV6_NODE:
746 case BGP_VNC_DEFAULTS_NODE:
747 case BGP_VNC_NVE_GROUP_NODE:
748 case BGP_VNC_L2_GROUP_NODE:
749 case BGP_IPV4_NODE:
750 case BGP_IPV4M_NODE:
751 case BGP_IPV6_NODE:
752 case BGP_IPV6M_NODE:
753 case RMAP_NODE:
754 case OSPF_NODE:
755 case OSPF6_NODE:
756 case LDP_NODE:
757 case LDP_IPV4_NODE:
758 case LDP_IPV6_NODE:
759 case LDP_IPV4_IFACE_NODE:
760 case LDP_IPV6_IFACE_NODE:
761 case LDP_L2VPN_NODE:
762 case LDP_PSEUDOWIRE_NODE:
763 case ISIS_NODE:
764 case KEYCHAIN_NODE:
765 case KEYCHAIN_KEY_NODE:
766 case MASC_NODE:
767 case PIM_NODE:
768 case VTY_NODE:
769 vty_config_unlock (vty);
770 vty->node = ENABLE_NODE;
771 break;
772 default:
773 /* Unknown node, we have to ignore it. */
774 break;
775 }
776
777 vty_prompt (vty);
778 vty->cp = 0;
779 }
780
781 /* Delete a charcter at the current point. */
782 static void
783 vty_delete_char (struct vty *vty)
784 {
785 int i;
786 int size;
787
788 if (vty->length == 0)
789 {
790 vty_down_level (vty);
791 return;
792 }
793
794 if (vty->cp == vty->length)
795 return; /* completion need here? */
796
797 size = vty->length - vty->cp;
798
799 vty->length--;
800 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
801 vty->buf[vty->length] = '\0';
802
803 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
804 return;
805
806 vty_write (vty, &vty->buf[vty->cp], size - 1);
807 vty_write (vty, &telnet_space_char, 1);
808
809 for (i = 0; i < size; i++)
810 vty_write (vty, &telnet_backward_char, 1);
811 }
812
813 /* Delete a character before the point. */
814 static void
815 vty_delete_backward_char (struct vty *vty)
816 {
817 if (vty->cp == 0)
818 return;
819
820 vty_backward_char (vty);
821 vty_delete_char (vty);
822 }
823
824 /* Kill rest of line from current point. */
825 static void
826 vty_kill_line (struct vty *vty)
827 {
828 int i;
829 int size;
830
831 size = vty->length - vty->cp;
832
833 if (size == 0)
834 return;
835
836 for (i = 0; i < size; i++)
837 vty_write (vty, &telnet_space_char, 1);
838 for (i = 0; i < size; i++)
839 vty_write (vty, &telnet_backward_char, 1);
840
841 memset (&vty->buf[vty->cp], 0, size);
842 vty->length = vty->cp;
843 }
844
845 /* Kill line from the beginning. */
846 static void
847 vty_kill_line_from_beginning (struct vty *vty)
848 {
849 vty_beginning_of_line (vty);
850 vty_kill_line (vty);
851 }
852
853 /* Delete a word before the point. */
854 static void
855 vty_forward_kill_word (struct vty *vty)
856 {
857 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
858 vty_delete_char (vty);
859 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
860 vty_delete_char (vty);
861 }
862
863 /* Delete a word before the point. */
864 static void
865 vty_backward_kill_word (struct vty *vty)
866 {
867 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
868 vty_delete_backward_char (vty);
869 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
870 vty_delete_backward_char (vty);
871 }
872
873 /* Transpose chars before or at the point. */
874 static void
875 vty_transpose_chars (struct vty *vty)
876 {
877 char c1, c2;
878
879 /* If length is short or point is near by the beginning of line then
880 return. */
881 if (vty->length < 2 || vty->cp < 1)
882 return;
883
884 /* In case of point is located at the end of the line. */
885 if (vty->cp == vty->length)
886 {
887 c1 = vty->buf[vty->cp - 1];
888 c2 = vty->buf[vty->cp - 2];
889
890 vty_backward_char (vty);
891 vty_backward_char (vty);
892 vty_self_insert_overwrite (vty, c1);
893 vty_self_insert_overwrite (vty, c2);
894 }
895 else
896 {
897 c1 = vty->buf[vty->cp];
898 c2 = vty->buf[vty->cp - 1];
899
900 vty_backward_char (vty);
901 vty_self_insert_overwrite (vty, c1);
902 vty_self_insert_overwrite (vty, c2);
903 }
904 }
905
906 /* Do completion at vty interface. */
907 static void
908 vty_complete_command (struct vty *vty)
909 {
910 int i;
911 int ret;
912 char **matched = NULL;
913 vector vline;
914
915 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
916 return;
917
918 vline = cmd_make_strvec (vty->buf);
919 if (vline == NULL)
920 return;
921
922 /* In case of 'help \t'. */
923 if (isspace ((int) vty->buf[vty->length - 1]))
924 vector_set (vline, NULL);
925
926 matched = cmd_complete_command_lib (vline, vty, &ret, 1);
927
928 cmd_free_strvec (vline);
929
930 vty_out (vty, "%s", VTY_NEWLINE);
931 switch (ret)
932 {
933 case CMD_ERR_AMBIGUOUS:
934 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
935 vty_prompt (vty);
936 vty_redraw_line (vty);
937 break;
938 case CMD_ERR_NO_MATCH:
939 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
940 vty_prompt (vty);
941 vty_redraw_line (vty);
942 break;
943 case CMD_COMPLETE_FULL_MATCH:
944 vty_prompt (vty);
945 vty_redraw_line (vty);
946 vty_backward_pure_word (vty);
947 vty_insert_word_overwrite (vty, matched[0]);
948 vty_self_insert (vty, ' ');
949 XFREE (MTYPE_TMP, matched[0]);
950 break;
951 case CMD_COMPLETE_MATCH:
952 vty_prompt (vty);
953 vty_redraw_line (vty);
954 vty_backward_pure_word (vty);
955 vty_insert_word_overwrite (vty, matched[0]);
956 XFREE (MTYPE_TMP, matched[0]);
957 vector_only_index_free (matched);
958 return;
959 break;
960 case CMD_COMPLETE_LIST_MATCH:
961 for (i = 0; matched[i] != NULL; i++)
962 {
963 if (i != 0 && ((i % 6) == 0))
964 vty_out (vty, "%s", VTY_NEWLINE);
965 vty_out (vty, "%-10s ", matched[i]);
966 XFREE (MTYPE_TMP, matched[i]);
967 }
968 vty_out (vty, "%s", VTY_NEWLINE);
969
970 vty_prompt (vty);
971 vty_redraw_line (vty);
972 break;
973 case CMD_ERR_NOTHING_TODO:
974 vty_prompt (vty);
975 vty_redraw_line (vty);
976 break;
977 default:
978 break;
979 }
980 if (matched)
981 vector_only_index_free (matched);
982 }
983
984 static void
985 vty_describe_fold (struct vty *vty, int cmd_width,
986 unsigned int desc_width, struct cmd_token *token)
987 {
988 char *buf;
989 const char *cmd, *p;
990 int pos;
991
992 cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
993
994 if (desc_width <= 0)
995 {
996 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
997 return;
998 }
999
1000 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
1001
1002 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
1003 {
1004 for (pos = desc_width; pos > 0; pos--)
1005 if (*(p + pos) == ' ')
1006 break;
1007
1008 if (pos == 0)
1009 break;
1010
1011 strncpy (buf, p, pos);
1012 buf[pos] = '\0';
1013 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
1014
1015 cmd = "";
1016 }
1017
1018 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
1019
1020 XFREE (MTYPE_TMP, buf);
1021 }
1022
1023 /* Describe matched command function. */
1024 static void
1025 vty_describe_command (struct vty *vty)
1026 {
1027 int ret;
1028 vector vline;
1029 vector describe;
1030 unsigned int i, width, desc_width;
1031 struct cmd_token *token, *token_cr = NULL;
1032
1033 vline = cmd_make_strvec (vty->buf);
1034
1035 /* In case of '> ?'. */
1036 if (vline == NULL)
1037 {
1038 vline = vector_init (1);
1039 vector_set (vline, NULL);
1040 }
1041 else
1042 if (isspace ((int) vty->buf[vty->length - 1]))
1043 vector_set (vline, NULL);
1044
1045 describe = cmd_describe_command (vline, vty, &ret);
1046
1047 vty_out (vty, "%s", VTY_NEWLINE);
1048
1049 /* Ambiguous error. */
1050 switch (ret)
1051 {
1052 case CMD_ERR_AMBIGUOUS:
1053 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
1054 goto out;
1055 break;
1056 case CMD_ERR_NO_MATCH:
1057 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
1058 goto out;
1059 break;
1060 }
1061
1062 /* Get width of command string. */
1063 width = 0;
1064 for (i = 0; i < vector_active (describe); i++)
1065 if ((token = vector_slot (describe, i)) != NULL)
1066 {
1067 unsigned int len;
1068
1069 if (token->cmd[0] == '\0')
1070 continue;
1071
1072 len = strlen (token->cmd);
1073 if (token->cmd[0] == '.')
1074 len--;
1075
1076 if (width < len)
1077 width = len;
1078 }
1079
1080 /* Get width of description string. */
1081 desc_width = vty->width - (width + 6);
1082
1083 /* Print out description. */
1084 for (i = 0; i < vector_active (describe); i++)
1085 if ((token = vector_slot (describe, i)) != NULL)
1086 {
1087 if (token->cmd[0] == '\0')
1088 continue;
1089
1090 if (strcmp (token->cmd, command_cr) == 0)
1091 {
1092 token_cr = token;
1093 continue;
1094 }
1095
1096 if (!token->desc)
1097 vty_out (vty, " %-s%s",
1098 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1099 VTY_NEWLINE);
1100 else if (desc_width >= strlen (token->desc))
1101 vty_out (vty, " %-*s %s%s", width,
1102 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1103 token->desc, VTY_NEWLINE);
1104 else
1105 vty_describe_fold (vty, width, desc_width, token);
1106
1107 #if 0
1108 vty_out (vty, " %-*s %s%s", width
1109 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1110 desc->str ? desc->str : "", VTY_NEWLINE);
1111 #endif /* 0 */
1112 }
1113
1114 if ((token = token_cr))
1115 {
1116 if (!token->desc)
1117 vty_out (vty, " %-s%s",
1118 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1119 VTY_NEWLINE);
1120 else if (desc_width >= strlen (token->desc))
1121 vty_out (vty, " %-*s %s%s", width,
1122 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1123 token->desc, VTY_NEWLINE);
1124 else
1125 vty_describe_fold (vty, width, desc_width, token);
1126 }
1127
1128 out:
1129 cmd_free_strvec (vline);
1130 if (describe)
1131 vector_free (describe);
1132
1133 vty_prompt (vty);
1134 vty_redraw_line (vty);
1135 }
1136
1137 static void
1138 vty_clear_buf (struct vty *vty)
1139 {
1140 memset (vty->buf, 0, vty->max);
1141 }
1142
1143 /* ^C stop current input and do not add command line to the history. */
1144 static void
1145 vty_stop_input (struct vty *vty)
1146 {
1147 vty->cp = vty->length = 0;
1148 vty_clear_buf (vty);
1149 vty_out (vty, "%s", VTY_NEWLINE);
1150
1151 switch (vty->node)
1152 {
1153 case VIEW_NODE:
1154 case ENABLE_NODE:
1155 /* Nothing to do. */
1156 break;
1157 case CONFIG_NODE:
1158 case INTERFACE_NODE:
1159 case ZEBRA_NODE:
1160 case RIP_NODE:
1161 case RIPNG_NODE:
1162 case BGP_NODE:
1163 case RMAP_NODE:
1164 case OSPF_NODE:
1165 case OSPF6_NODE:
1166 case LDP_NODE:
1167 case LDP_IPV4_NODE:
1168 case LDP_IPV6_NODE:
1169 case LDP_IPV4_IFACE_NODE:
1170 case LDP_IPV6_IFACE_NODE:
1171 case LDP_L2VPN_NODE:
1172 case LDP_PSEUDOWIRE_NODE:
1173 case ISIS_NODE:
1174 case KEYCHAIN_NODE:
1175 case KEYCHAIN_KEY_NODE:
1176 case MASC_NODE:
1177 case PIM_NODE:
1178 case VTY_NODE:
1179 vty_config_unlock (vty);
1180 vty->node = ENABLE_NODE;
1181 break;
1182 default:
1183 /* Unknown node, we have to ignore it. */
1184 break;
1185 }
1186 vty_prompt (vty);
1187
1188 /* Set history pointer to the latest one. */
1189 vty->hp = vty->hindex;
1190 }
1191
1192 /* Add current command line to the history buffer. */
1193 static void
1194 vty_hist_add (struct vty *vty)
1195 {
1196 int index;
1197
1198 if (vty->length == 0)
1199 return;
1200
1201 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1202
1203 /* Ignore the same string as previous one. */
1204 if (vty->hist[index])
1205 if (strcmp (vty->buf, vty->hist[index]) == 0)
1206 {
1207 vty->hp = vty->hindex;
1208 return;
1209 }
1210
1211 /* Insert history entry. */
1212 if (vty->hist[vty->hindex])
1213 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1214 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1215
1216 /* History index rotation. */
1217 vty->hindex++;
1218 if (vty->hindex == VTY_MAXHIST)
1219 vty->hindex = 0;
1220
1221 vty->hp = vty->hindex;
1222 }
1223
1224 /* #define TELNET_OPTION_DEBUG */
1225
1226 /* Get telnet window size. */
1227 static int
1228 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1229 {
1230 #ifdef TELNET_OPTION_DEBUG
1231 int i;
1232
1233 for (i = 0; i < nbytes; i++)
1234 {
1235 switch (buf[i])
1236 {
1237 case IAC:
1238 vty_out (vty, "IAC ");
1239 break;
1240 case WILL:
1241 vty_out (vty, "WILL ");
1242 break;
1243 case WONT:
1244 vty_out (vty, "WONT ");
1245 break;
1246 case DO:
1247 vty_out (vty, "DO ");
1248 break;
1249 case DONT:
1250 vty_out (vty, "DONT ");
1251 break;
1252 case SB:
1253 vty_out (vty, "SB ");
1254 break;
1255 case SE:
1256 vty_out (vty, "SE ");
1257 break;
1258 case TELOPT_ECHO:
1259 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1260 break;
1261 case TELOPT_SGA:
1262 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1263 break;
1264 case TELOPT_NAWS:
1265 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1266 break;
1267 default:
1268 vty_out (vty, "%x ", buf[i]);
1269 break;
1270 }
1271 }
1272 vty_out (vty, "%s", VTY_NEWLINE);
1273
1274 #endif /* TELNET_OPTION_DEBUG */
1275
1276 switch (buf[0])
1277 {
1278 case SB:
1279 vty->sb_len = 0;
1280 vty->iac_sb_in_progress = 1;
1281 return 0;
1282 break;
1283 case SE:
1284 {
1285 if (!vty->iac_sb_in_progress)
1286 return 0;
1287
1288 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
1289 {
1290 vty->iac_sb_in_progress = 0;
1291 return 0;
1292 }
1293 switch (vty->sb_buf[0])
1294 {
1295 case TELOPT_NAWS:
1296 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1297 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1298 "should send %d characters, but we received %lu",
1299 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1300 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1301 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1302 "too small to handle the telnet NAWS option",
1303 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1304 else
1305 {
1306 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1307 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1308 #ifdef TELNET_OPTION_DEBUG
1309 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1310 "width %d, height %d%s",
1311 vty->width, vty->height, VTY_NEWLINE);
1312 #endif
1313 }
1314 break;
1315 }
1316 vty->iac_sb_in_progress = 0;
1317 return 0;
1318 break;
1319 }
1320 default:
1321 break;
1322 }
1323 return 1;
1324 }
1325
1326 /* Execute current command line. */
1327 static int
1328 vty_execute (struct vty *vty)
1329 {
1330 int ret;
1331
1332 ret = CMD_SUCCESS;
1333
1334 switch (vty->node)
1335 {
1336 case AUTH_NODE:
1337 case AUTH_ENABLE_NODE:
1338 vty_auth (vty, vty->buf);
1339 break;
1340 default:
1341 ret = vty_command (vty, vty->buf);
1342 if (vty->type == VTY_TERM)
1343 vty_hist_add (vty);
1344 break;
1345 }
1346
1347 /* Clear command line buffer. */
1348 vty->cp = vty->length = 0;
1349 vty_clear_buf (vty);
1350
1351 if (vty->status != VTY_CLOSE )
1352 vty_prompt (vty);
1353
1354 return ret;
1355 }
1356
1357 #define CONTROL(X) ((X) - '@')
1358 #define VTY_NORMAL 0
1359 #define VTY_PRE_ESCAPE 1
1360 #define VTY_ESCAPE 2
1361
1362 /* Escape character command map. */
1363 static void
1364 vty_escape_map (unsigned char c, struct vty *vty)
1365 {
1366 switch (c)
1367 {
1368 case ('A'):
1369 vty_previous_line (vty);
1370 break;
1371 case ('B'):
1372 vty_next_line (vty);
1373 break;
1374 case ('C'):
1375 vty_forward_char (vty);
1376 break;
1377 case ('D'):
1378 vty_backward_char (vty);
1379 break;
1380 default:
1381 break;
1382 }
1383
1384 /* Go back to normal mode. */
1385 vty->escape = VTY_NORMAL;
1386 }
1387
1388 /* Quit print out to the buffer. */
1389 static void
1390 vty_buffer_reset (struct vty *vty)
1391 {
1392 buffer_reset (vty->obuf);
1393 vty_prompt (vty);
1394 vty_redraw_line (vty);
1395 }
1396
1397 /* Read data via vty socket. */
1398 static int
1399 vty_read (struct thread *thread)
1400 {
1401 int i;
1402 int nbytes;
1403 unsigned char buf[VTY_READ_BUFSIZ];
1404
1405 int vty_sock = THREAD_FD (thread);
1406 struct vty *vty = THREAD_ARG (thread);
1407 vty->t_read = NULL;
1408
1409 /* Read raw data from socket */
1410 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1411 {
1412 if (nbytes < 0)
1413 {
1414 if (ERRNO_IO_RETRY(errno))
1415 {
1416 vty_event (VTY_READ, vty_sock, vty);
1417 return 0;
1418 }
1419 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1420 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1421 __func__, vty->fd, safe_strerror(errno));
1422 buffer_reset(vty->obuf);
1423 }
1424 vty->status = VTY_CLOSE;
1425 }
1426
1427 for (i = 0; i < nbytes; i++)
1428 {
1429 if (buf[i] == IAC)
1430 {
1431 if (!vty->iac)
1432 {
1433 vty->iac = 1;
1434 continue;
1435 }
1436 else
1437 {
1438 vty->iac = 0;
1439 }
1440 }
1441
1442 if (vty->iac_sb_in_progress && !vty->iac)
1443 {
1444 if (vty->sb_len < sizeof(vty->sb_buf))
1445 vty->sb_buf[vty->sb_len] = buf[i];
1446 vty->sb_len++;
1447 continue;
1448 }
1449
1450 if (vty->iac)
1451 {
1452 /* In case of telnet command */
1453 int ret = 0;
1454 ret = vty_telnet_option (vty, buf + i, nbytes - i);
1455 vty->iac = 0;
1456 i += ret;
1457 continue;
1458 }
1459
1460
1461 if (vty->status == VTY_MORE)
1462 {
1463 switch (buf[i])
1464 {
1465 case CONTROL('C'):
1466 case 'q':
1467 case 'Q':
1468 vty_buffer_reset (vty);
1469 break;
1470 #if 0 /* More line does not work for "show ip bgp". */
1471 case '\n':
1472 case '\r':
1473 vty->status = VTY_MORELINE;
1474 break;
1475 #endif
1476 default:
1477 break;
1478 }
1479 continue;
1480 }
1481
1482 /* Escape character. */
1483 if (vty->escape == VTY_ESCAPE)
1484 {
1485 vty_escape_map (buf[i], vty);
1486 continue;
1487 }
1488
1489 /* Pre-escape status. */
1490 if (vty->escape == VTY_PRE_ESCAPE)
1491 {
1492 switch (buf[i])
1493 {
1494 case '[':
1495 vty->escape = VTY_ESCAPE;
1496 break;
1497 case 'b':
1498 vty_backward_word (vty);
1499 vty->escape = VTY_NORMAL;
1500 break;
1501 case 'f':
1502 vty_forward_word (vty);
1503 vty->escape = VTY_NORMAL;
1504 break;
1505 case 'd':
1506 vty_forward_kill_word (vty);
1507 vty->escape = VTY_NORMAL;
1508 break;
1509 case CONTROL('H'):
1510 case 0x7f:
1511 vty_backward_kill_word (vty);
1512 vty->escape = VTY_NORMAL;
1513 break;
1514 default:
1515 vty->escape = VTY_NORMAL;
1516 break;
1517 }
1518 continue;
1519 }
1520
1521 switch (buf[i])
1522 {
1523 case CONTROL('A'):
1524 vty_beginning_of_line (vty);
1525 break;
1526 case CONTROL('B'):
1527 vty_backward_char (vty);
1528 break;
1529 case CONTROL('C'):
1530 vty_stop_input (vty);
1531 break;
1532 case CONTROL('D'):
1533 vty_delete_char (vty);
1534 break;
1535 case CONTROL('E'):
1536 vty_end_of_line (vty);
1537 break;
1538 case CONTROL('F'):
1539 vty_forward_char (vty);
1540 break;
1541 case CONTROL('H'):
1542 case 0x7f:
1543 vty_delete_backward_char (vty);
1544 break;
1545 case CONTROL('K'):
1546 vty_kill_line (vty);
1547 break;
1548 case CONTROL('N'):
1549 vty_next_line (vty);
1550 break;
1551 case CONTROL('P'):
1552 vty_previous_line (vty);
1553 break;
1554 case CONTROL('T'):
1555 vty_transpose_chars (vty);
1556 break;
1557 case CONTROL('U'):
1558 vty_kill_line_from_beginning (vty);
1559 break;
1560 case CONTROL('W'):
1561 vty_backward_kill_word (vty);
1562 break;
1563 case CONTROL('Z'):
1564 vty_end_config (vty);
1565 break;
1566 case '\n':
1567 case '\r':
1568 vty_out (vty, "%s", VTY_NEWLINE);
1569 vty_execute (vty);
1570 break;
1571 case '\t':
1572 vty_complete_command (vty);
1573 break;
1574 case '?':
1575 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1576 vty_self_insert (vty, buf[i]);
1577 else
1578 vty_describe_command (vty);
1579 break;
1580 case '\033':
1581 if (i + 1 < nbytes && buf[i + 1] == '[')
1582 {
1583 vty->escape = VTY_ESCAPE;
1584 i++;
1585 }
1586 else
1587 vty->escape = VTY_PRE_ESCAPE;
1588 break;
1589 default:
1590 if (buf[i] > 31 && buf[i] < 127)
1591 vty_self_insert (vty, buf[i]);
1592 break;
1593 }
1594 }
1595
1596 /* Check status. */
1597 if (vty->status == VTY_CLOSE)
1598 vty_close (vty);
1599 else
1600 {
1601 vty_event (VTY_WRITE, vty->wfd, vty);
1602 vty_event (VTY_READ, vty_sock, vty);
1603 }
1604 return 0;
1605 }
1606
1607 /* Flush buffer to the vty. */
1608 static int
1609 vty_flush (struct thread *thread)
1610 {
1611 int erase;
1612 buffer_status_t flushrc;
1613 int vty_sock = THREAD_FD (thread);
1614 struct vty *vty = THREAD_ARG (thread);
1615
1616 vty->t_write = NULL;
1617
1618 /* Tempolary disable read thread. */
1619 if ((vty->lines == 0) && vty->t_read)
1620 {
1621 thread_cancel (vty->t_read);
1622 vty->t_read = NULL;
1623 }
1624
1625 /* Function execution continue. */
1626 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1627
1628 /* N.B. if width is 0, that means we don't know the window size. */
1629 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
1630 flushrc = buffer_flush_available(vty->obuf, vty_sock);
1631 else if (vty->status == VTY_MORELINE)
1632 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1633 1, erase, 0);
1634 else
1635 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1636 vty->lines >= 0 ? vty->lines :
1637 vty->height,
1638 erase, 0);
1639 switch (flushrc)
1640 {
1641 case BUFFER_ERROR:
1642 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1643 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1644 vty->fd);
1645 buffer_reset(vty->obuf);
1646 vty_close(vty);
1647 return 0;
1648 case BUFFER_EMPTY:
1649 if (vty->status == VTY_CLOSE)
1650 vty_close (vty);
1651 else
1652 {
1653 vty->status = VTY_NORMAL;
1654 if (vty->lines == 0)
1655 vty_event (VTY_READ, vty_sock, vty);
1656 }
1657 break;
1658 case BUFFER_PENDING:
1659 /* There is more data waiting to be written. */
1660 vty->status = VTY_MORE;
1661 if (vty->lines == 0)
1662 vty_event (VTY_WRITE, vty_sock, vty);
1663 break;
1664 }
1665
1666 return 0;
1667 }
1668
1669 /* allocate and initialise vty */
1670 static struct vty *
1671 vty_new_init (int vty_sock)
1672 {
1673 struct vty *vty;
1674
1675 vty = vty_new ();
1676 vty->fd = vty_sock;
1677 vty->wfd = vty_sock;
1678 vty->type = VTY_TERM;
1679 vty->node = AUTH_NODE;
1680 vty->fail = 0;
1681 vty->cp = 0;
1682 vty_clear_buf (vty);
1683 vty->length = 0;
1684 memset (vty->hist, 0, sizeof (vty->hist));
1685 vty->hp = 0;
1686 vty->hindex = 0;
1687 vector_set_index (vtyvec, vty_sock, vty);
1688 vty->status = VTY_NORMAL;
1689 vty->lines = -1;
1690 vty->iac = 0;
1691 vty->iac_sb_in_progress = 0;
1692 vty->sb_len = 0;
1693
1694 return vty;
1695 }
1696
1697 /* Create new vty structure. */
1698 static struct vty *
1699 vty_create (int vty_sock, union sockunion *su)
1700 {
1701 char buf[SU_ADDRSTRLEN];
1702 struct vty *vty;
1703
1704 sockunion2str(su, buf, SU_ADDRSTRLEN);
1705
1706 /* Allocate new vty structure and set up default values. */
1707 vty = vty_new_init (vty_sock);
1708
1709 /* configurable parameters not part of basic init */
1710 vty->v_timeout = vty_timeout_val;
1711 strcpy (vty->address, buf);
1712 if (no_password_check)
1713 {
1714 if (host.advanced)
1715 vty->node = ENABLE_NODE;
1716 else
1717 vty->node = VIEW_NODE;
1718 }
1719 if (host.lines >= 0)
1720 vty->lines = host.lines;
1721
1722 if (! no_password_check)
1723 {
1724 /* Vty is not available if password isn't set. */
1725 if (host.password == NULL && host.password_encrypt == NULL)
1726 {
1727 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1728 vty->status = VTY_CLOSE;
1729 vty_close (vty);
1730 return NULL;
1731 }
1732 }
1733
1734 /* Say hello to the world. */
1735 vty_hello (vty);
1736 if (! no_password_check)
1737 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1738
1739 /* Setting up terminal. */
1740 vty_will_echo (vty);
1741 vty_will_suppress_go_ahead (vty);
1742
1743 vty_dont_linemode (vty);
1744 vty_do_window_size (vty);
1745 /* vty_dont_lflow_ahead (vty); */
1746
1747 vty_prompt (vty);
1748
1749 /* Add read/write thread. */
1750 vty_event (VTY_WRITE, vty_sock, vty);
1751 vty_event (VTY_READ, vty_sock, vty);
1752
1753 return vty;
1754 }
1755
1756 /* create vty for stdio */
1757 static struct termios stdio_orig_termios;
1758 static struct vty *stdio_vty = NULL;
1759 static void (*stdio_vty_atclose)(void);
1760
1761 static void
1762 vty_stdio_reset (void)
1763 {
1764 if (stdio_vty)
1765 {
1766 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1767 stdio_vty = NULL;
1768
1769 if (stdio_vty_atclose)
1770 stdio_vty_atclose ();
1771 stdio_vty_atclose = NULL;
1772 }
1773 }
1774
1775 struct vty *
1776 vty_stdio (void (*atclose)())
1777 {
1778 struct vty *vty;
1779 struct termios termios;
1780
1781 /* refuse creating two vtys on stdio */
1782 if (stdio_vty)
1783 return NULL;
1784
1785 vty = stdio_vty = vty_new_init (0);
1786 stdio_vty_atclose = atclose;
1787 vty->wfd = 1;
1788
1789 /* always have stdio vty in a known _unchangeable_ state, don't want config
1790 * to have any effect here to make sure scripting this works as intended */
1791 vty->node = ENABLE_NODE;
1792 vty->v_timeout = 0;
1793 strcpy (vty->address, "console");
1794
1795 if (!tcgetattr (0, &stdio_orig_termios))
1796 {
1797 termios = stdio_orig_termios;
1798 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1799 | INLCR | IGNCR | ICRNL | IXON);
1800 termios.c_oflag &= ~OPOST;
1801 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1802 termios.c_cflag &= ~(CSIZE | PARENB);
1803 termios.c_cflag |= CS8;
1804 tcsetattr (0, TCSANOW, &termios);
1805 }
1806
1807 vty_prompt (vty);
1808
1809 /* Add read/write thread. */
1810 vty_event (VTY_WRITE, 1, vty);
1811 vty_event (VTY_READ, 0, vty);
1812
1813 return vty;
1814 }
1815
1816 /* Accept connection from the network. */
1817 static int
1818 vty_accept (struct thread *thread)
1819 {
1820 int vty_sock;
1821 union sockunion su;
1822 int ret;
1823 unsigned int on;
1824 int accept_sock;
1825 struct prefix p;
1826 struct access_list *acl = NULL;
1827 char buf[SU_ADDRSTRLEN];
1828
1829 accept_sock = THREAD_FD (thread);
1830
1831 /* We continue hearing vty socket. */
1832 vty_event (VTY_SERV, accept_sock, NULL);
1833
1834 memset (&su, 0, sizeof (union sockunion));
1835
1836 /* We can handle IPv4 or IPv6 socket. */
1837 vty_sock = sockunion_accept (accept_sock, &su);
1838 if (vty_sock < 0)
1839 {
1840 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1841 return -1;
1842 }
1843 set_nonblocking(vty_sock);
1844 set_cloexec(vty_sock);
1845
1846 sockunion2hostprefix (&su, &p);
1847
1848 /* VTY's accesslist apply. */
1849 if (p.family == AF_INET && vty_accesslist_name)
1850 {
1851 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1852 (access_list_apply (acl, &p) == FILTER_DENY))
1853 {
1854 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1855 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1856 close (vty_sock);
1857
1858 /* continue accepting connections */
1859 vty_event (VTY_SERV, accept_sock, NULL);
1860
1861 return 0;
1862 }
1863 }
1864
1865 #ifdef HAVE_IPV6
1866 /* VTY's ipv6 accesslist apply. */
1867 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
1868 {
1869 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1870 (access_list_apply (acl, &p) == FILTER_DENY))
1871 {
1872 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1873 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1874 close (vty_sock);
1875
1876 /* continue accepting connections */
1877 vty_event (VTY_SERV, accept_sock, NULL);
1878
1879 return 0;
1880 }
1881 }
1882 #endif /* HAVE_IPV6 */
1883
1884 on = 1;
1885 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1886 (char *) &on, sizeof (on));
1887 if (ret < 0)
1888 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
1889 safe_strerror (errno));
1890
1891 zlog (NULL, LOG_INFO, "Vty connection from %s",
1892 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1893
1894 vty_create (vty_sock, &su);
1895
1896 return 0;
1897 }
1898
1899 #ifdef HAVE_IPV6
1900 static void
1901 vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1902 {
1903 int ret;
1904 struct addrinfo req;
1905 struct addrinfo *ainfo;
1906 struct addrinfo *ainfo_save;
1907 int sock;
1908 char port_str[BUFSIZ];
1909
1910 memset (&req, 0, sizeof (struct addrinfo));
1911 req.ai_flags = AI_PASSIVE;
1912 req.ai_family = AF_UNSPEC;
1913 req.ai_socktype = SOCK_STREAM;
1914 sprintf (port_str, "%d", port);
1915 port_str[sizeof (port_str) - 1] = '\0';
1916
1917 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1918
1919 if (ret != 0)
1920 {
1921 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1922 exit (1);
1923 }
1924
1925 ainfo_save = ainfo;
1926
1927 do
1928 {
1929 if (ainfo->ai_family != AF_INET
1930 #ifdef HAVE_IPV6
1931 && ainfo->ai_family != AF_INET6
1932 #endif /* HAVE_IPV6 */
1933 )
1934 continue;
1935
1936 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1937 if (sock < 0)
1938 continue;
1939
1940 sockopt_v6only (ainfo->ai_family, sock);
1941 sockopt_reuseaddr (sock);
1942 sockopt_reuseport (sock);
1943 set_cloexec (sock);
1944
1945 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1946 if (ret < 0)
1947 {
1948 close (sock); /* Avoid sd leak. */
1949 continue;
1950 }
1951
1952 ret = listen (sock, 3);
1953 if (ret < 0)
1954 {
1955 close (sock); /* Avoid sd leak. */
1956 continue;
1957 }
1958
1959 vty_event (VTY_SERV, sock, NULL);
1960 }
1961 while ((ainfo = ainfo->ai_next) != NULL);
1962
1963 freeaddrinfo (ainfo_save);
1964 }
1965 #else /* HAVE_IPV6 */
1966
1967 /* Make vty server socket. */
1968 static void
1969 vty_serv_sock_family (const char* addr, unsigned short port, int family)
1970 {
1971 int ret;
1972 union sockunion su;
1973 int accept_sock;
1974 void* naddr=NULL;
1975
1976 memset (&su, 0, sizeof (union sockunion));
1977 su.sa.sa_family = family;
1978 if(addr)
1979 switch(family)
1980 {
1981 case AF_INET:
1982 naddr=&su.sin.sin_addr;
1983 break;
1984 #ifdef HAVE_IPV6
1985 case AF_INET6:
1986 naddr=&su.sin6.sin6_addr;
1987 break;
1988 #endif
1989 }
1990
1991 if(naddr)
1992 switch(inet_pton(family,addr,naddr))
1993 {
1994 case -1:
1995 zlog_err("bad address %s",addr);
1996 naddr=NULL;
1997 break;
1998 case 0:
1999 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
2000 naddr=NULL;
2001 }
2002
2003 /* Make new socket. */
2004 accept_sock = sockunion_stream_socket (&su);
2005 if (accept_sock < 0)
2006 return;
2007
2008 /* This is server, so reuse address. */
2009 sockopt_reuseaddr (accept_sock);
2010 sockopt_reuseport (accept_sock);
2011 set_cloexec (accept_sock);
2012
2013 /* Bind socket to universal address and given port. */
2014 ret = sockunion_bind (accept_sock, &su, port, naddr);
2015 if (ret < 0)
2016 {
2017 zlog_warn("can't bind socket");
2018 close (accept_sock); /* Avoid sd leak. */
2019 return;
2020 }
2021
2022 /* Listen socket under queue 3. */
2023 ret = listen (accept_sock, 3);
2024 if (ret < 0)
2025 {
2026 zlog (NULL, LOG_WARNING, "can't listen socket");
2027 close (accept_sock); /* Avoid sd leak. */
2028 return;
2029 }
2030
2031 /* Add vty server event. */
2032 vty_event (VTY_SERV, accept_sock, NULL);
2033 }
2034 #endif /* HAVE_IPV6 */
2035
2036 #ifdef VTYSH
2037 /* For sockaddr_un. */
2038 #include <sys/un.h>
2039
2040 /* VTY shell UNIX domain socket. */
2041 static void
2042 vty_serv_un (const char *path)
2043 {
2044 int ret;
2045 int sock, len;
2046 struct sockaddr_un serv;
2047 mode_t old_mask;
2048 struct zprivs_ids_t ids;
2049
2050 /* First of all, unlink existing socket */
2051 unlink (path);
2052
2053 /* Set umask */
2054 old_mask = umask (0007);
2055
2056 /* Make UNIX domain socket. */
2057 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2058 if (sock < 0)
2059 {
2060 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
2061 return;
2062 }
2063
2064 /* Make server socket. */
2065 memset (&serv, 0, sizeof (struct sockaddr_un));
2066 serv.sun_family = AF_UNIX;
2067 strncpy (serv.sun_path, path, strlen (path));
2068 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
2069 len = serv.sun_len = SUN_LEN(&serv);
2070 #else
2071 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
2072 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2073
2074 set_cloexec (sock);
2075
2076 ret = bind (sock, (struct sockaddr *) &serv, len);
2077 if (ret < 0)
2078 {
2079 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
2080 close (sock); /* Avoid sd leak. */
2081 return;
2082 }
2083
2084 ret = listen (sock, 5);
2085 if (ret < 0)
2086 {
2087 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
2088 close (sock); /* Avoid sd leak. */
2089 return;
2090 }
2091
2092 umask (old_mask);
2093
2094 zprivs_get_ids(&ids);
2095
2096 if (ids.gid_vty > 0)
2097 {
2098 /* set group of socket */
2099 if ( chown (path, -1, ids.gid_vty) )
2100 {
2101 zlog_err ("vty_serv_un: could chown socket, %s",
2102 safe_strerror (errno) );
2103 }
2104 }
2105
2106 vty_event (VTYSH_SERV, sock, NULL);
2107 }
2108
2109 /* #define VTYSH_DEBUG 1 */
2110
2111 static int
2112 vtysh_accept (struct thread *thread)
2113 {
2114 int accept_sock;
2115 int sock;
2116 int client_len;
2117 struct sockaddr_un client;
2118 struct vty *vty;
2119
2120 accept_sock = THREAD_FD (thread);
2121
2122 vty_event (VTYSH_SERV, accept_sock, NULL);
2123
2124 memset (&client, 0, sizeof (struct sockaddr_un));
2125 client_len = sizeof (struct sockaddr_un);
2126
2127 sock = accept (accept_sock, (struct sockaddr *) &client,
2128 (socklen_t *) &client_len);
2129
2130 if (sock < 0)
2131 {
2132 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
2133 return -1;
2134 }
2135
2136 if (set_nonblocking(sock) < 0)
2137 {
2138 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2139 " %s, closing", sock, safe_strerror (errno));
2140 close (sock);
2141 return -1;
2142 }
2143 set_cloexec(sock);
2144
2145 #ifdef VTYSH_DEBUG
2146 printf ("VTY shell accept\n");
2147 #endif /* VTYSH_DEBUG */
2148
2149 vty = vty_new ();
2150 vty->fd = sock;
2151 vty->wfd = sock;
2152 vty->type = VTY_SHELL_SERV;
2153 vty->node = VIEW_NODE;
2154
2155 vty_event (VTYSH_READ, sock, vty);
2156
2157 return 0;
2158 }
2159
2160 static int
2161 vtysh_flush(struct vty *vty)
2162 {
2163 switch (buffer_flush_available(vty->obuf, vty->wfd))
2164 {
2165 case BUFFER_PENDING:
2166 vty_event(VTYSH_WRITE, vty->wfd, vty);
2167 break;
2168 case BUFFER_ERROR:
2169 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2170 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2171 buffer_reset(vty->obuf);
2172 vty_close(vty);
2173 return -1;
2174 break;
2175 case BUFFER_EMPTY:
2176 break;
2177 }
2178 return 0;
2179 }
2180
2181 static int
2182 vtysh_read (struct thread *thread)
2183 {
2184 int ret;
2185 int sock;
2186 int nbytes;
2187 struct vty *vty;
2188 unsigned char buf[VTY_READ_BUFSIZ];
2189 unsigned char *p;
2190 u_char header[4] = {0, 0, 0, 0};
2191
2192 sock = THREAD_FD (thread);
2193 vty = THREAD_ARG (thread);
2194 vty->t_read = NULL;
2195
2196 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
2197 {
2198 if (nbytes < 0)
2199 {
2200 if (ERRNO_IO_RETRY(errno))
2201 {
2202 vty_event (VTYSH_READ, sock, vty);
2203 return 0;
2204 }
2205 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2206 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2207 __func__, sock, safe_strerror(errno));
2208 }
2209 buffer_reset(vty->obuf);
2210 vty_close (vty);
2211 #ifdef VTYSH_DEBUG
2212 printf ("close vtysh\n");
2213 #endif /* VTYSH_DEBUG */
2214 return 0;
2215 }
2216
2217 #ifdef VTYSH_DEBUG
2218 printf ("line: %.*s\n", nbytes, buf);
2219 #endif /* VTYSH_DEBUG */
2220
2221 for (p = buf; p < buf+nbytes; p++)
2222 {
2223 vty_ensure(vty, vty->length+1);
2224 vty->buf[vty->length++] = *p;
2225 if (*p == '\0')
2226 {
2227 /* Pass this line to parser. */
2228 ret = vty_execute (vty);
2229 /* Note that vty_execute clears the command buffer and resets
2230 vty->length to 0. */
2231
2232 /* Return result. */
2233 #ifdef VTYSH_DEBUG
2234 printf ("result: %d\n", ret);
2235 printf ("vtysh node: %d\n", vty->node);
2236 #endif /* VTYSH_DEBUG */
2237
2238 header[3] = ret;
2239 buffer_put(vty->obuf, header, 4);
2240
2241 if (!vty->t_write && (vtysh_flush(vty) < 0))
2242 /* Try to flush results; exit if a write error occurs. */
2243 return 0;
2244 }
2245 }
2246
2247 vty_event (VTYSH_READ, sock, vty);
2248
2249 return 0;
2250 }
2251
2252 static int
2253 vtysh_write (struct thread *thread)
2254 {
2255 struct vty *vty = THREAD_ARG (thread);
2256
2257 vty->t_write = NULL;
2258 vtysh_flush(vty);
2259 return 0;
2260 }
2261
2262 #endif /* VTYSH */
2263
2264 /* Determine address family to bind. */
2265 void
2266 vty_serv_sock (const char *addr, unsigned short port, const char *path)
2267 {
2268 /* If port is set to 0, do not listen on TCP/IP at all! */
2269 if (port)
2270 {
2271
2272 #ifdef HAVE_IPV6
2273 vty_serv_sock_addrinfo (addr, port);
2274 #else /* ! HAVE_IPV6 */
2275 vty_serv_sock_family (addr,port, AF_INET);
2276 #endif /* HAVE_IPV6 */
2277 }
2278
2279 #ifdef VTYSH
2280 vty_serv_un (path);
2281 #endif /* VTYSH */
2282 }
2283
2284 /* Close vty interface. Warning: call this only from functions that
2285 will be careful not to access the vty afterwards (since it has
2286 now been freed). This is safest from top-level functions (called
2287 directly by the thread dispatcher). */
2288 void
2289 vty_close (struct vty *vty)
2290 {
2291 int i;
2292
2293 /* Cancel threads.*/
2294 if (vty->t_read)
2295 thread_cancel (vty->t_read);
2296 if (vty->t_write)
2297 thread_cancel (vty->t_write);
2298 if (vty->t_timeout)
2299 thread_cancel (vty->t_timeout);
2300
2301 /* Flush buffer. */
2302 buffer_flush_all (vty->obuf, vty->wfd);
2303
2304 /* Free input buffer. */
2305 buffer_free (vty->obuf);
2306
2307 /* Free command history. */
2308 for (i = 0; i < VTY_MAXHIST; i++)
2309 if (vty->hist[i])
2310 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2311
2312 /* Unset vector. */
2313 vector_unset (vtyvec, vty->fd);
2314
2315 /* Close socket. */
2316 if (vty->fd > 0)
2317 close (vty->fd);
2318 else
2319 vty_stdio_reset ();
2320
2321 if (vty->buf)
2322 XFREE (MTYPE_VTY, vty->buf);
2323
2324 if (vty->error_buf)
2325 XFREE (MTYPE_VTY, vty->error_buf);
2326
2327 /* Check configure. */
2328 vty_config_unlock (vty);
2329
2330 /* OK free vty. */
2331 XFREE (MTYPE_VTY, vty);
2332 }
2333
2334 /* When time out occur output message then close connection. */
2335 static int
2336 vty_timeout (struct thread *thread)
2337 {
2338 struct vty *vty;
2339
2340 vty = THREAD_ARG (thread);
2341 vty->t_timeout = NULL;
2342 vty->v_timeout = 0;
2343
2344 /* Clear buffer*/
2345 buffer_reset (vty->obuf);
2346 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2347
2348 /* Close connection. */
2349 vty->status = VTY_CLOSE;
2350 vty_close (vty);
2351
2352 return 0;
2353 }
2354
2355 /* Read up configuration file from file_name. */
2356 static void
2357 vty_read_file (FILE *confp)
2358 {
2359 int ret;
2360 struct vty *vty;
2361 unsigned int line_num = 0;
2362
2363 vty = vty_new ();
2364 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2365 if (vty->wfd < 0)
2366 {
2367 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2368 vty->wfd = STDOUT_FILENO;
2369 }
2370 vty->fd = STDIN_FILENO;
2371 vty->type = VTY_FILE;
2372 vty->node = CONFIG_NODE;
2373
2374 /* Execute configuration file */
2375 ret = config_from_file (vty, confp, &line_num);
2376
2377 /* Flush any previous errors before printing messages below */
2378 buffer_flush_all (vty->obuf, vty->fd);
2379
2380 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
2381 {
2382 switch (ret)
2383 {
2384 case CMD_ERR_AMBIGUOUS:
2385 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
2386 break;
2387 case CMD_ERR_NO_MATCH:
2388 fprintf (stderr, "*** Error reading config: There is no such command.\n");
2389 break;
2390 }
2391 fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
2392 line_num, vty->error_buf);
2393 }
2394
2395 vty_close (vty);
2396 }
2397
2398 static FILE *
2399 vty_use_backup_config (char *fullpath)
2400 {
2401 char *fullpath_sav, *fullpath_tmp;
2402 FILE *ret = NULL;
2403 struct stat buf;
2404 int tmp, sav;
2405 int c;
2406 char buffer[512];
2407
2408 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2409 strcpy (fullpath_sav, fullpath);
2410 strcat (fullpath_sav, CONF_BACKUP_EXT);
2411 if (stat (fullpath_sav, &buf) == -1)
2412 {
2413 free (fullpath_sav);
2414 return NULL;
2415 }
2416
2417 fullpath_tmp = malloc (strlen (fullpath) + 8);
2418 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2419
2420 /* Open file to configuration write. */
2421 tmp = mkstemp (fullpath_tmp);
2422 if (tmp < 0)
2423 {
2424 free (fullpath_sav);
2425 free (fullpath_tmp);
2426 return NULL;
2427 }
2428
2429 sav = open (fullpath_sav, O_RDONLY);
2430 if (sav < 0)
2431 {
2432 unlink (fullpath_tmp);
2433 free (fullpath_sav);
2434 free (fullpath_tmp);
2435 return NULL;
2436 }
2437
2438 while((c = read (sav, buffer, 512)) > 0)
2439 {
2440 if (write (tmp, buffer, c) <= 0)
2441 {
2442 free (fullpath_sav);
2443 free (fullpath_tmp);
2444 close (sav);
2445 close (tmp);
2446 return NULL;
2447 }
2448 }
2449 close (sav);
2450 close (tmp);
2451
2452 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2453 {
2454 unlink (fullpath_tmp);
2455 free (fullpath_sav);
2456 free (fullpath_tmp);
2457 return NULL;
2458 }
2459
2460 if (link (fullpath_tmp, fullpath) == 0)
2461 ret = fopen (fullpath, "r");
2462
2463 unlink (fullpath_tmp);
2464
2465 free (fullpath_sav);
2466 free (fullpath_tmp);
2467 return ret;
2468 }
2469
2470 /* Read up configuration file from file_name. */
2471 void
2472 vty_read_config (char *config_file,
2473 char *config_default_dir)
2474 {
2475 char cwd[MAXPATHLEN];
2476 FILE *confp = NULL;
2477 char *fullpath;
2478 char *tmp = NULL;
2479
2480 /* If -f flag specified. */
2481 if (config_file != NULL)
2482 {
2483 if (! IS_DIRECTORY_SEP (config_file[0]))
2484 {
2485 if (getcwd (cwd, MAXPATHLEN) == NULL)
2486 {
2487 fprintf (stderr, "Failure to determine Current Working Directory %d!\n", errno);
2488 exit (1);
2489 }
2490 tmp = XMALLOC (MTYPE_TMP,
2491 strlen (cwd) + strlen (config_file) + 2);
2492 sprintf (tmp, "%s/%s", cwd, config_file);
2493 fullpath = tmp;
2494 }
2495 else
2496 fullpath = config_file;
2497
2498 confp = fopen (fullpath, "r");
2499
2500 if (confp == NULL)
2501 {
2502 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2503 __func__, fullpath, safe_strerror (errno));
2504
2505 confp = vty_use_backup_config (fullpath);
2506 if (confp)
2507 fprintf (stderr, "WARNING: using backup configuration file!\n");
2508 else
2509 {
2510 fprintf (stderr, "can't open configuration file [%s]\n",
2511 config_file);
2512 exit(1);
2513 }
2514 }
2515 }
2516 else
2517 {
2518
2519 host_config_set (config_default_dir);
2520
2521 #ifdef VTYSH
2522 int ret;
2523 struct stat conf_stat;
2524
2525 /* !!!!PLEASE LEAVE!!!!
2526 * This is NEEDED for use with vtysh -b, or else you can get
2527 * a real configuration food fight with a lot garbage in the
2528 * merged configuration file it creates coming from the per
2529 * daemon configuration files. This also allows the daemons
2530 * to start if there default configuration file is not
2531 * present or ignore them, as needed when using vtysh -b to
2532 * configure the daemons at boot - MAG
2533 */
2534
2535 /* Stat for vtysh Zebra.conf, if found startup and wait for
2536 * boot configuration
2537 */
2538
2539 if ( strstr(config_default_dir, "vtysh") == NULL)
2540 {
2541 ret = stat (integrate_default, &conf_stat);
2542 if (ret >= 0)
2543 goto tmp_free_and_out;
2544 }
2545 #endif /* VTYSH */
2546 confp = fopen (config_default_dir, "r");
2547 if (confp == NULL)
2548 {
2549 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2550 __func__, config_default_dir, safe_strerror (errno));
2551
2552 confp = vty_use_backup_config (config_default_dir);
2553 if (confp)
2554 {
2555 fprintf (stderr, "WARNING: using backup configuration file!\n");
2556 fullpath = config_default_dir;
2557 }
2558 else
2559 {
2560 fprintf (stderr, "can't open configuration file [%s]\n",
2561 config_default_dir);
2562 goto tmp_free_and_out;
2563 }
2564 }
2565 else
2566 fullpath = config_default_dir;
2567 }
2568
2569 vty_read_file (confp);
2570
2571 fclose (confp);
2572
2573 host_config_set (fullpath);
2574
2575 tmp_free_and_out:
2576 if (tmp)
2577 XFREE (MTYPE_TMP, fullpath);
2578 }
2579
2580 /* Small utility function which output log to the VTY. */
2581 void
2582 vty_log (const char *level, const char *proto_str,
2583 const char *format, struct timestamp_control *ctl, va_list va)
2584 {
2585 unsigned int i;
2586 struct vty *vty;
2587
2588 if (!vtyvec)
2589 return;
2590
2591 for (i = 0; i < vector_active (vtyvec); i++)
2592 if ((vty = vector_slot (vtyvec, i)) != NULL)
2593 if (vty->monitor)
2594 {
2595 va_list ac;
2596 va_copy(ac, va);
2597 vty_log_out (vty, level, proto_str, format, ctl, ac);
2598 va_end(ac);
2599 }
2600 }
2601
2602 /* Async-signal-safe version of vty_log for fixed strings. */
2603 void
2604 vty_log_fixed (char *buf, size_t len)
2605 {
2606 unsigned int i;
2607 struct iovec iov[2];
2608 char crlf[4] = "\r\n";
2609
2610 /* vty may not have been initialised */
2611 if (!vtyvec)
2612 return;
2613
2614 iov[0].iov_base = buf;
2615 iov[0].iov_len = len;
2616 iov[1].iov_base = crlf;
2617 iov[1].iov_len = 2;
2618
2619 for (i = 0; i < vector_active (vtyvec); i++)
2620 {
2621 struct vty *vty;
2622 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2623 /* N.B. We don't care about the return code, since process is
2624 most likely just about to die anyway. */
2625 if (writev(vty->wfd, iov, 2) == -1)
2626 {
2627 fprintf(stderr, "Failure to writev: %d\n", errno);
2628 exit(-1);
2629 }
2630 }
2631 }
2632
2633 int
2634 vty_config_lock (struct vty *vty)
2635 {
2636 if (vty_config_is_lockless)
2637 return 1;
2638 if (vty_config == 0)
2639 {
2640 vty->config = 1;
2641 vty_config = 1;
2642 }
2643 return vty->config;
2644 }
2645
2646 int
2647 vty_config_unlock (struct vty *vty)
2648 {
2649 if (vty_config_is_lockless)
2650 return 0;
2651 if (vty_config == 1 && vty->config == 1)
2652 {
2653 vty->config = 0;
2654 vty_config = 0;
2655 }
2656 return vty->config;
2657 }
2658
2659 void
2660 vty_config_lockless (void)
2661 {
2662 vty_config_is_lockless = 1;
2663 }
2664
2665 /* Master of the threads. */
2666 static struct thread_master *vty_master;
2667
2668 static void
2669 vty_event (enum event event, int sock, struct vty *vty)
2670 {
2671 struct thread *vty_serv_thread;
2672
2673 switch (event)
2674 {
2675 case VTY_SERV:
2676 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
2677 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2678 break;
2679 #ifdef VTYSH
2680 case VTYSH_SERV:
2681 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
2682 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2683 break;
2684 case VTYSH_READ:
2685 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
2686 break;
2687 case VTYSH_WRITE:
2688 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
2689 break;
2690 #endif /* VTYSH */
2691 case VTY_READ:
2692 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
2693
2694 /* Time out treatment. */
2695 if (vty->v_timeout)
2696 {
2697 if (vty->t_timeout)
2698 thread_cancel (vty->t_timeout);
2699 vty->t_timeout =
2700 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2701 }
2702 break;
2703 case VTY_WRITE:
2704 if (! vty->t_write)
2705 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
2706 break;
2707 case VTY_TIMEOUT_RESET:
2708 if (vty->t_timeout)
2709 {
2710 thread_cancel (vty->t_timeout);
2711 vty->t_timeout = NULL;
2712 }
2713 if (vty->v_timeout)
2714 {
2715 vty->t_timeout =
2716 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2717 }
2718 break;
2719 }
2720 }
2721
2722 DEFUN (config_who,
2723 config_who_cmd,
2724 "who",
2725 "Display who is on vty\n")
2726 {
2727 unsigned int i;
2728 struct vty *v;
2729
2730 for (i = 0; i < vector_active (vtyvec); i++)
2731 if ((v = vector_slot (vtyvec, i)) != NULL)
2732 vty_out (vty, "%svty[%d] connected from %s.%s",
2733 v->config ? "*" : " ",
2734 i, v->address, VTY_NEWLINE);
2735 return CMD_SUCCESS;
2736 }
2737
2738 /* Move to vty configuration mode. */
2739 DEFUN (line_vty,
2740 line_vty_cmd,
2741 "line vty",
2742 "Configure a terminal line\n"
2743 "Virtual terminal\n")
2744 {
2745 vty->node = VTY_NODE;
2746 return CMD_SUCCESS;
2747 }
2748
2749 /* Set time out value. */
2750 static int
2751 exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
2752 {
2753 unsigned long timeout = 0;
2754
2755 /* min_str and sec_str are already checked by parser. So it must be
2756 all digit string. */
2757 if (min_str)
2758 {
2759 timeout = strtol (min_str, NULL, 10);
2760 timeout *= 60;
2761 }
2762 if (sec_str)
2763 timeout += strtol (sec_str, NULL, 10);
2764
2765 vty_timeout_val = timeout;
2766 vty->v_timeout = timeout;
2767 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2768
2769
2770 return CMD_SUCCESS;
2771 }
2772
2773 DEFUN (exec_timeout_min,
2774 exec_timeout_min_cmd,
2775 "exec-timeout <0-35791>",
2776 "Set timeout value\n"
2777 "Timeout value in minutes\n")
2778 {
2779 return exec_timeout (vty, argv[0], NULL);
2780 }
2781
2782 DEFUN (exec_timeout_sec,
2783 exec_timeout_sec_cmd,
2784 "exec-timeout <0-35791> <0-2147483>",
2785 "Set the EXEC timeout\n"
2786 "Timeout in minutes\n"
2787 "Timeout in seconds\n")
2788 {
2789 return exec_timeout (vty, argv[0], argv[1]);
2790 }
2791
2792 DEFUN (no_exec_timeout,
2793 no_exec_timeout_cmd,
2794 "no exec-timeout",
2795 NO_STR
2796 "Set the EXEC timeout\n")
2797 {
2798 return exec_timeout (vty, NULL, NULL);
2799 }
2800
2801 /* Set vty access class. */
2802 DEFUN (vty_access_class,
2803 vty_access_class_cmd,
2804 "access-class WORD",
2805 "Filter connections based on an IP access list\n"
2806 "IP access list\n")
2807 {
2808 if (vty_accesslist_name)
2809 XFREE(MTYPE_VTY, vty_accesslist_name);
2810
2811 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2812
2813 return CMD_SUCCESS;
2814 }
2815
2816 /* Clear vty access class. */
2817 DEFUN (no_vty_access_class,
2818 no_vty_access_class_cmd,
2819 "no access-class [WORD]",
2820 NO_STR
2821 "Filter connections based on an IP access list\n"
2822 "IP access list\n")
2823 {
2824 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2825 {
2826 vty_out (vty, "Access-class is not currently applied to vty%s",
2827 VTY_NEWLINE);
2828 return CMD_WARNING;
2829 }
2830
2831 XFREE(MTYPE_VTY, vty_accesslist_name);
2832
2833 vty_accesslist_name = NULL;
2834
2835 return CMD_SUCCESS;
2836 }
2837
2838 #ifdef HAVE_IPV6
2839 /* Set vty access class. */
2840 DEFUN (vty_ipv6_access_class,
2841 vty_ipv6_access_class_cmd,
2842 "ipv6 access-class WORD",
2843 IPV6_STR
2844 "Filter connections based on an IP access list\n"
2845 "IPv6 access list\n")
2846 {
2847 if (vty_ipv6_accesslist_name)
2848 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2849
2850 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2851
2852 return CMD_SUCCESS;
2853 }
2854
2855 /* Clear vty access class. */
2856 DEFUN (no_vty_ipv6_access_class,
2857 no_vty_ipv6_access_class_cmd,
2858 "no ipv6 access-class [WORD]",
2859 NO_STR
2860 IPV6_STR
2861 "Filter connections based on an IP access list\n"
2862 "IPv6 access list\n")
2863 {
2864 if (! vty_ipv6_accesslist_name ||
2865 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2866 {
2867 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2868 VTY_NEWLINE);
2869 return CMD_WARNING;
2870 }
2871
2872 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2873
2874 vty_ipv6_accesslist_name = NULL;
2875
2876 return CMD_SUCCESS;
2877 }
2878 #endif /* HAVE_IPV6 */
2879
2880 /* vty login. */
2881 DEFUN (vty_login,
2882 vty_login_cmd,
2883 "login",
2884 "Enable password checking\n")
2885 {
2886 no_password_check = 0;
2887 return CMD_SUCCESS;
2888 }
2889
2890 DEFUN (no_vty_login,
2891 no_vty_login_cmd,
2892 "no login",
2893 NO_STR
2894 "Enable password checking\n")
2895 {
2896 no_password_check = 1;
2897 return CMD_SUCCESS;
2898 }
2899
2900 DEFUN (service_advanced_vty,
2901 service_advanced_vty_cmd,
2902 "service advanced-vty",
2903 "Set up miscellaneous service\n"
2904 "Enable advanced mode vty interface\n")
2905 {
2906 host.advanced = 1;
2907 return CMD_SUCCESS;
2908 }
2909
2910 DEFUN (no_service_advanced_vty,
2911 no_service_advanced_vty_cmd,
2912 "no service advanced-vty",
2913 NO_STR
2914 "Set up miscellaneous service\n"
2915 "Enable advanced mode vty interface\n")
2916 {
2917 host.advanced = 0;
2918 return CMD_SUCCESS;
2919 }
2920
2921 DEFUN (terminal_monitor,
2922 terminal_monitor_cmd,
2923 "terminal monitor",
2924 "Set terminal line parameters\n"
2925 "Copy debug output to the current terminal line\n")
2926 {
2927 vty->monitor = 1;
2928 return CMD_SUCCESS;
2929 }
2930
2931 DEFUN (terminal_no_monitor,
2932 terminal_no_monitor_cmd,
2933 "terminal no monitor",
2934 "Set terminal line parameters\n"
2935 NO_STR
2936 "Copy debug output to the current terminal line\n")
2937 {
2938 vty->monitor = 0;
2939 return CMD_SUCCESS;
2940 }
2941
2942 ALIAS (terminal_no_monitor,
2943 no_terminal_monitor_cmd,
2944 "no terminal monitor",
2945 NO_STR
2946 "Set terminal line parameters\n"
2947 "Copy debug output to the current terminal line\n")
2948
2949 DEFUN (show_history,
2950 show_history_cmd,
2951 "show history",
2952 SHOW_STR
2953 "Display the session command history\n")
2954 {
2955 int index;
2956
2957 for (index = vty->hindex + 1; index != vty->hindex;)
2958 {
2959 if (index == VTY_MAXHIST)
2960 {
2961 index = 0;
2962 continue;
2963 }
2964
2965 if (vty->hist[index] != NULL)
2966 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2967
2968 index++;
2969 }
2970
2971 return CMD_SUCCESS;
2972 }
2973
2974 /* vty login. */
2975 DEFUN (log_commands,
2976 log_commands_cmd,
2977 "log commands",
2978 "Logging control\n"
2979 "Log all commands (can't be unset without restart)\n")
2980 {
2981 do_log_commands = 1;
2982 return CMD_SUCCESS;
2983 }
2984
2985 /* Display current configuration. */
2986 static int
2987 vty_config_write (struct vty *vty)
2988 {
2989 vty_out (vty, "line vty%s", VTY_NEWLINE);
2990
2991 if (vty_accesslist_name)
2992 vty_out (vty, " access-class %s%s",
2993 vty_accesslist_name, VTY_NEWLINE);
2994
2995 if (vty_ipv6_accesslist_name)
2996 vty_out (vty, " ipv6 access-class %s%s",
2997 vty_ipv6_accesslist_name, VTY_NEWLINE);
2998
2999 /* exec-timeout */
3000 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
3001 vty_out (vty, " exec-timeout %ld %ld%s",
3002 vty_timeout_val / 60,
3003 vty_timeout_val % 60, VTY_NEWLINE);
3004
3005 /* login */
3006 if (no_password_check)
3007 vty_out (vty, " no login%s", VTY_NEWLINE);
3008
3009 if (do_log_commands)
3010 vty_out (vty, "log commands%s", VTY_NEWLINE);
3011
3012 vty_out (vty, "!%s", VTY_NEWLINE);
3013
3014 return CMD_SUCCESS;
3015 }
3016
3017 struct cmd_node vty_node =
3018 {
3019 VTY_NODE,
3020 "%s(config-line)# ",
3021 1,
3022 };
3023
3024 /* Reset all VTY status. */
3025 void
3026 vty_reset ()
3027 {
3028 unsigned int i;
3029 struct vty *vty;
3030 struct thread *vty_serv_thread;
3031
3032 for (i = 0; i < vector_active (vtyvec); i++)
3033 if ((vty = vector_slot (vtyvec, i)) != NULL)
3034 {
3035 buffer_reset (vty->obuf);
3036 vty->status = VTY_CLOSE;
3037 vty_close (vty);
3038 }
3039
3040 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
3041 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
3042 {
3043 thread_cancel (vty_serv_thread);
3044 vector_slot (Vvty_serv_thread, i) = NULL;
3045 close (i);
3046 }
3047
3048 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3049
3050 if (vty_accesslist_name)
3051 {
3052 XFREE(MTYPE_VTY, vty_accesslist_name);
3053 vty_accesslist_name = NULL;
3054 }
3055
3056 if (vty_ipv6_accesslist_name)
3057 {
3058 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
3059 vty_ipv6_accesslist_name = NULL;
3060 }
3061 }
3062
3063 static void
3064 vty_save_cwd (void)
3065 {
3066 char cwd[MAXPATHLEN];
3067 char *c;
3068
3069 c = getcwd (cwd, MAXPATHLEN);
3070
3071 if (!c)
3072 {
3073 /*
3074 * At this point if these go wrong, more than likely
3075 * the whole world is coming down around us
3076 * Hence not worrying about it too much.
3077 */
3078 if (!chdir (SYSCONFDIR))
3079 {
3080 fprintf(stderr, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR, errno);
3081 exit(-1);
3082 }
3083 if (getcwd (cwd, MAXPATHLEN) == NULL)
3084 {
3085 fprintf(stderr, "Failure to getcwd, errno: %d\n", errno);
3086 exit(-1);
3087 }
3088 }
3089
3090 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3091 strcpy (vty_cwd, cwd);
3092 }
3093
3094 char *
3095 vty_get_cwd ()
3096 {
3097 return vty_cwd;
3098 }
3099
3100 int
3101 vty_shell (struct vty *vty)
3102 {
3103 return vty->type == VTY_SHELL ? 1 : 0;
3104 }
3105
3106 int
3107 vty_shell_serv (struct vty *vty)
3108 {
3109 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3110 }
3111
3112 void
3113 vty_init_vtysh ()
3114 {
3115 vtyvec = vector_init (VECTOR_MIN_SIZE);
3116 }
3117
3118 /* Install vty's own commands like `who' command. */
3119 void
3120 vty_init (struct thread_master *master_thread)
3121 {
3122 /* For further configuration read, preserve current directory. */
3123 vty_save_cwd ();
3124
3125 vtyvec = vector_init (VECTOR_MIN_SIZE);
3126
3127 vty_master = master_thread;
3128
3129 atexit (vty_stdio_reset);
3130
3131 /* Initilize server thread vector. */
3132 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3133
3134 /* Install bgp top node. */
3135 install_node (&vty_node, vty_config_write);
3136
3137 install_element (VIEW_NODE, &config_who_cmd);
3138 install_element (VIEW_NODE, &show_history_cmd);
3139 install_element (CONFIG_NODE, &line_vty_cmd);
3140 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3141 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3142 install_element (CONFIG_NODE, &show_history_cmd);
3143 install_element (CONFIG_NODE, &log_commands_cmd);
3144 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3145 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
3146 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
3147
3148 install_default (VTY_NODE);
3149 install_element (VTY_NODE, &exec_timeout_min_cmd);
3150 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3151 install_element (VTY_NODE, &no_exec_timeout_cmd);
3152 install_element (VTY_NODE, &vty_access_class_cmd);
3153 install_element (VTY_NODE, &no_vty_access_class_cmd);
3154 install_element (VTY_NODE, &vty_login_cmd);
3155 install_element (VTY_NODE, &no_vty_login_cmd);
3156 #ifdef HAVE_IPV6
3157 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3158 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3159 #endif /* HAVE_IPV6 */
3160 }
3161
3162 void
3163 vty_terminate (void)
3164 {
3165 if (vty_cwd)
3166 XFREE (MTYPE_TMP, vty_cwd);
3167
3168 if (vtyvec && Vvty_serv_thread)
3169 {
3170 vty_reset ();
3171 vector_free (vtyvec);
3172 vector_free (Vvty_serv_thread);
3173 }
3174 }
3175
3176 /* Utility functions to get arguments from commands generated
3177 by the xml2cli.pl script. */
3178 const char *
3179 vty_get_arg_value (struct vty_arg *args[], const char *arg)
3180 {
3181 while (*args)
3182 {
3183 if (strcmp ((*args)->name, arg) == 0)
3184 return (*args)->value;
3185 args++;
3186 }
3187 return NULL;
3188 }
3189
3190 struct vty_arg *
3191 vty_get_arg (struct vty_arg *args[], const char *arg)
3192 {
3193 while (*args)
3194 {
3195 if (strcmp ((*args)->name, arg) == 0)
3196 return *args;
3197 args++;
3198 }
3199 return NULL;
3200 }