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