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