blob: 0d6345c83ab0c2fdcda253b603ede8654ca9294f [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "linklist.h"
paulb21b19c2003-06-15 01:28:29 +000026#include "thread.h"
paul718e3742002-12-13 20:15:29 +000027#include "buffer.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "command.h"
30#include "sockunion.h"
paul718e3742002-12-13 20:15:29 +000031#include "memory.h"
32#include "str.h"
33#include "log.h"
34#include "prefix.h"
35#include "filter.h"
paulb21b19c2003-06-15 01:28:29 +000036#include "vty.h"
pauledd7c242003-06-04 13:59:38 +000037#include "privs.h"
ajs9fc7ebf2005-02-23 15:12:34 +000038#include "network.h"
39
40#include <arpa/telnet.h>
paul718e3742002-12-13 20:15:29 +000041
42/* Vty events */
43enum event
44{
45 VTY_SERV,
46 VTY_READ,
47 VTY_WRITE,
48 VTY_TIMEOUT_RESET,
49#ifdef VTYSH
50 VTYSH_SERV,
ajs49ff6d92004-11-04 19:26:16 +000051 VTYSH_READ,
52 VTYSH_WRITE
paul718e3742002-12-13 20:15:29 +000053#endif /* VTYSH */
54};
55
56static void vty_event (enum event, int, struct vty *);
57
58/* Extern host structure from command.c */
59extern struct host host;
60
61/* Vector which store each vty structure. */
62static vector vtyvec;
63
64/* Vty timeout value. */
65static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
66
67/* Vty access-class command */
68static char *vty_accesslist_name = NULL;
69
70/* Vty access-calss for IPv6. */
71static char *vty_ipv6_accesslist_name = NULL;
72
73/* VTY server thread. */
Christian Franke677bcbb2013-02-27 13:47:23 +000074static vector Vvty_serv_thread;
paul718e3742002-12-13 20:15:29 +000075
76/* Current directory. */
77char *vty_cwd = NULL;
78
79/* Configure lock. */
80static int vty_config;
81
82/* Login password check. */
83static int no_password_check = 0;
84
Paul Jakma62687ff2008-08-23 14:27:06 +010085/* Restrict unauthenticated logins? */
86static const u_char restricted_mode_default = 0;
87static u_char restricted_mode = 0;
88
paul718e3742002-12-13 20:15:29 +000089/* Integrated configuration file path */
90char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
91
92
93/* VTY standard output function. */
94int
95vty_out (struct vty *vty, const char *format, ...)
96{
97 va_list args;
98 int len = 0;
99 int size = 1024;
100 char buf[1024];
101 char *p = NULL;
paul718e3742002-12-13 20:15:29 +0000102
103 if (vty_shell (vty))
ajsd246bd92004-11-23 17:35:08 +0000104 {
105 va_start (args, format);
106 vprintf (format, args);
107 va_end (args);
108 }
paul718e3742002-12-13 20:15:29 +0000109 else
110 {
111 /* Try to write to initial buffer. */
ajsd246bd92004-11-23 17:35:08 +0000112 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000113 len = vsnprintf (buf, sizeof buf, format, args);
ajsd246bd92004-11-23 17:35:08 +0000114 va_end (args);
paul718e3742002-12-13 20:15:29 +0000115
116 /* Initial buffer is not enough. */
117 if (len < 0 || len >= size)
118 {
119 while (1)
120 {
121 if (len > -1)
122 size = len + 1;
123 else
124 size = size * 2;
125
126 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
127 if (! p)
128 return -1;
129
ajsd246bd92004-11-23 17:35:08 +0000130 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000131 len = vsnprintf (p, size, format, args);
ajsd246bd92004-11-23 17:35:08 +0000132 va_end (args);
paul718e3742002-12-13 20:15:29 +0000133
134 if (len > -1 && len < size)
135 break;
136 }
137 }
138
139 /* When initial buffer is enough to store all output. */
140 if (! p)
141 p = buf;
142
143 /* Pointer p must point out buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +0000144 buffer_put (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000145
146 /* If p is not different with buf, it is allocated buffer. */
147 if (p != buf)
148 XFREE (MTYPE_VTY_OUT_BUF, p);
149 }
150
paul718e3742002-12-13 20:15:29 +0000151 return len;
152}
153
ajsd246bd92004-11-23 17:35:08 +0000154static int
ajs274a4a42004-12-07 15:39:31 +0000155vty_log_out (struct vty *vty, const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000156 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +0000157{
ajs9fc7ebf2005-02-23 15:12:34 +0000158 int ret;
paul718e3742002-12-13 20:15:29 +0000159 int len;
160 char buf[1024];
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000161
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000162 if (!ctl->already_rendered)
163 {
164 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
165 ctl->already_rendered = 1;
166 }
167 if (ctl->len+1 >= sizeof(buf))
168 return -1;
169 memcpy(buf, ctl->buf, len = ctl->len);
170 buf[len++] = ' ';
171 buf[len] = '\0';
paul718e3742002-12-13 20:15:29 +0000172
ajs274a4a42004-12-07 15:39:31 +0000173 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000174 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000175 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000176 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
177 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000178 return -1;
paul718e3742002-12-13 20:15:29 +0000179
ajs9fc7ebf2005-02-23 15:12:34 +0000180 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
181 ((size_t)((len += ret)+2) > sizeof(buf)))
182 return -1;
paul718e3742002-12-13 20:15:29 +0000183
ajs9fc7ebf2005-02-23 15:12:34 +0000184 buf[len++] = '\r';
185 buf[len++] = '\n';
186
187 if (write(vty->fd, buf, len) < 0)
188 {
189 if (ERRNO_IO_RETRY(errno))
190 /* Kernel buffer is full, probably too much debugging output, so just
191 drop the data and ignore. */
192 return -1;
193 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000194 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000195 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
196 __func__, vty->fd, safe_strerror(errno));
197 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000198 /* cannot call vty_close, because a parent routine may still try
199 to access the vty struct */
200 vty->status = VTY_CLOSE;
201 shutdown(vty->fd, SHUT_RDWR);
ajs9fc7ebf2005-02-23 15:12:34 +0000202 return -1;
203 }
204 return 0;
paul718e3742002-12-13 20:15:29 +0000205}
206
207/* Output current time to the vty. */
208void
209vty_time_print (struct vty *vty, int cr)
210{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000211 char buf [25];
paul718e3742002-12-13 20:15:29 +0000212
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000213 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
paul718e3742002-12-13 20:15:29 +0000214 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000215 zlog (NULL, LOG_INFO, "quagga_timestamp error");
paul718e3742002-12-13 20:15:29 +0000216 return;
217 }
218 if (cr)
219 vty_out (vty, "%s\n", buf);
220 else
221 vty_out (vty, "%s ", buf);
222
223 return;
224}
225
226/* Say hello to vty interface. */
227void
228vty_hello (struct vty *vty)
229{
paul3b0c5d92005-03-08 10:43:43 +0000230 if (host.motdfile)
231 {
232 FILE *f;
233 char buf[4096];
paul22085182005-03-08 16:00:12 +0000234
paul3b0c5d92005-03-08 10:43:43 +0000235 f = fopen (host.motdfile, "r");
236 if (f)
237 {
paulb45da6f2005-03-08 15:16:57 +0000238 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000239 {
paulb45da6f2005-03-08 15:16:57 +0000240 char *s;
paul22085182005-03-08 16:00:12 +0000241 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000242 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000243 s--);
244 *s = '\0';
245 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
246 }
paul3b0c5d92005-03-08 10:43:43 +0000247 fclose (f);
248 }
249 else
paulb45da6f2005-03-08 15:16:57 +0000250 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000251 }
252 else if (host.motd)
Nico Goldeb830c892010-08-01 15:24:35 +0200253 vty_out (vty, "%s", host.motd);
paul718e3742002-12-13 20:15:29 +0000254}
255
256/* Put out prompt and wait input from user. */
257static void
258vty_prompt (struct vty *vty)
259{
260 struct utsname names;
261 const char*hostname;
262
263 if (vty->type == VTY_TERM)
264 {
265 hostname = host.name;
266 if (!hostname)
267 {
268 uname (&names);
269 hostname = names.nodename;
270 }
271 vty_out (vty, cmd_prompt (vty->node), hostname);
272 }
273}
274
275/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000276static void
paul718e3742002-12-13 20:15:29 +0000277vty_will_echo (struct vty *vty)
278{
paul02ff83c2004-06-11 11:27:03 +0000279 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000280 vty_out (vty, "%s", cmd);
281}
282
283/* Make suppress Go-Ahead telnet option. */
284static void
285vty_will_suppress_go_ahead (struct vty *vty)
286{
paul02ff83c2004-06-11 11:27:03 +0000287 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000288 vty_out (vty, "%s", cmd);
289}
290
291/* Make don't use linemode over telnet. */
292static void
293vty_dont_linemode (struct vty *vty)
294{
paul02ff83c2004-06-11 11:27:03 +0000295 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000296 vty_out (vty, "%s", cmd);
297}
298
299/* Use window size. */
300static void
301vty_do_window_size (struct vty *vty)
302{
paul02ff83c2004-06-11 11:27:03 +0000303 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000304 vty_out (vty, "%s", cmd);
305}
306
307#if 0 /* Currently not used. */
308/* Make don't use lflow vty interface. */
309static void
310vty_dont_lflow_ahead (struct vty *vty)
311{
paul02ff83c2004-06-11 11:27:03 +0000312 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000313 vty_out (vty, "%s", cmd);
314}
315#endif /* 0 */
316
317/* Allocate new vty struct. */
318struct vty *
319vty_new ()
320{
321 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
322
ajs9fc7ebf2005-02-23 15:12:34 +0000323 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000324 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
325 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000326
327 return new;
328}
329
330/* Authentication of vty */
331static void
332vty_auth (struct vty *vty, char *buf)
333{
334 char *passwd = NULL;
335 enum node_type next_node = 0;
336 int fail;
337 char *crypt (const char *, const char *);
338
339 switch (vty->node)
340 {
341 case AUTH_NODE:
342 if (host.encrypt)
343 passwd = host.password_encrypt;
344 else
345 passwd = host.password;
346 if (host.advanced)
347 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
348 else
349 next_node = VIEW_NODE;
350 break;
351 case AUTH_ENABLE_NODE:
352 if (host.encrypt)
353 passwd = host.enable_encrypt;
354 else
355 passwd = host.enable;
356 next_node = ENABLE_NODE;
357 break;
358 }
359
360 if (passwd)
361 {
362 if (host.encrypt)
363 fail = strcmp (crypt(buf, passwd), passwd);
364 else
365 fail = strcmp (buf, passwd);
366 }
367 else
368 fail = 1;
369
370 if (! fail)
371 {
372 vty->fail = 0;
373 vty->node = next_node; /* Success ! */
374 }
375 else
376 {
377 vty->fail++;
378 if (vty->fail >= 3)
379 {
380 if (vty->node == AUTH_NODE)
381 {
382 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
383 vty->status = VTY_CLOSE;
384 }
385 else
386 {
387 /* AUTH_ENABLE_NODE */
388 vty->fail = 0;
389 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +0100390 vty->node = restricted_mode ? RESTRICTED_NODE : VIEW_NODE;
paul718e3742002-12-13 20:15:29 +0000391 }
392 }
393 }
394}
395
396/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000397static int
paul718e3742002-12-13 20:15:29 +0000398vty_command (struct vty *vty, char *buf)
399{
400 int ret;
401 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000402 const char *protocolname;
paul718e3742002-12-13 20:15:29 +0000403
404 /* Split readline string up into the vector */
405 vline = cmd_make_strvec (buf);
406
407 if (vline == NULL)
408 return CMD_SUCCESS;
409
ajs924b9222005-04-16 17:11:24 +0000410#ifdef CONSUMED_TIME_CHECK
411 {
412 RUSAGE_T before;
413 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000414 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000415
416 GETRUSAGE(&before);
417#endif /* CONSUMED_TIME_CHECK */
418
hasso87d683b2005-01-16 23:31:54 +0000419 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000420
vincentfbf5d032005-09-29 11:25:50 +0000421 /* Get the name of the protocol if any */
422 if (zlog_default)
423 protocolname = zlog_proto_names[zlog_default->protocol];
424 else
425 protocolname = zlog_proto_names[ZLOG_NONE];
426
ajs924b9222005-04-16 17:11:24 +0000427#ifdef CONSUMED_TIME_CHECK
428 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000429 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
430 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000431 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000432 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
433 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000434 }
435#endif /* CONSUMED_TIME_CHECK */
436
paul718e3742002-12-13 20:15:29 +0000437 if (ret != CMD_SUCCESS)
438 switch (ret)
439 {
440 case CMD_WARNING:
441 if (vty->type == VTY_FILE)
442 vty_out (vty, "Warning...%s", VTY_NEWLINE);
443 break;
444 case CMD_ERR_AMBIGUOUS:
445 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
446 break;
447 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000448 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000449 break;
450 case CMD_ERR_INCOMPLETE:
451 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
452 break;
453 }
454 cmd_free_strvec (vline);
455
456 return ret;
457}
458
ajs9fc7ebf2005-02-23 15:12:34 +0000459static const char telnet_backward_char = 0x08;
460static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000461
462/* Basic function to write buffer to vty. */
463static void
ajs9fc7ebf2005-02-23 15:12:34 +0000464vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000465{
466 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
467 return;
468
469 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000470 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000471}
472
473/* Ensure length of input buffer. Is buffer is short, double it. */
474static void
475vty_ensure (struct vty *vty, int length)
476{
477 if (vty->max <= length)
478 {
479 vty->max *= 2;
480 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
481 }
482}
483
484/* Basic function to insert character into vty. */
485static void
486vty_self_insert (struct vty *vty, char c)
487{
488 int i;
489 int length;
490
491 vty_ensure (vty, vty->length + 1);
492 length = vty->length - vty->cp;
493 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
494 vty->buf[vty->cp] = c;
495
496 vty_write (vty, &vty->buf[vty->cp], length + 1);
497 for (i = 0; i < length; i++)
498 vty_write (vty, &telnet_backward_char, 1);
499
500 vty->cp++;
501 vty->length++;
502}
503
504/* Self insert character 'c' in overwrite mode. */
505static void
506vty_self_insert_overwrite (struct vty *vty, char c)
507{
508 vty_ensure (vty, vty->length + 1);
509 vty->buf[vty->cp++] = c;
510
511 if (vty->cp > vty->length)
512 vty->length++;
513
514 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
515 return;
516
517 vty_write (vty, &c, 1);
518}
519
520/* Insert a word into vty interface with overwrite mode. */
521static void
522vty_insert_word_overwrite (struct vty *vty, char *str)
523{
524 int len = strlen (str);
525 vty_write (vty, str, len);
526 strcpy (&vty->buf[vty->cp], str);
527 vty->cp += len;
528 vty->length = vty->cp;
529}
530
531/* Forward character. */
532static void
533vty_forward_char (struct vty *vty)
534{
535 if (vty->cp < vty->length)
536 {
537 vty_write (vty, &vty->buf[vty->cp], 1);
538 vty->cp++;
539 }
540}
541
542/* Backward character. */
543static void
544vty_backward_char (struct vty *vty)
545{
546 if (vty->cp > 0)
547 {
548 vty->cp--;
549 vty_write (vty, &telnet_backward_char, 1);
550 }
551}
552
553/* Move to the beginning of the line. */
554static void
555vty_beginning_of_line (struct vty *vty)
556{
557 while (vty->cp)
558 vty_backward_char (vty);
559}
560
561/* Move to the end of the line. */
562static void
563vty_end_of_line (struct vty *vty)
564{
565 while (vty->cp < vty->length)
566 vty_forward_char (vty);
567}
568
569static void vty_kill_line_from_beginning (struct vty *);
570static void vty_redraw_line (struct vty *);
571
572/* Print command line history. This function is called from
573 vty_next_line and vty_previous_line. */
574static void
575vty_history_print (struct vty *vty)
576{
577 int length;
578
579 vty_kill_line_from_beginning (vty);
580
581 /* Get previous line from history buffer */
582 length = strlen (vty->hist[vty->hp]);
583 memcpy (vty->buf, vty->hist[vty->hp], length);
584 vty->cp = vty->length = length;
585
586 /* Redraw current line */
587 vty_redraw_line (vty);
588}
589
590/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000591static void
paul718e3742002-12-13 20:15:29 +0000592vty_next_line (struct vty *vty)
593{
594 int try_index;
595
596 if (vty->hp == vty->hindex)
597 return;
598
599 /* Try is there history exist or not. */
600 try_index = vty->hp;
601 if (try_index == (VTY_MAXHIST - 1))
602 try_index = 0;
603 else
604 try_index++;
605
606 /* If there is not history return. */
607 if (vty->hist[try_index] == NULL)
608 return;
609 else
610 vty->hp = try_index;
611
612 vty_history_print (vty);
613}
614
615/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000616static void
paul718e3742002-12-13 20:15:29 +0000617vty_previous_line (struct vty *vty)
618{
619 int try_index;
620
621 try_index = vty->hp;
622 if (try_index == 0)
623 try_index = VTY_MAXHIST - 1;
624 else
625 try_index--;
626
627 if (vty->hist[try_index] == NULL)
628 return;
629 else
630 vty->hp = try_index;
631
632 vty_history_print (vty);
633}
634
635/* This function redraw all of the command line character. */
636static void
637vty_redraw_line (struct vty *vty)
638{
639 vty_write (vty, vty->buf, vty->length);
640 vty->cp = vty->length;
641}
642
643/* Forward word. */
644static void
645vty_forward_word (struct vty *vty)
646{
647 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
648 vty_forward_char (vty);
649
650 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
651 vty_forward_char (vty);
652}
653
654/* Backward word without skipping training space. */
655static void
656vty_backward_pure_word (struct vty *vty)
657{
658 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
659 vty_backward_char (vty);
660}
661
662/* Backward word. */
663static void
664vty_backward_word (struct vty *vty)
665{
666 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
667 vty_backward_char (vty);
668
669 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
670 vty_backward_char (vty);
671}
672
673/* When '^D' is typed at the beginning of the line we move to the down
674 level. */
675static void
676vty_down_level (struct vty *vty)
677{
678 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000679 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000680 vty_prompt (vty);
681 vty->cp = 0;
682}
683
684/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000685static void
paul718e3742002-12-13 20:15:29 +0000686vty_end_config (struct vty *vty)
687{
688 vty_out (vty, "%s", VTY_NEWLINE);
689
690 switch (vty->node)
691 {
692 case VIEW_NODE:
693 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +0100694 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +0000695 /* Nothing to do. */
696 break;
697 case CONFIG_NODE:
698 case INTERFACE_NODE:
699 case ZEBRA_NODE:
700 case RIP_NODE:
701 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +0100702 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +0000703 case BGP_NODE:
704 case BGP_VPNV4_NODE:
705 case BGP_IPV4_NODE:
706 case BGP_IPV4M_NODE:
707 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000708 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000709 case RMAP_NODE:
710 case OSPF_NODE:
711 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000712 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000713 case KEYCHAIN_NODE:
714 case KEYCHAIN_KEY_NODE:
715 case MASC_NODE:
716 case VTY_NODE:
717 vty_config_unlock (vty);
718 vty->node = ENABLE_NODE;
719 break;
720 default:
721 /* Unknown node, we have to ignore it. */
722 break;
723 }
724
725 vty_prompt (vty);
726 vty->cp = 0;
727}
728
729/* Delete a charcter at the current point. */
730static void
731vty_delete_char (struct vty *vty)
732{
733 int i;
734 int size;
735
paul718e3742002-12-13 20:15:29 +0000736 if (vty->length == 0)
737 {
738 vty_down_level (vty);
739 return;
740 }
741
742 if (vty->cp == vty->length)
743 return; /* completion need here? */
744
745 size = vty->length - vty->cp;
746
747 vty->length--;
748 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
749 vty->buf[vty->length] = '\0';
Roy7f794f22008-08-13 17:27:38 +0100750
751 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
752 return;
paul718e3742002-12-13 20:15:29 +0000753
754 vty_write (vty, &vty->buf[vty->cp], size - 1);
755 vty_write (vty, &telnet_space_char, 1);
756
757 for (i = 0; i < size; i++)
758 vty_write (vty, &telnet_backward_char, 1);
759}
760
761/* Delete a character before the point. */
762static void
763vty_delete_backward_char (struct vty *vty)
764{
765 if (vty->cp == 0)
766 return;
767
768 vty_backward_char (vty);
769 vty_delete_char (vty);
770}
771
772/* Kill rest of line from current point. */
773static void
774vty_kill_line (struct vty *vty)
775{
776 int i;
777 int size;
778
779 size = vty->length - vty->cp;
780
781 if (size == 0)
782 return;
783
784 for (i = 0; i < size; i++)
785 vty_write (vty, &telnet_space_char, 1);
786 for (i = 0; i < size; i++)
787 vty_write (vty, &telnet_backward_char, 1);
788
789 memset (&vty->buf[vty->cp], 0, size);
790 vty->length = vty->cp;
791}
792
793/* Kill line from the beginning. */
794static void
795vty_kill_line_from_beginning (struct vty *vty)
796{
797 vty_beginning_of_line (vty);
798 vty_kill_line (vty);
799}
800
801/* Delete a word before the point. */
802static void
803vty_forward_kill_word (struct vty *vty)
804{
805 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
806 vty_delete_char (vty);
807 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
808 vty_delete_char (vty);
809}
810
811/* Delete a word before the point. */
812static void
813vty_backward_kill_word (struct vty *vty)
814{
815 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
816 vty_delete_backward_char (vty);
817 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
818 vty_delete_backward_char (vty);
819}
820
821/* Transpose chars before or at the point. */
822static void
823vty_transpose_chars (struct vty *vty)
824{
825 char c1, c2;
826
827 /* If length is short or point is near by the beginning of line then
828 return. */
829 if (vty->length < 2 || vty->cp < 1)
830 return;
831
832 /* In case of point is located at the end of the line. */
833 if (vty->cp == vty->length)
834 {
835 c1 = vty->buf[vty->cp - 1];
836 c2 = vty->buf[vty->cp - 2];
837
838 vty_backward_char (vty);
839 vty_backward_char (vty);
840 vty_self_insert_overwrite (vty, c1);
841 vty_self_insert_overwrite (vty, c2);
842 }
843 else
844 {
845 c1 = vty->buf[vty->cp];
846 c2 = vty->buf[vty->cp - 1];
847
848 vty_backward_char (vty);
849 vty_self_insert_overwrite (vty, c1);
850 vty_self_insert_overwrite (vty, c2);
851 }
852}
853
854/* Do completion at vty interface. */
855static void
856vty_complete_command (struct vty *vty)
857{
858 int i;
859 int ret;
860 char **matched = NULL;
861 vector vline;
862
863 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
864 return;
865
866 vline = cmd_make_strvec (vty->buf);
867 if (vline == NULL)
868 return;
869
870 /* In case of 'help \t'. */
871 if (isspace ((int) vty->buf[vty->length - 1]))
872 vector_set (vline, '\0');
873
874 matched = cmd_complete_command (vline, vty, &ret);
875
876 cmd_free_strvec (vline);
877
878 vty_out (vty, "%s", VTY_NEWLINE);
879 switch (ret)
880 {
881 case CMD_ERR_AMBIGUOUS:
882 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
883 vty_prompt (vty);
884 vty_redraw_line (vty);
885 break;
886 case CMD_ERR_NO_MATCH:
887 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
888 vty_prompt (vty);
889 vty_redraw_line (vty);
890 break;
891 case CMD_COMPLETE_FULL_MATCH:
892 vty_prompt (vty);
893 vty_redraw_line (vty);
894 vty_backward_pure_word (vty);
895 vty_insert_word_overwrite (vty, matched[0]);
896 vty_self_insert (vty, ' ');
897 XFREE (MTYPE_TMP, matched[0]);
898 break;
899 case CMD_COMPLETE_MATCH:
900 vty_prompt (vty);
901 vty_redraw_line (vty);
902 vty_backward_pure_word (vty);
903 vty_insert_word_overwrite (vty, matched[0]);
904 XFREE (MTYPE_TMP, matched[0]);
905 vector_only_index_free (matched);
906 return;
907 break;
908 case CMD_COMPLETE_LIST_MATCH:
909 for (i = 0; matched[i] != NULL; i++)
910 {
911 if (i != 0 && ((i % 6) == 0))
912 vty_out (vty, "%s", VTY_NEWLINE);
913 vty_out (vty, "%-10s ", matched[i]);
914 XFREE (MTYPE_TMP, matched[i]);
915 }
916 vty_out (vty, "%s", VTY_NEWLINE);
917
918 vty_prompt (vty);
919 vty_redraw_line (vty);
920 break;
921 case CMD_ERR_NOTHING_TODO:
922 vty_prompt (vty);
923 vty_redraw_line (vty);
924 break;
925 default:
926 break;
927 }
928 if (matched)
929 vector_only_index_free (matched);
930}
931
ajs9fc7ebf2005-02-23 15:12:34 +0000932static void
paul718e3742002-12-13 20:15:29 +0000933vty_describe_fold (struct vty *vty, int cmd_width,
hasso8c328f12004-10-05 21:01:23 +0000934 unsigned int desc_width, struct desc *desc)
paul718e3742002-12-13 20:15:29 +0000935{
hasso8c328f12004-10-05 21:01:23 +0000936 char *buf;
937 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000938 int pos;
939
940 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
941
942 if (desc_width <= 0)
943 {
944 vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
945 return;
946 }
947
948 buf = XCALLOC (MTYPE_TMP, strlen (desc->str) + 1);
949
950 for (p = desc->str; strlen (p) > desc_width; p += pos + 1)
951 {
952 for (pos = desc_width; pos > 0; pos--)
953 if (*(p + pos) == ' ')
954 break;
955
956 if (pos == 0)
957 break;
958
959 strncpy (buf, p, pos);
960 buf[pos] = '\0';
961 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
962
963 cmd = "";
964 }
965
966 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
967
968 XFREE (MTYPE_TMP, buf);
969}
970
971/* Describe matched command function. */
972static void
973vty_describe_command (struct vty *vty)
974{
975 int ret;
976 vector vline;
977 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000978 unsigned int i, width, desc_width;
paul718e3742002-12-13 20:15:29 +0000979 struct desc *desc, *desc_cr = NULL;
980
981 vline = cmd_make_strvec (vty->buf);
982
983 /* In case of '> ?'. */
984 if (vline == NULL)
985 {
986 vline = vector_init (1);
987 vector_set (vline, '\0');
988 }
989 else
990 if (isspace ((int) vty->buf[vty->length - 1]))
991 vector_set (vline, '\0');
992
993 describe = cmd_describe_command (vline, vty, &ret);
994
995 vty_out (vty, "%s", VTY_NEWLINE);
996
997 /* Ambiguous error. */
998 switch (ret)
999 {
1000 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +00001001 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001002 goto out;
paul718e3742002-12-13 20:15:29 +00001003 break;
1004 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +00001005 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001006 goto out;
paul718e3742002-12-13 20:15:29 +00001007 break;
1008 }
1009
1010 /* Get width of command string. */
1011 width = 0;
paul55468c82005-03-14 20:19:01 +00001012 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001013 if ((desc = vector_slot (describe, i)) != NULL)
1014 {
hasso8c328f12004-10-05 21:01:23 +00001015 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001016
1017 if (desc->cmd[0] == '\0')
1018 continue;
1019
1020 len = strlen (desc->cmd);
1021 if (desc->cmd[0] == '.')
1022 len--;
1023
1024 if (width < len)
1025 width = len;
1026 }
1027
1028 /* Get width of description string. */
1029 desc_width = vty->width - (width + 6);
1030
1031 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001032 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001033 if ((desc = vector_slot (describe, i)) != NULL)
1034 {
1035 if (desc->cmd[0] == '\0')
1036 continue;
1037
Chris Caputo228da422009-07-18 05:44:03 +00001038 if (strcmp (desc->cmd, command_cr) == 0)
paul718e3742002-12-13 20:15:29 +00001039 {
1040 desc_cr = desc;
1041 continue;
1042 }
1043
1044 if (!desc->str)
1045 vty_out (vty, " %-s%s",
1046 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1047 VTY_NEWLINE);
1048 else if (desc_width >= strlen (desc->str))
1049 vty_out (vty, " %-*s %s%s", width,
1050 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1051 desc->str, VTY_NEWLINE);
1052 else
1053 vty_describe_fold (vty, width, desc_width, desc);
1054
1055#if 0
1056 vty_out (vty, " %-*s %s%s", width
1057 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1058 desc->str ? desc->str : "", VTY_NEWLINE);
1059#endif /* 0 */
1060 }
1061
1062 if ((desc = desc_cr))
1063 {
1064 if (!desc->str)
1065 vty_out (vty, " %-s%s",
1066 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1067 VTY_NEWLINE);
1068 else if (desc_width >= strlen (desc->str))
1069 vty_out (vty, " %-*s %s%s", width,
1070 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1071 desc->str, VTY_NEWLINE);
1072 else
1073 vty_describe_fold (vty, width, desc_width, desc);
1074 }
1075
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001076out:
paul718e3742002-12-13 20:15:29 +00001077 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001078 if (describe)
1079 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001080
1081 vty_prompt (vty);
1082 vty_redraw_line (vty);
1083}
1084
ajs9fc7ebf2005-02-23 15:12:34 +00001085static void
paul718e3742002-12-13 20:15:29 +00001086vty_clear_buf (struct vty *vty)
1087{
1088 memset (vty->buf, 0, vty->max);
1089}
1090
1091/* ^C stop current input and do not add command line to the history. */
1092static void
1093vty_stop_input (struct vty *vty)
1094{
1095 vty->cp = vty->length = 0;
1096 vty_clear_buf (vty);
1097 vty_out (vty, "%s", VTY_NEWLINE);
1098
1099 switch (vty->node)
1100 {
1101 case VIEW_NODE:
1102 case ENABLE_NODE:
Paul Jakma62687ff2008-08-23 14:27:06 +01001103 case RESTRICTED_NODE:
paul718e3742002-12-13 20:15:29 +00001104 /* Nothing to do. */
1105 break;
1106 case CONFIG_NODE:
1107 case INTERFACE_NODE:
1108 case ZEBRA_NODE:
1109 case RIP_NODE:
1110 case RIPNG_NODE:
Paul Jakma57345092011-12-25 17:52:09 +01001111 case BABEL_NODE:
paul718e3742002-12-13 20:15:29 +00001112 case BGP_NODE:
1113 case RMAP_NODE:
1114 case OSPF_NODE:
1115 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001116 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001117 case KEYCHAIN_NODE:
1118 case KEYCHAIN_KEY_NODE:
1119 case MASC_NODE:
1120 case VTY_NODE:
1121 vty_config_unlock (vty);
1122 vty->node = ENABLE_NODE;
1123 break;
1124 default:
1125 /* Unknown node, we have to ignore it. */
1126 break;
1127 }
1128 vty_prompt (vty);
1129
1130 /* Set history pointer to the latest one. */
1131 vty->hp = vty->hindex;
1132}
1133
1134/* Add current command line to the history buffer. */
1135static void
1136vty_hist_add (struct vty *vty)
1137{
1138 int index;
1139
1140 if (vty->length == 0)
1141 return;
1142
1143 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1144
1145 /* Ignore the same string as previous one. */
1146 if (vty->hist[index])
1147 if (strcmp (vty->buf, vty->hist[index]) == 0)
1148 {
1149 vty->hp = vty->hindex;
1150 return;
1151 }
1152
1153 /* Insert history entry. */
1154 if (vty->hist[vty->hindex])
1155 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1156 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1157
1158 /* History index rotation. */
1159 vty->hindex++;
1160 if (vty->hindex == VTY_MAXHIST)
1161 vty->hindex = 0;
1162
1163 vty->hp = vty->hindex;
1164}
1165
1166/* #define TELNET_OPTION_DEBUG */
1167
1168/* Get telnet window size. */
1169static int
1170vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1171{
1172#ifdef TELNET_OPTION_DEBUG
1173 int i;
1174
1175 for (i = 0; i < nbytes; i++)
1176 {
1177 switch (buf[i])
1178 {
1179 case IAC:
1180 vty_out (vty, "IAC ");
1181 break;
1182 case WILL:
1183 vty_out (vty, "WILL ");
1184 break;
1185 case WONT:
1186 vty_out (vty, "WONT ");
1187 break;
1188 case DO:
1189 vty_out (vty, "DO ");
1190 break;
1191 case DONT:
1192 vty_out (vty, "DONT ");
1193 break;
1194 case SB:
1195 vty_out (vty, "SB ");
1196 break;
1197 case SE:
1198 vty_out (vty, "SE ");
1199 break;
1200 case TELOPT_ECHO:
1201 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1202 break;
1203 case TELOPT_SGA:
1204 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1205 break;
1206 case TELOPT_NAWS:
1207 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1208 break;
1209 default:
1210 vty_out (vty, "%x ", buf[i]);
1211 break;
1212 }
1213 }
1214 vty_out (vty, "%s", VTY_NEWLINE);
1215
1216#endif /* TELNET_OPTION_DEBUG */
1217
1218 switch (buf[0])
1219 {
1220 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001221 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001222 vty->iac_sb_in_progress = 1;
1223 return 0;
1224 break;
1225 case SE:
1226 {
paul718e3742002-12-13 20:15:29 +00001227 if (!vty->iac_sb_in_progress)
1228 return 0;
1229
ajs9fc7ebf2005-02-23 15:12:34 +00001230 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001231 {
1232 vty->iac_sb_in_progress = 0;
1233 return 0;
1234 }
ajs9fc7ebf2005-02-23 15:12:34 +00001235 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001236 {
1237 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001238 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1239 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1240 "should send %d characters, but we received %lu",
1241 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1242 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1243 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1244 "too small to handle the telnet NAWS option",
1245 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1246 else
1247 {
1248 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1249 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1250#ifdef TELNET_OPTION_DEBUG
1251 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1252 "width %d, height %d%s",
1253 vty->width, vty->height, VTY_NEWLINE);
1254#endif
1255 }
paul718e3742002-12-13 20:15:29 +00001256 break;
1257 }
1258 vty->iac_sb_in_progress = 0;
1259 return 0;
1260 break;
1261 }
1262 default:
1263 break;
1264 }
1265 return 1;
1266}
1267
1268/* Execute current command line. */
1269static int
1270vty_execute (struct vty *vty)
1271{
1272 int ret;
1273
1274 ret = CMD_SUCCESS;
1275
1276 switch (vty->node)
1277 {
1278 case AUTH_NODE:
1279 case AUTH_ENABLE_NODE:
1280 vty_auth (vty, vty->buf);
1281 break;
1282 default:
1283 ret = vty_command (vty, vty->buf);
1284 if (vty->type == VTY_TERM)
1285 vty_hist_add (vty);
1286 break;
1287 }
1288
1289 /* Clear command line buffer. */
1290 vty->cp = vty->length = 0;
1291 vty_clear_buf (vty);
1292
ajs5a646652004-11-05 01:25:55 +00001293 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001294 vty_prompt (vty);
1295
1296 return ret;
1297}
1298
1299#define CONTROL(X) ((X) - '@')
1300#define VTY_NORMAL 0
1301#define VTY_PRE_ESCAPE 1
1302#define VTY_ESCAPE 2
1303
1304/* Escape character command map. */
1305static void
1306vty_escape_map (unsigned char c, struct vty *vty)
1307{
1308 switch (c)
1309 {
1310 case ('A'):
1311 vty_previous_line (vty);
1312 break;
1313 case ('B'):
1314 vty_next_line (vty);
1315 break;
1316 case ('C'):
1317 vty_forward_char (vty);
1318 break;
1319 case ('D'):
1320 vty_backward_char (vty);
1321 break;
1322 default:
1323 break;
1324 }
1325
1326 /* Go back to normal mode. */
1327 vty->escape = VTY_NORMAL;
1328}
1329
1330/* Quit print out to the buffer. */
1331static void
1332vty_buffer_reset (struct vty *vty)
1333{
1334 buffer_reset (vty->obuf);
1335 vty_prompt (vty);
1336 vty_redraw_line (vty);
1337}
1338
1339/* Read data via vty socket. */
1340static int
1341vty_read (struct thread *thread)
1342{
1343 int i;
paul718e3742002-12-13 20:15:29 +00001344 int nbytes;
1345 unsigned char buf[VTY_READ_BUFSIZ];
1346
1347 int vty_sock = THREAD_FD (thread);
1348 struct vty *vty = THREAD_ARG (thread);
1349 vty->t_read = NULL;
1350
1351 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001352 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1353 {
1354 if (nbytes < 0)
1355 {
1356 if (ERRNO_IO_RETRY(errno))
1357 {
1358 vty_event (VTY_READ, vty_sock, vty);
1359 return 0;
1360 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001361 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001362 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1363 __func__, vty->fd, safe_strerror(errno));
1364 }
1365 buffer_reset(vty->obuf);
1366 vty->status = VTY_CLOSE;
1367 }
paul718e3742002-12-13 20:15:29 +00001368
1369 for (i = 0; i < nbytes; i++)
1370 {
1371 if (buf[i] == IAC)
1372 {
1373 if (!vty->iac)
1374 {
1375 vty->iac = 1;
1376 continue;
1377 }
1378 else
1379 {
1380 vty->iac = 0;
1381 }
1382 }
1383
1384 if (vty->iac_sb_in_progress && !vty->iac)
1385 {
ajs9fc7ebf2005-02-23 15:12:34 +00001386 if (vty->sb_len < sizeof(vty->sb_buf))
1387 vty->sb_buf[vty->sb_len] = buf[i];
1388 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001389 continue;
1390 }
1391
1392 if (vty->iac)
1393 {
1394 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001395 int ret = 0;
paule9372532003-10-26 21:36:07 +00001396 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001397 vty->iac = 0;
1398 i += ret;
1399 continue;
1400 }
paul5b8c1b02003-10-15 23:08:55 +00001401
paul718e3742002-12-13 20:15:29 +00001402
1403 if (vty->status == VTY_MORE)
1404 {
1405 switch (buf[i])
1406 {
1407 case CONTROL('C'):
1408 case 'q':
1409 case 'Q':
paul718e3742002-12-13 20:15:29 +00001410 vty_buffer_reset (vty);
1411 break;
1412#if 0 /* More line does not work for "show ip bgp". */
1413 case '\n':
1414 case '\r':
1415 vty->status = VTY_MORELINE;
1416 break;
1417#endif
1418 default:
paul718e3742002-12-13 20:15:29 +00001419 break;
1420 }
1421 continue;
1422 }
1423
1424 /* Escape character. */
1425 if (vty->escape == VTY_ESCAPE)
1426 {
1427 vty_escape_map (buf[i], vty);
1428 continue;
1429 }
1430
1431 /* Pre-escape status. */
1432 if (vty->escape == VTY_PRE_ESCAPE)
1433 {
1434 switch (buf[i])
1435 {
1436 case '[':
1437 vty->escape = VTY_ESCAPE;
1438 break;
1439 case 'b':
1440 vty_backward_word (vty);
1441 vty->escape = VTY_NORMAL;
1442 break;
1443 case 'f':
1444 vty_forward_word (vty);
1445 vty->escape = VTY_NORMAL;
1446 break;
1447 case 'd':
1448 vty_forward_kill_word (vty);
1449 vty->escape = VTY_NORMAL;
1450 break;
1451 case CONTROL('H'):
1452 case 0x7f:
1453 vty_backward_kill_word (vty);
1454 vty->escape = VTY_NORMAL;
1455 break;
1456 default:
1457 vty->escape = VTY_NORMAL;
1458 break;
1459 }
1460 continue;
1461 }
1462
1463 switch (buf[i])
1464 {
1465 case CONTROL('A'):
1466 vty_beginning_of_line (vty);
1467 break;
1468 case CONTROL('B'):
1469 vty_backward_char (vty);
1470 break;
1471 case CONTROL('C'):
1472 vty_stop_input (vty);
1473 break;
1474 case CONTROL('D'):
1475 vty_delete_char (vty);
1476 break;
1477 case CONTROL('E'):
1478 vty_end_of_line (vty);
1479 break;
1480 case CONTROL('F'):
1481 vty_forward_char (vty);
1482 break;
1483 case CONTROL('H'):
1484 case 0x7f:
1485 vty_delete_backward_char (vty);
1486 break;
1487 case CONTROL('K'):
1488 vty_kill_line (vty);
1489 break;
1490 case CONTROL('N'):
1491 vty_next_line (vty);
1492 break;
1493 case CONTROL('P'):
1494 vty_previous_line (vty);
1495 break;
1496 case CONTROL('T'):
1497 vty_transpose_chars (vty);
1498 break;
1499 case CONTROL('U'):
1500 vty_kill_line_from_beginning (vty);
1501 break;
1502 case CONTROL('W'):
1503 vty_backward_kill_word (vty);
1504 break;
1505 case CONTROL('Z'):
1506 vty_end_config (vty);
1507 break;
1508 case '\n':
1509 case '\r':
1510 vty_out (vty, "%s", VTY_NEWLINE);
1511 vty_execute (vty);
1512 break;
1513 case '\t':
1514 vty_complete_command (vty);
1515 break;
1516 case '?':
1517 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1518 vty_self_insert (vty, buf[i]);
1519 else
1520 vty_describe_command (vty);
1521 break;
1522 case '\033':
1523 if (i + 1 < nbytes && buf[i + 1] == '[')
1524 {
1525 vty->escape = VTY_ESCAPE;
1526 i++;
1527 }
1528 else
1529 vty->escape = VTY_PRE_ESCAPE;
1530 break;
1531 default:
1532 if (buf[i] > 31 && buf[i] < 127)
1533 vty_self_insert (vty, buf[i]);
1534 break;
1535 }
1536 }
1537
1538 /* Check status. */
1539 if (vty->status == VTY_CLOSE)
1540 vty_close (vty);
1541 else
1542 {
1543 vty_event (VTY_WRITE, vty_sock, vty);
1544 vty_event (VTY_READ, vty_sock, vty);
1545 }
1546 return 0;
1547}
1548
1549/* Flush buffer to the vty. */
1550static int
1551vty_flush (struct thread *thread)
1552{
1553 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001554 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001555 int vty_sock = THREAD_FD (thread);
1556 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001557
paul718e3742002-12-13 20:15:29 +00001558 vty->t_write = NULL;
1559
1560 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001561 if ((vty->lines == 0) && vty->t_read)
1562 {
1563 thread_cancel (vty->t_read);
1564 vty->t_read = NULL;
1565 }
paul718e3742002-12-13 20:15:29 +00001566
1567 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001568 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001569
ajs9fc7ebf2005-02-23 15:12:34 +00001570 /* N.B. if width is 0, that means we don't know the window size. */
1571 if ((vty->lines == 0) || (vty->width == 0))
1572 flushrc = buffer_flush_available(vty->obuf, vty->fd);
1573 else if (vty->status == VTY_MORELINE)
1574 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1575 1, erase, 0);
1576 else
1577 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1578 vty->lines >= 0 ? vty->lines :
1579 vty->height,
1580 erase, 0);
1581 switch (flushrc)
1582 {
1583 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001584 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001585 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1586 vty->fd);
1587 buffer_reset(vty->obuf);
1588 vty_close(vty);
1589 return 0;
1590 case BUFFER_EMPTY:
1591 if (vty->status == VTY_CLOSE)
1592 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001593 else
1594 {
ajs9fc7ebf2005-02-23 15:12:34 +00001595 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001596 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001597 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001598 }
ajs9fc7ebf2005-02-23 15:12:34 +00001599 break;
1600 case BUFFER_PENDING:
1601 /* There is more data waiting to be written. */
1602 vty->status = VTY_MORE;
1603 if (vty->lines == 0)
1604 vty_event (VTY_WRITE, vty_sock, vty);
1605 break;
1606 }
paul718e3742002-12-13 20:15:29 +00001607
1608 return 0;
1609}
1610
1611/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001612static struct vty *
paul718e3742002-12-13 20:15:29 +00001613vty_create (int vty_sock, union sockunion *su)
1614{
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001615 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001616 struct vty *vty;
1617
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001618 sockunion2str(su, buf, SU_ADDRSTRLEN);
1619
paul718e3742002-12-13 20:15:29 +00001620 /* Allocate new vty structure and set up default values. */
1621 vty = vty_new ();
1622 vty->fd = vty_sock;
1623 vty->type = VTY_TERM;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001624 strcpy (vty->address, buf);
paul718e3742002-12-13 20:15:29 +00001625 if (no_password_check)
1626 {
Paul Jakma62687ff2008-08-23 14:27:06 +01001627 if (restricted_mode)
1628 vty->node = RESTRICTED_NODE;
1629 else if (host.advanced)
paul718e3742002-12-13 20:15:29 +00001630 vty->node = ENABLE_NODE;
1631 else
1632 vty->node = VIEW_NODE;
1633 }
1634 else
1635 vty->node = AUTH_NODE;
1636 vty->fail = 0;
1637 vty->cp = 0;
1638 vty_clear_buf (vty);
1639 vty->length = 0;
1640 memset (vty->hist, 0, sizeof (vty->hist));
1641 vty->hp = 0;
1642 vty->hindex = 0;
1643 vector_set_index (vtyvec, vty_sock, vty);
1644 vty->status = VTY_NORMAL;
1645 vty->v_timeout = vty_timeout_val;
1646 if (host.lines >= 0)
1647 vty->lines = host.lines;
1648 else
1649 vty->lines = -1;
1650 vty->iac = 0;
1651 vty->iac_sb_in_progress = 0;
ajs9fc7ebf2005-02-23 15:12:34 +00001652 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001653
1654 if (! no_password_check)
1655 {
1656 /* Vty is not available if password isn't set. */
1657 if (host.password == NULL && host.password_encrypt == NULL)
1658 {
1659 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1660 vty->status = VTY_CLOSE;
1661 vty_close (vty);
1662 return NULL;
1663 }
1664 }
1665
1666 /* Say hello to the world. */
1667 vty_hello (vty);
1668 if (! no_password_check)
1669 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1670
1671 /* Setting up terminal. */
1672 vty_will_echo (vty);
1673 vty_will_suppress_go_ahead (vty);
1674
1675 vty_dont_linemode (vty);
1676 vty_do_window_size (vty);
1677 /* vty_dont_lflow_ahead (vty); */
1678
1679 vty_prompt (vty);
1680
1681 /* Add read/write thread. */
1682 vty_event (VTY_WRITE, vty_sock, vty);
1683 vty_event (VTY_READ, vty_sock, vty);
1684
1685 return vty;
1686}
1687
1688/* Accept connection from the network. */
1689static int
1690vty_accept (struct thread *thread)
1691{
1692 int vty_sock;
paul718e3742002-12-13 20:15:29 +00001693 union sockunion su;
1694 int ret;
1695 unsigned int on;
1696 int accept_sock;
1697 struct prefix *p = NULL;
1698 struct access_list *acl = NULL;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001699 char buf[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001700
1701 accept_sock = THREAD_FD (thread);
1702
1703 /* We continue hearing vty socket. */
1704 vty_event (VTY_SERV, accept_sock, NULL);
1705
1706 memset (&su, 0, sizeof (union sockunion));
1707
1708 /* We can handle IPv4 or IPv6 socket. */
1709 vty_sock = sockunion_accept (accept_sock, &su);
1710 if (vty_sock < 0)
1711 {
ajs6099b3b2004-11-20 02:06:59 +00001712 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001713 return -1;
1714 }
ajs9fc7ebf2005-02-23 15:12:34 +00001715 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001716
1717 p = sockunion2hostprefix (&su);
1718
1719 /* VTY's accesslist apply. */
1720 if (p->family == AF_INET && vty_accesslist_name)
1721 {
1722 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1723 (access_list_apply (acl, p) == FILTER_DENY))
1724 {
paul718e3742002-12-13 20:15:29 +00001725 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001726 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001727 close (vty_sock);
1728
1729 /* continue accepting connections */
1730 vty_event (VTY_SERV, accept_sock, NULL);
1731
1732 prefix_free (p);
1733
1734 return 0;
1735 }
1736 }
1737
1738#ifdef HAVE_IPV6
1739 /* VTY's ipv6 accesslist apply. */
1740 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1741 {
1742 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1743 (access_list_apply (acl, p) == FILTER_DENY))
1744 {
paul718e3742002-12-13 20:15:29 +00001745 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001746 sockunion2str (&su, buf, SU_ADDRSTRLEN));
paul718e3742002-12-13 20:15:29 +00001747 close (vty_sock);
1748
1749 /* continue accepting connections */
1750 vty_event (VTY_SERV, accept_sock, NULL);
1751
1752 prefix_free (p);
1753
1754 return 0;
1755 }
1756 }
1757#endif /* HAVE_IPV6 */
1758
1759 prefix_free (p);
1760
1761 on = 1;
1762 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1763 (char *) &on, sizeof (on));
1764 if (ret < 0)
1765 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001766 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001767
heasley78e6cd92009-12-07 16:41:14 +03001768 zlog (NULL, LOG_INFO, "Vty connection from %s",
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +02001769 sockunion2str (&su, buf, SU_ADDRSTRLEN));
heasley78e6cd92009-12-07 16:41:14 +03001770
Stephen Hemminger9206f9e2011-12-18 19:43:40 +04001771 vty_create (vty_sock, &su);
paul718e3742002-12-13 20:15:29 +00001772
1773 return 0;
1774}
1775
1776#if defined(HAVE_IPV6) && !defined(NRL)
ajs9fc7ebf2005-02-23 15:12:34 +00001777static void
paul718e3742002-12-13 20:15:29 +00001778vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1779{
1780 int ret;
1781 struct addrinfo req;
1782 struct addrinfo *ainfo;
1783 struct addrinfo *ainfo_save;
1784 int sock;
1785 char port_str[BUFSIZ];
1786
1787 memset (&req, 0, sizeof (struct addrinfo));
1788 req.ai_flags = AI_PASSIVE;
1789 req.ai_family = AF_UNSPEC;
1790 req.ai_socktype = SOCK_STREAM;
1791 sprintf (port_str, "%d", port);
1792 port_str[sizeof (port_str) - 1] = '\0';
1793
1794 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1795
1796 if (ret != 0)
1797 {
1798 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1799 exit (1);
1800 }
1801
1802 ainfo_save = ainfo;
1803
1804 do
1805 {
1806 if (ainfo->ai_family != AF_INET
1807#ifdef HAVE_IPV6
1808 && ainfo->ai_family != AF_INET6
1809#endif /* HAVE_IPV6 */
1810 )
1811 continue;
1812
1813 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1814 if (sock < 0)
1815 continue;
1816
David Lamparterca051262009-10-04 16:21:49 +02001817 sockopt_v6only (ainfo->ai_family, sock);
paul718e3742002-12-13 20:15:29 +00001818 sockopt_reuseaddr (sock);
1819 sockopt_reuseport (sock);
1820
1821 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1822 if (ret < 0)
1823 {
1824 close (sock); /* Avoid sd leak. */
1825 continue;
1826 }
1827
1828 ret = listen (sock, 3);
1829 if (ret < 0)
1830 {
1831 close (sock); /* Avoid sd leak. */
1832 continue;
1833 }
1834
1835 vty_event (VTY_SERV, sock, NULL);
1836 }
1837 while ((ainfo = ainfo->ai_next) != NULL);
1838
1839 freeaddrinfo (ainfo_save);
1840}
Stephen Hemminger46901402009-12-10 17:19:09 +00001841#else /* HAVE_IPV6 && ! NRL */
paul718e3742002-12-13 20:15:29 +00001842
1843/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001844static void
paul29db05b2003-05-08 20:10:22 +00001845vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001846{
1847 int ret;
1848 union sockunion su;
1849 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001850 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001851
1852 memset (&su, 0, sizeof (union sockunion));
1853 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001854 if(addr)
1855 switch(family)
1856 {
1857 case AF_INET:
1858 naddr=&su.sin.sin_addr;
1859#ifdef HAVE_IPV6
1860 case AF_INET6:
1861 naddr=&su.sin6.sin6_addr;
1862#endif
1863 }
1864
1865 if(naddr)
1866 switch(inet_pton(family,addr,naddr))
1867 {
1868 case -1:
1869 zlog_err("bad address %s",addr);
1870 naddr=NULL;
1871 break;
1872 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001873 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001874 naddr=NULL;
1875 }
paul718e3742002-12-13 20:15:29 +00001876
1877 /* Make new socket. */
1878 accept_sock = sockunion_stream_socket (&su);
1879 if (accept_sock < 0)
1880 return;
1881
1882 /* This is server, so reuse address. */
1883 sockopt_reuseaddr (accept_sock);
1884 sockopt_reuseport (accept_sock);
1885
1886 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001887 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001888 if (ret < 0)
1889 {
paul29db05b2003-05-08 20:10:22 +00001890 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001891 close (accept_sock); /* Avoid sd leak. */
1892 return;
1893 }
1894
1895 /* Listen socket under queue 3. */
1896 ret = listen (accept_sock, 3);
1897 if (ret < 0)
1898 {
1899 zlog (NULL, LOG_WARNING, "can't listen socket");
1900 close (accept_sock); /* Avoid sd leak. */
1901 return;
1902 }
1903
1904 /* Add vty server event. */
1905 vty_event (VTY_SERV, accept_sock, NULL);
1906}
Stephen Hemminger46901402009-12-10 17:19:09 +00001907#endif /* HAVE_IPV6 && ! NRL */
paul718e3742002-12-13 20:15:29 +00001908
1909#ifdef VTYSH
1910/* For sockaddr_un. */
1911#include <sys/un.h>
1912
1913/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001914static void
hasso6ad96ea2004-10-07 19:33:46 +00001915vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001916{
1917 int ret;
paul75e15fe2004-10-31 02:13:09 +00001918 int sock, len;
paul718e3742002-12-13 20:15:29 +00001919 struct sockaddr_un serv;
1920 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001921 struct zprivs_ids_t ids;
1922
paul718e3742002-12-13 20:15:29 +00001923 /* First of all, unlink existing socket */
1924 unlink (path);
1925
1926 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001927 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001928
1929 /* Make UNIX domain socket. */
1930 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1931 if (sock < 0)
1932 {
ajs6a52d0d2005-01-30 18:49:28 +00001933 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001934 return;
1935 }
1936
1937 /* Make server socket. */
1938 memset (&serv, 0, sizeof (struct sockaddr_un));
1939 serv.sun_family = AF_UNIX;
1940 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001941#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001942 len = serv.sun_len = SUN_LEN(&serv);
1943#else
1944 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001945#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001946
1947 ret = bind (sock, (struct sockaddr *) &serv, len);
1948 if (ret < 0)
1949 {
ajs6a52d0d2005-01-30 18:49:28 +00001950 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001951 close (sock); /* Avoid sd leak. */
1952 return;
1953 }
1954
1955 ret = listen (sock, 5);
1956 if (ret < 0)
1957 {
ajs6a52d0d2005-01-30 18:49:28 +00001958 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001959 close (sock); /* Avoid sd leak. */
1960 return;
1961 }
1962
1963 umask (old_mask);
1964
pauledd7c242003-06-04 13:59:38 +00001965 zprivs_get_ids(&ids);
1966
1967 if (ids.gid_vty > 0)
1968 {
1969 /* set group of socket */
1970 if ( chown (path, -1, ids.gid_vty) )
1971 {
1972 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00001973 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00001974 }
1975 }
1976
paul718e3742002-12-13 20:15:29 +00001977 vty_event (VTYSH_SERV, sock, NULL);
1978}
1979
1980/* #define VTYSH_DEBUG 1 */
1981
1982static int
1983vtysh_accept (struct thread *thread)
1984{
1985 int accept_sock;
1986 int sock;
1987 int client_len;
1988 struct sockaddr_un client;
1989 struct vty *vty;
1990
1991 accept_sock = THREAD_FD (thread);
1992
1993 vty_event (VTYSH_SERV, accept_sock, NULL);
1994
1995 memset (&client, 0, sizeof (struct sockaddr_un));
1996 client_len = sizeof (struct sockaddr_un);
1997
hassoe473b032004-09-26 16:08:11 +00001998 sock = accept (accept_sock, (struct sockaddr *) &client,
1999 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00002000
2001 if (sock < 0)
2002 {
ajs6099b3b2004-11-20 02:06:59 +00002003 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00002004 return -1;
2005 }
2006
ajs9fc7ebf2005-02-23 15:12:34 +00002007 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00002008 {
ajs9fc7ebf2005-02-23 15:12:34 +00002009 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
2010 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002011 close (sock);
2012 return -1;
2013 }
pauldccfb192004-10-29 08:29:36 +00002014
paul718e3742002-12-13 20:15:29 +00002015#ifdef VTYSH_DEBUG
2016 printf ("VTY shell accept\n");
2017#endif /* VTYSH_DEBUG */
2018
2019 vty = vty_new ();
2020 vty->fd = sock;
2021 vty->type = VTY_SHELL_SERV;
2022 vty->node = VIEW_NODE;
2023
2024 vty_event (VTYSH_READ, sock, vty);
2025
2026 return 0;
2027}
2028
2029static int
ajs9fc7ebf2005-02-23 15:12:34 +00002030vtysh_flush(struct vty *vty)
2031{
2032 switch (buffer_flush_available(vty->obuf, vty->fd))
2033 {
2034 case BUFFER_PENDING:
2035 vty_event(VTYSH_WRITE, vty->fd, vty);
2036 break;
2037 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002038 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002039 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2040 buffer_reset(vty->obuf);
2041 vty_close(vty);
2042 return -1;
2043 break;
2044 case BUFFER_EMPTY:
2045 break;
2046 }
2047 return 0;
2048}
2049
2050static int
paul718e3742002-12-13 20:15:29 +00002051vtysh_read (struct thread *thread)
2052{
2053 int ret;
2054 int sock;
2055 int nbytes;
2056 struct vty *vty;
2057 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002058 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002059 u_char header[4] = {0, 0, 0, 0};
2060
2061 sock = THREAD_FD (thread);
2062 vty = THREAD_ARG (thread);
2063 vty->t_read = NULL;
2064
ajs9fc7ebf2005-02-23 15:12:34 +00002065 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002066 {
ajs9fc7ebf2005-02-23 15:12:34 +00002067 if (nbytes < 0)
2068 {
2069 if (ERRNO_IO_RETRY(errno))
2070 {
2071 vty_event (VTYSH_READ, sock, vty);
2072 return 0;
2073 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002074 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002075 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2076 __func__, sock, safe_strerror(errno));
2077 }
2078 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002079 vty_close (vty);
2080#ifdef VTYSH_DEBUG
2081 printf ("close vtysh\n");
2082#endif /* VTYSH_DEBUG */
2083 return 0;
2084 }
2085
2086#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002087 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002088#endif /* VTYSH_DEBUG */
2089
ajs9fc7ebf2005-02-23 15:12:34 +00002090 for (p = buf; p < buf+nbytes; p++)
2091 {
2092 vty_ensure(vty, vty->length+1);
2093 vty->buf[vty->length++] = *p;
2094 if (*p == '\0')
2095 {
2096 /* Pass this line to parser. */
2097 ret = vty_execute (vty);
2098 /* Note that vty_execute clears the command buffer and resets
2099 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002100
ajs9fc7ebf2005-02-23 15:12:34 +00002101 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002102#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002103 printf ("result: %d\n", ret);
2104 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002105#endif /* VTYSH_DEBUG */
2106
ajs9fc7ebf2005-02-23 15:12:34 +00002107 header[3] = ret;
2108 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002109
ajs9fc7ebf2005-02-23 15:12:34 +00002110 if (!vty->t_write && (vtysh_flush(vty) < 0))
2111 /* Try to flush results; exit if a write error occurs. */
2112 return 0;
2113 }
2114 }
2115
paul718e3742002-12-13 20:15:29 +00002116 vty_event (VTYSH_READ, sock, vty);
2117
2118 return 0;
2119}
ajs49ff6d92004-11-04 19:26:16 +00002120
2121static int
2122vtysh_write (struct thread *thread)
2123{
2124 struct vty *vty = THREAD_ARG (thread);
2125
2126 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002127 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002128 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002129}
2130
paul718e3742002-12-13 20:15:29 +00002131#endif /* VTYSH */
2132
2133/* Determine address family to bind. */
2134void
hasso6ad96ea2004-10-07 19:33:46 +00002135vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002136{
2137 /* If port is set to 0, do not listen on TCP/IP at all! */
2138 if (port)
2139 {
2140
2141#ifdef HAVE_IPV6
2142#ifdef NRL
paul29db05b2003-05-08 20:10:22 +00002143 vty_serv_sock_family (addr, port, AF_INET);
2144 vty_serv_sock_family (addr, port, AF_INET6);
paul718e3742002-12-13 20:15:29 +00002145#else /* ! NRL */
paul29db05b2003-05-08 20:10:22 +00002146 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002147#endif /* NRL*/
2148#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002149 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002150#endif /* HAVE_IPV6 */
2151 }
2152
2153#ifdef VTYSH
2154 vty_serv_un (path);
2155#endif /* VTYSH */
2156}
2157
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002158/* Close vty interface. Warning: call this only from functions that
2159 will be careful not to access the vty afterwards (since it has
2160 now been freed). This is safest from top-level functions (called
2161 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002162void
2163vty_close (struct vty *vty)
2164{
2165 int i;
2166
2167 /* Cancel threads.*/
2168 if (vty->t_read)
2169 thread_cancel (vty->t_read);
2170 if (vty->t_write)
2171 thread_cancel (vty->t_write);
2172 if (vty->t_timeout)
2173 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002174
2175 /* Flush buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +00002176 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002177
2178 /* Free input buffer. */
2179 buffer_free (vty->obuf);
2180
paul718e3742002-12-13 20:15:29 +00002181 /* Free command history. */
2182 for (i = 0; i < VTY_MAXHIST; i++)
2183 if (vty->hist[i])
2184 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2185
2186 /* Unset vector. */
2187 vector_unset (vtyvec, vty->fd);
2188
2189 /* Close socket. */
2190 if (vty->fd > 0)
2191 close (vty->fd);
2192
paul718e3742002-12-13 20:15:29 +00002193 if (vty->buf)
2194 XFREE (MTYPE_VTY, vty->buf);
2195
2196 /* Check configure. */
2197 vty_config_unlock (vty);
2198
2199 /* OK free vty. */
2200 XFREE (MTYPE_VTY, vty);
2201}
2202
2203/* When time out occur output message then close connection. */
2204static int
2205vty_timeout (struct thread *thread)
2206{
2207 struct vty *vty;
2208
2209 vty = THREAD_ARG (thread);
2210 vty->t_timeout = NULL;
2211 vty->v_timeout = 0;
2212
2213 /* Clear buffer*/
2214 buffer_reset (vty->obuf);
2215 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2216
2217 /* Close connection. */
2218 vty->status = VTY_CLOSE;
2219 vty_close (vty);
2220
2221 return 0;
2222}
2223
2224/* Read up configuration file from file_name. */
2225static void
2226vty_read_file (FILE *confp)
2227{
2228 int ret;
2229 struct vty *vty;
2230
2231 vty = vty_new ();
2232 vty->fd = 0; /* stdout */
2233 vty->type = VTY_TERM;
2234 vty->node = CONFIG_NODE;
2235
2236 /* Execute configuration file */
2237 ret = config_from_file (vty, confp);
2238
paul7021c422003-07-15 12:52:22 +00002239 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002240 {
2241 switch (ret)
paul7021c422003-07-15 12:52:22 +00002242 {
2243 case CMD_ERR_AMBIGUOUS:
2244 fprintf (stderr, "Ambiguous command.\n");
2245 break;
2246 case CMD_ERR_NO_MATCH:
2247 fprintf (stderr, "There is no such command.\n");
2248 break;
2249 }
paul718e3742002-12-13 20:15:29 +00002250 fprintf (stderr, "Error occured during reading below line.\n%s\n",
2251 vty->buf);
2252 vty_close (vty);
2253 exit (1);
2254 }
2255
2256 vty_close (vty);
2257}
2258
ajs9fc7ebf2005-02-23 15:12:34 +00002259static FILE *
paul718e3742002-12-13 20:15:29 +00002260vty_use_backup_config (char *fullpath)
2261{
2262 char *fullpath_sav, *fullpath_tmp;
2263 FILE *ret = NULL;
2264 struct stat buf;
2265 int tmp, sav;
2266 int c;
2267 char buffer[512];
2268
2269 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2270 strcpy (fullpath_sav, fullpath);
2271 strcat (fullpath_sav, CONF_BACKUP_EXT);
2272 if (stat (fullpath_sav, &buf) == -1)
2273 {
2274 free (fullpath_sav);
2275 return NULL;
2276 }
2277
2278 fullpath_tmp = malloc (strlen (fullpath) + 8);
2279 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2280
2281 /* Open file to configuration write. */
2282 tmp = mkstemp (fullpath_tmp);
2283 if (tmp < 0)
2284 {
2285 free (fullpath_sav);
2286 free (fullpath_tmp);
2287 return NULL;
2288 }
2289
2290 sav = open (fullpath_sav, O_RDONLY);
2291 if (sav < 0)
2292 {
gdt3dbf9962003-12-22 20:18:18 +00002293 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002294 free (fullpath_sav);
2295 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002296 return NULL;
2297 }
2298
2299 while((c = read (sav, buffer, 512)) > 0)
2300 write (tmp, buffer, c);
2301
2302 close (sav);
2303 close (tmp);
2304
gdtaa593d52003-12-22 20:15:53 +00002305 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2306 {
gdt3dbf9962003-12-22 20:18:18 +00002307 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002308 free (fullpath_sav);
2309 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002310 return NULL;
2311 }
2312
paul718e3742002-12-13 20:15:29 +00002313 if (link (fullpath_tmp, fullpath) == 0)
2314 ret = fopen (fullpath, "r");
2315
2316 unlink (fullpath_tmp);
2317
2318 free (fullpath_sav);
2319 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002320 return ret;
paul718e3742002-12-13 20:15:29 +00002321}
2322
2323/* Read up configuration file from file_name. */
2324void
2325vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002326 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002327{
paulccc92352003-10-22 02:49:38 +00002328 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002329 FILE *confp = NULL;
2330 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002331 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002332
2333 /* If -f flag specified. */
2334 if (config_file != NULL)
2335 {
2336 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002337 {
2338 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002339 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002340 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002341 sprintf (tmp, "%s/%s", cwd, config_file);
2342 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002343 }
paul718e3742002-12-13 20:15:29 +00002344 else
hasso320ec102004-06-20 19:54:37 +00002345 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002346
2347 confp = fopen (fullpath, "r");
2348
2349 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002350 {
paul3d1dc852005-04-05 00:45:23 +00002351 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2352 __func__, fullpath, safe_strerror (errno));
2353
hasso320ec102004-06-20 19:54:37 +00002354 confp = vty_use_backup_config (fullpath);
2355 if (confp)
2356 fprintf (stderr, "WARNING: using backup configuration file!\n");
2357 else
2358 {
2359 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002360 config_file);
hasso320ec102004-06-20 19:54:37 +00002361 exit(1);
2362 }
2363 }
paul718e3742002-12-13 20:15:29 +00002364 }
2365 else
2366 {
paul718e3742002-12-13 20:15:29 +00002367#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002368 int ret;
2369 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002370
hasso320ec102004-06-20 19:54:37 +00002371 /* !!!!PLEASE LEAVE!!!!
2372 * This is NEEDED for use with vtysh -b, or else you can get
2373 * a real configuration food fight with a lot garbage in the
2374 * merged configuration file it creates coming from the per
2375 * daemon configuration files. This also allows the daemons
2376 * to start if there default configuration file is not
2377 * present or ignore them, as needed when using vtysh -b to
2378 * configure the daemons at boot - MAG
2379 */
paul718e3742002-12-13 20:15:29 +00002380
hasso320ec102004-06-20 19:54:37 +00002381 /* Stat for vtysh Zebra.conf, if found startup and wait for
2382 * boot configuration
2383 */
paul718e3742002-12-13 20:15:29 +00002384
hasso320ec102004-06-20 19:54:37 +00002385 if ( strstr(config_default_dir, "vtysh") == NULL)
2386 {
2387 ret = stat (integrate_default, &conf_stat);
2388 if (ret >= 0)
2389 return;
2390 }
paul718e3742002-12-13 20:15:29 +00002391#endif /* VTYSH */
2392
hasso320ec102004-06-20 19:54:37 +00002393 confp = fopen (config_default_dir, "r");
2394 if (confp == NULL)
2395 {
paul3d1dc852005-04-05 00:45:23 +00002396 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2397 __func__, config_default_dir, safe_strerror (errno));
2398
hasso320ec102004-06-20 19:54:37 +00002399 confp = vty_use_backup_config (config_default_dir);
2400 if (confp)
2401 {
2402 fprintf (stderr, "WARNING: using backup configuration file!\n");
2403 fullpath = config_default_dir;
2404 }
2405 else
2406 {
2407 fprintf (stderr, "can't open configuration file [%s]\n",
2408 config_default_dir);
2409 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002410 }
hasso320ec102004-06-20 19:54:37 +00002411 }
paul718e3742002-12-13 20:15:29 +00002412 else
hasso320ec102004-06-20 19:54:37 +00002413 fullpath = config_default_dir;
2414 }
2415
paul718e3742002-12-13 20:15:29 +00002416 vty_read_file (confp);
2417
2418 fclose (confp);
2419
2420 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002421
2422 if (tmp)
2423 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002424}
2425
2426/* Small utility function which output log to the VTY. */
2427void
ajs274a4a42004-12-07 15:39:31 +00002428vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002429 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002430{
hasso8c328f12004-10-05 21:01:23 +00002431 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002432 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002433
2434 if (!vtyvec)
2435 return;
paul718e3742002-12-13 20:15:29 +00002436
paul55468c82005-03-14 20:19:01 +00002437 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002438 if ((vty = vector_slot (vtyvec, i)) != NULL)
2439 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002440 {
2441 va_list ac;
2442 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002443 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002444 va_end(ac);
2445 }
paul718e3742002-12-13 20:15:29 +00002446}
2447
ajs274a4a42004-12-07 15:39:31 +00002448/* Async-signal-safe version of vty_log for fixed strings. */
2449void
2450vty_log_fixed (const char *buf, size_t len)
2451{
2452 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002453 struct iovec iov[2];
2454
Paul Jakmaa4b30302006-05-28 08:18:38 +00002455 /* vty may not have been initialised */
2456 if (!vtyvec)
2457 return;
2458
ajs926fe8f2005-04-08 18:50:40 +00002459 iov[0].iov_base = (void *)buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002460 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002461 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002462 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002463
paul55468c82005-03-14 20:19:01 +00002464 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002465 {
2466 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002467 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2468 /* N.B. We don't care about the return code, since process is
2469 most likely just about to die anyway. */
2470 writev(vty->fd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002471 }
2472}
2473
paul718e3742002-12-13 20:15:29 +00002474int
2475vty_config_lock (struct vty *vty)
2476{
2477 if (vty_config == 0)
2478 {
2479 vty->config = 1;
2480 vty_config = 1;
2481 }
2482 return vty->config;
2483}
2484
2485int
2486vty_config_unlock (struct vty *vty)
2487{
2488 if (vty_config == 1 && vty->config == 1)
2489 {
2490 vty->config = 0;
2491 vty_config = 0;
2492 }
2493 return vty->config;
2494}
2495
2496/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002497static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002498
2499static void
2500vty_event (enum event event, int sock, struct vty *vty)
2501{
2502 struct thread *vty_serv_thread;
2503
2504 switch (event)
2505 {
2506 case VTY_SERV:
2507 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2508 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2509 break;
2510#ifdef VTYSH
2511 case VTYSH_SERV:
Christian Franke677bcbb2013-02-27 13:47:23 +00002512 vty_serv_thread = thread_add_read (master, vtysh_accept, vty, sock);
2513 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
paul718e3742002-12-13 20:15:29 +00002514 break;
2515 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002516 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2517 break;
2518 case VTYSH_WRITE:
2519 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002520 break;
2521#endif /* VTYSH */
2522 case VTY_READ:
2523 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2524
2525 /* Time out treatment. */
2526 if (vty->v_timeout)
2527 {
2528 if (vty->t_timeout)
2529 thread_cancel (vty->t_timeout);
2530 vty->t_timeout =
2531 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2532 }
2533 break;
2534 case VTY_WRITE:
2535 if (! vty->t_write)
2536 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2537 break;
2538 case VTY_TIMEOUT_RESET:
2539 if (vty->t_timeout)
2540 {
2541 thread_cancel (vty->t_timeout);
2542 vty->t_timeout = NULL;
2543 }
2544 if (vty->v_timeout)
2545 {
2546 vty->t_timeout =
2547 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2548 }
2549 break;
2550 }
2551}
2552
2553DEFUN (config_who,
2554 config_who_cmd,
2555 "who",
2556 "Display who is on vty\n")
2557{
hasso8c328f12004-10-05 21:01:23 +00002558 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002559 struct vty *v;
2560
paul55468c82005-03-14 20:19:01 +00002561 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002562 if ((v = vector_slot (vtyvec, i)) != NULL)
2563 vty_out (vty, "%svty[%d] connected from %s.%s",
2564 v->config ? "*" : " ",
2565 i, v->address, VTY_NEWLINE);
2566 return CMD_SUCCESS;
2567}
2568
2569/* Move to vty configuration mode. */
2570DEFUN (line_vty,
2571 line_vty_cmd,
2572 "line vty",
2573 "Configure a terminal line\n"
2574 "Virtual terminal\n")
2575{
2576 vty->node = VTY_NODE;
2577 return CMD_SUCCESS;
2578}
2579
2580/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002581static int
paul9035efa2004-10-10 11:56:56 +00002582exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002583{
2584 unsigned long timeout = 0;
2585
2586 /* min_str and sec_str are already checked by parser. So it must be
2587 all digit string. */
2588 if (min_str)
2589 {
2590 timeout = strtol (min_str, NULL, 10);
2591 timeout *= 60;
2592 }
2593 if (sec_str)
2594 timeout += strtol (sec_str, NULL, 10);
2595
2596 vty_timeout_val = timeout;
2597 vty->v_timeout = timeout;
2598 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2599
2600
2601 return CMD_SUCCESS;
2602}
2603
2604DEFUN (exec_timeout_min,
2605 exec_timeout_min_cmd,
2606 "exec-timeout <0-35791>",
2607 "Set timeout value\n"
2608 "Timeout value in minutes\n")
2609{
2610 return exec_timeout (vty, argv[0], NULL);
2611}
2612
2613DEFUN (exec_timeout_sec,
2614 exec_timeout_sec_cmd,
2615 "exec-timeout <0-35791> <0-2147483>",
2616 "Set the EXEC timeout\n"
2617 "Timeout in minutes\n"
2618 "Timeout in seconds\n")
2619{
2620 return exec_timeout (vty, argv[0], argv[1]);
2621}
2622
2623DEFUN (no_exec_timeout,
2624 no_exec_timeout_cmd,
2625 "no exec-timeout",
2626 NO_STR
2627 "Set the EXEC timeout\n")
2628{
2629 return exec_timeout (vty, NULL, NULL);
2630}
2631
2632/* Set vty access class. */
2633DEFUN (vty_access_class,
2634 vty_access_class_cmd,
2635 "access-class WORD",
2636 "Filter connections based on an IP access list\n"
2637 "IP access list\n")
2638{
2639 if (vty_accesslist_name)
2640 XFREE(MTYPE_VTY, vty_accesslist_name);
2641
2642 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2643
2644 return CMD_SUCCESS;
2645}
2646
2647/* Clear vty access class. */
2648DEFUN (no_vty_access_class,
2649 no_vty_access_class_cmd,
2650 "no access-class [WORD]",
2651 NO_STR
2652 "Filter connections based on an IP access list\n"
2653 "IP access list\n")
2654{
2655 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2656 {
2657 vty_out (vty, "Access-class is not currently applied to vty%s",
2658 VTY_NEWLINE);
2659 return CMD_WARNING;
2660 }
2661
2662 XFREE(MTYPE_VTY, vty_accesslist_name);
2663
2664 vty_accesslist_name = NULL;
2665
2666 return CMD_SUCCESS;
2667}
2668
2669#ifdef HAVE_IPV6
2670/* Set vty access class. */
2671DEFUN (vty_ipv6_access_class,
2672 vty_ipv6_access_class_cmd,
2673 "ipv6 access-class WORD",
2674 IPV6_STR
2675 "Filter connections based on an IP access list\n"
2676 "IPv6 access list\n")
2677{
2678 if (vty_ipv6_accesslist_name)
2679 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2680
2681 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2682
2683 return CMD_SUCCESS;
2684}
2685
2686/* Clear vty access class. */
2687DEFUN (no_vty_ipv6_access_class,
2688 no_vty_ipv6_access_class_cmd,
2689 "no ipv6 access-class [WORD]",
2690 NO_STR
2691 IPV6_STR
2692 "Filter connections based on an IP access list\n"
2693 "IPv6 access list\n")
2694{
2695 if (! vty_ipv6_accesslist_name ||
2696 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2697 {
2698 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2699 VTY_NEWLINE);
2700 return CMD_WARNING;
2701 }
2702
2703 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2704
2705 vty_ipv6_accesslist_name = NULL;
2706
2707 return CMD_SUCCESS;
2708}
2709#endif /* HAVE_IPV6 */
2710
2711/* vty login. */
2712DEFUN (vty_login,
2713 vty_login_cmd,
2714 "login",
2715 "Enable password checking\n")
2716{
2717 no_password_check = 0;
2718 return CMD_SUCCESS;
2719}
2720
2721DEFUN (no_vty_login,
2722 no_vty_login_cmd,
2723 "no login",
2724 NO_STR
2725 "Enable password checking\n")
2726{
2727 no_password_check = 1;
2728 return CMD_SUCCESS;
2729}
2730
Paul Jakma62687ff2008-08-23 14:27:06 +01002731/* initial mode. */
2732DEFUN (vty_restricted_mode,
2733 vty_restricted_mode_cmd,
2734 "anonymous restricted",
2735 "Restrict view commands available in anonymous, unauthenticated vty\n")
2736{
2737 restricted_mode = 1;
2738 return CMD_SUCCESS;
2739}
2740
2741DEFUN (vty_no_restricted_mode,
2742 vty_no_restricted_mode_cmd,
2743 "no anonymous restricted",
2744 NO_STR
2745 "Enable password checking\n")
2746{
2747 restricted_mode = 0;
2748 return CMD_SUCCESS;
2749}
2750
paul718e3742002-12-13 20:15:29 +00002751DEFUN (service_advanced_vty,
2752 service_advanced_vty_cmd,
2753 "service advanced-vty",
2754 "Set up miscellaneous service\n"
2755 "Enable advanced mode vty interface\n")
2756{
2757 host.advanced = 1;
2758 return CMD_SUCCESS;
2759}
2760
2761DEFUN (no_service_advanced_vty,
2762 no_service_advanced_vty_cmd,
2763 "no service advanced-vty",
2764 NO_STR
2765 "Set up miscellaneous service\n"
2766 "Enable advanced mode vty interface\n")
2767{
2768 host.advanced = 0;
2769 return CMD_SUCCESS;
2770}
2771
2772DEFUN (terminal_monitor,
2773 terminal_monitor_cmd,
2774 "terminal monitor",
2775 "Set terminal line parameters\n"
2776 "Copy debug output to the current terminal line\n")
2777{
2778 vty->monitor = 1;
2779 return CMD_SUCCESS;
2780}
2781
2782DEFUN (terminal_no_monitor,
2783 terminal_no_monitor_cmd,
2784 "terminal no monitor",
2785 "Set terminal line parameters\n"
2786 NO_STR
2787 "Copy debug output to the current terminal line\n")
2788{
2789 vty->monitor = 0;
2790 return CMD_SUCCESS;
2791}
2792
paul789f78a2006-01-17 17:42:03 +00002793ALIAS (terminal_no_monitor,
2794 no_terminal_monitor_cmd,
2795 "no terminal monitor",
2796 NO_STR
2797 "Set terminal line parameters\n"
2798 "Copy debug output to the current terminal line\n")
2799
paul718e3742002-12-13 20:15:29 +00002800DEFUN (show_history,
2801 show_history_cmd,
2802 "show history",
2803 SHOW_STR
2804 "Display the session command history\n")
2805{
2806 int index;
2807
2808 for (index = vty->hindex + 1; index != vty->hindex;)
2809 {
2810 if (index == VTY_MAXHIST)
2811 {
2812 index = 0;
2813 continue;
2814 }
2815
2816 if (vty->hist[index] != NULL)
2817 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2818
2819 index++;
2820 }
2821
2822 return CMD_SUCCESS;
2823}
2824
2825/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002826static int
paul718e3742002-12-13 20:15:29 +00002827vty_config_write (struct vty *vty)
2828{
2829 vty_out (vty, "line vty%s", VTY_NEWLINE);
2830
2831 if (vty_accesslist_name)
2832 vty_out (vty, " access-class %s%s",
2833 vty_accesslist_name, VTY_NEWLINE);
2834
2835 if (vty_ipv6_accesslist_name)
2836 vty_out (vty, " ipv6 access-class %s%s",
2837 vty_ipv6_accesslist_name, VTY_NEWLINE);
2838
2839 /* exec-timeout */
2840 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2841 vty_out (vty, " exec-timeout %ld %ld%s",
2842 vty_timeout_val / 60,
2843 vty_timeout_val % 60, VTY_NEWLINE);
2844
2845 /* login */
2846 if (no_password_check)
2847 vty_out (vty, " no login%s", VTY_NEWLINE);
Paul Jakma62687ff2008-08-23 14:27:06 +01002848
2849 if (restricted_mode != restricted_mode_default)
2850 {
2851 if (restricted_mode_default)
2852 vty_out (vty, " no anonymous restricted%s", VTY_NEWLINE);
2853 else
2854 vty_out (vty, " anonymous restricted%s", VTY_NEWLINE);
2855 }
2856
paul718e3742002-12-13 20:15:29 +00002857 vty_out (vty, "!%s", VTY_NEWLINE);
2858
2859 return CMD_SUCCESS;
2860}
2861
2862struct cmd_node vty_node =
2863{
2864 VTY_NODE,
2865 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002866 1,
paul718e3742002-12-13 20:15:29 +00002867};
2868
2869/* Reset all VTY status. */
2870void
2871vty_reset ()
2872{
hasso8c328f12004-10-05 21:01:23 +00002873 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002874 struct vty *vty;
2875 struct thread *vty_serv_thread;
2876
paul55468c82005-03-14 20:19:01 +00002877 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002878 if ((vty = vector_slot (vtyvec, i)) != NULL)
2879 {
2880 buffer_reset (vty->obuf);
2881 vty->status = VTY_CLOSE;
2882 vty_close (vty);
2883 }
2884
paul55468c82005-03-14 20:19:01 +00002885 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002886 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2887 {
2888 thread_cancel (vty_serv_thread);
2889 vector_slot (Vvty_serv_thread, i) = NULL;
2890 close (i);
2891 }
2892
2893 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2894
2895 if (vty_accesslist_name)
2896 {
2897 XFREE(MTYPE_VTY, vty_accesslist_name);
2898 vty_accesslist_name = NULL;
2899 }
2900
2901 if (vty_ipv6_accesslist_name)
2902 {
2903 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2904 vty_ipv6_accesslist_name = NULL;
2905 }
2906}
2907
ajs9fc7ebf2005-02-23 15:12:34 +00002908static void
2909vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002910{
paul79ad2792003-10-15 22:09:28 +00002911 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002912 char *c;
paul718e3742002-12-13 20:15:29 +00002913
paulccc92352003-10-22 02:49:38 +00002914 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002915
paulccc92352003-10-22 02:49:38 +00002916 if (!c)
paul79ad2792003-10-15 22:09:28 +00002917 {
2918 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002919 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002920 }
paul718e3742002-12-13 20:15:29 +00002921
2922 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2923 strcpy (vty_cwd, cwd);
2924}
2925
2926char *
2927vty_get_cwd ()
2928{
2929 return vty_cwd;
2930}
2931
2932int
2933vty_shell (struct vty *vty)
2934{
2935 return vty->type == VTY_SHELL ? 1 : 0;
2936}
2937
2938int
2939vty_shell_serv (struct vty *vty)
2940{
2941 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2942}
2943
2944void
2945vty_init_vtysh ()
2946{
2947 vtyvec = vector_init (VECTOR_MIN_SIZE);
2948}
2949
2950/* Install vty's own commands like `who' command. */
2951void
paulb21b19c2003-06-15 01:28:29 +00002952vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00002953{
2954 /* For further configuration read, preserve current directory. */
2955 vty_save_cwd ();
2956
2957 vtyvec = vector_init (VECTOR_MIN_SIZE);
2958
paulb21b19c2003-06-15 01:28:29 +00002959 master = master_thread;
2960
paul718e3742002-12-13 20:15:29 +00002961 /* Initilize server thread vector. */
2962 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
2963
2964 /* Install bgp top node. */
2965 install_node (&vty_node, vty_config_write);
2966
Paul Jakma62687ff2008-08-23 14:27:06 +01002967 install_element (RESTRICTED_NODE, &config_who_cmd);
2968 install_element (RESTRICTED_NODE, &show_history_cmd);
paul718e3742002-12-13 20:15:29 +00002969 install_element (VIEW_NODE, &config_who_cmd);
2970 install_element (VIEW_NODE, &show_history_cmd);
2971 install_element (ENABLE_NODE, &config_who_cmd);
2972 install_element (CONFIG_NODE, &line_vty_cmd);
2973 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
2974 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
2975 install_element (CONFIG_NODE, &show_history_cmd);
2976 install_element (ENABLE_NODE, &terminal_monitor_cmd);
2977 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00002978 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00002979 install_element (ENABLE_NODE, &show_history_cmd);
2980
2981 install_default (VTY_NODE);
2982 install_element (VTY_NODE, &exec_timeout_min_cmd);
2983 install_element (VTY_NODE, &exec_timeout_sec_cmd);
2984 install_element (VTY_NODE, &no_exec_timeout_cmd);
2985 install_element (VTY_NODE, &vty_access_class_cmd);
2986 install_element (VTY_NODE, &no_vty_access_class_cmd);
2987 install_element (VTY_NODE, &vty_login_cmd);
2988 install_element (VTY_NODE, &no_vty_login_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01002989 install_element (VTY_NODE, &vty_restricted_mode_cmd);
2990 install_element (VTY_NODE, &vty_no_restricted_mode_cmd);
paul718e3742002-12-13 20:15:29 +00002991#ifdef HAVE_IPV6
2992 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
2993 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
2994#endif /* HAVE_IPV6 */
2995}
Chris Caputo228da422009-07-18 05:44:03 +00002996
2997void
2998vty_terminate (void)
2999{
3000 if (vty_cwd)
3001 XFREE (MTYPE_TMP, vty_cwd);
3002
3003 if (vtyvec && Vvty_serv_thread)
3004 {
3005 vty_reset ();
3006 vector_free (vtyvec);
3007 vector_free (Vvty_serv_thread);
3008 }
3009}