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