blob: ccf6640690957345ea20be785f6eee9a8d228694 [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. */
74vector Vvty_serv_thread;
75
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
85/* Integrated configuration file path */
86char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
87
88
89/* VTY standard output function. */
90int
91vty_out (struct vty *vty, const char *format, ...)
92{
93 va_list args;
94 int len = 0;
95 int size = 1024;
96 char buf[1024];
97 char *p = NULL;
paul718e3742002-12-13 20:15:29 +000098
99 if (vty_shell (vty))
ajsd246bd92004-11-23 17:35:08 +0000100 {
101 va_start (args, format);
102 vprintf (format, args);
103 va_end (args);
104 }
paul718e3742002-12-13 20:15:29 +0000105 else
106 {
107 /* Try to write to initial buffer. */
ajsd246bd92004-11-23 17:35:08 +0000108 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000109 len = vsnprintf (buf, sizeof buf, format, args);
ajsd246bd92004-11-23 17:35:08 +0000110 va_end (args);
paul718e3742002-12-13 20:15:29 +0000111
112 /* Initial buffer is not enough. */
113 if (len < 0 || len >= size)
114 {
115 while (1)
116 {
117 if (len > -1)
118 size = len + 1;
119 else
120 size = size * 2;
121
122 p = XREALLOC (MTYPE_VTY_OUT_BUF, p, size);
123 if (! p)
124 return -1;
125
ajsd246bd92004-11-23 17:35:08 +0000126 va_start (args, format);
paul718e3742002-12-13 20:15:29 +0000127 len = vsnprintf (p, size, format, args);
ajsd246bd92004-11-23 17:35:08 +0000128 va_end (args);
paul718e3742002-12-13 20:15:29 +0000129
130 if (len > -1 && len < size)
131 break;
132 }
133 }
134
135 /* When initial buffer is enough to store all output. */
136 if (! p)
137 p = buf;
138
139 /* Pointer p must point out buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +0000140 buffer_put (vty->obuf, (u_char *) p, len);
paul718e3742002-12-13 20:15:29 +0000141
142 /* If p is not different with buf, it is allocated buffer. */
143 if (p != buf)
144 XFREE (MTYPE_VTY_OUT_BUF, p);
145 }
146
paul718e3742002-12-13 20:15:29 +0000147 return len;
148}
149
ajsd246bd92004-11-23 17:35:08 +0000150static int
ajs274a4a42004-12-07 15:39:31 +0000151vty_log_out (struct vty *vty, const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000152 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +0000153{
ajs9fc7ebf2005-02-23 15:12:34 +0000154 int ret;
paul718e3742002-12-13 20:15:29 +0000155 int len;
156 char buf[1024];
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000157
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000158 if (!ctl->already_rendered)
159 {
160 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
161 ctl->already_rendered = 1;
162 }
163 if (ctl->len+1 >= sizeof(buf))
164 return -1;
165 memcpy(buf, ctl->buf, len = ctl->len);
166 buf[len++] = ' ';
167 buf[len] = '\0';
paul718e3742002-12-13 20:15:29 +0000168
ajs274a4a42004-12-07 15:39:31 +0000169 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000170 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000171 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000172 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
173 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000174 return -1;
paul718e3742002-12-13 20:15:29 +0000175
ajs9fc7ebf2005-02-23 15:12:34 +0000176 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
177 ((size_t)((len += ret)+2) > sizeof(buf)))
178 return -1;
paul718e3742002-12-13 20:15:29 +0000179
ajs9fc7ebf2005-02-23 15:12:34 +0000180 buf[len++] = '\r';
181 buf[len++] = '\n';
182
183 if (write(vty->fd, buf, len) < 0)
184 {
185 if (ERRNO_IO_RETRY(errno))
186 /* Kernel buffer is full, probably too much debugging output, so just
187 drop the data and ignore. */
188 return -1;
189 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000190 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000191 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
192 __func__, vty->fd, safe_strerror(errno));
193 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000194 /* cannot call vty_close, because a parent routine may still try
195 to access the vty struct */
196 vty->status = VTY_CLOSE;
197 shutdown(vty->fd, SHUT_RDWR);
ajs9fc7ebf2005-02-23 15:12:34 +0000198 return -1;
199 }
200 return 0;
paul718e3742002-12-13 20:15:29 +0000201}
202
203/* Output current time to the vty. */
204void
205vty_time_print (struct vty *vty, int cr)
206{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000207 char buf [25];
paul718e3742002-12-13 20:15:29 +0000208
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000209 if (quagga_timestamp(0, buf, sizeof(buf)) == 0)
paul718e3742002-12-13 20:15:29 +0000210 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000211 zlog (NULL, LOG_INFO, "quagga_timestamp error");
paul718e3742002-12-13 20:15:29 +0000212 return;
213 }
214 if (cr)
215 vty_out (vty, "%s\n", buf);
216 else
217 vty_out (vty, "%s ", buf);
218
219 return;
220}
221
222/* Say hello to vty interface. */
223void
224vty_hello (struct vty *vty)
225{
paul3b0c5d92005-03-08 10:43:43 +0000226 if (host.motdfile)
227 {
228 FILE *f;
229 char buf[4096];
paul22085182005-03-08 16:00:12 +0000230
paul3b0c5d92005-03-08 10:43:43 +0000231 f = fopen (host.motdfile, "r");
232 if (f)
233 {
paulb45da6f2005-03-08 15:16:57 +0000234 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000235 {
paulb45da6f2005-03-08 15:16:57 +0000236 char *s;
paul22085182005-03-08 16:00:12 +0000237 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000238 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000239 s--);
240 *s = '\0';
241 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
242 }
paul3b0c5d92005-03-08 10:43:43 +0000243 fclose (f);
244 }
245 else
paulb45da6f2005-03-08 15:16:57 +0000246 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000247 }
248 else if (host.motd)
paul718e3742002-12-13 20:15:29 +0000249 vty_out (vty, host.motd);
250}
251
252/* Put out prompt and wait input from user. */
253static void
254vty_prompt (struct vty *vty)
255{
256 struct utsname names;
257 const char*hostname;
258
259 if (vty->type == VTY_TERM)
260 {
261 hostname = host.name;
262 if (!hostname)
263 {
264 uname (&names);
265 hostname = names.nodename;
266 }
267 vty_out (vty, cmd_prompt (vty->node), hostname);
268 }
269}
270
271/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000272static void
paul718e3742002-12-13 20:15:29 +0000273vty_will_echo (struct vty *vty)
274{
paul02ff83c2004-06-11 11:27:03 +0000275 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000276 vty_out (vty, "%s", cmd);
277}
278
279/* Make suppress Go-Ahead telnet option. */
280static void
281vty_will_suppress_go_ahead (struct vty *vty)
282{
paul02ff83c2004-06-11 11:27:03 +0000283 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000284 vty_out (vty, "%s", cmd);
285}
286
287/* Make don't use linemode over telnet. */
288static void
289vty_dont_linemode (struct vty *vty)
290{
paul02ff83c2004-06-11 11:27:03 +0000291 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000292 vty_out (vty, "%s", cmd);
293}
294
295/* Use window size. */
296static void
297vty_do_window_size (struct vty *vty)
298{
paul02ff83c2004-06-11 11:27:03 +0000299 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000300 vty_out (vty, "%s", cmd);
301}
302
303#if 0 /* Currently not used. */
304/* Make don't use lflow vty interface. */
305static void
306vty_dont_lflow_ahead (struct vty *vty)
307{
paul02ff83c2004-06-11 11:27:03 +0000308 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000309 vty_out (vty, "%s", cmd);
310}
311#endif /* 0 */
312
313/* Allocate new vty struct. */
314struct vty *
315vty_new ()
316{
317 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
318
ajs9fc7ebf2005-02-23 15:12:34 +0000319 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000320 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
321 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000322
323 return new;
324}
325
326/* Authentication of vty */
327static void
328vty_auth (struct vty *vty, char *buf)
329{
330 char *passwd = NULL;
331 enum node_type next_node = 0;
332 int fail;
333 char *crypt (const char *, const char *);
334
335 switch (vty->node)
336 {
337 case AUTH_NODE:
338 if (host.encrypt)
339 passwd = host.password_encrypt;
340 else
341 passwd = host.password;
342 if (host.advanced)
343 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
344 else
345 next_node = VIEW_NODE;
346 break;
347 case AUTH_ENABLE_NODE:
348 if (host.encrypt)
349 passwd = host.enable_encrypt;
350 else
351 passwd = host.enable;
352 next_node = ENABLE_NODE;
353 break;
354 }
355
356 if (passwd)
357 {
358 if (host.encrypt)
359 fail = strcmp (crypt(buf, passwd), passwd);
360 else
361 fail = strcmp (buf, passwd);
362 }
363 else
364 fail = 1;
365
366 if (! fail)
367 {
368 vty->fail = 0;
369 vty->node = next_node; /* Success ! */
370 }
371 else
372 {
373 vty->fail++;
374 if (vty->fail >= 3)
375 {
376 if (vty->node == AUTH_NODE)
377 {
378 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
379 vty->status = VTY_CLOSE;
380 }
381 else
382 {
383 /* AUTH_ENABLE_NODE */
384 vty->fail = 0;
385 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
386 vty->node = VIEW_NODE;
387 }
388 }
389 }
390}
391
392/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000393static int
paul718e3742002-12-13 20:15:29 +0000394vty_command (struct vty *vty, char *buf)
395{
396 int ret;
397 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000398 const char *protocolname;
paul718e3742002-12-13 20:15:29 +0000399
400 /* Split readline string up into the vector */
401 vline = cmd_make_strvec (buf);
402
403 if (vline == NULL)
404 return CMD_SUCCESS;
405
ajs924b9222005-04-16 17:11:24 +0000406#ifdef CONSUMED_TIME_CHECK
407 {
408 RUSAGE_T before;
409 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000410 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000411
412 GETRUSAGE(&before);
413#endif /* CONSUMED_TIME_CHECK */
414
hasso87d683b2005-01-16 23:31:54 +0000415 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000416
vincentfbf5d032005-09-29 11:25:50 +0000417 /* Get the name of the protocol if any */
418 if (zlog_default)
419 protocolname = zlog_proto_names[zlog_default->protocol];
420 else
421 protocolname = zlog_proto_names[ZLOG_NONE];
422
ajs924b9222005-04-16 17:11:24 +0000423#ifdef CONSUMED_TIME_CHECK
424 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000425 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
426 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000427 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000428 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
429 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000430 }
431#endif /* CONSUMED_TIME_CHECK */
432
paul718e3742002-12-13 20:15:29 +0000433 if (ret != CMD_SUCCESS)
434 switch (ret)
435 {
436 case CMD_WARNING:
437 if (vty->type == VTY_FILE)
438 vty_out (vty, "Warning...%s", VTY_NEWLINE);
439 break;
440 case CMD_ERR_AMBIGUOUS:
441 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
442 break;
443 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000444 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000445 break;
446 case CMD_ERR_INCOMPLETE:
447 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
448 break;
449 }
450 cmd_free_strvec (vline);
451
452 return ret;
453}
454
ajs9fc7ebf2005-02-23 15:12:34 +0000455static const char telnet_backward_char = 0x08;
456static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000457
458/* Basic function to write buffer to vty. */
459static void
ajs9fc7ebf2005-02-23 15:12:34 +0000460vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000461{
462 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
463 return;
464
465 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000466 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000467}
468
469/* Ensure length of input buffer. Is buffer is short, double it. */
470static void
471vty_ensure (struct vty *vty, int length)
472{
473 if (vty->max <= length)
474 {
475 vty->max *= 2;
476 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
477 }
478}
479
480/* Basic function to insert character into vty. */
481static void
482vty_self_insert (struct vty *vty, char c)
483{
484 int i;
485 int length;
486
487 vty_ensure (vty, vty->length + 1);
488 length = vty->length - vty->cp;
489 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
490 vty->buf[vty->cp] = c;
491
492 vty_write (vty, &vty->buf[vty->cp], length + 1);
493 for (i = 0; i < length; i++)
494 vty_write (vty, &telnet_backward_char, 1);
495
496 vty->cp++;
497 vty->length++;
498}
499
500/* Self insert character 'c' in overwrite mode. */
501static void
502vty_self_insert_overwrite (struct vty *vty, char c)
503{
504 vty_ensure (vty, vty->length + 1);
505 vty->buf[vty->cp++] = c;
506
507 if (vty->cp > vty->length)
508 vty->length++;
509
510 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
511 return;
512
513 vty_write (vty, &c, 1);
514}
515
516/* Insert a word into vty interface with overwrite mode. */
517static void
518vty_insert_word_overwrite (struct vty *vty, char *str)
519{
520 int len = strlen (str);
521 vty_write (vty, str, len);
522 strcpy (&vty->buf[vty->cp], str);
523 vty->cp += len;
524 vty->length = vty->cp;
525}
526
527/* Forward character. */
528static void
529vty_forward_char (struct vty *vty)
530{
531 if (vty->cp < vty->length)
532 {
533 vty_write (vty, &vty->buf[vty->cp], 1);
534 vty->cp++;
535 }
536}
537
538/* Backward character. */
539static void
540vty_backward_char (struct vty *vty)
541{
542 if (vty->cp > 0)
543 {
544 vty->cp--;
545 vty_write (vty, &telnet_backward_char, 1);
546 }
547}
548
549/* Move to the beginning of the line. */
550static void
551vty_beginning_of_line (struct vty *vty)
552{
553 while (vty->cp)
554 vty_backward_char (vty);
555}
556
557/* Move to the end of the line. */
558static void
559vty_end_of_line (struct vty *vty)
560{
561 while (vty->cp < vty->length)
562 vty_forward_char (vty);
563}
564
565static void vty_kill_line_from_beginning (struct vty *);
566static void vty_redraw_line (struct vty *);
567
568/* Print command line history. This function is called from
569 vty_next_line and vty_previous_line. */
570static void
571vty_history_print (struct vty *vty)
572{
573 int length;
574
575 vty_kill_line_from_beginning (vty);
576
577 /* Get previous line from history buffer */
578 length = strlen (vty->hist[vty->hp]);
579 memcpy (vty->buf, vty->hist[vty->hp], length);
580 vty->cp = vty->length = length;
581
582 /* Redraw current line */
583 vty_redraw_line (vty);
584}
585
586/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000587static void
paul718e3742002-12-13 20:15:29 +0000588vty_next_line (struct vty *vty)
589{
590 int try_index;
591
592 if (vty->hp == vty->hindex)
593 return;
594
595 /* Try is there history exist or not. */
596 try_index = vty->hp;
597 if (try_index == (VTY_MAXHIST - 1))
598 try_index = 0;
599 else
600 try_index++;
601
602 /* If there is not history return. */
603 if (vty->hist[try_index] == NULL)
604 return;
605 else
606 vty->hp = try_index;
607
608 vty_history_print (vty);
609}
610
611/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000612static void
paul718e3742002-12-13 20:15:29 +0000613vty_previous_line (struct vty *vty)
614{
615 int try_index;
616
617 try_index = vty->hp;
618 if (try_index == 0)
619 try_index = VTY_MAXHIST - 1;
620 else
621 try_index--;
622
623 if (vty->hist[try_index] == NULL)
624 return;
625 else
626 vty->hp = try_index;
627
628 vty_history_print (vty);
629}
630
631/* This function redraw all of the command line character. */
632static void
633vty_redraw_line (struct vty *vty)
634{
635 vty_write (vty, vty->buf, vty->length);
636 vty->cp = vty->length;
637}
638
639/* Forward word. */
640static void
641vty_forward_word (struct vty *vty)
642{
643 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
644 vty_forward_char (vty);
645
646 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
647 vty_forward_char (vty);
648}
649
650/* Backward word without skipping training space. */
651static void
652vty_backward_pure_word (struct vty *vty)
653{
654 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
655 vty_backward_char (vty);
656}
657
658/* Backward word. */
659static void
660vty_backward_word (struct vty *vty)
661{
662 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
663 vty_backward_char (vty);
664
665 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
666 vty_backward_char (vty);
667}
668
669/* When '^D' is typed at the beginning of the line we move to the down
670 level. */
671static void
672vty_down_level (struct vty *vty)
673{
674 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000675 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000676 vty_prompt (vty);
677 vty->cp = 0;
678}
679
680/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000681static void
paul718e3742002-12-13 20:15:29 +0000682vty_end_config (struct vty *vty)
683{
684 vty_out (vty, "%s", VTY_NEWLINE);
685
686 switch (vty->node)
687 {
688 case VIEW_NODE:
689 case ENABLE_NODE:
690 /* Nothing to do. */
691 break;
692 case CONFIG_NODE:
693 case INTERFACE_NODE:
694 case ZEBRA_NODE:
695 case RIP_NODE:
696 case RIPNG_NODE:
697 case BGP_NODE:
698 case BGP_VPNV4_NODE:
699 case BGP_IPV4_NODE:
700 case BGP_IPV4M_NODE:
701 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000702 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000703 case RMAP_NODE:
704 case OSPF_NODE:
705 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000706 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000707 case KEYCHAIN_NODE:
708 case KEYCHAIN_KEY_NODE:
709 case MASC_NODE:
710 case VTY_NODE:
711 vty_config_unlock (vty);
712 vty->node = ENABLE_NODE;
713 break;
714 default:
715 /* Unknown node, we have to ignore it. */
716 break;
717 }
718
719 vty_prompt (vty);
720 vty->cp = 0;
721}
722
723/* Delete a charcter at the current point. */
724static void
725vty_delete_char (struct vty *vty)
726{
727 int i;
728 int size;
729
730 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
731 return;
732
733 if (vty->length == 0)
734 {
735 vty_down_level (vty);
736 return;
737 }
738
739 if (vty->cp == vty->length)
740 return; /* completion need here? */
741
742 size = vty->length - vty->cp;
743
744 vty->length--;
745 memmove (&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
746 vty->buf[vty->length] = '\0';
747
748 vty_write (vty, &vty->buf[vty->cp], size - 1);
749 vty_write (vty, &telnet_space_char, 1);
750
751 for (i = 0; i < size; i++)
752 vty_write (vty, &telnet_backward_char, 1);
753}
754
755/* Delete a character before the point. */
756static void
757vty_delete_backward_char (struct vty *vty)
758{
759 if (vty->cp == 0)
760 return;
761
762 vty_backward_char (vty);
763 vty_delete_char (vty);
764}
765
766/* Kill rest of line from current point. */
767static void
768vty_kill_line (struct vty *vty)
769{
770 int i;
771 int size;
772
773 size = vty->length - vty->cp;
774
775 if (size == 0)
776 return;
777
778 for (i = 0; i < size; i++)
779 vty_write (vty, &telnet_space_char, 1);
780 for (i = 0; i < size; i++)
781 vty_write (vty, &telnet_backward_char, 1);
782
783 memset (&vty->buf[vty->cp], 0, size);
784 vty->length = vty->cp;
785}
786
787/* Kill line from the beginning. */
788static void
789vty_kill_line_from_beginning (struct vty *vty)
790{
791 vty_beginning_of_line (vty);
792 vty_kill_line (vty);
793}
794
795/* Delete a word before the point. */
796static void
797vty_forward_kill_word (struct vty *vty)
798{
799 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
800 vty_delete_char (vty);
801 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
802 vty_delete_char (vty);
803}
804
805/* Delete a word before the point. */
806static void
807vty_backward_kill_word (struct vty *vty)
808{
809 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
810 vty_delete_backward_char (vty);
811 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
812 vty_delete_backward_char (vty);
813}
814
815/* Transpose chars before or at the point. */
816static void
817vty_transpose_chars (struct vty *vty)
818{
819 char c1, c2;
820
821 /* If length is short or point is near by the beginning of line then
822 return. */
823 if (vty->length < 2 || vty->cp < 1)
824 return;
825
826 /* In case of point is located at the end of the line. */
827 if (vty->cp == vty->length)
828 {
829 c1 = vty->buf[vty->cp - 1];
830 c2 = vty->buf[vty->cp - 2];
831
832 vty_backward_char (vty);
833 vty_backward_char (vty);
834 vty_self_insert_overwrite (vty, c1);
835 vty_self_insert_overwrite (vty, c2);
836 }
837 else
838 {
839 c1 = vty->buf[vty->cp];
840 c2 = vty->buf[vty->cp - 1];
841
842 vty_backward_char (vty);
843 vty_self_insert_overwrite (vty, c1);
844 vty_self_insert_overwrite (vty, c2);
845 }
846}
847
848/* Do completion at vty interface. */
849static void
850vty_complete_command (struct vty *vty)
851{
852 int i;
853 int ret;
854 char **matched = NULL;
855 vector vline;
856
857 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
858 return;
859
860 vline = cmd_make_strvec (vty->buf);
861 if (vline == NULL)
862 return;
863
864 /* In case of 'help \t'. */
865 if (isspace ((int) vty->buf[vty->length - 1]))
866 vector_set (vline, '\0');
867
868 matched = cmd_complete_command (vline, vty, &ret);
869
870 cmd_free_strvec (vline);
871
872 vty_out (vty, "%s", VTY_NEWLINE);
873 switch (ret)
874 {
875 case CMD_ERR_AMBIGUOUS:
876 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
877 vty_prompt (vty);
878 vty_redraw_line (vty);
879 break;
880 case CMD_ERR_NO_MATCH:
881 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
882 vty_prompt (vty);
883 vty_redraw_line (vty);
884 break;
885 case CMD_COMPLETE_FULL_MATCH:
886 vty_prompt (vty);
887 vty_redraw_line (vty);
888 vty_backward_pure_word (vty);
889 vty_insert_word_overwrite (vty, matched[0]);
890 vty_self_insert (vty, ' ');
891 XFREE (MTYPE_TMP, matched[0]);
892 break;
893 case CMD_COMPLETE_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 XFREE (MTYPE_TMP, matched[0]);
899 vector_only_index_free (matched);
900 return;
901 break;
902 case CMD_COMPLETE_LIST_MATCH:
903 for (i = 0; matched[i] != NULL; i++)
904 {
905 if (i != 0 && ((i % 6) == 0))
906 vty_out (vty, "%s", VTY_NEWLINE);
907 vty_out (vty, "%-10s ", matched[i]);
908 XFREE (MTYPE_TMP, matched[i]);
909 }
910 vty_out (vty, "%s", VTY_NEWLINE);
911
912 vty_prompt (vty);
913 vty_redraw_line (vty);
914 break;
915 case CMD_ERR_NOTHING_TODO:
916 vty_prompt (vty);
917 vty_redraw_line (vty);
918 break;
919 default:
920 break;
921 }
922 if (matched)
923 vector_only_index_free (matched);
924}
925
ajs9fc7ebf2005-02-23 15:12:34 +0000926static void
paul718e3742002-12-13 20:15:29 +0000927vty_describe_fold (struct vty *vty, int cmd_width,
hasso8c328f12004-10-05 21:01:23 +0000928 unsigned int desc_width, struct desc *desc)
paul718e3742002-12-13 20:15:29 +0000929{
hasso8c328f12004-10-05 21:01:23 +0000930 char *buf;
931 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000932 int pos;
933
934 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
935
936 if (desc_width <= 0)
937 {
938 vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
939 return;
940 }
941
942 buf = XCALLOC (MTYPE_TMP, strlen (desc->str) + 1);
943
944 for (p = desc->str; strlen (p) > desc_width; p += pos + 1)
945 {
946 for (pos = desc_width; pos > 0; pos--)
947 if (*(p + pos) == ' ')
948 break;
949
950 if (pos == 0)
951 break;
952
953 strncpy (buf, p, pos);
954 buf[pos] = '\0';
955 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
956
957 cmd = "";
958 }
959
960 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
961
962 XFREE (MTYPE_TMP, buf);
963}
964
965/* Describe matched command function. */
966static void
967vty_describe_command (struct vty *vty)
968{
969 int ret;
970 vector vline;
971 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000972 unsigned int i, width, desc_width;
paul718e3742002-12-13 20:15:29 +0000973 struct desc *desc, *desc_cr = NULL;
974
975 vline = cmd_make_strvec (vty->buf);
976
977 /* In case of '> ?'. */
978 if (vline == NULL)
979 {
980 vline = vector_init (1);
981 vector_set (vline, '\0');
982 }
983 else
984 if (isspace ((int) vty->buf[vty->length - 1]))
985 vector_set (vline, '\0');
986
987 describe = cmd_describe_command (vline, vty, &ret);
988
989 vty_out (vty, "%s", VTY_NEWLINE);
990
991 /* Ambiguous error. */
992 switch (ret)
993 {
994 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +0000995 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +0000996 goto out;
paul718e3742002-12-13 20:15:29 +0000997 break;
998 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +0000999 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001000 goto out;
paul718e3742002-12-13 20:15:29 +00001001 break;
1002 }
1003
1004 /* Get width of command string. */
1005 width = 0;
paul55468c82005-03-14 20:19:01 +00001006 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001007 if ((desc = vector_slot (describe, i)) != NULL)
1008 {
hasso8c328f12004-10-05 21:01:23 +00001009 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001010
1011 if (desc->cmd[0] == '\0')
1012 continue;
1013
1014 len = strlen (desc->cmd);
1015 if (desc->cmd[0] == '.')
1016 len--;
1017
1018 if (width < len)
1019 width = len;
1020 }
1021
1022 /* Get width of description string. */
1023 desc_width = vty->width - (width + 6);
1024
1025 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001026 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001027 if ((desc = vector_slot (describe, i)) != NULL)
1028 {
1029 if (desc->cmd[0] == '\0')
1030 continue;
1031
1032 if (strcmp (desc->cmd, "<cr>") == 0)
1033 {
1034 desc_cr = desc;
1035 continue;
1036 }
1037
1038 if (!desc->str)
1039 vty_out (vty, " %-s%s",
1040 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1041 VTY_NEWLINE);
1042 else if (desc_width >= strlen (desc->str))
1043 vty_out (vty, " %-*s %s%s", width,
1044 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1045 desc->str, VTY_NEWLINE);
1046 else
1047 vty_describe_fold (vty, width, desc_width, desc);
1048
1049#if 0
1050 vty_out (vty, " %-*s %s%s", width
1051 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1052 desc->str ? desc->str : "", VTY_NEWLINE);
1053#endif /* 0 */
1054 }
1055
1056 if ((desc = desc_cr))
1057 {
1058 if (!desc->str)
1059 vty_out (vty, " %-s%s",
1060 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1061 VTY_NEWLINE);
1062 else if (desc_width >= strlen (desc->str))
1063 vty_out (vty, " %-*s %s%s", width,
1064 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1065 desc->str, VTY_NEWLINE);
1066 else
1067 vty_describe_fold (vty, width, desc_width, desc);
1068 }
1069
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001070out:
paul718e3742002-12-13 20:15:29 +00001071 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001072 if (describe)
1073 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001074
1075 vty_prompt (vty);
1076 vty_redraw_line (vty);
1077}
1078
ajs9fc7ebf2005-02-23 15:12:34 +00001079static void
paul718e3742002-12-13 20:15:29 +00001080vty_clear_buf (struct vty *vty)
1081{
1082 memset (vty->buf, 0, vty->max);
1083}
1084
1085/* ^C stop current input and do not add command line to the history. */
1086static void
1087vty_stop_input (struct vty *vty)
1088{
1089 vty->cp = vty->length = 0;
1090 vty_clear_buf (vty);
1091 vty_out (vty, "%s", VTY_NEWLINE);
1092
1093 switch (vty->node)
1094 {
1095 case VIEW_NODE:
1096 case ENABLE_NODE:
1097 /* Nothing to do. */
1098 break;
1099 case CONFIG_NODE:
1100 case INTERFACE_NODE:
1101 case ZEBRA_NODE:
1102 case RIP_NODE:
1103 case RIPNG_NODE:
1104 case BGP_NODE:
1105 case RMAP_NODE:
1106 case OSPF_NODE:
1107 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001108 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001109 case KEYCHAIN_NODE:
1110 case KEYCHAIN_KEY_NODE:
1111 case MASC_NODE:
1112 case VTY_NODE:
1113 vty_config_unlock (vty);
1114 vty->node = ENABLE_NODE;
1115 break;
1116 default:
1117 /* Unknown node, we have to ignore it. */
1118 break;
1119 }
1120 vty_prompt (vty);
1121
1122 /* Set history pointer to the latest one. */
1123 vty->hp = vty->hindex;
1124}
1125
1126/* Add current command line to the history buffer. */
1127static void
1128vty_hist_add (struct vty *vty)
1129{
1130 int index;
1131
1132 if (vty->length == 0)
1133 return;
1134
1135 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1136
1137 /* Ignore the same string as previous one. */
1138 if (vty->hist[index])
1139 if (strcmp (vty->buf, vty->hist[index]) == 0)
1140 {
1141 vty->hp = vty->hindex;
1142 return;
1143 }
1144
1145 /* Insert history entry. */
1146 if (vty->hist[vty->hindex])
1147 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1148 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1149
1150 /* History index rotation. */
1151 vty->hindex++;
1152 if (vty->hindex == VTY_MAXHIST)
1153 vty->hindex = 0;
1154
1155 vty->hp = vty->hindex;
1156}
1157
1158/* #define TELNET_OPTION_DEBUG */
1159
1160/* Get telnet window size. */
1161static int
1162vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1163{
1164#ifdef TELNET_OPTION_DEBUG
1165 int i;
1166
1167 for (i = 0; i < nbytes; i++)
1168 {
1169 switch (buf[i])
1170 {
1171 case IAC:
1172 vty_out (vty, "IAC ");
1173 break;
1174 case WILL:
1175 vty_out (vty, "WILL ");
1176 break;
1177 case WONT:
1178 vty_out (vty, "WONT ");
1179 break;
1180 case DO:
1181 vty_out (vty, "DO ");
1182 break;
1183 case DONT:
1184 vty_out (vty, "DONT ");
1185 break;
1186 case SB:
1187 vty_out (vty, "SB ");
1188 break;
1189 case SE:
1190 vty_out (vty, "SE ");
1191 break;
1192 case TELOPT_ECHO:
1193 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1194 break;
1195 case TELOPT_SGA:
1196 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1197 break;
1198 case TELOPT_NAWS:
1199 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1200 break;
1201 default:
1202 vty_out (vty, "%x ", buf[i]);
1203 break;
1204 }
1205 }
1206 vty_out (vty, "%s", VTY_NEWLINE);
1207
1208#endif /* TELNET_OPTION_DEBUG */
1209
1210 switch (buf[0])
1211 {
1212 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001213 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001214 vty->iac_sb_in_progress = 1;
1215 return 0;
1216 break;
1217 case SE:
1218 {
paul718e3742002-12-13 20:15:29 +00001219 if (!vty->iac_sb_in_progress)
1220 return 0;
1221
ajs9fc7ebf2005-02-23 15:12:34 +00001222 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001223 {
1224 vty->iac_sb_in_progress = 0;
1225 return 0;
1226 }
ajs9fc7ebf2005-02-23 15:12:34 +00001227 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001228 {
1229 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001230 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1231 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1232 "should send %d characters, but we received %lu",
1233 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1234 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1235 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1236 "too small to handle the telnet NAWS option",
1237 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1238 else
1239 {
1240 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1241 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1242#ifdef TELNET_OPTION_DEBUG
1243 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1244 "width %d, height %d%s",
1245 vty->width, vty->height, VTY_NEWLINE);
1246#endif
1247 }
paul718e3742002-12-13 20:15:29 +00001248 break;
1249 }
1250 vty->iac_sb_in_progress = 0;
1251 return 0;
1252 break;
1253 }
1254 default:
1255 break;
1256 }
1257 return 1;
1258}
1259
1260/* Execute current command line. */
1261static int
1262vty_execute (struct vty *vty)
1263{
1264 int ret;
1265
1266 ret = CMD_SUCCESS;
1267
1268 switch (vty->node)
1269 {
1270 case AUTH_NODE:
1271 case AUTH_ENABLE_NODE:
1272 vty_auth (vty, vty->buf);
1273 break;
1274 default:
1275 ret = vty_command (vty, vty->buf);
1276 if (vty->type == VTY_TERM)
1277 vty_hist_add (vty);
1278 break;
1279 }
1280
1281 /* Clear command line buffer. */
1282 vty->cp = vty->length = 0;
1283 vty_clear_buf (vty);
1284
ajs5a646652004-11-05 01:25:55 +00001285 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001286 vty_prompt (vty);
1287
1288 return ret;
1289}
1290
1291#define CONTROL(X) ((X) - '@')
1292#define VTY_NORMAL 0
1293#define VTY_PRE_ESCAPE 1
1294#define VTY_ESCAPE 2
1295
1296/* Escape character command map. */
1297static void
1298vty_escape_map (unsigned char c, struct vty *vty)
1299{
1300 switch (c)
1301 {
1302 case ('A'):
1303 vty_previous_line (vty);
1304 break;
1305 case ('B'):
1306 vty_next_line (vty);
1307 break;
1308 case ('C'):
1309 vty_forward_char (vty);
1310 break;
1311 case ('D'):
1312 vty_backward_char (vty);
1313 break;
1314 default:
1315 break;
1316 }
1317
1318 /* Go back to normal mode. */
1319 vty->escape = VTY_NORMAL;
1320}
1321
1322/* Quit print out to the buffer. */
1323static void
1324vty_buffer_reset (struct vty *vty)
1325{
1326 buffer_reset (vty->obuf);
1327 vty_prompt (vty);
1328 vty_redraw_line (vty);
1329}
1330
1331/* Read data via vty socket. */
1332static int
1333vty_read (struct thread *thread)
1334{
1335 int i;
paul718e3742002-12-13 20:15:29 +00001336 int nbytes;
1337 unsigned char buf[VTY_READ_BUFSIZ];
1338
1339 int vty_sock = THREAD_FD (thread);
1340 struct vty *vty = THREAD_ARG (thread);
1341 vty->t_read = NULL;
1342
1343 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001344 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1345 {
1346 if (nbytes < 0)
1347 {
1348 if (ERRNO_IO_RETRY(errno))
1349 {
1350 vty_event (VTY_READ, vty_sock, vty);
1351 return 0;
1352 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001353 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001354 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1355 __func__, vty->fd, safe_strerror(errno));
1356 }
1357 buffer_reset(vty->obuf);
1358 vty->status = VTY_CLOSE;
1359 }
paul718e3742002-12-13 20:15:29 +00001360
1361 for (i = 0; i < nbytes; i++)
1362 {
1363 if (buf[i] == IAC)
1364 {
1365 if (!vty->iac)
1366 {
1367 vty->iac = 1;
1368 continue;
1369 }
1370 else
1371 {
1372 vty->iac = 0;
1373 }
1374 }
1375
1376 if (vty->iac_sb_in_progress && !vty->iac)
1377 {
ajs9fc7ebf2005-02-23 15:12:34 +00001378 if (vty->sb_len < sizeof(vty->sb_buf))
1379 vty->sb_buf[vty->sb_len] = buf[i];
1380 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001381 continue;
1382 }
1383
1384 if (vty->iac)
1385 {
1386 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001387 int ret = 0;
paule9372532003-10-26 21:36:07 +00001388 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001389 vty->iac = 0;
1390 i += ret;
1391 continue;
1392 }
paul5b8c1b02003-10-15 23:08:55 +00001393
paul718e3742002-12-13 20:15:29 +00001394
1395 if (vty->status == VTY_MORE)
1396 {
1397 switch (buf[i])
1398 {
1399 case CONTROL('C'):
1400 case 'q':
1401 case 'Q':
paul718e3742002-12-13 20:15:29 +00001402 vty_buffer_reset (vty);
1403 break;
1404#if 0 /* More line does not work for "show ip bgp". */
1405 case '\n':
1406 case '\r':
1407 vty->status = VTY_MORELINE;
1408 break;
1409#endif
1410 default:
paul718e3742002-12-13 20:15:29 +00001411 break;
1412 }
1413 continue;
1414 }
1415
1416 /* Escape character. */
1417 if (vty->escape == VTY_ESCAPE)
1418 {
1419 vty_escape_map (buf[i], vty);
1420 continue;
1421 }
1422
1423 /* Pre-escape status. */
1424 if (vty->escape == VTY_PRE_ESCAPE)
1425 {
1426 switch (buf[i])
1427 {
1428 case '[':
1429 vty->escape = VTY_ESCAPE;
1430 break;
1431 case 'b':
1432 vty_backward_word (vty);
1433 vty->escape = VTY_NORMAL;
1434 break;
1435 case 'f':
1436 vty_forward_word (vty);
1437 vty->escape = VTY_NORMAL;
1438 break;
1439 case 'd':
1440 vty_forward_kill_word (vty);
1441 vty->escape = VTY_NORMAL;
1442 break;
1443 case CONTROL('H'):
1444 case 0x7f:
1445 vty_backward_kill_word (vty);
1446 vty->escape = VTY_NORMAL;
1447 break;
1448 default:
1449 vty->escape = VTY_NORMAL;
1450 break;
1451 }
1452 continue;
1453 }
1454
1455 switch (buf[i])
1456 {
1457 case CONTROL('A'):
1458 vty_beginning_of_line (vty);
1459 break;
1460 case CONTROL('B'):
1461 vty_backward_char (vty);
1462 break;
1463 case CONTROL('C'):
1464 vty_stop_input (vty);
1465 break;
1466 case CONTROL('D'):
1467 vty_delete_char (vty);
1468 break;
1469 case CONTROL('E'):
1470 vty_end_of_line (vty);
1471 break;
1472 case CONTROL('F'):
1473 vty_forward_char (vty);
1474 break;
1475 case CONTROL('H'):
1476 case 0x7f:
1477 vty_delete_backward_char (vty);
1478 break;
1479 case CONTROL('K'):
1480 vty_kill_line (vty);
1481 break;
1482 case CONTROL('N'):
1483 vty_next_line (vty);
1484 break;
1485 case CONTROL('P'):
1486 vty_previous_line (vty);
1487 break;
1488 case CONTROL('T'):
1489 vty_transpose_chars (vty);
1490 break;
1491 case CONTROL('U'):
1492 vty_kill_line_from_beginning (vty);
1493 break;
1494 case CONTROL('W'):
1495 vty_backward_kill_word (vty);
1496 break;
1497 case CONTROL('Z'):
1498 vty_end_config (vty);
1499 break;
1500 case '\n':
1501 case '\r':
1502 vty_out (vty, "%s", VTY_NEWLINE);
1503 vty_execute (vty);
1504 break;
1505 case '\t':
1506 vty_complete_command (vty);
1507 break;
1508 case '?':
1509 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1510 vty_self_insert (vty, buf[i]);
1511 else
1512 vty_describe_command (vty);
1513 break;
1514 case '\033':
1515 if (i + 1 < nbytes && buf[i + 1] == '[')
1516 {
1517 vty->escape = VTY_ESCAPE;
1518 i++;
1519 }
1520 else
1521 vty->escape = VTY_PRE_ESCAPE;
1522 break;
1523 default:
1524 if (buf[i] > 31 && buf[i] < 127)
1525 vty_self_insert (vty, buf[i]);
1526 break;
1527 }
1528 }
1529
1530 /* Check status. */
1531 if (vty->status == VTY_CLOSE)
1532 vty_close (vty);
1533 else
1534 {
1535 vty_event (VTY_WRITE, vty_sock, vty);
1536 vty_event (VTY_READ, vty_sock, vty);
1537 }
1538 return 0;
1539}
1540
1541/* Flush buffer to the vty. */
1542static int
1543vty_flush (struct thread *thread)
1544{
1545 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001546 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001547 int vty_sock = THREAD_FD (thread);
1548 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001549
paul718e3742002-12-13 20:15:29 +00001550 vty->t_write = NULL;
1551
1552 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001553 if ((vty->lines == 0) && vty->t_read)
1554 {
1555 thread_cancel (vty->t_read);
1556 vty->t_read = NULL;
1557 }
paul718e3742002-12-13 20:15:29 +00001558
1559 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001560 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001561
ajs9fc7ebf2005-02-23 15:12:34 +00001562 /* N.B. if width is 0, that means we don't know the window size. */
1563 if ((vty->lines == 0) || (vty->width == 0))
1564 flushrc = buffer_flush_available(vty->obuf, vty->fd);
1565 else if (vty->status == VTY_MORELINE)
1566 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1567 1, erase, 0);
1568 else
1569 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1570 vty->lines >= 0 ? vty->lines :
1571 vty->height,
1572 erase, 0);
1573 switch (flushrc)
1574 {
1575 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001576 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001577 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1578 vty->fd);
1579 buffer_reset(vty->obuf);
1580 vty_close(vty);
1581 return 0;
1582 case BUFFER_EMPTY:
1583 if (vty->status == VTY_CLOSE)
1584 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001585 else
1586 {
ajs9fc7ebf2005-02-23 15:12:34 +00001587 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001588 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001589 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001590 }
ajs9fc7ebf2005-02-23 15:12:34 +00001591 break;
1592 case BUFFER_PENDING:
1593 /* There is more data waiting to be written. */
1594 vty->status = VTY_MORE;
1595 if (vty->lines == 0)
1596 vty_event (VTY_WRITE, vty_sock, vty);
1597 break;
1598 }
paul718e3742002-12-13 20:15:29 +00001599
1600 return 0;
1601}
1602
1603/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001604static struct vty *
paul718e3742002-12-13 20:15:29 +00001605vty_create (int vty_sock, union sockunion *su)
1606{
1607 struct vty *vty;
1608
1609 /* Allocate new vty structure and set up default values. */
1610 vty = vty_new ();
1611 vty->fd = vty_sock;
1612 vty->type = VTY_TERM;
1613 vty->address = sockunion_su2str (su);
1614 if (no_password_check)
1615 {
1616 if (host.advanced)
1617 vty->node = ENABLE_NODE;
1618 else
1619 vty->node = VIEW_NODE;
1620 }
1621 else
1622 vty->node = AUTH_NODE;
1623 vty->fail = 0;
1624 vty->cp = 0;
1625 vty_clear_buf (vty);
1626 vty->length = 0;
1627 memset (vty->hist, 0, sizeof (vty->hist));
1628 vty->hp = 0;
1629 vty->hindex = 0;
1630 vector_set_index (vtyvec, vty_sock, vty);
1631 vty->status = VTY_NORMAL;
1632 vty->v_timeout = vty_timeout_val;
1633 if (host.lines >= 0)
1634 vty->lines = host.lines;
1635 else
1636 vty->lines = -1;
1637 vty->iac = 0;
1638 vty->iac_sb_in_progress = 0;
ajs9fc7ebf2005-02-23 15:12:34 +00001639 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001640
1641 if (! no_password_check)
1642 {
1643 /* Vty is not available if password isn't set. */
1644 if (host.password == NULL && host.password_encrypt == NULL)
1645 {
1646 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1647 vty->status = VTY_CLOSE;
1648 vty_close (vty);
1649 return NULL;
1650 }
1651 }
1652
1653 /* Say hello to the world. */
1654 vty_hello (vty);
1655 if (! no_password_check)
1656 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1657
1658 /* Setting up terminal. */
1659 vty_will_echo (vty);
1660 vty_will_suppress_go_ahead (vty);
1661
1662 vty_dont_linemode (vty);
1663 vty_do_window_size (vty);
1664 /* vty_dont_lflow_ahead (vty); */
1665
1666 vty_prompt (vty);
1667
1668 /* Add read/write thread. */
1669 vty_event (VTY_WRITE, vty_sock, vty);
1670 vty_event (VTY_READ, vty_sock, vty);
1671
1672 return vty;
1673}
1674
1675/* Accept connection from the network. */
1676static int
1677vty_accept (struct thread *thread)
1678{
1679 int vty_sock;
1680 struct vty *vty;
1681 union sockunion su;
1682 int ret;
1683 unsigned int on;
1684 int accept_sock;
1685 struct prefix *p = NULL;
1686 struct access_list *acl = NULL;
1687
1688 accept_sock = THREAD_FD (thread);
1689
1690 /* We continue hearing vty socket. */
1691 vty_event (VTY_SERV, accept_sock, NULL);
1692
1693 memset (&su, 0, sizeof (union sockunion));
1694
1695 /* We can handle IPv4 or IPv6 socket. */
1696 vty_sock = sockunion_accept (accept_sock, &su);
1697 if (vty_sock < 0)
1698 {
ajs6099b3b2004-11-20 02:06:59 +00001699 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001700 return -1;
1701 }
ajs9fc7ebf2005-02-23 15:12:34 +00001702 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001703
1704 p = sockunion2hostprefix (&su);
1705
1706 /* VTY's accesslist apply. */
1707 if (p->family == AF_INET && vty_accesslist_name)
1708 {
1709 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1710 (access_list_apply (acl, p) == FILTER_DENY))
1711 {
1712 char *buf;
1713 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1714 (buf = sockunion_su2str (&su)));
1715 free (buf);
1716 close (vty_sock);
1717
1718 /* continue accepting connections */
1719 vty_event (VTY_SERV, accept_sock, NULL);
1720
1721 prefix_free (p);
1722
1723 return 0;
1724 }
1725 }
1726
1727#ifdef HAVE_IPV6
1728 /* VTY's ipv6 accesslist apply. */
1729 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1730 {
1731 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1732 (access_list_apply (acl, p) == FILTER_DENY))
1733 {
1734 char *buf;
1735 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1736 (buf = sockunion_su2str (&su)));
1737 free (buf);
1738 close (vty_sock);
1739
1740 /* continue accepting connections */
1741 vty_event (VTY_SERV, accept_sock, NULL);
1742
1743 prefix_free (p);
1744
1745 return 0;
1746 }
1747 }
1748#endif /* HAVE_IPV6 */
1749
1750 prefix_free (p);
1751
1752 on = 1;
1753 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1754 (char *) &on, sizeof (on));
1755 if (ret < 0)
1756 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001757 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001758
1759 vty = vty_create (vty_sock, &su);
1760
1761 return 0;
1762}
1763
1764#if defined(HAVE_IPV6) && !defined(NRL)
ajs9fc7ebf2005-02-23 15:12:34 +00001765static void
paul718e3742002-12-13 20:15:29 +00001766vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1767{
1768 int ret;
1769 struct addrinfo req;
1770 struct addrinfo *ainfo;
1771 struct addrinfo *ainfo_save;
1772 int sock;
1773 char port_str[BUFSIZ];
1774
1775 memset (&req, 0, sizeof (struct addrinfo));
1776 req.ai_flags = AI_PASSIVE;
1777 req.ai_family = AF_UNSPEC;
1778 req.ai_socktype = SOCK_STREAM;
1779 sprintf (port_str, "%d", port);
1780 port_str[sizeof (port_str) - 1] = '\0';
1781
1782 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1783
1784 if (ret != 0)
1785 {
1786 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1787 exit (1);
1788 }
1789
1790 ainfo_save = ainfo;
1791
1792 do
1793 {
1794 if (ainfo->ai_family != AF_INET
1795#ifdef HAVE_IPV6
1796 && ainfo->ai_family != AF_INET6
1797#endif /* HAVE_IPV6 */
1798 )
1799 continue;
1800
1801 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1802 if (sock < 0)
1803 continue;
1804
1805 sockopt_reuseaddr (sock);
1806 sockopt_reuseport (sock);
1807
1808 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1809 if (ret < 0)
1810 {
1811 close (sock); /* Avoid sd leak. */
1812 continue;
1813 }
1814
1815 ret = listen (sock, 3);
1816 if (ret < 0)
1817 {
1818 close (sock); /* Avoid sd leak. */
1819 continue;
1820 }
1821
1822 vty_event (VTY_SERV, sock, NULL);
1823 }
1824 while ((ainfo = ainfo->ai_next) != NULL);
1825
1826 freeaddrinfo (ainfo_save);
1827}
1828#endif /* HAVE_IPV6 && ! NRL */
1829
1830/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001831static void
paul29db05b2003-05-08 20:10:22 +00001832vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001833{
1834 int ret;
1835 union sockunion su;
1836 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001837 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001838
1839 memset (&su, 0, sizeof (union sockunion));
1840 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001841 if(addr)
1842 switch(family)
1843 {
1844 case AF_INET:
1845 naddr=&su.sin.sin_addr;
1846#ifdef HAVE_IPV6
1847 case AF_INET6:
1848 naddr=&su.sin6.sin6_addr;
1849#endif
1850 }
1851
1852 if(naddr)
1853 switch(inet_pton(family,addr,naddr))
1854 {
1855 case -1:
1856 zlog_err("bad address %s",addr);
1857 naddr=NULL;
1858 break;
1859 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001860 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001861 naddr=NULL;
1862 }
paul718e3742002-12-13 20:15:29 +00001863
1864 /* Make new socket. */
1865 accept_sock = sockunion_stream_socket (&su);
1866 if (accept_sock < 0)
1867 return;
1868
1869 /* This is server, so reuse address. */
1870 sockopt_reuseaddr (accept_sock);
1871 sockopt_reuseport (accept_sock);
1872
1873 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001874 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001875 if (ret < 0)
1876 {
paul29db05b2003-05-08 20:10:22 +00001877 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001878 close (accept_sock); /* Avoid sd leak. */
1879 return;
1880 }
1881
1882 /* Listen socket under queue 3. */
1883 ret = listen (accept_sock, 3);
1884 if (ret < 0)
1885 {
1886 zlog (NULL, LOG_WARNING, "can't listen socket");
1887 close (accept_sock); /* Avoid sd leak. */
1888 return;
1889 }
1890
1891 /* Add vty server event. */
1892 vty_event (VTY_SERV, accept_sock, NULL);
1893}
1894
1895#ifdef VTYSH
1896/* For sockaddr_un. */
1897#include <sys/un.h>
1898
1899/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001900static void
hasso6ad96ea2004-10-07 19:33:46 +00001901vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001902{
1903 int ret;
paul75e15fe2004-10-31 02:13:09 +00001904 int sock, len;
paul718e3742002-12-13 20:15:29 +00001905 struct sockaddr_un serv;
1906 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001907 struct zprivs_ids_t ids;
1908
paul718e3742002-12-13 20:15:29 +00001909 /* First of all, unlink existing socket */
1910 unlink (path);
1911
1912 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001913 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001914
1915 /* Make UNIX domain socket. */
1916 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1917 if (sock < 0)
1918 {
ajs6a52d0d2005-01-30 18:49:28 +00001919 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001920 return;
1921 }
1922
1923 /* Make server socket. */
1924 memset (&serv, 0, sizeof (struct sockaddr_un));
1925 serv.sun_family = AF_UNIX;
1926 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001927#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001928 len = serv.sun_len = SUN_LEN(&serv);
1929#else
1930 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001931#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001932
1933 ret = bind (sock, (struct sockaddr *) &serv, len);
1934 if (ret < 0)
1935 {
ajs6a52d0d2005-01-30 18:49:28 +00001936 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001937 close (sock); /* Avoid sd leak. */
1938 return;
1939 }
1940
1941 ret = listen (sock, 5);
1942 if (ret < 0)
1943 {
ajs6a52d0d2005-01-30 18:49:28 +00001944 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001945 close (sock); /* Avoid sd leak. */
1946 return;
1947 }
1948
1949 umask (old_mask);
1950
pauledd7c242003-06-04 13:59:38 +00001951 zprivs_get_ids(&ids);
1952
1953 if (ids.gid_vty > 0)
1954 {
1955 /* set group of socket */
1956 if ( chown (path, -1, ids.gid_vty) )
1957 {
1958 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00001959 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00001960 }
1961 }
1962
paul718e3742002-12-13 20:15:29 +00001963 vty_event (VTYSH_SERV, sock, NULL);
1964}
1965
1966/* #define VTYSH_DEBUG 1 */
1967
1968static int
1969vtysh_accept (struct thread *thread)
1970{
1971 int accept_sock;
1972 int sock;
1973 int client_len;
1974 struct sockaddr_un client;
1975 struct vty *vty;
1976
1977 accept_sock = THREAD_FD (thread);
1978
1979 vty_event (VTYSH_SERV, accept_sock, NULL);
1980
1981 memset (&client, 0, sizeof (struct sockaddr_un));
1982 client_len = sizeof (struct sockaddr_un);
1983
hassoe473b032004-09-26 16:08:11 +00001984 sock = accept (accept_sock, (struct sockaddr *) &client,
1985 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00001986
1987 if (sock < 0)
1988 {
ajs6099b3b2004-11-20 02:06:59 +00001989 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001990 return -1;
1991 }
1992
ajs9fc7ebf2005-02-23 15:12:34 +00001993 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00001994 {
ajs9fc7ebf2005-02-23 15:12:34 +00001995 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
1996 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00001997 close (sock);
1998 return -1;
1999 }
pauldccfb192004-10-29 08:29:36 +00002000
paul718e3742002-12-13 20:15:29 +00002001#ifdef VTYSH_DEBUG
2002 printf ("VTY shell accept\n");
2003#endif /* VTYSH_DEBUG */
2004
2005 vty = vty_new ();
2006 vty->fd = sock;
2007 vty->type = VTY_SHELL_SERV;
2008 vty->node = VIEW_NODE;
2009
2010 vty_event (VTYSH_READ, sock, vty);
2011
2012 return 0;
2013}
2014
2015static int
ajs9fc7ebf2005-02-23 15:12:34 +00002016vtysh_flush(struct vty *vty)
2017{
2018 switch (buffer_flush_available(vty->obuf, vty->fd))
2019 {
2020 case BUFFER_PENDING:
2021 vty_event(VTYSH_WRITE, vty->fd, vty);
2022 break;
2023 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002024 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002025 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2026 buffer_reset(vty->obuf);
2027 vty_close(vty);
2028 return -1;
2029 break;
2030 case BUFFER_EMPTY:
2031 break;
2032 }
2033 return 0;
2034}
2035
2036static int
paul718e3742002-12-13 20:15:29 +00002037vtysh_read (struct thread *thread)
2038{
2039 int ret;
2040 int sock;
2041 int nbytes;
2042 struct vty *vty;
2043 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002044 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002045 u_char header[4] = {0, 0, 0, 0};
2046
2047 sock = THREAD_FD (thread);
2048 vty = THREAD_ARG (thread);
2049 vty->t_read = NULL;
2050
ajs9fc7ebf2005-02-23 15:12:34 +00002051 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002052 {
ajs9fc7ebf2005-02-23 15:12:34 +00002053 if (nbytes < 0)
2054 {
2055 if (ERRNO_IO_RETRY(errno))
2056 {
2057 vty_event (VTYSH_READ, sock, vty);
2058 return 0;
2059 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002060 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002061 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2062 __func__, sock, safe_strerror(errno));
2063 }
2064 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002065 vty_close (vty);
2066#ifdef VTYSH_DEBUG
2067 printf ("close vtysh\n");
2068#endif /* VTYSH_DEBUG */
2069 return 0;
2070 }
2071
2072#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002073 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002074#endif /* VTYSH_DEBUG */
2075
ajs9fc7ebf2005-02-23 15:12:34 +00002076 for (p = buf; p < buf+nbytes; p++)
2077 {
2078 vty_ensure(vty, vty->length+1);
2079 vty->buf[vty->length++] = *p;
2080 if (*p == '\0')
2081 {
2082 /* Pass this line to parser. */
2083 ret = vty_execute (vty);
2084 /* Note that vty_execute clears the command buffer and resets
2085 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002086
ajs9fc7ebf2005-02-23 15:12:34 +00002087 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002088#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002089 printf ("result: %d\n", ret);
2090 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002091#endif /* VTYSH_DEBUG */
2092
ajs9fc7ebf2005-02-23 15:12:34 +00002093 header[3] = ret;
2094 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002095
ajs9fc7ebf2005-02-23 15:12:34 +00002096 if (!vty->t_write && (vtysh_flush(vty) < 0))
2097 /* Try to flush results; exit if a write error occurs. */
2098 return 0;
2099 }
2100 }
2101
paul718e3742002-12-13 20:15:29 +00002102 vty_event (VTYSH_READ, sock, vty);
2103
2104 return 0;
2105}
ajs49ff6d92004-11-04 19:26:16 +00002106
2107static int
2108vtysh_write (struct thread *thread)
2109{
2110 struct vty *vty = THREAD_ARG (thread);
2111
2112 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002113 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002114 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002115}
2116
paul718e3742002-12-13 20:15:29 +00002117#endif /* VTYSH */
2118
2119/* Determine address family to bind. */
2120void
hasso6ad96ea2004-10-07 19:33:46 +00002121vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002122{
2123 /* If port is set to 0, do not listen on TCP/IP at all! */
2124 if (port)
2125 {
2126
2127#ifdef HAVE_IPV6
2128#ifdef NRL
paul29db05b2003-05-08 20:10:22 +00002129 vty_serv_sock_family (addr, port, AF_INET);
2130 vty_serv_sock_family (addr, port, AF_INET6);
paul718e3742002-12-13 20:15:29 +00002131#else /* ! NRL */
paul29db05b2003-05-08 20:10:22 +00002132 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002133#endif /* NRL*/
2134#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002135 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002136#endif /* HAVE_IPV6 */
2137 }
2138
2139#ifdef VTYSH
2140 vty_serv_un (path);
2141#endif /* VTYSH */
2142}
2143
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002144/* Close vty interface. Warning: call this only from functions that
2145 will be careful not to access the vty afterwards (since it has
2146 now been freed). This is safest from top-level functions (called
2147 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002148void
2149vty_close (struct vty *vty)
2150{
2151 int i;
2152
2153 /* Cancel threads.*/
2154 if (vty->t_read)
2155 thread_cancel (vty->t_read);
2156 if (vty->t_write)
2157 thread_cancel (vty->t_write);
2158 if (vty->t_timeout)
2159 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002160
2161 /* Flush buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +00002162 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002163
2164 /* Free input buffer. */
2165 buffer_free (vty->obuf);
2166
paul718e3742002-12-13 20:15:29 +00002167 /* Free command history. */
2168 for (i = 0; i < VTY_MAXHIST; i++)
2169 if (vty->hist[i])
2170 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2171
2172 /* Unset vector. */
2173 vector_unset (vtyvec, vty->fd);
2174
2175 /* Close socket. */
2176 if (vty->fd > 0)
2177 close (vty->fd);
2178
2179 if (vty->address)
paul05865c92005-10-26 05:49:54 +00002180 XFREE (MTYPE_TMP, vty->address);
paul718e3742002-12-13 20:15:29 +00002181 if (vty->buf)
2182 XFREE (MTYPE_VTY, vty->buf);
2183
2184 /* Check configure. */
2185 vty_config_unlock (vty);
2186
2187 /* OK free vty. */
2188 XFREE (MTYPE_VTY, vty);
2189}
2190
2191/* When time out occur output message then close connection. */
2192static int
2193vty_timeout (struct thread *thread)
2194{
2195 struct vty *vty;
2196
2197 vty = THREAD_ARG (thread);
2198 vty->t_timeout = NULL;
2199 vty->v_timeout = 0;
2200
2201 /* Clear buffer*/
2202 buffer_reset (vty->obuf);
2203 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2204
2205 /* Close connection. */
2206 vty->status = VTY_CLOSE;
2207 vty_close (vty);
2208
2209 return 0;
2210}
2211
2212/* Read up configuration file from file_name. */
2213static void
2214vty_read_file (FILE *confp)
2215{
2216 int ret;
2217 struct vty *vty;
2218
2219 vty = vty_new ();
2220 vty->fd = 0; /* stdout */
2221 vty->type = VTY_TERM;
2222 vty->node = CONFIG_NODE;
2223
2224 /* Execute configuration file */
2225 ret = config_from_file (vty, confp);
2226
paul7021c422003-07-15 12:52:22 +00002227 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002228 {
2229 switch (ret)
paul7021c422003-07-15 12:52:22 +00002230 {
2231 case CMD_ERR_AMBIGUOUS:
2232 fprintf (stderr, "Ambiguous command.\n");
2233 break;
2234 case CMD_ERR_NO_MATCH:
2235 fprintf (stderr, "There is no such command.\n");
2236 break;
2237 }
paul718e3742002-12-13 20:15:29 +00002238 fprintf (stderr, "Error occured during reading below line.\n%s\n",
2239 vty->buf);
2240 vty_close (vty);
2241 exit (1);
2242 }
2243
2244 vty_close (vty);
2245}
2246
ajs9fc7ebf2005-02-23 15:12:34 +00002247static FILE *
paul718e3742002-12-13 20:15:29 +00002248vty_use_backup_config (char *fullpath)
2249{
2250 char *fullpath_sav, *fullpath_tmp;
2251 FILE *ret = NULL;
2252 struct stat buf;
2253 int tmp, sav;
2254 int c;
2255 char buffer[512];
2256
2257 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2258 strcpy (fullpath_sav, fullpath);
2259 strcat (fullpath_sav, CONF_BACKUP_EXT);
2260 if (stat (fullpath_sav, &buf) == -1)
2261 {
2262 free (fullpath_sav);
2263 return NULL;
2264 }
2265
2266 fullpath_tmp = malloc (strlen (fullpath) + 8);
2267 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2268
2269 /* Open file to configuration write. */
2270 tmp = mkstemp (fullpath_tmp);
2271 if (tmp < 0)
2272 {
2273 free (fullpath_sav);
2274 free (fullpath_tmp);
2275 return NULL;
2276 }
2277
2278 sav = open (fullpath_sav, O_RDONLY);
2279 if (sav < 0)
2280 {
gdt3dbf9962003-12-22 20:18:18 +00002281 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002282 free (fullpath_sav);
2283 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002284 return NULL;
2285 }
2286
2287 while((c = read (sav, buffer, 512)) > 0)
2288 write (tmp, buffer, c);
2289
2290 close (sav);
2291 close (tmp);
2292
gdtaa593d52003-12-22 20:15:53 +00002293 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2294 {
gdt3dbf9962003-12-22 20:18:18 +00002295 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002296 free (fullpath_sav);
2297 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002298 return NULL;
2299 }
2300
paul718e3742002-12-13 20:15:29 +00002301 if (link (fullpath_tmp, fullpath) == 0)
2302 ret = fopen (fullpath, "r");
2303
2304 unlink (fullpath_tmp);
2305
2306 free (fullpath_sav);
2307 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002308 return ret;
paul718e3742002-12-13 20:15:29 +00002309}
2310
2311/* Read up configuration file from file_name. */
2312void
2313vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002314 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002315{
paulccc92352003-10-22 02:49:38 +00002316 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002317 FILE *confp = NULL;
2318 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002319 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002320
2321 /* If -f flag specified. */
2322 if (config_file != NULL)
2323 {
2324 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002325 {
2326 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002327 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002328 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002329 sprintf (tmp, "%s/%s", cwd, config_file);
2330 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002331 }
paul718e3742002-12-13 20:15:29 +00002332 else
hasso320ec102004-06-20 19:54:37 +00002333 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002334
2335 confp = fopen (fullpath, "r");
2336
2337 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002338 {
paul3d1dc852005-04-05 00:45:23 +00002339 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2340 __func__, fullpath, safe_strerror (errno));
2341
hasso320ec102004-06-20 19:54:37 +00002342 confp = vty_use_backup_config (fullpath);
2343 if (confp)
2344 fprintf (stderr, "WARNING: using backup configuration file!\n");
2345 else
2346 {
2347 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002348 config_file);
hasso320ec102004-06-20 19:54:37 +00002349 exit(1);
2350 }
2351 }
paul718e3742002-12-13 20:15:29 +00002352 }
2353 else
2354 {
paul718e3742002-12-13 20:15:29 +00002355#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002356 int ret;
2357 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002358
hasso320ec102004-06-20 19:54:37 +00002359 /* !!!!PLEASE LEAVE!!!!
2360 * This is NEEDED for use with vtysh -b, or else you can get
2361 * a real configuration food fight with a lot garbage in the
2362 * merged configuration file it creates coming from the per
2363 * daemon configuration files. This also allows the daemons
2364 * to start if there default configuration file is not
2365 * present or ignore them, as needed when using vtysh -b to
2366 * configure the daemons at boot - MAG
2367 */
paul718e3742002-12-13 20:15:29 +00002368
hasso320ec102004-06-20 19:54:37 +00002369 /* Stat for vtysh Zebra.conf, if found startup and wait for
2370 * boot configuration
2371 */
paul718e3742002-12-13 20:15:29 +00002372
hasso320ec102004-06-20 19:54:37 +00002373 if ( strstr(config_default_dir, "vtysh") == NULL)
2374 {
2375 ret = stat (integrate_default, &conf_stat);
2376 if (ret >= 0)
2377 return;
2378 }
paul718e3742002-12-13 20:15:29 +00002379#endif /* VTYSH */
2380
hasso320ec102004-06-20 19:54:37 +00002381 confp = fopen (config_default_dir, "r");
2382 if (confp == NULL)
2383 {
paul3d1dc852005-04-05 00:45:23 +00002384 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2385 __func__, config_default_dir, safe_strerror (errno));
2386
hasso320ec102004-06-20 19:54:37 +00002387 confp = vty_use_backup_config (config_default_dir);
2388 if (confp)
2389 {
2390 fprintf (stderr, "WARNING: using backup configuration file!\n");
2391 fullpath = config_default_dir;
2392 }
2393 else
2394 {
2395 fprintf (stderr, "can't open configuration file [%s]\n",
2396 config_default_dir);
2397 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002398 }
hasso320ec102004-06-20 19:54:37 +00002399 }
paul718e3742002-12-13 20:15:29 +00002400 else
hasso320ec102004-06-20 19:54:37 +00002401 fullpath = config_default_dir;
2402 }
2403
paul718e3742002-12-13 20:15:29 +00002404 vty_read_file (confp);
2405
2406 fclose (confp);
2407
2408 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002409
2410 if (tmp)
2411 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002412}
2413
2414/* Small utility function which output log to the VTY. */
2415void
ajs274a4a42004-12-07 15:39:31 +00002416vty_log (const char *level, const char *proto_str,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002417 const char *format, struct timestamp_control *ctl, va_list va)
paul718e3742002-12-13 20:15:29 +00002418{
hasso8c328f12004-10-05 21:01:23 +00002419 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002420 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002421
2422 if (!vtyvec)
2423 return;
paul718e3742002-12-13 20:15:29 +00002424
paul55468c82005-03-14 20:19:01 +00002425 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002426 if ((vty = vector_slot (vtyvec, i)) != NULL)
2427 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002428 {
2429 va_list ac;
2430 va_copy(ac, va);
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002431 vty_log_out (vty, level, proto_str, format, ctl, ac);
ajsd246bd92004-11-23 17:35:08 +00002432 va_end(ac);
2433 }
paul718e3742002-12-13 20:15:29 +00002434}
2435
ajs274a4a42004-12-07 15:39:31 +00002436/* Async-signal-safe version of vty_log for fixed strings. */
2437void
2438vty_log_fixed (const char *buf, size_t len)
2439{
2440 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002441 struct iovec iov[2];
2442
Paul Jakmaa4b30302006-05-28 08:18:38 +00002443 /* vty may not have been initialised */
2444 if (!vtyvec)
2445 return;
2446
ajs926fe8f2005-04-08 18:50:40 +00002447 iov[0].iov_base = (void *)buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002448 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002449 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002450 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002451
paul55468c82005-03-14 20:19:01 +00002452 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002453 {
2454 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002455 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2456 /* N.B. We don't care about the return code, since process is
2457 most likely just about to die anyway. */
2458 writev(vty->fd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002459 }
2460}
2461
paul718e3742002-12-13 20:15:29 +00002462int
2463vty_config_lock (struct vty *vty)
2464{
2465 if (vty_config == 0)
2466 {
2467 vty->config = 1;
2468 vty_config = 1;
2469 }
2470 return vty->config;
2471}
2472
2473int
2474vty_config_unlock (struct vty *vty)
2475{
2476 if (vty_config == 1 && vty->config == 1)
2477 {
2478 vty->config = 0;
2479 vty_config = 0;
2480 }
2481 return vty->config;
2482}
2483
2484/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002485static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002486
2487static void
2488vty_event (enum event event, int sock, struct vty *vty)
2489{
2490 struct thread *vty_serv_thread;
2491
2492 switch (event)
2493 {
2494 case VTY_SERV:
2495 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2496 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2497 break;
2498#ifdef VTYSH
2499 case VTYSH_SERV:
2500 thread_add_read (master, vtysh_accept, vty, sock);
2501 break;
2502 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002503 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2504 break;
2505 case VTYSH_WRITE:
2506 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002507 break;
2508#endif /* VTYSH */
2509 case VTY_READ:
2510 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2511
2512 /* Time out treatment. */
2513 if (vty->v_timeout)
2514 {
2515 if (vty->t_timeout)
2516 thread_cancel (vty->t_timeout);
2517 vty->t_timeout =
2518 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2519 }
2520 break;
2521 case VTY_WRITE:
2522 if (! vty->t_write)
2523 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2524 break;
2525 case VTY_TIMEOUT_RESET:
2526 if (vty->t_timeout)
2527 {
2528 thread_cancel (vty->t_timeout);
2529 vty->t_timeout = NULL;
2530 }
2531 if (vty->v_timeout)
2532 {
2533 vty->t_timeout =
2534 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2535 }
2536 break;
2537 }
2538}
2539
2540DEFUN (config_who,
2541 config_who_cmd,
2542 "who",
2543 "Display who is on vty\n")
2544{
hasso8c328f12004-10-05 21:01:23 +00002545 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002546 struct vty *v;
2547
paul55468c82005-03-14 20:19:01 +00002548 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002549 if ((v = vector_slot (vtyvec, i)) != NULL)
2550 vty_out (vty, "%svty[%d] connected from %s.%s",
2551 v->config ? "*" : " ",
2552 i, v->address, VTY_NEWLINE);
2553 return CMD_SUCCESS;
2554}
2555
2556/* Move to vty configuration mode. */
2557DEFUN (line_vty,
2558 line_vty_cmd,
2559 "line vty",
2560 "Configure a terminal line\n"
2561 "Virtual terminal\n")
2562{
2563 vty->node = VTY_NODE;
2564 return CMD_SUCCESS;
2565}
2566
2567/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002568static int
paul9035efa2004-10-10 11:56:56 +00002569exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002570{
2571 unsigned long timeout = 0;
2572
2573 /* min_str and sec_str are already checked by parser. So it must be
2574 all digit string. */
2575 if (min_str)
2576 {
2577 timeout = strtol (min_str, NULL, 10);
2578 timeout *= 60;
2579 }
2580 if (sec_str)
2581 timeout += strtol (sec_str, NULL, 10);
2582
2583 vty_timeout_val = timeout;
2584 vty->v_timeout = timeout;
2585 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2586
2587
2588 return CMD_SUCCESS;
2589}
2590
2591DEFUN (exec_timeout_min,
2592 exec_timeout_min_cmd,
2593 "exec-timeout <0-35791>",
2594 "Set timeout value\n"
2595 "Timeout value in minutes\n")
2596{
2597 return exec_timeout (vty, argv[0], NULL);
2598}
2599
2600DEFUN (exec_timeout_sec,
2601 exec_timeout_sec_cmd,
2602 "exec-timeout <0-35791> <0-2147483>",
2603 "Set the EXEC timeout\n"
2604 "Timeout in minutes\n"
2605 "Timeout in seconds\n")
2606{
2607 return exec_timeout (vty, argv[0], argv[1]);
2608}
2609
2610DEFUN (no_exec_timeout,
2611 no_exec_timeout_cmd,
2612 "no exec-timeout",
2613 NO_STR
2614 "Set the EXEC timeout\n")
2615{
2616 return exec_timeout (vty, NULL, NULL);
2617}
2618
2619/* Set vty access class. */
2620DEFUN (vty_access_class,
2621 vty_access_class_cmd,
2622 "access-class WORD",
2623 "Filter connections based on an IP access list\n"
2624 "IP access list\n")
2625{
2626 if (vty_accesslist_name)
2627 XFREE(MTYPE_VTY, vty_accesslist_name);
2628
2629 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2630
2631 return CMD_SUCCESS;
2632}
2633
2634/* Clear vty access class. */
2635DEFUN (no_vty_access_class,
2636 no_vty_access_class_cmd,
2637 "no access-class [WORD]",
2638 NO_STR
2639 "Filter connections based on an IP access list\n"
2640 "IP access list\n")
2641{
2642 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2643 {
2644 vty_out (vty, "Access-class is not currently applied to vty%s",
2645 VTY_NEWLINE);
2646 return CMD_WARNING;
2647 }
2648
2649 XFREE(MTYPE_VTY, vty_accesslist_name);
2650
2651 vty_accesslist_name = NULL;
2652
2653 return CMD_SUCCESS;
2654}
2655
2656#ifdef HAVE_IPV6
2657/* Set vty access class. */
2658DEFUN (vty_ipv6_access_class,
2659 vty_ipv6_access_class_cmd,
2660 "ipv6 access-class WORD",
2661 IPV6_STR
2662 "Filter connections based on an IP access list\n"
2663 "IPv6 access list\n")
2664{
2665 if (vty_ipv6_accesslist_name)
2666 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2667
2668 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2669
2670 return CMD_SUCCESS;
2671}
2672
2673/* Clear vty access class. */
2674DEFUN (no_vty_ipv6_access_class,
2675 no_vty_ipv6_access_class_cmd,
2676 "no ipv6 access-class [WORD]",
2677 NO_STR
2678 IPV6_STR
2679 "Filter connections based on an IP access list\n"
2680 "IPv6 access list\n")
2681{
2682 if (! vty_ipv6_accesslist_name ||
2683 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2684 {
2685 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2686 VTY_NEWLINE);
2687 return CMD_WARNING;
2688 }
2689
2690 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2691
2692 vty_ipv6_accesslist_name = NULL;
2693
2694 return CMD_SUCCESS;
2695}
2696#endif /* HAVE_IPV6 */
2697
2698/* vty login. */
2699DEFUN (vty_login,
2700 vty_login_cmd,
2701 "login",
2702 "Enable password checking\n")
2703{
2704 no_password_check = 0;
2705 return CMD_SUCCESS;
2706}
2707
2708DEFUN (no_vty_login,
2709 no_vty_login_cmd,
2710 "no login",
2711 NO_STR
2712 "Enable password checking\n")
2713{
2714 no_password_check = 1;
2715 return CMD_SUCCESS;
2716}
2717
2718DEFUN (service_advanced_vty,
2719 service_advanced_vty_cmd,
2720 "service advanced-vty",
2721 "Set up miscellaneous service\n"
2722 "Enable advanced mode vty interface\n")
2723{
2724 host.advanced = 1;
2725 return CMD_SUCCESS;
2726}
2727
2728DEFUN (no_service_advanced_vty,
2729 no_service_advanced_vty_cmd,
2730 "no service advanced-vty",
2731 NO_STR
2732 "Set up miscellaneous service\n"
2733 "Enable advanced mode vty interface\n")
2734{
2735 host.advanced = 0;
2736 return CMD_SUCCESS;
2737}
2738
2739DEFUN (terminal_monitor,
2740 terminal_monitor_cmd,
2741 "terminal monitor",
2742 "Set terminal line parameters\n"
2743 "Copy debug output to the current terminal line\n")
2744{
2745 vty->monitor = 1;
2746 return CMD_SUCCESS;
2747}
2748
2749DEFUN (terminal_no_monitor,
2750 terminal_no_monitor_cmd,
2751 "terminal no monitor",
2752 "Set terminal line parameters\n"
2753 NO_STR
2754 "Copy debug output to the current terminal line\n")
2755{
2756 vty->monitor = 0;
2757 return CMD_SUCCESS;
2758}
2759
paul789f78a2006-01-17 17:42:03 +00002760ALIAS (terminal_no_monitor,
2761 no_terminal_monitor_cmd,
2762 "no terminal monitor",
2763 NO_STR
2764 "Set terminal line parameters\n"
2765 "Copy debug output to the current terminal line\n")
2766
paul718e3742002-12-13 20:15:29 +00002767DEFUN (show_history,
2768 show_history_cmd,
2769 "show history",
2770 SHOW_STR
2771 "Display the session command history\n")
2772{
2773 int index;
2774
2775 for (index = vty->hindex + 1; index != vty->hindex;)
2776 {
2777 if (index == VTY_MAXHIST)
2778 {
2779 index = 0;
2780 continue;
2781 }
2782
2783 if (vty->hist[index] != NULL)
2784 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2785
2786 index++;
2787 }
2788
2789 return CMD_SUCCESS;
2790}
2791
2792/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002793static int
paul718e3742002-12-13 20:15:29 +00002794vty_config_write (struct vty *vty)
2795{
2796 vty_out (vty, "line vty%s", VTY_NEWLINE);
2797
2798 if (vty_accesslist_name)
2799 vty_out (vty, " access-class %s%s",
2800 vty_accesslist_name, VTY_NEWLINE);
2801
2802 if (vty_ipv6_accesslist_name)
2803 vty_out (vty, " ipv6 access-class %s%s",
2804 vty_ipv6_accesslist_name, VTY_NEWLINE);
2805
2806 /* exec-timeout */
2807 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2808 vty_out (vty, " exec-timeout %ld %ld%s",
2809 vty_timeout_val / 60,
2810 vty_timeout_val % 60, VTY_NEWLINE);
2811
2812 /* login */
2813 if (no_password_check)
2814 vty_out (vty, " no login%s", VTY_NEWLINE);
2815
2816 vty_out (vty, "!%s", VTY_NEWLINE);
2817
2818 return CMD_SUCCESS;
2819}
2820
2821struct cmd_node vty_node =
2822{
2823 VTY_NODE,
2824 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002825 1,
paul718e3742002-12-13 20:15:29 +00002826};
2827
2828/* Reset all VTY status. */
2829void
2830vty_reset ()
2831{
hasso8c328f12004-10-05 21:01:23 +00002832 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002833 struct vty *vty;
2834 struct thread *vty_serv_thread;
2835
paul55468c82005-03-14 20:19:01 +00002836 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002837 if ((vty = vector_slot (vtyvec, i)) != NULL)
2838 {
2839 buffer_reset (vty->obuf);
2840 vty->status = VTY_CLOSE;
2841 vty_close (vty);
2842 }
2843
paul55468c82005-03-14 20:19:01 +00002844 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002845 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2846 {
2847 thread_cancel (vty_serv_thread);
2848 vector_slot (Vvty_serv_thread, i) = NULL;
2849 close (i);
2850 }
2851
2852 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2853
2854 if (vty_accesslist_name)
2855 {
2856 XFREE(MTYPE_VTY, vty_accesslist_name);
2857 vty_accesslist_name = NULL;
2858 }
2859
2860 if (vty_ipv6_accesslist_name)
2861 {
2862 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2863 vty_ipv6_accesslist_name = NULL;
2864 }
2865}
2866
ajs9fc7ebf2005-02-23 15:12:34 +00002867static void
2868vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002869{
paul79ad2792003-10-15 22:09:28 +00002870 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002871 char *c;
paul718e3742002-12-13 20:15:29 +00002872
paulccc92352003-10-22 02:49:38 +00002873 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002874
paulccc92352003-10-22 02:49:38 +00002875 if (!c)
paul79ad2792003-10-15 22:09:28 +00002876 {
2877 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002878 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002879 }
paul718e3742002-12-13 20:15:29 +00002880
2881 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2882 strcpy (vty_cwd, cwd);
2883}
2884
2885char *
2886vty_get_cwd ()
2887{
2888 return vty_cwd;
2889}
2890
2891int
2892vty_shell (struct vty *vty)
2893{
2894 return vty->type == VTY_SHELL ? 1 : 0;
2895}
2896
2897int
2898vty_shell_serv (struct vty *vty)
2899{
2900 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2901}
2902
2903void
2904vty_init_vtysh ()
2905{
2906 vtyvec = vector_init (VECTOR_MIN_SIZE);
2907}
2908
2909/* Install vty's own commands like `who' command. */
2910void
paulb21b19c2003-06-15 01:28:29 +00002911vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00002912{
2913 /* For further configuration read, preserve current directory. */
2914 vty_save_cwd ();
2915
2916 vtyvec = vector_init (VECTOR_MIN_SIZE);
2917
paulb21b19c2003-06-15 01:28:29 +00002918 master = master_thread;
2919
paul718e3742002-12-13 20:15:29 +00002920 /* Initilize server thread vector. */
2921 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
2922
2923 /* Install bgp top node. */
2924 install_node (&vty_node, vty_config_write);
2925
2926 install_element (VIEW_NODE, &config_who_cmd);
2927 install_element (VIEW_NODE, &show_history_cmd);
2928 install_element (ENABLE_NODE, &config_who_cmd);
2929 install_element (CONFIG_NODE, &line_vty_cmd);
2930 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
2931 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
2932 install_element (CONFIG_NODE, &show_history_cmd);
2933 install_element (ENABLE_NODE, &terminal_monitor_cmd);
2934 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00002935 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00002936 install_element (ENABLE_NODE, &show_history_cmd);
2937
2938 install_default (VTY_NODE);
2939 install_element (VTY_NODE, &exec_timeout_min_cmd);
2940 install_element (VTY_NODE, &exec_timeout_sec_cmd);
2941 install_element (VTY_NODE, &no_exec_timeout_cmd);
2942 install_element (VTY_NODE, &vty_access_class_cmd);
2943 install_element (VTY_NODE, &no_vty_access_class_cmd);
2944 install_element (VTY_NODE, &vty_login_cmd);
2945 install_element (VTY_NODE, &no_vty_login_cmd);
2946#ifdef HAVE_IPV6
2947 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
2948 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
2949#endif /* HAVE_IPV6 */
2950}