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