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