blob: 480d34fd6a5aa815d6ce2182a97a43dc66af01ea [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
paulb21b19c2003-06-15 01:28:29 +000026#include "thread.h"
paul718e3742002-12-13 20:15:29 +000027#include "buffer.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "command.h"
30#include "sockunion.h"
paul718e3742002-12-13 20:15:29 +000031#include "memory.h"
32#include "str.h"
33#include "log.h"
34#include "prefix.h"
35#include "filter.h"
paulb21b19c2003-06-15 01:28:29 +000036#include "vty.h"
pauledd7c242003-06-04 13:59:38 +000037#include "privs.h"
ajs9fc7ebf2005-02-23 15:12:34 +000038#include "network.h"
39
40#include <arpa/telnet.h>
David Lamparterba53a8f2015-05-05 11:04:46 +020041#include <termios.h>
paul718e3742002-12-13 20:15:29 +000042
43/* Vty events */
44enum event
45{
46 VTY_SERV,
47 VTY_READ,
48 VTY_WRITE,
49 VTY_TIMEOUT_RESET,
50#ifdef VTYSH
51 VTYSH_SERV,
ajs49ff6d92004-11-04 19:26:16 +000052 VTYSH_READ,
53 VTYSH_WRITE
paul718e3742002-12-13 20:15:29 +000054#endif /* VTYSH */
55};
56
57static void vty_event (enum event, int, struct vty *);
58
59/* Extern host structure from command.c */
60extern struct host host;
David Lamparter6b0655a2014-06-04 06:53:35 +020061
paul718e3742002-12-13 20:15:29 +000062/* Vector which store each vty structure. */
63static vector vtyvec;
64
65/* Vty timeout value. */
66static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
67
68/* Vty access-class command */
69static char *vty_accesslist_name = NULL;
70
71/* Vty access-calss for IPv6. */
72static char *vty_ipv6_accesslist_name = NULL;
73
74/* VTY server thread. */
Christian Franke677bcbb2013-02-27 13:47:23 +000075static vector Vvty_serv_thread;
paul718e3742002-12-13 20:15:29 +000076
77/* Current directory. */
78char *vty_cwd = NULL;
79
80/* Configure lock. */
81static int vty_config;
82
83/* Login password check. */
84static int no_password_check = 0;
85
Paul Jakma62687ff2008-08-23 14:27:06 +010086/* Restrict unauthenticated logins? */
87static const u_char restricted_mode_default = 0;
88static u_char restricted_mode = 0;
89
paul718e3742002-12-13 20:15:29 +000090/* Integrated configuration file path */
91char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
92
David Lamparter6b0655a2014-06-04 06:53:35 +020093
paul718e3742002-12-13 20:15:29 +000094/* VTY standard output function. */
95int
96vty_out (struct vty *vty, const char *format, ...)
97{
98 va_list args;
99 int len = 0;
100 int size = 1024;
101 char buf[1024];
102 char *p = NULL;
paul718e3742002-12-13 20:15:29 +0000103
104 if (vty_shell (vty))
ajsd246bd92004-11-23 17:35:08 +0000105 {
106 va_start (args, format);
107 vprintf (format, args);
108 va_end (args);
109 }
paul718e3742002-12-13 20:15:29 +0000110 else
111 {
112 /* Try to write to initial buffer. */
ajsd246bd92004-11-23 17:35:08 +0000113 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000114 len = vsnprintf (buf, sizeof buf, format, args);
ajsd246bd92004-11-23 17:35:08 +0000115 va_end (args);
paul718e3742002-12-13 20:15:29 +0000116
117 /* Initial buffer is not enough. */
118 if (len < 0 || len >= size)
119 {
120 while (1)
121 {
122 if (len > -1)
123 size = len + 1;
124 else
125 size = size * 2;
126
127 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
128 if (! p)
129 return -1;
130
ajsd246bd92004-11-23 17:35:08 +0000131 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000132 len = vsnprintf (p, size, format, args);
ajsd246bd92004-11-23 17:35:08 +0000133 va_end (args);
paul718e3742002-12-13 20:15:29 +0000134
135 if (len > -1 && len < size)
136 break;
137 }
138 }
139
140 /* When initial buffer is enough to store all output. */
141 if (! p)
142 p = buf;
143
144 /* Pointer p must point out buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +0000145 buffer_put (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000146
147 /* If p is not different with buf, it is allocated buffer. */
148 if (p != buf)
149 XFREE (MTYPE_VTY_OUT_BUF, p);
150 }
151
paul718e3742002-12-13 20:15:29 +0000152 return len;
153}
154
ajsd246bd92004-11-23 17:35:08 +0000155static int
ajs274a4a42004-12-07 15:39:31 +0000156vty_log_out (struct vty *vty, const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000157 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +0000158{
ajs9fc7ebf2005-02-23 15:12:34 +0000159 int ret;
paul718e3742002-12-13 20:15:29 +0000160 int len;
161 char buf[1024];
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000162
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000163 if (!ctl->already_rendered)
164 {
165 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
166 ctl->already_rendered = 1;
167 }
168 if (ctl->len+1 >= sizeof(buf))
169 return -1;
170 memcpy(buf, ctl->buf, len = ctl->len);
171 buf[len++] = ' ';
172 buf[len] = '\0';
paul718e3742002-12-13 20:15:29 +0000173
ajs274a4a42004-12-07 15:39:31 +0000174 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000175 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000176 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000177 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
178 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000179 return -1;
paul718e3742002-12-13 20:15:29 +0000180
ajs9fc7ebf2005-02-23 15:12:34 +0000181 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
182 ((size_t)((len += ret)+2) > sizeof(buf)))
183 return -1;
paul718e3742002-12-13 20:15:29 +0000184
ajs9fc7ebf2005-02-23 15:12:34 +0000185 buf[len++] = '\r';
186 buf[len++] = '\n';
187
David Lamparter4715a532013-05-30 16:31:49 +0200188 if (write(vty->wfd, buf, len) < 0)
ajs9fc7ebf2005-02-23 15:12:34 +0000189 {
190 if (ERRNO_IO_RETRY(errno))
191 /* Kernel buffer is full, probably too much debugging output, so just
192 drop the data and ignore. */
193 return -1;
194 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000195 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000196 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
197 __func__, vty->fd, safe_strerror(errno));
198 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000199 /* cannot call vty_close, because a parent routine may still try
200 to access the vty struct */
201 vty->status = VTY_CLOSE;
202 shutdown(vty->fd, SHUT_RDWR);
ajs9fc7ebf2005-02-23 15:12:34 +0000203 return -1;
204 }
205 return 0;
paul718e3742002-12-13 20:15:29 +0000206}
207
208/* Output current time to the vty. */
209void
210vty_time_print (struct vty *vty, int cr)
211{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000212 char buf [25];
paul718e3742002-12-13 20:15:29 +0000213
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000214 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
paul718e3742002-12-13 20:15:29 +0000215 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000216 zlog (NULL, LOG_INFO, "quagga_timestamp error");
paul718e3742002-12-13 20:15:29 +0000217 return;
218 }
219 if (cr)
220 vty_out (vty, "%s\n", buf);
221 else
222 vty_out (vty, "%s ", buf);
223
224 return;
225}
226
227/* Say hello to vty interface. */
228void
229vty_hello (struct vty *vty)
230{
paul3b0c5d92005-03-08 10:43:43 +0000231 if (host.motdfile)
232 {
233 FILE *f;
234 char buf[4096];
paul22085182005-03-08 16:00:12 +0000235
paul3b0c5d92005-03-08 10:43:43 +0000236 f = fopen (host.motdfile, "r");
237 if (f)
238 {
paulb45da6f2005-03-08 15:16:57 +0000239 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000240 {
paulb45da6f2005-03-08 15:16:57 +0000241 char *s;
paul22085182005-03-08 16:00:12 +0000242 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000243 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000244 s--);
245 *s = '\0';
246 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
247 }
paul3b0c5d92005-03-08 10:43:43 +0000248 fclose (f);
249 }
250 else
paulb45da6f2005-03-08 15:16:57 +0000251 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000252 }
253 else if (host.motd)
Nico Goldeb830c892010-08-01 15:24:35 +0200254 vty_out (vty, "%s", host.motd);
paul718e3742002-12-13 20:15:29 +0000255}
256
257/* Put out prompt and wait input from user. */
258static void
259vty_prompt (struct vty *vty)
260{
261 struct utsname names;
262 const char*hostname;
263
264 if (vty->type == VTY_TERM)
265 {
266 hostname = host.name;
267 if (!hostname)
268 {
269 uname (&names);
270 hostname = names.nodename;
271 }
272 vty_out (vty, cmd_prompt (vty->node), hostname);
273 }
274}
275
276/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000277static void
paul718e3742002-12-13 20:15:29 +0000278vty_will_echo (struct vty *vty)
279{
paul02ff83c2004-06-11 11:27:03 +0000280 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000281 vty_out (vty, "%s", cmd);
282}
283
284/* Make suppress Go-Ahead telnet option. */
285static void
286vty_will_suppress_go_ahead (struct vty *vty)
287{
paul02ff83c2004-06-11 11:27:03 +0000288 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000289 vty_out (vty, "%s", cmd);
290}
291
292/* Make don't use linemode over telnet. */
293static void
294vty_dont_linemode (struct vty *vty)
295{
paul02ff83c2004-06-11 11:27:03 +0000296 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000297 vty_out (vty, "%s", cmd);
298}
299
300/* Use window size. */
301static void
302vty_do_window_size (struct vty *vty)
303{
paul02ff83c2004-06-11 11:27:03 +0000304 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000305 vty_out (vty, "%s", cmd);
306}
307
308#if 0 /* Currently not used. */
309/* Make don't use lflow vty interface. */
310static void
311vty_dont_lflow_ahead (struct vty *vty)
312{
paul02ff83c2004-06-11 11:27:03 +0000313 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000314 vty_out (vty, "%s", cmd);
315}
316#endif /* 0 */
317
318/* Allocate new vty struct. */
319struct vty *
320vty_new ()
321{
322 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
323
ajs9fc7ebf2005-02-23 15:12:34 +0000324 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000325 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
326 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000327
328 return new;
329}
330
331/* Authentication of vty */
332static void
333vty_auth (struct vty *vty, char *buf)
334{
335 char *passwd = NULL;
336 enum node_type next_node = 0;
337 int fail;
338 char *crypt (const char *, const char *);
339
340 switch (vty->node)
341 {
342 case AUTH_NODE:
343 if (host.encrypt)
344 passwd = host.password_encrypt;
345 else
346 passwd = host.password;
347 if (host.advanced)
348 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
349 else
350 next_node = VIEW_NODE;
351 break;
352 case AUTH_ENABLE_NODE:
353 if (host.encrypt)
354 passwd = host.enable_encrypt;
355 else
356 passwd = host.enable;
357 next_node = ENABLE_NODE;
358 break;
359 }
360
361 if (passwd)
362 {
363 if (host.encrypt)
364 fail = strcmp (crypt(buf, passwd), passwd);
365 else
366 fail = strcmp (buf, passwd);
367 }
368 else
369 fail = 1;
370
371 if (! fail)
372 {
373 vty->fail = 0;
374 vty->node = next_node; /* Success ! */
375 }
376 else
377 {
378 vty->fail++;
379 if (vty->fail >= 3)
380 {
381 if (vty->node == AUTH_NODE)
382 {
383 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
384 vty->status = VTY_CLOSE;
385 }
386 else
387 {
388 /* AUTH_ENABLE_NODE */
389 vty->fail = 0;
390 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +0100391 vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
paul718e3742002-12-13 20:15:29 +0000392 }
393 }
394 }
395}
396
397/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000398static int
paul718e3742002-12-13 20:15:29 +0000399vty_command (struct vty *vty, char *buf)
400{
401 int ret;
402 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000403 const char *protocolname;
paul718e3742002-12-13 20:15:29 +0000404
405 /* Split readline string up into the vector */
406 vline = cmd_make_strvec (buf);
407
408 if (vline == NULL)
409 return CMD_SUCCESS;
410
ajs924b9222005-04-16 17:11:24 +0000411#ifdef CONSUMED_TIME_CHECK
412 {
413 RUSAGE_T before;
414 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000415 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000416
417 GETRUSAGE(&before);
418#endif /* CONSUMED_TIME_CHECK */
419
hasso87d683b2005-01-16 23:31:54 +0000420 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000421
vincentfbf5d032005-09-29 11:25:50 +0000422 /* Get the name of the protocol if any */
423 if (zlog_default)
424 protocolname = zlog_proto_names[zlog_default->protocol];
425 else
426 protocolname = zlog_proto_names[ZLOG_NONE];
427
ajs924b9222005-04-16 17:11:24 +0000428#ifdef CONSUMED_TIME_CHECK
429 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000430 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
431 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000432 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000433 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
434 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000435 }
436#endif /* CONSUMED_TIME_CHECK */
437
paul718e3742002-12-13 20:15:29 +0000438 if (ret != CMD_SUCCESS)
439 switch (ret)
440 {
441 case CMD_WARNING:
442 if (vty->type == VTY_FILE)
443 vty_out (vty, "Warning...%s", VTY_NEWLINE);
444 break;
445 case CMD_ERR_AMBIGUOUS:
446 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
447 break;
448 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000449 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000450 break;
451 case CMD_ERR_INCOMPLETE:
452 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
453 break;
454 }
455 cmd_free_strvec (vline);
456
457 return ret;
458}
David Lamparter6b0655a2014-06-04 06:53:35 +0200459
ajs9fc7ebf2005-02-23 15:12:34 +0000460static const char telnet_backward_char = 0x08;
461static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000462
463/* Basic function to write buffer to vty. */
464static void
ajs9fc7ebf2005-02-23 15:12:34 +0000465vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000466{
467 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
468 return;
469
470 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000471 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000472}
473
474/* Ensure length of input buffer. Is buffer is short, double it. */
475static void
476vty_ensure (struct vty *vty, int length)
477{
478 if (vty->max <= length)
479 {
480 vty->max *= 2;
481 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
482 }
483}
484
485/* Basic function to insert character into vty. */
486static void
487vty_self_insert (struct vty *vty, char c)
488{
489 int i;
490 int length;
491
492 vty_ensure (vty, vty->length + 1);
493 length = vty->length - vty->cp;
494 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
495 vty->buf[vty->cp] = c;
496
497 vty_write (vty, &vty->buf[vty->cp], length + 1);
498 for (i = 0; i < length; i++)
499 vty_write (vty, &telnet_backward_char, 1);
500
501 vty->cp++;
502 vty->length++;
503}
504
505/* Self insert character 'c' in overwrite mode. */
506static void
507vty_self_insert_overwrite (struct vty *vty, char c)
508{
509 vty_ensure (vty, vty->length + 1);
510 vty->buf[vty->cp++] = c;
511
512 if (vty->cp > vty->length)
513 vty->length++;
514
515 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
516 return;
517
518 vty_write (vty, &c, 1);
519}
520
521/* Insert a word into vty interface with overwrite mode. */
522static void
523vty_insert_word_overwrite (struct vty *vty, char *str)
524{
525 int len = strlen (str);
526 vty_write (vty, str, len);
527 strcpy (&vty->buf[vty->cp], str);
528 vty->cp += len;
529 vty->length = vty->cp;
530}
531
532/* Forward character. */
533static void
534vty_forward_char (struct vty *vty)
535{
536 if (vty->cp < vty->length)
537 {
538 vty_write (vty, &vty->buf[vty->cp], 1);
539 vty->cp++;
540 }
541}
542
543/* Backward character. */
544static void
545vty_backward_char (struct vty *vty)
546{
547 if (vty->cp > 0)
548 {
549 vty->cp--;
550 vty_write (vty, &telnet_backward_char, 1);
551 }
552}
553
554/* Move to the beginning of the line. */
555static void
556vty_beginning_of_line (struct vty *vty)
557{
558 while (vty->cp)
559 vty_backward_char (vty);
560}
561
562/* Move to the end of the line. */
563static void
564vty_end_of_line (struct vty *vty)
565{
566 while (vty->cp < vty->length)
567 vty_forward_char (vty);
568}
569
570static void vty_kill_line_from_beginning (struct vty *);
571static void vty_redraw_line (struct vty *);
572
573/* Print command line history. This function is called from
574 vty_next_line and vty_previous_line. */
575static void
576vty_history_print (struct vty *vty)
577{
578 int length;
579
580 vty_kill_line_from_beginning (vty);
581
582 /* Get previous line from history buffer */
583 length = strlen (vty->hist[vty->hp]);
584 memcpy (vty->buf, vty->hist[vty->hp], length);
585 vty->cp = vty->length = length;
586
587 /* Redraw current line */
588 vty_redraw_line (vty);
589}
590
591/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000592static void
paul718e3742002-12-13 20:15:29 +0000593vty_next_line (struct vty *vty)
594{
595 int try_index;
596
597 if (vty->hp == vty->hindex)
598 return;
599
600 /* Try is there history exist or not. */
601 try_index = vty->hp;
602 if (try_index == (VTY_MAXHIST - 1))
603 try_index = 0;
604 else
605 try_index++;
606
607 /* If there is not history return. */
608 if (vty->hist[try_index] == NULL)
609 return;
610 else
611 vty->hp = try_index;
612
613 vty_history_print (vty);
614}
615
616/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000617static void
paul718e3742002-12-13 20:15:29 +0000618vty_previous_line (struct vty *vty)
619{
620 int try_index;
621
622 try_index = vty->hp;
623 if (try_index == 0)
624 try_index = VTY_MAXHIST - 1;
625 else
626 try_index--;
627
628 if (vty->hist[try_index] == NULL)
629 return;
630 else
631 vty->hp = try_index;
632
633 vty_history_print (vty);
634}
635
636/* This function redraw all of the command line character. */
637static void
638vty_redraw_line (struct vty *vty)
639{
640 vty_write (vty, vty->buf, vty->length);
641 vty->cp = vty->length;
642}
643
644/* Forward word. */
645static void
646vty_forward_word (struct vty *vty)
647{
648 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
649 vty_forward_char (vty);
650
651 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
652 vty_forward_char (vty);
653}
654
655/* Backward word without skipping training space. */
656static void
657vty_backward_pure_word (struct vty *vty)
658{
659 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
660 vty_backward_char (vty);
661}
662
663/* Backward word. */
664static void
665vty_backward_word (struct vty *vty)
666{
667 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
668 vty_backward_char (vty);
669
670 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
671 vty_backward_char (vty);
672}
673
674/* When '^D' is typed at the beginning of the line we move to the down
675 level. */
676static void
677vty_down_level (struct vty *vty)
678{
679 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000680 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000681 vty_prompt (vty);
682 vty->cp = 0;
683}
684
685/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000686static void
paul718e3742002-12-13 20:15:29 +0000687vty_end_config (struct vty *vty)
688{
689 vty_out (vty, "%s", VTY_NEWLINE);
690
691 switch (vty->node)
692 {
693 case VIEW_NODE:
694 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +0100695 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +0000696 /* Nothing to do. */
697 break;
698 case CONFIG_NODE:
699 case INTERFACE_NODE:
700 case ZEBRA_NODE:
701 case RIP_NODE:
702 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +0100703 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +0000704 case BGP_NODE:
705 case BGP_VPNV4_NODE:
706 case BGP_IPV4_NODE:
707 case BGP_IPV4M_NODE:
708 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000709 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000710 case RMAP_NODE:
711 case OSPF_NODE:
712 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000713 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000714 case KEYCHAIN_NODE:
715 case KEYCHAIN_KEY_NODE:
716 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -0200717 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +0000718 case VTY_NODE:
719 vty_config_unlock (vty);
720 vty->node = ENABLE_NODE;
721 break;
722 default:
723 /* Unknown node, we have to ignore it. */
724 break;
725 }
726
727 vty_prompt (vty);
728 vty->cp = 0;
729}
730
731/* Delete a charcter at the current point. */
732static void
733vty_delete_char (struct vty *vty)
734{
735 int i;
736 int size;
737
paul718e3742002-12-13 20:15:29 +0000738 if (vty->length == 0)
739 {
740 vty_down_level (vty);
741 return;
742 }
743
744 if (vty->cp == vty->length)
745 return; /* completion need here? */
746
747 size = vty->length - vty->cp;
748
749 vty->length--;
750 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
751 vty->buf[vty->length] = '\0';
Roy7f794f22008-08-13 17:27:38 +0100752
753 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
754 return;
paul718e3742002-12-13 20:15:29 +0000755
756 vty_write (vty, &vty->buf[vty->cp], size - 1);
757 vty_write (vty, &telnet_space_char, 1);
758
759 for (i = 0; i < size; i++)
760 vty_write (vty, &telnet_backward_char, 1);
761}
762
763/* Delete a character before the point. */
764static void
765vty_delete_backward_char (struct vty *vty)
766{
767 if (vty->cp == 0)
768 return;
769
770 vty_backward_char (vty);
771 vty_delete_char (vty);
772}
773
774/* Kill rest of line from current point. */
775static void
776vty_kill_line (struct vty *vty)
777{
778 int i;
779 int size;
780
781 size = vty->length - vty->cp;
782
783 if (size == 0)
784 return;
785
786 for (i = 0; i < size; i++)
787 vty_write (vty, &telnet_space_char, 1);
788 for (i = 0; i < size; i++)
789 vty_write (vty, &telnet_backward_char, 1);
790
791 memset (&vty->buf[vty->cp], 0, size);
792 vty->length = vty->cp;
793}
794
795/* Kill line from the beginning. */
796static void
797vty_kill_line_from_beginning (struct vty *vty)
798{
799 vty_beginning_of_line (vty);
800 vty_kill_line (vty);
801}
802
803/* Delete a word before the point. */
804static void
805vty_forward_kill_word (struct vty *vty)
806{
807 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
808 vty_delete_char (vty);
809 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
810 vty_delete_char (vty);
811}
812
813/* Delete a word before the point. */
814static void
815vty_backward_kill_word (struct vty *vty)
816{
817 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
818 vty_delete_backward_char (vty);
819 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
820 vty_delete_backward_char (vty);
821}
822
823/* Transpose chars before or at the point. */
824static void
825vty_transpose_chars (struct vty *vty)
826{
827 char c1, c2;
828
829 /* If length is short or point is near by the beginning of line then
830 return. */
831 if (vty->length < 2 || vty->cp < 1)
832 return;
833
834 /* In case of point is located at the end of the line. */
835 if (vty->cp == vty->length)
836 {
837 c1 = vty->buf[vty->cp - 1];
838 c2 = vty->buf[vty->cp - 2];
839
840 vty_backward_char (vty);
841 vty_backward_char (vty);
842 vty_self_insert_overwrite (vty, c1);
843 vty_self_insert_overwrite (vty, c2);
844 }
845 else
846 {
847 c1 = vty->buf[vty->cp];
848 c2 = vty->buf[vty->cp - 1];
849
850 vty_backward_char (vty);
851 vty_self_insert_overwrite (vty, c1);
852 vty_self_insert_overwrite (vty, c2);
853 }
854}
855
856/* Do completion at vty interface. */
857static void
858vty_complete_command (struct vty *vty)
859{
860 int i;
861 int ret;
862 char **matched = NULL;
863 vector vline;
864
865 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
866 return;
867
868 vline = cmd_make_strvec (vty->buf);
869 if (vline == NULL)
870 return;
871
872 /* In case of 'help \t'. */
873 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +0100874 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000875
Lou Berger67290032016-01-12 13:41:46 -0500876 matched = cmd_complete_command_lib (vline, vty, &ret, 1);
paul718e3742002-12-13 20:15:29 +0000877
878 cmd_free_strvec (vline);
879
880 vty_out (vty, "%s", VTY_NEWLINE);
881 switch (ret)
882 {
883 case CMD_ERR_AMBIGUOUS:
884 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
885 vty_prompt (vty);
886 vty_redraw_line (vty);
887 break;
888 case CMD_ERR_NO_MATCH:
889 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
890 vty_prompt (vty);
891 vty_redraw_line (vty);
892 break;
893 case CMD_COMPLETE_FULL_MATCH:
894 vty_prompt (vty);
895 vty_redraw_line (vty);
896 vty_backward_pure_word (vty);
897 vty_insert_word_overwrite (vty, matched[0]);
898 vty_self_insert (vty, ' ');
899 XFREE (MTYPE_TMP, matched[0]);
900 break;
901 case CMD_COMPLETE_MATCH:
902 vty_prompt (vty);
903 vty_redraw_line (vty);
904 vty_backward_pure_word (vty);
905 vty_insert_word_overwrite (vty, matched[0]);
906 XFREE (MTYPE_TMP, matched[0]);
907 vector_only_index_free (matched);
908 return;
909 break;
910 case CMD_COMPLETE_LIST_MATCH:
911 for (i = 0; matched[i] != NULL; i++)
912 {
913 if (i != 0 && ((i % 6) == 0))
914 vty_out (vty, "%s", VTY_NEWLINE);
915 vty_out (vty, "%-10s ", matched[i]);
916 XFREE (MTYPE_TMP, matched[i]);
917 }
918 vty_out (vty, "%s", VTY_NEWLINE);
919
920 vty_prompt (vty);
921 vty_redraw_line (vty);
922 break;
923 case CMD_ERR_NOTHING_TODO:
924 vty_prompt (vty);
925 vty_redraw_line (vty);
926 break;
927 default:
928 break;
929 }
930 if (matched)
931 vector_only_index_free (matched);
932}
933
ajs9fc7ebf2005-02-23 15:12:34 +0000934static void
paul718e3742002-12-13 20:15:29 +0000935vty_describe_fold (struct vty *vty, int cmd_width,
Christian Frankecd40b322013-09-30 12:27:51 +0000936 unsigned int desc_width, struct cmd_token *token)
paul718e3742002-12-13 20:15:29 +0000937{
hasso8c328f12004-10-05 21:01:23 +0000938 char *buf;
939 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000940 int pos;
941
Christian Frankecd40b322013-09-30 12:27:51 +0000942 cmd = token->cmd[0] == '.' ? token->cmd + 1 : token->cmd;
paul718e3742002-12-13 20:15:29 +0000943
944 if (desc_width <= 0)
945 {
Christian Frankecd40b322013-09-30 12:27:51 +0000946 vty_out (vty, " %-*s %s%s", cmd_width, cmd, token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000947 return;
948 }
949
Christian Frankecd40b322013-09-30 12:27:51 +0000950 buf = XCALLOC (MTYPE_TMP, strlen (token->desc) + 1);
paul718e3742002-12-13 20:15:29 +0000951
Christian Frankecd40b322013-09-30 12:27:51 +0000952 for (p = token->desc; strlen (p) > desc_width; p += pos + 1)
paul718e3742002-12-13 20:15:29 +0000953 {
954 for (pos = desc_width; pos > 0; pos--)
955 if (*(p + pos) == ' ')
956 break;
957
958 if (pos == 0)
959 break;
960
961 strncpy (buf, p, pos);
962 buf[pos] = '\0';
963 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
964
965 cmd = "";
966 }
967
968 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
969
970 XFREE (MTYPE_TMP, buf);
971}
972
973/* Describe matched command function. */
974static void
975vty_describe_command (struct vty *vty)
976{
977 int ret;
978 vector vline;
979 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000980 unsigned int i, width, desc_width;
Christian Frankecd40b322013-09-30 12:27:51 +0000981 struct cmd_token *token, *token_cr = NULL;
paul718e3742002-12-13 20:15:29 +0000982
983 vline = cmd_make_strvec (vty->buf);
984
985 /* In case of '> ?'. */
986 if (vline == NULL)
987 {
988 vline = vector_init (1);
David Lampartera91a3ba2015-03-03 09:06:51 +0100989 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000990 }
991 else
992 if (isspace ((int) vty->buf[vty->length - 1]))
David Lampartera91a3ba2015-03-03 09:06:51 +0100993 vector_set (vline, NULL);
paul718e3742002-12-13 20:15:29 +0000994
995 describe = cmd_describe_command (vline, vty, &ret);
996
997 vty_out (vty, "%s", VTY_NEWLINE);
998
999 /* Ambiguous error. */
1000 switch (ret)
1001 {
1002 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +00001003 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001004 goto out;
paul718e3742002-12-13 20:15:29 +00001005 break;
1006 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +00001007 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001008 goto out;
paul718e3742002-12-13 20:15:29 +00001009 break;
1010 }
1011
1012 /* Get width of command string. */
1013 width = 0;
paul55468c82005-03-14 20:19:01 +00001014 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001015 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001016 {
hasso8c328f12004-10-05 21:01:23 +00001017 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001018
Christian Frankecd40b322013-09-30 12:27:51 +00001019 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001020 continue;
1021
Christian Frankecd40b322013-09-30 12:27:51 +00001022 len = strlen (token->cmd);
1023 if (token->cmd[0] == '.')
paul718e3742002-12-13 20:15:29 +00001024 len--;
1025
1026 if (width < len)
1027 width = len;
1028 }
1029
1030 /* Get width of description string. */
1031 desc_width = vty->width - (width + 6);
1032
1033 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001034 for (i = 0; i < vector_active (describe); i++)
Christian Frankecd40b322013-09-30 12:27:51 +00001035 if ((token = vector_slot (describe, i)) != NULL)
paul718e3742002-12-13 20:15:29 +00001036 {
Christian Frankecd40b322013-09-30 12:27:51 +00001037 if (token->cmd[0] == '\0')
paul718e3742002-12-13 20:15:29 +00001038 continue;
1039
Christian Frankecd40b322013-09-30 12:27:51 +00001040 if (strcmp (token->cmd, command_cr) == 0)
paul718e3742002-12-13 20:15:29 +00001041 {
Christian Frankecd40b322013-09-30 12:27:51 +00001042 token_cr = token;
paul718e3742002-12-13 20:15:29 +00001043 continue;
1044 }
1045
Christian Frankecd40b322013-09-30 12:27:51 +00001046 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001047 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001048 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001049 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001050 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001051 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001052 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1053 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001054 else
Christian Frankecd40b322013-09-30 12:27:51 +00001055 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001056
1057#if 0
1058 vty_out (vty, " %-*s %s%s", width
1059 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1060 desc->str ? desc->str : "", VTY_NEWLINE);
1061#endif /* 0 */
1062 }
1063
Christian Frankecd40b322013-09-30 12:27:51 +00001064 if ((token = token_cr))
paul718e3742002-12-13 20:15:29 +00001065 {
Christian Frankecd40b322013-09-30 12:27:51 +00001066 if (!token->desc)
paul718e3742002-12-13 20:15:29 +00001067 vty_out (vty, " %-s%s",
Christian Frankecd40b322013-09-30 12:27:51 +00001068 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
paul718e3742002-12-13 20:15:29 +00001069 VTY_NEWLINE);
Christian Frankecd40b322013-09-30 12:27:51 +00001070 else if (desc_width >= strlen (token->desc))
paul718e3742002-12-13 20:15:29 +00001071 vty_out (vty, " %-*s %s%s", width,
Christian Frankecd40b322013-09-30 12:27:51 +00001072 token->cmd[0] == '.' ? token->cmd + 1 : token->cmd,
1073 token->desc, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001074 else
Christian Frankecd40b322013-09-30 12:27:51 +00001075 vty_describe_fold (vty, width, desc_width, token);
paul718e3742002-12-13 20:15:29 +00001076 }
1077
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001078out:
paul718e3742002-12-13 20:15:29 +00001079 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001080 if (describe)
1081 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001082
1083 vty_prompt (vty);
1084 vty_redraw_line (vty);
1085}
1086
ajs9fc7ebf2005-02-23 15:12:34 +00001087static void
paul718e3742002-12-13 20:15:29 +00001088vty_clear_buf (struct vty *vty)
1089{
1090 memset (vty->buf, 0, vty->max);
1091}
1092
1093/* ^C stop current input and do not add command line to the history. */
1094static void
1095vty_stop_input (struct vty *vty)
1096{
1097 vty->cp = vty->length = 0;
1098 vty_clear_buf (vty);
1099 vty_out (vty, "%s", VTY_NEWLINE);
1100
1101 switch (vty->node)
1102 {
1103 case VIEW_NODE:
1104 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01001105 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00001106 /* Nothing to do. */
1107 break;
1108 case CONFIG_NODE:
1109 case INTERFACE_NODE:
1110 case ZEBRA_NODE:
1111 case RIP_NODE:
1112 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01001113 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00001114 case BGP_NODE:
1115 case RMAP_NODE:
1116 case OSPF_NODE:
1117 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001118 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001119 case KEYCHAIN_NODE:
1120 case KEYCHAIN_KEY_NODE:
1121 case MASC_NODE:
Everton Marques42e30782009-11-18 17:19:43 -02001122 case PIM_NODE:
paul718e3742002-12-13 20:15:29 +00001123 case VTY_NODE:
1124 vty_config_unlock (vty);
1125 vty->node = ENABLE_NODE;
1126 break;
1127 default:
1128 /* Unknown node, we have to ignore it. */
1129 break;
1130 }
1131 vty_prompt (vty);
1132
1133 /* Set history pointer to the latest one. */
1134 vty->hp = vty->hindex;
1135}
1136
1137/* Add current command line to the history buffer. */
1138static void
1139vty_hist_add (struct vty *vty)
1140{
1141 int index;
1142
1143 if (vty->length == 0)
1144 return;
1145
1146 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1147
1148 /* Ignore the same string as previous one. */
1149 if (vty->hist[index])
1150 if (strcmp (vty->buf, vty->hist[index]) == 0)
1151 {
1152 vty->hp = vty->hindex;
1153 return;
1154 }
1155
1156 /* Insert history entry. */
1157 if (vty->hist[vty->hindex])
1158 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1159 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1160
1161 /* History index rotation. */
1162 vty->hindex++;
1163 if (vty->hindex == VTY_MAXHIST)
1164 vty->hindex = 0;
1165
1166 vty->hp = vty->hindex;
1167}
1168
1169/* #define TELNET_OPTION_DEBUG */
1170
1171/* Get telnet window size. */
1172static int
1173vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1174{
1175#ifdef TELNET_OPTION_DEBUG
1176 int i;
1177
1178 for (i = 0; i < nbytes; i++)
1179 {
1180 switch (buf[i])
1181 {
1182 case IAC:
1183 vty_out (vty, "IAC ");
1184 break;
1185 case WILL:
1186 vty_out (vty, "WILL ");
1187 break;
1188 case WONT:
1189 vty_out (vty, "WONT ");
1190 break;
1191 case DO:
1192 vty_out (vty, "DO ");
1193 break;
1194 case DONT:
1195 vty_out (vty, "DONT ");
1196 break;
1197 case SB:
1198 vty_out (vty, "SB ");
1199 break;
1200 case SE:
1201 vty_out (vty, "SE ");
1202 break;
1203 case TELOPT_ECHO:
1204 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1205 break;
1206 case TELOPT_SGA:
1207 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1208 break;
1209 case TELOPT_NAWS:
1210 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1211 break;
1212 default:
1213 vty_out (vty, "%x ", buf[i]);
1214 break;
1215 }
1216 }
1217 vty_out (vty, "%s", VTY_NEWLINE);
1218
1219#endif /* TELNET_OPTION_DEBUG */
1220
1221 switch (buf[0])
1222 {
1223 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001224 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001225 vty->iac_sb_in_progress = 1;
1226 return 0;
1227 break;
1228 case SE:
1229 {
paul718e3742002-12-13 20:15:29 +00001230 if (!vty->iac_sb_in_progress)
1231 return 0;
1232
ajs9fc7ebf2005-02-23 15:12:34 +00001233 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001234 {
1235 vty->iac_sb_in_progress = 0;
1236 return 0;
1237 }
ajs9fc7ebf2005-02-23 15:12:34 +00001238 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001239 {
1240 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001241 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1242 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1243 "should send %d characters, but we received %lu",
1244 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1245 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1246 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1247 "too small to handle the telnet NAWS option",
1248 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1249 else
1250 {
1251 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1252 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1253#ifdef TELNET_OPTION_DEBUG
1254 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1255 "width %d, height %d%s",
1256 vty->width, vty->height, VTY_NEWLINE);
1257#endif
1258 }
paul718e3742002-12-13 20:15:29 +00001259 break;
1260 }
1261 vty->iac_sb_in_progress = 0;
1262 return 0;
1263 break;
1264 }
1265 default:
1266 break;
1267 }
1268 return 1;
1269}
1270
1271/* Execute current command line. */
1272static int
1273vty_execute (struct vty *vty)
1274{
1275 int ret;
1276
1277 ret = CMD_SUCCESS;
1278
1279 switch (vty->node)
1280 {
1281 case AUTH_NODE:
1282 case AUTH_ENABLE_NODE:
1283 vty_auth (vty, vty->buf);
1284 break;
1285 default:
1286 ret = vty_command (vty, vty->buf);
1287 if (vty->type == VTY_TERM)
1288 vty_hist_add (vty);
1289 break;
1290 }
1291
1292 /* Clear command line buffer. */
1293 vty->cp = vty->length = 0;
1294 vty_clear_buf (vty);
1295
ajs5a646652004-11-05 01:25:55 +00001296 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001297 vty_prompt (vty);
1298
1299 return ret;
1300}
1301
1302#define CONTROL(X) ((X) - '@')
1303#define VTY_NORMAL 0
1304#define VTY_PRE_ESCAPE 1
1305#define VTY_ESCAPE 2
1306
1307/* Escape character command map. */
1308static void
1309vty_escape_map (unsigned char c, struct vty *vty)
1310{
1311 switch (c)
1312 {
1313 case ('A'):
1314 vty_previous_line (vty);
1315 break;
1316 case ('B'):
1317 vty_next_line (vty);
1318 break;
1319 case ('C'):
1320 vty_forward_char (vty);
1321 break;
1322 case ('D'):
1323 vty_backward_char (vty);
1324 break;
1325 default:
1326 break;
1327 }
1328
1329 /* Go back to normal mode. */
1330 vty->escape = VTY_NORMAL;
1331}
1332
1333/* Quit print out to the buffer. */
1334static void
1335vty_buffer_reset (struct vty *vty)
1336{
1337 buffer_reset (vty->obuf);
1338 vty_prompt (vty);
1339 vty_redraw_line (vty);
1340}
1341
1342/* Read data via vty socket. */
1343static int
1344vty_read (struct thread *thread)
1345{
1346 int i;
paul718e3742002-12-13 20:15:29 +00001347 int nbytes;
1348 unsigned char buf[VTY_READ_BUFSIZ];
1349
1350 int vty_sock = THREAD_FD (thread);
1351 struct vty *vty = THREAD_ARG (thread);
1352 vty->t_read = NULL;
1353
1354 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001355 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1356 {
1357 if (nbytes < 0)
1358 {
1359 if (ERRNO_IO_RETRY(errno))
1360 {
1361 vty_event (VTY_READ, vty_sock, vty);
1362 return 0;
1363 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001364 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001365 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1366 __func__, vty->fd, safe_strerror(errno));
David Lamparter90d31352015-05-14 14:24:06 +02001367 buffer_reset(vty->obuf);
ajs9fc7ebf2005-02-23 15:12:34 +00001368 }
ajs9fc7ebf2005-02-23 15:12:34 +00001369 vty->status = VTY_CLOSE;
1370 }
paul718e3742002-12-13 20:15:29 +00001371
1372 for (i = 0; i < nbytes; i++)
1373 {
1374 if (buf[i] == IAC)
1375 {
1376 if (!vty->iac)
1377 {
1378 vty->iac = 1;
1379 continue;
1380 }
1381 else
1382 {
1383 vty->iac = 0;
1384 }
1385 }
1386
1387 if (vty->iac_sb_in_progress && !vty->iac)
1388 {
ajs9fc7ebf2005-02-23 15:12:34 +00001389 if (vty->sb_len < sizeof(vty->sb_buf))
1390 vty->sb_buf[vty->sb_len] = buf[i];
1391 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001392 continue;
1393 }
1394
1395 if (vty->iac)
1396 {
1397 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001398 int ret = 0;
paule9372532003-10-26 21:36:07 +00001399 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001400 vty->iac = 0;
1401 i += ret;
1402 continue;
1403 }
paul5b8c1b02003-10-15 23:08:55 +00001404
paul718e3742002-12-13 20:15:29 +00001405
1406 if (vty->status == VTY_MORE)
1407 {
1408 switch (buf[i])
1409 {
1410 case CONTROL('C'):
1411 case 'q':
1412 case 'Q':
paul718e3742002-12-13 20:15:29 +00001413 vty_buffer_reset (vty);
1414 break;
1415#if 0 /* More line does not work for "show ip bgp". */
1416 case '\n':
1417 case '\r':
1418 vty->status = VTY_MORELINE;
1419 break;
1420#endif
1421 default:
paul718e3742002-12-13 20:15:29 +00001422 break;
1423 }
1424 continue;
1425 }
1426
1427 /* Escape character. */
1428 if (vty->escape == VTY_ESCAPE)
1429 {
1430 vty_escape_map (buf[i], vty);
1431 continue;
1432 }
1433
1434 /* Pre-escape status. */
1435 if (vty->escape == VTY_PRE_ESCAPE)
1436 {
1437 switch (buf[i])
1438 {
1439 case '[':
1440 vty->escape = VTY_ESCAPE;
1441 break;
1442 case 'b':
1443 vty_backward_word (vty);
1444 vty->escape = VTY_NORMAL;
1445 break;
1446 case 'f':
1447 vty_forward_word (vty);
1448 vty->escape = VTY_NORMAL;
1449 break;
1450 case 'd':
1451 vty_forward_kill_word (vty);
1452 vty->escape = VTY_NORMAL;
1453 break;
1454 case CONTROL('H'):
1455 case 0x7f:
1456 vty_backward_kill_word (vty);
1457 vty->escape = VTY_NORMAL;
1458 break;
1459 default:
1460 vty->escape = VTY_NORMAL;
1461 break;
1462 }
1463 continue;
1464 }
1465
1466 switch (buf[i])
1467 {
1468 case CONTROL('A'):
1469 vty_beginning_of_line (vty);
1470 break;
1471 case CONTROL('B'):
1472 vty_backward_char (vty);
1473 break;
1474 case CONTROL('C'):
1475 vty_stop_input (vty);
1476 break;
1477 case CONTROL('D'):
1478 vty_delete_char (vty);
1479 break;
1480 case CONTROL('E'):
1481 vty_end_of_line (vty);
1482 break;
1483 case CONTROL('F'):
1484 vty_forward_char (vty);
1485 break;
1486 case CONTROL('H'):
1487 case 0x7f:
1488 vty_delete_backward_char (vty);
1489 break;
1490 case CONTROL('K'):
1491 vty_kill_line (vty);
1492 break;
1493 case CONTROL('N'):
1494 vty_next_line (vty);
1495 break;
1496 case CONTROL('P'):
1497 vty_previous_line (vty);
1498 break;
1499 case CONTROL('T'):
1500 vty_transpose_chars (vty);
1501 break;
1502 case CONTROL('U'):
1503 vty_kill_line_from_beginning (vty);
1504 break;
1505 case CONTROL('W'):
1506 vty_backward_kill_word (vty);
1507 break;
1508 case CONTROL('Z'):
1509 vty_end_config (vty);
1510 break;
1511 case '\n':
1512 case '\r':
1513 vty_out (vty, "%s", VTY_NEWLINE);
1514 vty_execute (vty);
1515 break;
1516 case '\t':
1517 vty_complete_command (vty);
1518 break;
1519 case '?':
1520 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1521 vty_self_insert (vty, buf[i]);
1522 else
1523 vty_describe_command (vty);
1524 break;
1525 case '\033':
1526 if (i + 1 < nbytes && buf[i + 1] == '[')
1527 {
1528 vty->escape = VTY_ESCAPE;
1529 i++;
1530 }
1531 else
1532 vty->escape = VTY_PRE_ESCAPE;
1533 break;
1534 default:
1535 if (buf[i] > 31 && buf[i] < 127)
1536 vty_self_insert (vty, buf[i]);
1537 break;
1538 }
1539 }
1540
1541 /* Check status. */
1542 if (vty->status == VTY_CLOSE)
1543 vty_close (vty);
1544 else
1545 {
David Lamparter4715a532013-05-30 16:31:49 +02001546 vty_event (VTY_WRITE, vty->wfd, vty);
paul718e3742002-12-13 20:15:29 +00001547 vty_event (VTY_READ, vty_sock, vty);
1548 }
1549 return 0;
1550}
1551
1552/* Flush buffer to the vty. */
1553static int
1554vty_flush (struct thread *thread)
1555{
1556 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001557 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001558 int vty_sock = THREAD_FD (thread);
1559 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001560
paul718e3742002-12-13 20:15:29 +00001561 vty->t_write = NULL;
1562
1563 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001564 if ((vty->lines == 0) && vty->t_read)
1565 {
1566 thread_cancel (vty->t_read);
1567 vty->t_read = NULL;
1568 }
paul718e3742002-12-13 20:15:29 +00001569
1570 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001571 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001572
ajs9fc7ebf2005-02-23 15:12:34 +00001573 /* N.B. if width is 0, that means we don't know the window size. */
1574 if ((vty->lines == 0) || (vty->width == 0))
David Lamparter4715a532013-05-30 16:31:49 +02001575 flushrc = buffer_flush_available(vty->obuf, vty_sock);
ajs9fc7ebf2005-02-23 15:12:34 +00001576 else if (vty->status == VTY_MORELINE)
David Lamparter4715a532013-05-30 16:31:49 +02001577 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001578 1, erase, 0);
1579 else
David Lamparter4715a532013-05-30 16:31:49 +02001580 flushrc = buffer_flush_window(vty->obuf, vty_sock, vty->width,
ajs9fc7ebf2005-02-23 15:12:34 +00001581 vty->lines >= 0 ? vty->lines :
1582 vty->height,
1583 erase, 0);
1584 switch (flushrc)
1585 {
1586 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001587 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001588 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1589 vty->fd);
1590 buffer_reset(vty->obuf);
1591 vty_close(vty);
1592 return 0;
1593 case BUFFER_EMPTY:
1594 if (vty->status == VTY_CLOSE)
1595 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001596 else
1597 {
ajs9fc7ebf2005-02-23 15:12:34 +00001598 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001599 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001600 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001601 }
ajs9fc7ebf2005-02-23 15:12:34 +00001602 break;
1603 case BUFFER_PENDING:
1604 /* There is more data waiting to be written. */
1605 vty->status = VTY_MORE;
1606 if (vty->lines == 0)
1607 vty_event (VTY_WRITE, vty_sock, vty);
1608 break;
1609 }
paul718e3742002-12-13 20:15:29 +00001610
1611 return 0;
1612}
1613
David Lamparterba5dc5e2013-05-30 16:33:45 +02001614/* allocate and initialise vty */
1615static struct vty *
1616vty_new_init (int vty_sock)
1617{
1618 struct vty *vty;
1619
1620 vty = vty_new ();
1621 vty->fd = vty_sock;
1622 vty->wfd = vty_sock;
1623 vty->type = VTY_TERM;
1624 vty->node = AUTH_NODE;
1625 vty->fail = 0;
1626 vty->cp = 0;
1627 vty_clear_buf (vty);
1628 vty->length = 0;
1629 memset (vty->hist, 0, sizeof (vty->hist));
1630 vty->hp = 0;
1631 vty->hindex = 0;
1632 vector_set_index (vtyvec, vty_sock, vty);
1633 vty->status = VTY_NORMAL;
1634 vty->lines = -1;
1635 vty->iac = 0;
1636 vty->iac_sb_in_progress = 0;
1637 vty->sb_len = 0;
1638
1639 return vty;
1640}
1641
paul718e3742002-12-13 20:15:29 +00001642/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001643static struct vty *
paul718e3742002-12-13 20:15:29 +00001644vty_create (int vty_sock, union sockunion *su)
1645{
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001646 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001647 struct vty *vty;
1648
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001649 sockunion2str(su, buf, SU_ADDRSTRLEN);
1650
paul718e3742002-12-13 20:15:29 +00001651 /* Allocate new vty structure and set up default values. */
David Lamparterba5dc5e2013-05-30 16:33:45 +02001652 vty = vty_new_init (vty_sock);
1653
1654 /* configurable parameters not part of basic init */
1655 vty->v_timeout = vty_timeout_val;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001656 strcpy (vty->address, buf);
paul718e3742002-12-13 20:15:29 +00001657 if (no_password_check)
1658 {
Paul Jakma62687ff2008-08-23 14:27:06 +01001659 if (restricted_mode)
1660 vty->node = RESTRICTED_NODE;
1661 else if (host.advanced)
paul718e3742002-12-13 20:15:29 +00001662 vty->node = ENABLE_NODE;
1663 else
1664 vty->node = VIEW_NODE;
1665 }
paul718e3742002-12-13 20:15:29 +00001666 if (host.lines >= 0)
1667 vty->lines = host.lines;
paul718e3742002-12-13 20:15:29 +00001668
1669 if (! no_password_check)
1670 {
1671 /* Vty is not available if password isn't set. */
1672 if (host.password == NULL && host.password_encrypt == NULL)
1673 {
1674 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1675 vty->status = VTY_CLOSE;
1676 vty_close (vty);
1677 return NULL;
1678 }
1679 }
1680
1681 /* Say hello to the world. */
1682 vty_hello (vty);
1683 if (! no_password_check)
1684 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1685
1686 /* Setting up terminal. */
1687 vty_will_echo (vty);
1688 vty_will_suppress_go_ahead (vty);
1689
1690 vty_dont_linemode (vty);
1691 vty_do_window_size (vty);
1692 /* vty_dont_lflow_ahead (vty); */
1693
1694 vty_prompt (vty);
1695
1696 /* Add read/write thread. */
1697 vty_event (VTY_WRITE, vty_sock, vty);
1698 vty_event (VTY_READ, vty_sock, vty);
1699
1700 return vty;
1701}
1702
David Lamparterba5dc5e2013-05-30 16:33:45 +02001703/* create vty for stdio */
David Lamparterba53a8f2015-05-05 11:04:46 +02001704static struct termios stdio_orig_termios;
1705static struct vty *stdio_vty = NULL;
David Lamparter464ccf32015-05-12 21:56:18 +02001706static void (*stdio_vty_atclose)(void);
David Lamparterba53a8f2015-05-05 11:04:46 +02001707
1708static void
1709vty_stdio_reset (void)
1710{
1711 if (stdio_vty)
1712 {
1713 tcsetattr (0, TCSANOW, &stdio_orig_termios);
1714 stdio_vty = NULL;
David Lamparter464ccf32015-05-12 21:56:18 +02001715
1716 if (stdio_vty_atclose)
1717 stdio_vty_atclose ();
1718 stdio_vty_atclose = NULL;
David Lamparterba53a8f2015-05-05 11:04:46 +02001719 }
1720}
1721
David Lamparterba5dc5e2013-05-30 16:33:45 +02001722struct vty *
David Lamparter464ccf32015-05-12 21:56:18 +02001723vty_stdio (void (*atclose)())
David Lamparterba5dc5e2013-05-30 16:33:45 +02001724{
1725 struct vty *vty;
David Lamparterba53a8f2015-05-05 11:04:46 +02001726 struct termios termios;
David Lamparterba5dc5e2013-05-30 16:33:45 +02001727
David Lamparterba53a8f2015-05-05 11:04:46 +02001728 /* refuse creating two vtys on stdio */
1729 if (stdio_vty)
1730 return NULL;
1731
1732 vty = stdio_vty = vty_new_init (0);
David Lamparter464ccf32015-05-12 21:56:18 +02001733 stdio_vty_atclose = atclose;
David Lamparterba5dc5e2013-05-30 16:33:45 +02001734 vty->wfd = 1;
1735
1736 /* always have stdio vty in a known _unchangeable_ state, don't want config
1737 * to have any effect here to make sure scripting this works as intended */
1738 vty->node = ENABLE_NODE;
1739 vty->v_timeout = 0;
1740 strcpy (vty->address, "console");
1741
David Lamparterba53a8f2015-05-05 11:04:46 +02001742 if (!tcgetattr (0, &stdio_orig_termios))
1743 {
1744 termios = stdio_orig_termios;
1745 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
1746 | INLCR | IGNCR | ICRNL | IXON);
1747 termios.c_oflag &= ~OPOST;
1748 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
1749 termios.c_cflag &= ~(CSIZE | PARENB);
1750 termios.c_cflag |= CS8;
1751 tcsetattr (0, TCSANOW, &termios);
1752 }
1753
David Lamparterba5dc5e2013-05-30 16:33:45 +02001754 vty_prompt (vty);
1755
1756 /* Add read/write thread. */
1757 vty_event (VTY_WRITE, 1, vty);
1758 vty_event (VTY_READ, 0, vty);
1759
1760 return vty;
1761}
1762
paul718e3742002-12-13 20:15:29 +00001763/* Accept connection from the network. */
1764static int
1765vty_accept (struct thread *thread)
1766{
1767 int vty_sock;
paul718e3742002-12-13 20:15:29 +00001768 union sockunion su;
1769 int ret;
1770 unsigned int on;
1771 int accept_sock;
Timo Teräsc1c69e42015-05-22 13:40:57 +03001772 struct prefix p;
paul718e3742002-12-13 20:15:29 +00001773 struct access_list *acl = NULL;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001774 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001775
1776 accept_sock = THREAD_FD (thread);
1777
1778 /* We continue hearing vty socket. */
1779 vty_event (VTY_SERV, accept_sock, NULL);
1780
1781 memset (&su, 0, sizeof (union sockunion));
1782
1783 /* We can handle IPv4 or IPv6 socket. */
1784 vty_sock = sockunion_accept (accept_sock, &su);
1785 if (vty_sock < 0)
1786 {
ajs6099b3b2004-11-20 02:06:59 +00001787 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001788 return -1;
1789 }
ajs9fc7ebf2005-02-23 15:12:34 +00001790 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001791
Timo Teräsc1c69e42015-05-22 13:40:57 +03001792 sockunion2hostprefix (&su, &p);
paul718e3742002-12-13 20:15:29 +00001793
1794 /* VTY's accesslist apply. */
Timo Teräsc1c69e42015-05-22 13:40:57 +03001795 if (p.family == AF_INET && vty_accesslist_name)
paul718e3742002-12-13 20:15:29 +00001796 {
1797 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
Timo Teräsc1c69e42015-05-22 13:40:57 +03001798 (access_list_apply (acl, &p) == FILTER_DENY))
paul718e3742002-12-13 20:15:29 +00001799 {
paul718e3742002-12-13 20:15:29 +00001800 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001801 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001802 close (vty_sock);
1803
1804 /* continue accepting connections */
1805 vty_event (VTY_SERV, accept_sock, NULL);
1806
paul718e3742002-12-13 20:15:29 +00001807 return 0;
1808 }
1809 }
1810
1811#ifdef HAVE_IPV6
1812 /* VTY's ipv6 accesslist apply. */
Timo Teräsc1c69e42015-05-22 13:40:57 +03001813 if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
paul718e3742002-12-13 20:15:29 +00001814 {
1815 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
Timo Teräsc1c69e42015-05-22 13:40:57 +03001816 (access_list_apply (acl, &p) == FILTER_DENY))
paul718e3742002-12-13 20:15:29 +00001817 {
paul718e3742002-12-13 20:15:29 +00001818 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001819 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001820 close (vty_sock);
1821
1822 /* continue accepting connections */
1823 vty_event (VTY_SERV, accept_sock, NULL);
1824
paul718e3742002-12-13 20:15:29 +00001825 return 0;
1826 }
1827 }
1828#endif /* HAVE_IPV6 */
1829
paul718e3742002-12-13 20:15:29 +00001830 on = 1;
1831 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1832 (char *) &on, sizeof (on));
1833 if (ret < 0)
1834 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001835 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001836
heasley78e6cd92009-12-07 16:41:14 +03001837 zlog (NULL, LOG_INFO, "Vty connection from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001838 sockunion2str (&su, buf, SU_ADDRSTRLEN));
heasley78e6cd92009-12-07 16:41:14 +03001839
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001840 vty_create (vty_sock, &su);
paul718e3742002-12-13 20:15:29 +00001841
1842 return 0;
1843}
1844
David Lamparter6d6df302014-06-28 21:12:37 +02001845#ifdef HAVE_IPV6
ajs9fc7ebf2005-02-23 15:12:34 +00001846static void
paul718e3742002-12-13 20:15:29 +00001847vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1848{
1849 int ret;
1850 struct addrinfo req;
1851 struct addrinfo *ainfo;
1852 struct addrinfo *ainfo_save;
1853 int sock;
1854 char port_str[BUFSIZ];
1855
1856 memset (&req, 0, sizeof (struct addrinfo));
1857 req.ai_flags = AI_PASSIVE;
1858 req.ai_family = AF_UNSPEC;
1859 req.ai_socktype = SOCK_STREAM;
1860 sprintf (port_str, "%d", port);
1861 port_str[sizeof (port_str) - 1] = '\0';
1862
1863 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1864
1865 if (ret != 0)
1866 {
1867 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1868 exit (1);
1869 }
1870
1871 ainfo_save = ainfo;
1872
1873 do
1874 {
1875 if (ainfo->ai_family != AF_INET
1876#ifdef HAVE_IPV6
1877 && ainfo->ai_family != AF_INET6
1878#endif /* HAVE_IPV6 */
1879 )
1880 continue;
1881
1882 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1883 if (sock < 0)
1884 continue;
1885
David Lamparterca051262009-10-04 16:21:49 +02001886 sockopt_v6only (ainfo->ai_family, sock);
paul718e3742002-12-13 20:15:29 +00001887 sockopt_reuseaddr (sock);
1888 sockopt_reuseport (sock);
1889
1890 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1891 if (ret < 0)
1892 {
1893 close (sock); /* Avoid sd leak. */
1894 continue;
1895 }
1896
1897 ret = listen (sock, 3);
1898 if (ret < 0)
1899 {
1900 close (sock); /* Avoid sd leak. */
1901 continue;
1902 }
1903
1904 vty_event (VTY_SERV, sock, NULL);
1905 }
1906 while ((ainfo = ainfo->ai_next) != NULL);
1907
1908 freeaddrinfo (ainfo_save);
1909}
David Lamparter6d6df302014-06-28 21:12:37 +02001910#else /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001911
1912/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001913static void
paul29db05b2003-05-08 20:10:22 +00001914vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001915{
1916 int ret;
1917 union sockunion su;
1918 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001919 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001920
1921 memset (&su, 0, sizeof (union sockunion));
1922 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001923 if(addr)
1924 switch(family)
1925 {
1926 case AF_INET:
1927 naddr=&su.sin.sin_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001928 break;
paul29db05b2003-05-08 20:10:22 +00001929#ifdef HAVE_IPV6
1930 case AF_INET6:
1931 naddr=&su.sin6.sin6_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001932 break;
paul29db05b2003-05-08 20:10:22 +00001933#endif
1934 }
1935
1936 if(naddr)
1937 switch(inet_pton(family,addr,naddr))
1938 {
1939 case -1:
1940 zlog_err("bad address %s",addr);
1941 naddr=NULL;
1942 break;
1943 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001944 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001945 naddr=NULL;
1946 }
paul718e3742002-12-13 20:15:29 +00001947
1948 /* Make new socket. */
1949 accept_sock = sockunion_stream_socket (&su);
1950 if (accept_sock < 0)
1951 return;
1952
1953 /* This is server, so reuse address. */
1954 sockopt_reuseaddr (accept_sock);
1955 sockopt_reuseport (accept_sock);
1956
1957 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001958 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001959 if (ret < 0)
1960 {
paul29db05b2003-05-08 20:10:22 +00001961 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001962 close (accept_sock); /* Avoid sd leak. */
1963 return;
1964 }
1965
1966 /* Listen socket under queue 3. */
1967 ret = listen (accept_sock, 3);
1968 if (ret < 0)
1969 {
1970 zlog (NULL, LOG_WARNING, "can't listen socket");
1971 close (accept_sock); /* Avoid sd leak. */
1972 return;
1973 }
1974
1975 /* Add vty server event. */
1976 vty_event (VTY_SERV, accept_sock, NULL);
1977}
David Lamparter6d6df302014-06-28 21:12:37 +02001978#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001979
1980#ifdef VTYSH
1981/* For sockaddr_un. */
1982#include <sys/un.h>
1983
1984/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001985static void
hasso6ad96ea2004-10-07 19:33:46 +00001986vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001987{
1988 int ret;
paul75e15fe2004-10-31 02:13:09 +00001989 int sock, len;
paul718e3742002-12-13 20:15:29 +00001990 struct sockaddr_un serv;
1991 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001992 struct zprivs_ids_t ids;
1993
paul718e3742002-12-13 20:15:29 +00001994 /* First of all, unlink existing socket */
1995 unlink (path);
1996
1997 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001998 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001999
2000 /* Make UNIX domain socket. */
2001 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2002 if (sock < 0)
2003 {
ajs6a52d0d2005-01-30 18:49:28 +00002004 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002005 return;
2006 }
2007
2008 /* Make server socket. */
2009 memset (&serv, 0, sizeof (struct sockaddr_un));
2010 serv.sun_family = AF_UNIX;
2011 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002012#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00002013 len = serv.sun_len = SUN_LEN(&serv);
2014#else
2015 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002016#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00002017
2018 ret = bind (sock, (struct sockaddr *) &serv, len);
2019 if (ret < 0)
2020 {
ajs6a52d0d2005-01-30 18:49:28 +00002021 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002022 close (sock); /* Avoid sd leak. */
2023 return;
2024 }
2025
2026 ret = listen (sock, 5);
2027 if (ret < 0)
2028 {
ajs6a52d0d2005-01-30 18:49:28 +00002029 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002030 close (sock); /* Avoid sd leak. */
2031 return;
2032 }
2033
2034 umask (old_mask);
2035
pauledd7c242003-06-04 13:59:38 +00002036 zprivs_get_ids(&ids);
2037
2038 if (ids.gid_vty > 0)
2039 {
2040 /* set group of socket */
2041 if ( chown (path, -1, ids.gid_vty) )
2042 {
2043 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00002044 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00002045 }
2046 }
2047
paul718e3742002-12-13 20:15:29 +00002048 vty_event (VTYSH_SERV, sock, NULL);
2049}
2050
2051/* #define VTYSH_DEBUG 1 */
2052
2053static int
2054vtysh_accept (struct thread *thread)
2055{
2056 int accept_sock;
2057 int sock;
2058 int client_len;
2059 struct sockaddr_un client;
2060 struct vty *vty;
2061
2062 accept_sock = THREAD_FD (thread);
2063
2064 vty_event (VTYSH_SERV, accept_sock, NULL);
2065
2066 memset (&client, 0, sizeof (struct sockaddr_un));
2067 client_len = sizeof (struct sockaddr_un);
2068
hassoe473b032004-09-26 16:08:11 +00002069 sock = accept (accept_sock, (struct sockaddr *) &client,
2070 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00002071
2072 if (sock < 0)
2073 {
ajs6099b3b2004-11-20 02:06:59 +00002074 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002075 return -1;
2076 }
2077
ajs9fc7ebf2005-02-23 15:12:34 +00002078 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00002079 {
ajs9fc7ebf2005-02-23 15:12:34 +00002080 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2081 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002082 close (sock);
2083 return -1;
2084 }
pauldccfb192004-10-29 08:29:36 +00002085
paul718e3742002-12-13 20:15:29 +00002086#ifdef VTYSH_DEBUG
2087 printf ("VTY shell accept\n");
2088#endif /* VTYSH_DEBUG */
2089
2090 vty = vty_new ();
2091 vty->fd = sock;
David Lamparter4715a532013-05-30 16:31:49 +02002092 vty->wfd = sock;
paul718e3742002-12-13 20:15:29 +00002093 vty->type = VTY_SHELL_SERV;
2094 vty->node = VIEW_NODE;
2095
2096 vty_event (VTYSH_READ, sock, vty);
2097
2098 return 0;
2099}
2100
2101static int
ajs9fc7ebf2005-02-23 15:12:34 +00002102vtysh_flush(struct vty *vty)
2103{
David Lamparter4715a532013-05-30 16:31:49 +02002104 switch (buffer_flush_available(vty->obuf, vty->wfd))
ajs9fc7ebf2005-02-23 15:12:34 +00002105 {
2106 case BUFFER_PENDING:
David Lamparter4715a532013-05-30 16:31:49 +02002107 vty_event(VTYSH_WRITE, vty->wfd, vty);
ajs9fc7ebf2005-02-23 15:12:34 +00002108 break;
2109 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002110 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002111 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2112 buffer_reset(vty->obuf);
2113 vty_close(vty);
2114 return -1;
2115 break;
2116 case BUFFER_EMPTY:
2117 break;
2118 }
2119 return 0;
2120}
2121
2122static int
paul718e3742002-12-13 20:15:29 +00002123vtysh_read (struct thread *thread)
2124{
2125 int ret;
2126 int sock;
2127 int nbytes;
2128 struct vty *vty;
2129 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002130 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002131 u_char header[4] = {0, 0, 0, 0};
2132
2133 sock = THREAD_FD (thread);
2134 vty = THREAD_ARG (thread);
2135 vty->t_read = NULL;
2136
ajs9fc7ebf2005-02-23 15:12:34 +00002137 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002138 {
ajs9fc7ebf2005-02-23 15:12:34 +00002139 if (nbytes < 0)
2140 {
2141 if (ERRNO_IO_RETRY(errno))
2142 {
2143 vty_event (VTYSH_READ, sock, vty);
2144 return 0;
2145 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002146 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002147 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2148 __func__, sock, safe_strerror(errno));
2149 }
2150 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002151 vty_close (vty);
2152#ifdef VTYSH_DEBUG
2153 printf ("close vtysh\n");
2154#endif /* VTYSH_DEBUG */
2155 return 0;
2156 }
2157
2158#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002159 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002160#endif /* VTYSH_DEBUG */
2161
ajs9fc7ebf2005-02-23 15:12:34 +00002162 for (p = buf; p < buf+nbytes; p++)
2163 {
2164 vty_ensure(vty, vty->length+1);
2165 vty->buf[vty->length++] = *p;
2166 if (*p == '\0')
2167 {
2168 /* Pass this line to parser. */
2169 ret = vty_execute (vty);
2170 /* Note that vty_execute clears the command buffer and resets
2171 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002172
ajs9fc7ebf2005-02-23 15:12:34 +00002173 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002174#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002175 printf ("result: %d\n", ret);
2176 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002177#endif /* VTYSH_DEBUG */
2178
ajs9fc7ebf2005-02-23 15:12:34 +00002179 header[3] = ret;
2180 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002181
ajs9fc7ebf2005-02-23 15:12:34 +00002182 if (!vty->t_write && (vtysh_flush(vty) < 0))
2183 /* Try to flush results; exit if a write error occurs. */
2184 return 0;
2185 }
2186 }
2187
paul718e3742002-12-13 20:15:29 +00002188 vty_event (VTYSH_READ, sock, vty);
2189
2190 return 0;
2191}
ajs49ff6d92004-11-04 19:26:16 +00002192
2193static int
2194vtysh_write (struct thread *thread)
2195{
2196 struct vty *vty = THREAD_ARG (thread);
2197
2198 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002199 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002200 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002201}
2202
paul718e3742002-12-13 20:15:29 +00002203#endif /* VTYSH */
2204
2205/* Determine address family to bind. */
2206void
hasso6ad96ea2004-10-07 19:33:46 +00002207vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002208{
2209 /* If port is set to 0, do not listen on TCP/IP at all! */
2210 if (port)
2211 {
2212
2213#ifdef HAVE_IPV6
paul29db05b2003-05-08 20:10:22 +00002214 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002215#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002216 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002217#endif /* HAVE_IPV6 */
2218 }
2219
2220#ifdef VTYSH
2221 vty_serv_un (path);
2222#endif /* VTYSH */
2223}
2224
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002225/* Close vty interface. Warning: call this only from functions that
2226 will be careful not to access the vty afterwards (since it has
2227 now been freed). This is safest from top-level functions (called
2228 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002229void
2230vty_close (struct vty *vty)
2231{
2232 int i;
2233
2234 /* Cancel threads.*/
2235 if (vty->t_read)
2236 thread_cancel (vty->t_read);
2237 if (vty->t_write)
2238 thread_cancel (vty->t_write);
2239 if (vty->t_timeout)
2240 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002241
2242 /* Flush buffer. */
David Lamparter4715a532013-05-30 16:31:49 +02002243 buffer_flush_all (vty->obuf, vty->wfd);
paul718e3742002-12-13 20:15:29 +00002244
2245 /* Free input buffer. */
2246 buffer_free (vty->obuf);
2247
paul718e3742002-12-13 20:15:29 +00002248 /* Free command history. */
2249 for (i = 0; i < VTY_MAXHIST; i++)
2250 if (vty->hist[i])
2251 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2252
2253 /* Unset vector. */
2254 vector_unset (vtyvec, vty->fd);
2255
2256 /* Close socket. */
2257 if (vty->fd > 0)
2258 close (vty->fd);
David Lamparterba53a8f2015-05-05 11:04:46 +02002259 else
2260 vty_stdio_reset ();
paul718e3742002-12-13 20:15:29 +00002261
paul718e3742002-12-13 20:15:29 +00002262 if (vty->buf)
2263 XFREE (MTYPE_VTY, vty->buf);
2264
2265 /* Check configure. */
2266 vty_config_unlock (vty);
2267
2268 /* OK free vty. */
2269 XFREE (MTYPE_VTY, vty);
2270}
2271
2272/* When time out occur output message then close connection. */
2273static int
2274vty_timeout (struct thread *thread)
2275{
2276 struct vty *vty;
2277
2278 vty = THREAD_ARG (thread);
2279 vty->t_timeout = NULL;
2280 vty->v_timeout = 0;
2281
2282 /* Clear buffer*/
2283 buffer_reset (vty->obuf);
2284 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2285
2286 /* Close connection. */
2287 vty->status = VTY_CLOSE;
2288 vty_close (vty);
2289
2290 return 0;
2291}
2292
2293/* Read up configuration file from file_name. */
2294static void
2295vty_read_file (FILE *confp)
2296{
2297 int ret;
2298 struct vty *vty;
Steve Hillea555002009-07-28 16:36:14 -04002299 unsigned int line_num = 0;
paul718e3742002-12-13 20:15:29 +00002300
2301 vty = vty_new ();
David Lamparter4715a532013-05-30 16:31:49 +02002302 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2303 if (vty->wfd < 0)
Steve Hillea555002009-07-28 16:36:14 -04002304 {
2305 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
David Lamparter4715a532013-05-30 16:31:49 +02002306 vty->wfd = STDOUT_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002307 }
David Lamparter4715a532013-05-30 16:31:49 +02002308 vty->fd = STDIN_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002309 vty->type = VTY_FILE;
paul718e3742002-12-13 20:15:29 +00002310 vty->node = CONFIG_NODE;
2311
2312 /* Execute configuration file */
Steve Hillea555002009-07-28 16:36:14 -04002313 ret = config_from_file (vty, confp, &line_num);
2314
2315 /* Flush any previous errors before printing messages below */
2316 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002317
paul7021c422003-07-15 12:52:22 +00002318 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002319 {
2320 switch (ret)
paul7021c422003-07-15 12:52:22 +00002321 {
2322 case CMD_ERR_AMBIGUOUS:
Steve Hillea555002009-07-28 16:36:14 -04002323 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
paul7021c422003-07-15 12:52:22 +00002324 break;
2325 case CMD_ERR_NO_MATCH:
Steve Hillea555002009-07-28 16:36:14 -04002326 fprintf (stderr, "*** Error reading config: There is no such command.\n");
paul7021c422003-07-15 12:52:22 +00002327 break;
2328 }
Steve Hillea555002009-07-28 16:36:14 -04002329 fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
2330 line_num, vty->buf);
paul718e3742002-12-13 20:15:29 +00002331 vty_close (vty);
2332 exit (1);
2333 }
2334
2335 vty_close (vty);
2336}
2337
ajs9fc7ebf2005-02-23 15:12:34 +00002338static FILE *
paul718e3742002-12-13 20:15:29 +00002339vty_use_backup_config (char *fullpath)
2340{
2341 char *fullpath_sav, *fullpath_tmp;
2342 FILE *ret = NULL;
2343 struct stat buf;
2344 int tmp, sav;
2345 int c;
2346 char buffer[512];
2347
2348 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2349 strcpy (fullpath_sav, fullpath);
2350 strcat (fullpath_sav, CONF_BACKUP_EXT);
2351 if (stat (fullpath_sav, &buf) == -1)
2352 {
2353 free (fullpath_sav);
2354 return NULL;
2355 }
2356
2357 fullpath_tmp = malloc (strlen (fullpath) + 8);
2358 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2359
2360 /* Open file to configuration write. */
2361 tmp = mkstemp (fullpath_tmp);
2362 if (tmp < 0)
2363 {
2364 free (fullpath_sav);
2365 free (fullpath_tmp);
2366 return NULL;
2367 }
2368
2369 sav = open (fullpath_sav, O_RDONLY);
2370 if (sav < 0)
2371 {
gdt3dbf9962003-12-22 20:18:18 +00002372 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002373 free (fullpath_sav);
2374 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002375 return NULL;
2376 }
2377
2378 while((c = read (sav, buffer, 512)) > 0)
2379 write (tmp, buffer, c);
2380
2381 close (sav);
2382 close (tmp);
2383
gdtaa593d52003-12-22 20:15:53 +00002384 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2385 {
gdt3dbf9962003-12-22 20:18:18 +00002386 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002387 free (fullpath_sav);
2388 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002389 return NULL;
2390 }
2391
paul718e3742002-12-13 20:15:29 +00002392 if (link (fullpath_tmp, fullpath) == 0)
2393 ret = fopen (fullpath, "r");
2394
2395 unlink (fullpath_tmp);
2396
2397 free (fullpath_sav);
2398 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002399 return ret;
paul718e3742002-12-13 20:15:29 +00002400}
2401
2402/* Read up configuration file from file_name. */
2403void
2404vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002405 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002406{
paulccc92352003-10-22 02:49:38 +00002407 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002408 FILE *confp = NULL;
2409 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002410 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002411
2412 /* If -f flag specified. */
2413 if (config_file != NULL)
2414 {
2415 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002416 {
2417 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002418 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002419 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002420 sprintf (tmp, "%s/%s", cwd, config_file);
2421 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002422 }
paul718e3742002-12-13 20:15:29 +00002423 else
hasso320ec102004-06-20 19:54:37 +00002424 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002425
2426 confp = fopen (fullpath, "r");
2427
2428 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002429 {
paul3d1dc852005-04-05 00:45:23 +00002430 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2431 __func__, fullpath, safe_strerror (errno));
2432
hasso320ec102004-06-20 19:54:37 +00002433 confp = vty_use_backup_config (fullpath);
2434 if (confp)
2435 fprintf (stderr, "WARNING: using backup configuration file!\n");
2436 else
2437 {
2438 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002439 config_file);
hasso320ec102004-06-20 19:54:37 +00002440 exit(1);
2441 }
2442 }
paul718e3742002-12-13 20:15:29 +00002443 }
2444 else
2445 {
paul718e3742002-12-13 20:15:29 +00002446#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002447 int ret;
2448 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002449
hasso320ec102004-06-20 19:54:37 +00002450 /* !!!!PLEASE LEAVE!!!!
2451 * This is NEEDED for use with vtysh -b, or else you can get
2452 * a real configuration food fight with a lot garbage in the
2453 * merged configuration file it creates coming from the per
2454 * daemon configuration files. This also allows the daemons
2455 * to start if there default configuration file is not
2456 * present or ignore them, as needed when using vtysh -b to
2457 * configure the daemons at boot - MAG
2458 */
paul718e3742002-12-13 20:15:29 +00002459
hasso320ec102004-06-20 19:54:37 +00002460 /* Stat for vtysh Zebra.conf, if found startup and wait for
2461 * boot configuration
2462 */
paul718e3742002-12-13 20:15:29 +00002463
hasso320ec102004-06-20 19:54:37 +00002464 if ( strstr(config_default_dir, "vtysh") == NULL)
2465 {
2466 ret = stat (integrate_default, &conf_stat);
2467 if (ret >= 0)
2468 return;
2469 }
paul718e3742002-12-13 20:15:29 +00002470#endif /* VTYSH */
2471
hasso320ec102004-06-20 19:54:37 +00002472 confp = fopen (config_default_dir, "r");
2473 if (confp == NULL)
2474 {
paul3d1dc852005-04-05 00:45:23 +00002475 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2476 __func__, config_default_dir, safe_strerror (errno));
2477
hasso320ec102004-06-20 19:54:37 +00002478 confp = vty_use_backup_config (config_default_dir);
2479 if (confp)
2480 {
2481 fprintf (stderr, "WARNING: using backup configuration file!\n");
2482 fullpath = config_default_dir;
2483 }
2484 else
2485 {
2486 fprintf (stderr, "can't open configuration file [%s]\n",
2487 config_default_dir);
2488 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002489 }
hasso320ec102004-06-20 19:54:37 +00002490 }
paul718e3742002-12-13 20:15:29 +00002491 else
hasso320ec102004-06-20 19:54:37 +00002492 fullpath = config_default_dir;
2493 }
2494
paul718e3742002-12-13 20:15:29 +00002495 vty_read_file (confp);
2496
2497 fclose (confp);
2498
2499 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002500
2501 if (tmp)
2502 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002503}
2504
2505/* Small utility function which output log to the VTY. */
2506void
ajs274a4a42004-12-07 15:39:31 +00002507vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002508 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002509{
hasso8c328f12004-10-05 21:01:23 +00002510 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002511 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002512
2513 if (!vtyvec)
2514 return;
paul718e3742002-12-13 20:15:29 +00002515
paul55468c82005-03-14 20:19:01 +00002516 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002517 if ((vty = vector_slot (vtyvec, i)) != NULL)
2518 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002519 {
2520 va_list ac;
2521 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002522 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002523 va_end(ac);
2524 }
paul718e3742002-12-13 20:15:29 +00002525}
2526
ajs274a4a42004-12-07 15:39:31 +00002527/* Async-signal-safe version of vty_log for fixed strings. */
2528void
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002529vty_log_fixed (char *buf, size_t len)
ajs274a4a42004-12-07 15:39:31 +00002530{
2531 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002532 struct iovec iov[2];
2533
Paul Jakmaa4b30302006-05-28 08:18:38 +00002534 /* vty may not have been initialised */
2535 if (!vtyvec)
2536 return;
2537
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002538 iov[0].iov_base = buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002539 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002540 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002541 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002542
paul55468c82005-03-14 20:19:01 +00002543 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002544 {
2545 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002546 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2547 /* N.B. We don't care about the return code, since process is
2548 most likely just about to die anyway. */
David Lamparter4715a532013-05-30 16:31:49 +02002549 writev(vty->wfd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002550 }
2551}
2552
paul718e3742002-12-13 20:15:29 +00002553int
2554vty_config_lock (struct vty *vty)
2555{
2556 if (vty_config == 0)
2557 {
2558 vty->config = 1;
2559 vty_config = 1;
2560 }
2561 return vty->config;
2562}
2563
2564int
2565vty_config_unlock (struct vty *vty)
2566{
2567 if (vty_config == 1 && vty->config == 1)
2568 {
2569 vty->config = 0;
2570 vty_config = 0;
2571 }
2572 return vty->config;
2573}
David Lamparter6b0655a2014-06-04 06:53:35 +02002574
paul718e3742002-12-13 20:15:29 +00002575/* Master of the threads. */
Donald Sharpeeef0db2015-10-14 08:50:38 -04002576static struct thread_master *vty_master;
paul718e3742002-12-13 20:15:29 +00002577
2578static void
2579vty_event (enum event event, int sock, struct vty *vty)
2580{
2581 struct thread *vty_serv_thread;
2582
2583 switch (event)
2584 {
2585 case VTY_SERV:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002586 vty_serv_thread = thread_add_read (vty_master, vty_accept, vty, sock);
paul718e3742002-12-13 20:15:29 +00002587 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2588 break;
2589#ifdef VTYSH
2590 case VTYSH_SERV:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002591 vty_serv_thread = thread_add_read (vty_master, vtysh_accept, vty, sock);
Christian Franke677bcbb2013-02-27 13:47:23 +00002592 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
paul718e3742002-12-13 20:15:29 +00002593 break;
2594 case VTYSH_READ:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002595 vty->t_read = thread_add_read (vty_master, vtysh_read, vty, sock);
ajs49ff6d92004-11-04 19:26:16 +00002596 break;
2597 case VTYSH_WRITE:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002598 vty->t_write = thread_add_write (vty_master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002599 break;
2600#endif /* VTYSH */
2601 case VTY_READ:
Donald Sharpeeef0db2015-10-14 08:50:38 -04002602 vty->t_read = thread_add_read (vty_master, vty_read, vty, sock);
paul718e3742002-12-13 20:15:29 +00002603
2604 /* Time out treatment. */
2605 if (vty->v_timeout)
2606 {
2607 if (vty->t_timeout)
2608 thread_cancel (vty->t_timeout);
2609 vty->t_timeout =
Donald Sharpeeef0db2015-10-14 08:50:38 -04002610 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
paul718e3742002-12-13 20:15:29 +00002611 }
2612 break;
2613 case VTY_WRITE:
2614 if (! vty->t_write)
Donald Sharpeeef0db2015-10-14 08:50:38 -04002615 vty->t_write = thread_add_write (vty_master, vty_flush, vty, sock);
paul718e3742002-12-13 20:15:29 +00002616 break;
2617 case VTY_TIMEOUT_RESET:
2618 if (vty->t_timeout)
2619 {
2620 thread_cancel (vty->t_timeout);
2621 vty->t_timeout = NULL;
2622 }
2623 if (vty->v_timeout)
2624 {
2625 vty->t_timeout =
Donald Sharpeeef0db2015-10-14 08:50:38 -04002626 thread_add_timer (vty_master, vty_timeout, vty, vty->v_timeout);
paul718e3742002-12-13 20:15:29 +00002627 }
2628 break;
2629 }
2630}
David Lamparter6b0655a2014-06-04 06:53:35 +02002631
paul718e3742002-12-13 20:15:29 +00002632DEFUN (config_who,
2633 config_who_cmd,
2634 "who",
2635 "Display who is on vty\n")
2636{
hasso8c328f12004-10-05 21:01:23 +00002637 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002638 struct vty *v;
2639
paul55468c82005-03-14 20:19:01 +00002640 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002641 if ((v = vector_slot (vtyvec, i)) != NULL)
2642 vty_out (vty, "%svty[%d] connected from %s.%s",
2643 v->config ? "*" : " ",
2644 i, v->address, VTY_NEWLINE);
2645 return CMD_SUCCESS;
2646}
2647
2648/* Move to vty configuration mode. */
2649DEFUN (line_vty,
2650 line_vty_cmd,
2651 "line vty",
2652 "Configure a terminal line\n"
2653 "Virtual terminal\n")
2654{
2655 vty->node = VTY_NODE;
2656 return CMD_SUCCESS;
2657}
2658
2659/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002660static int
paul9035efa2004-10-10 11:56:56 +00002661exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002662{
2663 unsigned long timeout = 0;
2664
2665 /* min_str and sec_str are already checked by parser. So it must be
2666 all digit string. */
2667 if (min_str)
2668 {
2669 timeout = strtol (min_str, NULL, 10);
2670 timeout *= 60;
2671 }
2672 if (sec_str)
2673 timeout += strtol (sec_str, NULL, 10);
2674
2675 vty_timeout_val = timeout;
2676 vty->v_timeout = timeout;
2677 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2678
2679
2680 return CMD_SUCCESS;
2681}
2682
2683DEFUN (exec_timeout_min,
2684 exec_timeout_min_cmd,
2685 "exec-timeout <0-35791>",
2686 "Set timeout value\n"
2687 "Timeout value in minutes\n")
2688{
2689 return exec_timeout (vty, argv[0], NULL);
2690}
2691
2692DEFUN (exec_timeout_sec,
2693 exec_timeout_sec_cmd,
2694 "exec-timeout <0-35791> <0-2147483>",
2695 "Set the EXEC timeout\n"
2696 "Timeout in minutes\n"
2697 "Timeout in seconds\n")
2698{
2699 return exec_timeout (vty, argv[0], argv[1]);
2700}
2701
2702DEFUN (no_exec_timeout,
2703 no_exec_timeout_cmd,
2704 "no exec-timeout",
2705 NO_STR
2706 "Set the EXEC timeout\n")
2707{
2708 return exec_timeout (vty, NULL, NULL);
2709}
2710
2711/* Set vty access class. */
2712DEFUN (vty_access_class,
2713 vty_access_class_cmd,
2714 "access-class WORD",
2715 "Filter connections based on an IP access list\n"
2716 "IP access list\n")
2717{
2718 if (vty_accesslist_name)
2719 XFREE(MTYPE_VTY, vty_accesslist_name);
2720
2721 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2722
2723 return CMD_SUCCESS;
2724}
2725
2726/* Clear vty access class. */
2727DEFUN (no_vty_access_class,
2728 no_vty_access_class_cmd,
2729 "no access-class [WORD]",
2730 NO_STR
2731 "Filter connections based on an IP access list\n"
2732 "IP access list\n")
2733{
2734 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2735 {
2736 vty_out (vty, "Access-class is not currently applied to vty%s",
2737 VTY_NEWLINE);
2738 return CMD_WARNING;
2739 }
2740
2741 XFREE(MTYPE_VTY, vty_accesslist_name);
2742
2743 vty_accesslist_name = NULL;
2744
2745 return CMD_SUCCESS;
2746}
2747
2748#ifdef HAVE_IPV6
2749/* Set vty access class. */
2750DEFUN (vty_ipv6_access_class,
2751 vty_ipv6_access_class_cmd,
2752 "ipv6 access-class WORD",
2753 IPV6_STR
2754 "Filter connections based on an IP access list\n"
2755 "IPv6 access list\n")
2756{
2757 if (vty_ipv6_accesslist_name)
2758 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2759
2760 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2761
2762 return CMD_SUCCESS;
2763}
2764
2765/* Clear vty access class. */
2766DEFUN (no_vty_ipv6_access_class,
2767 no_vty_ipv6_access_class_cmd,
2768 "no ipv6 access-class [WORD]",
2769 NO_STR
2770 IPV6_STR
2771 "Filter connections based on an IP access list\n"
2772 "IPv6 access list\n")
2773{
2774 if (! vty_ipv6_accesslist_name ||
2775 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2776 {
2777 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2778 VTY_NEWLINE);
2779 return CMD_WARNING;
2780 }
2781
2782 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2783
2784 vty_ipv6_accesslist_name = NULL;
2785
2786 return CMD_SUCCESS;
2787}
2788#endif /* HAVE_IPV6 */
2789
2790/* vty login. */
2791DEFUN (vty_login,
2792 vty_login_cmd,
2793 "login",
2794 "Enable password checking\n")
2795{
2796 no_password_check = 0;
2797 return CMD_SUCCESS;
2798}
2799
2800DEFUN (no_vty_login,
2801 no_vty_login_cmd,
2802 "no login",
2803 NO_STR
2804 "Enable password checking\n")
2805{
2806 no_password_check = 1;
2807 return CMD_SUCCESS;
2808}
2809
Paul Jakma62687ff2008-08-23 14:27:06 +01002810/* initial mode. */
2811DEFUN (vty_restricted_mode,
2812 vty_restricted_mode_cmd,
2813 "anonymous restricted",
2814 "Restrict view commands available in anonymous, unauthenticated vty\n")
2815{
2816 restricted_mode = 1;
2817 return CMD_SUCCESS;
2818}
2819
2820DEFUN (vty_no_restricted_mode,
2821 vty_no_restricted_mode_cmd,
2822 "no anonymous restricted",
2823 NO_STR
2824 "Enable password checking\n")
2825{
2826 restricted_mode = 0;
2827 return CMD_SUCCESS;
2828}
2829
paul718e3742002-12-13 20:15:29 +00002830DEFUN (service_advanced_vty,
2831 service_advanced_vty_cmd,
2832 "service advanced-vty",
2833 "Set up miscellaneous service\n"
2834 "Enable advanced mode vty interface\n")
2835{
2836 host.advanced = 1;
2837 return CMD_SUCCESS;
2838}
2839
2840DEFUN (no_service_advanced_vty,
2841 no_service_advanced_vty_cmd,
2842 "no service advanced-vty",
2843 NO_STR
2844 "Set up miscellaneous service\n"
2845 "Enable advanced mode vty interface\n")
2846{
2847 host.advanced = 0;
2848 return CMD_SUCCESS;
2849}
2850
2851DEFUN (terminal_monitor,
2852 terminal_monitor_cmd,
2853 "terminal monitor",
2854 "Set terminal line parameters\n"
2855 "Copy debug output to the current terminal line\n")
2856{
2857 vty->monitor = 1;
2858 return CMD_SUCCESS;
2859}
2860
2861DEFUN (terminal_no_monitor,
2862 terminal_no_monitor_cmd,
2863 "terminal no monitor",
2864 "Set terminal line parameters\n"
2865 NO_STR
2866 "Copy debug output to the current terminal line\n")
2867{
2868 vty->monitor = 0;
2869 return CMD_SUCCESS;
2870}
2871
paul789f78a2006-01-17 17:42:03 +00002872ALIAS (terminal_no_monitor,
2873 no_terminal_monitor_cmd,
2874 "no terminal monitor",
2875 NO_STR
2876 "Set terminal line parameters\n"
2877 "Copy debug output to the current terminal line\n")
2878
paul718e3742002-12-13 20:15:29 +00002879DEFUN (show_history,
2880 show_history_cmd,
2881 "show history",
2882 SHOW_STR
2883 "Display the session command history\n")
2884{
2885 int index;
2886
2887 for (index = vty->hindex + 1; index != vty->hindex;)
2888 {
2889 if (index == VTY_MAXHIST)
2890 {
2891 index = 0;
2892 continue;
2893 }
2894
2895 if (vty->hist[index] != NULL)
2896 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2897
2898 index++;
2899 }
2900
2901 return CMD_SUCCESS;
2902}
2903
2904/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002905static int
paul718e3742002-12-13 20:15:29 +00002906vty_config_write (struct vty *vty)
2907{
2908 vty_out (vty, "line vty%s", VTY_NEWLINE);
2909
2910 if (vty_accesslist_name)
2911 vty_out (vty, " access-class %s%s",
2912 vty_accesslist_name, VTY_NEWLINE);
2913
2914 if (vty_ipv6_accesslist_name)
2915 vty_out (vty, " ipv6 access-class %s%s",
2916 vty_ipv6_accesslist_name, VTY_NEWLINE);
2917
2918 /* exec-timeout */
2919 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2920 vty_out (vty, " exec-timeout %ld %ld%s",
2921 vty_timeout_val / 60,
2922 vty_timeout_val % 60, VTY_NEWLINE);
2923
2924 /* login */
2925 if (no_password_check)
2926 vty_out (vty, " no login%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +01002927
2928 if (restricted_mode != restricted_mode_default)
2929 {
2930 if (restricted_mode_default)
2931 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
2932 else
2933 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
2934 }
2935
paul718e3742002-12-13 20:15:29 +00002936 vty_out (vty, "!%s", VTY_NEWLINE);
2937
2938 return CMD_SUCCESS;
2939}
2940
2941struct cmd_node vty_node =
2942{
2943 VTY_NODE,
2944 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002945 1,
paul718e3742002-12-13 20:15:29 +00002946};
2947
2948/* Reset all VTY status. */
2949void
2950vty_reset ()
2951{
hasso8c328f12004-10-05 21:01:23 +00002952 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002953 struct vty *vty;
2954 struct thread *vty_serv_thread;
2955
paul55468c82005-03-14 20:19:01 +00002956 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002957 if ((vty = vector_slot (vtyvec, i)) != NULL)
2958 {
2959 buffer_reset (vty->obuf);
2960 vty->status = VTY_CLOSE;
2961 vty_close (vty);
2962 }
2963
paul55468c82005-03-14 20:19:01 +00002964 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002965 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2966 {
2967 thread_cancel (vty_serv_thread);
2968 vector_slot (Vvty_serv_thread, i) = NULL;
2969 close (i);
2970 }
2971
2972 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2973
2974 if (vty_accesslist_name)
2975 {
2976 XFREE(MTYPE_VTY, vty_accesslist_name);
2977 vty_accesslist_name = NULL;
2978 }
2979
2980 if (vty_ipv6_accesslist_name)
2981 {
2982 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2983 vty_ipv6_accesslist_name = NULL;
2984 }
2985}
2986
ajs9fc7ebf2005-02-23 15:12:34 +00002987static void
2988vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002989{
paul79ad2792003-10-15 22:09:28 +00002990 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002991 char *c;
paul718e3742002-12-13 20:15:29 +00002992
paulccc92352003-10-22 02:49:38 +00002993 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002994
paulccc92352003-10-22 02:49:38 +00002995 if (!c)
paul79ad2792003-10-15 22:09:28 +00002996 {
2997 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002998 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002999 }
paul718e3742002-12-13 20:15:29 +00003000
3001 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3002 strcpy (vty_cwd, cwd);
3003}
3004
3005char *
3006vty_get_cwd ()
3007{
3008 return vty_cwd;
3009}
3010
3011int
3012vty_shell (struct vty *vty)
3013{
3014 return vty->type == VTY_SHELL ? 1 : 0;
3015}
3016
3017int
3018vty_shell_serv (struct vty *vty)
3019{
3020 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3021}
3022
3023void
3024vty_init_vtysh ()
3025{
3026 vtyvec = vector_init (VECTOR_MIN_SIZE);
3027}
3028
3029/* Install vty's own commands like `who' command. */
3030void
paulb21b19c2003-06-15 01:28:29 +00003031vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00003032{
3033 /* For further configuration read, preserve current directory. */
3034 vty_save_cwd ();
3035
3036 vtyvec = vector_init (VECTOR_MIN_SIZE);
3037
Donald Sharpeeef0db2015-10-14 08:50:38 -04003038 vty_master = master_thread;
paulb21b19c2003-06-15 01:28:29 +00003039
David Lamparterba53a8f2015-05-05 11:04:46 +02003040 atexit (vty_stdio_reset);
3041
paul718e3742002-12-13 20:15:29 +00003042 /* Initilize server thread vector. */
3043 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3044
3045 /* Install bgp top node. */
3046 install_node (&vty_node, vty_config_write);
3047
Paul Jakma62687ff2008-08-23 14:27:06 +01003048 install_element (RESTRICTED_NODE, &config_who_cmd);
3049 install_element (RESTRICTED_NODE, &show_history_cmd);
paul718e3742002-12-13 20:15:29 +00003050 install_element (VIEW_NODE, &config_who_cmd);
3051 install_element (VIEW_NODE, &show_history_cmd);
3052 install_element (ENABLE_NODE, &config_who_cmd);
3053 install_element (CONFIG_NODE, &line_vty_cmd);
3054 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3055 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3056 install_element (CONFIG_NODE, &show_history_cmd);
3057 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3058 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00003059 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00003060 install_element (ENABLE_NODE, &show_history_cmd);
3061
3062 install_default (VTY_NODE);
3063 install_element (VTY_NODE, &exec_timeout_min_cmd);
3064 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3065 install_element (VTY_NODE, &no_exec_timeout_cmd);
3066 install_element (VTY_NODE, &vty_access_class_cmd);
3067 install_element (VTY_NODE, &no_vty_access_class_cmd);
3068 install_element (VTY_NODE, &vty_login_cmd);
3069 install_element (VTY_NODE, &no_vty_login_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01003070 install_element (VTY_NODE, &vty_restricted_mode_cmd);
3071 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
paul718e3742002-12-13 20:15:29 +00003072#ifdef HAVE_IPV6
3073 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3074 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3075#endif /* HAVE_IPV6 */
3076}
Chris Caputo228da422009-07-18 05:44:03 +00003077
3078void
3079vty_terminate (void)
3080{
3081 if (vty_cwd)
3082 XFREE (MTYPE_TMP, vty_cwd);
3083
3084 if (vtyvec && Vvty_serv_thread)
3085 {
3086 vty_reset ();
3087 vector_free (vtyvec);
3088 vector_free (Vvty_serv_thread);
3089 }
3090}