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