blob: ff013cf61b8237f186f21834359620bc484833ee [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
paulb21b19c2003-06-15 01:28:29 +000026#include "thread.h"
paul718e3742002-12-13 20:15:29 +000027#include "buffer.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "command.h"
30#include "sockunion.h"
paul718e3742002-12-13 20:15:29 +000031#include "memory.h"
32#include "str.h"
33#include "log.h"
34#include "prefix.h"
35#include "filter.h"
paulb21b19c2003-06-15 01:28:29 +000036#include "vty.h"
pauledd7c242003-06-04 13:59:38 +000037#include "privs.h"
ajs9fc7ebf2005-02-23 15:12:34 +000038#include "network.h"
39
40#include <arpa/telnet.h>
David Lamparterba53a8f2015-05-05 11:04:46 +020041#include <termios.h>
paul718e3742002-12-13 20:15:29 +000042
43/* Vty events */
44enum event
45{
46 VTY_SERV,
47 VTY_READ,
48 VTY_WRITE,
49 VTY_TIMEOUT_RESET,
50#ifdef VTYSH
51 VTYSH_SERV,
ajs49ff6d92004-11-04 19:26:16 +000052 VTYSH_READ,
53 VTYSH_WRITE
paul718e3742002-12-13 20:15:29 +000054#endif /* VTYSH */
55};
56
57static void vty_event (enum event, int, struct vty *);
58
59/* Extern host structure from command.c */
60extern struct host host;
David Lamparter6b0655a2014-06-04 06:53:35 +020061
paul718e3742002-12-13 20:15:29 +000062/* Vector which store each vty structure. */
63static vector vtyvec;
64
65/* Vty timeout value. */
66static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
67
68/* Vty access-class command */
69static char *vty_accesslist_name = NULL;
70
71/* Vty access-calss for IPv6. */
72static char *vty_ipv6_accesslist_name = NULL;
73
74/* VTY server thread. */
Christian Franke677bcbb2013-02-27 13:47:23 +000075static vector Vvty_serv_thread;
paul718e3742002-12-13 20:15:29 +000076
77/* Current directory. */
78char *vty_cwd = NULL;
79
80/* Configure lock. */
81static int vty_config;
82
83/* Login password check. */
84static int no_password_check = 0;
85
Paul Jakma62687ff2008-08-23 14:27:06 +010086/* Restrict unauthenticated logins? */
87static const u_char restricted_mode_default = 0;
88static u_char restricted_mode = 0;
89
paul718e3742002-12-13 20:15:29 +000090/* Integrated configuration file path */
91char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
92
Lou Berger86b2a0a2016-05-17 12:19:51 -040093static int do_log_commands = 0;
David Lamparter6b0655a2014-06-04 06:53:35 +020094
paul718e3742002-12-13 20:15:29 +000095/* 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;
paul718e3742002-12-13 20:15:29 +0000104
105 if (vty_shell (vty))
ajsd246bd92004-11-23 17:35:08 +0000106 {
107 va_start (args, format);
108 vprintf (format, args);
109 va_end (args);
110 }
paul718e3742002-12-13 20:15:29 +0000111 else
112 {
113 /* Try to write to initial buffer. */
ajsd246bd92004-11-23 17:35:08 +0000114 va_start (args, format);
Lou Bergerc7f7e492016-01-12 13:41:49 -0500115 len = vsnprintf (buf, sizeof(buf), format, args);
ajsd246bd92004-11-23 17:35:08 +0000116 va_end (args);
paul718e3742002-12-13 20:15:29 +0000117
118 /* Initial buffer is not enough. */
119 if (len < 0 || len >= size)
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
ajsd246bd92004-11-23 17:35:08 +0000132 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000133 len = vsnprintf (p, size, format, args);
ajsd246bd92004-11-23 17:35:08 +0000134 va_end (args);
paul718e3742002-12-13 20:15:29 +0000135
136 if (len > -1 && len < size)
137 break;
138 }
139 }
140
141 /* When initial buffer is enough to store all output. */
142 if (! p)
143 p = buf;
144
145 /* Pointer p must point out buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +0000146 buffer_put (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000147
148 /* If p is not different with buf, it is allocated buffer. */
149 if (p != buf)
150 XFREE (MTYPE_VTY_OUT_BUF, p);
151 }
152
paul718e3742002-12-13 20:15:29 +0000153 return len;
154}
155
ajsd246bd92004-11-23 17:35:08 +0000156static int
ajs274a4a42004-12-07 15:39:31 +0000157vty_log_out (struct vty *vty, const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000158 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +0000159{
ajs9fc7ebf2005-02-23 15:12:34 +0000160 int ret;
paul718e3742002-12-13 20:15:29 +0000161 int len;
162 char buf[1024];
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000163
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000164 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';
paul718e3742002-12-13 20:15:29 +0000174
ajs274a4a42004-12-07 15:39:31 +0000175 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000176 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000177 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000178 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
179 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000180 return -1;
paul718e3742002-12-13 20:15:29 +0000181
ajs9fc7ebf2005-02-23 15:12:34 +0000182 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
183 ((size_t)((len += ret)+2) > sizeof(buf)))
184 return -1;
paul718e3742002-12-13 20:15:29 +0000185
ajs9fc7ebf2005-02-23 15:12:34 +0000186 buf[len++] = '\r';
187 buf[len++] = '\n';
188
David Lamparter4715a532013-05-30 16:31:49 +0200189 if (write(vty->wfd, buf, len) < 0)
ajs9fc7ebf2005-02-23 15:12:34 +0000190 {
191 if (ERRNO_IO_RETRY(errno))
192 /* Kernel buffer is full, probably too much debugging output, so just
193 drop the data and ignore. */
194 return -1;
195 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000196 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000197 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
198 __func__, vty->fd, safe_strerror(errno));
199 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000200 /* 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);
ajs9fc7ebf2005-02-23 15:12:34 +0000204 return -1;
205 }
206 return 0;
paul718e3742002-12-13 20:15:29 +0000207}
208
209/* Output current time to the vty. */
210void
211vty_time_print (struct vty *vty, int cr)
212{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000213 char buf [25];
paul718e3742002-12-13 20:15:29 +0000214
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000215 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
paul718e3742002-12-13 20:15:29 +0000216 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000217 zlog (NULL, LOG_INFO, "quagga_timestamp error");
paul718e3742002-12-13 20:15:29 +0000218 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{
paul3b0c5d92005-03-08 10:43:43 +0000232 if (host.motdfile)
233 {
234 FILE *f;
235 char buf[4096];
paul22085182005-03-08 16:00:12 +0000236
paul3b0c5d92005-03-08 10:43:43 +0000237 f = fopen (host.motdfile, "r");
238 if (f)
239 {
paulb45da6f2005-03-08 15:16:57 +0000240 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000241 {
paulb45da6f2005-03-08 15:16:57 +0000242 char *s;
paul22085182005-03-08 16:00:12 +0000243 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000244 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000245 s--);
246 *s = '\0';
247 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
248 }
paul3b0c5d92005-03-08 10:43:43 +0000249 fclose (f);
250 }
251 else
paulb45da6f2005-03-08 15:16:57 +0000252 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000253 }
254 else if (host.motd)
Nico Goldeb830c892010-08-01 15:24:35 +0200255 vty_out (vty, "%s", host.motd);
paul718e3742002-12-13 20:15:29 +0000256}
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)
269 {
270 uname (&names);
271 hostname = names.nodename;
272 }
273 vty_out (vty, cmd_prompt (vty->node), hostname);
274 }
275}
276
277/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000278static void
paul718e3742002-12-13 20:15:29 +0000279vty_will_echo (struct vty *vty)
280{
paul02ff83c2004-06-11 11:27:03 +0000281 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000282 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{
paul02ff83c2004-06-11 11:27:03 +0000289 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000290 vty_out (vty, "%s", cmd);
291}
292
293/* Make don't use linemode over telnet. */
294static void
295vty_dont_linemode (struct vty *vty)
296{
paul02ff83c2004-06-11 11:27:03 +0000297 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000298 vty_out (vty, "%s", cmd);
299}
300
301/* Use window size. */
302static void
303vty_do_window_size (struct vty *vty)
304{
paul02ff83c2004-06-11 11:27:03 +0000305 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000306 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{
paul02ff83c2004-06-11 11:27:03 +0000314 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000315 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
ajs9fc7ebf2005-02-23 15:12:34 +0000325 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000326 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
327 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000328
329 return new;
330}
331
332/* Authentication of vty */
333static void
334vty_auth (struct vty *vty, char *buf)
335{
336 char *passwd = NULL;
337 enum node_type next_node = 0;
338 int fail;
339 char *crypt (const char *, const char *);
340
341 switch (vty->node)
342 {
343 case AUTH_NODE:
344 if (host.encrypt)
345 passwd = host.password_encrypt;
346 else
347 passwd = host.password;
348 if (host.advanced)
349 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
350 else
351 next_node = VIEW_NODE;
352 break;
353 case AUTH_ENABLE_NODE:
354 if (host.encrypt)
355 passwd = host.enable_encrypt;
356 else
357 passwd = host.enable;
358 next_node = ENABLE_NODE;
359 break;
360 }
361
362 if (passwd)
363 {
364 if (host.encrypt)
365 fail = strcmp (crypt(buf, passwd), passwd);
366 else
367 fail = strcmp (buf, passwd);
368 }
369 else
370 fail = 1;
371
372 if (! fail)
373 {
374 vty->fail = 0;
375 vty->node = next_node; /* Success ! */
376 }
377 else
378 {
379 vty->fail++;
380 if (vty->fail >= 3)
381 {
382 if (vty->node == AUTH_NODE)
383 {
384 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
385 vty->status = VTY_CLOSE;
386 }
387 else
388 {
389 /* AUTH_ENABLE_NODE */
390 vty->fail = 0;
391 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +0100392 vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
paul718e3742002-12-13 20:15:29 +0000393 }
394 }
395 }
396}
397
398/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000399static int
paul718e3742002-12-13 20:15:29 +0000400vty_command (struct vty *vty, char *buf)
401{
402 int ret;
403 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000404 const char *protocolname;
Lou Berger86b2a0a2016-05-17 12:19:51 -0400405 char *cp = NULL;
paul718e3742002-12-13 20:15:29 +0000406
Lou Bergerc7f7e492016-01-12 13:41:49 -0500407 /*
408 * Log non empty command lines
409 */
Lou Berger86b2a0a2016-05-17 12:19:51 -0400410 if (do_log_commands)
411 cp = buf;
Lou Bergerc7f7e492016-01-12 13:41:49 -0500412 if (cp != NULL)
413 {
414 /* Skip white spaces. */
415 while (isspace ((int) *cp) && *cp != '\0')
416 cp++;
417 }
418 if (cp != NULL && *cp != '\0')
419 {
420 unsigned i;
421 char vty_str[VTY_BUFSIZ];
422 char prompt_str[VTY_BUFSIZ];
423
424 /* format the base vty info */
425 snprintf(vty_str, sizeof(vty_str), "vty[??]@%s", vty->address);
426 if (vty)
427 for (i = 0; i < vector_active (vtyvec); i++)
428 if ((vty == vector_slot (vtyvec, i)))
429 {
430 snprintf(vty_str, sizeof(vty_str), "vty[%d]@%s",
431 i, vty->address);
432 break;
433 }
434
435 /* format the prompt */
436 snprintf(prompt_str, sizeof(prompt_str), cmd_prompt (vty->node), vty_str);
437
438 /* now log the command */
Lou Berger86b2a0a2016-05-17 12:19:51 -0400439 zlog(NULL, LOG_ERR, "%s%s", prompt_str, buf);
Lou Bergerc7f7e492016-01-12 13:41:49 -0500440 }
paul718e3742002-12-13 20:15:29 +0000441 /* Split readline string up into the vector */
442 vline = cmd_make_strvec (buf);
443
444 if (vline == NULL)
445 return CMD_SUCCESS;
446
ajs924b9222005-04-16 17:11:24 +0000447#ifdef CONSUMED_TIME_CHECK
448 {
449 RUSAGE_T before;
450 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000451 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000452
453 GETRUSAGE(&before);
454#endif /* CONSUMED_TIME_CHECK */
455
hasso87d683b2005-01-16 23:31:54 +0000456 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000457
vincentfbf5d032005-09-29 11:25:50 +0000458 /* Get the name of the protocol if any */
459 if (zlog_default)
460 protocolname = zlog_proto_names[zlog_default->protocol];
461 else
462 protocolname = zlog_proto_names[ZLOG_NONE];
463
ajs924b9222005-04-16 17:11:24 +0000464#ifdef CONSUMED_TIME_CHECK
465 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000466 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
467 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000468 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000469 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
470 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000471 }
472#endif /* CONSUMED_TIME_CHECK */
473
paul718e3742002-12-13 20:15:29 +0000474 if (ret != CMD_SUCCESS)
475 switch (ret)
476 {
477 case CMD_WARNING:
478 if (vty->type == VTY_FILE)
479 vty_out (vty, "Warning...%s", VTY_NEWLINE);
480 break;
481 case CMD_ERR_AMBIGUOUS:
482 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
483 break;
484 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000485 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000486 break;
487 case CMD_ERR_INCOMPLETE:
488 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
489 break;
490 }
491 cmd_free_strvec (vline);
492
493 return ret;
494}
David Lamparter6b0655a2014-06-04 06:53:35 +0200495
ajs9fc7ebf2005-02-23 15:12:34 +0000496static const char telnet_backward_char = 0x08;
497static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000498
499/* Basic function to write buffer to vty. */
500static void
ajs9fc7ebf2005-02-23 15:12:34 +0000501vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000502{
503 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
504 return;
505
506 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000507 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000508}
509
510/* Ensure length of input buffer. Is buffer is short, double it. */
511static void
512vty_ensure (struct vty *vty, int length)
513{
514 if (vty->max <= length)
515 {
516 vty->max *= 2;
517 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
518 }
519}
520
521/* Basic function to insert character into vty. */
522static void
523vty_self_insert (struct vty *vty, char c)
524{
525 int i;
526 int length;
527
528 vty_ensure (vty, vty->length + 1);
529 length = vty->length - vty->cp;
530 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
531 vty->buf[vty->cp] = c;
532
533 vty_write (vty, &vty->buf[vty->cp], length + 1);
534 for (i = 0; i < length; i++)
535 vty_write (vty, &telnet_backward_char, 1);
536
537 vty->cp++;
538 vty->length++;
539}
540
541/* Self insert character 'c' in overwrite mode. */
542static void
543vty_self_insert_overwrite (struct vty *vty, char c)
544{
545 vty_ensure (vty, vty->length + 1);
546 vty->buf[vty->cp++] = c;
547
548 if (vty->cp > vty->length)
549 vty->length++;
550
551 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
552 return;
553
554 vty_write (vty, &c, 1);
555}
556
557/* Insert a word into vty interface with overwrite mode. */
558static void
559vty_insert_word_overwrite (struct vty *vty, char *str)
560{
561 int len = strlen (str);
562 vty_write (vty, str, len);
563 strcpy (&vty->buf[vty->cp], str);
564 vty->cp += len;
565 vty->length = vty->cp;
566}
567
568/* Forward character. */
569static void
570vty_forward_char (struct vty *vty)
571{
572 if (vty->cp < vty->length)
573 {
574 vty_write (vty, &vty->buf[vty->cp], 1);
575 vty->cp++;
576 }
577}
578
579/* Backward character. */
580static void
581vty_backward_char (struct vty *vty)
582{
583 if (vty->cp > 0)
584 {
585 vty->cp--;
586 vty_write (vty, &telnet_backward_char, 1);
587 }
588}
589
590/* Move to the beginning of the line. */
591static void
592vty_beginning_of_line (struct vty *vty)
593{
594 while (vty->cp)
595 vty_backward_char (vty);
596}
597
598/* Move to the end of the line. */
599static void
600vty_end_of_line (struct vty *vty)
601{
602 while (vty->cp < vty->length)
603 vty_forward_char (vty);
604}
605
606static void vty_kill_line_from_beginning (struct vty *);
607static void vty_redraw_line (struct vty *);
608
609/* Print command line history. This function is called from
610 vty_next_line and vty_previous_line. */
611static void
612vty_history_print (struct vty *vty)
613{
614 int length;
615
616 vty_kill_line_from_beginning (vty);
617
618 /* Get previous line from history buffer */
619 length = strlen (vty->hist[vty->hp]);
620 memcpy (vty->buf, vty->hist[vty->hp], length);
621 vty->cp = vty->length = length;
622
623 /* Redraw current line */
624 vty_redraw_line (vty);
625}
626
627/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000628static void
paul718e3742002-12-13 20:15:29 +0000629vty_next_line (struct vty *vty)
630{
631 int try_index;
632
633 if (vty->hp == vty->hindex)
634 return;
635
636 /* Try is there history exist or not. */
637 try_index = vty->hp;
638 if (try_index == (VTY_MAXHIST - 1))
639 try_index = 0;
640 else
641 try_index++;
642
643 /* If there is not history return. */
644 if (vty->hist[try_index] == NULL)
645 return;
646 else
647 vty->hp = try_index;
648
649 vty_history_print (vty);
650}
651
652/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000653static void
paul718e3742002-12-13 20:15:29 +0000654vty_previous_line (struct vty *vty)
655{
656 int try_index;
657
658 try_index = vty->hp;
659 if (try_index == 0)
660 try_index = VTY_MAXHIST - 1;
661 else
662 try_index--;
663
664 if (vty->hist[try_index] == NULL)
665 return;
666 else
667 vty->hp = try_index;
668
669 vty_history_print (vty);
670}
671
672/* This function redraw all of the command line character. */
673static void
674vty_redraw_line (struct vty *vty)
675{
676 vty_write (vty, vty->buf, vty->length);
677 vty->cp = vty->length;
678}
679
680/* Forward word. */
681static void
682vty_forward_word (struct vty *vty)
683{
684 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
685 vty_forward_char (vty);
686
687 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
688 vty_forward_char (vty);
689}
690
691/* Backward word without skipping training space. */
692static void
693vty_backward_pure_word (struct vty *vty)
694{
695 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
696 vty_backward_char (vty);
697}
698
699/* Backward word. */
700static void
701vty_backward_word (struct vty *vty)
702{
703 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
704 vty_backward_char (vty);
705
706 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
707 vty_backward_char (vty);
708}
709
710/* When '^D' is typed at the beginning of the line we move to the down
711 level. */
712static void
713vty_down_level (struct vty *vty)
714{
715 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000716 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000717 vty_prompt (vty);
718 vty->cp = 0;
719}
720
721/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000722static void
paul718e3742002-12-13 20:15:29 +0000723vty_end_config (struct vty *vty)
724{
725 vty_out (vty, "%s", VTY_NEWLINE);
726
727 switch (vty->node)
728 {
729 case VIEW_NODE:
730 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +0100731 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +0000732 /* Nothing to do. */
733 break;
734 case CONFIG_NODE:
735 case INTERFACE_NODE:
736 case ZEBRA_NODE:
737 case RIP_NODE:
738 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +0100739 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +0000740 case BGP_NODE:
741 case BGP_VPNV4_NODE:
Lou Berger13c378d2016-01-12 13:41:56 -0500742 case BGP_VPNV6_NODE:
Lou Bergera3fda882016-01-12 13:42:04 -0500743 case BGP_ENCAP_NODE:
744 case BGP_ENCAPV6_NODE:
paul718e3742002-12-13 20:15:29 +0000745 case BGP_IPV4_NODE:
746 case BGP_IPV4M_NODE:
747 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000748 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000749 case RMAP_NODE:
750 case OSPF_NODE:
751 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000752 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000753 case KEYCHAIN_NODE:
754 case KEYCHAIN_KEY_NODE:
755 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -0200756 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +0000757 case VTY_NODE:
758 vty_config_unlock (vty);
759 vty->node = ENABLE_NODE;
760 break;
761 default:
762 /* Unknown node, we have to ignore it. */
763 break;
764 }
765
766 vty_prompt (vty);
767 vty->cp = 0;
768}
769
770/* Delete a charcter at the current point. */
771static void
772vty_delete_char (struct vty *vty)
773{
774 int i;
775 int size;
776
paul718e3742002-12-13 20:15:29 +0000777 if (vty->length == 0)
778 {
779 vty_down_level (vty);
780 return;
781 }
782
783 if (vty->cp == vty->length)
784 return; /* completion need here? */
785
786 size = vty->length - vty->cp;
787
788 vty->length--;
789 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
790 vty->buf[vty->length] = '\0';
Roy7f794f22008-08-13 17:27:38 +0100791
792 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
793 return;
paul718e3742002-12-13 20:15:29 +0000794
795 vty_write (vty, &vty->buf[vty->cp], size - 1);
796 vty_write (vty, &telnet_space_char, 1);
797
798 for (i = 0; i < size; i++)
799 vty_write (vty, &telnet_backward_char, 1);
800}
801
802/* Delete a character before the point. */
803static void
804vty_delete_backward_char (struct vty *vty)
805{
806 if (vty->cp == 0)
807 return;
808
809 vty_backward_char (vty);
810 vty_delete_char (vty);
811}
812
813/* Kill rest of line from current point. */
814static void
815vty_kill_line (struct vty *vty)
816{
817 int i;
818 int size;
819
820 size = vty->length - vty->cp;
821
822 if (size == 0)
823 return;
824
825 for (i = 0; i < size; i++)
826 vty_write (vty, &telnet_space_char, 1);
827 for (i = 0; i < size; i++)
828 vty_write (vty, &telnet_backward_char, 1);
829
830 memset (&vty->buf[vty->cp], 0, size);
831 vty->length = vty->cp;
832}
833
834/* Kill line from the beginning. */
835static void
836vty_kill_line_from_beginning (struct vty *vty)
837{
838 vty_beginning_of_line (vty);
839 vty_kill_line (vty);
840}
841
842/* Delete a word before the point. */
843static void
844vty_forward_kill_word (struct vty *vty)
845{
846 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
847 vty_delete_char (vty);
848 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
849 vty_delete_char (vty);
850}
851
852/* Delete a word before the point. */
853static void
854vty_backward_kill_word (struct vty *vty)
855{
856 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
857 vty_delete_backward_char (vty);
858 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
859 vty_delete_backward_char (vty);
860}
861
862/* Transpose chars before or at the point. */
863static void
864vty_transpose_chars (struct vty *vty)
865{
866 char c1, c2;
867
868 /* If length is short or point is near by the beginning of line then
869 return. */
870 if (vty->length < 2 || vty->cp < 1)
871 return;
872
873 /* In case of point is located at the end of the line. */
874 if (vty->cp == vty->length)
875 {
876 c1 = vty->buf[vty->cp - 1];
877 c2 = vty->buf[vty->cp - 2];
878
879 vty_backward_char (vty);
880 vty_backward_char (vty);
881 vty_self_insert_overwrite (vty, c1);
882 vty_self_insert_overwrite (vty, c2);
883 }
884 else
885 {
886 c1 = vty->buf[vty->cp];
887 c2 = vty->buf[vty->cp - 1];
888
889 vty_backward_char (vty);
890 vty_self_insert_overwrite (vty, c1);
891 vty_self_insert_overwrite (vty, c2);
892 }
893}
894
895/* Do completion at vty interface. */
896static void
897vty_complete_command (struct vty *vty)
898{
899 int i;
900 int ret;
901 char **matched = NULL;
902 vector vline;
903
904 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
905 return;
906
907 vline = cmd_make_strvec (vty->buf);
908 if (vline == NULL)
909 return;
910
911 /* In case of 'help \t'. */
912 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +0100913 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000914
Lou Berger67290032016-01-12 13:41:46 -0500915 matched = cmd_complete_command_lib (vline, vty, &ret, 1);
paul718e3742002-12-13 20:15:29 +0000916
917 cmd_free_strvec (vline);
918
919 vty_out (vty, "%s", VTY_NEWLINE);
920 switch (ret)
921 {
922 case CMD_ERR_AMBIGUOUS:
923 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
924 vty_prompt (vty);
925 vty_redraw_line (vty);
926 break;
927 case CMD_ERR_NO_MATCH:
928 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
929 vty_prompt (vty);
930 vty_redraw_line (vty);
931 break;
932 case CMD_COMPLETE_FULL_MATCH:
933 vty_prompt (vty);
934 vty_redraw_line (vty);
935 vty_backward_pure_word (vty);
936 vty_insert_word_overwrite (vty, matched[0]);
937 vty_self_insert (vty, ' ');
938 XFREE (MTYPE_TMP, matched[0]);
939 break;
940 case CMD_COMPLETE_MATCH:
941 vty_prompt (vty);
942 vty_redraw_line (vty);
943 vty_backward_pure_word (vty);
944 vty_insert_word_overwrite (vty, matched[0]);
945 XFREE (MTYPE_TMP, matched[0]);
946 vector_only_index_free (matched);
947 return;
948 break;
949 case CMD_COMPLETE_LIST_MATCH:
950 for (i = 0; matched[i] != NULL; i++)
951 {
952 if (i != 0 && ((i % 6) == 0))
953 vty_out (vty, "%s", VTY_NEWLINE);
954 vty_out (vty, "%-10s ", matched[i]);
955 XFREE (MTYPE_TMP, matched[i]);
956 }
957 vty_out (vty, "%s", VTY_NEWLINE);
958
959 vty_prompt (vty);
960 vty_redraw_line (vty);
961 break;
962 case CMD_ERR_NOTHING_TODO:
963 vty_prompt (vty);
964 vty_redraw_line (vty);
965 break;
966 default:
967 break;
968 }
969 if (matched)
970 vector_only_index_free (matched);
971}
972
ajs9fc7ebf2005-02-23 15:12:34 +0000973static void
paul718e3742002-12-13 20:15:29 +0000974vty_describe_fold (struct vty *vty, int cmd_width,
Christian Frankecd40b322013-09-30 12:27:51 +0000975 unsigned int desc_width, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +0000976{
hasso8c328f12004-10-05 21:01:23 +0000977 char *buf;
978 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000979 int pos;
980
Christian Frankecd40b322013-09-30 12:27:51 +0000981 cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
paul718e3742002-12-13 20:15:29 +0000982
983 if (desc_width <= 0)
984 {
Christian Frankecd40b322013-09-30 12:27:51 +0000985 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000986 return;
987 }
988
Christian Frankecd40b322013-09-30 12:27:51 +0000989 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
paul718e3742002-12-13 20:15:29 +0000990
Christian Frankecd40b322013-09-30 12:27:51 +0000991 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
paul718e3742002-12-13 20:15:29 +0000992 {
993 for (pos = desc_width; pos > 0; pos--)
994 if (*(p + pos) == ' ')
995 break;
996
997 if (pos == 0)
998 break;
999
1000 strncpy (buf, p, pos);
1001 buf[pos] = '\0';
1002 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
1003
1004 cmd = "";
1005 }
1006
1007 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
1008
1009 XFREE (MTYPE_TMP, buf);
1010}
1011
1012/* Describe matched command function. */
1013static void
1014vty_describe_command (struct vty *vty)
1015{
1016 int ret;
1017 vector vline;
1018 vector describe;
hasso8c328f12004-10-05 21:01:23 +00001019 unsigned int i, width, desc_width;
Christian Frankecd40b322013-09-30 12:27:51 +00001020 struct cmd_token *token, *token_cr = NULL;
paul718e3742002-12-13 20:15:29 +00001021
1022 vline = cmd_make_strvec (vty->buf);
1023
1024 /* In case of '> ?'. */
1025 if (vline == NULL)
1026 {
1027 vline = vector_init (1);
David Lampartera91a3ba2015-03-03 09:06:51 +01001028 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +00001029 }
1030 else
1031 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +01001032 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +00001033
1034 describe = cmd_describe_command (vline, vty, &ret);
1035
1036 vty_out (vty, "%s", VTY_NEWLINE);
1037
1038 /* Ambiguous error. */
1039 switch (ret)
1040 {
1041 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +00001042 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001043 goto out;
paul718e3742002-12-13 20:15:29 +00001044 break;
1045 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +00001046 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001047 goto out;
paul718e3742002-12-13 20:15:29 +00001048 break;
1049 }
1050
1051 /* Get width of command string. */
1052 width = 0;
paul55468c82005-03-14 20:19:01 +00001053 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001054 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001055 {
hasso8c328f12004-10-05 21:01:23 +00001056 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001057
Christian Frankecd40b322013-09-30 12:27:51 +00001058 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001059 continue;
1060
Christian Frankecd40b322013-09-30 12:27:51 +00001061 len = strlen (token->cmd);
1062 if (token->cmd[0] == '.')
paul718e3742002-12-13 20:15:29 +00001063 len--;
1064
1065 if (width < len)
1066 width = len;
1067 }
1068
1069 /* Get width of description string. */
1070 desc_width = vty->width - (width + 6);
1071
1072 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001073 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001074 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001075 {
Christian Frankecd40b322013-09-30 12:27:51 +00001076 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001077 continue;
1078
Christian Frankecd40b322013-09-30 12:27:51 +00001079 if (strcmp (token->cmd, command_cr) == 0)
paul718e3742002-12-13 20:15:29 +00001080 {
Christian Frankecd40b322013-09-30 12:27:51 +00001081 token_cr = token;
paul718e3742002-12-13 20:15:29 +00001082 continue;
1083 }
1084
Christian Frankecd40b322013-09-30 12:27:51 +00001085 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001086 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001087 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001088 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001089 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001090 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001091 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1092 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001093 else
Christian Frankecd40b322013-09-30 12:27:51 +00001094 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001095
1096#if 0
1097 vty_out (vty, " %-*s %s%s", width
1098 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1099 desc->str ? desc->str : "", VTY_NEWLINE);
1100#endif /* 0 */
1101 }
1102
Christian Frankecd40b322013-09-30 12:27:51 +00001103 if ((token = token_cr))
paul718e3742002-12-13 20:15:29 +00001104 {
Christian Frankecd40b322013-09-30 12:27:51 +00001105 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001106 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001107 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001108 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001109 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001110 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001111 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1112 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001113 else
Christian Frankecd40b322013-09-30 12:27:51 +00001114 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001115 }
1116
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001117out:
paul718e3742002-12-13 20:15:29 +00001118 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001119 if (describe)
1120 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001121
1122 vty_prompt (vty);
1123 vty_redraw_line (vty);
1124}
1125
ajs9fc7ebf2005-02-23 15:12:34 +00001126static void
paul718e3742002-12-13 20:15:29 +00001127vty_clear_buf (struct vty *vty)
1128{
1129 memset (vty->buf, 0, vty->max);
1130}
1131
1132/* ^C stop current input and do not add command line to the history. */
1133static void
1134vty_stop_input (struct vty *vty)
1135{
1136 vty->cp = vty->length = 0;
1137 vty_clear_buf (vty);
1138 vty_out (vty, "%s", VTY_NEWLINE);
1139
1140 switch (vty->node)
1141 {
1142 case VIEW_NODE:
1143 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01001144 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00001145 /* Nothing to do. */
1146 break;
1147 case CONFIG_NODE:
1148 case INTERFACE_NODE:
1149 case ZEBRA_NODE:
1150 case RIP_NODE:
1151 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01001152 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00001153 case BGP_NODE:
1154 case RMAP_NODE:
1155 case OSPF_NODE:
1156 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001157 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001158 case KEYCHAIN_NODE:
1159 case KEYCHAIN_KEY_NODE:
1160 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02001161 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00001162 case VTY_NODE:
1163 vty_config_unlock (vty);
1164 vty->node = ENABLE_NODE;
1165 break;
1166 default:
1167 /* Unknown node, we have to ignore it. */
1168 break;
1169 }
1170 vty_prompt (vty);
1171
1172 /* Set history pointer to the latest one. */
1173 vty->hp = vty->hindex;
1174}
1175
1176/* Add current command line to the history buffer. */
1177static void
1178vty_hist_add (struct vty *vty)
1179{
1180 int index;
1181
1182 if (vty->length == 0)
1183 return;
1184
1185 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1186
1187 /* Ignore the same string as previous one. */
1188 if (vty->hist[index])
1189 if (strcmp (vty->buf, vty->hist[index]) == 0)
1190 {
1191 vty->hp = vty->hindex;
1192 return;
1193 }
1194
1195 /* Insert history entry. */
1196 if (vty->hist[vty->hindex])
1197 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1198 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1199
1200 /* History index rotation. */
1201 vty->hindex++;
1202 if (vty->hindex == VTY_MAXHIST)
1203 vty->hindex = 0;
1204
1205 vty->hp = vty->hindex;
1206}
1207
1208/* #define TELNET_OPTION_DEBUG */
1209
1210/* Get telnet window size. */
1211static int
1212vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1213{
1214#ifdef TELNET_OPTION_DEBUG
1215 int i;
1216
1217 for (i = 0; i < nbytes; i++)
1218 {
1219 switch (buf[i])
1220 {
1221 case IAC:
1222 vty_out (vty, "IAC ");
1223 break;
1224 case WILL:
1225 vty_out (vty, "WILL ");
1226 break;
1227 case WONT:
1228 vty_out (vty, "WONT ");
1229 break;
1230 case DO:
1231 vty_out (vty, "DO ");
1232 break;
1233 case DONT:
1234 vty_out (vty, "DONT ");
1235 break;
1236 case SB:
1237 vty_out (vty, "SB ");
1238 break;
1239 case SE:
1240 vty_out (vty, "SE ");
1241 break;
1242 case TELOPT_ECHO:
1243 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1244 break;
1245 case TELOPT_SGA:
1246 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1247 break;
1248 case TELOPT_NAWS:
1249 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1250 break;
1251 default:
1252 vty_out (vty, "%x ", buf[i]);
1253 break;
1254 }
1255 }
1256 vty_out (vty, "%s", VTY_NEWLINE);
1257
1258#endif /* TELNET_OPTION_DEBUG */
1259
1260 switch (buf[0])
1261 {
1262 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001263 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001264 vty->iac_sb_in_progress = 1;
1265 return 0;
1266 break;
1267 case SE:
1268 {
paul718e3742002-12-13 20:15:29 +00001269 if (!vty->iac_sb_in_progress)
1270 return 0;
1271
ajs9fc7ebf2005-02-23 15:12:34 +00001272 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001273 {
1274 vty->iac_sb_in_progress = 0;
1275 return 0;
1276 }
ajs9fc7ebf2005-02-23 15:12:34 +00001277 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001278 {
1279 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001280 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1281 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1282 "should send %d characters, but we received %lu",
1283 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1284 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1285 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1286 "too small to handle the telnet NAWS option",
1287 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1288 else
1289 {
1290 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1291 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1292#ifdef TELNET_OPTION_DEBUG
1293 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1294 "width %d, height %d%s",
1295 vty->width, vty->height, VTY_NEWLINE);
1296#endif
1297 }
paul718e3742002-12-13 20:15:29 +00001298 break;
1299 }
1300 vty->iac_sb_in_progress = 0;
1301 return 0;
1302 break;
1303 }
1304 default:
1305 break;
1306 }
1307 return 1;
1308}
1309
1310/* Execute current command line. */
1311static int
1312vty_execute (struct vty *vty)
1313{
1314 int ret;
1315
1316 ret = CMD_SUCCESS;
1317
1318 switch (vty->node)
1319 {
1320 case AUTH_NODE:
1321 case AUTH_ENABLE_NODE:
1322 vty_auth (vty, vty->buf);
1323 break;
1324 default:
1325 ret = vty_command (vty, vty->buf);
1326 if (vty->type == VTY_TERM)
1327 vty_hist_add (vty);
1328 break;
1329 }
1330
1331 /* Clear command line buffer. */
1332 vty->cp = vty->length = 0;
1333 vty_clear_buf (vty);
1334
ajs5a646652004-11-05 01:25:55 +00001335 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001336 vty_prompt (vty);
1337
1338 return ret;
1339}
1340
1341#define CONTROL(X) ((X) - '@')
1342#define VTY_NORMAL 0
1343#define VTY_PRE_ESCAPE 1
1344#define VTY_ESCAPE 2
1345
1346/* Escape character command map. */
1347static void
1348vty_escape_map (unsigned char c, struct vty *vty)
1349{
1350 switch (c)
1351 {
1352 case ('A'):
1353 vty_previous_line (vty);
1354 break;
1355 case ('B'):
1356 vty_next_line (vty);
1357 break;
1358 case ('C'):
1359 vty_forward_char (vty);
1360 break;
1361 case ('D'):
1362 vty_backward_char (vty);
1363 break;
1364 default:
1365 break;
1366 }
1367
1368 /* Go back to normal mode. */
1369 vty->escape = VTY_NORMAL;
1370}
1371
1372/* Quit print out to the buffer. */
1373static void
1374vty_buffer_reset (struct vty *vty)
1375{
1376 buffer_reset (vty->obuf);
1377 vty_prompt (vty);
1378 vty_redraw_line (vty);
1379}
1380
1381/* Read data via vty socket. */
1382static int
1383vty_read (struct thread *thread)
1384{
1385 int i;
paul718e3742002-12-13 20:15:29 +00001386 int nbytes;
1387 unsigned char buf[VTY_READ_BUFSIZ];
1388
1389 int vty_sock = THREAD_FD (thread);
1390 struct vty *vty = THREAD_ARG (thread);
1391 vty->t_read = NULL;
1392
1393 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001394 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1395 {
1396 if (nbytes < 0)
1397 {
1398 if (ERRNO_IO_RETRY(errno))
1399 {
1400 vty_event (VTY_READ, vty_sock, vty);
1401 return 0;
1402 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001403 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001404 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1405 __func__, vty->fd, safe_strerror(errno));
David Lamparter90d31352015-05-14 14:24:06 +02001406 buffer_reset(vty->obuf);
ajs9fc7ebf2005-02-23 15:12:34 +00001407 }
ajs9fc7ebf2005-02-23 15:12:34 +00001408 vty->status = VTY_CLOSE;
1409 }
paul718e3742002-12-13 20:15:29 +00001410
1411 for (i = 0; i < nbytes; i++)
1412 {
1413 if (buf[i] == IAC)
1414 {
1415 if (!vty->iac)
1416 {
1417 vty->iac = 1;
1418 continue;
1419 }
1420 else
1421 {
1422 vty->iac = 0;
1423 }
1424 }
1425
1426 if (vty->iac_sb_in_progress && !vty->iac)
1427 {
ajs9fc7ebf2005-02-23 15:12:34 +00001428 if (vty->sb_len < sizeof(vty->sb_buf))
1429 vty->sb_buf[vty->sb_len] = buf[i];
1430 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001431 continue;
1432 }
1433
1434 if (vty->iac)
1435 {
1436 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001437 int ret = 0;
paule9372532003-10-26 21:36:07 +00001438 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001439 vty->iac = 0;
1440 i += ret;
1441 continue;
1442 }
paul5b8c1b02003-10-15 23:08:55 +00001443
paul718e3742002-12-13 20:15:29 +00001444
1445 if (vty->status == VTY_MORE)
1446 {
1447 switch (buf[i])
1448 {
1449 case CONTROL('C'):
1450 case 'q':
1451 case 'Q':
paul718e3742002-12-13 20:15:29 +00001452 vty_buffer_reset (vty);
1453 break;
1454#if 0 /* More line does not work for "show ip bgp". */
1455 case '\n':
1456 case '\r':
1457 vty->status = VTY_MORELINE;
1458 break;
1459#endif
1460 default:
paul718e3742002-12-13 20:15:29 +00001461 break;
1462 }
1463 continue;
1464 }
1465
1466 /* Escape character. */
1467 if (vty->escape == VTY_ESCAPE)
1468 {
1469 vty_escape_map (buf[i], vty);
1470 continue;
1471 }
1472
1473 /* Pre-escape status. */
1474 if (vty->escape == VTY_PRE_ESCAPE)
1475 {
1476 switch (buf[i])
1477 {
1478 case '[':
1479 vty->escape = VTY_ESCAPE;
1480 break;
1481 case 'b':
1482 vty_backward_word (vty);
1483 vty->escape = VTY_NORMAL;
1484 break;
1485 case 'f':
1486 vty_forward_word (vty);
1487 vty->escape = VTY_NORMAL;
1488 break;
1489 case 'd':
1490 vty_forward_kill_word (vty);
1491 vty->escape = VTY_NORMAL;
1492 break;
1493 case CONTROL('H'):
1494 case 0x7f:
1495 vty_backward_kill_word (vty);
1496 vty->escape = VTY_NORMAL;
1497 break;
1498 default:
1499 vty->escape = VTY_NORMAL;
1500 break;
1501 }
1502 continue;
1503 }
1504
1505 switch (buf[i])
1506 {
1507 case CONTROL('A'):
1508 vty_beginning_of_line (vty);
1509 break;
1510 case CONTROL('B'):
1511 vty_backward_char (vty);
1512 break;
1513 case CONTROL('C'):
1514 vty_stop_input (vty);
1515 break;
1516 case CONTROL('D'):
1517 vty_delete_char (vty);
1518 break;
1519 case CONTROL('E'):
1520 vty_end_of_line (vty);
1521 break;
1522 case CONTROL('F'):
1523 vty_forward_char (vty);
1524 break;
1525 case CONTROL('H'):
1526 case 0x7f:
1527 vty_delete_backward_char (vty);
1528 break;
1529 case CONTROL('K'):
1530 vty_kill_line (vty);
1531 break;
1532 case CONTROL('N'):
1533 vty_next_line (vty);
1534 break;
1535 case CONTROL('P'):
1536 vty_previous_line (vty);
1537 break;
1538 case CONTROL('T'):
1539 vty_transpose_chars (vty);
1540 break;
1541 case CONTROL('U'):
1542 vty_kill_line_from_beginning (vty);
1543 break;
1544 case CONTROL('W'):
1545 vty_backward_kill_word (vty);
1546 break;
1547 case CONTROL('Z'):
1548 vty_end_config (vty);
1549 break;
1550 case '\n':
1551 case '\r':
1552 vty_out (vty, "%s", VTY_NEWLINE);
1553 vty_execute (vty);
1554 break;
1555 case '\t':
1556 vty_complete_command (vty);
1557 break;
1558 case '?':
1559 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1560 vty_self_insert (vty, buf[i]);
1561 else
1562 vty_describe_command (vty);
1563 break;
1564 case '\033':
1565 if (i + 1 < nbytes && buf[i + 1] == '[')
1566 {
1567 vty->escape = VTY_ESCAPE;
1568 i++;
1569 }
1570 else
1571 vty->escape = VTY_PRE_ESCAPE;
1572 break;
1573 default:
1574 if (buf[i] > 31 && buf[i] < 127)
1575 vty_self_insert (vty, buf[i]);
1576 break;
1577 }
1578 }
1579
1580 /* Check status. */
1581 if (vty->status == VTY_CLOSE)
1582 vty_close (vty);
1583 else
1584 {
David Lamparter4715a532013-05-30 16:31:49 +02001585 vty_event (VTY_WRITE, vty->wfd, vty);
paul718e3742002-12-13 20:15:29 +00001586 vty_event (VTY_READ, vty_sock, vty);
1587 }
1588 return 0;
1589}
1590
1591/* Flush buffer to the vty. */
1592static int
1593vty_flush (struct thread *thread)
1594{
1595 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001596 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001597 int vty_sock = THREAD_FD (thread);
1598 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001599
paul718e3742002-12-13 20:15:29 +00001600 vty->t_write = NULL;
1601
1602 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001603 if ((vty->lines == 0) && vty->t_read)
1604 {
1605 thread_cancel (vty->t_read);
1606 vty->t_read = NULL;
1607 }
paul718e3742002-12-13 20:15:29 +00001608
1609 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001610 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001611
ajs9fc7ebf2005-02-23 15:12:34 +00001612 /* N.B. if width is 0, that means we don't know the window size. */
Lou Bergerc7f7e492016-01-12 13:41:49 -05001613 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
David Lamparter4715a532013-05-30 16:31:49 +02001614 flushrc = buffer_flush_available(vty->obuf, vty_sock);
ajs9fc7ebf2005-02-23 15:12:34 +00001615 else if (vty->status == VTY_MORELINE)
David Lamparter4715a532013-05-30 16:31:49 +02001616 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001617 1, erase, 0);
1618 else
David Lamparter4715a532013-05-30 16:31:49 +02001619 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001620 vty->lines >= 0 ? vty->lines :
1621 vty->height,
1622 erase, 0);
1623 switch (flushrc)
1624 {
1625 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001626 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001627 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1628 vty->fd);
1629 buffer_reset(vty->obuf);
1630 vty_close(vty);
1631 return 0;
1632 case BUFFER_EMPTY:
1633 if (vty->status == VTY_CLOSE)
1634 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001635 else
1636 {
ajs9fc7ebf2005-02-23 15:12:34 +00001637 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001638 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001639 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001640 }
ajs9fc7ebf2005-02-23 15:12:34 +00001641 break;
1642 case BUFFER_PENDING:
1643 /* There is more data waiting to be written. */
1644 vty->status = VTY_MORE;
1645 if (vty->lines == 0)
1646 vty_event (VTY_WRITE, vty_sock, vty);
1647 break;
1648 }
paul718e3742002-12-13 20:15:29 +00001649
1650 return 0;
1651}
1652
David Lamparterba5dc5e2013-05-30 16:33:45 +02001653/* allocate and initialise vty */
1654static struct vty *
1655vty_new_init (int vty_sock)
1656{
1657 struct vty *vty;
1658
1659 vty = vty_new ();
1660 vty->fd = vty_sock;
1661 vty->wfd = vty_sock;
1662 vty->type = VTY_TERM;
1663 vty->node = AUTH_NODE;
1664 vty->fail = 0;
1665 vty->cp = 0;
1666 vty_clear_buf (vty);
1667 vty->length = 0;
1668 memset (vty->hist, 0, sizeof (vty->hist));
1669 vty->hp = 0;
1670 vty->hindex = 0;
1671 vector_set_index (vtyvec, vty_sock, vty);
1672 vty->status = VTY_NORMAL;
1673 vty->lines = -1;
1674 vty->iac = 0;
1675 vty->iac_sb_in_progress = 0;
1676 vty->sb_len = 0;
1677
1678 return vty;
1679}
1680
paul718e3742002-12-13 20:15:29 +00001681/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001682static struct vty *
paul718e3742002-12-13 20:15:29 +00001683vty_create (int vty_sock, union sockunion *su)
1684{
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001685 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001686 struct vty *vty;
1687
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001688 sockunion2str(su, buf, SU_ADDRSTRLEN);
1689
paul718e3742002-12-13 20:15:29 +00001690 /* Allocate new vty structure and set up default values. */
David Lamparterba5dc5e2013-05-30 16:33:45 +02001691 vty = vty_new_init (vty_sock);
1692
1693 /* configurable parameters not part of basic init */
1694 vty->v_timeout = vty_timeout_val;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001695 strcpy (vty->address, buf);
paul718e3742002-12-13 20:15:29 +00001696 if (no_password_check)
1697 {
Paul Jakma62687ff2008-08-23 14:27:06 +01001698 if (restricted_mode)
1699 vty->node = RESTRICTED_NODE;
1700 else if (host.advanced)
paul718e3742002-12-13 20:15:29 +00001701 vty->node = ENABLE_NODE;
1702 else
1703 vty->node = VIEW_NODE;
1704 }
paul718e3742002-12-13 20:15:29 +00001705 if (host.lines >= 0)
1706 vty->lines = host.lines;
paul718e3742002-12-13 20:15:29 +00001707
1708 if (! no_password_check)
1709 {
1710 /* Vty is not available if password isn't set. */
1711 if (host.password == NULL && host.password_encrypt == NULL)
1712 {
1713 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1714 vty->status = VTY_CLOSE;
1715 vty_close (vty);
1716 return NULL;
1717 }
1718 }
1719
1720 /* Say hello to the world. */
1721 vty_hello (vty);
1722 if (! no_password_check)
1723 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1724
1725 /* Setting up terminal. */
1726 vty_will_echo (vty);
1727 vty_will_suppress_go_ahead (vty);
1728
1729 vty_dont_linemode (vty);
1730 vty_do_window_size (vty);
1731 /* vty_dont_lflow_ahead (vty); */
1732
1733 vty_prompt (vty);
1734
1735 /* Add read/write thread. */
1736 vty_event (VTY_WRITE, vty_sock, vty);
1737 vty_event (VTY_READ, vty_sock, vty);
1738
1739 return vty;
1740}
1741
David Lamparterba5dc5e2013-05-30 16:33:45 +02001742/* create vty for stdio */
David Lamparterba53a8f2015-05-05 11:04:46 +02001743static struct termios stdio_orig_termios;
1744static struct vty *stdio_vty = NULL;
David Lamparter464ccf32015-05-12 21:56:18 +02001745static void (*stdio_vty_atclose)(void);
David Lamparterba53a8f2015-05-05 11:04:46 +02001746
1747static void
1748vty_stdio_reset (void)
1749{
1750 if (stdio_vty)
1751 {
1752 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1753 stdio_vty = NULL;
David Lamparter464ccf32015-05-12 21:56:18 +02001754
1755 if (stdio_vty_atclose)
1756 stdio_vty_atclose ();
1757 stdio_vty_atclose = NULL;
David Lamparterba53a8f2015-05-05 11:04:46 +02001758 }
1759}
1760
David Lamparterba5dc5e2013-05-30 16:33:45 +02001761struct vty *
David Lamparter464ccf32015-05-12 21:56:18 +02001762vty_stdio (void (*atclose)())
David Lamparterba5dc5e2013-05-30 16:33:45 +02001763{
1764 struct vty *vty;
David Lamparterba53a8f2015-05-05 11:04:46 +02001765 struct termios termios;
David Lamparterba5dc5e2013-05-30 16:33:45 +02001766
David Lamparterba53a8f2015-05-05 11:04:46 +02001767 /* refuse creating two vtys on stdio */
1768 if (stdio_vty)
1769 return NULL;
1770
1771 vty = stdio_vty = vty_new_init (0);
David Lamparter464ccf32015-05-12 21:56:18 +02001772 stdio_vty_atclose = atclose;
David Lamparterba5dc5e2013-05-30 16:33:45 +02001773 vty->wfd = 1;
1774
1775 /* always have stdio vty in a known _unchangeable_ state, don't want config
1776 * to have any effect here to make sure scripting this works as intended */
1777 vty->node = ENABLE_NODE;
1778 vty->v_timeout = 0;
1779 strcpy (vty->address, "console");
1780
David Lamparterba53a8f2015-05-05 11:04:46 +02001781 if (!tcgetattr (0, &stdio_orig_termios))
1782 {
1783 termios = stdio_orig_termios;
1784 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1785 | INLCR | IGNCR | ICRNL | IXON);
1786 termios.c_oflag &= ~OPOST;
1787 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1788 termios.c_cflag &= ~(CSIZE | PARENB);
1789 termios.c_cflag |= CS8;
1790 tcsetattr (0, TCSANOW, &termios);
1791 }
1792
David Lamparterba5dc5e2013-05-30 16:33:45 +02001793 vty_prompt (vty);
1794
1795 /* Add read/write thread. */
1796 vty_event (VTY_WRITE, 1, vty);
1797 vty_event (VTY_READ, 0, vty);
1798
1799 return vty;
1800}
1801
paul718e3742002-12-13 20:15:29 +00001802/* Accept connection from the network. */
1803static int
1804vty_accept (struct thread *thread)
1805{
1806 int vty_sock;
paul718e3742002-12-13 20:15:29 +00001807 union sockunion su;
1808 int ret;
1809 unsigned int on;
1810 int accept_sock;
Timo Teräsc1c69e42015-05-22 13:40:57 +03001811 struct prefix p;
paul718e3742002-12-13 20:15:29 +00001812 struct access_list *acl = NULL;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001813 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001814
1815 accept_sock = THREAD_FD (thread);
1816
1817 /* We continue hearing vty socket. */
1818 vty_event (VTY_SERV, accept_sock, NULL);
1819
1820 memset (&su, 0, sizeof (union sockunion));
1821
1822 /* We can handle IPv4 or IPv6 socket. */
1823 vty_sock = sockunion_accept (accept_sock, &su);
1824 if (vty_sock < 0)
1825 {
ajs6099b3b2004-11-20 02:06:59 +00001826 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001827 return -1;
1828 }
ajs9fc7ebf2005-02-23 15:12:34 +00001829 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001830
Timo Teräsc1c69e42015-05-22 13:40:57 +03001831 sockunion2hostprefix (&su, &p);
paul718e3742002-12-13 20:15:29 +00001832
1833 /* VTY's accesslist apply. */
Timo Teräsc1c69e42015-05-22 13:40:57 +03001834 if (p.family == AF_INET && vty_accesslist_name)
paul718e3742002-12-13 20:15:29 +00001835 {
1836 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
Timo Teräsc1c69e42015-05-22 13:40:57 +03001837 (access_list_apply (acl, &p) == FILTER_DENY))
paul718e3742002-12-13 20:15:29 +00001838 {
paul718e3742002-12-13 20:15:29 +00001839 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001840 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001841 close (vty_sock);
1842
1843 /* continue accepting connections */
1844 vty_event (VTY_SERV, accept_sock, NULL);
1845
paul718e3742002-12-13 20:15:29 +00001846 return 0;
1847 }
1848 }
1849
1850#ifdef HAVE_IPV6
1851 /* VTY's ipv6 accesslist apply. */
Timo Teräsc1c69e42015-05-22 13:40:57 +03001852 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
paul718e3742002-12-13 20:15:29 +00001853 {
1854 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
Timo Teräsc1c69e42015-05-22 13:40:57 +03001855 (access_list_apply (acl, &p) == FILTER_DENY))
paul718e3742002-12-13 20:15:29 +00001856 {
paul718e3742002-12-13 20:15:29 +00001857 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001858 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001859 close (vty_sock);
1860
1861 /* continue accepting connections */
1862 vty_event (VTY_SERV, accept_sock, NULL);
1863
paul718e3742002-12-13 20:15:29 +00001864 return 0;
1865 }
1866 }
1867#endif /* HAVE_IPV6 */
1868
paul718e3742002-12-13 20:15:29 +00001869 on = 1;
1870 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1871 (char *) &on, sizeof (on));
1872 if (ret < 0)
1873 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001874 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001875
heasley78e6cd92009-12-07 16:41:14 +03001876 zlog (NULL, LOG_INFO, "Vty connection from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001877 sockunion2str (&su, buf, SU_ADDRSTRLEN));
heasley78e6cd92009-12-07 16:41:14 +03001878
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001879 vty_create (vty_sock, &su);
paul718e3742002-12-13 20:15:29 +00001880
1881 return 0;
1882}
1883
David Lamparter6d6df302014-06-28 21:12:37 +02001884#ifdef HAVE_IPV6
ajs9fc7ebf2005-02-23 15:12:34 +00001885static void
paul718e3742002-12-13 20:15:29 +00001886vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1887{
1888 int ret;
1889 struct addrinfo req;
1890 struct addrinfo *ainfo;
1891 struct addrinfo *ainfo_save;
1892 int sock;
1893 char port_str[BUFSIZ];
1894
1895 memset (&req, 0, sizeof (struct addrinfo));
1896 req.ai_flags = AI_PASSIVE;
1897 req.ai_family = AF_UNSPEC;
1898 req.ai_socktype = SOCK_STREAM;
1899 sprintf (port_str, "%d", port);
1900 port_str[sizeof (port_str) - 1] = '\0';
1901
1902 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1903
1904 if (ret != 0)
1905 {
1906 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1907 exit (1);
1908 }
1909
1910 ainfo_save = ainfo;
1911
1912 do
1913 {
1914 if (ainfo->ai_family != AF_INET
1915#ifdef HAVE_IPV6
1916 && ainfo->ai_family != AF_INET6
1917#endif /* HAVE_IPV6 */
1918 )
1919 continue;
1920
1921 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1922 if (sock < 0)
1923 continue;
1924
David Lamparterca051262009-10-04 16:21:49 +02001925 sockopt_v6only (ainfo->ai_family, sock);
paul718e3742002-12-13 20:15:29 +00001926 sockopt_reuseaddr (sock);
1927 sockopt_reuseport (sock);
1928
1929 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1930 if (ret < 0)
1931 {
1932 close (sock); /* Avoid sd leak. */
1933 continue;
1934 }
1935
1936 ret = listen (sock, 3);
1937 if (ret < 0)
1938 {
1939 close (sock); /* Avoid sd leak. */
1940 continue;
1941 }
1942
1943 vty_event (VTY_SERV, sock, NULL);
1944 }
1945 while ((ainfo = ainfo->ai_next) != NULL);
1946
1947 freeaddrinfo (ainfo_save);
1948}
David Lamparter6d6df302014-06-28 21:12:37 +02001949#else /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001950
1951/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001952static void
paul29db05b2003-05-08 20:10:22 +00001953vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001954{
1955 int ret;
1956 union sockunion su;
1957 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001958 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001959
1960 memset (&su, 0, sizeof (union sockunion));
1961 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001962 if(addr)
1963 switch(family)
1964 {
1965 case AF_INET:
1966 naddr=&su.sin.sin_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001967 break;
paul29db05b2003-05-08 20:10:22 +00001968#ifdef HAVE_IPV6
1969 case AF_INET6:
1970 naddr=&su.sin6.sin6_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001971 break;
paul29db05b2003-05-08 20:10:22 +00001972#endif
1973 }
1974
1975 if(naddr)
1976 switch(inet_pton(family,addr,naddr))
1977 {
1978 case -1:
1979 zlog_err("bad address %s",addr);
1980 naddr=NULL;
1981 break;
1982 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001983 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001984 naddr=NULL;
1985 }
paul718e3742002-12-13 20:15:29 +00001986
1987 /* Make new socket. */
1988 accept_sock = sockunion_stream_socket (&su);
1989 if (accept_sock < 0)
1990 return;
1991
1992 /* This is server, so reuse address. */
1993 sockopt_reuseaddr (accept_sock);
1994 sockopt_reuseport (accept_sock);
1995
1996 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001997 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001998 if (ret < 0)
1999 {
paul29db05b2003-05-08 20:10:22 +00002000 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00002001 close (accept_sock); /* Avoid sd leak. */
2002 return;
2003 }
2004
2005 /* Listen socket under queue 3. */
2006 ret = listen (accept_sock, 3);
2007 if (ret < 0)
2008 {
2009 zlog (NULL, LOG_WARNING, "can't listen socket");
2010 close (accept_sock); /* Avoid sd leak. */
2011 return;
2012 }
2013
2014 /* Add vty server event. */
2015 vty_event (VTY_SERV, accept_sock, NULL);
2016}
David Lamparter6d6df302014-06-28 21:12:37 +02002017#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00002018
2019#ifdef VTYSH
2020/* For sockaddr_un. */
2021#include <sys/un.h>
2022
2023/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00002024static void
hasso6ad96ea2004-10-07 19:33:46 +00002025vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00002026{
2027 int ret;
paul75e15fe2004-10-31 02:13:09 +00002028 int sock, len;
paul718e3742002-12-13 20:15:29 +00002029 struct sockaddr_un serv;
2030 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00002031 struct zprivs_ids_t ids;
2032
paul718e3742002-12-13 20:15:29 +00002033 /* First of all, unlink existing socket */
2034 unlink (path);
2035
2036 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00002037 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00002038
2039 /* Make UNIX domain socket. */
2040 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2041 if (sock < 0)
2042 {
ajs6a52d0d2005-01-30 18:49:28 +00002043 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002044 return;
2045 }
2046
2047 /* Make server socket. */
2048 memset (&serv, 0, sizeof (struct sockaddr_un));
2049 serv.sun_family = AF_UNIX;
2050 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002051#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00002052 len = serv.sun_len = SUN_LEN(&serv);
2053#else
2054 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002055#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00002056
2057 ret = bind (sock, (struct sockaddr *) &serv, len);
2058 if (ret < 0)
2059 {
ajs6a52d0d2005-01-30 18:49:28 +00002060 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002061 close (sock); /* Avoid sd leak. */
2062 return;
2063 }
2064
2065 ret = listen (sock, 5);
2066 if (ret < 0)
2067 {
ajs6a52d0d2005-01-30 18:49:28 +00002068 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002069 close (sock); /* Avoid sd leak. */
2070 return;
2071 }
2072
2073 umask (old_mask);
2074
pauledd7c242003-06-04 13:59:38 +00002075 zprivs_get_ids(&ids);
2076
2077 if (ids.gid_vty > 0)
2078 {
2079 /* set group of socket */
2080 if ( chown (path, -1, ids.gid_vty) )
2081 {
2082 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00002083 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00002084 }
2085 }
2086
paul718e3742002-12-13 20:15:29 +00002087 vty_event (VTYSH_SERV, sock, NULL);
2088}
2089
2090/* #define VTYSH_DEBUG 1 */
2091
2092static int
2093vtysh_accept (struct thread *thread)
2094{
2095 int accept_sock;
2096 int sock;
2097 int client_len;
2098 struct sockaddr_un client;
2099 struct vty *vty;
2100
2101 accept_sock = THREAD_FD (thread);
2102
2103 vty_event (VTYSH_SERV, accept_sock, NULL);
2104
2105 memset (&client, 0, sizeof (struct sockaddr_un));
2106 client_len = sizeof (struct sockaddr_un);
2107
hassoe473b032004-09-26 16:08:11 +00002108 sock = accept (accept_sock, (struct sockaddr *) &client,
2109 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00002110
2111 if (sock < 0)
2112 {
ajs6099b3b2004-11-20 02:06:59 +00002113 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002114 return -1;
2115 }
2116
ajs9fc7ebf2005-02-23 15:12:34 +00002117 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00002118 {
ajs9fc7ebf2005-02-23 15:12:34 +00002119 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2120 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002121 close (sock);
2122 return -1;
2123 }
pauldccfb192004-10-29 08:29:36 +00002124
paul718e3742002-12-13 20:15:29 +00002125#ifdef VTYSH_DEBUG
2126 printf ("VTY shell accept\n");
2127#endif /* VTYSH_DEBUG */
2128
2129 vty = vty_new ();
2130 vty->fd = sock;
David Lamparter4715a532013-05-30 16:31:49 +02002131 vty->wfd = sock;
paul718e3742002-12-13 20:15:29 +00002132 vty->type = VTY_SHELL_SERV;
2133 vty->node = VIEW_NODE;
2134
2135 vty_event (VTYSH_READ, sock, vty);
2136
2137 return 0;
2138}
2139
2140static int
ajs9fc7ebf2005-02-23 15:12:34 +00002141vtysh_flush(struct vty *vty)
2142{
David Lamparter4715a532013-05-30 16:31:49 +02002143 switch (buffer_flush_available(vty->obuf, vty->wfd))
ajs9fc7ebf2005-02-23 15:12:34 +00002144 {
2145 case BUFFER_PENDING:
David Lamparter4715a532013-05-30 16:31:49 +02002146 vty_event(VTYSH_WRITE, vty->wfd, vty);
ajs9fc7ebf2005-02-23 15:12:34 +00002147 break;
2148 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002149 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002150 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2151 buffer_reset(vty->obuf);
2152 vty_close(vty);
2153 return -1;
2154 break;
2155 case BUFFER_EMPTY:
2156 break;
2157 }
2158 return 0;
2159}
2160
2161static int
paul718e3742002-12-13 20:15:29 +00002162vtysh_read (struct thread *thread)
2163{
2164 int ret;
2165 int sock;
2166 int nbytes;
2167 struct vty *vty;
2168 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002169 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002170 u_char header[4] = {0, 0, 0, 0};
2171
2172 sock = THREAD_FD (thread);
2173 vty = THREAD_ARG (thread);
2174 vty->t_read = NULL;
2175
ajs9fc7ebf2005-02-23 15:12:34 +00002176 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002177 {
ajs9fc7ebf2005-02-23 15:12:34 +00002178 if (nbytes < 0)
2179 {
2180 if (ERRNO_IO_RETRY(errno))
2181 {
2182 vty_event (VTYSH_READ, sock, vty);
2183 return 0;
2184 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002185 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002186 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2187 __func__, sock, safe_strerror(errno));
2188 }
2189 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002190 vty_close (vty);
2191#ifdef VTYSH_DEBUG
2192 printf ("close vtysh\n");
2193#endif /* VTYSH_DEBUG */
2194 return 0;
2195 }
2196
2197#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002198 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002199#endif /* VTYSH_DEBUG */
2200
ajs9fc7ebf2005-02-23 15:12:34 +00002201 for (p = buf; p < buf+nbytes; p++)
2202 {
2203 vty_ensure(vty, vty->length+1);
2204 vty->buf[vty->length++] = *p;
2205 if (*p == '\0')
2206 {
2207 /* Pass this line to parser. */
2208 ret = vty_execute (vty);
2209 /* Note that vty_execute clears the command buffer and resets
2210 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002211
ajs9fc7ebf2005-02-23 15:12:34 +00002212 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002213#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002214 printf ("result: %d\n", ret);
2215 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002216#endif /* VTYSH_DEBUG */
2217
ajs9fc7ebf2005-02-23 15:12:34 +00002218 header[3] = ret;
2219 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002220
ajs9fc7ebf2005-02-23 15:12:34 +00002221 if (!vty->t_write && (vtysh_flush(vty) < 0))
2222 /* Try to flush results; exit if a write error occurs. */
2223 return 0;
2224 }
2225 }
2226
paul718e3742002-12-13 20:15:29 +00002227 vty_event (VTYSH_READ, sock, vty);
2228
2229 return 0;
2230}
ajs49ff6d92004-11-04 19:26:16 +00002231
2232static int
2233vtysh_write (struct thread *thread)
2234{
2235 struct vty *vty = THREAD_ARG (thread);
2236
2237 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002238 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002239 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002240}
2241
paul718e3742002-12-13 20:15:29 +00002242#endif /* VTYSH */
2243
2244/* Determine address family to bind. */
2245void
hasso6ad96ea2004-10-07 19:33:46 +00002246vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002247{
2248 /* If port is set to 0, do not listen on TCP/IP at all! */
2249 if (port)
2250 {
2251
2252#ifdef HAVE_IPV6
paul29db05b2003-05-08 20:10:22 +00002253 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002254#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002255 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002256#endif /* HAVE_IPV6 */
2257 }
2258
2259#ifdef VTYSH
2260 vty_serv_un (path);
2261#endif /* VTYSH */
2262}
2263
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002264/* Close vty interface. Warning: call this only from functions that
2265 will be careful not to access the vty afterwards (since it has
2266 now been freed). This is safest from top-level functions (called
2267 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002268void
2269vty_close (struct vty *vty)
2270{
2271 int i;
2272
2273 /* Cancel threads.*/
2274 if (vty->t_read)
2275 thread_cancel (vty->t_read);
2276 if (vty->t_write)
2277 thread_cancel (vty->t_write);
2278 if (vty->t_timeout)
2279 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002280
2281 /* Flush buffer. */
David Lamparter4715a532013-05-30 16:31:49 +02002282 buffer_flush_all (vty->obuf, vty->wfd);
paul718e3742002-12-13 20:15:29 +00002283
2284 /* Free input buffer. */
2285 buffer_free (vty->obuf);
2286
paul718e3742002-12-13 20:15:29 +00002287 /* Free command history. */
2288 for (i = 0; i < VTY_MAXHIST; i++)
2289 if (vty->hist[i])
2290 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2291
2292 /* Unset vector. */
2293 vector_unset (vtyvec, vty->fd);
2294
2295 /* Close socket. */
2296 if (vty->fd > 0)
2297 close (vty->fd);
David Lamparterba53a8f2015-05-05 11:04:46 +02002298 else
2299 vty_stdio_reset ();
paul718e3742002-12-13 20:15:29 +00002300
paul718e3742002-12-13 20:15:29 +00002301 if (vty->buf)
2302 XFREE (MTYPE_VTY, vty->buf);
2303
2304 /* Check configure. */
2305 vty_config_unlock (vty);
2306
2307 /* OK free vty. */
2308 XFREE (MTYPE_VTY, vty);
2309}
2310
2311/* When time out occur output message then close connection. */
2312static int
2313vty_timeout (struct thread *thread)
2314{
2315 struct vty *vty;
2316
2317 vty = THREAD_ARG (thread);
2318 vty->t_timeout = NULL;
2319 vty->v_timeout = 0;
2320
2321 /* Clear buffer*/
2322 buffer_reset (vty->obuf);
2323 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2324
2325 /* Close connection. */
2326 vty->status = VTY_CLOSE;
2327 vty_close (vty);
2328
2329 return 0;
2330}
2331
2332/* Read up configuration file from file_name. */
2333static void
2334vty_read_file (FILE *confp)
2335{
2336 int ret;
2337 struct vty *vty;
Steve Hillea555002009-07-28 16:36:14 -04002338 unsigned int line_num = 0;
paul718e3742002-12-13 20:15:29 +00002339
2340 vty = vty_new ();
David Lamparter4715a532013-05-30 16:31:49 +02002341 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2342 if (vty->wfd < 0)
Steve Hillea555002009-07-28 16:36:14 -04002343 {
2344 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
David Lamparter4715a532013-05-30 16:31:49 +02002345 vty->wfd = STDOUT_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002346 }
David Lamparter4715a532013-05-30 16:31:49 +02002347 vty->fd = STDIN_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002348 vty->type = VTY_FILE;
paul718e3742002-12-13 20:15:29 +00002349 vty->node = CONFIG_NODE;
2350
2351 /* Execute configuration file */
Steve Hillea555002009-07-28 16:36:14 -04002352 ret = config_from_file (vty, confp, &line_num);
2353
2354 /* Flush any previous errors before printing messages below */
2355 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002356
paul7021c422003-07-15 12:52:22 +00002357 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002358 {
2359 switch (ret)
paul7021c422003-07-15 12:52:22 +00002360 {
2361 case CMD_ERR_AMBIGUOUS:
Steve Hillea555002009-07-28 16:36:14 -04002362 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
paul7021c422003-07-15 12:52:22 +00002363 break;
2364 case CMD_ERR_NO_MATCH:
Steve Hillea555002009-07-28 16:36:14 -04002365 fprintf (stderr, "*** Error reading config: There is no such command.\n");
paul7021c422003-07-15 12:52:22 +00002366 break;
2367 }
Steve Hillea555002009-07-28 16:36:14 -04002368 fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
2369 line_num, vty->buf);
paul718e3742002-12-13 20:15:29 +00002370 vty_close (vty);
2371 exit (1);
2372 }
2373
2374 vty_close (vty);
2375}
2376
ajs9fc7ebf2005-02-23 15:12:34 +00002377static FILE *
paul718e3742002-12-13 20:15:29 +00002378vty_use_backup_config (char *fullpath)
2379{
2380 char *fullpath_sav, *fullpath_tmp;
2381 FILE *ret = NULL;
2382 struct stat buf;
2383 int tmp, sav;
2384 int c;
2385 char buffer[512];
2386
2387 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2388 strcpy (fullpath_sav, fullpath);
2389 strcat (fullpath_sav, CONF_BACKUP_EXT);
2390 if (stat (fullpath_sav, &buf) == -1)
2391 {
2392 free (fullpath_sav);
2393 return NULL;
2394 }
2395
2396 fullpath_tmp = malloc (strlen (fullpath) + 8);
2397 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2398
2399 /* Open file to configuration write. */
2400 tmp = mkstemp (fullpath_tmp);
2401 if (tmp < 0)
2402 {
2403 free (fullpath_sav);
2404 free (fullpath_tmp);
2405 return NULL;
2406 }
2407
2408 sav = open (fullpath_sav, O_RDONLY);
2409 if (sav < 0)
2410 {
gdt3dbf9962003-12-22 20:18:18 +00002411 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002412 free (fullpath_sav);
2413 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002414 return NULL;
2415 }
2416
2417 while((c = read (sav, buffer, 512)) > 0)
2418 write (tmp, buffer, c);
2419
2420 close (sav);
2421 close (tmp);
2422
gdtaa593d52003-12-22 20:15:53 +00002423 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2424 {
gdt3dbf9962003-12-22 20:18:18 +00002425 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002426 free (fullpath_sav);
2427 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002428 return NULL;
2429 }
2430
paul718e3742002-12-13 20:15:29 +00002431 if (link (fullpath_tmp, fullpath) == 0)
2432 ret = fopen (fullpath, "r");
2433
2434 unlink (fullpath_tmp);
2435
2436 free (fullpath_sav);
2437 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002438 return ret;
paul718e3742002-12-13 20:15:29 +00002439}
2440
2441/* Read up configuration file from file_name. */
2442void
2443vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002444 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002445{
paulccc92352003-10-22 02:49:38 +00002446 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002447 FILE *confp = NULL;
2448 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002449 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002450
2451 /* If -f flag specified. */
2452 if (config_file != NULL)
2453 {
2454 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002455 {
2456 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002457 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002458 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002459 sprintf (tmp, "%s/%s", cwd, config_file);
2460 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002461 }
paul718e3742002-12-13 20:15:29 +00002462 else
hasso320ec102004-06-20 19:54:37 +00002463 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002464
2465 confp = fopen (fullpath, "r");
2466
2467 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002468 {
paul3d1dc852005-04-05 00:45:23 +00002469 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2470 __func__, fullpath, safe_strerror (errno));
2471
hasso320ec102004-06-20 19:54:37 +00002472 confp = vty_use_backup_config (fullpath);
2473 if (confp)
2474 fprintf (stderr, "WARNING: using backup configuration file!\n");
2475 else
2476 {
2477 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002478 config_file);
hasso320ec102004-06-20 19:54:37 +00002479 exit(1);
2480 }
2481 }
paul718e3742002-12-13 20:15:29 +00002482 }
2483 else
2484 {
paul718e3742002-12-13 20:15:29 +00002485#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002486 int ret;
2487 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002488
hasso320ec102004-06-20 19:54:37 +00002489 /* !!!!PLEASE LEAVE!!!!
2490 * This is NEEDED for use with vtysh -b, or else you can get
2491 * a real configuration food fight with a lot garbage in the
2492 * merged configuration file it creates coming from the per
2493 * daemon configuration files. This also allows the daemons
2494 * to start if there default configuration file is not
2495 * present or ignore them, as needed when using vtysh -b to
2496 * configure the daemons at boot - MAG
2497 */
paul718e3742002-12-13 20:15:29 +00002498
hasso320ec102004-06-20 19:54:37 +00002499 /* Stat for vtysh Zebra.conf, if found startup and wait for
2500 * boot configuration
2501 */
paul718e3742002-12-13 20:15:29 +00002502
hasso320ec102004-06-20 19:54:37 +00002503 if ( strstr(config_default_dir, "vtysh") == NULL)
2504 {
2505 ret = stat (integrate_default, &conf_stat);
2506 if (ret >= 0)
2507 return;
2508 }
paul718e3742002-12-13 20:15:29 +00002509#endif /* VTYSH */
2510
hasso320ec102004-06-20 19:54:37 +00002511 confp = fopen (config_default_dir, "r");
2512 if (confp == NULL)
2513 {
paul3d1dc852005-04-05 00:45:23 +00002514 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2515 __func__, config_default_dir, safe_strerror (errno));
2516
hasso320ec102004-06-20 19:54:37 +00002517 confp = vty_use_backup_config (config_default_dir);
2518 if (confp)
2519 {
2520 fprintf (stderr, "WARNING: using backup configuration file!\n");
2521 fullpath = config_default_dir;
2522 }
2523 else
2524 {
2525 fprintf (stderr, "can't open configuration file [%s]\n",
2526 config_default_dir);
2527 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002528 }
hasso320ec102004-06-20 19:54:37 +00002529 }
paul718e3742002-12-13 20:15:29 +00002530 else
hasso320ec102004-06-20 19:54:37 +00002531 fullpath = config_default_dir;
2532 }
2533
paul718e3742002-12-13 20:15:29 +00002534 vty_read_file (confp);
2535
2536 fclose (confp);
2537
2538 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002539
2540 if (tmp)
2541 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002542}
2543
2544/* Small utility function which output log to the VTY. */
2545void
ajs274a4a42004-12-07 15:39:31 +00002546vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002547 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002548{
hasso8c328f12004-10-05 21:01:23 +00002549 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002550 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002551
2552 if (!vtyvec)
2553 return;
paul718e3742002-12-13 20:15:29 +00002554
paul55468c82005-03-14 20:19:01 +00002555 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002556 if ((vty = vector_slot (vtyvec, i)) != NULL)
2557 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002558 {
2559 va_list ac;
2560 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002561 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002562 va_end(ac);
2563 }
paul718e3742002-12-13 20:15:29 +00002564}
2565
ajs274a4a42004-12-07 15:39:31 +00002566/* Async-signal-safe version of vty_log for fixed strings. */
2567void
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002568vty_log_fixed (char *buf, size_t len)
ajs274a4a42004-12-07 15:39:31 +00002569{
2570 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002571 struct iovec iov[2];
2572
Paul Jakmaa4b30302006-05-28 08:18:38 +00002573 /* vty may not have been initialised */
2574 if (!vtyvec)
2575 return;
2576
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002577 iov[0].iov_base = buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002578 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002579 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002580 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002581
paul55468c82005-03-14 20:19:01 +00002582 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002583 {
2584 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002585 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2586 /* N.B. We don't care about the return code, since process is
2587 most likely just about to die anyway. */
David Lamparter4715a532013-05-30 16:31:49 +02002588 writev(vty->wfd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002589 }
2590}
2591
paul718e3742002-12-13 20:15:29 +00002592int
2593vty_config_lock (struct vty *vty)
2594{
2595 if (vty_config == 0)
2596 {
2597 vty->config = 1;
2598 vty_config = 1;
2599 }
2600 return vty->config;
2601}
2602
2603int
2604vty_config_unlock (struct vty *vty)
2605{
2606 if (vty_config == 1 && vty->config == 1)
2607 {
2608 vty->config = 0;
2609 vty_config = 0;
2610 }
2611 return vty->config;
2612}
David Lamparter6b0655a2014-06-04 06:53:35 +02002613
paul718e3742002-12-13 20:15:29 +00002614/* Master of the threads. */
Donald Sharpeeef0db2015-10-14 08:50:38 -04002615static struct thread_master *vty_master;
paul718e3742002-12-13 20:15:29 +00002616
2617static void
2618vty_event (enum event event, int sock, struct vty *vty)
2619{
2620 struct thread *vty_serv_thread;
2621
2622 switch (event)
2623 {
2624 case VTY_SERV:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002625 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
paul718e3742002-12-13 20:15:29 +00002626 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2627 break;
2628#ifdef VTYSH
2629 case VTYSH_SERV:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002630 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
Christian Franke677bcbb2013-02-27 13:47:23 +00002631 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
paul718e3742002-12-13 20:15:29 +00002632 break;
2633 case VTYSH_READ:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002634 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
ajs49ff6d92004-11-04 19:26:16 +00002635 break;
2636 case VTYSH_WRITE:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002637 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002638 break;
2639#endif /* VTYSH */
2640 case VTY_READ:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002641 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
paul718e3742002-12-13 20:15:29 +00002642
2643 /* Time out treatment. */
2644 if (vty->v_timeout)
2645 {
2646 if (vty->t_timeout)
2647 thread_cancel (vty->t_timeout);
2648 vty->t_timeout =
Donald Sharpeeef0db2015-10-14 08:50:38 -04002649 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
paul718e3742002-12-13 20:15:29 +00002650 }
2651 break;
2652 case VTY_WRITE:
2653 if (! vty->t_write)
Donald Sharpeeef0db2015-10-14 08:50:38 -04002654 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
paul718e3742002-12-13 20:15:29 +00002655 break;
2656 case VTY_TIMEOUT_RESET:
2657 if (vty->t_timeout)
2658 {
2659 thread_cancel (vty->t_timeout);
2660 vty->t_timeout = NULL;
2661 }
2662 if (vty->v_timeout)
2663 {
2664 vty->t_timeout =
Donald Sharpeeef0db2015-10-14 08:50:38 -04002665 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
paul718e3742002-12-13 20:15:29 +00002666 }
2667 break;
2668 }
2669}
David Lamparter6b0655a2014-06-04 06:53:35 +02002670
paul718e3742002-12-13 20:15:29 +00002671DEFUN (config_who,
2672 config_who_cmd,
2673 "who",
2674 "Display who is on vty\n")
2675{
hasso8c328f12004-10-05 21:01:23 +00002676 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002677 struct vty *v;
2678
paul55468c82005-03-14 20:19:01 +00002679 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002680 if ((v = vector_slot (vtyvec, i)) != NULL)
2681 vty_out (vty, "%svty[%d] connected from %s.%s",
2682 v->config ? "*" : " ",
2683 i, v->address, VTY_NEWLINE);
2684 return CMD_SUCCESS;
2685}
2686
2687/* Move to vty configuration mode. */
2688DEFUN (line_vty,
2689 line_vty_cmd,
2690 "line vty",
2691 "Configure a terminal line\n"
2692 "Virtual terminal\n")
2693{
2694 vty->node = VTY_NODE;
2695 return CMD_SUCCESS;
2696}
2697
2698/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002699static int
paul9035efa2004-10-10 11:56:56 +00002700exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002701{
2702 unsigned long timeout = 0;
2703
2704 /* min_str and sec_str are already checked by parser. So it must be
2705 all digit string. */
2706 if (min_str)
2707 {
2708 timeout = strtol (min_str, NULL, 10);
2709 timeout *= 60;
2710 }
2711 if (sec_str)
2712 timeout += strtol (sec_str, NULL, 10);
2713
2714 vty_timeout_val = timeout;
2715 vty->v_timeout = timeout;
2716 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2717
2718
2719 return CMD_SUCCESS;
2720}
2721
2722DEFUN (exec_timeout_min,
2723 exec_timeout_min_cmd,
2724 "exec-timeout <0-35791>",
2725 "Set timeout value\n"
2726 "Timeout value in minutes\n")
2727{
2728 return exec_timeout (vty, argv[0], NULL);
2729}
2730
2731DEFUN (exec_timeout_sec,
2732 exec_timeout_sec_cmd,
2733 "exec-timeout <0-35791> <0-2147483>",
2734 "Set the EXEC timeout\n"
2735 "Timeout in minutes\n"
2736 "Timeout in seconds\n")
2737{
2738 return exec_timeout (vty, argv[0], argv[1]);
2739}
2740
2741DEFUN (no_exec_timeout,
2742 no_exec_timeout_cmd,
2743 "no exec-timeout",
2744 NO_STR
2745 "Set the EXEC timeout\n")
2746{
2747 return exec_timeout (vty, NULL, NULL);
2748}
2749
2750/* Set vty access class. */
2751DEFUN (vty_access_class,
2752 vty_access_class_cmd,
2753 "access-class WORD",
2754 "Filter connections based on an IP access list\n"
2755 "IP access list\n")
2756{
2757 if (vty_accesslist_name)
2758 XFREE(MTYPE_VTY, vty_accesslist_name);
2759
2760 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
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{
2773 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2774 {
2775 vty_out (vty, "Access-class is not currently applied to vty%s",
2776 VTY_NEWLINE);
2777 return CMD_WARNING;
2778 }
2779
2780 XFREE(MTYPE_VTY, vty_accesslist_name);
2781
2782 vty_accesslist_name = NULL;
2783
2784 return CMD_SUCCESS;
2785}
2786
2787#ifdef HAVE_IPV6
2788/* Set vty access class. */
2789DEFUN (vty_ipv6_access_class,
2790 vty_ipv6_access_class_cmd,
2791 "ipv6 access-class WORD",
2792 IPV6_STR
2793 "Filter connections based on an IP access list\n"
2794 "IPv6 access list\n")
2795{
2796 if (vty_ipv6_accesslist_name)
2797 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2798
2799 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2800
2801 return CMD_SUCCESS;
2802}
2803
2804/* Clear vty access class. */
2805DEFUN (no_vty_ipv6_access_class,
2806 no_vty_ipv6_access_class_cmd,
2807 "no ipv6 access-class [WORD]",
2808 NO_STR
2809 IPV6_STR
2810 "Filter connections based on an IP access list\n"
2811 "IPv6 access list\n")
2812{
2813 if (! vty_ipv6_accesslist_name ||
2814 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2815 {
2816 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2817 VTY_NEWLINE);
2818 return CMD_WARNING;
2819 }
2820
2821 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2822
2823 vty_ipv6_accesslist_name = NULL;
2824
2825 return CMD_SUCCESS;
2826}
2827#endif /* HAVE_IPV6 */
2828
2829/* vty login. */
2830DEFUN (vty_login,
2831 vty_login_cmd,
2832 "login",
2833 "Enable password checking\n")
2834{
2835 no_password_check = 0;
2836 return CMD_SUCCESS;
2837}
2838
2839DEFUN (no_vty_login,
2840 no_vty_login_cmd,
2841 "no login",
2842 NO_STR
2843 "Enable password checking\n")
2844{
2845 no_password_check = 1;
2846 return CMD_SUCCESS;
2847}
2848
Paul Jakma62687ff2008-08-23 14:27:06 +01002849/* initial mode. */
2850DEFUN (vty_restricted_mode,
2851 vty_restricted_mode_cmd,
2852 "anonymous restricted",
2853 "Restrict view commands available in anonymous, unauthenticated vty\n")
2854{
2855 restricted_mode = 1;
2856 return CMD_SUCCESS;
2857}
2858
2859DEFUN (vty_no_restricted_mode,
2860 vty_no_restricted_mode_cmd,
2861 "no anonymous restricted",
2862 NO_STR
2863 "Enable password checking\n")
2864{
2865 restricted_mode = 0;
2866 return CMD_SUCCESS;
2867}
2868
paul718e3742002-12-13 20:15:29 +00002869DEFUN (service_advanced_vty,
2870 service_advanced_vty_cmd,
2871 "service advanced-vty",
2872 "Set up miscellaneous service\n"
2873 "Enable advanced mode vty interface\n")
2874{
2875 host.advanced = 1;
2876 return CMD_SUCCESS;
2877}
2878
2879DEFUN (no_service_advanced_vty,
2880 no_service_advanced_vty_cmd,
2881 "no service advanced-vty",
2882 NO_STR
2883 "Set up miscellaneous service\n"
2884 "Enable advanced mode vty interface\n")
2885{
2886 host.advanced = 0;
2887 return CMD_SUCCESS;
2888}
2889
2890DEFUN (terminal_monitor,
2891 terminal_monitor_cmd,
2892 "terminal monitor",
2893 "Set terminal line parameters\n"
2894 "Copy debug output to the current terminal line\n")
2895{
2896 vty->monitor = 1;
2897 return CMD_SUCCESS;
2898}
2899
2900DEFUN (terminal_no_monitor,
2901 terminal_no_monitor_cmd,
2902 "terminal no monitor",
2903 "Set terminal line parameters\n"
2904 NO_STR
2905 "Copy debug output to the current terminal line\n")
2906{
2907 vty->monitor = 0;
2908 return CMD_SUCCESS;
2909}
2910
paul789f78a2006-01-17 17:42:03 +00002911ALIAS (terminal_no_monitor,
2912 no_terminal_monitor_cmd,
2913 "no terminal monitor",
2914 NO_STR
2915 "Set terminal line parameters\n"
2916 "Copy debug output to the current terminal line\n")
2917
paul718e3742002-12-13 20:15:29 +00002918DEFUN (show_history,
2919 show_history_cmd,
2920 "show history",
2921 SHOW_STR
2922 "Display the session command history\n")
2923{
2924 int index;
2925
2926 for (index = vty->hindex + 1; index != vty->hindex;)
2927 {
2928 if (index == VTY_MAXHIST)
2929 {
2930 index = 0;
2931 continue;
2932 }
2933
2934 if (vty->hist[index] != NULL)
2935 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2936
2937 index++;
2938 }
2939
2940 return CMD_SUCCESS;
2941}
2942
Lou Berger86b2a0a2016-05-17 12:19:51 -04002943/* vty login. */
2944DEFUN (log_commands,
2945 log_commands_cmd,
2946 "log commands",
2947 "Logging control\n"
2948 "Log all commands (can't be unset without restart)\n")
2949{
2950 do_log_commands = 1;
2951 return CMD_SUCCESS;
2952}
2953
paul718e3742002-12-13 20:15:29 +00002954/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002955static int
paul718e3742002-12-13 20:15:29 +00002956vty_config_write (struct vty *vty)
2957{
2958 vty_out (vty, "line vty%s", VTY_NEWLINE);
2959
2960 if (vty_accesslist_name)
2961 vty_out (vty, " access-class %s%s",
2962 vty_accesslist_name, VTY_NEWLINE);
2963
2964 if (vty_ipv6_accesslist_name)
2965 vty_out (vty, " ipv6 access-class %s%s",
2966 vty_ipv6_accesslist_name, VTY_NEWLINE);
2967
2968 /* exec-timeout */
2969 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2970 vty_out (vty, " exec-timeout %ld %ld%s",
2971 vty_timeout_val / 60,
2972 vty_timeout_val % 60, VTY_NEWLINE);
2973
2974 /* login */
2975 if (no_password_check)
2976 vty_out (vty, " no login%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +01002977
2978 if (restricted_mode != restricted_mode_default)
2979 {
2980 if (restricted_mode_default)
2981 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
2982 else
2983 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
2984 }
2985
Lou Berger86b2a0a2016-05-17 12:19:51 -04002986 if (do_log_commands)
2987 vty_out (vty, "log commands%s", VTY_NEWLINE);
2988
paul718e3742002-12-13 20:15:29 +00002989 vty_out (vty, "!%s", VTY_NEWLINE);
2990
2991 return CMD_SUCCESS;
2992}
2993
2994struct cmd_node vty_node =
2995{
2996 VTY_NODE,
2997 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002998 1,
paul718e3742002-12-13 20:15:29 +00002999};
3000
3001/* Reset all VTY status. */
3002void
3003vty_reset ()
3004{
hasso8c328f12004-10-05 21:01:23 +00003005 unsigned int i;
paul718e3742002-12-13 20:15:29 +00003006 struct vty *vty;
3007 struct thread *vty_serv_thread;
3008
paul55468c82005-03-14 20:19:01 +00003009 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00003010 if ((vty = vector_slot (vtyvec, i)) != NULL)
3011 {
3012 buffer_reset (vty->obuf);
3013 vty->status = VTY_CLOSE;
3014 vty_close (vty);
3015 }
3016
paul55468c82005-03-14 20:19:01 +00003017 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00003018 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
3019 {
3020 thread_cancel (vty_serv_thread);
3021 vector_slot (Vvty_serv_thread, i) = NULL;
3022 close (i);
3023 }
3024
3025 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3026
3027 if (vty_accesslist_name)
3028 {
3029 XFREE(MTYPE_VTY, vty_accesslist_name);
3030 vty_accesslist_name = NULL;
3031 }
3032
3033 if (vty_ipv6_accesslist_name)
3034 {
3035 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
3036 vty_ipv6_accesslist_name = NULL;
3037 }
3038}
3039
ajs9fc7ebf2005-02-23 15:12:34 +00003040static void
3041vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00003042{
paul79ad2792003-10-15 22:09:28 +00003043 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00003044 char *c;
paul718e3742002-12-13 20:15:29 +00003045
paulccc92352003-10-22 02:49:38 +00003046 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00003047
paulccc92352003-10-22 02:49:38 +00003048 if (!c)
paul79ad2792003-10-15 22:09:28 +00003049 {
3050 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00003051 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00003052 }
paul718e3742002-12-13 20:15:29 +00003053
3054 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3055 strcpy (vty_cwd, cwd);
3056}
3057
3058char *
3059vty_get_cwd ()
3060{
3061 return vty_cwd;
3062}
3063
3064int
3065vty_shell (struct vty *vty)
3066{
3067 return vty->type == VTY_SHELL ? 1 : 0;
3068}
3069
3070int
3071vty_shell_serv (struct vty *vty)
3072{
3073 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3074}
3075
3076void
3077vty_init_vtysh ()
3078{
3079 vtyvec = vector_init (VECTOR_MIN_SIZE);
3080}
3081
3082/* Install vty's own commands like `who' command. */
3083void
paulb21b19c2003-06-15 01:28:29 +00003084vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00003085{
3086 /* For further configuration read, preserve current directory. */
3087 vty_save_cwd ();
3088
3089 vtyvec = vector_init (VECTOR_MIN_SIZE);
3090
Donald Sharpeeef0db2015-10-14 08:50:38 -04003091 vty_master = master_thread;
paulb21b19c2003-06-15 01:28:29 +00003092
David Lamparterba53a8f2015-05-05 11:04:46 +02003093 atexit (vty_stdio_reset);
3094
paul718e3742002-12-13 20:15:29 +00003095 /* Initilize server thread vector. */
3096 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3097
3098 /* Install bgp top node. */
3099 install_node (&vty_node, vty_config_write);
3100
Paul Jakma62687ff2008-08-23 14:27:06 +01003101 install_element (RESTRICTED_NODE, &config_who_cmd);
3102 install_element (RESTRICTED_NODE, &show_history_cmd);
paul718e3742002-12-13 20:15:29 +00003103 install_element (VIEW_NODE, &config_who_cmd);
3104 install_element (VIEW_NODE, &show_history_cmd);
3105 install_element (ENABLE_NODE, &config_who_cmd);
3106 install_element (CONFIG_NODE, &line_vty_cmd);
3107 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3108 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3109 install_element (CONFIG_NODE, &show_history_cmd);
Lou Berger86b2a0a2016-05-17 12:19:51 -04003110 install_element (CONFIG_NODE, &log_commands_cmd);
paul718e3742002-12-13 20:15:29 +00003111 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3112 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00003113 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00003114 install_element (ENABLE_NODE, &show_history_cmd);
3115
3116 install_default (VTY_NODE);
3117 install_element (VTY_NODE, &exec_timeout_min_cmd);
3118 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3119 install_element (VTY_NODE, &no_exec_timeout_cmd);
3120 install_element (VTY_NODE, &vty_access_class_cmd);
3121 install_element (VTY_NODE, &no_vty_access_class_cmd);
3122 install_element (VTY_NODE, &vty_login_cmd);
3123 install_element (VTY_NODE, &no_vty_login_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01003124 install_element (VTY_NODE, &vty_restricted_mode_cmd);
3125 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
paul718e3742002-12-13 20:15:29 +00003126#ifdef HAVE_IPV6
3127 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3128 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3129#endif /* HAVE_IPV6 */
3130}
Chris Caputo228da422009-07-18 05:44:03 +00003131
3132void
3133vty_terminate (void)
3134{
3135 if (vty_cwd)
3136 XFREE (MTYPE_TMP, vty_cwd);
3137
3138 if (vtyvec && Vvty_serv_thread)
3139 {
3140 vty_reset ();
3141 vector_free (vtyvec);
3142 vector_free (Vvty_serv_thread);
3143 }
3144}