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