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