]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.c
*: add frr_config_fork()
[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 (NULL, LOG_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(NULL, LOG_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 if (zlog_default)
461 protocolname = zlog_proto_names[zlog_default->protocol];
462 else
463 protocolname = zlog_proto_names[ZLOG_NONE];
464
465 #ifdef CONSUMED_TIME_CHECK
466 GETRUSAGE(&after);
467 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
468 CONSUMED_TIME_CHECK)
469 /* Warn about CPU hog that must be fixed. */
470 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
471 realtime/1000, cputime/1000, buf);
472 }
473 #endif /* CONSUMED_TIME_CHECK */
474
475 if (ret != CMD_SUCCESS)
476 switch (ret)
477 {
478 case CMD_WARNING:
479 if (vty->type == VTY_FILE)
480 vty_out (vty, "Warning...%s", VTY_NEWLINE);
481 break;
482 case CMD_ERR_AMBIGUOUS:
483 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
484 break;
485 case CMD_ERR_NO_MATCH:
486 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
487 break;
488 case CMD_ERR_INCOMPLETE:
489 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
490 break;
491 }
492 cmd_free_strvec (vline);
493
494 return ret;
495 }
496
497 static const char telnet_backward_char = 0x08;
498 static const char telnet_space_char = ' ';
499
500 /* Basic function to write buffer to vty. */
501 static void
502 vty_write (struct vty *vty, const char *buf, size_t nbytes)
503 {
504 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
505 return;
506
507 /* Should we do buffering here ? And make vty_flush (vty) ? */
508 buffer_put (vty->obuf, buf, nbytes);
509 }
510
511 /* Basic function to insert character into vty. */
512 static void
513 vty_self_insert (struct vty *vty, char c)
514 {
515 int i;
516 int length;
517
518 if (vty->length + 1 >= VTY_BUFSIZ)
519 return;
520
521 length = vty->length - vty->cp;
522 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
523 vty->buf[vty->cp] = c;
524
525 vty_write (vty, &vty->buf[vty->cp], length + 1);
526 for (i = 0; i < length; i++)
527 vty_write (vty, &telnet_backward_char, 1);
528
529 vty->cp++;
530 vty->length++;
531
532 vty->buf[vty->length] = '\0';
533 }
534
535 /* Self insert character 'c' in overwrite mode. */
536 static void
537 vty_self_insert_overwrite (struct vty *vty, char c)
538 {
539 if (vty->cp == vty->length)
540 {
541 vty_self_insert (vty, c);
542 return;
543 }
544
545 vty->buf[vty->cp++] = c;
546 vty_write (vty, &c, 1);
547 }
548
549 /**
550 * Insert a string into vty->buf at the current cursor position.
551 *
552 * If the resultant string would be larger than VTY_BUFSIZ it is
553 * truncated to fit.
554 */
555 static void
556 vty_insert_word_overwrite (struct vty *vty, char *str)
557 {
558 if (vty->cp == VTY_BUFSIZ)
559 return;
560
561 size_t nwrite = MIN ((int) strlen (str), VTY_BUFSIZ - vty->cp - 1);
562 memcpy (&vty->buf[vty->cp], str, nwrite);
563 vty->cp += nwrite;
564 vty->length = MAX (vty->cp, vty->length);
565 vty->buf[vty->length] = '\0';
566 vty_write (vty, str, nwrite);
567 }
568
569 /* Forward character. */
570 static void
571 vty_forward_char (struct vty *vty)
572 {
573 if (vty->cp < vty->length)
574 {
575 vty_write (vty, &vty->buf[vty->cp], 1);
576 vty->cp++;
577 }
578 }
579
580 /* Backward character. */
581 static void
582 vty_backward_char (struct vty *vty)
583 {
584 if (vty->cp > 0)
585 {
586 vty->cp--;
587 vty_write (vty, &telnet_backward_char, 1);
588 }
589 }
590
591 /* Move to the beginning of the line. */
592 static void
593 vty_beginning_of_line (struct vty *vty)
594 {
595 while (vty->cp)
596 vty_backward_char (vty);
597 }
598
599 /* Move to the end of the line. */
600 static void
601 vty_end_of_line (struct vty *vty)
602 {
603 while (vty->cp < vty->length)
604 vty_forward_char (vty);
605 }
606
607 static void vty_kill_line_from_beginning (struct vty *);
608 static void vty_redraw_line (struct vty *);
609
610 /* Print command line history. This function is called from
611 vty_next_line and vty_previous_line. */
612 static void
613 vty_history_print (struct vty *vty)
614 {
615 int length;
616
617 vty_kill_line_from_beginning (vty);
618
619 /* Get previous line from history buffer */
620 length = strlen (vty->hist[vty->hp]);
621 memcpy (vty->buf, vty->hist[vty->hp], length);
622 vty->cp = vty->length = length;
623 vty->buf[vty->length] = '\0';
624
625 /* Redraw current line */
626 vty_redraw_line (vty);
627 }
628
629 /* Show next command line history. */
630 static void
631 vty_next_line (struct vty *vty)
632 {
633 int try_index;
634
635 if (vty->hp == vty->hindex)
636 return;
637
638 /* Try is there history exist or not. */
639 try_index = vty->hp;
640 if (try_index == (VTY_MAXHIST - 1))
641 try_index = 0;
642 else
643 try_index++;
644
645 /* If there is not history return. */
646 if (vty->hist[try_index] == NULL)
647 return;
648 else
649 vty->hp = try_index;
650
651 vty_history_print (vty);
652 }
653
654 /* Show previous command line history. */
655 static void
656 vty_previous_line (struct vty *vty)
657 {
658 int try_index;
659
660 try_index = vty->hp;
661 if (try_index == 0)
662 try_index = VTY_MAXHIST - 1;
663 else
664 try_index--;
665
666 if (vty->hist[try_index] == NULL)
667 return;
668 else
669 vty->hp = try_index;
670
671 vty_history_print (vty);
672 }
673
674 /* This function redraw all of the command line character. */
675 static void
676 vty_redraw_line (struct vty *vty)
677 {
678 vty_write (vty, vty->buf, vty->length);
679 vty->cp = vty->length;
680 }
681
682 /* Forward word. */
683 static void
684 vty_forward_word (struct vty *vty)
685 {
686 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
687 vty_forward_char (vty);
688
689 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
690 vty_forward_char (vty);
691 }
692
693 /* Backward word without skipping training space. */
694 static void
695 vty_backward_pure_word (struct vty *vty)
696 {
697 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
698 vty_backward_char (vty);
699 }
700
701 /* Backward word. */
702 static void
703 vty_backward_word (struct vty *vty)
704 {
705 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
706 vty_backward_char (vty);
707
708 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
709 vty_backward_char (vty);
710 }
711
712 /* When '^D' is typed at the beginning of the line we move to the down
713 level. */
714 static void
715 vty_down_level (struct vty *vty)
716 {
717 vty_out (vty, "%s", VTY_NEWLINE);
718 cmd_exit (vty);
719 vty_prompt (vty);
720 vty->cp = 0;
721 }
722
723 /* When '^Z' is received from vty, move down to the enable mode. */
724 static void
725 vty_end_config (struct vty *vty)
726 {
727 vty_out (vty, "%s", VTY_NEWLINE);
728
729 switch (vty->node)
730 {
731 case VIEW_NODE:
732 case ENABLE_NODE:
733 /* Nothing to do. */
734 break;
735 case CONFIG_NODE:
736 case INTERFACE_NODE:
737 case ZEBRA_NODE:
738 case RIP_NODE:
739 case RIPNG_NODE:
740 case BGP_NODE:
741 case BGP_VPNV4_NODE:
742 case BGP_VPNV6_NODE:
743 case BGP_ENCAP_NODE:
744 case BGP_ENCAPV6_NODE:
745 case BGP_VRF_POLICY_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 BGP_EVPN_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 BGP_NODE:
1168 case RMAP_NODE:
1169 case OSPF_NODE:
1170 case OSPF6_NODE:
1171 case LDP_NODE:
1172 case LDP_IPV4_NODE:
1173 case LDP_IPV6_NODE:
1174 case LDP_IPV4_IFACE_NODE:
1175 case LDP_IPV6_IFACE_NODE:
1176 case LDP_L2VPN_NODE:
1177 case LDP_PSEUDOWIRE_NODE:
1178 case ISIS_NODE:
1179 case KEYCHAIN_NODE:
1180 case KEYCHAIN_KEY_NODE:
1181 case MASC_NODE:
1182 case PIM_NODE:
1183 case VTY_NODE:
1184 vty_config_unlock (vty);
1185 vty->node = ENABLE_NODE;
1186 break;
1187 default:
1188 /* Unknown node, we have to ignore it. */
1189 break;
1190 }
1191 vty_prompt (vty);
1192
1193 /* Set history pointer to the latest one. */
1194 vty->hp = vty->hindex;
1195 }
1196
1197 /* Add current command line to the history buffer. */
1198 static void
1199 vty_hist_add (struct vty *vty)
1200 {
1201 int index;
1202
1203 if (vty->length == 0)
1204 return;
1205
1206 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1207
1208 /* Ignore the same string as previous one. */
1209 if (vty->hist[index])
1210 if (strcmp (vty->buf, vty->hist[index]) == 0)
1211 {
1212 vty->hp = vty->hindex;
1213 return;
1214 }
1215
1216 /* Insert history entry. */
1217 if (vty->hist[vty->hindex])
1218 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1219 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1220
1221 /* History index rotation. */
1222 vty->hindex++;
1223 if (vty->hindex == VTY_MAXHIST)
1224 vty->hindex = 0;
1225
1226 vty->hp = vty->hindex;
1227 }
1228
1229 /* #define TELNET_OPTION_DEBUG */
1230
1231 /* Get telnet window size. */
1232 static int
1233 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1234 {
1235 #ifdef TELNET_OPTION_DEBUG
1236 int i;
1237
1238 for (i = 0; i < nbytes; i++)
1239 {
1240 switch (buf[i])
1241 {
1242 case IAC:
1243 vty_out (vty, "IAC ");
1244 break;
1245 case WILL:
1246 vty_out (vty, "WILL ");
1247 break;
1248 case WONT:
1249 vty_out (vty, "WONT ");
1250 break;
1251 case DO:
1252 vty_out (vty, "DO ");
1253 break;
1254 case DONT:
1255 vty_out (vty, "DONT ");
1256 break;
1257 case SB:
1258 vty_out (vty, "SB ");
1259 break;
1260 case SE:
1261 vty_out (vty, "SE ");
1262 break;
1263 case TELOPT_ECHO:
1264 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1265 break;
1266 case TELOPT_SGA:
1267 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1268 break;
1269 case TELOPT_NAWS:
1270 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1271 break;
1272 default:
1273 vty_out (vty, "%x ", buf[i]);
1274 break;
1275 }
1276 }
1277 vty_out (vty, "%s", VTY_NEWLINE);
1278
1279 #endif /* TELNET_OPTION_DEBUG */
1280
1281 switch (buf[0])
1282 {
1283 case SB:
1284 vty->sb_len = 0;
1285 vty->iac_sb_in_progress = 1;
1286 return 0;
1287 break;
1288 case SE:
1289 {
1290 if (!vty->iac_sb_in_progress)
1291 return 0;
1292
1293 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
1294 {
1295 vty->iac_sb_in_progress = 0;
1296 return 0;
1297 }
1298 switch (vty->sb_buf[0])
1299 {
1300 case TELOPT_NAWS:
1301 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1302 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1303 "should send %d characters, but we received %lu",
1304 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1305 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1306 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1307 "too small to handle the telnet NAWS option",
1308 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1309 else
1310 {
1311 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1312 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1313 #ifdef TELNET_OPTION_DEBUG
1314 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1315 "width %d, height %d%s",
1316 vty->width, vty->height, VTY_NEWLINE);
1317 #endif
1318 }
1319 break;
1320 }
1321 vty->iac_sb_in_progress = 0;
1322 return 0;
1323 break;
1324 }
1325 default:
1326 break;
1327 }
1328 return 1;
1329 }
1330
1331 /* Execute current command line. */
1332 static int
1333 vty_execute (struct vty *vty)
1334 {
1335 int ret;
1336
1337 ret = CMD_SUCCESS;
1338
1339 switch (vty->node)
1340 {
1341 case AUTH_NODE:
1342 case AUTH_ENABLE_NODE:
1343 vty_auth (vty, vty->buf);
1344 break;
1345 default:
1346 ret = vty_command (vty, vty->buf);
1347 if (vty->type == VTY_TERM)
1348 vty_hist_add (vty);
1349 break;
1350 }
1351
1352 /* Clear command line buffer. */
1353 vty->cp = vty->length = 0;
1354 vty_clear_buf (vty);
1355
1356 if (vty->status != VTY_CLOSE )
1357 vty_prompt (vty);
1358
1359 return ret;
1360 }
1361
1362 #define CONTROL(X) ((X) - '@')
1363 #define VTY_NORMAL 0
1364 #define VTY_PRE_ESCAPE 1
1365 #define VTY_ESCAPE 2
1366
1367 /* Escape character command map. */
1368 static void
1369 vty_escape_map (unsigned char c, struct vty *vty)
1370 {
1371 switch (c)
1372 {
1373 case ('A'):
1374 vty_previous_line (vty);
1375 break;
1376 case ('B'):
1377 vty_next_line (vty);
1378 break;
1379 case ('C'):
1380 vty_forward_char (vty);
1381 break;
1382 case ('D'):
1383 vty_backward_char (vty);
1384 break;
1385 default:
1386 break;
1387 }
1388
1389 /* Go back to normal mode. */
1390 vty->escape = VTY_NORMAL;
1391 }
1392
1393 /* Quit print out to the buffer. */
1394 static void
1395 vty_buffer_reset (struct vty *vty)
1396 {
1397 buffer_reset (vty->obuf);
1398 vty_prompt (vty);
1399 vty_redraw_line (vty);
1400 }
1401
1402 /* Read data via vty socket. */
1403 static int
1404 vty_read (struct thread *thread)
1405 {
1406 int i;
1407 int nbytes;
1408 unsigned char buf[VTY_READ_BUFSIZ];
1409
1410 int vty_sock = THREAD_FD (thread);
1411 struct vty *vty = THREAD_ARG (thread);
1412 vty->t_read = NULL;
1413
1414 /* Read raw data from socket */
1415 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1416 {
1417 if (nbytes < 0)
1418 {
1419 if (ERRNO_IO_RETRY(errno))
1420 {
1421 vty_event (VTY_READ, vty_sock, vty);
1422 return 0;
1423 }
1424 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1425 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1426 __func__, vty->fd, safe_strerror(errno));
1427 buffer_reset(vty->obuf);
1428 }
1429 vty->status = VTY_CLOSE;
1430 }
1431
1432 for (i = 0; i < nbytes; i++)
1433 {
1434 if (buf[i] == IAC)
1435 {
1436 if (!vty->iac)
1437 {
1438 vty->iac = 1;
1439 continue;
1440 }
1441 else
1442 {
1443 vty->iac = 0;
1444 }
1445 }
1446
1447 if (vty->iac_sb_in_progress && !vty->iac)
1448 {
1449 if (vty->sb_len < sizeof(vty->sb_buf))
1450 vty->sb_buf[vty->sb_len] = buf[i];
1451 vty->sb_len++;
1452 continue;
1453 }
1454
1455 if (vty->iac)
1456 {
1457 /* In case of telnet command */
1458 int ret = 0;
1459 ret = vty_telnet_option (vty, buf + i, nbytes - i);
1460 vty->iac = 0;
1461 i += ret;
1462 continue;
1463 }
1464
1465
1466 if (vty->status == VTY_MORE)
1467 {
1468 switch (buf[i])
1469 {
1470 case CONTROL('C'):
1471 case 'q':
1472 case 'Q':
1473 vty_buffer_reset (vty);
1474 break;
1475 #if 0 /* More line does not work for "show ip bgp". */
1476 case '\n':
1477 case '\r':
1478 vty->status = VTY_MORELINE;
1479 break;
1480 #endif
1481 default:
1482 break;
1483 }
1484 continue;
1485 }
1486
1487 /* Escape character. */
1488 if (vty->escape == VTY_ESCAPE)
1489 {
1490 vty_escape_map (buf[i], vty);
1491 continue;
1492 }
1493
1494 /* Pre-escape status. */
1495 if (vty->escape == VTY_PRE_ESCAPE)
1496 {
1497 switch (buf[i])
1498 {
1499 case '[':
1500 vty->escape = VTY_ESCAPE;
1501 break;
1502 case 'b':
1503 vty_backward_word (vty);
1504 vty->escape = VTY_NORMAL;
1505 break;
1506 case 'f':
1507 vty_forward_word (vty);
1508 vty->escape = VTY_NORMAL;
1509 break;
1510 case 'd':
1511 vty_forward_kill_word (vty);
1512 vty->escape = VTY_NORMAL;
1513 break;
1514 case CONTROL('H'):
1515 case 0x7f:
1516 vty_backward_kill_word (vty);
1517 vty->escape = VTY_NORMAL;
1518 break;
1519 default:
1520 vty->escape = VTY_NORMAL;
1521 break;
1522 }
1523 continue;
1524 }
1525
1526 switch (buf[i])
1527 {
1528 case CONTROL('A'):
1529 vty_beginning_of_line (vty);
1530 break;
1531 case CONTROL('B'):
1532 vty_backward_char (vty);
1533 break;
1534 case CONTROL('C'):
1535 vty_stop_input (vty);
1536 break;
1537 case CONTROL('D'):
1538 vty_delete_char (vty);
1539 break;
1540 case CONTROL('E'):
1541 vty_end_of_line (vty);
1542 break;
1543 case CONTROL('F'):
1544 vty_forward_char (vty);
1545 break;
1546 case CONTROL('H'):
1547 case 0x7f:
1548 vty_delete_backward_char (vty);
1549 break;
1550 case CONTROL('K'):
1551 vty_kill_line (vty);
1552 break;
1553 case CONTROL('N'):
1554 vty_next_line (vty);
1555 break;
1556 case CONTROL('P'):
1557 vty_previous_line (vty);
1558 break;
1559 case CONTROL('T'):
1560 vty_transpose_chars (vty);
1561 break;
1562 case CONTROL('U'):
1563 vty_kill_line_from_beginning (vty);
1564 break;
1565 case CONTROL('W'):
1566 vty_backward_kill_word (vty);
1567 break;
1568 case CONTROL('Z'):
1569 vty_end_config (vty);
1570 break;
1571 case '\n':
1572 case '\r':
1573 vty_out (vty, "%s", VTY_NEWLINE);
1574 vty_execute (vty);
1575 break;
1576 case '\t':
1577 vty_complete_command (vty);
1578 break;
1579 case '?':
1580 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1581 vty_self_insert (vty, buf[i]);
1582 else
1583 vty_describe_command (vty);
1584 break;
1585 case '\033':
1586 if (i + 1 < nbytes && buf[i + 1] == '[')
1587 {
1588 vty->escape = VTY_ESCAPE;
1589 i++;
1590 }
1591 else
1592 vty->escape = VTY_PRE_ESCAPE;
1593 break;
1594 default:
1595 if (buf[i] > 31 && buf[i] < 127)
1596 vty_self_insert (vty, buf[i]);
1597 break;
1598 }
1599 }
1600
1601 /* Check status. */
1602 if (vty->status == VTY_CLOSE)
1603 vty_close (vty);
1604 else
1605 {
1606 vty_event (VTY_WRITE, vty->wfd, vty);
1607 vty_event (VTY_READ, vty_sock, vty);
1608 }
1609 return 0;
1610 }
1611
1612 /* Flush buffer to the vty. */
1613 static int
1614 vty_flush (struct thread *thread)
1615 {
1616 int erase;
1617 buffer_status_t flushrc;
1618 int vty_sock = THREAD_FD (thread);
1619 struct vty *vty = THREAD_ARG (thread);
1620
1621 vty->t_write = NULL;
1622
1623 /* Tempolary disable read thread. */
1624 if ((vty->lines == 0) && vty->t_read)
1625 {
1626 thread_cancel (vty->t_read);
1627 vty->t_read = NULL;
1628 }
1629
1630 /* Function execution continue. */
1631 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1632
1633 /* N.B. if width is 0, that means we don't know the window size. */
1634 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
1635 flushrc = buffer_flush_available(vty->obuf, vty_sock);
1636 else if (vty->status == VTY_MORELINE)
1637 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1638 1, erase, 0);
1639 else
1640 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1641 vty->lines >= 0 ? vty->lines :
1642 vty->height,
1643 erase, 0);
1644 switch (flushrc)
1645 {
1646 case BUFFER_ERROR:
1647 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1648 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1649 vty->fd);
1650 buffer_reset(vty->obuf);
1651 vty_close(vty);
1652 return 0;
1653 case BUFFER_EMPTY:
1654 if (vty->status == VTY_CLOSE)
1655 vty_close (vty);
1656 else
1657 {
1658 vty->status = VTY_NORMAL;
1659 if (vty->lines == 0)
1660 vty_event (VTY_READ, vty_sock, vty);
1661 }
1662 break;
1663 case BUFFER_PENDING:
1664 /* There is more data waiting to be written. */
1665 vty->status = VTY_MORE;
1666 if (vty->lines == 0)
1667 vty_event (VTY_WRITE, vty_sock, vty);
1668 break;
1669 }
1670
1671 return 0;
1672 }
1673
1674 /* allocate and initialise vty */
1675 static struct vty *
1676 vty_new_init (int vty_sock)
1677 {
1678 struct vty *vty;
1679
1680 vty = vty_new ();
1681 vty->fd = vty_sock;
1682 vty->wfd = vty_sock;
1683 vty->type = VTY_TERM;
1684 vty->node = AUTH_NODE;
1685 vty->fail = 0;
1686 vty->cp = 0;
1687 vty_clear_buf (vty);
1688 vty->length = 0;
1689 memset (vty->hist, 0, sizeof (vty->hist));
1690 vty->hp = 0;
1691 vty->hindex = 0;
1692 vector_set_index (vtyvec, vty_sock, vty);
1693 vty->status = VTY_NORMAL;
1694 vty->lines = -1;
1695 vty->iac = 0;
1696 vty->iac_sb_in_progress = 0;
1697 vty->sb_len = 0;
1698
1699 return vty;
1700 }
1701
1702 /* Create new vty structure. */
1703 static struct vty *
1704 vty_create (int vty_sock, union sockunion *su)
1705 {
1706 char buf[SU_ADDRSTRLEN];
1707 struct vty *vty;
1708
1709 sockunion2str(su, buf, SU_ADDRSTRLEN);
1710
1711 /* Allocate new vty structure and set up default values. */
1712 vty = vty_new_init (vty_sock);
1713
1714 /* configurable parameters not part of basic init */
1715 vty->v_timeout = vty_timeout_val;
1716 strcpy (vty->address, buf);
1717 if (no_password_check)
1718 {
1719 if (host.advanced)
1720 vty->node = ENABLE_NODE;
1721 else
1722 vty->node = VIEW_NODE;
1723 }
1724 if (host.lines >= 0)
1725 vty->lines = host.lines;
1726
1727 if (! no_password_check)
1728 {
1729 /* Vty is not available if password isn't set. */
1730 if (host.password == NULL && host.password_encrypt == NULL)
1731 {
1732 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1733 vty->status = VTY_CLOSE;
1734 vty_close (vty);
1735 return NULL;
1736 }
1737 }
1738
1739 /* Say hello to the world. */
1740 vty_hello (vty);
1741 if (! no_password_check)
1742 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1743
1744 /* Setting up terminal. */
1745 vty_will_echo (vty);
1746 vty_will_suppress_go_ahead (vty);
1747
1748 vty_dont_linemode (vty);
1749 vty_do_window_size (vty);
1750 /* vty_dont_lflow_ahead (vty); */
1751
1752 vty_prompt (vty);
1753
1754 /* Add read/write thread. */
1755 vty_event (VTY_WRITE, vty_sock, vty);
1756 vty_event (VTY_READ, vty_sock, vty);
1757
1758 return vty;
1759 }
1760
1761 /* create vty for stdio */
1762 static struct termios stdio_orig_termios;
1763 static struct vty *stdio_vty = NULL;
1764 static void (*stdio_vty_atclose)(void);
1765
1766 static void
1767 vty_stdio_reset (void)
1768 {
1769 if (stdio_vty)
1770 {
1771 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1772 stdio_vty = NULL;
1773
1774 if (stdio_vty_atclose)
1775 stdio_vty_atclose ();
1776 stdio_vty_atclose = NULL;
1777 }
1778 }
1779
1780 struct vty *
1781 vty_stdio (void (*atclose)())
1782 {
1783 struct vty *vty;
1784 struct termios termios;
1785
1786 /* refuse creating two vtys on stdio */
1787 if (stdio_vty)
1788 return NULL;
1789
1790 vty = stdio_vty = vty_new_init (0);
1791 stdio_vty_atclose = atclose;
1792 vty->wfd = 1;
1793
1794 /* always have stdio vty in a known _unchangeable_ state, don't want config
1795 * to have any effect here to make sure scripting this works as intended */
1796 vty->node = ENABLE_NODE;
1797 vty->v_timeout = 0;
1798 strcpy (vty->address, "console");
1799
1800 if (!tcgetattr (0, &stdio_orig_termios))
1801 {
1802 termios = stdio_orig_termios;
1803 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1804 | INLCR | IGNCR | ICRNL | IXON);
1805 termios.c_oflag &= ~OPOST;
1806 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1807 termios.c_cflag &= ~(CSIZE | PARENB);
1808 termios.c_cflag |= CS8;
1809 tcsetattr (0, TCSANOW, &termios);
1810 }
1811
1812 vty_prompt (vty);
1813
1814 /* Add read/write thread. */
1815 vty_event (VTY_WRITE, 1, vty);
1816 vty_event (VTY_READ, 0, vty);
1817
1818 return vty;
1819 }
1820
1821 /* Accept connection from the network. */
1822 static int
1823 vty_accept (struct thread *thread)
1824 {
1825 int vty_sock;
1826 union sockunion su;
1827 int ret;
1828 unsigned int on;
1829 int accept_sock;
1830 struct prefix p;
1831 struct access_list *acl = NULL;
1832 char buf[SU_ADDRSTRLEN];
1833
1834 accept_sock = THREAD_FD (thread);
1835
1836 /* We continue hearing vty socket. */
1837 vty_event (VTY_SERV, accept_sock, NULL);
1838
1839 memset (&su, 0, sizeof (union sockunion));
1840
1841 /* We can handle IPv4 or IPv6 socket. */
1842 vty_sock = sockunion_accept (accept_sock, &su);
1843 if (vty_sock < 0)
1844 {
1845 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1846 return -1;
1847 }
1848 set_nonblocking(vty_sock);
1849 set_cloexec(vty_sock);
1850
1851 sockunion2hostprefix (&su, &p);
1852
1853 /* VTY's accesslist apply. */
1854 if (p.family == AF_INET && vty_accesslist_name)
1855 {
1856 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1857 (access_list_apply (acl, &p) == FILTER_DENY))
1858 {
1859 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1860 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1861 close (vty_sock);
1862
1863 /* continue accepting connections */
1864 vty_event (VTY_SERV, accept_sock, NULL);
1865
1866 return 0;
1867 }
1868 }
1869
1870 /* VTY's ipv6 accesslist apply. */
1871 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
1872 {
1873 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1874 (access_list_apply (acl, &p) == FILTER_DENY))
1875 {
1876 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1877 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1878 close (vty_sock);
1879
1880 /* continue accepting connections */
1881 vty_event (VTY_SERV, accept_sock, NULL);
1882
1883 return 0;
1884 }
1885 }
1886
1887 on = 1;
1888 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1889 (char *) &on, sizeof (on));
1890 if (ret < 0)
1891 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
1892 safe_strerror (errno));
1893
1894 zlog (NULL, LOG_INFO, "Vty connection from %s",
1895 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1896
1897 vty_create (vty_sock, &su);
1898
1899 return 0;
1900 }
1901
1902 static void
1903 vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1904 {
1905 int ret;
1906 struct addrinfo req;
1907 struct addrinfo *ainfo;
1908 struct addrinfo *ainfo_save;
1909 int sock;
1910 char port_str[BUFSIZ];
1911
1912 memset (&req, 0, sizeof (struct addrinfo));
1913 req.ai_flags = AI_PASSIVE;
1914 req.ai_family = AF_UNSPEC;
1915 req.ai_socktype = SOCK_STREAM;
1916 sprintf (port_str, "%d", port);
1917 port_str[sizeof (port_str) - 1] = '\0';
1918
1919 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1920
1921 if (ret != 0)
1922 {
1923 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1924 exit (1);
1925 }
1926
1927 ainfo_save = ainfo;
1928
1929 do
1930 {
1931 if (ainfo->ai_family != AF_INET
1932 && ainfo->ai_family != AF_INET6
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
1966 #ifdef VTYSH
1967 /* For sockaddr_un. */
1968 #include <sys/un.h>
1969
1970 /* VTY shell UNIX domain socket. */
1971 static void
1972 vty_serv_un (const char *path)
1973 {
1974 int ret;
1975 int sock, len;
1976 struct sockaddr_un serv;
1977 mode_t old_mask;
1978 struct zprivs_ids_t ids;
1979
1980 /* First of all, unlink existing socket */
1981 unlink (path);
1982
1983 /* Set umask */
1984 old_mask = umask (0007);
1985
1986 /* Make UNIX domain socket. */
1987 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1988 if (sock < 0)
1989 {
1990 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
1991 return;
1992 }
1993
1994 /* Make server socket. */
1995 memset (&serv, 0, sizeof (struct sockaddr_un));
1996 serv.sun_family = AF_UNIX;
1997 strlcpy (serv.sun_path, path, sizeof (serv.sun_path));
1998 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1999 len = serv.sun_len = SUN_LEN(&serv);
2000 #else
2001 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
2002 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2003
2004 set_cloexec (sock);
2005
2006 ret = bind (sock, (struct sockaddr *) &serv, len);
2007 if (ret < 0)
2008 {
2009 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
2010 close (sock); /* Avoid sd leak. */
2011 return;
2012 }
2013
2014 ret = listen (sock, 5);
2015 if (ret < 0)
2016 {
2017 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
2018 close (sock); /* Avoid sd leak. */
2019 return;
2020 }
2021
2022 umask (old_mask);
2023
2024 zprivs_get_ids(&ids);
2025
2026 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2027 earlier for the case when we don't need to chown the file
2028 type casting it here to make a compare */
2029 if ((int)ids.gid_vty > 0)
2030 {
2031 /* set group of socket */
2032 if ( chown (path, -1, ids.gid_vty) )
2033 {
2034 zlog_err ("vty_serv_un: could chown socket, %s",
2035 safe_strerror (errno) );
2036 }
2037 }
2038
2039 vty_event (VTYSH_SERV, sock, NULL);
2040 }
2041
2042 /* #define VTYSH_DEBUG 1 */
2043
2044 static int
2045 vtysh_accept (struct thread *thread)
2046 {
2047 int accept_sock;
2048 int sock;
2049 int client_len;
2050 struct sockaddr_un client;
2051 struct vty *vty;
2052
2053 accept_sock = THREAD_FD (thread);
2054
2055 vty_event (VTYSH_SERV, accept_sock, NULL);
2056
2057 memset (&client, 0, sizeof (struct sockaddr_un));
2058 client_len = sizeof (struct sockaddr_un);
2059
2060 sock = accept (accept_sock, (struct sockaddr *) &client,
2061 (socklen_t *) &client_len);
2062
2063 if (sock < 0)
2064 {
2065 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
2066 return -1;
2067 }
2068
2069 if (set_nonblocking(sock) < 0)
2070 {
2071 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2072 " %s, closing", sock, safe_strerror (errno));
2073 close (sock);
2074 return -1;
2075 }
2076 set_cloexec(sock);
2077
2078 #ifdef VTYSH_DEBUG
2079 printf ("VTY shell accept\n");
2080 #endif /* VTYSH_DEBUG */
2081
2082 vty = vty_new ();
2083 vty->fd = sock;
2084 vty->wfd = sock;
2085 vty->type = VTY_SHELL_SERV;
2086 vty->node = VIEW_NODE;
2087
2088 vty_event (VTYSH_READ, sock, vty);
2089
2090 return 0;
2091 }
2092
2093 static int
2094 vtysh_flush(struct vty *vty)
2095 {
2096 switch (buffer_flush_available(vty->obuf, vty->wfd))
2097 {
2098 case BUFFER_PENDING:
2099 vty_event(VTYSH_WRITE, vty->wfd, vty);
2100 break;
2101 case BUFFER_ERROR:
2102 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2103 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2104 buffer_reset(vty->obuf);
2105 vty_close(vty);
2106 return -1;
2107 break;
2108 case BUFFER_EMPTY:
2109 break;
2110 }
2111 return 0;
2112 }
2113
2114 static int
2115 vtysh_read (struct thread *thread)
2116 {
2117 int ret;
2118 int sock;
2119 int nbytes;
2120 struct vty *vty;
2121 unsigned char buf[VTY_READ_BUFSIZ];
2122 unsigned char *p;
2123 u_char header[4] = {0, 0, 0, 0};
2124
2125 sock = THREAD_FD (thread);
2126 vty = THREAD_ARG (thread);
2127 vty->t_read = NULL;
2128
2129 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
2130 {
2131 if (nbytes < 0)
2132 {
2133 if (ERRNO_IO_RETRY(errno))
2134 {
2135 vty_event (VTYSH_READ, sock, vty);
2136 return 0;
2137 }
2138 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2139 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2140 __func__, sock, safe_strerror(errno));
2141 }
2142 buffer_reset(vty->obuf);
2143 vty_close (vty);
2144 #ifdef VTYSH_DEBUG
2145 printf ("close vtysh\n");
2146 #endif /* VTYSH_DEBUG */
2147 return 0;
2148 }
2149
2150 #ifdef VTYSH_DEBUG
2151 printf ("line: %.*s\n", nbytes, buf);
2152 #endif /* VTYSH_DEBUG */
2153
2154 if (vty->length + nbytes >= VTY_BUFSIZ)
2155 {
2156 /* Clear command line buffer. */
2157 vty->cp = vty->length = 0;
2158 vty_clear_buf (vty);
2159 vty_out (vty, "%% Command is too long.%s", VTY_NEWLINE);
2160 }
2161 else
2162 {
2163 for (p = buf; p < buf+nbytes; p++)
2164 {
2165 vty->buf[vty->length++] = *p;
2166 if (*p == '\0')
2167 {
2168 /* Pass this line to parser. */
2169 ret = vty_execute (vty);
2170 /* Note that vty_execute clears the command buffer and resets
2171 vty->length to 0. */
2172
2173 /* Return result. */
2174 #ifdef VTYSH_DEBUG
2175 printf ("result: %d\n", ret);
2176 printf ("vtysh node: %d\n", vty->node);
2177 #endif /* VTYSH_DEBUG */
2178
2179 /* hack for asynchronous "write integrated"
2180 * - other commands in "buf" will be ditched
2181 * - input during pending config-write is "unsupported" */
2182 if (ret == CMD_SUSPEND)
2183 break;
2184
2185 /* warning: watchquagga hardcodes this result write */
2186 header[3] = ret;
2187 buffer_put(vty->obuf, header, 4);
2188
2189 if (!vty->t_write && (vtysh_flush(vty) < 0))
2190 /* Try to flush results; exit if a write error occurs. */
2191 return 0;
2192 }
2193 }
2194 }
2195
2196 vty_event (VTYSH_READ, sock, vty);
2197
2198 return 0;
2199 }
2200
2201 static int
2202 vtysh_write (struct thread *thread)
2203 {
2204 struct vty *vty = THREAD_ARG (thread);
2205
2206 vty->t_write = NULL;
2207 vtysh_flush(vty);
2208 return 0;
2209 }
2210
2211 #endif /* VTYSH */
2212
2213 /* Determine address family to bind. */
2214 void
2215 vty_serv_sock (const char *addr, unsigned short port, const char *path)
2216 {
2217 /* If port is set to 0, do not listen on TCP/IP at all! */
2218 if (port)
2219 vty_serv_sock_addrinfo (addr, port);
2220
2221 #ifdef VTYSH
2222 vty_serv_un (path);
2223 #endif /* VTYSH */
2224 }
2225
2226 /* Close vty interface. Warning: call this only from functions that
2227 will be careful not to access the vty afterwards (since it has
2228 now been freed). This is safest from top-level functions (called
2229 directly by the thread dispatcher). */
2230 void
2231 vty_close (struct vty *vty)
2232 {
2233 int i;
2234 bool was_stdio = false;
2235
2236 /* Cancel threads.*/
2237 if (vty->t_read)
2238 thread_cancel (vty->t_read);
2239 if (vty->t_write)
2240 thread_cancel (vty->t_write);
2241 if (vty->t_timeout)
2242 thread_cancel (vty->t_timeout);
2243
2244 /* Flush buffer. */
2245 buffer_flush_all (vty->obuf, vty->wfd);
2246
2247 /* Free input buffer. */
2248 buffer_free (vty->obuf);
2249
2250 /* Free command history. */
2251 for (i = 0; i < VTY_MAXHIST; i++)
2252 if (vty->hist[i])
2253 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2254
2255 /* Unset vector. */
2256 vector_unset (vtyvec, vty->fd);
2257
2258 if (vty->wfd > 0 && vty->type == VTY_FILE)
2259 fsync (vty->wfd);
2260
2261 /* Close socket. */
2262 if (vty->fd > 0)
2263 {
2264 close (vty->fd);
2265 if (vty->wfd > 0 && vty->wfd != vty->fd)
2266 close (vty->wfd);
2267 }
2268 else
2269 was_stdio = true;
2270
2271 if (vty->buf)
2272 XFREE (MTYPE_VTY, vty->buf);
2273
2274 if (vty->error_buf)
2275 XFREE (MTYPE_VTY, vty->error_buf);
2276
2277 /* Check configure. */
2278 vty_config_unlock (vty);
2279
2280 /* OK free vty. */
2281 XFREE (MTYPE_VTY, vty);
2282
2283 if (was_stdio)
2284 vty_stdio_reset ();
2285 }
2286
2287 /* When time out occur output message then close connection. */
2288 static int
2289 vty_timeout (struct thread *thread)
2290 {
2291 struct vty *vty;
2292
2293 vty = THREAD_ARG (thread);
2294 vty->t_timeout = NULL;
2295 vty->v_timeout = 0;
2296
2297 /* Clear buffer*/
2298 buffer_reset (vty->obuf);
2299 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2300
2301 /* Close connection. */
2302 vty->status = VTY_CLOSE;
2303 vty_close (vty);
2304
2305 return 0;
2306 }
2307
2308 /* Read up configuration file from file_name. */
2309 static void
2310 vty_read_file (FILE *confp)
2311 {
2312 int ret;
2313 struct vty *vty;
2314 unsigned int line_num = 0;
2315
2316 vty = vty_new ();
2317 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2318 if (vty->wfd < 0)
2319 {
2320 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2321 vty->wfd = STDOUT_FILENO;
2322 }
2323 vty->fd = STDIN_FILENO;
2324 vty->type = VTY_FILE;
2325 vty->node = CONFIG_NODE;
2326
2327 /* Execute configuration file */
2328 ret = config_from_file (vty, confp, &line_num);
2329
2330 /* Flush any previous errors before printing messages below */
2331 buffer_flush_all (vty->obuf, vty->fd);
2332
2333 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
2334 {
2335 const char *message = NULL;
2336 switch (ret)
2337 {
2338 case CMD_ERR_AMBIGUOUS:
2339 message = "*** Error reading config: Ambiguous command.";
2340 break;
2341 case CMD_ERR_NO_MATCH:
2342 message = "*** Error reading config: There is no such command.";
2343 break;
2344 }
2345 fprintf (stderr, "%s\n", message);
2346 zlog_err ("%s", message);
2347 fprintf (stderr, "*** Error occurred processing line %u, below:\n%s\n",
2348 line_num, vty->error_buf);
2349 zlog_err ("*** Error occurred processing line %u, below:\n%s",
2350 line_num, vty->error_buf);
2351 }
2352
2353 vty_close (vty);
2354 }
2355
2356 static FILE *
2357 vty_use_backup_config (const char *fullpath)
2358 {
2359 char *fullpath_sav, *fullpath_tmp;
2360 FILE *ret = NULL;
2361 int tmp, sav;
2362 int c;
2363 char buffer[512];
2364
2365 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2366 strcpy (fullpath_sav, fullpath);
2367 strcat (fullpath_sav, CONF_BACKUP_EXT);
2368
2369 sav = open (fullpath_sav, O_RDONLY);
2370 if (sav < 0)
2371 {
2372 free (fullpath_sav);
2373 return NULL;
2374 }
2375
2376 fullpath_tmp = malloc (strlen (fullpath) + 8);
2377 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2378
2379 /* Open file to configuration write. */
2380 tmp = mkstemp (fullpath_tmp);
2381 if (tmp < 0)
2382 goto out_close_sav;
2383
2384 if (fchmod (tmp, CONFIGFILE_MASK) != 0)
2385 goto out_close;
2386
2387 while((c = read (sav, buffer, 512)) > 0)
2388 {
2389 if (write (tmp, buffer, c) <= 0)
2390 goto out_close;
2391 }
2392 close (sav);
2393 close (tmp);
2394
2395 if (rename (fullpath_tmp, fullpath) == 0)
2396 ret = fopen (fullpath, "r");
2397 else
2398 unlink (fullpath_tmp);
2399
2400 if (0)
2401 {
2402 out_close:
2403 close (tmp);
2404 unlink (fullpath_tmp);
2405 out_close_sav:
2406 close (sav);
2407 }
2408
2409 free (fullpath_sav);
2410 free (fullpath_tmp);
2411 return ret;
2412 }
2413
2414 /* Read up configuration file from file_name. */
2415 void
2416 vty_read_config (const char *config_file,
2417 char *config_default_dir)
2418 {
2419 char cwd[MAXPATHLEN];
2420 FILE *confp = NULL;
2421 const char *fullpath;
2422 char *tmp = NULL;
2423
2424 /* If -f flag specified. */
2425 if (config_file != NULL)
2426 {
2427 if (! IS_DIRECTORY_SEP (config_file[0]))
2428 {
2429 if (getcwd (cwd, MAXPATHLEN) == NULL)
2430 {
2431 fprintf (stderr, "Failure to determine Current Working Directory %d!\n", errno);
2432 exit (1);
2433 }
2434 tmp = XMALLOC (MTYPE_TMP,
2435 strlen (cwd) + strlen (config_file) + 2);
2436 sprintf (tmp, "%s/%s", cwd, config_file);
2437 fullpath = tmp;
2438 }
2439 else
2440 fullpath = config_file;
2441
2442 confp = fopen (fullpath, "r");
2443
2444 if (confp == NULL)
2445 {
2446 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2447 __func__, fullpath, safe_strerror (errno));
2448
2449 confp = vty_use_backup_config (fullpath);
2450 if (confp)
2451 fprintf (stderr, "WARNING: using backup configuration file!\n");
2452 else
2453 {
2454 fprintf (stderr, "can't open configuration file [%s]\n",
2455 config_file);
2456 exit(1);
2457 }
2458 }
2459 }
2460 else
2461 {
2462
2463 host_config_set (config_default_dir);
2464
2465 #ifdef VTYSH
2466 int ret;
2467 struct stat conf_stat;
2468
2469 /* !!!!PLEASE LEAVE!!!!
2470 * This is NEEDED for use with vtysh -b, or else you can get
2471 * a real configuration food fight with a lot garbage in the
2472 * merged configuration file it creates coming from the per
2473 * daemon configuration files. This also allows the daemons
2474 * to start if there default configuration file is not
2475 * present or ignore them, as needed when using vtysh -b to
2476 * configure the daemons at boot - MAG
2477 */
2478
2479 /* Stat for vtysh Zebra.conf, if found startup and wait for
2480 * boot configuration
2481 */
2482
2483 if ( strstr(config_default_dir, "vtysh") == NULL)
2484 {
2485 ret = stat (integrate_default, &conf_stat);
2486 if (ret >= 0)
2487 goto tmp_free_and_out;
2488 }
2489 #endif /* VTYSH */
2490 confp = fopen (config_default_dir, "r");
2491 if (confp == NULL)
2492 {
2493 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2494 __func__, config_default_dir, safe_strerror (errno));
2495
2496 confp = vty_use_backup_config (config_default_dir);
2497 if (confp)
2498 {
2499 fprintf (stderr, "WARNING: using backup configuration file!\n");
2500 fullpath = config_default_dir;
2501 }
2502 else
2503 {
2504 fprintf (stderr, "can't open configuration file [%s]\n",
2505 config_default_dir);
2506 goto tmp_free_and_out;
2507 }
2508 }
2509 else
2510 fullpath = config_default_dir;
2511 }
2512
2513 vty_read_file (confp);
2514
2515 fclose (confp);
2516
2517 host_config_set (fullpath);
2518
2519 tmp_free_and_out:
2520 if (tmp)
2521 XFREE (MTYPE_TMP, tmp);
2522 }
2523
2524 /* Small utility function which output log to the VTY. */
2525 void
2526 vty_log (const char *level, const char *proto_str,
2527 const char *format, struct timestamp_control *ctl, va_list va)
2528 {
2529 unsigned int i;
2530 struct vty *vty;
2531
2532 if (!vtyvec)
2533 return;
2534
2535 for (i = 0; i < vector_active (vtyvec); i++)
2536 if ((vty = vector_slot (vtyvec, i)) != NULL)
2537 if (vty->monitor)
2538 {
2539 va_list ac;
2540 va_copy(ac, va);
2541 vty_log_out (vty, level, proto_str, format, ctl, ac);
2542 va_end(ac);
2543 }
2544 }
2545
2546 /* Async-signal-safe version of vty_log for fixed strings. */
2547 void
2548 vty_log_fixed (char *buf, size_t len)
2549 {
2550 unsigned int i;
2551 struct iovec iov[2];
2552 char crlf[4] = "\r\n";
2553
2554 /* vty may not have been initialised */
2555 if (!vtyvec)
2556 return;
2557
2558 iov[0].iov_base = buf;
2559 iov[0].iov_len = len;
2560 iov[1].iov_base = crlf;
2561 iov[1].iov_len = 2;
2562
2563 for (i = 0; i < vector_active (vtyvec); i++)
2564 {
2565 struct vty *vty;
2566 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2567 /* N.B. We don't care about the return code, since process is
2568 most likely just about to die anyway. */
2569 if (writev(vty->wfd, iov, 2) == -1)
2570 {
2571 fprintf(stderr, "Failure to writev: %d\n", errno);
2572 exit(-1);
2573 }
2574 }
2575 }
2576
2577 int
2578 vty_config_lock (struct vty *vty)
2579 {
2580 if (vty_config_is_lockless)
2581 return 1;
2582 if (vty_config == 0)
2583 {
2584 vty->config = 1;
2585 vty_config = 1;
2586 }
2587 return vty->config;
2588 }
2589
2590 int
2591 vty_config_unlock (struct vty *vty)
2592 {
2593 if (vty_config_is_lockless)
2594 return 0;
2595 if (vty_config == 1 && vty->config == 1)
2596 {
2597 vty->config = 0;
2598 vty_config = 0;
2599 }
2600 return vty->config;
2601 }
2602
2603 void
2604 vty_config_lockless (void)
2605 {
2606 vty_config_is_lockless = 1;
2607 }
2608
2609 /* Master of the threads. */
2610 static struct thread_master *vty_master;
2611
2612 static void
2613 vty_event (enum event event, int sock, struct vty *vty)
2614 {
2615 struct thread *vty_serv_thread;
2616
2617 switch (event)
2618 {
2619 case VTY_SERV:
2620 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
2621 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2622 break;
2623 #ifdef VTYSH
2624 case VTYSH_SERV:
2625 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
2626 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2627 break;
2628 case VTYSH_READ:
2629 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
2630 break;
2631 case VTYSH_WRITE:
2632 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
2633 break;
2634 #endif /* VTYSH */
2635 case VTY_READ:
2636 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
2637
2638 /* Time out treatment. */
2639 if (vty->v_timeout)
2640 {
2641 if (vty->t_timeout)
2642 thread_cancel (vty->t_timeout);
2643 vty->t_timeout =
2644 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2645 }
2646 break;
2647 case VTY_WRITE:
2648 if (! vty->t_write)
2649 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
2650 break;
2651 case VTY_TIMEOUT_RESET:
2652 if (vty->t_timeout)
2653 {
2654 thread_cancel (vty->t_timeout);
2655 vty->t_timeout = NULL;
2656 }
2657 if (vty->v_timeout)
2658 {
2659 vty->t_timeout =
2660 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2661 }
2662 break;
2663 }
2664 }
2665
2666 DEFUN (config_who,
2667 config_who_cmd,
2668 "who",
2669 "Display who is on vty\n")
2670 {
2671 unsigned int i;
2672 struct vty *v;
2673
2674 for (i = 0; i < vector_active (vtyvec); i++)
2675 if ((v = vector_slot (vtyvec, i)) != NULL)
2676 vty_out (vty, "%svty[%d] connected from %s.%s",
2677 v->config ? "*" : " ",
2678 i, v->address, VTY_NEWLINE);
2679 return CMD_SUCCESS;
2680 }
2681
2682 /* Move to vty configuration mode. */
2683 DEFUN (line_vty,
2684 line_vty_cmd,
2685 "line vty",
2686 "Configure a terminal line\n"
2687 "Virtual terminal\n")
2688 {
2689 vty->node = VTY_NODE;
2690 return CMD_SUCCESS;
2691 }
2692
2693 /* Set time out value. */
2694 static int
2695 exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
2696 {
2697 unsigned long timeout = 0;
2698
2699 /* min_str and sec_str are already checked by parser. So it must be
2700 all digit string. */
2701 if (min_str)
2702 {
2703 timeout = strtol (min_str, NULL, 10);
2704 timeout *= 60;
2705 }
2706 if (sec_str)
2707 timeout += strtol (sec_str, NULL, 10);
2708
2709 vty_timeout_val = timeout;
2710 vty->v_timeout = timeout;
2711 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2712
2713
2714 return CMD_SUCCESS;
2715 }
2716
2717 DEFUN (exec_timeout_min,
2718 exec_timeout_min_cmd,
2719 "exec-timeout (0-35791)",
2720 "Set timeout value\n"
2721 "Timeout value in minutes\n")
2722 {
2723 int idx_number = 1;
2724 return exec_timeout (vty, argv[idx_number]->arg, NULL);
2725 }
2726
2727 DEFUN (exec_timeout_sec,
2728 exec_timeout_sec_cmd,
2729 "exec-timeout (0-35791) (0-2147483)",
2730 "Set the EXEC timeout\n"
2731 "Timeout in minutes\n"
2732 "Timeout in seconds\n")
2733 {
2734 int idx_number = 1;
2735 int idx_number_2 = 2;
2736 return exec_timeout (vty, argv[idx_number]->arg, argv[idx_number_2]->arg);
2737 }
2738
2739 DEFUN (no_exec_timeout,
2740 no_exec_timeout_cmd,
2741 "no exec-timeout",
2742 NO_STR
2743 "Set the EXEC timeout\n")
2744 {
2745 return exec_timeout (vty, NULL, NULL);
2746 }
2747
2748 /* Set vty access class. */
2749 DEFUN (vty_access_class,
2750 vty_access_class_cmd,
2751 "access-class WORD",
2752 "Filter connections based on an IP access list\n"
2753 "IP access list\n")
2754 {
2755 int idx_word = 1;
2756 if (vty_accesslist_name)
2757 XFREE(MTYPE_VTY, vty_accesslist_name);
2758
2759 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
2760
2761 return CMD_SUCCESS;
2762 }
2763
2764 /* Clear vty access class. */
2765 DEFUN (no_vty_access_class,
2766 no_vty_access_class_cmd,
2767 "no access-class [WORD]",
2768 NO_STR
2769 "Filter connections based on an IP access list\n"
2770 "IP access list\n")
2771 {
2772 int idx_word = 2;
2773 const char *accesslist = (argc == 3) ? argv[idx_word]->arg : NULL;
2774 if (! vty_accesslist_name || (argc == 3 && strcmp(vty_accesslist_name, accesslist)))
2775 {
2776 vty_out (vty, "Access-class is not currently applied to vty%s",
2777 VTY_NEWLINE);
2778 return CMD_WARNING;
2779 }
2780
2781 XFREE(MTYPE_VTY, vty_accesslist_name);
2782
2783 vty_accesslist_name = NULL;
2784
2785 return CMD_SUCCESS;
2786 }
2787
2788 /* Set vty access class. */
2789 DEFUN (vty_ipv6_access_class,
2790 vty_ipv6_access_class_cmd,
2791 "ipv6 access-class WORD",
2792 IPV6_STR
2793 "Filter connections based on an IP access list\n"
2794 "IPv6 access list\n")
2795 {
2796 int idx_word = 2;
2797 if (vty_ipv6_accesslist_name)
2798 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2799
2800 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
2801
2802 return CMD_SUCCESS;
2803 }
2804
2805 /* Clear vty access class. */
2806 DEFUN (no_vty_ipv6_access_class,
2807 no_vty_ipv6_access_class_cmd,
2808 "no ipv6 access-class [WORD]",
2809 NO_STR
2810 IPV6_STR
2811 "Filter connections based on an IP access list\n"
2812 "IPv6 access list\n")
2813 {
2814 int idx_word = 3;
2815 const char *accesslist = (argc == 4) ? argv[idx_word]->arg : NULL;
2816
2817 if (! vty_ipv6_accesslist_name ||
2818 (argc == 4 && strcmp(vty_ipv6_accesslist_name, accesslist)))
2819 {
2820 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2821 VTY_NEWLINE);
2822 return CMD_WARNING;
2823 }
2824
2825 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2826
2827 vty_ipv6_accesslist_name = NULL;
2828
2829 return CMD_SUCCESS;
2830 }
2831
2832 /* vty login. */
2833 DEFUN (vty_login,
2834 vty_login_cmd,
2835 "login",
2836 "Enable password checking\n")
2837 {
2838 no_password_check = 0;
2839 return CMD_SUCCESS;
2840 }
2841
2842 DEFUN (no_vty_login,
2843 no_vty_login_cmd,
2844 "no login",
2845 NO_STR
2846 "Enable password checking\n")
2847 {
2848 no_password_check = 1;
2849 return CMD_SUCCESS;
2850 }
2851
2852 DEFUN (service_advanced_vty,
2853 service_advanced_vty_cmd,
2854 "service advanced-vty",
2855 "Set up miscellaneous service\n"
2856 "Enable advanced mode vty interface\n")
2857 {
2858 host.advanced = 1;
2859 return CMD_SUCCESS;
2860 }
2861
2862 DEFUN (no_service_advanced_vty,
2863 no_service_advanced_vty_cmd,
2864 "no service advanced-vty",
2865 NO_STR
2866 "Set up miscellaneous service\n"
2867 "Enable advanced mode vty interface\n")
2868 {
2869 host.advanced = 0;
2870 return CMD_SUCCESS;
2871 }
2872
2873 DEFUN (terminal_monitor,
2874 terminal_monitor_cmd,
2875 "terminal monitor",
2876 "Set terminal line parameters\n"
2877 "Copy debug output to the current terminal line\n")
2878 {
2879 vty->monitor = 1;
2880 return CMD_SUCCESS;
2881 }
2882
2883 DEFUN (terminal_no_monitor,
2884 terminal_no_monitor_cmd,
2885 "terminal no monitor",
2886 "Set terminal line parameters\n"
2887 NO_STR
2888 "Copy debug output to the current terminal line\n")
2889 {
2890 vty->monitor = 0;
2891 return CMD_SUCCESS;
2892 }
2893
2894 DEFUN (no_terminal_monitor,
2895 no_terminal_monitor_cmd,
2896 "no terminal monitor",
2897 NO_STR
2898 "Set terminal line parameters\n"
2899 "Copy debug output to the current terminal line\n")
2900 {
2901 return terminal_no_monitor (self, vty, argc, argv);
2902 }
2903
2904
2905 DEFUN (show_history,
2906 show_history_cmd,
2907 "show history",
2908 SHOW_STR
2909 "Display the session command history\n")
2910 {
2911 int index;
2912
2913 for (index = vty->hindex + 1; index != vty->hindex;)
2914 {
2915 if (index == VTY_MAXHIST)
2916 {
2917 index = 0;
2918 continue;
2919 }
2920
2921 if (vty->hist[index] != NULL)
2922 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2923
2924 index++;
2925 }
2926
2927 return CMD_SUCCESS;
2928 }
2929
2930 /* vty login. */
2931 DEFUN (log_commands,
2932 log_commands_cmd,
2933 "log commands",
2934 "Logging control\n"
2935 "Log all commands (can't be unset without restart)\n")
2936 {
2937 do_log_commands = 1;
2938 return CMD_SUCCESS;
2939 }
2940
2941 /* Display current configuration. */
2942 static int
2943 vty_config_write (struct vty *vty)
2944 {
2945 vty_out (vty, "line vty%s", VTY_NEWLINE);
2946
2947 if (vty_accesslist_name)
2948 vty_out (vty, " access-class %s%s",
2949 vty_accesslist_name, VTY_NEWLINE);
2950
2951 if (vty_ipv6_accesslist_name)
2952 vty_out (vty, " ipv6 access-class %s%s",
2953 vty_ipv6_accesslist_name, VTY_NEWLINE);
2954
2955 /* exec-timeout */
2956 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2957 vty_out (vty, " exec-timeout %ld %ld%s",
2958 vty_timeout_val / 60,
2959 vty_timeout_val % 60, VTY_NEWLINE);
2960
2961 /* login */
2962 if (no_password_check)
2963 vty_out (vty, " no login%s", VTY_NEWLINE);
2964
2965 if (do_log_commands)
2966 vty_out (vty, "log commands%s", VTY_NEWLINE);
2967
2968 vty_out (vty, "!%s", VTY_NEWLINE);
2969
2970 return CMD_SUCCESS;
2971 }
2972
2973 struct cmd_node vty_node =
2974 {
2975 VTY_NODE,
2976 "%s(config-line)# ",
2977 1,
2978 };
2979
2980 /* Reset all VTY status. */
2981 void
2982 vty_reset ()
2983 {
2984 unsigned int i;
2985 struct vty *vty;
2986 struct thread *vty_serv_thread;
2987
2988 for (i = 0; i < vector_active (vtyvec); i++)
2989 if ((vty = vector_slot (vtyvec, i)) != NULL)
2990 {
2991 buffer_reset (vty->obuf);
2992 vty->status = VTY_CLOSE;
2993 vty_close (vty);
2994 }
2995
2996 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
2997 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2998 {
2999 thread_cancel (vty_serv_thread);
3000 vector_slot (Vvty_serv_thread, i) = NULL;
3001 close (i);
3002 }
3003
3004 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3005
3006 if (vty_accesslist_name)
3007 {
3008 XFREE(MTYPE_VTY, vty_accesslist_name);
3009 vty_accesslist_name = NULL;
3010 }
3011
3012 if (vty_ipv6_accesslist_name)
3013 {
3014 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
3015 vty_ipv6_accesslist_name = NULL;
3016 }
3017 }
3018
3019 static void
3020 vty_save_cwd (void)
3021 {
3022 char cwd[MAXPATHLEN];
3023 char *c;
3024
3025 c = getcwd (cwd, MAXPATHLEN);
3026
3027 if (!c)
3028 {
3029 /*
3030 * At this point if these go wrong, more than likely
3031 * the whole world is coming down around us
3032 * Hence not worrying about it too much.
3033 */
3034 if (!chdir (SYSCONFDIR))
3035 {
3036 fprintf(stderr, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR, errno);
3037 exit(-1);
3038 }
3039 if (getcwd (cwd, MAXPATHLEN) == NULL)
3040 {
3041 fprintf(stderr, "Failure to getcwd, errno: %d\n", errno);
3042 exit(-1);
3043 }
3044 }
3045
3046 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3047 strcpy (vty_cwd, cwd);
3048 }
3049
3050 char *
3051 vty_get_cwd ()
3052 {
3053 return vty_cwd;
3054 }
3055
3056 int
3057 vty_shell (struct vty *vty)
3058 {
3059 return vty->type == VTY_SHELL ? 1 : 0;
3060 }
3061
3062 int
3063 vty_shell_serv (struct vty *vty)
3064 {
3065 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3066 }
3067
3068 void
3069 vty_init_vtysh ()
3070 {
3071 vtyvec = vector_init (VECTOR_MIN_SIZE);
3072 }
3073
3074 /* Install vty's own commands like `who' command. */
3075 void
3076 vty_init (struct thread_master *master_thread)
3077 {
3078 /* For further configuration read, preserve current directory. */
3079 vty_save_cwd ();
3080
3081 vtyvec = vector_init (VECTOR_MIN_SIZE);
3082
3083 vty_master = master_thread;
3084
3085 atexit (vty_stdio_reset);
3086
3087 /* Initilize server thread vector. */
3088 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3089
3090 /* Install bgp top node. */
3091 install_node (&vty_node, vty_config_write);
3092
3093 install_element (VIEW_NODE, &config_who_cmd);
3094 install_element (VIEW_NODE, &show_history_cmd);
3095 install_element (CONFIG_NODE, &line_vty_cmd);
3096 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3097 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3098 install_element (CONFIG_NODE, &show_history_cmd);
3099 install_element (CONFIG_NODE, &log_commands_cmd);
3100 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3101 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
3102 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
3103
3104 install_default (VTY_NODE);
3105 install_element (VTY_NODE, &exec_timeout_min_cmd);
3106 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3107 install_element (VTY_NODE, &no_exec_timeout_cmd);
3108 install_element (VTY_NODE, &vty_access_class_cmd);
3109 install_element (VTY_NODE, &no_vty_access_class_cmd);
3110 install_element (VTY_NODE, &vty_login_cmd);
3111 install_element (VTY_NODE, &no_vty_login_cmd);
3112 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3113 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3114 }
3115
3116 void
3117 vty_terminate (void)
3118 {
3119 if (vty_cwd)
3120 XFREE (MTYPE_TMP, vty_cwd);
3121
3122 if (vtyvec && Vvty_serv_thread)
3123 {
3124 vty_reset ();
3125 vector_free (vtyvec);
3126 vector_free (Vvty_serv_thread);
3127 }
3128 }
3129
3130 /* Utility functions to get arguments from commands generated
3131 by the xml2cli.pl script. */
3132 const char *
3133 vty_get_arg_value (struct vty_arg *args[], const char *arg)
3134 {
3135 while (*args)
3136 {
3137 if (strcmp ((*args)->name, arg) == 0)
3138 return (*args)->value;
3139 args++;
3140 }
3141 return NULL;
3142 }
3143
3144 struct vty_arg *
3145 vty_get_arg (struct vty_arg *args[], const char *arg)
3146 {
3147 while (*args)
3148 {
3149 if (strcmp ((*args)->name, arg) == 0)
3150 return *args;
3151 args++;
3152 }
3153 return NULL;
3154 }