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