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