blob: 24df32c42cdb195548099eacd307869486e721ea [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>
paul718e3742002-12-13 20:15:29 +000041
42/* Vty events */
43enum event
44{
45 VTY_SERV,
46 VTY_READ,
47 VTY_WRITE,
48 VTY_TIMEOUT_RESET,
49#ifdef VTYSH
50 VTYSH_SERV,
ajs49ff6d92004-11-04 19:26:16 +000051 VTYSH_READ,
52 VTYSH_WRITE
paul718e3742002-12-13 20:15:29 +000053#endif /* VTYSH */
54};
55
56static void vty_event (enum event, int, struct vty *);
57
58/* Extern host structure from command.c */
59extern struct host host;
David Lamparter6b0655a2014-06-04 06:53:35 +020060
paul718e3742002-12-13 20:15:29 +000061/* Vector which store each vty structure. */
62static vector vtyvec;
63
64/* Vty timeout value. */
65static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
66
67/* Vty access-class command */
68static char *vty_accesslist_name = NULL;
69
70/* Vty access-calss for IPv6. */
71static char *vty_ipv6_accesslist_name = NULL;
72
73/* VTY server thread. */
Christian Franke677bcbb2013-02-27 13:47:23 +000074static vector Vvty_serv_thread;
paul718e3742002-12-13 20:15:29 +000075
76/* Current directory. */
77char *vty_cwd = NULL;
78
79/* Configure lock. */
80static int vty_config;
81
82/* Login password check. */
83static int no_password_check = 0;
84
Paul Jakma62687ff2008-08-23 14:27:06 +010085/* Restrict unauthenticated logins? */
86static const u_char restricted_mode_default = 0;
87static u_char restricted_mode = 0;
88
paul718e3742002-12-13 20:15:29 +000089/* Integrated configuration file path */
90char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
91
David Lamparter6b0655a2014-06-04 06:53:35 +020092
paul718e3742002-12-13 20:15:29 +000093/* VTY standard output function. */
94int
95vty_out (struct vty *vty, const char *format, ...)
96{
97 va_list args;
98 int len = 0;
99 int size = 1024;
100 char buf[1024];
101 char *p = NULL;
paul718e3742002-12-13 20:15:29 +0000102
103 if (vty_shell (vty))
ajsd246bd92004-11-23 17:35:08 +0000104 {
105 va_start (args, format);
106 vprintf (format, args);
107 va_end (args);
108 }
paul718e3742002-12-13 20:15:29 +0000109 else
110 {
111 /* Try to write to initial buffer. */
ajsd246bd92004-11-23 17:35:08 +0000112 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000113 len = vsnprintf (buf, sizeof buf, format, args);
ajsd246bd92004-11-23 17:35:08 +0000114 va_end (args);
paul718e3742002-12-13 20:15:29 +0000115
116 /* Initial buffer is not enough. */
117 if (len < 0 || len >= size)
118 {
119 while (1)
120 {
121 if (len > -1)
122 size = len + 1;
123 else
124 size = size * 2;
125
126 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
127 if (! p)
128 return -1;
129
ajsd246bd92004-11-23 17:35:08 +0000130 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000131 len = vsnprintf (p, size, format, args);
ajsd246bd92004-11-23 17:35:08 +0000132 va_end (args);
paul718e3742002-12-13 20:15:29 +0000133
134 if (len > -1 && len < size)
135 break;
136 }
137 }
138
139 /* When initial buffer is enough to store all output. */
140 if (! p)
141 p = buf;
142
143 /* Pointer p must point out buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +0000144 buffer_put (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000145
146 /* If p is not different with buf, it is allocated buffer. */
147 if (p != buf)
148 XFREE (MTYPE_VTY_OUT_BUF, p);
149 }
150
paul718e3742002-12-13 20:15:29 +0000151 return len;
152}
153
ajsd246bd92004-11-23 17:35:08 +0000154static int
ajs274a4a42004-12-07 15:39:31 +0000155vty_log_out (struct vty *vty, const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000156 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +0000157{
ajs9fc7ebf2005-02-23 15:12:34 +0000158 int ret;
paul718e3742002-12-13 20:15:29 +0000159 int len;
160 char buf[1024];
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000161
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000162 if (!ctl->already_rendered)
163 {
164 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
165 ctl->already_rendered = 1;
166 }
167 if (ctl->len+1 >= sizeof(buf))
168 return -1;
169 memcpy(buf, ctl->buf, len = ctl->len);
170 buf[len++] = ' ';
171 buf[len] = '\0';
paul718e3742002-12-13 20:15:29 +0000172
ajs274a4a42004-12-07 15:39:31 +0000173 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000174 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000175 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000176 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
177 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000178 return -1;
paul718e3742002-12-13 20:15:29 +0000179
ajs9fc7ebf2005-02-23 15:12:34 +0000180 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
181 ((size_t)((len += ret)+2) > sizeof(buf)))
182 return -1;
paul718e3742002-12-13 20:15:29 +0000183
ajs9fc7ebf2005-02-23 15:12:34 +0000184 buf[len++] = '\r';
185 buf[len++] = '\n';
186
David Lamparter4715a532013-05-30 16:31:49 +0200187 if (write(vty->wfd, buf, len) < 0)
ajs9fc7ebf2005-02-23 15:12:34 +0000188 {
189 if (ERRNO_IO_RETRY(errno))
190 /* Kernel buffer is full, probably too much debugging output, so just
191 drop the data and ignore. */
192 return -1;
193 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000194 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000195 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
196 __func__, vty->fd, safe_strerror(errno));
197 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000198 /* cannot call vty_close, because a parent routine may still try
199 to access the vty struct */
200 vty->status = VTY_CLOSE;
201 shutdown(vty->fd, SHUT_RDWR);
ajs9fc7ebf2005-02-23 15:12:34 +0000202 return -1;
203 }
204 return 0;
paul718e3742002-12-13 20:15:29 +0000205}
206
207/* Output current time to the vty. */
208void
209vty_time_print (struct vty *vty, int cr)
210{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000211 char buf [25];
paul718e3742002-12-13 20:15:29 +0000212
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000213 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
paul718e3742002-12-13 20:15:29 +0000214 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000215 zlog (NULL, LOG_INFO, "quagga_timestamp error");
paul718e3742002-12-13 20:15:29 +0000216 return;
217 }
218 if (cr)
219 vty_out (vty, "%s\n", buf);
220 else
221 vty_out (vty, "%s ", buf);
222
223 return;
224}
225
226/* Say hello to vty interface. */
227void
228vty_hello (struct vty *vty)
229{
paul3b0c5d92005-03-08 10:43:43 +0000230 if (host.motdfile)
231 {
232 FILE *f;
233 char buf[4096];
paul22085182005-03-08 16:00:12 +0000234
paul3b0c5d92005-03-08 10:43:43 +0000235 f = fopen (host.motdfile, "r");
236 if (f)
237 {
paulb45da6f2005-03-08 15:16:57 +0000238 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000239 {
paulb45da6f2005-03-08 15:16:57 +0000240 char *s;
paul22085182005-03-08 16:00:12 +0000241 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000242 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000243 s--);
244 *s = '\0';
245 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
246 }
paul3b0c5d92005-03-08 10:43:43 +0000247 fclose (f);
248 }
249 else
paulb45da6f2005-03-08 15:16:57 +0000250 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000251 }
252 else if (host.motd)
Nico Goldeb830c892010-08-01 15:24:35 +0200253 vty_out (vty, "%s", host.motd);
paul718e3742002-12-13 20:15:29 +0000254}
255
256/* Put out prompt and wait input from user. */
257static void
258vty_prompt (struct vty *vty)
259{
260 struct utsname names;
261 const char*hostname;
262
263 if (vty->type == VTY_TERM)
264 {
265 hostname = host.name;
266 if (!hostname)
267 {
268 uname (&names);
269 hostname = names.nodename;
270 }
271 vty_out (vty, cmd_prompt (vty->node), hostname);
272 }
273}
274
275/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000276static void
paul718e3742002-12-13 20:15:29 +0000277vty_will_echo (struct vty *vty)
278{
paul02ff83c2004-06-11 11:27:03 +0000279 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000280 vty_out (vty, "%s", cmd);
281}
282
283/* Make suppress Go-Ahead telnet option. */
284static void
285vty_will_suppress_go_ahead (struct vty *vty)
286{
paul02ff83c2004-06-11 11:27:03 +0000287 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000288 vty_out (vty, "%s", cmd);
289}
290
291/* Make don't use linemode over telnet. */
292static void
293vty_dont_linemode (struct vty *vty)
294{
paul02ff83c2004-06-11 11:27:03 +0000295 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000296 vty_out (vty, "%s", cmd);
297}
298
299/* Use window size. */
300static void
301vty_do_window_size (struct vty *vty)
302{
paul02ff83c2004-06-11 11:27:03 +0000303 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000304 vty_out (vty, "%s", cmd);
305}
306
307#if 0 /* Currently not used. */
308/* Make don't use lflow vty interface. */
309static void
310vty_dont_lflow_ahead (struct vty *vty)
311{
paul02ff83c2004-06-11 11:27:03 +0000312 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000313 vty_out (vty, "%s", cmd);
314}
315#endif /* 0 */
316
317/* Allocate new vty struct. */
318struct vty *
319vty_new ()
320{
321 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
322
ajs9fc7ebf2005-02-23 15:12:34 +0000323 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000324 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
325 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000326
327 return new;
328}
329
330/* Authentication of vty */
331static void
332vty_auth (struct vty *vty, char *buf)
333{
334 char *passwd = NULL;
335 enum node_type next_node = 0;
336 int fail;
337 char *crypt (const char *, const char *);
338
339 switch (vty->node)
340 {
341 case AUTH_NODE:
342 if (host.encrypt)
343 passwd = host.password_encrypt;
344 else
345 passwd = host.password;
346 if (host.advanced)
347 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
348 else
349 next_node = VIEW_NODE;
350 break;
351 case AUTH_ENABLE_NODE:
352 if (host.encrypt)
353 passwd = host.enable_encrypt;
354 else
355 passwd = host.enable;
356 next_node = ENABLE_NODE;
357 break;
358 }
359
360 if (passwd)
361 {
362 if (host.encrypt)
363 fail = strcmp (crypt(buf, passwd), passwd);
364 else
365 fail = strcmp (buf, passwd);
366 }
367 else
368 fail = 1;
369
370 if (! fail)
371 {
372 vty->fail = 0;
373 vty->node = next_node; /* Success ! */
374 }
375 else
376 {
377 vty->fail++;
378 if (vty->fail >= 3)
379 {
380 if (vty->node == AUTH_NODE)
381 {
382 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
383 vty->status = VTY_CLOSE;
384 }
385 else
386 {
387 /* AUTH_ENABLE_NODE */
388 vty->fail = 0;
389 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +0100390 vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
paul718e3742002-12-13 20:15:29 +0000391 }
392 }
393 }
394}
395
396/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000397static int
paul718e3742002-12-13 20:15:29 +0000398vty_command (struct vty *vty, char *buf)
399{
400 int ret;
401 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000402 const char *protocolname;
paul718e3742002-12-13 20:15:29 +0000403
404 /* Split readline string up into the vector */
405 vline = cmd_make_strvec (buf);
406
407 if (vline == NULL)
408 return CMD_SUCCESS;
409
ajs924b9222005-04-16 17:11:24 +0000410#ifdef CONSUMED_TIME_CHECK
411 {
412 RUSAGE_T before;
413 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000414 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000415
416 GETRUSAGE(&before);
417#endif /* CONSUMED_TIME_CHECK */
418
hasso87d683b2005-01-16 23:31:54 +0000419 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000420
vincentfbf5d032005-09-29 11:25:50 +0000421 /* Get the name of the protocol if any */
422 if (zlog_default)
423 protocolname = zlog_proto_names[zlog_default->protocol];
424 else
425 protocolname = zlog_proto_names[ZLOG_NONE];
426
ajs924b9222005-04-16 17:11:24 +0000427#ifdef CONSUMED_TIME_CHECK
428 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000429 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
430 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000431 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000432 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
433 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000434 }
435#endif /* CONSUMED_TIME_CHECK */
436
paul718e3742002-12-13 20:15:29 +0000437 if (ret != CMD_SUCCESS)
438 switch (ret)
439 {
440 case CMD_WARNING:
441 if (vty->type == VTY_FILE)
442 vty_out (vty, "Warning...%s", VTY_NEWLINE);
443 break;
444 case CMD_ERR_AMBIGUOUS:
445 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
446 break;
447 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000448 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000449 break;
450 case CMD_ERR_INCOMPLETE:
451 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
452 break;
453 }
454 cmd_free_strvec (vline);
455
456 return ret;
457}
David Lamparter6b0655a2014-06-04 06:53:35 +0200458
ajs9fc7ebf2005-02-23 15:12:34 +0000459static const char telnet_backward_char = 0x08;
460static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000461
462/* Basic function to write buffer to vty. */
463static void
ajs9fc7ebf2005-02-23 15:12:34 +0000464vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000465{
466 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
467 return;
468
469 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000470 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000471}
472
473/* Ensure length of input buffer. Is buffer is short, double it. */
474static void
475vty_ensure (struct vty *vty, int length)
476{
477 if (vty->max <= length)
478 {
479 vty->max *= 2;
480 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
481 }
482}
483
484/* Basic function to insert character into vty. */
485static void
486vty_self_insert (struct vty *vty, char c)
487{
488 int i;
489 int length;
490
491 vty_ensure (vty, vty->length + 1);
492 length = vty->length - vty->cp;
493 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
494 vty->buf[vty->cp] = c;
495
496 vty_write (vty, &vty->buf[vty->cp], length + 1);
497 for (i = 0; i < length; i++)
498 vty_write (vty, &telnet_backward_char, 1);
499
500 vty->cp++;
501 vty->length++;
502}
503
504/* Self insert character 'c' in overwrite mode. */
505static void
506vty_self_insert_overwrite (struct vty *vty, char c)
507{
508 vty_ensure (vty, vty->length + 1);
509 vty->buf[vty->cp++] = c;
510
511 if (vty->cp > vty->length)
512 vty->length++;
513
514 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
515 return;
516
517 vty_write (vty, &c, 1);
518}
519
520/* Insert a word into vty interface with overwrite mode. */
521static void
522vty_insert_word_overwrite (struct vty *vty, char *str)
523{
524 int len = strlen (str);
525 vty_write (vty, str, len);
526 strcpy (&vty->buf[vty->cp], str);
527 vty->cp += len;
528 vty->length = vty->cp;
529}
530
531/* Forward character. */
532static void
533vty_forward_char (struct vty *vty)
534{
535 if (vty->cp < vty->length)
536 {
537 vty_write (vty, &vty->buf[vty->cp], 1);
538 vty->cp++;
539 }
540}
541
542/* Backward character. */
543static void
544vty_backward_char (struct vty *vty)
545{
546 if (vty->cp > 0)
547 {
548 vty->cp--;
549 vty_write (vty, &telnet_backward_char, 1);
550 }
551}
552
553/* Move to the beginning of the line. */
554static void
555vty_beginning_of_line (struct vty *vty)
556{
557 while (vty->cp)
558 vty_backward_char (vty);
559}
560
561/* Move to the end of the line. */
562static void
563vty_end_of_line (struct vty *vty)
564{
565 while (vty->cp < vty->length)
566 vty_forward_char (vty);
567}
568
569static void vty_kill_line_from_beginning (struct vty *);
570static void vty_redraw_line (struct vty *);
571
572/* Print command line history. This function is called from
573 vty_next_line and vty_previous_line. */
574static void
575vty_history_print (struct vty *vty)
576{
577 int length;
578
579 vty_kill_line_from_beginning (vty);
580
581 /* Get previous line from history buffer */
582 length = strlen (vty->hist[vty->hp]);
583 memcpy (vty->buf, vty->hist[vty->hp], length);
584 vty->cp = vty->length = length;
585
586 /* Redraw current line */
587 vty_redraw_line (vty);
588}
589
590/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000591static void
paul718e3742002-12-13 20:15:29 +0000592vty_next_line (struct vty *vty)
593{
594 int try_index;
595
596 if (vty->hp == vty->hindex)
597 return;
598
599 /* Try is there history exist or not. */
600 try_index = vty->hp;
601 if (try_index == (VTY_MAXHIST - 1))
602 try_index = 0;
603 else
604 try_index++;
605
606 /* If there is not history return. */
607 if (vty->hist[try_index] == NULL)
608 return;
609 else
610 vty->hp = try_index;
611
612 vty_history_print (vty);
613}
614
615/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000616static void
paul718e3742002-12-13 20:15:29 +0000617vty_previous_line (struct vty *vty)
618{
619 int try_index;
620
621 try_index = vty->hp;
622 if (try_index == 0)
623 try_index = VTY_MAXHIST - 1;
624 else
625 try_index--;
626
627 if (vty->hist[try_index] == NULL)
628 return;
629 else
630 vty->hp = try_index;
631
632 vty_history_print (vty);
633}
634
635/* This function redraw all of the command line character. */
636static void
637vty_redraw_line (struct vty *vty)
638{
639 vty_write (vty, vty->buf, vty->length);
640 vty->cp = vty->length;
641}
642
643/* Forward word. */
644static void
645vty_forward_word (struct vty *vty)
646{
647 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
648 vty_forward_char (vty);
649
650 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
651 vty_forward_char (vty);
652}
653
654/* Backward word without skipping training space. */
655static void
656vty_backward_pure_word (struct vty *vty)
657{
658 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
659 vty_backward_char (vty);
660}
661
662/* Backward word. */
663static void
664vty_backward_word (struct vty *vty)
665{
666 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
667 vty_backward_char (vty);
668
669 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
670 vty_backward_char (vty);
671}
672
673/* When '^D' is typed at the beginning of the line we move to the down
674 level. */
675static void
676vty_down_level (struct vty *vty)
677{
678 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000679 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000680 vty_prompt (vty);
681 vty->cp = 0;
682}
683
684/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000685static void
paul718e3742002-12-13 20:15:29 +0000686vty_end_config (struct vty *vty)
687{
688 vty_out (vty, "%s", VTY_NEWLINE);
689
690 switch (vty->node)
691 {
692 case VIEW_NODE:
693 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +0100694 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +0000695 /* Nothing to do. */
696 break;
697 case CONFIG_NODE:
698 case INTERFACE_NODE:
699 case ZEBRA_NODE:
700 case RIP_NODE:
701 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +0100702 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +0000703 case BGP_NODE:
704 case BGP_VPNV4_NODE:
705 case BGP_IPV4_NODE:
706 case BGP_IPV4M_NODE:
707 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000708 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000709 case RMAP_NODE:
710 case OSPF_NODE:
711 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000712 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000713 case KEYCHAIN_NODE:
714 case KEYCHAIN_KEY_NODE:
715 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -0200716 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +0000717 case VTY_NODE:
718 vty_config_unlock (vty);
719 vty->node = ENABLE_NODE;
720 break;
721 default:
722 /* Unknown node, we have to ignore it. */
723 break;
724 }
725
726 vty_prompt (vty);
727 vty->cp = 0;
728}
729
730/* Delete a charcter at the current point. */
731static void
732vty_delete_char (struct vty *vty)
733{
734 int i;
735 int size;
736
paul718e3742002-12-13 20:15:29 +0000737 if (vty->length == 0)
738 {
739 vty_down_level (vty);
740 return;
741 }
742
743 if (vty->cp == vty->length)
744 return; /* completion need here? */
745
746 size = vty->length - vty->cp;
747
748 vty->length--;
749 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
750 vty->buf[vty->length] = '\0';
Roy7f794f22008-08-13 17:27:38 +0100751
752 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
753 return;
paul718e3742002-12-13 20:15:29 +0000754
755 vty_write (vty, &vty->buf[vty->cp], size - 1);
756 vty_write (vty, &telnet_space_char, 1);
757
758 for (i = 0; i < size; i++)
759 vty_write (vty, &telnet_backward_char, 1);
760}
761
762/* Delete a character before the point. */
763static void
764vty_delete_backward_char (struct vty *vty)
765{
766 if (vty->cp == 0)
767 return;
768
769 vty_backward_char (vty);
770 vty_delete_char (vty);
771}
772
773/* Kill rest of line from current point. */
774static void
775vty_kill_line (struct vty *vty)
776{
777 int i;
778 int size;
779
780 size = vty->length - vty->cp;
781
782 if (size == 0)
783 return;
784
785 for (i = 0; i < size; i++)
786 vty_write (vty, &telnet_space_char, 1);
787 for (i = 0; i < size; i++)
788 vty_write (vty, &telnet_backward_char, 1);
789
790 memset (&vty->buf[vty->cp], 0, size);
791 vty->length = vty->cp;
792}
793
794/* Kill line from the beginning. */
795static void
796vty_kill_line_from_beginning (struct vty *vty)
797{
798 vty_beginning_of_line (vty);
799 vty_kill_line (vty);
800}
801
802/* Delete a word before the point. */
803static void
804vty_forward_kill_word (struct vty *vty)
805{
806 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
807 vty_delete_char (vty);
808 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
809 vty_delete_char (vty);
810}
811
812/* Delete a word before the point. */
813static void
814vty_backward_kill_word (struct vty *vty)
815{
816 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
817 vty_delete_backward_char (vty);
818 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
819 vty_delete_backward_char (vty);
820}
821
822/* Transpose chars before or at the point. */
823static void
824vty_transpose_chars (struct vty *vty)
825{
826 char c1, c2;
827
828 /* If length is short or point is near by the beginning of line then
829 return. */
830 if (vty->length < 2 || vty->cp < 1)
831 return;
832
833 /* In case of point is located at the end of the line. */
834 if (vty->cp == vty->length)
835 {
836 c1 = vty->buf[vty->cp - 1];
837 c2 = vty->buf[vty->cp - 2];
838
839 vty_backward_char (vty);
840 vty_backward_char (vty);
841 vty_self_insert_overwrite (vty, c1);
842 vty_self_insert_overwrite (vty, c2);
843 }
844 else
845 {
846 c1 = vty->buf[vty->cp];
847 c2 = vty->buf[vty->cp - 1];
848
849 vty_backward_char (vty);
850 vty_self_insert_overwrite (vty, c1);
851 vty_self_insert_overwrite (vty, c2);
852 }
853}
854
855/* Do completion at vty interface. */
856static void
857vty_complete_command (struct vty *vty)
858{
859 int i;
860 int ret;
861 char **matched = NULL;
862 vector vline;
863
864 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
865 return;
866
867 vline = cmd_make_strvec (vty->buf);
868 if (vline == NULL)
869 return;
870
871 /* In case of 'help \t'. */
872 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +0100873 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000874
875 matched = cmd_complete_command (vline, vty, &ret);
876
877 cmd_free_strvec (vline);
878
879 vty_out (vty, "%s", VTY_NEWLINE);
880 switch (ret)
881 {
882 case CMD_ERR_AMBIGUOUS:
883 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
884 vty_prompt (vty);
885 vty_redraw_line (vty);
886 break;
887 case CMD_ERR_NO_MATCH:
888 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
889 vty_prompt (vty);
890 vty_redraw_line (vty);
891 break;
892 case CMD_COMPLETE_FULL_MATCH:
893 vty_prompt (vty);
894 vty_redraw_line (vty);
895 vty_backward_pure_word (vty);
896 vty_insert_word_overwrite (vty, matched[0]);
897 vty_self_insert (vty, ' ');
898 XFREE (MTYPE_TMP, matched[0]);
899 break;
900 case CMD_COMPLETE_MATCH:
901 vty_prompt (vty);
902 vty_redraw_line (vty);
903 vty_backward_pure_word (vty);
904 vty_insert_word_overwrite (vty, matched[0]);
905 XFREE (MTYPE_TMP, matched[0]);
906 vector_only_index_free (matched);
907 return;
908 break;
909 case CMD_COMPLETE_LIST_MATCH:
910 for (i = 0; matched[i] != NULL; i++)
911 {
912 if (i != 0 && ((i % 6) == 0))
913 vty_out (vty, "%s", VTY_NEWLINE);
914 vty_out (vty, "%-10s ", matched[i]);
915 XFREE (MTYPE_TMP, matched[i]);
916 }
917 vty_out (vty, "%s", VTY_NEWLINE);
918
919 vty_prompt (vty);
920 vty_redraw_line (vty);
921 break;
922 case CMD_ERR_NOTHING_TODO:
923 vty_prompt (vty);
924 vty_redraw_line (vty);
925 break;
926 default:
927 break;
928 }
929 if (matched)
930 vector_only_index_free (matched);
931}
932
ajs9fc7ebf2005-02-23 15:12:34 +0000933static void
paul718e3742002-12-13 20:15:29 +0000934vty_describe_fold (struct vty *vty, int cmd_width,
Christian Frankecd40b322013-09-30 12:27:51 +0000935 unsigned int desc_width, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +0000936{
hasso8c328f12004-10-05 21:01:23 +0000937 char *buf;
938 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000939 int pos;
940
Christian Frankecd40b322013-09-30 12:27:51 +0000941 cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
paul718e3742002-12-13 20:15:29 +0000942
943 if (desc_width <= 0)
944 {
Christian Frankecd40b322013-09-30 12:27:51 +0000945 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000946 return;
947 }
948
Christian Frankecd40b322013-09-30 12:27:51 +0000949 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
paul718e3742002-12-13 20:15:29 +0000950
Christian Frankecd40b322013-09-30 12:27:51 +0000951 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
paul718e3742002-12-13 20:15:29 +0000952 {
953 for (pos = desc_width; pos > 0; pos--)
954 if (*(p + pos) == ' ')
955 break;
956
957 if (pos == 0)
958 break;
959
960 strncpy (buf, p, pos);
961 buf[pos] = '\0';
962 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
963
964 cmd = "";
965 }
966
967 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
968
969 XFREE (MTYPE_TMP, buf);
970}
971
972/* Describe matched command function. */
973static void
974vty_describe_command (struct vty *vty)
975{
976 int ret;
977 vector vline;
978 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000979 unsigned int i, width, desc_width;
Christian Frankecd40b322013-09-30 12:27:51 +0000980 struct cmd_token *token, *token_cr = NULL;
paul718e3742002-12-13 20:15:29 +0000981
982 vline = cmd_make_strvec (vty->buf);
983
984 /* In case of '> ?'. */
985 if (vline == NULL)
986 {
987 vline = vector_init (1);
David Lampartera91a3ba2015-03-03 09:06:51 +0100988 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000989 }
990 else
991 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +0100992 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000993
994 describe = cmd_describe_command (vline, vty, &ret);
995
996 vty_out (vty, "%s", VTY_NEWLINE);
997
998 /* Ambiguous error. */
999 switch (ret)
1000 {
1001 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +00001002 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001003 goto out;
paul718e3742002-12-13 20:15:29 +00001004 break;
1005 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +00001006 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001007 goto out;
paul718e3742002-12-13 20:15:29 +00001008 break;
1009 }
1010
1011 /* Get width of command string. */
1012 width = 0;
paul55468c82005-03-14 20:19:01 +00001013 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001014 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001015 {
hasso8c328f12004-10-05 21:01:23 +00001016 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001017
Christian Frankecd40b322013-09-30 12:27:51 +00001018 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001019 continue;
1020
Christian Frankecd40b322013-09-30 12:27:51 +00001021 len = strlen (token->cmd);
1022 if (token->cmd[0] == '.')
paul718e3742002-12-13 20:15:29 +00001023 len--;
1024
1025 if (width < len)
1026 width = len;
1027 }
1028
1029 /* Get width of description string. */
1030 desc_width = vty->width - (width + 6);
1031
1032 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001033 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001034 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001035 {
Christian Frankecd40b322013-09-30 12:27:51 +00001036 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001037 continue;
1038
Christian Frankecd40b322013-09-30 12:27:51 +00001039 if (strcmp (token->cmd, command_cr) == 0)
paul718e3742002-12-13 20:15:29 +00001040 {
Christian Frankecd40b322013-09-30 12:27:51 +00001041 token_cr = token;
paul718e3742002-12-13 20:15:29 +00001042 continue;
1043 }
1044
Christian Frankecd40b322013-09-30 12:27:51 +00001045 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001046 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001047 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001048 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001049 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001050 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001051 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1052 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001053 else
Christian Frankecd40b322013-09-30 12:27:51 +00001054 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001055
1056#if 0
1057 vty_out (vty, " %-*s %s%s", width
1058 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1059 desc->str ? desc->str : "", VTY_NEWLINE);
1060#endif /* 0 */
1061 }
1062
Christian Frankecd40b322013-09-30 12:27:51 +00001063 if ((token = token_cr))
paul718e3742002-12-13 20:15:29 +00001064 {
Christian Frankecd40b322013-09-30 12:27:51 +00001065 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001066 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001067 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001068 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001069 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001070 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001071 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1072 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001073 else
Christian Frankecd40b322013-09-30 12:27:51 +00001074 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001075 }
1076
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001077out:
paul718e3742002-12-13 20:15:29 +00001078 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001079 if (describe)
1080 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001081
1082 vty_prompt (vty);
1083 vty_redraw_line (vty);
1084}
1085
ajs9fc7ebf2005-02-23 15:12:34 +00001086static void
paul718e3742002-12-13 20:15:29 +00001087vty_clear_buf (struct vty *vty)
1088{
1089 memset (vty->buf, 0, vty->max);
1090}
1091
1092/* ^C stop current input and do not add command line to the history. */
1093static void
1094vty_stop_input (struct vty *vty)
1095{
1096 vty->cp = vty->length = 0;
1097 vty_clear_buf (vty);
1098 vty_out (vty, "%s", VTY_NEWLINE);
1099
1100 switch (vty->node)
1101 {
1102 case VIEW_NODE:
1103 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01001104 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00001105 /* Nothing to do. */
1106 break;
1107 case CONFIG_NODE:
1108 case INTERFACE_NODE:
1109 case ZEBRA_NODE:
1110 case RIP_NODE:
1111 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01001112 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00001113 case BGP_NODE:
1114 case RMAP_NODE:
1115 case OSPF_NODE:
1116 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001117 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001118 case KEYCHAIN_NODE:
1119 case KEYCHAIN_KEY_NODE:
1120 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02001121 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00001122 case VTY_NODE:
1123 vty_config_unlock (vty);
1124 vty->node = ENABLE_NODE;
1125 break;
1126 default:
1127 /* Unknown node, we have to ignore it. */
1128 break;
1129 }
1130 vty_prompt (vty);
1131
1132 /* Set history pointer to the latest one. */
1133 vty->hp = vty->hindex;
1134}
1135
1136/* Add current command line to the history buffer. */
1137static void
1138vty_hist_add (struct vty *vty)
1139{
1140 int index;
1141
1142 if (vty->length == 0)
1143 return;
1144
1145 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1146
1147 /* Ignore the same string as previous one. */
1148 if (vty->hist[index])
1149 if (strcmp (vty->buf, vty->hist[index]) == 0)
1150 {
1151 vty->hp = vty->hindex;
1152 return;
1153 }
1154
1155 /* Insert history entry. */
1156 if (vty->hist[vty->hindex])
1157 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1158 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1159
1160 /* History index rotation. */
1161 vty->hindex++;
1162 if (vty->hindex == VTY_MAXHIST)
1163 vty->hindex = 0;
1164
1165 vty->hp = vty->hindex;
1166}
1167
1168/* #define TELNET_OPTION_DEBUG */
1169
1170/* Get telnet window size. */
1171static int
1172vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1173{
1174#ifdef TELNET_OPTION_DEBUG
1175 int i;
1176
1177 for (i = 0; i < nbytes; i++)
1178 {
1179 switch (buf[i])
1180 {
1181 case IAC:
1182 vty_out (vty, "IAC ");
1183 break;
1184 case WILL:
1185 vty_out (vty, "WILL ");
1186 break;
1187 case WONT:
1188 vty_out (vty, "WONT ");
1189 break;
1190 case DO:
1191 vty_out (vty, "DO ");
1192 break;
1193 case DONT:
1194 vty_out (vty, "DONT ");
1195 break;
1196 case SB:
1197 vty_out (vty, "SB ");
1198 break;
1199 case SE:
1200 vty_out (vty, "SE ");
1201 break;
1202 case TELOPT_ECHO:
1203 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1204 break;
1205 case TELOPT_SGA:
1206 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1207 break;
1208 case TELOPT_NAWS:
1209 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1210 break;
1211 default:
1212 vty_out (vty, "%x ", buf[i]);
1213 break;
1214 }
1215 }
1216 vty_out (vty, "%s", VTY_NEWLINE);
1217
1218#endif /* TELNET_OPTION_DEBUG */
1219
1220 switch (buf[0])
1221 {
1222 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001223 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001224 vty->iac_sb_in_progress = 1;
1225 return 0;
1226 break;
1227 case SE:
1228 {
paul718e3742002-12-13 20:15:29 +00001229 if (!vty->iac_sb_in_progress)
1230 return 0;
1231
ajs9fc7ebf2005-02-23 15:12:34 +00001232 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001233 {
1234 vty->iac_sb_in_progress = 0;
1235 return 0;
1236 }
ajs9fc7ebf2005-02-23 15:12:34 +00001237 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001238 {
1239 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001240 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1241 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1242 "should send %d characters, but we received %lu",
1243 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1244 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1245 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1246 "too small to handle the telnet NAWS option",
1247 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1248 else
1249 {
1250 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1251 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1252#ifdef TELNET_OPTION_DEBUG
1253 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1254 "width %d, height %d%s",
1255 vty->width, vty->height, VTY_NEWLINE);
1256#endif
1257 }
paul718e3742002-12-13 20:15:29 +00001258 break;
1259 }
1260 vty->iac_sb_in_progress = 0;
1261 return 0;
1262 break;
1263 }
1264 default:
1265 break;
1266 }
1267 return 1;
1268}
1269
1270/* Execute current command line. */
1271static int
1272vty_execute (struct vty *vty)
1273{
1274 int ret;
1275
1276 ret = CMD_SUCCESS;
1277
1278 switch (vty->node)
1279 {
1280 case AUTH_NODE:
1281 case AUTH_ENABLE_NODE:
1282 vty_auth (vty, vty->buf);
1283 break;
1284 default:
1285 ret = vty_command (vty, vty->buf);
1286 if (vty->type == VTY_TERM)
1287 vty_hist_add (vty);
1288 break;
1289 }
1290
1291 /* Clear command line buffer. */
1292 vty->cp = vty->length = 0;
1293 vty_clear_buf (vty);
1294
ajs5a646652004-11-05 01:25:55 +00001295 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001296 vty_prompt (vty);
1297
1298 return ret;
1299}
1300
1301#define CONTROL(X) ((X) - '@')
1302#define VTY_NORMAL 0
1303#define VTY_PRE_ESCAPE 1
1304#define VTY_ESCAPE 2
1305
1306/* Escape character command map. */
1307static void
1308vty_escape_map (unsigned char c, struct vty *vty)
1309{
1310 switch (c)
1311 {
1312 case ('A'):
1313 vty_previous_line (vty);
1314 break;
1315 case ('B'):
1316 vty_next_line (vty);
1317 break;
1318 case ('C'):
1319 vty_forward_char (vty);
1320 break;
1321 case ('D'):
1322 vty_backward_char (vty);
1323 break;
1324 default:
1325 break;
1326 }
1327
1328 /* Go back to normal mode. */
1329 vty->escape = VTY_NORMAL;
1330}
1331
1332/* Quit print out to the buffer. */
1333static void
1334vty_buffer_reset (struct vty *vty)
1335{
1336 buffer_reset (vty->obuf);
1337 vty_prompt (vty);
1338 vty_redraw_line (vty);
1339}
1340
1341/* Read data via vty socket. */
1342static int
1343vty_read (struct thread *thread)
1344{
1345 int i;
paul718e3742002-12-13 20:15:29 +00001346 int nbytes;
1347 unsigned char buf[VTY_READ_BUFSIZ];
1348
1349 int vty_sock = THREAD_FD (thread);
1350 struct vty *vty = THREAD_ARG (thread);
1351 vty->t_read = NULL;
1352
1353 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001354 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1355 {
1356 if (nbytes < 0)
1357 {
1358 if (ERRNO_IO_RETRY(errno))
1359 {
1360 vty_event (VTY_READ, vty_sock, vty);
1361 return 0;
1362 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001363 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001364 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1365 __func__, vty->fd, safe_strerror(errno));
1366 }
1367 buffer_reset(vty->obuf);
1368 vty->status = VTY_CLOSE;
1369 }
paul718e3742002-12-13 20:15:29 +00001370
1371 for (i = 0; i < nbytes; i++)
1372 {
1373 if (buf[i] == IAC)
1374 {
1375 if (!vty->iac)
1376 {
1377 vty->iac = 1;
1378 continue;
1379 }
1380 else
1381 {
1382 vty->iac = 0;
1383 }
1384 }
1385
1386 if (vty->iac_sb_in_progress && !vty->iac)
1387 {
ajs9fc7ebf2005-02-23 15:12:34 +00001388 if (vty->sb_len < sizeof(vty->sb_buf))
1389 vty->sb_buf[vty->sb_len] = buf[i];
1390 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001391 continue;
1392 }
1393
1394 if (vty->iac)
1395 {
1396 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001397 int ret = 0;
paule9372532003-10-26 21:36:07 +00001398 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001399 vty->iac = 0;
1400 i += ret;
1401 continue;
1402 }
paul5b8c1b02003-10-15 23:08:55 +00001403
paul718e3742002-12-13 20:15:29 +00001404
1405 if (vty->status == VTY_MORE)
1406 {
1407 switch (buf[i])
1408 {
1409 case CONTROL('C'):
1410 case 'q':
1411 case 'Q':
paul718e3742002-12-13 20:15:29 +00001412 vty_buffer_reset (vty);
1413 break;
1414#if 0 /* More line does not work for "show ip bgp". */
1415 case '\n':
1416 case '\r':
1417 vty->status = VTY_MORELINE;
1418 break;
1419#endif
1420 default:
paul718e3742002-12-13 20:15:29 +00001421 break;
1422 }
1423 continue;
1424 }
1425
1426 /* Escape character. */
1427 if (vty->escape == VTY_ESCAPE)
1428 {
1429 vty_escape_map (buf[i], vty);
1430 continue;
1431 }
1432
1433 /* Pre-escape status. */
1434 if (vty->escape == VTY_PRE_ESCAPE)
1435 {
1436 switch (buf[i])
1437 {
1438 case '[':
1439 vty->escape = VTY_ESCAPE;
1440 break;
1441 case 'b':
1442 vty_backward_word (vty);
1443 vty->escape = VTY_NORMAL;
1444 break;
1445 case 'f':
1446 vty_forward_word (vty);
1447 vty->escape = VTY_NORMAL;
1448 break;
1449 case 'd':
1450 vty_forward_kill_word (vty);
1451 vty->escape = VTY_NORMAL;
1452 break;
1453 case CONTROL('H'):
1454 case 0x7f:
1455 vty_backward_kill_word (vty);
1456 vty->escape = VTY_NORMAL;
1457 break;
1458 default:
1459 vty->escape = VTY_NORMAL;
1460 break;
1461 }
1462 continue;
1463 }
1464
1465 switch (buf[i])
1466 {
1467 case CONTROL('A'):
1468 vty_beginning_of_line (vty);
1469 break;
1470 case CONTROL('B'):
1471 vty_backward_char (vty);
1472 break;
1473 case CONTROL('C'):
1474 vty_stop_input (vty);
1475 break;
1476 case CONTROL('D'):
1477 vty_delete_char (vty);
1478 break;
1479 case CONTROL('E'):
1480 vty_end_of_line (vty);
1481 break;
1482 case CONTROL('F'):
1483 vty_forward_char (vty);
1484 break;
1485 case CONTROL('H'):
1486 case 0x7f:
1487 vty_delete_backward_char (vty);
1488 break;
1489 case CONTROL('K'):
1490 vty_kill_line (vty);
1491 break;
1492 case CONTROL('N'):
1493 vty_next_line (vty);
1494 break;
1495 case CONTROL('P'):
1496 vty_previous_line (vty);
1497 break;
1498 case CONTROL('T'):
1499 vty_transpose_chars (vty);
1500 break;
1501 case CONTROL('U'):
1502 vty_kill_line_from_beginning (vty);
1503 break;
1504 case CONTROL('W'):
1505 vty_backward_kill_word (vty);
1506 break;
1507 case CONTROL('Z'):
1508 vty_end_config (vty);
1509 break;
1510 case '\n':
1511 case '\r':
1512 vty_out (vty, "%s", VTY_NEWLINE);
1513 vty_execute (vty);
1514 break;
1515 case '\t':
1516 vty_complete_command (vty);
1517 break;
1518 case '?':
1519 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1520 vty_self_insert (vty, buf[i]);
1521 else
1522 vty_describe_command (vty);
1523 break;
1524 case '\033':
1525 if (i + 1 < nbytes && buf[i + 1] == '[')
1526 {
1527 vty->escape = VTY_ESCAPE;
1528 i++;
1529 }
1530 else
1531 vty->escape = VTY_PRE_ESCAPE;
1532 break;
1533 default:
1534 if (buf[i] > 31 && buf[i] < 127)
1535 vty_self_insert (vty, buf[i]);
1536 break;
1537 }
1538 }
1539
1540 /* Check status. */
1541 if (vty->status == VTY_CLOSE)
1542 vty_close (vty);
1543 else
1544 {
David Lamparter4715a532013-05-30 16:31:49 +02001545 vty_event (VTY_WRITE, vty->wfd, vty);
paul718e3742002-12-13 20:15:29 +00001546 vty_event (VTY_READ, vty_sock, vty);
1547 }
1548 return 0;
1549}
1550
1551/* Flush buffer to the vty. */
1552static int
1553vty_flush (struct thread *thread)
1554{
1555 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001556 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001557 int vty_sock = THREAD_FD (thread);
1558 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001559
paul718e3742002-12-13 20:15:29 +00001560 vty->t_write = NULL;
1561
1562 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001563 if ((vty->lines == 0) && vty->t_read)
1564 {
1565 thread_cancel (vty->t_read);
1566 vty->t_read = NULL;
1567 }
paul718e3742002-12-13 20:15:29 +00001568
1569 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001570 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001571
ajs9fc7ebf2005-02-23 15:12:34 +00001572 /* N.B. if width is 0, that means we don't know the window size. */
1573 if ((vty->lines == 0) || (vty->width == 0))
David Lamparter4715a532013-05-30 16:31:49 +02001574 flushrc = buffer_flush_available(vty->obuf, vty_sock);
ajs9fc7ebf2005-02-23 15:12:34 +00001575 else if (vty->status == VTY_MORELINE)
David Lamparter4715a532013-05-30 16:31:49 +02001576 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001577 1, erase, 0);
1578 else
David Lamparter4715a532013-05-30 16:31:49 +02001579 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001580 vty->lines >= 0 ? vty->lines :
1581 vty->height,
1582 erase, 0);
1583 switch (flushrc)
1584 {
1585 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001586 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001587 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1588 vty->fd);
1589 buffer_reset(vty->obuf);
1590 vty_close(vty);
1591 return 0;
1592 case BUFFER_EMPTY:
1593 if (vty->status == VTY_CLOSE)
1594 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001595 else
1596 {
ajs9fc7ebf2005-02-23 15:12:34 +00001597 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001598 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001599 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001600 }
ajs9fc7ebf2005-02-23 15:12:34 +00001601 break;
1602 case BUFFER_PENDING:
1603 /* There is more data waiting to be written. */
1604 vty->status = VTY_MORE;
1605 if (vty->lines == 0)
1606 vty_event (VTY_WRITE, vty_sock, vty);
1607 break;
1608 }
paul718e3742002-12-13 20:15:29 +00001609
1610 return 0;
1611}
1612
David Lamparterba5dc5e2013-05-30 16:33:45 +02001613/* allocate and initialise vty */
1614static struct vty *
1615vty_new_init (int vty_sock)
1616{
1617 struct vty *vty;
1618
1619 vty = vty_new ();
1620 vty->fd = vty_sock;
1621 vty->wfd = vty_sock;
1622 vty->type = VTY_TERM;
1623 vty->node = AUTH_NODE;
1624 vty->fail = 0;
1625 vty->cp = 0;
1626 vty_clear_buf (vty);
1627 vty->length = 0;
1628 memset (vty->hist, 0, sizeof (vty->hist));
1629 vty->hp = 0;
1630 vty->hindex = 0;
1631 vector_set_index (vtyvec, vty_sock, vty);
1632 vty->status = VTY_NORMAL;
1633 vty->lines = -1;
1634 vty->iac = 0;
1635 vty->iac_sb_in_progress = 0;
1636 vty->sb_len = 0;
1637
1638 return vty;
1639}
1640
paul718e3742002-12-13 20:15:29 +00001641/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001642static struct vty *
paul718e3742002-12-13 20:15:29 +00001643vty_create (int vty_sock, union sockunion *su)
1644{
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001645 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001646 struct vty *vty;
1647
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001648 sockunion2str(su, buf, SU_ADDRSTRLEN);
1649
paul718e3742002-12-13 20:15:29 +00001650 /* Allocate new vty structure and set up default values. */
David Lamparterba5dc5e2013-05-30 16:33:45 +02001651 vty = vty_new_init (vty_sock);
1652
1653 /* configurable parameters not part of basic init */
1654 vty->v_timeout = vty_timeout_val;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001655 strcpy (vty->address, buf);
paul718e3742002-12-13 20:15:29 +00001656 if (no_password_check)
1657 {
Paul Jakma62687ff2008-08-23 14:27:06 +01001658 if (restricted_mode)
1659 vty->node = RESTRICTED_NODE;
1660 else if (host.advanced)
paul718e3742002-12-13 20:15:29 +00001661 vty->node = ENABLE_NODE;
1662 else
1663 vty->node = VIEW_NODE;
1664 }
paul718e3742002-12-13 20:15:29 +00001665 if (host.lines >= 0)
1666 vty->lines = host.lines;
paul718e3742002-12-13 20:15:29 +00001667
1668 if (! no_password_check)
1669 {
1670 /* Vty is not available if password isn't set. */
1671 if (host.password == NULL && host.password_encrypt == NULL)
1672 {
1673 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1674 vty->status = VTY_CLOSE;
1675 vty_close (vty);
1676 return NULL;
1677 }
1678 }
1679
1680 /* Say hello to the world. */
1681 vty_hello (vty);
1682 if (! no_password_check)
1683 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1684
1685 /* Setting up terminal. */
1686 vty_will_echo (vty);
1687 vty_will_suppress_go_ahead (vty);
1688
1689 vty_dont_linemode (vty);
1690 vty_do_window_size (vty);
1691 /* vty_dont_lflow_ahead (vty); */
1692
1693 vty_prompt (vty);
1694
1695 /* Add read/write thread. */
1696 vty_event (VTY_WRITE, vty_sock, vty);
1697 vty_event (VTY_READ, vty_sock, vty);
1698
1699 return vty;
1700}
1701
David Lamparterba5dc5e2013-05-30 16:33:45 +02001702/* create vty for stdio */
1703struct vty *
1704vty_stdio (void)
1705{
1706 struct vty *vty;
1707
1708 vty = vty_new_init (0);
1709 vty->wfd = 1;
1710
1711 /* always have stdio vty in a known _unchangeable_ state, don't want config
1712 * to have any effect here to make sure scripting this works as intended */
1713 vty->node = ENABLE_NODE;
1714 vty->v_timeout = 0;
1715 strcpy (vty->address, "console");
1716
1717 vty_prompt (vty);
1718
1719 /* Add read/write thread. */
1720 vty_event (VTY_WRITE, 1, vty);
1721 vty_event (VTY_READ, 0, vty);
1722
1723 return vty;
1724}
1725
paul718e3742002-12-13 20:15:29 +00001726/* Accept connection from the network. */
1727static int
1728vty_accept (struct thread *thread)
1729{
1730 int vty_sock;
paul718e3742002-12-13 20:15:29 +00001731 union sockunion su;
1732 int ret;
1733 unsigned int on;
1734 int accept_sock;
1735 struct prefix *p = NULL;
1736 struct access_list *acl = NULL;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001737 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001738
1739 accept_sock = THREAD_FD (thread);
1740
1741 /* We continue hearing vty socket. */
1742 vty_event (VTY_SERV, accept_sock, NULL);
1743
1744 memset (&su, 0, sizeof (union sockunion));
1745
1746 /* We can handle IPv4 or IPv6 socket. */
1747 vty_sock = sockunion_accept (accept_sock, &su);
1748 if (vty_sock < 0)
1749 {
ajs6099b3b2004-11-20 02:06:59 +00001750 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001751 return -1;
1752 }
ajs9fc7ebf2005-02-23 15:12:34 +00001753 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001754
1755 p = sockunion2hostprefix (&su);
1756
1757 /* VTY's accesslist apply. */
1758 if (p->family == AF_INET && vty_accesslist_name)
1759 {
1760 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1761 (access_list_apply (acl, p) == FILTER_DENY))
1762 {
paul718e3742002-12-13 20:15:29 +00001763 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001764 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001765 close (vty_sock);
1766
1767 /* continue accepting connections */
1768 vty_event (VTY_SERV, accept_sock, NULL);
1769
1770 prefix_free (p);
1771
1772 return 0;
1773 }
1774 }
1775
1776#ifdef HAVE_IPV6
1777 /* VTY's ipv6 accesslist apply. */
1778 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1779 {
1780 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1781 (access_list_apply (acl, p) == FILTER_DENY))
1782 {
paul718e3742002-12-13 20:15:29 +00001783 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001784 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001785 close (vty_sock);
1786
1787 /* continue accepting connections */
1788 vty_event (VTY_SERV, accept_sock, NULL);
1789
1790 prefix_free (p);
1791
1792 return 0;
1793 }
1794 }
1795#endif /* HAVE_IPV6 */
1796
1797 prefix_free (p);
1798
1799 on = 1;
1800 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1801 (char *) &on, sizeof (on));
1802 if (ret < 0)
1803 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001804 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001805
heasley78e6cd92009-12-07 16:41:14 +03001806 zlog (NULL, LOG_INFO, "Vty connection from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001807 sockunion2str (&su, buf, SU_ADDRSTRLEN));
heasley78e6cd92009-12-07 16:41:14 +03001808
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001809 vty_create (vty_sock, &su);
paul718e3742002-12-13 20:15:29 +00001810
1811 return 0;
1812}
1813
David Lamparter6d6df302014-06-28 21:12:37 +02001814#ifdef HAVE_IPV6
ajs9fc7ebf2005-02-23 15:12:34 +00001815static void
paul718e3742002-12-13 20:15:29 +00001816vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1817{
1818 int ret;
1819 struct addrinfo req;
1820 struct addrinfo *ainfo;
1821 struct addrinfo *ainfo_save;
1822 int sock;
1823 char port_str[BUFSIZ];
1824
1825 memset (&req, 0, sizeof (struct addrinfo));
1826 req.ai_flags = AI_PASSIVE;
1827 req.ai_family = AF_UNSPEC;
1828 req.ai_socktype = SOCK_STREAM;
1829 sprintf (port_str, "%d", port);
1830 port_str[sizeof (port_str) - 1] = '\0';
1831
1832 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1833
1834 if (ret != 0)
1835 {
1836 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1837 exit (1);
1838 }
1839
1840 ainfo_save = ainfo;
1841
1842 do
1843 {
1844 if (ainfo->ai_family != AF_INET
1845#ifdef HAVE_IPV6
1846 && ainfo->ai_family != AF_INET6
1847#endif /* HAVE_IPV6 */
1848 )
1849 continue;
1850
1851 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1852 if (sock < 0)
1853 continue;
1854
David Lamparterca051262009-10-04 16:21:49 +02001855 sockopt_v6only (ainfo->ai_family, sock);
paul718e3742002-12-13 20:15:29 +00001856 sockopt_reuseaddr (sock);
1857 sockopt_reuseport (sock);
1858
1859 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1860 if (ret < 0)
1861 {
1862 close (sock); /* Avoid sd leak. */
1863 continue;
1864 }
1865
1866 ret = listen (sock, 3);
1867 if (ret < 0)
1868 {
1869 close (sock); /* Avoid sd leak. */
1870 continue;
1871 }
1872
1873 vty_event (VTY_SERV, sock, NULL);
1874 }
1875 while ((ainfo = ainfo->ai_next) != NULL);
1876
1877 freeaddrinfo (ainfo_save);
1878}
David Lamparter6d6df302014-06-28 21:12:37 +02001879#else /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001880
1881/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001882static void
paul29db05b2003-05-08 20:10:22 +00001883vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001884{
1885 int ret;
1886 union sockunion su;
1887 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001888 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001889
1890 memset (&su, 0, sizeof (union sockunion));
1891 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001892 if(addr)
1893 switch(family)
1894 {
1895 case AF_INET:
1896 naddr=&su.sin.sin_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001897 break;
paul29db05b2003-05-08 20:10:22 +00001898#ifdef HAVE_IPV6
1899 case AF_INET6:
1900 naddr=&su.sin6.sin6_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001901 break;
paul29db05b2003-05-08 20:10:22 +00001902#endif
1903 }
1904
1905 if(naddr)
1906 switch(inet_pton(family,addr,naddr))
1907 {
1908 case -1:
1909 zlog_err("bad address %s",addr);
1910 naddr=NULL;
1911 break;
1912 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001913 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001914 naddr=NULL;
1915 }
paul718e3742002-12-13 20:15:29 +00001916
1917 /* Make new socket. */
1918 accept_sock = sockunion_stream_socket (&su);
1919 if (accept_sock < 0)
1920 return;
1921
1922 /* This is server, so reuse address. */
1923 sockopt_reuseaddr (accept_sock);
1924 sockopt_reuseport (accept_sock);
1925
1926 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001927 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001928 if (ret < 0)
1929 {
paul29db05b2003-05-08 20:10:22 +00001930 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001931 close (accept_sock); /* Avoid sd leak. */
1932 return;
1933 }
1934
1935 /* Listen socket under queue 3. */
1936 ret = listen (accept_sock, 3);
1937 if (ret < 0)
1938 {
1939 zlog (NULL, LOG_WARNING, "can't listen socket");
1940 close (accept_sock); /* Avoid sd leak. */
1941 return;
1942 }
1943
1944 /* Add vty server event. */
1945 vty_event (VTY_SERV, accept_sock, NULL);
1946}
David Lamparter6d6df302014-06-28 21:12:37 +02001947#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001948
1949#ifdef VTYSH
1950/* For sockaddr_un. */
1951#include <sys/un.h>
1952
1953/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001954static void
hasso6ad96ea2004-10-07 19:33:46 +00001955vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001956{
1957 int ret;
paul75e15fe2004-10-31 02:13:09 +00001958 int sock, len;
paul718e3742002-12-13 20:15:29 +00001959 struct sockaddr_un serv;
1960 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001961 struct zprivs_ids_t ids;
1962
paul718e3742002-12-13 20:15:29 +00001963 /* First of all, unlink existing socket */
1964 unlink (path);
1965
1966 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001967 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001968
1969 /* Make UNIX domain socket. */
1970 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1971 if (sock < 0)
1972 {
ajs6a52d0d2005-01-30 18:49:28 +00001973 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001974 return;
1975 }
1976
1977 /* Make server socket. */
1978 memset (&serv, 0, sizeof (struct sockaddr_un));
1979 serv.sun_family = AF_UNIX;
1980 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001981#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001982 len = serv.sun_len = SUN_LEN(&serv);
1983#else
1984 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001985#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001986
1987 ret = bind (sock, (struct sockaddr *) &serv, len);
1988 if (ret < 0)
1989 {
ajs6a52d0d2005-01-30 18:49:28 +00001990 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001991 close (sock); /* Avoid sd leak. */
1992 return;
1993 }
1994
1995 ret = listen (sock, 5);
1996 if (ret < 0)
1997 {
ajs6a52d0d2005-01-30 18:49:28 +00001998 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001999 close (sock); /* Avoid sd leak. */
2000 return;
2001 }
2002
2003 umask (old_mask);
2004
pauledd7c242003-06-04 13:59:38 +00002005 zprivs_get_ids(&ids);
2006
2007 if (ids.gid_vty > 0)
2008 {
2009 /* set group of socket */
2010 if ( chown (path, -1, ids.gid_vty) )
2011 {
2012 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00002013 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00002014 }
2015 }
2016
paul718e3742002-12-13 20:15:29 +00002017 vty_event (VTYSH_SERV, sock, NULL);
2018}
2019
2020/* #define VTYSH_DEBUG 1 */
2021
2022static int
2023vtysh_accept (struct thread *thread)
2024{
2025 int accept_sock;
2026 int sock;
2027 int client_len;
2028 struct sockaddr_un client;
2029 struct vty *vty;
2030
2031 accept_sock = THREAD_FD (thread);
2032
2033 vty_event (VTYSH_SERV, accept_sock, NULL);
2034
2035 memset (&client, 0, sizeof (struct sockaddr_un));
2036 client_len = sizeof (struct sockaddr_un);
2037
hassoe473b032004-09-26 16:08:11 +00002038 sock = accept (accept_sock, (struct sockaddr *) &client,
2039 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00002040
2041 if (sock < 0)
2042 {
ajs6099b3b2004-11-20 02:06:59 +00002043 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002044 return -1;
2045 }
2046
ajs9fc7ebf2005-02-23 15:12:34 +00002047 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00002048 {
ajs9fc7ebf2005-02-23 15:12:34 +00002049 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2050 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002051 close (sock);
2052 return -1;
2053 }
pauldccfb192004-10-29 08:29:36 +00002054
paul718e3742002-12-13 20:15:29 +00002055#ifdef VTYSH_DEBUG
2056 printf ("VTY shell accept\n");
2057#endif /* VTYSH_DEBUG */
2058
2059 vty = vty_new ();
2060 vty->fd = sock;
David Lamparter4715a532013-05-30 16:31:49 +02002061 vty->wfd = sock;
paul718e3742002-12-13 20:15:29 +00002062 vty->type = VTY_SHELL_SERV;
2063 vty->node = VIEW_NODE;
2064
2065 vty_event (VTYSH_READ, sock, vty);
2066
2067 return 0;
2068}
2069
2070static int
ajs9fc7ebf2005-02-23 15:12:34 +00002071vtysh_flush(struct vty *vty)
2072{
David Lamparter4715a532013-05-30 16:31:49 +02002073 switch (buffer_flush_available(vty->obuf, vty->wfd))
ajs9fc7ebf2005-02-23 15:12:34 +00002074 {
2075 case BUFFER_PENDING:
David Lamparter4715a532013-05-30 16:31:49 +02002076 vty_event(VTYSH_WRITE, vty->wfd, vty);
ajs9fc7ebf2005-02-23 15:12:34 +00002077 break;
2078 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002079 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002080 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2081 buffer_reset(vty->obuf);
2082 vty_close(vty);
2083 return -1;
2084 break;
2085 case BUFFER_EMPTY:
2086 break;
2087 }
2088 return 0;
2089}
2090
2091static int
paul718e3742002-12-13 20:15:29 +00002092vtysh_read (struct thread *thread)
2093{
2094 int ret;
2095 int sock;
2096 int nbytes;
2097 struct vty *vty;
2098 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002099 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002100 u_char header[4] = {0, 0, 0, 0};
2101
2102 sock = THREAD_FD (thread);
2103 vty = THREAD_ARG (thread);
2104 vty->t_read = NULL;
2105
ajs9fc7ebf2005-02-23 15:12:34 +00002106 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002107 {
ajs9fc7ebf2005-02-23 15:12:34 +00002108 if (nbytes < 0)
2109 {
2110 if (ERRNO_IO_RETRY(errno))
2111 {
2112 vty_event (VTYSH_READ, sock, vty);
2113 return 0;
2114 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002115 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002116 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2117 __func__, sock, safe_strerror(errno));
2118 }
2119 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002120 vty_close (vty);
2121#ifdef VTYSH_DEBUG
2122 printf ("close vtysh\n");
2123#endif /* VTYSH_DEBUG */
2124 return 0;
2125 }
2126
2127#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002128 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002129#endif /* VTYSH_DEBUG */
2130
ajs9fc7ebf2005-02-23 15:12:34 +00002131 for (p = buf; p < buf+nbytes; p++)
2132 {
2133 vty_ensure(vty, vty->length+1);
2134 vty->buf[vty->length++] = *p;
2135 if (*p == '\0')
2136 {
2137 /* Pass this line to parser. */
2138 ret = vty_execute (vty);
2139 /* Note that vty_execute clears the command buffer and resets
2140 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002141
ajs9fc7ebf2005-02-23 15:12:34 +00002142 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002143#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002144 printf ("result: %d\n", ret);
2145 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002146#endif /* VTYSH_DEBUG */
2147
ajs9fc7ebf2005-02-23 15:12:34 +00002148 header[3] = ret;
2149 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002150
ajs9fc7ebf2005-02-23 15:12:34 +00002151 if (!vty->t_write && (vtysh_flush(vty) < 0))
2152 /* Try to flush results; exit if a write error occurs. */
2153 return 0;
2154 }
2155 }
2156
paul718e3742002-12-13 20:15:29 +00002157 vty_event (VTYSH_READ, sock, vty);
2158
2159 return 0;
2160}
ajs49ff6d92004-11-04 19:26:16 +00002161
2162static int
2163vtysh_write (struct thread *thread)
2164{
2165 struct vty *vty = THREAD_ARG (thread);
2166
2167 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002168 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002169 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002170}
2171
paul718e3742002-12-13 20:15:29 +00002172#endif /* VTYSH */
2173
2174/* Determine address family to bind. */
2175void
hasso6ad96ea2004-10-07 19:33:46 +00002176vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002177{
2178 /* If port is set to 0, do not listen on TCP/IP at all! */
2179 if (port)
2180 {
2181
2182#ifdef HAVE_IPV6
paul29db05b2003-05-08 20:10:22 +00002183 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002184#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002185 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002186#endif /* HAVE_IPV6 */
2187 }
2188
2189#ifdef VTYSH
2190 vty_serv_un (path);
2191#endif /* VTYSH */
2192}
2193
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002194/* Close vty interface. Warning: call this only from functions that
2195 will be careful not to access the vty afterwards (since it has
2196 now been freed). This is safest from top-level functions (called
2197 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002198void
2199vty_close (struct vty *vty)
2200{
2201 int i;
2202
2203 /* Cancel threads.*/
2204 if (vty->t_read)
2205 thread_cancel (vty->t_read);
2206 if (vty->t_write)
2207 thread_cancel (vty->t_write);
2208 if (vty->t_timeout)
2209 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002210
2211 /* Flush buffer. */
David Lamparter4715a532013-05-30 16:31:49 +02002212 buffer_flush_all (vty->obuf, vty->wfd);
paul718e3742002-12-13 20:15:29 +00002213
2214 /* Free input buffer. */
2215 buffer_free (vty->obuf);
2216
paul718e3742002-12-13 20:15:29 +00002217 /* Free command history. */
2218 for (i = 0; i < VTY_MAXHIST; i++)
2219 if (vty->hist[i])
2220 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2221
2222 /* Unset vector. */
2223 vector_unset (vtyvec, vty->fd);
2224
2225 /* Close socket. */
2226 if (vty->fd > 0)
2227 close (vty->fd);
2228
paul718e3742002-12-13 20:15:29 +00002229 if (vty->buf)
2230 XFREE (MTYPE_VTY, vty->buf);
2231
2232 /* Check configure. */
2233 vty_config_unlock (vty);
2234
2235 /* OK free vty. */
2236 XFREE (MTYPE_VTY, vty);
2237}
2238
2239/* When time out occur output message then close connection. */
2240static int
2241vty_timeout (struct thread *thread)
2242{
2243 struct vty *vty;
2244
2245 vty = THREAD_ARG (thread);
2246 vty->t_timeout = NULL;
2247 vty->v_timeout = 0;
2248
2249 /* Clear buffer*/
2250 buffer_reset (vty->obuf);
2251 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2252
2253 /* Close connection. */
2254 vty->status = VTY_CLOSE;
2255 vty_close (vty);
2256
2257 return 0;
2258}
2259
2260/* Read up configuration file from file_name. */
2261static void
2262vty_read_file (FILE *confp)
2263{
2264 int ret;
2265 struct vty *vty;
Steve Hillea555002009-07-28 16:36:14 -04002266 unsigned int line_num = 0;
paul718e3742002-12-13 20:15:29 +00002267
2268 vty = vty_new ();
David Lamparter4715a532013-05-30 16:31:49 +02002269 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2270 if (vty->wfd < 0)
Steve Hillea555002009-07-28 16:36:14 -04002271 {
2272 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
David Lamparter4715a532013-05-30 16:31:49 +02002273 vty->wfd = STDOUT_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002274 }
David Lamparter4715a532013-05-30 16:31:49 +02002275 vty->fd = STDIN_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002276 vty->type = VTY_FILE;
paul718e3742002-12-13 20:15:29 +00002277 vty->node = CONFIG_NODE;
2278
2279 /* Execute configuration file */
Steve Hillea555002009-07-28 16:36:14 -04002280 ret = config_from_file (vty, confp, &line_num);
2281
2282 /* Flush any previous errors before printing messages below */
2283 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002284
paul7021c422003-07-15 12:52:22 +00002285 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002286 {
2287 switch (ret)
paul7021c422003-07-15 12:52:22 +00002288 {
2289 case CMD_ERR_AMBIGUOUS:
Steve Hillea555002009-07-28 16:36:14 -04002290 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
paul7021c422003-07-15 12:52:22 +00002291 break;
2292 case CMD_ERR_NO_MATCH:
Steve Hillea555002009-07-28 16:36:14 -04002293 fprintf (stderr, "*** Error reading config: There is no such command.\n");
paul7021c422003-07-15 12:52:22 +00002294 break;
2295 }
Steve Hillea555002009-07-28 16:36:14 -04002296 fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
2297 line_num, vty->buf);
paul718e3742002-12-13 20:15:29 +00002298 vty_close (vty);
2299 exit (1);
2300 }
2301
2302 vty_close (vty);
2303}
2304
ajs9fc7ebf2005-02-23 15:12:34 +00002305static FILE *
paul718e3742002-12-13 20:15:29 +00002306vty_use_backup_config (char *fullpath)
2307{
2308 char *fullpath_sav, *fullpath_tmp;
2309 FILE *ret = NULL;
2310 struct stat buf;
2311 int tmp, sav;
2312 int c;
2313 char buffer[512];
2314
2315 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2316 strcpy (fullpath_sav, fullpath);
2317 strcat (fullpath_sav, CONF_BACKUP_EXT);
2318 if (stat (fullpath_sav, &buf) == -1)
2319 {
2320 free (fullpath_sav);
2321 return NULL;
2322 }
2323
2324 fullpath_tmp = malloc (strlen (fullpath) + 8);
2325 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2326
2327 /* Open file to configuration write. */
2328 tmp = mkstemp (fullpath_tmp);
2329 if (tmp < 0)
2330 {
2331 free (fullpath_sav);
2332 free (fullpath_tmp);
2333 return NULL;
2334 }
2335
2336 sav = open (fullpath_sav, O_RDONLY);
2337 if (sav < 0)
2338 {
gdt3dbf9962003-12-22 20:18:18 +00002339 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002340 free (fullpath_sav);
2341 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002342 return NULL;
2343 }
2344
2345 while((c = read (sav, buffer, 512)) > 0)
2346 write (tmp, buffer, c);
2347
2348 close (sav);
2349 close (tmp);
2350
gdtaa593d52003-12-22 20:15:53 +00002351 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2352 {
gdt3dbf9962003-12-22 20:18:18 +00002353 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002354 free (fullpath_sav);
2355 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002356 return NULL;
2357 }
2358
paul718e3742002-12-13 20:15:29 +00002359 if (link (fullpath_tmp, fullpath) == 0)
2360 ret = fopen (fullpath, "r");
2361
2362 unlink (fullpath_tmp);
2363
2364 free (fullpath_sav);
2365 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002366 return ret;
paul718e3742002-12-13 20:15:29 +00002367}
2368
2369/* Read up configuration file from file_name. */
2370void
2371vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002372 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002373{
paulccc92352003-10-22 02:49:38 +00002374 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002375 FILE *confp = NULL;
2376 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002377 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002378
2379 /* If -f flag specified. */
2380 if (config_file != NULL)
2381 {
2382 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002383 {
2384 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002385 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002386 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002387 sprintf (tmp, "%s/%s", cwd, config_file);
2388 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002389 }
paul718e3742002-12-13 20:15:29 +00002390 else
hasso320ec102004-06-20 19:54:37 +00002391 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002392
2393 confp = fopen (fullpath, "r");
2394
2395 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002396 {
paul3d1dc852005-04-05 00:45:23 +00002397 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2398 __func__, fullpath, safe_strerror (errno));
2399
hasso320ec102004-06-20 19:54:37 +00002400 confp = vty_use_backup_config (fullpath);
2401 if (confp)
2402 fprintf (stderr, "WARNING: using backup configuration file!\n");
2403 else
2404 {
2405 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002406 config_file);
hasso320ec102004-06-20 19:54:37 +00002407 exit(1);
2408 }
2409 }
paul718e3742002-12-13 20:15:29 +00002410 }
2411 else
2412 {
paul718e3742002-12-13 20:15:29 +00002413#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002414 int ret;
2415 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002416
hasso320ec102004-06-20 19:54:37 +00002417 /* !!!!PLEASE LEAVE!!!!
2418 * This is NEEDED for use with vtysh -b, or else you can get
2419 * a real configuration food fight with a lot garbage in the
2420 * merged configuration file it creates coming from the per
2421 * daemon configuration files. This also allows the daemons
2422 * to start if there default configuration file is not
2423 * present or ignore them, as needed when using vtysh -b to
2424 * configure the daemons at boot - MAG
2425 */
paul718e3742002-12-13 20:15:29 +00002426
hasso320ec102004-06-20 19:54:37 +00002427 /* Stat for vtysh Zebra.conf, if found startup and wait for
2428 * boot configuration
2429 */
paul718e3742002-12-13 20:15:29 +00002430
hasso320ec102004-06-20 19:54:37 +00002431 if ( strstr(config_default_dir, "vtysh") == NULL)
2432 {
2433 ret = stat (integrate_default, &conf_stat);
2434 if (ret >= 0)
2435 return;
2436 }
paul718e3742002-12-13 20:15:29 +00002437#endif /* VTYSH */
2438
hasso320ec102004-06-20 19:54:37 +00002439 confp = fopen (config_default_dir, "r");
2440 if (confp == NULL)
2441 {
paul3d1dc852005-04-05 00:45:23 +00002442 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2443 __func__, config_default_dir, safe_strerror (errno));
2444
hasso320ec102004-06-20 19:54:37 +00002445 confp = vty_use_backup_config (config_default_dir);
2446 if (confp)
2447 {
2448 fprintf (stderr, "WARNING: using backup configuration file!\n");
2449 fullpath = config_default_dir;
2450 }
2451 else
2452 {
2453 fprintf (stderr, "can't open configuration file [%s]\n",
2454 config_default_dir);
2455 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002456 }
hasso320ec102004-06-20 19:54:37 +00002457 }
paul718e3742002-12-13 20:15:29 +00002458 else
hasso320ec102004-06-20 19:54:37 +00002459 fullpath = config_default_dir;
2460 }
2461
paul718e3742002-12-13 20:15:29 +00002462 vty_read_file (confp);
2463
2464 fclose (confp);
2465
2466 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002467
2468 if (tmp)
2469 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002470}
2471
2472/* Small utility function which output log to the VTY. */
2473void
ajs274a4a42004-12-07 15:39:31 +00002474vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002475 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002476{
hasso8c328f12004-10-05 21:01:23 +00002477 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002478 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002479
2480 if (!vtyvec)
2481 return;
paul718e3742002-12-13 20:15:29 +00002482
paul55468c82005-03-14 20:19:01 +00002483 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002484 if ((vty = vector_slot (vtyvec, i)) != NULL)
2485 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002486 {
2487 va_list ac;
2488 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002489 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002490 va_end(ac);
2491 }
paul718e3742002-12-13 20:15:29 +00002492}
2493
ajs274a4a42004-12-07 15:39:31 +00002494/* Async-signal-safe version of vty_log for fixed strings. */
2495void
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002496vty_log_fixed (char *buf, size_t len)
ajs274a4a42004-12-07 15:39:31 +00002497{
2498 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002499 struct iovec iov[2];
2500
Paul Jakmaa4b30302006-05-28 08:18:38 +00002501 /* vty may not have been initialised */
2502 if (!vtyvec)
2503 return;
2504
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002505 iov[0].iov_base = buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002506 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002507 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002508 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002509
paul55468c82005-03-14 20:19:01 +00002510 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002511 {
2512 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002513 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2514 /* N.B. We don't care about the return code, since process is
2515 most likely just about to die anyway. */
David Lamparter4715a532013-05-30 16:31:49 +02002516 writev(vty->wfd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002517 }
2518}
2519
paul718e3742002-12-13 20:15:29 +00002520int
2521vty_config_lock (struct vty *vty)
2522{
2523 if (vty_config == 0)
2524 {
2525 vty->config = 1;
2526 vty_config = 1;
2527 }
2528 return vty->config;
2529}
2530
2531int
2532vty_config_unlock (struct vty *vty)
2533{
2534 if (vty_config == 1 && vty->config == 1)
2535 {
2536 vty->config = 0;
2537 vty_config = 0;
2538 }
2539 return vty->config;
2540}
David Lamparter6b0655a2014-06-04 06:53:35 +02002541
paul718e3742002-12-13 20:15:29 +00002542/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002543static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002544
2545static void
2546vty_event (enum event event, int sock, struct vty *vty)
2547{
2548 struct thread *vty_serv_thread;
2549
2550 switch (event)
2551 {
2552 case VTY_SERV:
2553 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2554 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2555 break;
2556#ifdef VTYSH
2557 case VTYSH_SERV:
Christian Franke677bcbb2013-02-27 13:47:23 +00002558 vty_serv_thread = thread_add_read (master, vtysh_accept, vty, sock);
2559 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
paul718e3742002-12-13 20:15:29 +00002560 break;
2561 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002562 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2563 break;
2564 case VTYSH_WRITE:
2565 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002566 break;
2567#endif /* VTYSH */
2568 case VTY_READ:
2569 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2570
2571 /* Time out treatment. */
2572 if (vty->v_timeout)
2573 {
2574 if (vty->t_timeout)
2575 thread_cancel (vty->t_timeout);
2576 vty->t_timeout =
2577 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2578 }
2579 break;
2580 case VTY_WRITE:
2581 if (! vty->t_write)
2582 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2583 break;
2584 case VTY_TIMEOUT_RESET:
2585 if (vty->t_timeout)
2586 {
2587 thread_cancel (vty->t_timeout);
2588 vty->t_timeout = NULL;
2589 }
2590 if (vty->v_timeout)
2591 {
2592 vty->t_timeout =
2593 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2594 }
2595 break;
2596 }
2597}
David Lamparter6b0655a2014-06-04 06:53:35 +02002598
paul718e3742002-12-13 20:15:29 +00002599DEFUN (config_who,
2600 config_who_cmd,
2601 "who",
2602 "Display who is on vty\n")
2603{
hasso8c328f12004-10-05 21:01:23 +00002604 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002605 struct vty *v;
2606
paul55468c82005-03-14 20:19:01 +00002607 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002608 if ((v = vector_slot (vtyvec, i)) != NULL)
2609 vty_out (vty, "%svty[%d] connected from %s.%s",
2610 v->config ? "*" : " ",
2611 i, v->address, VTY_NEWLINE);
2612 return CMD_SUCCESS;
2613}
2614
2615/* Move to vty configuration mode. */
2616DEFUN (line_vty,
2617 line_vty_cmd,
2618 "line vty",
2619 "Configure a terminal line\n"
2620 "Virtual terminal\n")
2621{
2622 vty->node = VTY_NODE;
2623 return CMD_SUCCESS;
2624}
2625
2626/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002627static int
paul9035efa2004-10-10 11:56:56 +00002628exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002629{
2630 unsigned long timeout = 0;
2631
2632 /* min_str and sec_str are already checked by parser. So it must be
2633 all digit string. */
2634 if (min_str)
2635 {
2636 timeout = strtol (min_str, NULL, 10);
2637 timeout *= 60;
2638 }
2639 if (sec_str)
2640 timeout += strtol (sec_str, NULL, 10);
2641
2642 vty_timeout_val = timeout;
2643 vty->v_timeout = timeout;
2644 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2645
2646
2647 return CMD_SUCCESS;
2648}
2649
2650DEFUN (exec_timeout_min,
2651 exec_timeout_min_cmd,
2652 "exec-timeout <0-35791>",
2653 "Set timeout value\n"
2654 "Timeout value in minutes\n")
2655{
2656 return exec_timeout (vty, argv[0], NULL);
2657}
2658
2659DEFUN (exec_timeout_sec,
2660 exec_timeout_sec_cmd,
2661 "exec-timeout <0-35791> <0-2147483>",
2662 "Set the EXEC timeout\n"
2663 "Timeout in minutes\n"
2664 "Timeout in seconds\n")
2665{
2666 return exec_timeout (vty, argv[0], argv[1]);
2667}
2668
2669DEFUN (no_exec_timeout,
2670 no_exec_timeout_cmd,
2671 "no exec-timeout",
2672 NO_STR
2673 "Set the EXEC timeout\n")
2674{
2675 return exec_timeout (vty, NULL, NULL);
2676}
2677
2678/* Set vty access class. */
2679DEFUN (vty_access_class,
2680 vty_access_class_cmd,
2681 "access-class WORD",
2682 "Filter connections based on an IP access list\n"
2683 "IP access list\n")
2684{
2685 if (vty_accesslist_name)
2686 XFREE(MTYPE_VTY, vty_accesslist_name);
2687
2688 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2689
2690 return CMD_SUCCESS;
2691}
2692
2693/* Clear vty access class. */
2694DEFUN (no_vty_access_class,
2695 no_vty_access_class_cmd,
2696 "no access-class [WORD]",
2697 NO_STR
2698 "Filter connections based on an IP access list\n"
2699 "IP access list\n")
2700{
2701 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2702 {
2703 vty_out (vty, "Access-class is not currently applied to vty%s",
2704 VTY_NEWLINE);
2705 return CMD_WARNING;
2706 }
2707
2708 XFREE(MTYPE_VTY, vty_accesslist_name);
2709
2710 vty_accesslist_name = NULL;
2711
2712 return CMD_SUCCESS;
2713}
2714
2715#ifdef HAVE_IPV6
2716/* Set vty access class. */
2717DEFUN (vty_ipv6_access_class,
2718 vty_ipv6_access_class_cmd,
2719 "ipv6 access-class WORD",
2720 IPV6_STR
2721 "Filter connections based on an IP access list\n"
2722 "IPv6 access list\n")
2723{
2724 if (vty_ipv6_accesslist_name)
2725 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2726
2727 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2728
2729 return CMD_SUCCESS;
2730}
2731
2732/* Clear vty access class. */
2733DEFUN (no_vty_ipv6_access_class,
2734 no_vty_ipv6_access_class_cmd,
2735 "no ipv6 access-class [WORD]",
2736 NO_STR
2737 IPV6_STR
2738 "Filter connections based on an IP access list\n"
2739 "IPv6 access list\n")
2740{
2741 if (! vty_ipv6_accesslist_name ||
2742 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2743 {
2744 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2745 VTY_NEWLINE);
2746 return CMD_WARNING;
2747 }
2748
2749 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2750
2751 vty_ipv6_accesslist_name = NULL;
2752
2753 return CMD_SUCCESS;
2754}
2755#endif /* HAVE_IPV6 */
2756
2757/* vty login. */
2758DEFUN (vty_login,
2759 vty_login_cmd,
2760 "login",
2761 "Enable password checking\n")
2762{
2763 no_password_check = 0;
2764 return CMD_SUCCESS;
2765}
2766
2767DEFUN (no_vty_login,
2768 no_vty_login_cmd,
2769 "no login",
2770 NO_STR
2771 "Enable password checking\n")
2772{
2773 no_password_check = 1;
2774 return CMD_SUCCESS;
2775}
2776
Paul Jakma62687ff2008-08-23 14:27:06 +01002777/* initial mode. */
2778DEFUN (vty_restricted_mode,
2779 vty_restricted_mode_cmd,
2780 "anonymous restricted",
2781 "Restrict view commands available in anonymous, unauthenticated vty\n")
2782{
2783 restricted_mode = 1;
2784 return CMD_SUCCESS;
2785}
2786
2787DEFUN (vty_no_restricted_mode,
2788 vty_no_restricted_mode_cmd,
2789 "no anonymous restricted",
2790 NO_STR
2791 "Enable password checking\n")
2792{
2793 restricted_mode = 0;
2794 return CMD_SUCCESS;
2795}
2796
paul718e3742002-12-13 20:15:29 +00002797DEFUN (service_advanced_vty,
2798 service_advanced_vty_cmd,
2799 "service advanced-vty",
2800 "Set up miscellaneous service\n"
2801 "Enable advanced mode vty interface\n")
2802{
2803 host.advanced = 1;
2804 return CMD_SUCCESS;
2805}
2806
2807DEFUN (no_service_advanced_vty,
2808 no_service_advanced_vty_cmd,
2809 "no service advanced-vty",
2810 NO_STR
2811 "Set up miscellaneous service\n"
2812 "Enable advanced mode vty interface\n")
2813{
2814 host.advanced = 0;
2815 return CMD_SUCCESS;
2816}
2817
2818DEFUN (terminal_monitor,
2819 terminal_monitor_cmd,
2820 "terminal monitor",
2821 "Set terminal line parameters\n"
2822 "Copy debug output to the current terminal line\n")
2823{
2824 vty->monitor = 1;
2825 return CMD_SUCCESS;
2826}
2827
2828DEFUN (terminal_no_monitor,
2829 terminal_no_monitor_cmd,
2830 "terminal no monitor",
2831 "Set terminal line parameters\n"
2832 NO_STR
2833 "Copy debug output to the current terminal line\n")
2834{
2835 vty->monitor = 0;
2836 return CMD_SUCCESS;
2837}
2838
paul789f78a2006-01-17 17:42:03 +00002839ALIAS (terminal_no_monitor,
2840 no_terminal_monitor_cmd,
2841 "no terminal monitor",
2842 NO_STR
2843 "Set terminal line parameters\n"
2844 "Copy debug output to the current terminal line\n")
2845
paul718e3742002-12-13 20:15:29 +00002846DEFUN (show_history,
2847 show_history_cmd,
2848 "show history",
2849 SHOW_STR
2850 "Display the session command history\n")
2851{
2852 int index;
2853
2854 for (index = vty->hindex + 1; index != vty->hindex;)
2855 {
2856 if (index == VTY_MAXHIST)
2857 {
2858 index = 0;
2859 continue;
2860 }
2861
2862 if (vty->hist[index] != NULL)
2863 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2864
2865 index++;
2866 }
2867
2868 return CMD_SUCCESS;
2869}
2870
2871/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002872static int
paul718e3742002-12-13 20:15:29 +00002873vty_config_write (struct vty *vty)
2874{
2875 vty_out (vty, "line vty%s", VTY_NEWLINE);
2876
2877 if (vty_accesslist_name)
2878 vty_out (vty, " access-class %s%s",
2879 vty_accesslist_name, VTY_NEWLINE);
2880
2881 if (vty_ipv6_accesslist_name)
2882 vty_out (vty, " ipv6 access-class %s%s",
2883 vty_ipv6_accesslist_name, VTY_NEWLINE);
2884
2885 /* exec-timeout */
2886 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2887 vty_out (vty, " exec-timeout %ld %ld%s",
2888 vty_timeout_val / 60,
2889 vty_timeout_val % 60, VTY_NEWLINE);
2890
2891 /* login */
2892 if (no_password_check)
2893 vty_out (vty, " no login%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +01002894
2895 if (restricted_mode != restricted_mode_default)
2896 {
2897 if (restricted_mode_default)
2898 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
2899 else
2900 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
2901 }
2902
paul718e3742002-12-13 20:15:29 +00002903 vty_out (vty, "!%s", VTY_NEWLINE);
2904
2905 return CMD_SUCCESS;
2906}
2907
2908struct cmd_node vty_node =
2909{
2910 VTY_NODE,
2911 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002912 1,
paul718e3742002-12-13 20:15:29 +00002913};
2914
2915/* Reset all VTY status. */
2916void
2917vty_reset ()
2918{
hasso8c328f12004-10-05 21:01:23 +00002919 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002920 struct vty *vty;
2921 struct thread *vty_serv_thread;
2922
paul55468c82005-03-14 20:19:01 +00002923 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002924 if ((vty = vector_slot (vtyvec, i)) != NULL)
2925 {
2926 buffer_reset (vty->obuf);
2927 vty->status = VTY_CLOSE;
2928 vty_close (vty);
2929 }
2930
paul55468c82005-03-14 20:19:01 +00002931 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002932 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2933 {
2934 thread_cancel (vty_serv_thread);
2935 vector_slot (Vvty_serv_thread, i) = NULL;
2936 close (i);
2937 }
2938
2939 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2940
2941 if (vty_accesslist_name)
2942 {
2943 XFREE(MTYPE_VTY, vty_accesslist_name);
2944 vty_accesslist_name = NULL;
2945 }
2946
2947 if (vty_ipv6_accesslist_name)
2948 {
2949 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2950 vty_ipv6_accesslist_name = NULL;
2951 }
2952}
2953
ajs9fc7ebf2005-02-23 15:12:34 +00002954static void
2955vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002956{
paul79ad2792003-10-15 22:09:28 +00002957 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002958 char *c;
paul718e3742002-12-13 20:15:29 +00002959
paulccc92352003-10-22 02:49:38 +00002960 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002961
paulccc92352003-10-22 02:49:38 +00002962 if (!c)
paul79ad2792003-10-15 22:09:28 +00002963 {
2964 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002965 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002966 }
paul718e3742002-12-13 20:15:29 +00002967
2968 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2969 strcpy (vty_cwd, cwd);
2970}
2971
2972char *
2973vty_get_cwd ()
2974{
2975 return vty_cwd;
2976}
2977
2978int
2979vty_shell (struct vty *vty)
2980{
2981 return vty->type == VTY_SHELL ? 1 : 0;
2982}
2983
2984int
2985vty_shell_serv (struct vty *vty)
2986{
2987 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2988}
2989
2990void
2991vty_init_vtysh ()
2992{
2993 vtyvec = vector_init (VECTOR_MIN_SIZE);
2994}
2995
2996/* Install vty's own commands like `who' command. */
2997void
paulb21b19c2003-06-15 01:28:29 +00002998vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00002999{
3000 /* For further configuration read, preserve current directory. */
3001 vty_save_cwd ();
3002
3003 vtyvec = vector_init (VECTOR_MIN_SIZE);
3004
paulb21b19c2003-06-15 01:28:29 +00003005 master = master_thread;
3006
paul718e3742002-12-13 20:15:29 +00003007 /* Initilize server thread vector. */
3008 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3009
3010 /* Install bgp top node. */
3011 install_node (&vty_node, vty_config_write);
3012
Paul Jakma62687ff2008-08-23 14:27:06 +01003013 install_element (RESTRICTED_NODE, &config_who_cmd);
3014 install_element (RESTRICTED_NODE, &show_history_cmd);
paul718e3742002-12-13 20:15:29 +00003015 install_element (VIEW_NODE, &config_who_cmd);
3016 install_element (VIEW_NODE, &show_history_cmd);
3017 install_element (ENABLE_NODE, &config_who_cmd);
3018 install_element (CONFIG_NODE, &line_vty_cmd);
3019 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3020 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3021 install_element (CONFIG_NODE, &show_history_cmd);
3022 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3023 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00003024 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00003025 install_element (ENABLE_NODE, &show_history_cmd);
3026
3027 install_default (VTY_NODE);
3028 install_element (VTY_NODE, &exec_timeout_min_cmd);
3029 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3030 install_element (VTY_NODE, &no_exec_timeout_cmd);
3031 install_element (VTY_NODE, &vty_access_class_cmd);
3032 install_element (VTY_NODE, &no_vty_access_class_cmd);
3033 install_element (VTY_NODE, &vty_login_cmd);
3034 install_element (VTY_NODE, &no_vty_login_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01003035 install_element (VTY_NODE, &vty_restricted_mode_cmd);
3036 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
paul718e3742002-12-13 20:15:29 +00003037#ifdef HAVE_IPV6
3038 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3039 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3040#endif /* HAVE_IPV6 */
3041}
Chris Caputo228da422009-07-18 05:44:03 +00003042
3043void
3044vty_terminate (void)
3045{
3046 if (vty_cwd)
3047 XFREE (MTYPE_TMP, vty_cwd);
3048
3049 if (vtyvec && Vvty_serv_thread)
3050 {
3051 vty_reset ();
3052 vector_free (vtyvec);
3053 vector_free (Vvty_serv_thread);
3054 }
3055}