blob: 3477f638c1e32def663abb2e28ca7d9bb3ea8edd [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
876 matched = cmd_complete_command (vline, vty, &ret);
877
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));
1367 }
1368 buffer_reset(vty->obuf);
1369 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;
1772 struct prefix *p = NULL;
1773 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
1792 p = sockunion2hostprefix (&su);
1793
1794 /* VTY's accesslist apply. */
1795 if (p->family == AF_INET && vty_accesslist_name)
1796 {
1797 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1798 (access_list_apply (acl, p) == FILTER_DENY))
1799 {
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
1807 prefix_free (p);
1808
1809 return 0;
1810 }
1811 }
1812
1813#ifdef HAVE_IPV6
1814 /* VTY's ipv6 accesslist apply. */
1815 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1816 {
1817 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1818 (access_list_apply (acl, p) == FILTER_DENY))
1819 {
paul718e3742002-12-13 20:15:29 +00001820 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001821 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001822 close (vty_sock);
1823
1824 /* continue accepting connections */
1825 vty_event (VTY_SERV, accept_sock, NULL);
1826
1827 prefix_free (p);
1828
1829 return 0;
1830 }
1831 }
1832#endif /* HAVE_IPV6 */
1833
1834 prefix_free (p);
1835
1836 on = 1;
1837 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1838 (char *) &on, sizeof (on));
1839 if (ret < 0)
1840 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001841 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001842
heasley78e6cd92009-12-07 16:41:14 +03001843 zlog (NULL, LOG_INFO, "Vty connection from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001844 sockunion2str (&su, buf, SU_ADDRSTRLEN));
heasley78e6cd92009-12-07 16:41:14 +03001845
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001846 vty_create (vty_sock, &su);
paul718e3742002-12-13 20:15:29 +00001847
1848 return 0;
1849}
1850
David Lamparter6d6df302014-06-28 21:12:37 +02001851#ifdef HAVE_IPV6
ajs9fc7ebf2005-02-23 15:12:34 +00001852static void
paul718e3742002-12-13 20:15:29 +00001853vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1854{
1855 int ret;
1856 struct addrinfo req;
1857 struct addrinfo *ainfo;
1858 struct addrinfo *ainfo_save;
1859 int sock;
1860 char port_str[BUFSIZ];
1861
1862 memset (&req, 0, sizeof (struct addrinfo));
1863 req.ai_flags = AI_PASSIVE;
1864 req.ai_family = AF_UNSPEC;
1865 req.ai_socktype = SOCK_STREAM;
1866 sprintf (port_str, "%d", port);
1867 port_str[sizeof (port_str) - 1] = '\0';
1868
1869 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1870
1871 if (ret != 0)
1872 {
1873 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1874 exit (1);
1875 }
1876
1877 ainfo_save = ainfo;
1878
1879 do
1880 {
1881 if (ainfo->ai_family != AF_INET
1882#ifdef HAVE_IPV6
1883 && ainfo->ai_family != AF_INET6
1884#endif /* HAVE_IPV6 */
1885 )
1886 continue;
1887
1888 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1889 if (sock < 0)
1890 continue;
1891
David Lamparterca051262009-10-04 16:21:49 +02001892 sockopt_v6only (ainfo->ai_family, sock);
paul718e3742002-12-13 20:15:29 +00001893 sockopt_reuseaddr (sock);
1894 sockopt_reuseport (sock);
1895
1896 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1897 if (ret < 0)
1898 {
1899 close (sock); /* Avoid sd leak. */
1900 continue;
1901 }
1902
1903 ret = listen (sock, 3);
1904 if (ret < 0)
1905 {
1906 close (sock); /* Avoid sd leak. */
1907 continue;
1908 }
1909
1910 vty_event (VTY_SERV, sock, NULL);
1911 }
1912 while ((ainfo = ainfo->ai_next) != NULL);
1913
1914 freeaddrinfo (ainfo_save);
1915}
David Lamparter6d6df302014-06-28 21:12:37 +02001916#else /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001917
1918/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001919static void
paul29db05b2003-05-08 20:10:22 +00001920vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001921{
1922 int ret;
1923 union sockunion su;
1924 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001925 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001926
1927 memset (&su, 0, sizeof (union sockunion));
1928 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001929 if(addr)
1930 switch(family)
1931 {
1932 case AF_INET:
1933 naddr=&su.sin.sin_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001934 break;
paul29db05b2003-05-08 20:10:22 +00001935#ifdef HAVE_IPV6
1936 case AF_INET6:
1937 naddr=&su.sin6.sin6_addr;
Remi Gacognea11e0122013-09-08 13:48:34 +00001938 break;
paul29db05b2003-05-08 20:10:22 +00001939#endif
1940 }
1941
1942 if(naddr)
1943 switch(inet_pton(family,addr,naddr))
1944 {
1945 case -1:
1946 zlog_err("bad address %s",addr);
1947 naddr=NULL;
1948 break;
1949 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001950 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001951 naddr=NULL;
1952 }
paul718e3742002-12-13 20:15:29 +00001953
1954 /* Make new socket. */
1955 accept_sock = sockunion_stream_socket (&su);
1956 if (accept_sock < 0)
1957 return;
1958
1959 /* This is server, so reuse address. */
1960 sockopt_reuseaddr (accept_sock);
1961 sockopt_reuseport (accept_sock);
1962
1963 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001964 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001965 if (ret < 0)
1966 {
paul29db05b2003-05-08 20:10:22 +00001967 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001968 close (accept_sock); /* Avoid sd leak. */
1969 return;
1970 }
1971
1972 /* Listen socket under queue 3. */
1973 ret = listen (accept_sock, 3);
1974 if (ret < 0)
1975 {
1976 zlog (NULL, LOG_WARNING, "can't listen socket");
1977 close (accept_sock); /* Avoid sd leak. */
1978 return;
1979 }
1980
1981 /* Add vty server event. */
1982 vty_event (VTY_SERV, accept_sock, NULL);
1983}
David Lamparter6d6df302014-06-28 21:12:37 +02001984#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001985
1986#ifdef VTYSH
1987/* For sockaddr_un. */
1988#include <sys/un.h>
1989
1990/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001991static void
hasso6ad96ea2004-10-07 19:33:46 +00001992vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001993{
1994 int ret;
paul75e15fe2004-10-31 02:13:09 +00001995 int sock, len;
paul718e3742002-12-13 20:15:29 +00001996 struct sockaddr_un serv;
1997 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001998 struct zprivs_ids_t ids;
1999
paul718e3742002-12-13 20:15:29 +00002000 /* First of all, unlink existing socket */
2001 unlink (path);
2002
2003 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00002004 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00002005
2006 /* Make UNIX domain socket. */
2007 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2008 if (sock < 0)
2009 {
ajs6a52d0d2005-01-30 18:49:28 +00002010 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002011 return;
2012 }
2013
2014 /* Make server socket. */
2015 memset (&serv, 0, sizeof (struct sockaddr_un));
2016 serv.sun_family = AF_UNIX;
2017 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002018#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00002019 len = serv.sun_len = SUN_LEN(&serv);
2020#else
2021 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00002022#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00002023
2024 ret = bind (sock, (struct sockaddr *) &serv, len);
2025 if (ret < 0)
2026 {
ajs6a52d0d2005-01-30 18:49:28 +00002027 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002028 close (sock); /* Avoid sd leak. */
2029 return;
2030 }
2031
2032 ret = listen (sock, 5);
2033 if (ret < 0)
2034 {
ajs6a52d0d2005-01-30 18:49:28 +00002035 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00002036 close (sock); /* Avoid sd leak. */
2037 return;
2038 }
2039
2040 umask (old_mask);
2041
pauledd7c242003-06-04 13:59:38 +00002042 zprivs_get_ids(&ids);
2043
2044 if (ids.gid_vty > 0)
2045 {
2046 /* set group of socket */
2047 if ( chown (path, -1, ids.gid_vty) )
2048 {
2049 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00002050 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00002051 }
2052 }
2053
paul718e3742002-12-13 20:15:29 +00002054 vty_event (VTYSH_SERV, sock, NULL);
2055}
2056
2057/* #define VTYSH_DEBUG 1 */
2058
2059static int
2060vtysh_accept (struct thread *thread)
2061{
2062 int accept_sock;
2063 int sock;
2064 int client_len;
2065 struct sockaddr_un client;
2066 struct vty *vty;
2067
2068 accept_sock = THREAD_FD (thread);
2069
2070 vty_event (VTYSH_SERV, accept_sock, NULL);
2071
2072 memset (&client, 0, sizeof (struct sockaddr_un));
2073 client_len = sizeof (struct sockaddr_un);
2074
hassoe473b032004-09-26 16:08:11 +00002075 sock = accept (accept_sock, (struct sockaddr *) &client,
2076 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00002077
2078 if (sock < 0)
2079 {
ajs6099b3b2004-11-20 02:06:59 +00002080 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002081 return -1;
2082 }
2083
ajs9fc7ebf2005-02-23 15:12:34 +00002084 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00002085 {
ajs9fc7ebf2005-02-23 15:12:34 +00002086 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2087 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002088 close (sock);
2089 return -1;
2090 }
pauldccfb192004-10-29 08:29:36 +00002091
paul718e3742002-12-13 20:15:29 +00002092#ifdef VTYSH_DEBUG
2093 printf ("VTY shell accept\n");
2094#endif /* VTYSH_DEBUG */
2095
2096 vty = vty_new ();
2097 vty->fd = sock;
David Lamparter4715a532013-05-30 16:31:49 +02002098 vty->wfd = sock;
paul718e3742002-12-13 20:15:29 +00002099 vty->type = VTY_SHELL_SERV;
2100 vty->node = VIEW_NODE;
2101
2102 vty_event (VTYSH_READ, sock, vty);
2103
2104 return 0;
2105}
2106
2107static int
ajs9fc7ebf2005-02-23 15:12:34 +00002108vtysh_flush(struct vty *vty)
2109{
David Lamparter4715a532013-05-30 16:31:49 +02002110 switch (buffer_flush_available(vty->obuf, vty->wfd))
ajs9fc7ebf2005-02-23 15:12:34 +00002111 {
2112 case BUFFER_PENDING:
David Lamparter4715a532013-05-30 16:31:49 +02002113 vty_event(VTYSH_WRITE, vty->wfd, vty);
ajs9fc7ebf2005-02-23 15:12:34 +00002114 break;
2115 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002116 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002117 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2118 buffer_reset(vty->obuf);
2119 vty_close(vty);
2120 return -1;
2121 break;
2122 case BUFFER_EMPTY:
2123 break;
2124 }
2125 return 0;
2126}
2127
2128static int
paul718e3742002-12-13 20:15:29 +00002129vtysh_read (struct thread *thread)
2130{
2131 int ret;
2132 int sock;
2133 int nbytes;
2134 struct vty *vty;
2135 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002136 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002137 u_char header[4] = {0, 0, 0, 0};
2138
2139 sock = THREAD_FD (thread);
2140 vty = THREAD_ARG (thread);
2141 vty->t_read = NULL;
2142
ajs9fc7ebf2005-02-23 15:12:34 +00002143 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002144 {
ajs9fc7ebf2005-02-23 15:12:34 +00002145 if (nbytes < 0)
2146 {
2147 if (ERRNO_IO_RETRY(errno))
2148 {
2149 vty_event (VTYSH_READ, sock, vty);
2150 return 0;
2151 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002152 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002153 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2154 __func__, sock, safe_strerror(errno));
2155 }
2156 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002157 vty_close (vty);
2158#ifdef VTYSH_DEBUG
2159 printf ("close vtysh\n");
2160#endif /* VTYSH_DEBUG */
2161 return 0;
2162 }
2163
2164#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002165 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002166#endif /* VTYSH_DEBUG */
2167
ajs9fc7ebf2005-02-23 15:12:34 +00002168 for (p = buf; p < buf+nbytes; p++)
2169 {
2170 vty_ensure(vty, vty->length+1);
2171 vty->buf[vty->length++] = *p;
2172 if (*p == '\0')
2173 {
2174 /* Pass this line to parser. */
2175 ret = vty_execute (vty);
2176 /* Note that vty_execute clears the command buffer and resets
2177 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002178
ajs9fc7ebf2005-02-23 15:12:34 +00002179 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002180#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002181 printf ("result: %d\n", ret);
2182 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002183#endif /* VTYSH_DEBUG */
2184
ajs9fc7ebf2005-02-23 15:12:34 +00002185 header[3] = ret;
2186 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002187
ajs9fc7ebf2005-02-23 15:12:34 +00002188 if (!vty->t_write && (vtysh_flush(vty) < 0))
2189 /* Try to flush results; exit if a write error occurs. */
2190 return 0;
2191 }
2192 }
2193
paul718e3742002-12-13 20:15:29 +00002194 vty_event (VTYSH_READ, sock, vty);
2195
2196 return 0;
2197}
ajs49ff6d92004-11-04 19:26:16 +00002198
2199static int
2200vtysh_write (struct thread *thread)
2201{
2202 struct vty *vty = THREAD_ARG (thread);
2203
2204 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002205 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002206 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002207}
2208
paul718e3742002-12-13 20:15:29 +00002209#endif /* VTYSH */
2210
2211/* Determine address family to bind. */
2212void
hasso6ad96ea2004-10-07 19:33:46 +00002213vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002214{
2215 /* If port is set to 0, do not listen on TCP/IP at all! */
2216 if (port)
2217 {
2218
2219#ifdef HAVE_IPV6
paul29db05b2003-05-08 20:10:22 +00002220 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002221#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002222 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002223#endif /* HAVE_IPV6 */
2224 }
2225
2226#ifdef VTYSH
2227 vty_serv_un (path);
2228#endif /* VTYSH */
2229}
2230
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002231/* Close vty interface. Warning: call this only from functions that
2232 will be careful not to access the vty afterwards (since it has
2233 now been freed). This is safest from top-level functions (called
2234 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002235void
2236vty_close (struct vty *vty)
2237{
2238 int i;
2239
2240 /* Cancel threads.*/
2241 if (vty->t_read)
2242 thread_cancel (vty->t_read);
2243 if (vty->t_write)
2244 thread_cancel (vty->t_write);
2245 if (vty->t_timeout)
2246 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002247
2248 /* Flush buffer. */
David Lamparter4715a532013-05-30 16:31:49 +02002249 buffer_flush_all (vty->obuf, vty->wfd);
paul718e3742002-12-13 20:15:29 +00002250
2251 /* Free input buffer. */
2252 buffer_free (vty->obuf);
2253
paul718e3742002-12-13 20:15:29 +00002254 /* Free command history. */
2255 for (i = 0; i < VTY_MAXHIST; i++)
2256 if (vty->hist[i])
2257 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2258
2259 /* Unset vector. */
2260 vector_unset (vtyvec, vty->fd);
2261
2262 /* Close socket. */
2263 if (vty->fd > 0)
2264 close (vty->fd);
David Lamparterba53a8f2015-05-05 11:04:46 +02002265 else
2266 vty_stdio_reset ();
paul718e3742002-12-13 20:15:29 +00002267
paul718e3742002-12-13 20:15:29 +00002268 if (vty->buf)
2269 XFREE (MTYPE_VTY, vty->buf);
2270
2271 /* Check configure. */
2272 vty_config_unlock (vty);
2273
2274 /* OK free vty. */
2275 XFREE (MTYPE_VTY, vty);
2276}
2277
2278/* When time out occur output message then close connection. */
2279static int
2280vty_timeout (struct thread *thread)
2281{
2282 struct vty *vty;
2283
2284 vty = THREAD_ARG (thread);
2285 vty->t_timeout = NULL;
2286 vty->v_timeout = 0;
2287
2288 /* Clear buffer*/
2289 buffer_reset (vty->obuf);
2290 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2291
2292 /* Close connection. */
2293 vty->status = VTY_CLOSE;
2294 vty_close (vty);
2295
2296 return 0;
2297}
2298
2299/* Read up configuration file from file_name. */
2300static void
2301vty_read_file (FILE *confp)
2302{
2303 int ret;
2304 struct vty *vty;
Steve Hillea555002009-07-28 16:36:14 -04002305 unsigned int line_num = 0;
paul718e3742002-12-13 20:15:29 +00002306
2307 vty = vty_new ();
David Lamparter4715a532013-05-30 16:31:49 +02002308 vty->wfd = dup(STDERR_FILENO); /* vty_close() will close this */
2309 if (vty->wfd < 0)
Steve Hillea555002009-07-28 16:36:14 -04002310 {
2311 /* Fine, we couldn't make a new fd. vty_close doesn't close stdout. */
David Lamparter4715a532013-05-30 16:31:49 +02002312 vty->wfd = STDOUT_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002313 }
David Lamparter4715a532013-05-30 16:31:49 +02002314 vty->fd = STDIN_FILENO;
Steve Hillea555002009-07-28 16:36:14 -04002315 vty->type = VTY_FILE;
paul718e3742002-12-13 20:15:29 +00002316 vty->node = CONFIG_NODE;
2317
2318 /* Execute configuration file */
Steve Hillea555002009-07-28 16:36:14 -04002319 ret = config_from_file (vty, confp, &line_num);
2320
2321 /* Flush any previous errors before printing messages below */
2322 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002323
paul7021c422003-07-15 12:52:22 +00002324 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002325 {
2326 switch (ret)
paul7021c422003-07-15 12:52:22 +00002327 {
2328 case CMD_ERR_AMBIGUOUS:
Steve Hillea555002009-07-28 16:36:14 -04002329 fprintf (stderr, "*** Error reading config: Ambiguous command.\n");
paul7021c422003-07-15 12:52:22 +00002330 break;
2331 case CMD_ERR_NO_MATCH:
Steve Hillea555002009-07-28 16:36:14 -04002332 fprintf (stderr, "*** Error reading config: There is no such command.\n");
paul7021c422003-07-15 12:52:22 +00002333 break;
2334 }
Steve Hillea555002009-07-28 16:36:14 -04002335 fprintf (stderr, "*** Error occured processing line %u, below:\n%s\n",
2336 line_num, vty->buf);
paul718e3742002-12-13 20:15:29 +00002337 vty_close (vty);
2338 exit (1);
2339 }
2340
2341 vty_close (vty);
2342}
2343
ajs9fc7ebf2005-02-23 15:12:34 +00002344static FILE *
paul718e3742002-12-13 20:15:29 +00002345vty_use_backup_config (char *fullpath)
2346{
2347 char *fullpath_sav, *fullpath_tmp;
2348 FILE *ret = NULL;
2349 struct stat buf;
2350 int tmp, sav;
2351 int c;
2352 char buffer[512];
2353
2354 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2355 strcpy (fullpath_sav, fullpath);
2356 strcat (fullpath_sav, CONF_BACKUP_EXT);
2357 if (stat (fullpath_sav, &buf) == -1)
2358 {
2359 free (fullpath_sav);
2360 return NULL;
2361 }
2362
2363 fullpath_tmp = malloc (strlen (fullpath) + 8);
2364 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2365
2366 /* Open file to configuration write. */
2367 tmp = mkstemp (fullpath_tmp);
2368 if (tmp < 0)
2369 {
2370 free (fullpath_sav);
2371 free (fullpath_tmp);
2372 return NULL;
2373 }
2374
2375 sav = open (fullpath_sav, O_RDONLY);
2376 if (sav < 0)
2377 {
gdt3dbf9962003-12-22 20:18:18 +00002378 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002379 free (fullpath_sav);
2380 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002381 return NULL;
2382 }
2383
2384 while((c = read (sav, buffer, 512)) > 0)
2385 write (tmp, buffer, c);
2386
2387 close (sav);
2388 close (tmp);
2389
gdtaa593d52003-12-22 20:15:53 +00002390 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2391 {
gdt3dbf9962003-12-22 20:18:18 +00002392 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002393 free (fullpath_sav);
2394 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002395 return NULL;
2396 }
2397
paul718e3742002-12-13 20:15:29 +00002398 if (link (fullpath_tmp, fullpath) == 0)
2399 ret = fopen (fullpath, "r");
2400
2401 unlink (fullpath_tmp);
2402
2403 free (fullpath_sav);
2404 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002405 return ret;
paul718e3742002-12-13 20:15:29 +00002406}
2407
2408/* Read up configuration file from file_name. */
2409void
2410vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002411 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002412{
paulccc92352003-10-22 02:49:38 +00002413 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002414 FILE *confp = NULL;
2415 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002416 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002417
2418 /* If -f flag specified. */
2419 if (config_file != NULL)
2420 {
2421 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002422 {
2423 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002424 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002425 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002426 sprintf (tmp, "%s/%s", cwd, config_file);
2427 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002428 }
paul718e3742002-12-13 20:15:29 +00002429 else
hasso320ec102004-06-20 19:54:37 +00002430 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002431
2432 confp = fopen (fullpath, "r");
2433
2434 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002435 {
paul3d1dc852005-04-05 00:45:23 +00002436 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2437 __func__, fullpath, safe_strerror (errno));
2438
hasso320ec102004-06-20 19:54:37 +00002439 confp = vty_use_backup_config (fullpath);
2440 if (confp)
2441 fprintf (stderr, "WARNING: using backup configuration file!\n");
2442 else
2443 {
2444 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002445 config_file);
hasso320ec102004-06-20 19:54:37 +00002446 exit(1);
2447 }
2448 }
paul718e3742002-12-13 20:15:29 +00002449 }
2450 else
2451 {
paul718e3742002-12-13 20:15:29 +00002452#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002453 int ret;
2454 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002455
hasso320ec102004-06-20 19:54:37 +00002456 /* !!!!PLEASE LEAVE!!!!
2457 * This is NEEDED for use with vtysh -b, or else you can get
2458 * a real configuration food fight with a lot garbage in the
2459 * merged configuration file it creates coming from the per
2460 * daemon configuration files. This also allows the daemons
2461 * to start if there default configuration file is not
2462 * present or ignore them, as needed when using vtysh -b to
2463 * configure the daemons at boot - MAG
2464 */
paul718e3742002-12-13 20:15:29 +00002465
hasso320ec102004-06-20 19:54:37 +00002466 /* Stat for vtysh Zebra.conf, if found startup and wait for
2467 * boot configuration
2468 */
paul718e3742002-12-13 20:15:29 +00002469
hasso320ec102004-06-20 19:54:37 +00002470 if ( strstr(config_default_dir, "vtysh") == NULL)
2471 {
2472 ret = stat (integrate_default, &conf_stat);
2473 if (ret >= 0)
2474 return;
2475 }
paul718e3742002-12-13 20:15:29 +00002476#endif /* VTYSH */
2477
hasso320ec102004-06-20 19:54:37 +00002478 confp = fopen (config_default_dir, "r");
2479 if (confp == NULL)
2480 {
paul3d1dc852005-04-05 00:45:23 +00002481 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2482 __func__, config_default_dir, safe_strerror (errno));
2483
hasso320ec102004-06-20 19:54:37 +00002484 confp = vty_use_backup_config (config_default_dir);
2485 if (confp)
2486 {
2487 fprintf (stderr, "WARNING: using backup configuration file!\n");
2488 fullpath = config_default_dir;
2489 }
2490 else
2491 {
2492 fprintf (stderr, "can't open configuration file [%s]\n",
2493 config_default_dir);
2494 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002495 }
hasso320ec102004-06-20 19:54:37 +00002496 }
paul718e3742002-12-13 20:15:29 +00002497 else
hasso320ec102004-06-20 19:54:37 +00002498 fullpath = config_default_dir;
2499 }
2500
paul718e3742002-12-13 20:15:29 +00002501 vty_read_file (confp);
2502
2503 fclose (confp);
2504
2505 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002506
2507 if (tmp)
2508 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002509}
2510
2511/* Small utility function which output log to the VTY. */
2512void
ajs274a4a42004-12-07 15:39:31 +00002513vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002514 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002515{
hasso8c328f12004-10-05 21:01:23 +00002516 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002517 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002518
2519 if (!vtyvec)
2520 return;
paul718e3742002-12-13 20:15:29 +00002521
paul55468c82005-03-14 20:19:01 +00002522 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002523 if ((vty = vector_slot (vtyvec, i)) != NULL)
2524 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002525 {
2526 va_list ac;
2527 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002528 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002529 va_end(ac);
2530 }
paul718e3742002-12-13 20:15:29 +00002531}
2532
ajs274a4a42004-12-07 15:39:31 +00002533/* Async-signal-safe version of vty_log for fixed strings. */
2534void
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002535vty_log_fixed (char *buf, size_t len)
ajs274a4a42004-12-07 15:39:31 +00002536{
2537 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002538 struct iovec iov[2];
2539
Paul Jakmaa4b30302006-05-28 08:18:38 +00002540 /* vty may not have been initialised */
2541 if (!vtyvec)
2542 return;
2543
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002544 iov[0].iov_base = buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002545 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002546 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002547 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002548
paul55468c82005-03-14 20:19:01 +00002549 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002550 {
2551 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002552 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2553 /* N.B. We don't care about the return code, since process is
2554 most likely just about to die anyway. */
David Lamparter4715a532013-05-30 16:31:49 +02002555 writev(vty->wfd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002556 }
2557}
2558
paul718e3742002-12-13 20:15:29 +00002559int
2560vty_config_lock (struct vty *vty)
2561{
2562 if (vty_config == 0)
2563 {
2564 vty->config = 1;
2565 vty_config = 1;
2566 }
2567 return vty->config;
2568}
2569
2570int
2571vty_config_unlock (struct vty *vty)
2572{
2573 if (vty_config == 1 && vty->config == 1)
2574 {
2575 vty->config = 0;
2576 vty_config = 0;
2577 }
2578 return vty->config;
2579}
David Lamparter6b0655a2014-06-04 06:53:35 +02002580
paul718e3742002-12-13 20:15:29 +00002581/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002582static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002583
2584static void
2585vty_event (enum event event, int sock, struct vty *vty)
2586{
2587 struct thread *vty_serv_thread;
2588
2589 switch (event)
2590 {
2591 case VTY_SERV:
2592 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2593 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2594 break;
2595#ifdef VTYSH
2596 case VTYSH_SERV:
Christian Franke677bcbb2013-02-27 13:47:23 +00002597 vty_serv_thread = thread_add_read (master, vtysh_accept, vty, sock);
2598 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
paul718e3742002-12-13 20:15:29 +00002599 break;
2600 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002601 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2602 break;
2603 case VTYSH_WRITE:
2604 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002605 break;
2606#endif /* VTYSH */
2607 case VTY_READ:
2608 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2609
2610 /* Time out treatment. */
2611 if (vty->v_timeout)
2612 {
2613 if (vty->t_timeout)
2614 thread_cancel (vty->t_timeout);
2615 vty->t_timeout =
2616 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2617 }
2618 break;
2619 case VTY_WRITE:
2620 if (! vty->t_write)
2621 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2622 break;
2623 case VTY_TIMEOUT_RESET:
2624 if (vty->t_timeout)
2625 {
2626 thread_cancel (vty->t_timeout);
2627 vty->t_timeout = NULL;
2628 }
2629 if (vty->v_timeout)
2630 {
2631 vty->t_timeout =
2632 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2633 }
2634 break;
2635 }
2636}
David Lamparter6b0655a2014-06-04 06:53:35 +02002637
paul718e3742002-12-13 20:15:29 +00002638DEFUN (config_who,
2639 config_who_cmd,
2640 "who",
2641 "Display who is on vty\n")
2642{
hasso8c328f12004-10-05 21:01:23 +00002643 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002644 struct vty *v;
2645
paul55468c82005-03-14 20:19:01 +00002646 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002647 if ((v = vector_slot (vtyvec, i)) != NULL)
2648 vty_out (vty, "%svty[%d] connected from %s.%s",
2649 v->config ? "*" : " ",
2650 i, v->address, VTY_NEWLINE);
2651 return CMD_SUCCESS;
2652}
2653
2654/* Move to vty configuration mode. */
2655DEFUN (line_vty,
2656 line_vty_cmd,
2657 "line vty",
2658 "Configure a terminal line\n"
2659 "Virtual terminal\n")
2660{
2661 vty->node = VTY_NODE;
2662 return CMD_SUCCESS;
2663}
2664
2665/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002666static int
paul9035efa2004-10-10 11:56:56 +00002667exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002668{
2669 unsigned long timeout = 0;
2670
2671 /* min_str and sec_str are already checked by parser. So it must be
2672 all digit string. */
2673 if (min_str)
2674 {
2675 timeout = strtol (min_str, NULL, 10);
2676 timeout *= 60;
2677 }
2678 if (sec_str)
2679 timeout += strtol (sec_str, NULL, 10);
2680
2681 vty_timeout_val = timeout;
2682 vty->v_timeout = timeout;
2683 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2684
2685
2686 return CMD_SUCCESS;
2687}
2688
2689DEFUN (exec_timeout_min,
2690 exec_timeout_min_cmd,
2691 "exec-timeout <0-35791>",
2692 "Set timeout value\n"
2693 "Timeout value in minutes\n")
2694{
2695 return exec_timeout (vty, argv[0], NULL);
2696}
2697
2698DEFUN (exec_timeout_sec,
2699 exec_timeout_sec_cmd,
2700 "exec-timeout <0-35791> <0-2147483>",
2701 "Set the EXEC timeout\n"
2702 "Timeout in minutes\n"
2703 "Timeout in seconds\n")
2704{
2705 return exec_timeout (vty, argv[0], argv[1]);
2706}
2707
2708DEFUN (no_exec_timeout,
2709 no_exec_timeout_cmd,
2710 "no exec-timeout",
2711 NO_STR
2712 "Set the EXEC timeout\n")
2713{
2714 return exec_timeout (vty, NULL, NULL);
2715}
2716
2717/* Set vty access class. */
2718DEFUN (vty_access_class,
2719 vty_access_class_cmd,
2720 "access-class WORD",
2721 "Filter connections based on an IP access list\n"
2722 "IP access list\n")
2723{
2724 if (vty_accesslist_name)
2725 XFREE(MTYPE_VTY, vty_accesslist_name);
2726
2727 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2728
2729 return CMD_SUCCESS;
2730}
2731
2732/* Clear vty access class. */
2733DEFUN (no_vty_access_class,
2734 no_vty_access_class_cmd,
2735 "no access-class [WORD]",
2736 NO_STR
2737 "Filter connections based on an IP access list\n"
2738 "IP access list\n")
2739{
2740 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2741 {
2742 vty_out (vty, "Access-class is not currently applied to vty%s",
2743 VTY_NEWLINE);
2744 return CMD_WARNING;
2745 }
2746
2747 XFREE(MTYPE_VTY, vty_accesslist_name);
2748
2749 vty_accesslist_name = NULL;
2750
2751 return CMD_SUCCESS;
2752}
2753
2754#ifdef HAVE_IPV6
2755/* Set vty access class. */
2756DEFUN (vty_ipv6_access_class,
2757 vty_ipv6_access_class_cmd,
2758 "ipv6 access-class WORD",
2759 IPV6_STR
2760 "Filter connections based on an IP access list\n"
2761 "IPv6 access list\n")
2762{
2763 if (vty_ipv6_accesslist_name)
2764 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2765
2766 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2767
2768 return CMD_SUCCESS;
2769}
2770
2771/* Clear vty access class. */
2772DEFUN (no_vty_ipv6_access_class,
2773 no_vty_ipv6_access_class_cmd,
2774 "no ipv6 access-class [WORD]",
2775 NO_STR
2776 IPV6_STR
2777 "Filter connections based on an IP access list\n"
2778 "IPv6 access list\n")
2779{
2780 if (! vty_ipv6_accesslist_name ||
2781 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2782 {
2783 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2784 VTY_NEWLINE);
2785 return CMD_WARNING;
2786 }
2787
2788 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2789
2790 vty_ipv6_accesslist_name = NULL;
2791
2792 return CMD_SUCCESS;
2793}
2794#endif /* HAVE_IPV6 */
2795
2796/* vty login. */
2797DEFUN (vty_login,
2798 vty_login_cmd,
2799 "login",
2800 "Enable password checking\n")
2801{
2802 no_password_check = 0;
2803 return CMD_SUCCESS;
2804}
2805
2806DEFUN (no_vty_login,
2807 no_vty_login_cmd,
2808 "no login",
2809 NO_STR
2810 "Enable password checking\n")
2811{
2812 no_password_check = 1;
2813 return CMD_SUCCESS;
2814}
2815
Paul Jakma62687ff2008-08-23 14:27:06 +01002816/* initial mode. */
2817DEFUN (vty_restricted_mode,
2818 vty_restricted_mode_cmd,
2819 "anonymous restricted",
2820 "Restrict view commands available in anonymous, unauthenticated vty\n")
2821{
2822 restricted_mode = 1;
2823 return CMD_SUCCESS;
2824}
2825
2826DEFUN (vty_no_restricted_mode,
2827 vty_no_restricted_mode_cmd,
2828 "no anonymous restricted",
2829 NO_STR
2830 "Enable password checking\n")
2831{
2832 restricted_mode = 0;
2833 return CMD_SUCCESS;
2834}
2835
paul718e3742002-12-13 20:15:29 +00002836DEFUN (service_advanced_vty,
2837 service_advanced_vty_cmd,
2838 "service advanced-vty",
2839 "Set up miscellaneous service\n"
2840 "Enable advanced mode vty interface\n")
2841{
2842 host.advanced = 1;
2843 return CMD_SUCCESS;
2844}
2845
2846DEFUN (no_service_advanced_vty,
2847 no_service_advanced_vty_cmd,
2848 "no service advanced-vty",
2849 NO_STR
2850 "Set up miscellaneous service\n"
2851 "Enable advanced mode vty interface\n")
2852{
2853 host.advanced = 0;
2854 return CMD_SUCCESS;
2855}
2856
2857DEFUN (terminal_monitor,
2858 terminal_monitor_cmd,
2859 "terminal monitor",
2860 "Set terminal line parameters\n"
2861 "Copy debug output to the current terminal line\n")
2862{
2863 vty->monitor = 1;
2864 return CMD_SUCCESS;
2865}
2866
2867DEFUN (terminal_no_monitor,
2868 terminal_no_monitor_cmd,
2869 "terminal no monitor",
2870 "Set terminal line parameters\n"
2871 NO_STR
2872 "Copy debug output to the current terminal line\n")
2873{
2874 vty->monitor = 0;
2875 return CMD_SUCCESS;
2876}
2877
paul789f78a2006-01-17 17:42:03 +00002878ALIAS (terminal_no_monitor,
2879 no_terminal_monitor_cmd,
2880 "no terminal monitor",
2881 NO_STR
2882 "Set terminal line parameters\n"
2883 "Copy debug output to the current terminal line\n")
2884
paul718e3742002-12-13 20:15:29 +00002885DEFUN (show_history,
2886 show_history_cmd,
2887 "show history",
2888 SHOW_STR
2889 "Display the session command history\n")
2890{
2891 int index;
2892
2893 for (index = vty->hindex + 1; index != vty->hindex;)
2894 {
2895 if (index == VTY_MAXHIST)
2896 {
2897 index = 0;
2898 continue;
2899 }
2900
2901 if (vty->hist[index] != NULL)
2902 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2903
2904 index++;
2905 }
2906
2907 return CMD_SUCCESS;
2908}
2909
2910/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002911static int
paul718e3742002-12-13 20:15:29 +00002912vty_config_write (struct vty *vty)
2913{
2914 vty_out (vty, "line vty%s", VTY_NEWLINE);
2915
2916 if (vty_accesslist_name)
2917 vty_out (vty, " access-class %s%s",
2918 vty_accesslist_name, VTY_NEWLINE);
2919
2920 if (vty_ipv6_accesslist_name)
2921 vty_out (vty, " ipv6 access-class %s%s",
2922 vty_ipv6_accesslist_name, VTY_NEWLINE);
2923
2924 /* exec-timeout */
2925 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2926 vty_out (vty, " exec-timeout %ld %ld%s",
2927 vty_timeout_val / 60,
2928 vty_timeout_val % 60, VTY_NEWLINE);
2929
2930 /* login */
2931 if (no_password_check)
2932 vty_out (vty, " no login%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +01002933
2934 if (restricted_mode != restricted_mode_default)
2935 {
2936 if (restricted_mode_default)
2937 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
2938 else
2939 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
2940 }
2941
paul718e3742002-12-13 20:15:29 +00002942 vty_out (vty, "!%s", VTY_NEWLINE);
2943
2944 return CMD_SUCCESS;
2945}
2946
2947struct cmd_node vty_node =
2948{
2949 VTY_NODE,
2950 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002951 1,
paul718e3742002-12-13 20:15:29 +00002952};
2953
2954/* Reset all VTY status. */
2955void
2956vty_reset ()
2957{
hasso8c328f12004-10-05 21:01:23 +00002958 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002959 struct vty *vty;
2960 struct thread *vty_serv_thread;
2961
paul55468c82005-03-14 20:19:01 +00002962 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002963 if ((vty = vector_slot (vtyvec, i)) != NULL)
2964 {
2965 buffer_reset (vty->obuf);
2966 vty->status = VTY_CLOSE;
2967 vty_close (vty);
2968 }
2969
paul55468c82005-03-14 20:19:01 +00002970 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002971 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2972 {
2973 thread_cancel (vty_serv_thread);
2974 vector_slot (Vvty_serv_thread, i) = NULL;
2975 close (i);
2976 }
2977
2978 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2979
2980 if (vty_accesslist_name)
2981 {
2982 XFREE(MTYPE_VTY, vty_accesslist_name);
2983 vty_accesslist_name = NULL;
2984 }
2985
2986 if (vty_ipv6_accesslist_name)
2987 {
2988 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2989 vty_ipv6_accesslist_name = NULL;
2990 }
2991}
2992
ajs9fc7ebf2005-02-23 15:12:34 +00002993static void
2994vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002995{
paul79ad2792003-10-15 22:09:28 +00002996 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002997 char *c;
paul718e3742002-12-13 20:15:29 +00002998
paulccc92352003-10-22 02:49:38 +00002999 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00003000
paulccc92352003-10-22 02:49:38 +00003001 if (!c)
paul79ad2792003-10-15 22:09:28 +00003002 {
3003 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00003004 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00003005 }
paul718e3742002-12-13 20:15:29 +00003006
3007 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
3008 strcpy (vty_cwd, cwd);
3009}
3010
3011char *
3012vty_get_cwd ()
3013{
3014 return vty_cwd;
3015}
3016
3017int
3018vty_shell (struct vty *vty)
3019{
3020 return vty->type == VTY_SHELL ? 1 : 0;
3021}
3022
3023int
3024vty_shell_serv (struct vty *vty)
3025{
3026 return vty->type == VTY_SHELL_SERV ? 1 : 0;
3027}
3028
3029void
3030vty_init_vtysh ()
3031{
3032 vtyvec = vector_init (VECTOR_MIN_SIZE);
3033}
3034
3035/* Install vty's own commands like `who' command. */
3036void
paulb21b19c2003-06-15 01:28:29 +00003037vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00003038{
3039 /* For further configuration read, preserve current directory. */
3040 vty_save_cwd ();
3041
3042 vtyvec = vector_init (VECTOR_MIN_SIZE);
3043
paulb21b19c2003-06-15 01:28:29 +00003044 master = master_thread;
3045
David Lamparterba53a8f2015-05-05 11:04:46 +02003046 atexit (vty_stdio_reset);
3047
paul718e3742002-12-13 20:15:29 +00003048 /* Initilize server thread vector. */
3049 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
3050
3051 /* Install bgp top node. */
3052 install_node (&vty_node, vty_config_write);
3053
Paul Jakma62687ff2008-08-23 14:27:06 +01003054 install_element (RESTRICTED_NODE, &config_who_cmd);
3055 install_element (RESTRICTED_NODE, &show_history_cmd);
paul718e3742002-12-13 20:15:29 +00003056 install_element (VIEW_NODE, &config_who_cmd);
3057 install_element (VIEW_NODE, &show_history_cmd);
3058 install_element (ENABLE_NODE, &config_who_cmd);
3059 install_element (CONFIG_NODE, &line_vty_cmd);
3060 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
3061 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
3062 install_element (CONFIG_NODE, &show_history_cmd);
3063 install_element (ENABLE_NODE, &terminal_monitor_cmd);
3064 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00003065 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00003066 install_element (ENABLE_NODE, &show_history_cmd);
3067
3068 install_default (VTY_NODE);
3069 install_element (VTY_NODE, &exec_timeout_min_cmd);
3070 install_element (VTY_NODE, &exec_timeout_sec_cmd);
3071 install_element (VTY_NODE, &no_exec_timeout_cmd);
3072 install_element (VTY_NODE, &vty_access_class_cmd);
3073 install_element (VTY_NODE, &no_vty_access_class_cmd);
3074 install_element (VTY_NODE, &vty_login_cmd);
3075 install_element (VTY_NODE, &no_vty_login_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01003076 install_element (VTY_NODE, &vty_restricted_mode_cmd);
3077 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
paul718e3742002-12-13 20:15:29 +00003078#ifdef HAVE_IPV6
3079 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
3080 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
3081#endif /* HAVE_IPV6 */
3082}
Chris Caputo228da422009-07-18 05:44:03 +00003083
3084void
3085vty_terminate (void)
3086{
3087 if (vty_cwd)
3088 XFREE (MTYPE_TMP, vty_cwd);
3089
3090 if (vtyvec && Vvty_serv_thread)
3091 {
3092 vty_reset ();
3093 vector_free (vtyvec);
3094 vector_free (Vvty_serv_thread);
3095 }
3096}