]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.c
Merge remote-tracking branch 'origin/master' into EIGRP
[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_IPV6_NODE:
750 case BGP_IPV6M_NODE:
751 case BGP_EVPN_NODE:
752 case RMAP_NODE:
753 case OSPF_NODE:
754 case OSPF6_NODE:
755 case LDP_NODE:
756 case LDP_IPV4_NODE:
757 case LDP_IPV6_NODE:
758 case LDP_IPV4_IFACE_NODE:
759 case LDP_IPV6_IFACE_NODE:
760 case LDP_L2VPN_NODE:
761 case LDP_PSEUDOWIRE_NODE:
762 case ISIS_NODE:
763 case KEYCHAIN_NODE:
764 case KEYCHAIN_KEY_NODE:
765 case MASC_NODE:
766 case PIM_NODE:
767 case VTY_NODE:
768 vty_config_unlock (vty);
769 vty->node = ENABLE_NODE;
770 break;
771 default:
772 /* Unknown node, we have to ignore it. */
773 break;
774 }
775
776 vty_prompt (vty);
777 vty->cp = 0;
778 }
779
780 /* Delete a charcter at the current point. */
781 static void
782 vty_delete_char (struct vty *vty)
783 {
784 int i;
785 int size;
786
787 if (vty->length == 0)
788 {
789 vty_down_level (vty);
790 return;
791 }
792
793 if (vty->cp == vty->length)
794 return; /* completion need here? */
795
796 size = vty->length - vty->cp;
797
798 vty->length--;
799 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
800 vty->buf[vty->length] = '\0';
801
802 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
803 return;
804
805 vty_write (vty, &vty->buf[vty->cp], size - 1);
806 vty_write (vty, &telnet_space_char, 1);
807
808 for (i = 0; i < size; i++)
809 vty_write (vty, &telnet_backward_char, 1);
810 }
811
812 /* Delete a character before the point. */
813 static void
814 vty_delete_backward_char (struct vty *vty)
815 {
816 if (vty->cp == 0)
817 return;
818
819 vty_backward_char (vty);
820 vty_delete_char (vty);
821 }
822
823 /* Kill rest of line from current point. */
824 static void
825 vty_kill_line (struct vty *vty)
826 {
827 int i;
828 int size;
829
830 size = vty->length - vty->cp;
831
832 if (size == 0)
833 return;
834
835 for (i = 0; i < size; i++)
836 vty_write (vty, &telnet_space_char, 1);
837 for (i = 0; i < size; i++)
838 vty_write (vty, &telnet_backward_char, 1);
839
840 memset (&vty->buf[vty->cp], 0, size);
841 vty->length = vty->cp;
842 }
843
844 /* Kill line from the beginning. */
845 static void
846 vty_kill_line_from_beginning (struct vty *vty)
847 {
848 vty_beginning_of_line (vty);
849 vty_kill_line (vty);
850 }
851
852 /* Delete a word before the point. */
853 static void
854 vty_forward_kill_word (struct vty *vty)
855 {
856 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
857 vty_delete_char (vty);
858 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
859 vty_delete_char (vty);
860 }
861
862 /* Delete a word before the point. */
863 static void
864 vty_backward_kill_word (struct vty *vty)
865 {
866 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
867 vty_delete_backward_char (vty);
868 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
869 vty_delete_backward_char (vty);
870 }
871
872 /* Transpose chars before or at the point. */
873 static void
874 vty_transpose_chars (struct vty *vty)
875 {
876 char c1, c2;
877
878 /* If length is short or point is near by the beginning of line then
879 return. */
880 if (vty->length < 2 || vty->cp < 1)
881 return;
882
883 /* In case of point is located at the end of the line. */
884 if (vty->cp == vty->length)
885 {
886 c1 = vty->buf[vty->cp - 1];
887 c2 = vty->buf[vty->cp - 2];
888
889 vty_backward_char (vty);
890 vty_backward_char (vty);
891 vty_self_insert_overwrite (vty, c1);
892 vty_self_insert_overwrite (vty, c2);
893 }
894 else
895 {
896 c1 = vty->buf[vty->cp];
897 c2 = vty->buf[vty->cp - 1];
898
899 vty_backward_char (vty);
900 vty_self_insert_overwrite (vty, c1);
901 vty_self_insert_overwrite (vty, c2);
902 }
903 }
904
905 /* Do completion at vty interface. */
906 static void
907 vty_complete_command (struct vty *vty)
908 {
909 int i;
910 int ret;
911 char **matched = NULL;
912 vector vline;
913
914 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
915 return;
916
917 vline = cmd_make_strvec (vty->buf);
918 if (vline == NULL)
919 return;
920
921 /* In case of 'help \t'. */
922 if (isspace ((int) vty->buf[vty->length - 1]))
923 vector_set (vline, NULL);
924
925 matched = cmd_complete_command (vline, vty, &ret);
926
927 cmd_free_strvec (vline);
928
929 vty_out (vty, "%s", VTY_NEWLINE);
930 switch (ret)
931 {
932 case CMD_ERR_AMBIGUOUS:
933 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
934 vty_prompt (vty);
935 vty_redraw_line (vty);
936 break;
937 case CMD_ERR_NO_MATCH:
938 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
939 vty_prompt (vty);
940 vty_redraw_line (vty);
941 break;
942 case CMD_COMPLETE_FULL_MATCH:
943 if (!matched[0])
944 {
945 /* 2016-11-28 equinox -- need to debug, SEGV here */
946 vty_out (vty, "%% CLI BUG: FULL_MATCH with NULL str%s", VTY_NEWLINE);
947 vty_prompt (vty);
948 vty_redraw_line (vty);
949 break;
950 }
951 vty_prompt (vty);
952 vty_redraw_line (vty);
953 vty_backward_pure_word (vty);
954 vty_insert_word_overwrite (vty, matched[0]);
955 vty_self_insert (vty, ' ');
956 XFREE (MTYPE_TMP, matched[0]);
957 break;
958 case CMD_COMPLETE_MATCH:
959 vty_prompt (vty);
960 vty_redraw_line (vty);
961 vty_backward_pure_word (vty);
962 vty_insert_word_overwrite (vty, matched[0]);
963 XFREE (MTYPE_TMP, matched[0]);
964 break;
965 case CMD_COMPLETE_LIST_MATCH:
966 for (i = 0; matched[i] != NULL; i++)
967 {
968 if (i != 0 && ((i % 6) == 0))
969 vty_out (vty, "%s", VTY_NEWLINE);
970 vty_out (vty, "%-10s ", matched[i]);
971 XFREE (MTYPE_TMP, matched[i]);
972 }
973 vty_out (vty, "%s", VTY_NEWLINE);
974
975 vty_prompt (vty);
976 vty_redraw_line (vty);
977 break;
978 case CMD_ERR_NOTHING_TODO:
979 vty_prompt (vty);
980 vty_redraw_line (vty);
981 break;
982 default:
983 break;
984 }
985 if (matched)
986 XFREE (MTYPE_TMP, matched);
987 }
988
989 static void
990 vty_describe_fold (struct vty *vty, int cmd_width,
991 unsigned int desc_width, struct cmd_token *token)
992 {
993 char *buf;
994 const char *cmd, *p;
995 int pos;
996
997 cmd = token->text;
998
999 if (desc_width <= 0)
1000 {
1001 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
1002 return;
1003 }
1004
1005 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
1006
1007 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
1008 {
1009 for (pos = desc_width; pos > 0; pos--)
1010 if (*(p + pos) == ' ')
1011 break;
1012
1013 if (pos == 0)
1014 break;
1015
1016 strncpy (buf, p, pos);
1017 buf[pos] = '\0';
1018 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
1019
1020 cmd = "";
1021 }
1022
1023 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
1024
1025 XFREE (MTYPE_TMP, buf);
1026 }
1027
1028 /* Describe matched command function. */
1029 static void
1030 vty_describe_command (struct vty *vty)
1031 {
1032 int ret;
1033 vector vline;
1034 vector describe;
1035 unsigned int i, width, desc_width;
1036 struct cmd_token *token, *token_cr = NULL;
1037
1038 vline = cmd_make_strvec (vty->buf);
1039
1040 /* In case of '> ?'. */
1041 if (vline == NULL)
1042 {
1043 vline = vector_init (1);
1044 vector_set (vline, NULL);
1045 }
1046 else
1047 if (isspace ((int) vty->buf[vty->length - 1]))
1048 vector_set (vline, NULL);
1049
1050 describe = cmd_describe_command (vline, vty, &ret);
1051
1052 vty_out (vty, "%s", VTY_NEWLINE);
1053
1054 /* Ambiguous error. */
1055 switch (ret)
1056 {
1057 case CMD_ERR_AMBIGUOUS:
1058 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
1059 goto out;
1060 break;
1061 case CMD_ERR_NO_MATCH:
1062 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
1063 goto out;
1064 break;
1065 }
1066
1067 /* Get width of command string. */
1068 width = 0;
1069 for (i = 0; i < vector_active (describe); i++)
1070 if ((token = vector_slot (describe, i)) != NULL)
1071 {
1072 unsigned int len;
1073
1074 if (token->text[0] == '\0')
1075 continue;
1076
1077 len = strlen (token->text);
1078
1079 if (width < len)
1080 width = len;
1081 }
1082
1083 /* Get width of description string. */
1084 desc_width = vty->width - (width + 6);
1085
1086 /* Print out description. */
1087 for (i = 0; i < vector_active (describe); i++)
1088 if ((token = vector_slot (describe, i)) != NULL)
1089 {
1090 if (token->text[0] == '\0')
1091 continue;
1092
1093 if (strcmp (token->text, CMD_CR_TEXT) == 0)
1094 {
1095 token_cr = token;
1096 continue;
1097 }
1098
1099 if (!token->desc)
1100 vty_out (vty, " %-s%s",
1101 token->text,
1102 VTY_NEWLINE);
1103 else if (desc_width >= strlen (token->desc))
1104 vty_out (vty, " %-*s %s%s", width,
1105 token->text,
1106 token->desc, VTY_NEWLINE);
1107 else
1108 vty_describe_fold (vty, width, desc_width, token);
1109
1110 #if 0
1111 vty_out (vty, " %-*s %s%s", width
1112 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1113 desc->str ? desc->str : "", VTY_NEWLINE);
1114 #endif /* 0 */
1115 }
1116
1117 if ((token = token_cr))
1118 {
1119 if (!token->desc)
1120 vty_out (vty, " %-s%s",
1121 token->text,
1122 VTY_NEWLINE);
1123 else if (desc_width >= strlen (token->desc))
1124 vty_out (vty, " %-*s %s%s", width,
1125 token->text,
1126 token->desc, VTY_NEWLINE);
1127 else
1128 vty_describe_fold (vty, width, desc_width, token);
1129 }
1130
1131 out:
1132 cmd_free_strvec (vline);
1133 if (describe)
1134 vector_free (describe);
1135
1136 vty_prompt (vty);
1137 vty_redraw_line (vty);
1138 }
1139
1140 static void
1141 vty_clear_buf (struct vty *vty)
1142 {
1143 memset (vty->buf, 0, vty->max);
1144 }
1145
1146 /* ^C stop current input and do not add command line to the history. */
1147 static void
1148 vty_stop_input (struct vty *vty)
1149 {
1150 vty->cp = vty->length = 0;
1151 vty_clear_buf (vty);
1152 vty_out (vty, "%s", VTY_NEWLINE);
1153
1154 switch (vty->node)
1155 {
1156 case VIEW_NODE:
1157 case ENABLE_NODE:
1158 /* Nothing to do. */
1159 break;
1160 case CONFIG_NODE:
1161 case INTERFACE_NODE:
1162 case ZEBRA_NODE:
1163 case RIP_NODE:
1164 case RIPNG_NODE:
1165 case EIGRP_NODE:
1166 case BGP_NODE:
1167 case RMAP_NODE:
1168 case OSPF_NODE:
1169 case OSPF6_NODE:
1170 case LDP_NODE:
1171 case LDP_IPV4_NODE:
1172 case LDP_IPV6_NODE:
1173 case LDP_IPV4_IFACE_NODE:
1174 case LDP_IPV6_IFACE_NODE:
1175 case LDP_L2VPN_NODE:
1176 case LDP_PSEUDOWIRE_NODE:
1177 case ISIS_NODE:
1178 case KEYCHAIN_NODE:
1179 case KEYCHAIN_KEY_NODE:
1180 case MASC_NODE:
1181 case PIM_NODE:
1182 case VTY_NODE:
1183 vty_config_unlock (vty);
1184 vty->node = ENABLE_NODE;
1185 break;
1186 default:
1187 /* Unknown node, we have to ignore it. */
1188 break;
1189 }
1190 vty_prompt (vty);
1191
1192 /* Set history pointer to the latest one. */
1193 vty->hp = vty->hindex;
1194 }
1195
1196 /* Add current command line to the history buffer. */
1197 static void
1198 vty_hist_add (struct vty *vty)
1199 {
1200 int index;
1201
1202 if (vty->length == 0)
1203 return;
1204
1205 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1206
1207 /* Ignore the same string as previous one. */
1208 if (vty->hist[index])
1209 if (strcmp (vty->buf, vty->hist[index]) == 0)
1210 {
1211 vty->hp = vty->hindex;
1212 return;
1213 }
1214
1215 /* Insert history entry. */
1216 if (vty->hist[vty->hindex])
1217 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1218 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1219
1220 /* History index rotation. */
1221 vty->hindex++;
1222 if (vty->hindex == VTY_MAXHIST)
1223 vty->hindex = 0;
1224
1225 vty->hp = vty->hindex;
1226 }
1227
1228 /* #define TELNET_OPTION_DEBUG */
1229
1230 /* Get telnet window size. */
1231 static int
1232 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1233 {
1234 #ifdef TELNET_OPTION_DEBUG
1235 int i;
1236
1237 for (i = 0; i < nbytes; i++)
1238 {
1239 switch (buf[i])
1240 {
1241 case IAC:
1242 vty_out (vty, "IAC ");
1243 break;
1244 case WILL:
1245 vty_out (vty, "WILL ");
1246 break;
1247 case WONT:
1248 vty_out (vty, "WONT ");
1249 break;
1250 case DO:
1251 vty_out (vty, "DO ");
1252 break;
1253 case DONT:
1254 vty_out (vty, "DONT ");
1255 break;
1256 case SB:
1257 vty_out (vty, "SB ");
1258 break;
1259 case SE:
1260 vty_out (vty, "SE ");
1261 break;
1262 case TELOPT_ECHO:
1263 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1264 break;
1265 case TELOPT_SGA:
1266 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1267 break;
1268 case TELOPT_NAWS:
1269 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1270 break;
1271 default:
1272 vty_out (vty, "%x ", buf[i]);
1273 break;
1274 }
1275 }
1276 vty_out (vty, "%s", VTY_NEWLINE);
1277
1278 #endif /* TELNET_OPTION_DEBUG */
1279
1280 switch (buf[0])
1281 {
1282 case SB:
1283 vty->sb_len = 0;
1284 vty->iac_sb_in_progress = 1;
1285 return 0;
1286 break;
1287 case SE:
1288 {
1289 if (!vty->iac_sb_in_progress)
1290 return 0;
1291
1292 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
1293 {
1294 vty->iac_sb_in_progress = 0;
1295 return 0;
1296 }
1297 switch (vty->sb_buf[0])
1298 {
1299 case TELOPT_NAWS:
1300 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1301 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1302 "should send %d characters, but we received %lu",
1303 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1304 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1305 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1306 "too small to handle the telnet NAWS option",
1307 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1308 else
1309 {
1310 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1311 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1312 #ifdef TELNET_OPTION_DEBUG
1313 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1314 "width %d, height %d%s",
1315 vty->width, vty->height, VTY_NEWLINE);
1316 #endif
1317 }
1318 break;
1319 }
1320 vty->iac_sb_in_progress = 0;
1321 return 0;
1322 break;
1323 }
1324 default:
1325 break;
1326 }
1327 return 1;
1328 }
1329
1330 /* Execute current command line. */
1331 static int
1332 vty_execute (struct vty *vty)
1333 {
1334 int ret;
1335
1336 ret = CMD_SUCCESS;
1337
1338 switch (vty->node)
1339 {
1340 case AUTH_NODE:
1341 case AUTH_ENABLE_NODE:
1342 vty_auth (vty, vty->buf);
1343 break;
1344 default:
1345 ret = vty_command (vty, vty->buf);
1346 if (vty->type == VTY_TERM)
1347 vty_hist_add (vty);
1348 break;
1349 }
1350
1351 /* Clear command line buffer. */
1352 vty->cp = vty->length = 0;
1353 vty_clear_buf (vty);
1354
1355 if (vty->status != VTY_CLOSE )
1356 vty_prompt (vty);
1357
1358 return ret;
1359 }
1360
1361 #define CONTROL(X) ((X) - '@')
1362 #define VTY_NORMAL 0
1363 #define VTY_PRE_ESCAPE 1
1364 #define VTY_ESCAPE 2
1365
1366 /* Escape character command map. */
1367 static void
1368 vty_escape_map (unsigned char c, struct vty *vty)
1369 {
1370 switch (c)
1371 {
1372 case ('A'):
1373 vty_previous_line (vty);
1374 break;
1375 case ('B'):
1376 vty_next_line (vty);
1377 break;
1378 case ('C'):
1379 vty_forward_char (vty);
1380 break;
1381 case ('D'):
1382 vty_backward_char (vty);
1383 break;
1384 default:
1385 break;
1386 }
1387
1388 /* Go back to normal mode. */
1389 vty->escape = VTY_NORMAL;
1390 }
1391
1392 /* Quit print out to the buffer. */
1393 static void
1394 vty_buffer_reset (struct vty *vty)
1395 {
1396 buffer_reset (vty->obuf);
1397 vty_prompt (vty);
1398 vty_redraw_line (vty);
1399 }
1400
1401 /* Read data via vty socket. */
1402 static int
1403 vty_read (struct thread *thread)
1404 {
1405 int i;
1406 int nbytes;
1407 unsigned char buf[VTY_READ_BUFSIZ];
1408
1409 int vty_sock = THREAD_FD (thread);
1410 struct vty *vty = THREAD_ARG (thread);
1411 vty->t_read = NULL;
1412
1413 /* Read raw data from socket */
1414 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1415 {
1416 if (nbytes < 0)
1417 {
1418 if (ERRNO_IO_RETRY(errno))
1419 {
1420 vty_event (VTY_READ, vty_sock, vty);
1421 return 0;
1422 }
1423 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1424 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1425 __func__, vty->fd, safe_strerror(errno));
1426 buffer_reset(vty->obuf);
1427 }
1428 vty->status = VTY_CLOSE;
1429 }
1430
1431 for (i = 0; i < nbytes; i++)
1432 {
1433 if (buf[i] == IAC)
1434 {
1435 if (!vty->iac)
1436 {
1437 vty->iac = 1;
1438 continue;
1439 }
1440 else
1441 {
1442 vty->iac = 0;
1443 }
1444 }
1445
1446 if (vty->iac_sb_in_progress && !vty->iac)
1447 {
1448 if (vty->sb_len < sizeof(vty->sb_buf))
1449 vty->sb_buf[vty->sb_len] = buf[i];
1450 vty->sb_len++;
1451 continue;
1452 }
1453
1454 if (vty->iac)
1455 {
1456 /* In case of telnet command */
1457 int ret = 0;
1458 ret = vty_telnet_option (vty, buf + i, nbytes - i);
1459 vty->iac = 0;
1460 i += ret;
1461 continue;
1462 }
1463
1464
1465 if (vty->status == VTY_MORE)
1466 {
1467 switch (buf[i])
1468 {
1469 case CONTROL('C'):
1470 case 'q':
1471 case 'Q':
1472 vty_buffer_reset (vty);
1473 break;
1474 #if 0 /* More line does not work for "show ip bgp". */
1475 case '\n':
1476 case '\r':
1477 vty->status = VTY_MORELINE;
1478 break;
1479 #endif
1480 default:
1481 break;
1482 }
1483 continue;
1484 }
1485
1486 /* Escape character. */
1487 if (vty->escape == VTY_ESCAPE)
1488 {
1489 vty_escape_map (buf[i], vty);
1490 continue;
1491 }
1492
1493 /* Pre-escape status. */
1494 if (vty->escape == VTY_PRE_ESCAPE)
1495 {
1496 switch (buf[i])
1497 {
1498 case '[':
1499 vty->escape = VTY_ESCAPE;
1500 break;
1501 case 'b':
1502 vty_backward_word (vty);
1503 vty->escape = VTY_NORMAL;
1504 break;
1505 case 'f':
1506 vty_forward_word (vty);
1507 vty->escape = VTY_NORMAL;
1508 break;
1509 case 'd':
1510 vty_forward_kill_word (vty);
1511 vty->escape = VTY_NORMAL;
1512 break;
1513 case CONTROL('H'):
1514 case 0x7f:
1515 vty_backward_kill_word (vty);
1516 vty->escape = VTY_NORMAL;
1517 break;
1518 default:
1519 vty->escape = VTY_NORMAL;
1520 break;
1521 }
1522 continue;
1523 }
1524
1525 switch (buf[i])
1526 {
1527 case CONTROL('A'):
1528 vty_beginning_of_line (vty);
1529 break;
1530 case CONTROL('B'):
1531 vty_backward_char (vty);
1532 break;
1533 case CONTROL('C'):
1534 vty_stop_input (vty);
1535 break;
1536 case CONTROL('D'):
1537 vty_delete_char (vty);
1538 break;
1539 case CONTROL('E'):
1540 vty_end_of_line (vty);
1541 break;
1542 case CONTROL('F'):
1543 vty_forward_char (vty);
1544 break;
1545 case CONTROL('H'):
1546 case 0x7f:
1547 vty_delete_backward_char (vty);
1548 break;
1549 case CONTROL('K'):
1550 vty_kill_line (vty);
1551 break;
1552 case CONTROL('N'):
1553 vty_next_line (vty);
1554 break;
1555 case CONTROL('P'):
1556 vty_previous_line (vty);
1557 break;
1558 case CONTROL('T'):
1559 vty_transpose_chars (vty);
1560 break;
1561 case CONTROL('U'):
1562 vty_kill_line_from_beginning (vty);
1563 break;
1564 case CONTROL('W'):
1565 vty_backward_kill_word (vty);
1566 break;
1567 case CONTROL('Z'):
1568 vty_end_config (vty);
1569 break;
1570 case '\n':
1571 case '\r':
1572 vty_out (vty, "%s", VTY_NEWLINE);
1573 vty_execute (vty);
1574 break;
1575 case '\t':
1576 vty_complete_command (vty);
1577 break;
1578 case '?':
1579 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1580 vty_self_insert (vty, buf[i]);
1581 else
1582 vty_describe_command (vty);
1583 break;
1584 case '\033':
1585 if (i + 1 < nbytes && buf[i + 1] == '[')
1586 {
1587 vty->escape = VTY_ESCAPE;
1588 i++;
1589 }
1590 else
1591 vty->escape = VTY_PRE_ESCAPE;
1592 break;
1593 default:
1594 if (buf[i] > 31 && buf[i] < 127)
1595 vty_self_insert (vty, buf[i]);
1596 break;
1597 }
1598 }
1599
1600 /* Check status. */
1601 if (vty->status == VTY_CLOSE)
1602 vty_close (vty);
1603 else
1604 {
1605 vty_event (VTY_WRITE, vty->wfd, vty);
1606 vty_event (VTY_READ, vty_sock, vty);
1607 }
1608 return 0;
1609 }
1610
1611 /* Flush buffer to the vty. */
1612 static int
1613 vty_flush (struct thread *thread)
1614 {
1615 int erase;
1616 buffer_status_t flushrc;
1617 int vty_sock = THREAD_FD (thread);
1618 struct vty *vty = THREAD_ARG (thread);
1619
1620 vty->t_write = NULL;
1621
1622 /* Tempolary disable read thread. */
1623 if ((vty->lines == 0) && vty->t_read)
1624 {
1625 thread_cancel (vty->t_read);
1626 vty->t_read = NULL;
1627 }
1628
1629 /* Function execution continue. */
1630 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1631
1632 /* N.B. if width is 0, that means we don't know the window size. */
1633 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
1634 flushrc = buffer_flush_available(vty->obuf, vty_sock);
1635 else if (vty->status == VTY_MORELINE)
1636 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1637 1, erase, 0);
1638 else
1639 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
1640 vty->lines >= 0 ? vty->lines :
1641 vty->height,
1642 erase, 0);
1643 switch (flushrc)
1644 {
1645 case BUFFER_ERROR:
1646 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
1647 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1648 vty->fd);
1649 buffer_reset(vty->obuf);
1650 vty_close(vty);
1651 return 0;
1652 case BUFFER_EMPTY:
1653 if (vty->status == VTY_CLOSE)
1654 vty_close (vty);
1655 else
1656 {
1657 vty->status = VTY_NORMAL;
1658 if (vty->lines == 0)
1659 vty_event (VTY_READ, vty_sock, vty);
1660 }
1661 break;
1662 case BUFFER_PENDING:
1663 /* There is more data waiting to be written. */
1664 vty->status = VTY_MORE;
1665 if (vty->lines == 0)
1666 vty_event (VTY_WRITE, vty_sock, vty);
1667 break;
1668 }
1669
1670 return 0;
1671 }
1672
1673 /* allocate and initialise vty */
1674 static struct vty *
1675 vty_new_init (int vty_sock)
1676 {
1677 struct vty *vty;
1678
1679 vty = vty_new ();
1680 vty->fd = vty_sock;
1681 vty->wfd = vty_sock;
1682 vty->type = VTY_TERM;
1683 vty->node = AUTH_NODE;
1684 vty->fail = 0;
1685 vty->cp = 0;
1686 vty_clear_buf (vty);
1687 vty->length = 0;
1688 memset (vty->hist, 0, sizeof (vty->hist));
1689 vty->hp = 0;
1690 vty->hindex = 0;
1691 vector_set_index (vtyvec, vty_sock, vty);
1692 vty->status = VTY_NORMAL;
1693 vty->lines = -1;
1694 vty->iac = 0;
1695 vty->iac_sb_in_progress = 0;
1696 vty->sb_len = 0;
1697
1698 return vty;
1699 }
1700
1701 /* Create new vty structure. */
1702 static struct vty *
1703 vty_create (int vty_sock, union sockunion *su)
1704 {
1705 char buf[SU_ADDRSTRLEN];
1706 struct vty *vty;
1707
1708 sockunion2str(su, buf, SU_ADDRSTRLEN);
1709
1710 /* Allocate new vty structure and set up default values. */
1711 vty = vty_new_init (vty_sock);
1712
1713 /* configurable parameters not part of basic init */
1714 vty->v_timeout = vty_timeout_val;
1715 strcpy (vty->address, buf);
1716 if (no_password_check)
1717 {
1718 if (host.advanced)
1719 vty->node = ENABLE_NODE;
1720 else
1721 vty->node = VIEW_NODE;
1722 }
1723 if (host.lines >= 0)
1724 vty->lines = host.lines;
1725
1726 if (! no_password_check)
1727 {
1728 /* Vty is not available if password isn't set. */
1729 if (host.password == NULL && host.password_encrypt == NULL)
1730 {
1731 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1732 vty->status = VTY_CLOSE;
1733 vty_close (vty);
1734 return NULL;
1735 }
1736 }
1737
1738 /* Say hello to the world. */
1739 vty_hello (vty);
1740 if (! no_password_check)
1741 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1742
1743 /* Setting up terminal. */
1744 vty_will_echo (vty);
1745 vty_will_suppress_go_ahead (vty);
1746
1747 vty_dont_linemode (vty);
1748 vty_do_window_size (vty);
1749 /* vty_dont_lflow_ahead (vty); */
1750
1751 vty_prompt (vty);
1752
1753 /* Add read/write thread. */
1754 vty_event (VTY_WRITE, vty_sock, vty);
1755 vty_event (VTY_READ, vty_sock, vty);
1756
1757 return vty;
1758 }
1759
1760 /* create vty for stdio */
1761 static struct termios stdio_orig_termios;
1762 static struct vty *stdio_vty = NULL;
1763 static void (*stdio_vty_atclose)(void);
1764
1765 static void
1766 vty_stdio_reset (void)
1767 {
1768 if (stdio_vty)
1769 {
1770 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1771 stdio_vty = NULL;
1772
1773 if (stdio_vty_atclose)
1774 stdio_vty_atclose ();
1775 stdio_vty_atclose = NULL;
1776 }
1777 }
1778
1779 struct vty *
1780 vty_stdio (void (*atclose)())
1781 {
1782 struct vty *vty;
1783 struct termios termios;
1784
1785 /* refuse creating two vtys on stdio */
1786 if (stdio_vty)
1787 return NULL;
1788
1789 vty = stdio_vty = vty_new_init (0);
1790 stdio_vty_atclose = atclose;
1791 vty->wfd = 1;
1792
1793 /* always have stdio vty in a known _unchangeable_ state, don't want config
1794 * to have any effect here to make sure scripting this works as intended */
1795 vty->node = ENABLE_NODE;
1796 vty->v_timeout = 0;
1797 strcpy (vty->address, "console");
1798
1799 if (!tcgetattr (0, &stdio_orig_termios))
1800 {
1801 termios = stdio_orig_termios;
1802 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1803 | INLCR | IGNCR | ICRNL | IXON);
1804 termios.c_oflag &= ~OPOST;
1805 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1806 termios.c_cflag &= ~(CSIZE | PARENB);
1807 termios.c_cflag |= CS8;
1808 tcsetattr (0, TCSANOW, &termios);
1809 }
1810
1811 vty_prompt (vty);
1812
1813 /* Add read/write thread. */
1814 vty_event (VTY_WRITE, 1, vty);
1815 vty_event (VTY_READ, 0, vty);
1816
1817 return vty;
1818 }
1819
1820 /* Accept connection from the network. */
1821 static int
1822 vty_accept (struct thread *thread)
1823 {
1824 int vty_sock;
1825 union sockunion su;
1826 int ret;
1827 unsigned int on;
1828 int accept_sock;
1829 struct prefix p;
1830 struct access_list *acl = NULL;
1831 char buf[SU_ADDRSTRLEN];
1832
1833 accept_sock = THREAD_FD (thread);
1834
1835 /* We continue hearing vty socket. */
1836 vty_event (VTY_SERV, accept_sock, NULL);
1837
1838 memset (&su, 0, sizeof (union sockunion));
1839
1840 /* We can handle IPv4 or IPv6 socket. */
1841 vty_sock = sockunion_accept (accept_sock, &su);
1842 if (vty_sock < 0)
1843 {
1844 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
1845 return -1;
1846 }
1847 set_nonblocking(vty_sock);
1848 set_cloexec(vty_sock);
1849
1850 sockunion2hostprefix (&su, &p);
1851
1852 /* VTY's accesslist apply. */
1853 if (p.family == AF_INET && vty_accesslist_name)
1854 {
1855 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1856 (access_list_apply (acl, &p) == FILTER_DENY))
1857 {
1858 zlog_info ("Vty connection refused from %s",
1859 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1860 close (vty_sock);
1861
1862 /* continue accepting connections */
1863 vty_event (VTY_SERV, accept_sock, NULL);
1864
1865 return 0;
1866 }
1867 }
1868
1869 /* VTY's ipv6 accesslist apply. */
1870 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
1871 {
1872 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1873 (access_list_apply (acl, &p) == FILTER_DENY))
1874 {
1875 zlog_info ("Vty connection refused from %s",
1876 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1877 close (vty_sock);
1878
1879 /* continue accepting connections */
1880 vty_event (VTY_SERV, accept_sock, NULL);
1881
1882 return 0;
1883 }
1884 }
1885
1886 on = 1;
1887 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1888 (char *) &on, sizeof (on));
1889 if (ret < 0)
1890 zlog_info ("can't set sockopt to vty_sock : %s",
1891 safe_strerror (errno));
1892
1893 zlog_info ("Vty connection from %s",
1894 sockunion2str (&su, buf, SU_ADDRSTRLEN));
1895
1896 vty_create (vty_sock, &su);
1897
1898 return 0;
1899 }
1900
1901 static void
1902 vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1903 {
1904 int ret;
1905 struct addrinfo req;
1906 struct addrinfo *ainfo;
1907 struct addrinfo *ainfo_save;
1908 int sock;
1909 char port_str[BUFSIZ];
1910
1911 memset (&req, 0, sizeof (struct addrinfo));
1912 req.ai_flags = AI_PASSIVE;
1913 req.ai_family = AF_UNSPEC;
1914 req.ai_socktype = SOCK_STREAM;
1915 sprintf (port_str, "%d", port);
1916 port_str[sizeof (port_str) - 1] = '\0';
1917
1918 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1919
1920 if (ret != 0)
1921 {
1922 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1923 exit (1);
1924 }
1925
1926 ainfo_save = ainfo;
1927
1928 do
1929 {
1930 if (ainfo->ai_family != AF_INET
1931 && ainfo->ai_family != AF_INET6
1932 )
1933 continue;
1934
1935 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1936 if (sock < 0)
1937 continue;
1938
1939 sockopt_v6only (ainfo->ai_family, sock);
1940 sockopt_reuseaddr (sock);
1941 sockopt_reuseport (sock);
1942 set_cloexec (sock);
1943
1944 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1945 if (ret < 0)
1946 {
1947 close (sock); /* Avoid sd leak. */
1948 continue;
1949 }
1950
1951 ret = listen (sock, 3);
1952 if (ret < 0)
1953 {
1954 close (sock); /* Avoid sd leak. */
1955 continue;
1956 }
1957
1958 vty_event (VTY_SERV, sock, NULL);
1959 }
1960 while ((ainfo = ainfo->ai_next) != NULL);
1961
1962 freeaddrinfo (ainfo_save);
1963 }
1964
1965 #ifdef VTYSH
1966 /* For sockaddr_un. */
1967 #include <sys/un.h>
1968
1969 /* VTY shell UNIX domain socket. */
1970 static void
1971 vty_serv_un (const char *path)
1972 {
1973 int ret;
1974 int sock, len;
1975 struct sockaddr_un serv;
1976 mode_t old_mask;
1977 struct zprivs_ids_t ids;
1978
1979 /* First of all, unlink existing socket */
1980 unlink (path);
1981
1982 /* Set umask */
1983 old_mask = umask (0007);
1984
1985 /* Make UNIX domain socket. */
1986 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1987 if (sock < 0)
1988 {
1989 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
1990 return;
1991 }
1992
1993 /* Make server socket. */
1994 memset (&serv, 0, sizeof (struct sockaddr_un));
1995 serv.sun_family = AF_UNIX;
1996 strlcpy (serv.sun_path, path, sizeof (serv.sun_path));
1997 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
1998 len = serv.sun_len = SUN_LEN(&serv);
1999 #else
2000 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
2001 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
2002
2003 set_cloexec (sock);
2004
2005 ret = bind (sock, (struct sockaddr *) &serv, len);
2006 if (ret < 0)
2007 {
2008 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
2009 close (sock); /* Avoid sd leak. */
2010 return;
2011 }
2012
2013 ret = listen (sock, 5);
2014 if (ret < 0)
2015 {
2016 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
2017 close (sock); /* Avoid sd leak. */
2018 return;
2019 }
2020
2021 umask (old_mask);
2022
2023 zprivs_get_ids(&ids);
2024
2025 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
2026 earlier for the case when we don't need to chown the file
2027 type casting it here to make a compare */
2028 if ((int)ids.gid_vty > 0)
2029 {
2030 /* set group of socket */
2031 if ( chown (path, -1, ids.gid_vty) )
2032 {
2033 zlog_err ("vty_serv_un: could chown socket, %s",
2034 safe_strerror (errno) );
2035 }
2036 }
2037
2038 vty_event (VTYSH_SERV, sock, NULL);
2039 }
2040
2041 /* #define VTYSH_DEBUG 1 */
2042
2043 static int
2044 vtysh_accept (struct thread *thread)
2045 {
2046 int accept_sock;
2047 int sock;
2048 int client_len;
2049 struct sockaddr_un client;
2050 struct vty *vty;
2051
2052 accept_sock = THREAD_FD (thread);
2053
2054 vty_event (VTYSH_SERV, accept_sock, NULL);
2055
2056 memset (&client, 0, sizeof (struct sockaddr_un));
2057 client_len = sizeof (struct sockaddr_un);
2058
2059 sock = accept (accept_sock, (struct sockaddr *) &client,
2060 (socklen_t *) &client_len);
2061
2062 if (sock < 0)
2063 {
2064 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
2065 return -1;
2066 }
2067
2068 if (set_nonblocking(sock) < 0)
2069 {
2070 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2071 " %s, closing", sock, safe_strerror (errno));
2072 close (sock);
2073 return -1;
2074 }
2075 set_cloexec(sock);
2076
2077 #ifdef VTYSH_DEBUG
2078 printf ("VTY shell accept\n");
2079 #endif /* VTYSH_DEBUG */
2080
2081 vty = vty_new ();
2082 vty->fd = sock;
2083 vty->wfd = sock;
2084 vty->type = VTY_SHELL_SERV;
2085 vty->node = VIEW_NODE;
2086
2087 vty_event (VTYSH_READ, sock, vty);
2088
2089 return 0;
2090 }
2091
2092 static int
2093 vtysh_flush(struct vty *vty)
2094 {
2095 switch (buffer_flush_available(vty->obuf, vty->wfd))
2096 {
2097 case BUFFER_PENDING:
2098 vty_event(VTYSH_WRITE, vty->wfd, vty);
2099 break;
2100 case BUFFER_ERROR:
2101 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2102 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2103 buffer_reset(vty->obuf);
2104 vty_close(vty);
2105 return -1;
2106 break;
2107 case BUFFER_EMPTY:
2108 break;
2109 }
2110 return 0;
2111 }
2112
2113 static int
2114 vtysh_read (struct thread *thread)
2115 {
2116 int ret;
2117 int sock;
2118 int nbytes;
2119 struct vty *vty;
2120 unsigned char buf[VTY_READ_BUFSIZ];
2121 unsigned char *p;
2122 u_char header[4] = {0, 0, 0, 0};
2123
2124 sock = THREAD_FD (thread);
2125 vty = THREAD_ARG (thread);
2126 vty->t_read = NULL;
2127
2128 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
2129 {
2130 if (nbytes < 0)
2131 {
2132 if (ERRNO_IO_RETRY(errno))
2133 {
2134 vty_event (VTYSH_READ, sock, vty);
2135 return 0;
2136 }
2137 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
2138 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2139 __func__, sock, safe_strerror(errno));
2140 }
2141 buffer_reset(vty->obuf);
2142 vty_close (vty);
2143 #ifdef VTYSH_DEBUG
2144 printf ("close vtysh\n");
2145 #endif /* VTYSH_DEBUG */
2146 return 0;
2147 }
2148
2149 #ifdef VTYSH_DEBUG
2150 printf ("line: %.*s\n", nbytes, buf);
2151 #endif /* VTYSH_DEBUG */
2152
2153 if (vty->length + nbytes >= VTY_BUFSIZ)
2154 {
2155 /* Clear command line buffer. */
2156 vty->cp = vty->length = 0;
2157 vty_clear_buf (vty);
2158 vty_out (vty, "%% Command is too long.%s", VTY_NEWLINE);
2159 }
2160 else
2161 {
2162 for (p = buf; p < buf+nbytes; p++)
2163 {
2164 vty->buf[vty->length++] = *p;
2165 if (*p == '\0')
2166 {
2167 /* Pass this line to parser. */
2168 ret = vty_execute (vty);
2169 /* Note that vty_execute clears the command buffer and resets
2170 vty->length to 0. */
2171
2172 /* Return result. */
2173 #ifdef VTYSH_DEBUG
2174 printf ("result: %d\n", ret);
2175 printf ("vtysh node: %d\n", vty->node);
2176 #endif /* VTYSH_DEBUG */
2177
2178 /* hack for asynchronous "write integrated"
2179 * - other commands in "buf" will be ditched
2180 * - input during pending config-write is "unsupported" */
2181 if (ret == CMD_SUSPEND)
2182 break;
2183
2184 /* warning: watchquagga hardcodes this result write */
2185 header[3] = ret;
2186 buffer_put(vty->obuf, header, 4);
2187
2188 if (!vty->t_write && (vtysh_flush(vty) < 0))
2189 /* Try to flush results; exit if a write error occurs. */
2190 return 0;
2191 }
2192 }
2193 }
2194
2195 vty_event (VTYSH_READ, sock, vty);
2196
2197 return 0;
2198 }
2199
2200 static int
2201 vtysh_write (struct thread *thread)
2202 {
2203 struct vty *vty = THREAD_ARG (thread);
2204
2205 vty->t_write = NULL;
2206 vtysh_flush(vty);
2207 return 0;
2208 }
2209
2210 #endif /* VTYSH */
2211
2212 /* Determine address family to bind. */
2213 void
2214 vty_serv_sock (const char *addr, unsigned short port, const char *path)
2215 {
2216 /* If port is set to 0, do not listen on TCP/IP at all! */
2217 if (port)
2218 vty_serv_sock_addrinfo (addr, port);
2219
2220 #ifdef VTYSH
2221 vty_serv_un (path);
2222 #endif /* VTYSH */
2223 }
2224
2225 /* Close vty interface. Warning: call this only from functions that
2226 will be careful not to access the vty afterwards (since it has
2227 now been freed). This is safest from top-level functions (called
2228 directly by the thread dispatcher). */
2229 void
2230 vty_close (struct vty *vty)
2231 {
2232 int i;
2233 bool was_stdio = false;
2234
2235 /* Cancel threads.*/
2236 if (vty->t_read)
2237 thread_cancel (vty->t_read);
2238 if (vty->t_write)
2239 thread_cancel (vty->t_write);
2240 if (vty->t_timeout)
2241 thread_cancel (vty->t_timeout);
2242
2243 /* Flush buffer. */
2244 buffer_flush_all (vty->obuf, vty->wfd);
2245
2246 /* Free input buffer. */
2247 buffer_free (vty->obuf);
2248
2249 /* Free command history. */
2250 for (i = 0; i < VTY_MAXHIST; i++)
2251 if (vty->hist[i])
2252 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2253
2254 /* Unset vector. */
2255 vector_unset (vtyvec, vty->fd);
2256
2257 if (vty->wfd > 0 && vty->type == VTY_FILE)
2258 fsync (vty->wfd);
2259
2260 /* Close socket. */
2261 if (vty->fd > 0)
2262 {
2263 close (vty->fd);
2264 if (vty->wfd > 0 && vty->wfd != vty->fd)
2265 close (vty->wfd);
2266 }
2267 else
2268 was_stdio = true;
2269
2270 if (vty->buf)
2271 XFREE (MTYPE_VTY, vty->buf);
2272
2273 if (vty->error_buf)
2274 XFREE (MTYPE_VTY, vty->error_buf);
2275
2276 /* Check configure. */
2277 vty_config_unlock (vty);
2278
2279 /* OK free vty. */
2280 XFREE (MTYPE_VTY, vty);
2281
2282 if (was_stdio)
2283 vty_stdio_reset ();
2284 }
2285
2286 /* When time out occur output message then close connection. */
2287 static int
2288 vty_timeout (struct thread *thread)
2289 {
2290 struct vty *vty;
2291
2292 vty = THREAD_ARG (thread);
2293 vty->t_timeout = NULL;
2294 vty->v_timeout = 0;
2295
2296 /* Clear buffer*/
2297 buffer_reset (vty->obuf);
2298 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2299
2300 /* Close connection. */
2301 vty->status = VTY_CLOSE;
2302 vty_close (vty);
2303
2304 return 0;
2305 }
2306
2307 /* Read up configuration file from file_name. */
2308 static void
2309 vty_read_file (FILE *confp)
2310 {
2311 int ret;
2312 struct vty *vty;
2313 unsigned int line_num = 0;
2314
2315 vty = vty_new ();
2316 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2317 if (vty->wfd < 0)
2318 {
2319 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
2320 vty->wfd = STDOUT_FILENO;
2321 }
2322 vty->fd = STDIN_FILENO;
2323 vty->type = VTY_FILE;
2324 vty->node = CONFIG_NODE;
2325
2326 /* Execute configuration file */
2327 ret = config_from_file (vty, confp, &line_num);
2328
2329 /* Flush any previous errors before printing messages below */
2330 buffer_flush_all (vty->obuf, vty->fd);
2331
2332 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
2333 {
2334 const char *message = NULL;
2335 switch (ret)
2336 {
2337 case CMD_ERR_AMBIGUOUS:
2338 message = "*** Error reading config: Ambiguous command.";
2339 break;
2340 case CMD_ERR_NO_MATCH:
2341 message = "*** Error reading config: There is no such command.";
2342 break;
2343 }
2344 fprintf (stderr, "%s\n", message);
2345 zlog_err ("%s", message);
2346 fprintf (stderr, "*** Error occurred processing line %u, below:\n%s\n",
2347 line_num, vty->error_buf);
2348 zlog_err ("*** Error occurred processing line %u, below:\n%s",
2349 line_num, vty->error_buf);
2350 }
2351
2352 vty_close (vty);
2353 }
2354
2355 static FILE *
2356 vty_use_backup_config (const char *fullpath)
2357 {
2358 char *fullpath_sav, *fullpath_tmp;
2359 FILE *ret = NULL;
2360 int tmp, sav;
2361 int c;
2362 char buffer[512];
2363
2364 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2365 strcpy (fullpath_sav, fullpath);
2366 strcat (fullpath_sav, CONF_BACKUP_EXT);
2367
2368 sav = open (fullpath_sav, O_RDONLY);
2369 if (sav < 0)
2370 {
2371 free (fullpath_sav);
2372 return NULL;
2373 }
2374
2375 fullpath_tmp = malloc (strlen (fullpath) + 8);
2376 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2377
2378 /* Open file to configuration write. */
2379 tmp = mkstemp (fullpath_tmp);
2380 if (tmp < 0)
2381 goto out_close_sav;
2382
2383 if (fchmod (tmp, CONFIGFILE_MASK) != 0)
2384 goto out_close;
2385
2386 while((c = read (sav, buffer, 512)) > 0)
2387 {
2388 if (write (tmp, buffer, c) <= 0)
2389 goto out_close;
2390 }
2391 close (sav);
2392 close (tmp);
2393
2394 if (rename (fullpath_tmp, fullpath) == 0)
2395 ret = fopen (fullpath, "r");
2396 else
2397 unlink (fullpath_tmp);
2398
2399 if (0)
2400 {
2401 out_close:
2402 close (tmp);
2403 unlink (fullpath_tmp);
2404 out_close_sav:
2405 close (sav);
2406 }
2407
2408 free (fullpath_sav);
2409 free (fullpath_tmp);
2410 return ret;
2411 }
2412
2413 /* Read up configuration file from file_name. */
2414 void
2415 vty_read_config (const char *config_file,
2416 char *config_default_dir)
2417 {
2418 char cwd[MAXPATHLEN];
2419 FILE *confp = NULL;
2420 const char *fullpath;
2421 char *tmp = NULL;
2422
2423 /* If -f flag specified. */
2424 if (config_file != NULL)
2425 {
2426 if (! IS_DIRECTORY_SEP (config_file[0]))
2427 {
2428 if (getcwd (cwd, MAXPATHLEN) == NULL)
2429 {
2430 fprintf (stderr, "Failure to determine Current Working Directory %d!\n", errno);
2431 exit (1);
2432 }
2433 tmp = XMALLOC (MTYPE_TMP,
2434 strlen (cwd) + strlen (config_file) + 2);
2435 sprintf (tmp, "%s/%s", cwd, config_file);
2436 fullpath = tmp;
2437 }
2438 else
2439 fullpath = config_file;
2440
2441 confp = fopen (fullpath, "r");
2442
2443 if (confp == NULL)
2444 {
2445 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2446 __func__, fullpath, safe_strerror (errno));
2447
2448 confp = vty_use_backup_config (fullpath);
2449 if (confp)
2450 fprintf (stderr, "WARNING: using backup configuration file!\n");
2451 else
2452 {
2453 fprintf (stderr, "can't open configuration file [%s]\n",
2454 config_file);
2455 exit(1);
2456 }
2457 }
2458 }
2459 else
2460 {
2461
2462 host_config_set (config_default_dir);
2463
2464 #ifdef VTYSH
2465 int ret;
2466 struct stat conf_stat;
2467
2468 /* !!!!PLEASE LEAVE!!!!
2469 * This is NEEDED for use with vtysh -b, or else you can get
2470 * a real configuration food fight with a lot garbage in the
2471 * merged configuration file it creates coming from the per
2472 * daemon configuration files. This also allows the daemons
2473 * to start if there default configuration file is not
2474 * present or ignore them, as needed when using vtysh -b to
2475 * configure the daemons at boot - MAG
2476 */
2477
2478 /* Stat for vtysh Zebra.conf, if found startup and wait for
2479 * boot configuration
2480 */
2481
2482 if ( strstr(config_default_dir, "vtysh") == NULL)
2483 {
2484 ret = stat (integrate_default, &conf_stat);
2485 if (ret >= 0)
2486 goto tmp_free_and_out;
2487 }
2488 #endif /* VTYSH */
2489 confp = fopen (config_default_dir, "r");
2490 if (confp == NULL)
2491 {
2492 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2493 __func__, config_default_dir, safe_strerror (errno));
2494
2495 confp = vty_use_backup_config (config_default_dir);
2496 if (confp)
2497 {
2498 fprintf (stderr, "WARNING: using backup configuration file!\n");
2499 fullpath = config_default_dir;
2500 }
2501 else
2502 {
2503 fprintf (stderr, "can't open configuration file [%s]\n",
2504 config_default_dir);
2505 goto tmp_free_and_out;
2506 }
2507 }
2508 else
2509 fullpath = config_default_dir;
2510 }
2511
2512 vty_read_file (confp);
2513
2514 fclose (confp);
2515
2516 host_config_set (fullpath);
2517
2518 tmp_free_and_out:
2519 if (tmp)
2520 XFREE (MTYPE_TMP, tmp);
2521 }
2522
2523 /* Small utility function which output log to the VTY. */
2524 void
2525 vty_log (const char *level, const char *proto_str,
2526 const char *format, struct timestamp_control *ctl, va_list va)
2527 {
2528 unsigned int i;
2529 struct vty *vty;
2530
2531 if (!vtyvec)
2532 return;
2533
2534 for (i = 0; i < vector_active (vtyvec); i++)
2535 if ((vty = vector_slot (vtyvec, i)) != NULL)
2536 if (vty->monitor)
2537 {
2538 va_list ac;
2539 va_copy(ac, va);
2540 vty_log_out (vty, level, proto_str, format, ctl, ac);
2541 va_end(ac);
2542 }
2543 }
2544
2545 /* Async-signal-safe version of vty_log for fixed strings. */
2546 void
2547 vty_log_fixed (char *buf, size_t len)
2548 {
2549 unsigned int i;
2550 struct iovec iov[2];
2551 char crlf[4] = "\r\n";
2552
2553 /* vty may not have been initialised */
2554 if (!vtyvec)
2555 return;
2556
2557 iov[0].iov_base = buf;
2558 iov[0].iov_len = len;
2559 iov[1].iov_base = crlf;
2560 iov[1].iov_len = 2;
2561
2562 for (i = 0; i < vector_active (vtyvec); i++)
2563 {
2564 struct vty *vty;
2565 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2566 /* N.B. We don't care about the return code, since process is
2567 most likely just about to die anyway. */
2568 if (writev(vty->wfd, iov, 2) == -1)
2569 {
2570 fprintf(stderr, "Failure to writev: %d\n", errno);
2571 exit(-1);
2572 }
2573 }
2574 }
2575
2576 int
2577 vty_config_lock (struct vty *vty)
2578 {
2579 if (vty_config_is_lockless)
2580 return 1;
2581 if (vty_config == 0)
2582 {
2583 vty->config = 1;
2584 vty_config = 1;
2585 }
2586 return vty->config;
2587 }
2588
2589 int
2590 vty_config_unlock (struct vty *vty)
2591 {
2592 if (vty_config_is_lockless)
2593 return 0;
2594 if (vty_config == 1 && vty->config == 1)
2595 {
2596 vty->config = 0;
2597 vty_config = 0;
2598 }
2599 return vty->config;
2600 }
2601
2602 void
2603 vty_config_lockless (void)
2604 {
2605 vty_config_is_lockless = 1;
2606 }
2607
2608 /* Master of the threads. */
2609 static struct thread_master *vty_master;
2610
2611 static void
2612 vty_event (enum event event, int sock, struct vty *vty)
2613 {
2614 struct thread *vty_serv_thread;
2615
2616 switch (event)
2617 {
2618 case VTY_SERV:
2619 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
2620 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2621 break;
2622 #ifdef VTYSH
2623 case VTYSH_SERV:
2624 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
2625 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2626 break;
2627 case VTYSH_READ:
2628 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
2629 break;
2630 case VTYSH_WRITE:
2631 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
2632 break;
2633 #endif /* VTYSH */
2634 case VTY_READ:
2635 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
2636
2637 /* Time out treatment. */
2638 if (vty->v_timeout)
2639 {
2640 if (vty->t_timeout)
2641 thread_cancel (vty->t_timeout);
2642 vty->t_timeout =
2643 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2644 }
2645 break;
2646 case VTY_WRITE:
2647 if (! vty->t_write)
2648 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
2649 break;
2650 case VTY_TIMEOUT_RESET:
2651 if (vty->t_timeout)
2652 {
2653 thread_cancel (vty->t_timeout);
2654 vty->t_timeout = NULL;
2655 }
2656 if (vty->v_timeout)
2657 {
2658 vty->t_timeout =
2659 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
2660 }
2661 break;
2662 }
2663 }
2664
2665 DEFUN_NOSH (config_who,
2666 config_who_cmd,
2667 "who",
2668 "Display who is on vty\n")
2669 {
2670 unsigned int i;
2671 struct vty *v;
2672
2673 for (i = 0; i < vector_active (vtyvec); i++)
2674 if ((v = vector_slot (vtyvec, i)) != NULL)
2675 vty_out (vty, "%svty[%d] connected from %s.%s",
2676 v->config ? "*" : " ",
2677 i, v->address, VTY_NEWLINE);
2678 return CMD_SUCCESS;
2679 }
2680
2681 /* Move to vty configuration mode. */
2682 DEFUN_NOSH (line_vty,
2683 line_vty_cmd,
2684 "line vty",
2685 "Configure a terminal line\n"
2686 "Virtual terminal\n")
2687 {
2688 vty->node = VTY_NODE;
2689 return CMD_SUCCESS;
2690 }
2691
2692 /* Set time out value. */
2693 static int
2694 exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
2695 {
2696 unsigned long timeout = 0;
2697
2698 /* min_str and sec_str are already checked by parser. So it must be
2699 all digit string. */
2700 if (min_str)
2701 {
2702 timeout = strtol (min_str, NULL, 10);
2703 timeout *= 60;
2704 }
2705 if (sec_str)
2706 timeout += strtol (sec_str, NULL, 10);
2707
2708 vty_timeout_val = timeout;
2709 vty->v_timeout = timeout;
2710 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2711
2712
2713 return CMD_SUCCESS;
2714 }
2715
2716 DEFUN (exec_timeout_min,
2717 exec_timeout_min_cmd,
2718 "exec-timeout (0-35791)",
2719 "Set timeout value\n"
2720 "Timeout value in minutes\n")
2721 {
2722 int idx_number = 1;
2723 return exec_timeout (vty, argv[idx_number]->arg, NULL);
2724 }
2725
2726 DEFUN (exec_timeout_sec,
2727 exec_timeout_sec_cmd,
2728 "exec-timeout (0-35791) (0-2147483)",
2729 "Set the EXEC timeout\n"
2730 "Timeout in minutes\n"
2731 "Timeout in seconds\n")
2732 {
2733 int idx_number = 1;
2734 int idx_number_2 = 2;
2735 return exec_timeout (vty, argv[idx_number]->arg, argv[idx_number_2]->arg);
2736 }
2737
2738 DEFUN (no_exec_timeout,
2739 no_exec_timeout_cmd,
2740 "no exec-timeout",
2741 NO_STR
2742 "Set the EXEC timeout\n")
2743 {
2744 return exec_timeout (vty, NULL, NULL);
2745 }
2746
2747 /* Set vty access class. */
2748 DEFUN (vty_access_class,
2749 vty_access_class_cmd,
2750 "access-class WORD",
2751 "Filter connections based on an IP access list\n"
2752 "IP access list\n")
2753 {
2754 int idx_word = 1;
2755 if (vty_accesslist_name)
2756 XFREE(MTYPE_VTY, vty_accesslist_name);
2757
2758 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
2759
2760 return CMD_SUCCESS;
2761 }
2762
2763 /* Clear vty access class. */
2764 DEFUN (no_vty_access_class,
2765 no_vty_access_class_cmd,
2766 "no access-class [WORD]",
2767 NO_STR
2768 "Filter connections based on an IP access list\n"
2769 "IP access list\n")
2770 {
2771 int idx_word = 2;
2772 const char *accesslist = (argc == 3) ? argv[idx_word]->arg : NULL;
2773 if (! vty_accesslist_name || (argc == 3 && strcmp(vty_accesslist_name, accesslist)))
2774 {
2775 vty_out (vty, "Access-class is not currently applied to vty%s",
2776 VTY_NEWLINE);
2777 return CMD_WARNING;
2778 }
2779
2780 XFREE(MTYPE_VTY, vty_accesslist_name);
2781
2782 vty_accesslist_name = NULL;
2783
2784 return CMD_SUCCESS;
2785 }
2786
2787 /* Set vty access class. */
2788 DEFUN (vty_ipv6_access_class,
2789 vty_ipv6_access_class_cmd,
2790 "ipv6 access-class WORD",
2791 IPV6_STR
2792 "Filter connections based on an IP access list\n"
2793 "IPv6 access list\n")
2794 {
2795 int idx_word = 2;
2796 if (vty_ipv6_accesslist_name)
2797 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2798
2799 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
2800
2801 return CMD_SUCCESS;
2802 }
2803
2804 /* Clear vty access class. */
2805 DEFUN (no_vty_ipv6_access_class,
2806 no_vty_ipv6_access_class_cmd,
2807 "no ipv6 access-class [WORD]",
2808 NO_STR
2809 IPV6_STR
2810 "Filter connections based on an IP access list\n"
2811 "IPv6 access list\n")
2812 {
2813 int idx_word = 3;
2814 const char *accesslist = (argc == 4) ? argv[idx_word]->arg : NULL;
2815
2816 if (! vty_ipv6_accesslist_name ||
2817 (argc == 4 && strcmp(vty_ipv6_accesslist_name, accesslist)))
2818 {
2819 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2820 VTY_NEWLINE);
2821 return CMD_WARNING;
2822 }
2823
2824 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2825
2826 vty_ipv6_accesslist_name = NULL;
2827
2828 return CMD_SUCCESS;
2829 }
2830
2831 /* vty login. */
2832 DEFUN (vty_login,
2833 vty_login_cmd,
2834 "login",
2835 "Enable password checking\n")
2836 {
2837 no_password_check = 0;
2838 return CMD_SUCCESS;
2839 }
2840
2841 DEFUN (no_vty_login,
2842 no_vty_login_cmd,
2843 "no login",
2844 NO_STR
2845 "Enable password checking\n")
2846 {
2847 no_password_check = 1;
2848 return CMD_SUCCESS;
2849 }
2850
2851 DEFUN (service_advanced_vty,
2852 service_advanced_vty_cmd,
2853 "service advanced-vty",
2854 "Set up miscellaneous service\n"
2855 "Enable advanced mode vty interface\n")
2856 {
2857 host.advanced = 1;
2858 return CMD_SUCCESS;
2859 }
2860
2861 DEFUN (no_service_advanced_vty,
2862 no_service_advanced_vty_cmd,
2863 "no service advanced-vty",
2864 NO_STR
2865 "Set up miscellaneous service\n"
2866 "Enable advanced mode vty interface\n")
2867 {
2868 host.advanced = 0;
2869 return CMD_SUCCESS;
2870 }
2871
2872 DEFUN_NOSH (terminal_monitor,
2873 terminal_monitor_cmd,
2874 "terminal monitor",
2875 "Set terminal line parameters\n"
2876 "Copy debug output to the current terminal line\n")
2877 {
2878 vty->monitor = 1;
2879 return CMD_SUCCESS;
2880 }
2881
2882 DEFUN_NOSH (terminal_no_monitor,
2883 terminal_no_monitor_cmd,
2884 "terminal no monitor",
2885 "Set terminal line parameters\n"
2886 NO_STR
2887 "Copy debug output to the current terminal line\n")
2888 {
2889 vty->monitor = 0;
2890 return CMD_SUCCESS;
2891 }
2892
2893 DEFUN_NOSH (no_terminal_monitor,
2894 no_terminal_monitor_cmd,
2895 "no terminal monitor",
2896 NO_STR
2897 "Set terminal line parameters\n"
2898 "Copy debug output to the current terminal line\n")
2899 {
2900 return terminal_no_monitor (self, vty, argc, argv);
2901 }
2902
2903
2904 DEFUN_NOSH (show_history,
2905 show_history_cmd,
2906 "show history",
2907 SHOW_STR
2908 "Display the session command history\n")
2909 {
2910 int index;
2911
2912 for (index = vty->hindex + 1; index != vty->hindex;)
2913 {
2914 if (index == VTY_MAXHIST)
2915 {
2916 index = 0;
2917 continue;
2918 }
2919
2920 if (vty->hist[index] != NULL)
2921 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2922
2923 index++;
2924 }
2925
2926 return CMD_SUCCESS;
2927 }
2928
2929 /* vty login. */
2930 DEFUN (log_commands,
2931 log_commands_cmd,
2932 "log commands",
2933 "Logging control\n"
2934 "Log all commands (can't be unset without restart)\n")
2935 {
2936 do_log_commands = 1;
2937 return CMD_SUCCESS;
2938 }
2939
2940 /* Display current configuration. */
2941 static int
2942 vty_config_write (struct vty *vty)
2943 {
2944 vty_out (vty, "line vty%s", VTY_NEWLINE);
2945
2946 if (vty_accesslist_name)
2947 vty_out (vty, " access-class %s%s",
2948 vty_accesslist_name, VTY_NEWLINE);
2949
2950 if (vty_ipv6_accesslist_name)
2951 vty_out (vty, " ipv6 access-class %s%s",
2952 vty_ipv6_accesslist_name, VTY_NEWLINE);
2953
2954 /* exec-timeout */
2955 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2956 vty_out (vty, " exec-timeout %ld %ld%s",
2957 vty_timeout_val / 60,
2958 vty_timeout_val % 60, VTY_NEWLINE);
2959
2960 /* login */
2961 if (no_password_check)
2962 vty_out (vty, " no login%s", VTY_NEWLINE);
2963
2964 if (do_log_commands)
2965 vty_out (vty, "log commands%s", VTY_NEWLINE);
2966
2967 vty_out (vty, "!%s", VTY_NEWLINE);
2968
2969 return CMD_SUCCESS;
2970 }
2971
2972 struct cmd_node vty_node =
2973 {
2974 VTY_NODE,
2975 "%s(config-line)# ",
2976 1,
2977 };
2978
2979 /* Reset all VTY status. */
2980 void
2981 vty_reset ()
2982 {
2983 unsigned int i;
2984 struct vty *vty;
2985 struct thread *vty_serv_thread;
2986
2987 for (i = 0; i < vector_active (vtyvec); i++)
2988 if ((vty = vector_slot (vtyvec, i)) != NULL)
2989 {
2990 buffer_reset (vty->obuf);
2991 vty->status = VTY_CLOSE;
2992 vty_close (vty);
2993 }
2994
2995 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
2996 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2997 {
2998 thread_cancel (vty_serv_thread);
2999 vector_slot (Vvty_serv_thread, i) = NULL;
3000 close (i);
3001 }
3002
3003 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3004
3005 if (vty_accesslist_name)
3006 {
3007 XFREE(MTYPE_VTY, vty_accesslist_name);
3008 vty_accesslist_name = NULL;
3009 }
3010
3011 if (vty_ipv6_accesslist_name)
3012 {
3013 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
3014 vty_ipv6_accesslist_name = NULL;
3015 }
3016 }
3017
3018 static void
3019 vty_save_cwd (void)
3020 {
3021 char cwd[MAXPATHLEN];
3022 char *c;
3023
3024 c = getcwd (cwd, MAXPATHLEN);
3025
3026 if (!c)
3027 {
3028 /*
3029 * At this point if these go wrong, more than likely
3030 * the whole world is coming down around us
3031 * Hence not worrying about it too much.
3032 */
3033 if (!chdir (SYSCONFDIR))
3034 {
3035 fprintf(stderr, "Failure to chdir to %s, errno: %d\n", SYSCONFDIR, errno);
3036 exit(-1);
3037 }
3038 if (getcwd (cwd, MAXPATHLEN) == NULL)
3039 {
3040 fprintf(stderr, "Failure to getcwd, errno: %d\n", errno);
3041 exit(-1);
3042 }
3043 }
3044
3045 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3046 strcpy (vty_cwd, cwd);
3047 }
3048
3049 char *
3050 vty_get_cwd ()
3051 {
3052 return vty_cwd;
3053 }
3054
3055 int
3056 vty_shell (struct vty *vty)
3057 {
3058 return vty->type == VTY_SHELL ? 1 : 0;
3059 }
3060
3061 int
3062 vty_shell_serv (struct vty *vty)
3063 {
3064 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3065 }
3066
3067 void
3068 vty_init_vtysh ()
3069 {
3070 vtyvec = vector_init (VECTOR_MIN_SIZE);
3071 }
3072
3073 /* Install vty's own commands like `who' command. */
3074 void
3075 vty_init (struct thread_master *master_thread)
3076 {
3077 /* For further configuration read, preserve current directory. */
3078 vty_save_cwd ();
3079
3080 vtyvec = vector_init (VECTOR_MIN_SIZE);
3081
3082 vty_master = master_thread;
3083
3084 atexit (vty_stdio_reset);
3085
3086 /* Initilize server thread vector. */
3087 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3088
3089 /* Install bgp top node. */
3090 install_node (&vty_node, vty_config_write);
3091
3092 install_element (VIEW_NODE, &config_who_cmd);
3093 install_element (VIEW_NODE, &show_history_cmd);
3094 install_element (CONFIG_NODE, &line_vty_cmd);
3095 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3096 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3097 install_element (CONFIG_NODE, &show_history_cmd);
3098 install_element (CONFIG_NODE, &log_commands_cmd);
3099 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3100 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
3101 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
3102
3103 install_default (VTY_NODE);
3104 install_element (VTY_NODE, &exec_timeout_min_cmd);
3105 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3106 install_element (VTY_NODE, &no_exec_timeout_cmd);
3107 install_element (VTY_NODE, &vty_access_class_cmd);
3108 install_element (VTY_NODE, &no_vty_access_class_cmd);
3109 install_element (VTY_NODE, &vty_login_cmd);
3110 install_element (VTY_NODE, &no_vty_login_cmd);
3111 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3112 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3113 }
3114
3115 void
3116 vty_terminate (void)
3117 {
3118 if (vty_cwd)
3119 XFREE (MTYPE_TMP, vty_cwd);
3120
3121 if (vtyvec && Vvty_serv_thread)
3122 {
3123 vty_reset ();
3124 vector_free (vtyvec);
3125 vector_free (Vvty_serv_thread);
3126 vtyvec = NULL;
3127 Vvty_serv_thread = NULL;
3128 }
3129 }
3130
3131 /* Utility functions to get arguments from commands generated
3132 by the xml2cli.pl script. */
3133 const char *
3134 vty_get_arg_value (struct vty_arg *args[], const char *arg)
3135 {
3136 while (*args)
3137 {
3138 if (strcmp ((*args)->name, arg) == 0)
3139 return (*args)->value;
3140 args++;
3141 }
3142 return NULL;
3143 }
3144
3145 struct vty_arg *
3146 vty_get_arg (struct vty_arg *args[], const char *arg)
3147 {
3148 while (*args)
3149 {
3150 if (strcmp ((*args)->name, arg) == 0)
3151 return *args;
3152 args++;
3153 }
3154 return NULL;
3155 }