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