blob: 4288e150d16f02f0fa561e4cc70bb138716f4e11 [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,
152 const char *format, 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 struct tm *tm;
158
159 if ((tm = localtime(&recent_time.tv_sec)) != NULL)
160 len = strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S ", tm);
161 else
162 len = 0;
paul718e3742002-12-13 20:15:29 +0000163
ajs274a4a42004-12-07 15:39:31 +0000164 if (level)
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000165 ret = snprintf(buf+len, sizeof(buf)-len, "%s: %s: ", level, proto_str);
ajs274a4a42004-12-07 15:39:31 +0000166 else
Andrew J. Schorr08942da2006-07-03 20:58:29 +0000167 ret = snprintf(buf+len, sizeof(buf)-len, "%s: ", proto_str);
168 if ((ret < 0) || ((size_t)(len += ret) >= sizeof(buf)))
paul718e3742002-12-13 20:15:29 +0000169 return -1;
paul718e3742002-12-13 20:15:29 +0000170
ajs9fc7ebf2005-02-23 15:12:34 +0000171 if (((ret = vsnprintf(buf+len, sizeof(buf)-len, format, va)) < 0) ||
172 ((size_t)((len += ret)+2) > sizeof(buf)))
173 return -1;
paul718e3742002-12-13 20:15:29 +0000174
ajs9fc7ebf2005-02-23 15:12:34 +0000175 buf[len++] = '\r';
176 buf[len++] = '\n';
177
178 if (write(vty->fd, buf, len) < 0)
179 {
180 if (ERRNO_IO_RETRY(errno))
181 /* Kernel buffer is full, probably too much debugging output, so just
182 drop the data and ignore. */
183 return -1;
184 /* Fatal I/O error. */
Andrew J. Schorr74542d72006-07-10 18:09:42 +0000185 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +0000186 zlog_warn("%s: write failed to vty client fd %d, closing: %s",
187 __func__, vty->fd, safe_strerror(errno));
188 buffer_reset(vty->obuf);
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +0000189 /* cannot call vty_close, because a parent routine may still try
190 to access the vty struct */
191 vty->status = VTY_CLOSE;
192 shutdown(vty->fd, SHUT_RDWR);
ajs9fc7ebf2005-02-23 15:12:34 +0000193 return -1;
194 }
195 return 0;
paul718e3742002-12-13 20:15:29 +0000196}
197
198/* Output current time to the vty. */
199void
200vty_time_print (struct vty *vty, int cr)
201{
202 time_t clock;
203 struct tm *tm;
204#define TIME_BUF 25
205 char buf [TIME_BUF];
206 int ret;
207
208 time (&clock);
209 tm = localtime (&clock);
210
211 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
212 if (ret == 0)
213 {
214 zlog (NULL, LOG_INFO, "strftime error");
215 return;
216 }
217 if (cr)
218 vty_out (vty, "%s\n", buf);
219 else
220 vty_out (vty, "%s ", buf);
221
222 return;
223}
224
225/* Say hello to vty interface. */
226void
227vty_hello (struct vty *vty)
228{
paul3b0c5d92005-03-08 10:43:43 +0000229 if (host.motdfile)
230 {
231 FILE *f;
232 char buf[4096];
paul22085182005-03-08 16:00:12 +0000233
paul3b0c5d92005-03-08 10:43:43 +0000234 f = fopen (host.motdfile, "r");
235 if (f)
236 {
paulb45da6f2005-03-08 15:16:57 +0000237 while (fgets (buf, sizeof (buf), f))
paul3b0c5d92005-03-08 10:43:43 +0000238 {
paulb45da6f2005-03-08 15:16:57 +0000239 char *s;
paul22085182005-03-08 16:00:12 +0000240 /* work backwards to ignore trailling isspace() */
gdtf80a0162005-12-29 16:03:32 +0000241 for (s = buf + strlen (buf); (s > buf) && isspace ((int)*(s - 1));
paul22085182005-03-08 16:00:12 +0000242 s--);
243 *s = '\0';
244 vty_out (vty, "%s%s", buf, VTY_NEWLINE);
245 }
paul3b0c5d92005-03-08 10:43:43 +0000246 fclose (f);
247 }
248 else
paulb45da6f2005-03-08 15:16:57 +0000249 vty_out (vty, "MOTD file not found%s", VTY_NEWLINE);
paul3b0c5d92005-03-08 10:43:43 +0000250 }
251 else if (host.motd)
paul718e3742002-12-13 20:15:29 +0000252 vty_out (vty, host.motd);
253}
254
255/* Put out prompt and wait input from user. */
256static void
257vty_prompt (struct vty *vty)
258{
259 struct utsname names;
260 const char*hostname;
261
262 if (vty->type == VTY_TERM)
263 {
264 hostname = host.name;
265 if (!hostname)
266 {
267 uname (&names);
268 hostname = names.nodename;
269 }
270 vty_out (vty, cmd_prompt (vty->node), hostname);
271 }
272}
273
274/* Send WILL TELOPT_ECHO to remote server. */
ajs9fc7ebf2005-02-23 15:12:34 +0000275static void
paul718e3742002-12-13 20:15:29 +0000276vty_will_echo (struct vty *vty)
277{
paul02ff83c2004-06-11 11:27:03 +0000278 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
paul718e3742002-12-13 20:15:29 +0000279 vty_out (vty, "%s", cmd);
280}
281
282/* Make suppress Go-Ahead telnet option. */
283static void
284vty_will_suppress_go_ahead (struct vty *vty)
285{
paul02ff83c2004-06-11 11:27:03 +0000286 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
paul718e3742002-12-13 20:15:29 +0000287 vty_out (vty, "%s", cmd);
288}
289
290/* Make don't use linemode over telnet. */
291static void
292vty_dont_linemode (struct vty *vty)
293{
paul02ff83c2004-06-11 11:27:03 +0000294 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
paul718e3742002-12-13 20:15:29 +0000295 vty_out (vty, "%s", cmd);
296}
297
298/* Use window size. */
299static void
300vty_do_window_size (struct vty *vty)
301{
paul02ff83c2004-06-11 11:27:03 +0000302 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
paul718e3742002-12-13 20:15:29 +0000303 vty_out (vty, "%s", cmd);
304}
305
306#if 0 /* Currently not used. */
307/* Make don't use lflow vty interface. */
308static void
309vty_dont_lflow_ahead (struct vty *vty)
310{
paul02ff83c2004-06-11 11:27:03 +0000311 unsigned char cmd[] = { IAC, DONT, TELOPT_LFLOW, '\0' };
paul718e3742002-12-13 20:15:29 +0000312 vty_out (vty, "%s", cmd);
313}
314#endif /* 0 */
315
316/* Allocate new vty struct. */
317struct vty *
318vty_new ()
319{
320 struct vty *new = XCALLOC (MTYPE_VTY, sizeof (struct vty));
321
ajs9fc7ebf2005-02-23 15:12:34 +0000322 new->obuf = buffer_new(0); /* Use default buffer size. */
paul718e3742002-12-13 20:15:29 +0000323 new->buf = XCALLOC (MTYPE_VTY, VTY_BUFSIZ);
324 new->max = VTY_BUFSIZ;
paul718e3742002-12-13 20:15:29 +0000325
326 return new;
327}
328
329/* Authentication of vty */
330static void
331vty_auth (struct vty *vty, char *buf)
332{
333 char *passwd = NULL;
334 enum node_type next_node = 0;
335 int fail;
336 char *crypt (const char *, const char *);
337
338 switch (vty->node)
339 {
340 case AUTH_NODE:
341 if (host.encrypt)
342 passwd = host.password_encrypt;
343 else
344 passwd = host.password;
345 if (host.advanced)
346 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
347 else
348 next_node = VIEW_NODE;
349 break;
350 case AUTH_ENABLE_NODE:
351 if (host.encrypt)
352 passwd = host.enable_encrypt;
353 else
354 passwd = host.enable;
355 next_node = ENABLE_NODE;
356 break;
357 }
358
359 if (passwd)
360 {
361 if (host.encrypt)
362 fail = strcmp (crypt(buf, passwd), passwd);
363 else
364 fail = strcmp (buf, passwd);
365 }
366 else
367 fail = 1;
368
369 if (! fail)
370 {
371 vty->fail = 0;
372 vty->node = next_node; /* Success ! */
373 }
374 else
375 {
376 vty->fail++;
377 if (vty->fail >= 3)
378 {
379 if (vty->node == AUTH_NODE)
380 {
381 vty_out (vty, "%% Bad passwords, too many failures!%s", VTY_NEWLINE);
382 vty->status = VTY_CLOSE;
383 }
384 else
385 {
386 /* AUTH_ENABLE_NODE */
387 vty->fail = 0;
388 vty_out (vty, "%% Bad enable passwords, too many failures!%s", VTY_NEWLINE);
389 vty->node = VIEW_NODE;
390 }
391 }
392 }
393}
394
395/* Command execution over the vty interface. */
ajs9fc7ebf2005-02-23 15:12:34 +0000396static int
paul718e3742002-12-13 20:15:29 +0000397vty_command (struct vty *vty, char *buf)
398{
399 int ret;
400 vector vline;
vincentfbf5d032005-09-29 11:25:50 +0000401 const char *protocolname;
paul718e3742002-12-13 20:15:29 +0000402
403 /* Split readline string up into the vector */
404 vline = cmd_make_strvec (buf);
405
406 if (vline == NULL)
407 return CMD_SUCCESS;
408
ajs924b9222005-04-16 17:11:24 +0000409#ifdef CONSUMED_TIME_CHECK
410 {
411 RUSAGE_T before;
412 RUSAGE_T after;
ajs8b70d0b2005-04-28 01:31:13 +0000413 unsigned long realtime, cputime;
ajs924b9222005-04-16 17:11:24 +0000414
415 GETRUSAGE(&before);
416#endif /* CONSUMED_TIME_CHECK */
417
hasso87d683b2005-01-16 23:31:54 +0000418 ret = cmd_execute_command (vline, vty, NULL, 0);
paul718e3742002-12-13 20:15:29 +0000419
vincentfbf5d032005-09-29 11:25:50 +0000420 /* Get the name of the protocol if any */
421 if (zlog_default)
422 protocolname = zlog_proto_names[zlog_default->protocol];
423 else
424 protocolname = zlog_proto_names[ZLOG_NONE];
425
ajs924b9222005-04-16 17:11:24 +0000426#ifdef CONSUMED_TIME_CHECK
427 GETRUSAGE(&after);
ajs8b70d0b2005-04-28 01:31:13 +0000428 if ((realtime = thread_consumed_time(&after, &before, &cputime)) >
429 CONSUMED_TIME_CHECK)
ajs924b9222005-04-16 17:11:24 +0000430 /* Warn about CPU hog that must be fixed. */
ajs8b70d0b2005-04-28 01:31:13 +0000431 zlog_warn("SLOW COMMAND: command took %lums (cpu time %lums): %s",
432 realtime/1000, cputime/1000, buf);
ajs924b9222005-04-16 17:11:24 +0000433 }
434#endif /* CONSUMED_TIME_CHECK */
435
paul718e3742002-12-13 20:15:29 +0000436 if (ret != CMD_SUCCESS)
437 switch (ret)
438 {
439 case CMD_WARNING:
440 if (vty->type == VTY_FILE)
441 vty_out (vty, "Warning...%s", VTY_NEWLINE);
442 break;
443 case CMD_ERR_AMBIGUOUS:
444 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
445 break;
446 case CMD_ERR_NO_MATCH:
vincentfbf5d032005-09-29 11:25:50 +0000447 vty_out (vty, "%% [%s] Unknown command: %s%s", protocolname, buf, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000448 break;
449 case CMD_ERR_INCOMPLETE:
450 vty_out (vty, "%% Command incomplete.%s", VTY_NEWLINE);
451 break;
452 }
453 cmd_free_strvec (vline);
454
455 return ret;
456}
457
ajs9fc7ebf2005-02-23 15:12:34 +0000458static const char telnet_backward_char = 0x08;
459static const char telnet_space_char = ' ';
paul718e3742002-12-13 20:15:29 +0000460
461/* Basic function to write buffer to vty. */
462static void
ajs9fc7ebf2005-02-23 15:12:34 +0000463vty_write (struct vty *vty, const char *buf, size_t nbytes)
paul718e3742002-12-13 20:15:29 +0000464{
465 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
466 return;
467
468 /* Should we do buffering here ? And make vty_flush (vty) ? */
ajs9fc7ebf2005-02-23 15:12:34 +0000469 buffer_put (vty->obuf, buf, nbytes);
paul718e3742002-12-13 20:15:29 +0000470}
471
472/* Ensure length of input buffer. Is buffer is short, double it. */
473static void
474vty_ensure (struct vty *vty, int length)
475{
476 if (vty->max <= length)
477 {
478 vty->max *= 2;
479 vty->buf = XREALLOC (MTYPE_VTY, vty->buf, vty->max);
480 }
481}
482
483/* Basic function to insert character into vty. */
484static void
485vty_self_insert (struct vty *vty, char c)
486{
487 int i;
488 int length;
489
490 vty_ensure (vty, vty->length + 1);
491 length = vty->length - vty->cp;
492 memmove (&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
493 vty->buf[vty->cp] = c;
494
495 vty_write (vty, &vty->buf[vty->cp], length + 1);
496 for (i = 0; i < length; i++)
497 vty_write (vty, &telnet_backward_char, 1);
498
499 vty->cp++;
500 vty->length++;
501}
502
503/* Self insert character 'c' in overwrite mode. */
504static void
505vty_self_insert_overwrite (struct vty *vty, char c)
506{
507 vty_ensure (vty, vty->length + 1);
508 vty->buf[vty->cp++] = c;
509
510 if (vty->cp > vty->length)
511 vty->length++;
512
513 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
514 return;
515
516 vty_write (vty, &c, 1);
517}
518
519/* Insert a word into vty interface with overwrite mode. */
520static void
521vty_insert_word_overwrite (struct vty *vty, char *str)
522{
523 int len = strlen (str);
524 vty_write (vty, str, len);
525 strcpy (&vty->buf[vty->cp], str);
526 vty->cp += len;
527 vty->length = vty->cp;
528}
529
530/* Forward character. */
531static void
532vty_forward_char (struct vty *vty)
533{
534 if (vty->cp < vty->length)
535 {
536 vty_write (vty, &vty->buf[vty->cp], 1);
537 vty->cp++;
538 }
539}
540
541/* Backward character. */
542static void
543vty_backward_char (struct vty *vty)
544{
545 if (vty->cp > 0)
546 {
547 vty->cp--;
548 vty_write (vty, &telnet_backward_char, 1);
549 }
550}
551
552/* Move to the beginning of the line. */
553static void
554vty_beginning_of_line (struct vty *vty)
555{
556 while (vty->cp)
557 vty_backward_char (vty);
558}
559
560/* Move to the end of the line. */
561static void
562vty_end_of_line (struct vty *vty)
563{
564 while (vty->cp < vty->length)
565 vty_forward_char (vty);
566}
567
568static void vty_kill_line_from_beginning (struct vty *);
569static void vty_redraw_line (struct vty *);
570
571/* Print command line history. This function is called from
572 vty_next_line and vty_previous_line. */
573static void
574vty_history_print (struct vty *vty)
575{
576 int length;
577
578 vty_kill_line_from_beginning (vty);
579
580 /* Get previous line from history buffer */
581 length = strlen (vty->hist[vty->hp]);
582 memcpy (vty->buf, vty->hist[vty->hp], length);
583 vty->cp = vty->length = length;
584
585 /* Redraw current line */
586 vty_redraw_line (vty);
587}
588
589/* Show next command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000590static void
paul718e3742002-12-13 20:15:29 +0000591vty_next_line (struct vty *vty)
592{
593 int try_index;
594
595 if (vty->hp == vty->hindex)
596 return;
597
598 /* Try is there history exist or not. */
599 try_index = vty->hp;
600 if (try_index == (VTY_MAXHIST - 1))
601 try_index = 0;
602 else
603 try_index++;
604
605 /* If there is not history return. */
606 if (vty->hist[try_index] == NULL)
607 return;
608 else
609 vty->hp = try_index;
610
611 vty_history_print (vty);
612}
613
614/* Show previous command line history. */
ajs9fc7ebf2005-02-23 15:12:34 +0000615static void
paul718e3742002-12-13 20:15:29 +0000616vty_previous_line (struct vty *vty)
617{
618 int try_index;
619
620 try_index = vty->hp;
621 if (try_index == 0)
622 try_index = VTY_MAXHIST - 1;
623 else
624 try_index--;
625
626 if (vty->hist[try_index] == NULL)
627 return;
628 else
629 vty->hp = try_index;
630
631 vty_history_print (vty);
632}
633
634/* This function redraw all of the command line character. */
635static void
636vty_redraw_line (struct vty *vty)
637{
638 vty_write (vty, vty->buf, vty->length);
639 vty->cp = vty->length;
640}
641
642/* Forward word. */
643static void
644vty_forward_word (struct vty *vty)
645{
646 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
647 vty_forward_char (vty);
648
649 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
650 vty_forward_char (vty);
651}
652
653/* Backward word without skipping training space. */
654static void
655vty_backward_pure_word (struct vty *vty)
656{
657 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
658 vty_backward_char (vty);
659}
660
661/* Backward word. */
662static void
663vty_backward_word (struct vty *vty)
664{
665 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
666 vty_backward_char (vty);
667
668 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
669 vty_backward_char (vty);
670}
671
672/* When '^D' is typed at the beginning of the line we move to the down
673 level. */
674static void
675vty_down_level (struct vty *vty)
676{
677 vty_out (vty, "%s", VTY_NEWLINE);
ajs274a4a42004-12-07 15:39:31 +0000678 (*config_exit_cmd.func)(NULL, vty, 0, NULL);
paul718e3742002-12-13 20:15:29 +0000679 vty_prompt (vty);
680 vty->cp = 0;
681}
682
683/* When '^Z' is received from vty, move down to the enable mode. */
ajs9fc7ebf2005-02-23 15:12:34 +0000684static void
paul718e3742002-12-13 20:15:29 +0000685vty_end_config (struct vty *vty)
686{
687 vty_out (vty, "%s", VTY_NEWLINE);
688
689 switch (vty->node)
690 {
691 case VIEW_NODE:
692 case ENABLE_NODE:
693 /* Nothing to do. */
694 break;
695 case CONFIG_NODE:
696 case INTERFACE_NODE:
697 case ZEBRA_NODE:
698 case RIP_NODE:
699 case RIPNG_NODE:
700 case BGP_NODE:
701 case BGP_VPNV4_NODE:
702 case BGP_IPV4_NODE:
703 case BGP_IPV4M_NODE:
704 case BGP_IPV6_NODE:
paul1e836592005-08-22 22:39:56 +0000705 case BGP_IPV6M_NODE:
paul718e3742002-12-13 20:15:29 +0000706 case RMAP_NODE:
707 case OSPF_NODE:
708 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +0000709 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +0000710 case KEYCHAIN_NODE:
711 case KEYCHAIN_KEY_NODE:
712 case MASC_NODE:
713 case VTY_NODE:
714 vty_config_unlock (vty);
715 vty->node = ENABLE_NODE;
716 break;
717 default:
718 /* Unknown node, we have to ignore it. */
719 break;
720 }
721
722 vty_prompt (vty);
723 vty->cp = 0;
724}
725
726/* Delete a charcter at the current point. */
727static void
728vty_delete_char (struct vty *vty)
729{
730 int i;
731 int size;
732
733 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
734 return;
735
736 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';
750
751 vty_write (vty, &vty->buf[vty->cp], size - 1);
752 vty_write (vty, &telnet_space_char, 1);
753
754 for (i = 0; i < size; i++)
755 vty_write (vty, &telnet_backward_char, 1);
756}
757
758/* Delete a character before the point. */
759static void
760vty_delete_backward_char (struct vty *vty)
761{
762 if (vty->cp == 0)
763 return;
764
765 vty_backward_char (vty);
766 vty_delete_char (vty);
767}
768
769/* Kill rest of line from current point. */
770static void
771vty_kill_line (struct vty *vty)
772{
773 int i;
774 int size;
775
776 size = vty->length - vty->cp;
777
778 if (size == 0)
779 return;
780
781 for (i = 0; i < size; i++)
782 vty_write (vty, &telnet_space_char, 1);
783 for (i = 0; i < size; i++)
784 vty_write (vty, &telnet_backward_char, 1);
785
786 memset (&vty->buf[vty->cp], 0, size);
787 vty->length = vty->cp;
788}
789
790/* Kill line from the beginning. */
791static void
792vty_kill_line_from_beginning (struct vty *vty)
793{
794 vty_beginning_of_line (vty);
795 vty_kill_line (vty);
796}
797
798/* Delete a word before the point. */
799static void
800vty_forward_kill_word (struct vty *vty)
801{
802 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
803 vty_delete_char (vty);
804 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
805 vty_delete_char (vty);
806}
807
808/* Delete a word before the point. */
809static void
810vty_backward_kill_word (struct vty *vty)
811{
812 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
813 vty_delete_backward_char (vty);
814 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
815 vty_delete_backward_char (vty);
816}
817
818/* Transpose chars before or at the point. */
819static void
820vty_transpose_chars (struct vty *vty)
821{
822 char c1, c2;
823
824 /* If length is short or point is near by the beginning of line then
825 return. */
826 if (vty->length < 2 || vty->cp < 1)
827 return;
828
829 /* In case of point is located at the end of the line. */
830 if (vty->cp == vty->length)
831 {
832 c1 = vty->buf[vty->cp - 1];
833 c2 = vty->buf[vty->cp - 2];
834
835 vty_backward_char (vty);
836 vty_backward_char (vty);
837 vty_self_insert_overwrite (vty, c1);
838 vty_self_insert_overwrite (vty, c2);
839 }
840 else
841 {
842 c1 = vty->buf[vty->cp];
843 c2 = vty->buf[vty->cp - 1];
844
845 vty_backward_char (vty);
846 vty_self_insert_overwrite (vty, c1);
847 vty_self_insert_overwrite (vty, c2);
848 }
849}
850
851/* Do completion at vty interface. */
852static void
853vty_complete_command (struct vty *vty)
854{
855 int i;
856 int ret;
857 char **matched = NULL;
858 vector vline;
859
860 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
861 return;
862
863 vline = cmd_make_strvec (vty->buf);
864 if (vline == NULL)
865 return;
866
867 /* In case of 'help \t'. */
868 if (isspace ((int) vty->buf[vty->length - 1]))
869 vector_set (vline, '\0');
870
871 matched = cmd_complete_command (vline, vty, &ret);
872
873 cmd_free_strvec (vline);
874
875 vty_out (vty, "%s", VTY_NEWLINE);
876 switch (ret)
877 {
878 case CMD_ERR_AMBIGUOUS:
879 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
880 vty_prompt (vty);
881 vty_redraw_line (vty);
882 break;
883 case CMD_ERR_NO_MATCH:
884 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
885 vty_prompt (vty);
886 vty_redraw_line (vty);
887 break;
888 case CMD_COMPLETE_FULL_MATCH:
889 vty_prompt (vty);
890 vty_redraw_line (vty);
891 vty_backward_pure_word (vty);
892 vty_insert_word_overwrite (vty, matched[0]);
893 vty_self_insert (vty, ' ');
894 XFREE (MTYPE_TMP, matched[0]);
895 break;
896 case CMD_COMPLETE_MATCH:
897 vty_prompt (vty);
898 vty_redraw_line (vty);
899 vty_backward_pure_word (vty);
900 vty_insert_word_overwrite (vty, matched[0]);
901 XFREE (MTYPE_TMP, matched[0]);
902 vector_only_index_free (matched);
903 return;
904 break;
905 case CMD_COMPLETE_LIST_MATCH:
906 for (i = 0; matched[i] != NULL; i++)
907 {
908 if (i != 0 && ((i % 6) == 0))
909 vty_out (vty, "%s", VTY_NEWLINE);
910 vty_out (vty, "%-10s ", matched[i]);
911 XFREE (MTYPE_TMP, matched[i]);
912 }
913 vty_out (vty, "%s", VTY_NEWLINE);
914
915 vty_prompt (vty);
916 vty_redraw_line (vty);
917 break;
918 case CMD_ERR_NOTHING_TODO:
919 vty_prompt (vty);
920 vty_redraw_line (vty);
921 break;
922 default:
923 break;
924 }
925 if (matched)
926 vector_only_index_free (matched);
927}
928
ajs9fc7ebf2005-02-23 15:12:34 +0000929static void
paul718e3742002-12-13 20:15:29 +0000930vty_describe_fold (struct vty *vty, int cmd_width,
hasso8c328f12004-10-05 21:01:23 +0000931 unsigned int desc_width, struct desc *desc)
paul718e3742002-12-13 20:15:29 +0000932{
hasso8c328f12004-10-05 21:01:23 +0000933 char *buf;
934 const char *cmd, *p;
paul718e3742002-12-13 20:15:29 +0000935 int pos;
936
937 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
938
939 if (desc_width <= 0)
940 {
941 vty_out (vty, " %-*s %s%s", cmd_width, cmd, desc->str, VTY_NEWLINE);
942 return;
943 }
944
945 buf = XCALLOC (MTYPE_TMP, strlen (desc->str) + 1);
946
947 for (p = desc->str; strlen (p) > desc_width; p += pos + 1)
948 {
949 for (pos = desc_width; pos > 0; pos--)
950 if (*(p + pos) == ' ')
951 break;
952
953 if (pos == 0)
954 break;
955
956 strncpy (buf, p, pos);
957 buf[pos] = '\0';
958 vty_out (vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
959
960 cmd = "";
961 }
962
963 vty_out (vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
964
965 XFREE (MTYPE_TMP, buf);
966}
967
968/* Describe matched command function. */
969static void
970vty_describe_command (struct vty *vty)
971{
972 int ret;
973 vector vline;
974 vector describe;
hasso8c328f12004-10-05 21:01:23 +0000975 unsigned int i, width, desc_width;
paul718e3742002-12-13 20:15:29 +0000976 struct desc *desc, *desc_cr = NULL;
977
978 vline = cmd_make_strvec (vty->buf);
979
980 /* In case of '> ?'. */
981 if (vline == NULL)
982 {
983 vline = vector_init (1);
984 vector_set (vline, '\0');
985 }
986 else
987 if (isspace ((int) vty->buf[vty->length - 1]))
988 vector_set (vline, '\0');
989
990 describe = cmd_describe_command (vline, vty, &ret);
991
992 vty_out (vty, "%s", VTY_NEWLINE);
993
994 /* Ambiguous error. */
995 switch (ret)
996 {
997 case CMD_ERR_AMBIGUOUS:
paul718e3742002-12-13 20:15:29 +0000998 vty_out (vty, "%% Ambiguous command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +0000999 goto out;
paul718e3742002-12-13 20:15:29 +00001000 break;
1001 case CMD_ERR_NO_MATCH:
paul718e3742002-12-13 20:15:29 +00001002 vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE);
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001003 goto out;
paul718e3742002-12-13 20:15:29 +00001004 break;
1005 }
1006
1007 /* Get width of command string. */
1008 width = 0;
paul55468c82005-03-14 20:19:01 +00001009 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001010 if ((desc = vector_slot (describe, i)) != NULL)
1011 {
hasso8c328f12004-10-05 21:01:23 +00001012 unsigned int len;
paul718e3742002-12-13 20:15:29 +00001013
1014 if (desc->cmd[0] == '\0')
1015 continue;
1016
1017 len = strlen (desc->cmd);
1018 if (desc->cmd[0] == '.')
1019 len--;
1020
1021 if (width < len)
1022 width = len;
1023 }
1024
1025 /* Get width of description string. */
1026 desc_width = vty->width - (width + 6);
1027
1028 /* Print out description. */
paul55468c82005-03-14 20:19:01 +00001029 for (i = 0; i < vector_active (describe); i++)
paul718e3742002-12-13 20:15:29 +00001030 if ((desc = vector_slot (describe, i)) != NULL)
1031 {
1032 if (desc->cmd[0] == '\0')
1033 continue;
1034
1035 if (strcmp (desc->cmd, "<cr>") == 0)
1036 {
1037 desc_cr = desc;
1038 continue;
1039 }
1040
1041 if (!desc->str)
1042 vty_out (vty, " %-s%s",
1043 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1044 VTY_NEWLINE);
1045 else if (desc_width >= strlen (desc->str))
1046 vty_out (vty, " %-*s %s%s", width,
1047 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1048 desc->str, VTY_NEWLINE);
1049 else
1050 vty_describe_fold (vty, width, desc_width, desc);
1051
1052#if 0
1053 vty_out (vty, " %-*s %s%s", width
1054 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1055 desc->str ? desc->str : "", VTY_NEWLINE);
1056#endif /* 0 */
1057 }
1058
1059 if ((desc = desc_cr))
1060 {
1061 if (!desc->str)
1062 vty_out (vty, " %-s%s",
1063 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1064 VTY_NEWLINE);
1065 else if (desc_width >= strlen (desc->str))
1066 vty_out (vty, " %-*s %s%s", width,
1067 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1068 desc->str, VTY_NEWLINE);
1069 else
1070 vty_describe_fold (vty, width, desc_width, desc);
1071 }
1072
Paul Jakma2fe8aba2006-05-12 23:22:01 +00001073out:
paul718e3742002-12-13 20:15:29 +00001074 cmd_free_strvec (vline);
Paul Jakmad16e0432006-05-15 10:56:46 +00001075 if (describe)
1076 vector_free (describe);
paul718e3742002-12-13 20:15:29 +00001077
1078 vty_prompt (vty);
1079 vty_redraw_line (vty);
1080}
1081
ajs9fc7ebf2005-02-23 15:12:34 +00001082static void
paul718e3742002-12-13 20:15:29 +00001083vty_clear_buf (struct vty *vty)
1084{
1085 memset (vty->buf, 0, vty->max);
1086}
1087
1088/* ^C stop current input and do not add command line to the history. */
1089static void
1090vty_stop_input (struct vty *vty)
1091{
1092 vty->cp = vty->length = 0;
1093 vty_clear_buf (vty);
1094 vty_out (vty, "%s", VTY_NEWLINE);
1095
1096 switch (vty->node)
1097 {
1098 case VIEW_NODE:
1099 case ENABLE_NODE:
1100 /* Nothing to do. */
1101 break;
1102 case CONFIG_NODE:
1103 case INTERFACE_NODE:
1104 case ZEBRA_NODE:
1105 case RIP_NODE:
1106 case RIPNG_NODE:
1107 case BGP_NODE:
1108 case RMAP_NODE:
1109 case OSPF_NODE:
1110 case OSPF6_NODE:
jardin9e867fe2003-12-23 08:56:18 +00001111 case ISIS_NODE:
paul718e3742002-12-13 20:15:29 +00001112 case KEYCHAIN_NODE:
1113 case KEYCHAIN_KEY_NODE:
1114 case MASC_NODE:
1115 case VTY_NODE:
1116 vty_config_unlock (vty);
1117 vty->node = ENABLE_NODE;
1118 break;
1119 default:
1120 /* Unknown node, we have to ignore it. */
1121 break;
1122 }
1123 vty_prompt (vty);
1124
1125 /* Set history pointer to the latest one. */
1126 vty->hp = vty->hindex;
1127}
1128
1129/* Add current command line to the history buffer. */
1130static void
1131vty_hist_add (struct vty *vty)
1132{
1133 int index;
1134
1135 if (vty->length == 0)
1136 return;
1137
1138 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
1139
1140 /* Ignore the same string as previous one. */
1141 if (vty->hist[index])
1142 if (strcmp (vty->buf, vty->hist[index]) == 0)
1143 {
1144 vty->hp = vty->hindex;
1145 return;
1146 }
1147
1148 /* Insert history entry. */
1149 if (vty->hist[vty->hindex])
1150 XFREE (MTYPE_VTY_HIST, vty->hist[vty->hindex]);
1151 vty->hist[vty->hindex] = XSTRDUP (MTYPE_VTY_HIST, vty->buf);
1152
1153 /* History index rotation. */
1154 vty->hindex++;
1155 if (vty->hindex == VTY_MAXHIST)
1156 vty->hindex = 0;
1157
1158 vty->hp = vty->hindex;
1159}
1160
1161/* #define TELNET_OPTION_DEBUG */
1162
1163/* Get telnet window size. */
1164static int
1165vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
1166{
1167#ifdef TELNET_OPTION_DEBUG
1168 int i;
1169
1170 for (i = 0; i < nbytes; i++)
1171 {
1172 switch (buf[i])
1173 {
1174 case IAC:
1175 vty_out (vty, "IAC ");
1176 break;
1177 case WILL:
1178 vty_out (vty, "WILL ");
1179 break;
1180 case WONT:
1181 vty_out (vty, "WONT ");
1182 break;
1183 case DO:
1184 vty_out (vty, "DO ");
1185 break;
1186 case DONT:
1187 vty_out (vty, "DONT ");
1188 break;
1189 case SB:
1190 vty_out (vty, "SB ");
1191 break;
1192 case SE:
1193 vty_out (vty, "SE ");
1194 break;
1195 case TELOPT_ECHO:
1196 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
1197 break;
1198 case TELOPT_SGA:
1199 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
1200 break;
1201 case TELOPT_NAWS:
1202 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
1203 break;
1204 default:
1205 vty_out (vty, "%x ", buf[i]);
1206 break;
1207 }
1208 }
1209 vty_out (vty, "%s", VTY_NEWLINE);
1210
1211#endif /* TELNET_OPTION_DEBUG */
1212
1213 switch (buf[0])
1214 {
1215 case SB:
ajs9fc7ebf2005-02-23 15:12:34 +00001216 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001217 vty->iac_sb_in_progress = 1;
1218 return 0;
1219 break;
1220 case SE:
1221 {
paul718e3742002-12-13 20:15:29 +00001222 if (!vty->iac_sb_in_progress)
1223 return 0;
1224
ajs9fc7ebf2005-02-23 15:12:34 +00001225 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
paul718e3742002-12-13 20:15:29 +00001226 {
1227 vty->iac_sb_in_progress = 0;
1228 return 0;
1229 }
ajs9fc7ebf2005-02-23 15:12:34 +00001230 switch (vty->sb_buf[0])
paul718e3742002-12-13 20:15:29 +00001231 {
1232 case TELOPT_NAWS:
ajs9fc7ebf2005-02-23 15:12:34 +00001233 if (vty->sb_len != TELNET_NAWS_SB_LEN)
1234 zlog_warn("RFC 1073 violation detected: telnet NAWS option "
1235 "should send %d characters, but we received %lu",
1236 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
1237 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
1238 zlog_err("Bug detected: sizeof(vty->sb_buf) %lu < %d, "
1239 "too small to handle the telnet NAWS option",
1240 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
1241 else
1242 {
1243 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
1244 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
1245#ifdef TELNET_OPTION_DEBUG
1246 vty_out(vty, "TELNET NAWS window size negotiation completed: "
1247 "width %d, height %d%s",
1248 vty->width, vty->height, VTY_NEWLINE);
1249#endif
1250 }
paul718e3742002-12-13 20:15:29 +00001251 break;
1252 }
1253 vty->iac_sb_in_progress = 0;
1254 return 0;
1255 break;
1256 }
1257 default:
1258 break;
1259 }
1260 return 1;
1261}
1262
1263/* Execute current command line. */
1264static int
1265vty_execute (struct vty *vty)
1266{
1267 int ret;
1268
1269 ret = CMD_SUCCESS;
1270
1271 switch (vty->node)
1272 {
1273 case AUTH_NODE:
1274 case AUTH_ENABLE_NODE:
1275 vty_auth (vty, vty->buf);
1276 break;
1277 default:
1278 ret = vty_command (vty, vty->buf);
1279 if (vty->type == VTY_TERM)
1280 vty_hist_add (vty);
1281 break;
1282 }
1283
1284 /* Clear command line buffer. */
1285 vty->cp = vty->length = 0;
1286 vty_clear_buf (vty);
1287
ajs5a646652004-11-05 01:25:55 +00001288 if (vty->status != VTY_CLOSE )
paul718e3742002-12-13 20:15:29 +00001289 vty_prompt (vty);
1290
1291 return ret;
1292}
1293
1294#define CONTROL(X) ((X) - '@')
1295#define VTY_NORMAL 0
1296#define VTY_PRE_ESCAPE 1
1297#define VTY_ESCAPE 2
1298
1299/* Escape character command map. */
1300static void
1301vty_escape_map (unsigned char c, struct vty *vty)
1302{
1303 switch (c)
1304 {
1305 case ('A'):
1306 vty_previous_line (vty);
1307 break;
1308 case ('B'):
1309 vty_next_line (vty);
1310 break;
1311 case ('C'):
1312 vty_forward_char (vty);
1313 break;
1314 case ('D'):
1315 vty_backward_char (vty);
1316 break;
1317 default:
1318 break;
1319 }
1320
1321 /* Go back to normal mode. */
1322 vty->escape = VTY_NORMAL;
1323}
1324
1325/* Quit print out to the buffer. */
1326static void
1327vty_buffer_reset (struct vty *vty)
1328{
1329 buffer_reset (vty->obuf);
1330 vty_prompt (vty);
1331 vty_redraw_line (vty);
1332}
1333
1334/* Read data via vty socket. */
1335static int
1336vty_read (struct thread *thread)
1337{
1338 int i;
paul718e3742002-12-13 20:15:29 +00001339 int nbytes;
1340 unsigned char buf[VTY_READ_BUFSIZ];
1341
1342 int vty_sock = THREAD_FD (thread);
1343 struct vty *vty = THREAD_ARG (thread);
1344 vty->t_read = NULL;
1345
1346 /* Read raw data from socket */
ajs9fc7ebf2005-02-23 15:12:34 +00001347 if ((nbytes = read (vty->fd, buf, VTY_READ_BUFSIZ)) <= 0)
1348 {
1349 if (nbytes < 0)
1350 {
1351 if (ERRNO_IO_RETRY(errno))
1352 {
1353 vty_event (VTY_READ, vty_sock, vty);
1354 return 0;
1355 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001356 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001357 zlog_warn("%s: read error on vty client fd %d, closing: %s",
1358 __func__, vty->fd, safe_strerror(errno));
1359 }
1360 buffer_reset(vty->obuf);
1361 vty->status = VTY_CLOSE;
1362 }
paul718e3742002-12-13 20:15:29 +00001363
1364 for (i = 0; i < nbytes; i++)
1365 {
1366 if (buf[i] == IAC)
1367 {
1368 if (!vty->iac)
1369 {
1370 vty->iac = 1;
1371 continue;
1372 }
1373 else
1374 {
1375 vty->iac = 0;
1376 }
1377 }
1378
1379 if (vty->iac_sb_in_progress && !vty->iac)
1380 {
ajs9fc7ebf2005-02-23 15:12:34 +00001381 if (vty->sb_len < sizeof(vty->sb_buf))
1382 vty->sb_buf[vty->sb_len] = buf[i];
1383 vty->sb_len++;
paul718e3742002-12-13 20:15:29 +00001384 continue;
1385 }
1386
1387 if (vty->iac)
1388 {
1389 /* In case of telnet command */
paul5b8c1b02003-10-15 23:08:55 +00001390 int ret = 0;
paule9372532003-10-26 21:36:07 +00001391 ret = vty_telnet_option (vty, buf + i, nbytes - i);
paul718e3742002-12-13 20:15:29 +00001392 vty->iac = 0;
1393 i += ret;
1394 continue;
1395 }
paul5b8c1b02003-10-15 23:08:55 +00001396
paul718e3742002-12-13 20:15:29 +00001397
1398 if (vty->status == VTY_MORE)
1399 {
1400 switch (buf[i])
1401 {
1402 case CONTROL('C'):
1403 case 'q':
1404 case 'Q':
paul718e3742002-12-13 20:15:29 +00001405 vty_buffer_reset (vty);
1406 break;
1407#if 0 /* More line does not work for "show ip bgp". */
1408 case '\n':
1409 case '\r':
1410 vty->status = VTY_MORELINE;
1411 break;
1412#endif
1413 default:
paul718e3742002-12-13 20:15:29 +00001414 break;
1415 }
1416 continue;
1417 }
1418
1419 /* Escape character. */
1420 if (vty->escape == VTY_ESCAPE)
1421 {
1422 vty_escape_map (buf[i], vty);
1423 continue;
1424 }
1425
1426 /* Pre-escape status. */
1427 if (vty->escape == VTY_PRE_ESCAPE)
1428 {
1429 switch (buf[i])
1430 {
1431 case '[':
1432 vty->escape = VTY_ESCAPE;
1433 break;
1434 case 'b':
1435 vty_backward_word (vty);
1436 vty->escape = VTY_NORMAL;
1437 break;
1438 case 'f':
1439 vty_forward_word (vty);
1440 vty->escape = VTY_NORMAL;
1441 break;
1442 case 'd':
1443 vty_forward_kill_word (vty);
1444 vty->escape = VTY_NORMAL;
1445 break;
1446 case CONTROL('H'):
1447 case 0x7f:
1448 vty_backward_kill_word (vty);
1449 vty->escape = VTY_NORMAL;
1450 break;
1451 default:
1452 vty->escape = VTY_NORMAL;
1453 break;
1454 }
1455 continue;
1456 }
1457
1458 switch (buf[i])
1459 {
1460 case CONTROL('A'):
1461 vty_beginning_of_line (vty);
1462 break;
1463 case CONTROL('B'):
1464 vty_backward_char (vty);
1465 break;
1466 case CONTROL('C'):
1467 vty_stop_input (vty);
1468 break;
1469 case CONTROL('D'):
1470 vty_delete_char (vty);
1471 break;
1472 case CONTROL('E'):
1473 vty_end_of_line (vty);
1474 break;
1475 case CONTROL('F'):
1476 vty_forward_char (vty);
1477 break;
1478 case CONTROL('H'):
1479 case 0x7f:
1480 vty_delete_backward_char (vty);
1481 break;
1482 case CONTROL('K'):
1483 vty_kill_line (vty);
1484 break;
1485 case CONTROL('N'):
1486 vty_next_line (vty);
1487 break;
1488 case CONTROL('P'):
1489 vty_previous_line (vty);
1490 break;
1491 case CONTROL('T'):
1492 vty_transpose_chars (vty);
1493 break;
1494 case CONTROL('U'):
1495 vty_kill_line_from_beginning (vty);
1496 break;
1497 case CONTROL('W'):
1498 vty_backward_kill_word (vty);
1499 break;
1500 case CONTROL('Z'):
1501 vty_end_config (vty);
1502 break;
1503 case '\n':
1504 case '\r':
1505 vty_out (vty, "%s", VTY_NEWLINE);
1506 vty_execute (vty);
1507 break;
1508 case '\t':
1509 vty_complete_command (vty);
1510 break;
1511 case '?':
1512 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1513 vty_self_insert (vty, buf[i]);
1514 else
1515 vty_describe_command (vty);
1516 break;
1517 case '\033':
1518 if (i + 1 < nbytes && buf[i + 1] == '[')
1519 {
1520 vty->escape = VTY_ESCAPE;
1521 i++;
1522 }
1523 else
1524 vty->escape = VTY_PRE_ESCAPE;
1525 break;
1526 default:
1527 if (buf[i] > 31 && buf[i] < 127)
1528 vty_self_insert (vty, buf[i]);
1529 break;
1530 }
1531 }
1532
1533 /* Check status. */
1534 if (vty->status == VTY_CLOSE)
1535 vty_close (vty);
1536 else
1537 {
1538 vty_event (VTY_WRITE, vty_sock, vty);
1539 vty_event (VTY_READ, vty_sock, vty);
1540 }
1541 return 0;
1542}
1543
1544/* Flush buffer to the vty. */
1545static int
1546vty_flush (struct thread *thread)
1547{
1548 int erase;
ajs9fc7ebf2005-02-23 15:12:34 +00001549 buffer_status_t flushrc;
paul718e3742002-12-13 20:15:29 +00001550 int vty_sock = THREAD_FD (thread);
1551 struct vty *vty = THREAD_ARG (thread);
ajs9fc7ebf2005-02-23 15:12:34 +00001552
paul718e3742002-12-13 20:15:29 +00001553 vty->t_write = NULL;
1554
1555 /* Tempolary disable read thread. */
ajs9fc7ebf2005-02-23 15:12:34 +00001556 if ((vty->lines == 0) && vty->t_read)
1557 {
1558 thread_cancel (vty->t_read);
1559 vty->t_read = NULL;
1560 }
paul718e3742002-12-13 20:15:29 +00001561
1562 /* Function execution continue. */
ajs9fc7ebf2005-02-23 15:12:34 +00001563 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
paul718e3742002-12-13 20:15:29 +00001564
ajs9fc7ebf2005-02-23 15:12:34 +00001565 /* N.B. if width is 0, that means we don't know the window size. */
1566 if ((vty->lines == 0) || (vty->width == 0))
1567 flushrc = buffer_flush_available(vty->obuf, vty->fd);
1568 else if (vty->status == VTY_MORELINE)
1569 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1570 1, erase, 0);
1571 else
1572 flushrc = buffer_flush_window(vty->obuf, vty->fd, vty->width,
1573 vty->lines >= 0 ? vty->lines :
1574 vty->height,
1575 erase, 0);
1576 switch (flushrc)
1577 {
1578 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00001579 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00001580 zlog_warn("buffer_flush failed on vty client fd %d, closing",
1581 vty->fd);
1582 buffer_reset(vty->obuf);
1583 vty_close(vty);
1584 return 0;
1585 case BUFFER_EMPTY:
1586 if (vty->status == VTY_CLOSE)
1587 vty_close (vty);
paul718e3742002-12-13 20:15:29 +00001588 else
1589 {
ajs9fc7ebf2005-02-23 15:12:34 +00001590 vty->status = VTY_NORMAL;
paul718e3742002-12-13 20:15:29 +00001591 if (vty->lines == 0)
ajs9fc7ebf2005-02-23 15:12:34 +00001592 vty_event (VTY_READ, vty_sock, vty);
paul718e3742002-12-13 20:15:29 +00001593 }
ajs9fc7ebf2005-02-23 15:12:34 +00001594 break;
1595 case BUFFER_PENDING:
1596 /* There is more data waiting to be written. */
1597 vty->status = VTY_MORE;
1598 if (vty->lines == 0)
1599 vty_event (VTY_WRITE, vty_sock, vty);
1600 break;
1601 }
paul718e3742002-12-13 20:15:29 +00001602
1603 return 0;
1604}
1605
1606/* Create new vty structure. */
ajs9fc7ebf2005-02-23 15:12:34 +00001607static struct vty *
paul718e3742002-12-13 20:15:29 +00001608vty_create (int vty_sock, union sockunion *su)
1609{
1610 struct vty *vty;
1611
1612 /* Allocate new vty structure and set up default values. */
1613 vty = vty_new ();
1614 vty->fd = vty_sock;
1615 vty->type = VTY_TERM;
1616 vty->address = sockunion_su2str (su);
1617 if (no_password_check)
1618 {
1619 if (host.advanced)
1620 vty->node = ENABLE_NODE;
1621 else
1622 vty->node = VIEW_NODE;
1623 }
1624 else
1625 vty->node = AUTH_NODE;
1626 vty->fail = 0;
1627 vty->cp = 0;
1628 vty_clear_buf (vty);
1629 vty->length = 0;
1630 memset (vty->hist, 0, sizeof (vty->hist));
1631 vty->hp = 0;
1632 vty->hindex = 0;
1633 vector_set_index (vtyvec, vty_sock, vty);
1634 vty->status = VTY_NORMAL;
1635 vty->v_timeout = vty_timeout_val;
1636 if (host.lines >= 0)
1637 vty->lines = host.lines;
1638 else
1639 vty->lines = -1;
1640 vty->iac = 0;
1641 vty->iac_sb_in_progress = 0;
ajs9fc7ebf2005-02-23 15:12:34 +00001642 vty->sb_len = 0;
paul718e3742002-12-13 20:15:29 +00001643
1644 if (! no_password_check)
1645 {
1646 /* Vty is not available if password isn't set. */
1647 if (host.password == NULL && host.password_encrypt == NULL)
1648 {
1649 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1650 vty->status = VTY_CLOSE;
1651 vty_close (vty);
1652 return NULL;
1653 }
1654 }
1655
1656 /* Say hello to the world. */
1657 vty_hello (vty);
1658 if (! no_password_check)
1659 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1660
1661 /* Setting up terminal. */
1662 vty_will_echo (vty);
1663 vty_will_suppress_go_ahead (vty);
1664
1665 vty_dont_linemode (vty);
1666 vty_do_window_size (vty);
1667 /* vty_dont_lflow_ahead (vty); */
1668
1669 vty_prompt (vty);
1670
1671 /* Add read/write thread. */
1672 vty_event (VTY_WRITE, vty_sock, vty);
1673 vty_event (VTY_READ, vty_sock, vty);
1674
1675 return vty;
1676}
1677
1678/* Accept connection from the network. */
1679static int
1680vty_accept (struct thread *thread)
1681{
1682 int vty_sock;
1683 struct vty *vty;
1684 union sockunion su;
1685 int ret;
1686 unsigned int on;
1687 int accept_sock;
1688 struct prefix *p = NULL;
1689 struct access_list *acl = NULL;
1690
1691 accept_sock = THREAD_FD (thread);
1692
1693 /* We continue hearing vty socket. */
1694 vty_event (VTY_SERV, accept_sock, NULL);
1695
1696 memset (&su, 0, sizeof (union sockunion));
1697
1698 /* We can handle IPv4 or IPv6 socket. */
1699 vty_sock = sockunion_accept (accept_sock, &su);
1700 if (vty_sock < 0)
1701 {
ajs6099b3b2004-11-20 02:06:59 +00001702 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001703 return -1;
1704 }
ajs9fc7ebf2005-02-23 15:12:34 +00001705 set_nonblocking(vty_sock);
paul718e3742002-12-13 20:15:29 +00001706
1707 p = sockunion2hostprefix (&su);
1708
1709 /* VTY's accesslist apply. */
1710 if (p->family == AF_INET && vty_accesslist_name)
1711 {
1712 if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
1713 (access_list_apply (acl, p) == FILTER_DENY))
1714 {
1715 char *buf;
1716 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1717 (buf = sockunion_su2str (&su)));
1718 free (buf);
1719 close (vty_sock);
1720
1721 /* continue accepting connections */
1722 vty_event (VTY_SERV, accept_sock, NULL);
1723
1724 prefix_free (p);
1725
1726 return 0;
1727 }
1728 }
1729
1730#ifdef HAVE_IPV6
1731 /* VTY's ipv6 accesslist apply. */
1732 if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
1733 {
1734 if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
1735 (access_list_apply (acl, p) == FILTER_DENY))
1736 {
1737 char *buf;
1738 zlog (NULL, LOG_INFO, "Vty connection refused from %s",
1739 (buf = sockunion_su2str (&su)));
1740 free (buf);
1741 close (vty_sock);
1742
1743 /* continue accepting connections */
1744 vty_event (VTY_SERV, accept_sock, NULL);
1745
1746 prefix_free (p);
1747
1748 return 0;
1749 }
1750 }
1751#endif /* HAVE_IPV6 */
1752
1753 prefix_free (p);
1754
1755 on = 1;
1756 ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,
1757 (char *) &on, sizeof (on));
1758 if (ret < 0)
1759 zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s",
ajs6099b3b2004-11-20 02:06:59 +00001760 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001761
1762 vty = vty_create (vty_sock, &su);
1763
1764 return 0;
1765}
1766
1767#if defined(HAVE_IPV6) && !defined(NRL)
ajs9fc7ebf2005-02-23 15:12:34 +00001768static void
paul718e3742002-12-13 20:15:29 +00001769vty_serv_sock_addrinfo (const char *hostname, unsigned short port)
1770{
1771 int ret;
1772 struct addrinfo req;
1773 struct addrinfo *ainfo;
1774 struct addrinfo *ainfo_save;
1775 int sock;
1776 char port_str[BUFSIZ];
1777
1778 memset (&req, 0, sizeof (struct addrinfo));
1779 req.ai_flags = AI_PASSIVE;
1780 req.ai_family = AF_UNSPEC;
1781 req.ai_socktype = SOCK_STREAM;
1782 sprintf (port_str, "%d", port);
1783 port_str[sizeof (port_str) - 1] = '\0';
1784
1785 ret = getaddrinfo (hostname, port_str, &req, &ainfo);
1786
1787 if (ret != 0)
1788 {
1789 fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
1790 exit (1);
1791 }
1792
1793 ainfo_save = ainfo;
1794
1795 do
1796 {
1797 if (ainfo->ai_family != AF_INET
1798#ifdef HAVE_IPV6
1799 && ainfo->ai_family != AF_INET6
1800#endif /* HAVE_IPV6 */
1801 )
1802 continue;
1803
1804 sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
1805 if (sock < 0)
1806 continue;
1807
1808 sockopt_reuseaddr (sock);
1809 sockopt_reuseport (sock);
1810
1811 ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
1812 if (ret < 0)
1813 {
1814 close (sock); /* Avoid sd leak. */
1815 continue;
1816 }
1817
1818 ret = listen (sock, 3);
1819 if (ret < 0)
1820 {
1821 close (sock); /* Avoid sd leak. */
1822 continue;
1823 }
1824
1825 vty_event (VTY_SERV, sock, NULL);
1826 }
1827 while ((ainfo = ainfo->ai_next) != NULL);
1828
1829 freeaddrinfo (ainfo_save);
1830}
1831#endif /* HAVE_IPV6 && ! NRL */
1832
1833/* Make vty server socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001834static void
paul29db05b2003-05-08 20:10:22 +00001835vty_serv_sock_family (const char* addr, unsigned short port, int family)
paul718e3742002-12-13 20:15:29 +00001836{
1837 int ret;
1838 union sockunion su;
1839 int accept_sock;
paul29db05b2003-05-08 20:10:22 +00001840 void* naddr=NULL;
paul718e3742002-12-13 20:15:29 +00001841
1842 memset (&su, 0, sizeof (union sockunion));
1843 su.sa.sa_family = family;
paul29db05b2003-05-08 20:10:22 +00001844 if(addr)
1845 switch(family)
1846 {
1847 case AF_INET:
1848 naddr=&su.sin.sin_addr;
1849#ifdef HAVE_IPV6
1850 case AF_INET6:
1851 naddr=&su.sin6.sin6_addr;
1852#endif
1853 }
1854
1855 if(naddr)
1856 switch(inet_pton(family,addr,naddr))
1857 {
1858 case -1:
1859 zlog_err("bad address %s",addr);
1860 naddr=NULL;
1861 break;
1862 case 0:
ajs6099b3b2004-11-20 02:06:59 +00001863 zlog_err("error translating address %s: %s",addr,safe_strerror(errno));
paul29db05b2003-05-08 20:10:22 +00001864 naddr=NULL;
1865 }
paul718e3742002-12-13 20:15:29 +00001866
1867 /* Make new socket. */
1868 accept_sock = sockunion_stream_socket (&su);
1869 if (accept_sock < 0)
1870 return;
1871
1872 /* This is server, so reuse address. */
1873 sockopt_reuseaddr (accept_sock);
1874 sockopt_reuseport (accept_sock);
1875
1876 /* Bind socket to universal address and given port. */
paul29db05b2003-05-08 20:10:22 +00001877 ret = sockunion_bind (accept_sock, &su, port, naddr);
paul718e3742002-12-13 20:15:29 +00001878 if (ret < 0)
1879 {
paul29db05b2003-05-08 20:10:22 +00001880 zlog_warn("can't bind socket");
paul718e3742002-12-13 20:15:29 +00001881 close (accept_sock); /* Avoid sd leak. */
1882 return;
1883 }
1884
1885 /* Listen socket under queue 3. */
1886 ret = listen (accept_sock, 3);
1887 if (ret < 0)
1888 {
1889 zlog (NULL, LOG_WARNING, "can't listen socket");
1890 close (accept_sock); /* Avoid sd leak. */
1891 return;
1892 }
1893
1894 /* Add vty server event. */
1895 vty_event (VTY_SERV, accept_sock, NULL);
1896}
1897
1898#ifdef VTYSH
1899/* For sockaddr_un. */
1900#include <sys/un.h>
1901
1902/* VTY shell UNIX domain socket. */
ajs9fc7ebf2005-02-23 15:12:34 +00001903static void
hasso6ad96ea2004-10-07 19:33:46 +00001904vty_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001905{
1906 int ret;
paul75e15fe2004-10-31 02:13:09 +00001907 int sock, len;
paul718e3742002-12-13 20:15:29 +00001908 struct sockaddr_un serv;
1909 mode_t old_mask;
pauledd7c242003-06-04 13:59:38 +00001910 struct zprivs_ids_t ids;
1911
paul718e3742002-12-13 20:15:29 +00001912 /* First of all, unlink existing socket */
1913 unlink (path);
1914
1915 /* Set umask */
paul1921e6f2003-05-23 08:12:36 +00001916 old_mask = umask (0007);
paul718e3742002-12-13 20:15:29 +00001917
1918 /* Make UNIX domain socket. */
1919 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1920 if (sock < 0)
1921 {
ajs6a52d0d2005-01-30 18:49:28 +00001922 zlog_err("Cannot create unix stream socket: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001923 return;
1924 }
1925
1926 /* Make server socket. */
1927 memset (&serv, 0, sizeof (struct sockaddr_un));
1928 serv.sun_family = AF_UNIX;
1929 strncpy (serv.sun_path, path, strlen (path));
1930#ifdef HAVE_SUN_LEN
1931 len = serv.sun_len = SUN_LEN(&serv);
1932#else
1933 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
1934#endif /* HAVE_SUN_LEN */
1935
1936 ret = bind (sock, (struct sockaddr *) &serv, len);
1937 if (ret < 0)
1938 {
ajs6a52d0d2005-01-30 18:49:28 +00001939 zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001940 close (sock); /* Avoid sd leak. */
1941 return;
1942 }
1943
1944 ret = listen (sock, 5);
1945 if (ret < 0)
1946 {
ajs6a52d0d2005-01-30 18:49:28 +00001947 zlog_err("listen(fd %d) failed: %s", sock, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +00001948 close (sock); /* Avoid sd leak. */
1949 return;
1950 }
1951
1952 umask (old_mask);
1953
pauledd7c242003-06-04 13:59:38 +00001954 zprivs_get_ids(&ids);
1955
1956 if (ids.gid_vty > 0)
1957 {
1958 /* set group of socket */
1959 if ( chown (path, -1, ids.gid_vty) )
1960 {
1961 zlog_err ("vty_serv_un: could chown socket, %s",
ajs6099b3b2004-11-20 02:06:59 +00001962 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +00001963 }
1964 }
1965
paul718e3742002-12-13 20:15:29 +00001966 vty_event (VTYSH_SERV, sock, NULL);
1967}
1968
1969/* #define VTYSH_DEBUG 1 */
1970
1971static int
1972vtysh_accept (struct thread *thread)
1973{
1974 int accept_sock;
1975 int sock;
1976 int client_len;
1977 struct sockaddr_un client;
1978 struct vty *vty;
1979
1980 accept_sock = THREAD_FD (thread);
1981
1982 vty_event (VTYSH_SERV, accept_sock, NULL);
1983
1984 memset (&client, 0, sizeof (struct sockaddr_un));
1985 client_len = sizeof (struct sockaddr_un);
1986
hassoe473b032004-09-26 16:08:11 +00001987 sock = accept (accept_sock, (struct sockaddr *) &client,
1988 (socklen_t *) &client_len);
paul718e3742002-12-13 20:15:29 +00001989
1990 if (sock < 0)
1991 {
ajs6099b3b2004-11-20 02:06:59 +00001992 zlog_warn ("can't accept vty socket : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001993 return -1;
1994 }
1995
ajs9fc7ebf2005-02-23 15:12:34 +00001996 if (set_nonblocking(sock) < 0)
paul75e15fe2004-10-31 02:13:09 +00001997 {
ajs9fc7ebf2005-02-23 15:12:34 +00001998 zlog_warn ("vtysh_accept: could not set vty socket %d to non-blocking,"
1999 " %s, closing", sock, safe_strerror (errno));
paul75e15fe2004-10-31 02:13:09 +00002000 close (sock);
2001 return -1;
2002 }
pauldccfb192004-10-29 08:29:36 +00002003
paul718e3742002-12-13 20:15:29 +00002004#ifdef VTYSH_DEBUG
2005 printf ("VTY shell accept\n");
2006#endif /* VTYSH_DEBUG */
2007
2008 vty = vty_new ();
2009 vty->fd = sock;
2010 vty->type = VTY_SHELL_SERV;
2011 vty->node = VIEW_NODE;
2012
2013 vty_event (VTYSH_READ, sock, vty);
2014
2015 return 0;
2016}
2017
2018static int
ajs9fc7ebf2005-02-23 15:12:34 +00002019vtysh_flush(struct vty *vty)
2020{
2021 switch (buffer_flush_available(vty->obuf, vty->fd))
2022 {
2023 case BUFFER_PENDING:
2024 vty_event(VTYSH_WRITE, vty->fd, vty);
2025 break;
2026 case BUFFER_ERROR:
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002027 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002028 zlog_warn("%s: write error to fd %d, closing", __func__, vty->fd);
2029 buffer_reset(vty->obuf);
2030 vty_close(vty);
2031 return -1;
2032 break;
2033 case BUFFER_EMPTY:
2034 break;
2035 }
2036 return 0;
2037}
2038
2039static int
paul718e3742002-12-13 20:15:29 +00002040vtysh_read (struct thread *thread)
2041{
2042 int ret;
2043 int sock;
2044 int nbytes;
2045 struct vty *vty;
2046 unsigned char buf[VTY_READ_BUFSIZ];
ajs9fc7ebf2005-02-23 15:12:34 +00002047 unsigned char *p;
paul718e3742002-12-13 20:15:29 +00002048 u_char header[4] = {0, 0, 0, 0};
2049
2050 sock = THREAD_FD (thread);
2051 vty = THREAD_ARG (thread);
2052 vty->t_read = NULL;
2053
ajs9fc7ebf2005-02-23 15:12:34 +00002054 if ((nbytes = read (sock, buf, VTY_READ_BUFSIZ)) <= 0)
paul718e3742002-12-13 20:15:29 +00002055 {
ajs9fc7ebf2005-02-23 15:12:34 +00002056 if (nbytes < 0)
2057 {
2058 if (ERRNO_IO_RETRY(errno))
2059 {
2060 vty_event (VTYSH_READ, sock, vty);
2061 return 0;
2062 }
Andrew J. Schorr74542d72006-07-10 18:09:42 +00002063 vty->monitor = 0; /* disable monitoring to avoid infinite recursion */
ajs9fc7ebf2005-02-23 15:12:34 +00002064 zlog_warn("%s: read failed on vtysh client fd %d, closing: %s",
2065 __func__, sock, safe_strerror(errno));
2066 }
2067 buffer_reset(vty->obuf);
paul718e3742002-12-13 20:15:29 +00002068 vty_close (vty);
2069#ifdef VTYSH_DEBUG
2070 printf ("close vtysh\n");
2071#endif /* VTYSH_DEBUG */
2072 return 0;
2073 }
2074
2075#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002076 printf ("line: %.*s\n", nbytes, buf);
paul718e3742002-12-13 20:15:29 +00002077#endif /* VTYSH_DEBUG */
2078
ajs9fc7ebf2005-02-23 15:12:34 +00002079 for (p = buf; p < buf+nbytes; p++)
2080 {
2081 vty_ensure(vty, vty->length+1);
2082 vty->buf[vty->length++] = *p;
2083 if (*p == '\0')
2084 {
2085 /* Pass this line to parser. */
2086 ret = vty_execute (vty);
2087 /* Note that vty_execute clears the command buffer and resets
2088 vty->length to 0. */
paul718e3742002-12-13 20:15:29 +00002089
ajs9fc7ebf2005-02-23 15:12:34 +00002090 /* Return result. */
paul718e3742002-12-13 20:15:29 +00002091#ifdef VTYSH_DEBUG
ajs9fc7ebf2005-02-23 15:12:34 +00002092 printf ("result: %d\n", ret);
2093 printf ("vtysh node: %d\n", vty->node);
paul718e3742002-12-13 20:15:29 +00002094#endif /* VTYSH_DEBUG */
2095
ajs9fc7ebf2005-02-23 15:12:34 +00002096 header[3] = ret;
2097 buffer_put(vty->obuf, header, 4);
paul718e3742002-12-13 20:15:29 +00002098
ajs9fc7ebf2005-02-23 15:12:34 +00002099 if (!vty->t_write && (vtysh_flush(vty) < 0))
2100 /* Try to flush results; exit if a write error occurs. */
2101 return 0;
2102 }
2103 }
2104
paul718e3742002-12-13 20:15:29 +00002105 vty_event (VTYSH_READ, sock, vty);
2106
2107 return 0;
2108}
ajs49ff6d92004-11-04 19:26:16 +00002109
2110static int
2111vtysh_write (struct thread *thread)
2112{
2113 struct vty *vty = THREAD_ARG (thread);
2114
2115 vty->t_write = NULL;
ajs9fc7ebf2005-02-23 15:12:34 +00002116 vtysh_flush(vty);
ajs976d8c72004-11-10 15:40:09 +00002117 return 0;
ajs49ff6d92004-11-04 19:26:16 +00002118}
2119
paul718e3742002-12-13 20:15:29 +00002120#endif /* VTYSH */
2121
2122/* Determine address family to bind. */
2123void
hasso6ad96ea2004-10-07 19:33:46 +00002124vty_serv_sock (const char *addr, unsigned short port, const char *path)
paul718e3742002-12-13 20:15:29 +00002125{
2126 /* If port is set to 0, do not listen on TCP/IP at all! */
2127 if (port)
2128 {
2129
2130#ifdef HAVE_IPV6
2131#ifdef NRL
paul29db05b2003-05-08 20:10:22 +00002132 vty_serv_sock_family (addr, port, AF_INET);
2133 vty_serv_sock_family (addr, port, AF_INET6);
paul718e3742002-12-13 20:15:29 +00002134#else /* ! NRL */
paul29db05b2003-05-08 20:10:22 +00002135 vty_serv_sock_addrinfo (addr, port);
paul718e3742002-12-13 20:15:29 +00002136#endif /* NRL*/
2137#else /* ! HAVE_IPV6 */
paul29db05b2003-05-08 20:10:22 +00002138 vty_serv_sock_family (addr,port, AF_INET);
paul718e3742002-12-13 20:15:29 +00002139#endif /* HAVE_IPV6 */
2140 }
2141
2142#ifdef VTYSH
2143 vty_serv_un (path);
2144#endif /* VTYSH */
2145}
2146
Andrew J. Schorr9d0a3262006-07-11 00:06:49 +00002147/* Close vty interface. Warning: call this only from functions that
2148 will be careful not to access the vty afterwards (since it has
2149 now been freed). This is safest from top-level functions (called
2150 directly by the thread dispatcher). */
paul718e3742002-12-13 20:15:29 +00002151void
2152vty_close (struct vty *vty)
2153{
2154 int i;
2155
2156 /* Cancel threads.*/
2157 if (vty->t_read)
2158 thread_cancel (vty->t_read);
2159 if (vty->t_write)
2160 thread_cancel (vty->t_write);
2161 if (vty->t_timeout)
2162 thread_cancel (vty->t_timeout);
paul718e3742002-12-13 20:15:29 +00002163
2164 /* Flush buffer. */
ajs9fc7ebf2005-02-23 15:12:34 +00002165 buffer_flush_all (vty->obuf, vty->fd);
paul718e3742002-12-13 20:15:29 +00002166
2167 /* Free input buffer. */
2168 buffer_free (vty->obuf);
2169
paul718e3742002-12-13 20:15:29 +00002170 /* Free command history. */
2171 for (i = 0; i < VTY_MAXHIST; i++)
2172 if (vty->hist[i])
2173 XFREE (MTYPE_VTY_HIST, vty->hist[i]);
2174
2175 /* Unset vector. */
2176 vector_unset (vtyvec, vty->fd);
2177
2178 /* Close socket. */
2179 if (vty->fd > 0)
2180 close (vty->fd);
2181
2182 if (vty->address)
paul05865c92005-10-26 05:49:54 +00002183 XFREE (MTYPE_TMP, vty->address);
paul718e3742002-12-13 20:15:29 +00002184 if (vty->buf)
2185 XFREE (MTYPE_VTY, vty->buf);
2186
2187 /* Check configure. */
2188 vty_config_unlock (vty);
2189
2190 /* OK free vty. */
2191 XFREE (MTYPE_VTY, vty);
2192}
2193
2194/* When time out occur output message then close connection. */
2195static int
2196vty_timeout (struct thread *thread)
2197{
2198 struct vty *vty;
2199
2200 vty = THREAD_ARG (thread);
2201 vty->t_timeout = NULL;
2202 vty->v_timeout = 0;
2203
2204 /* Clear buffer*/
2205 buffer_reset (vty->obuf);
2206 vty_out (vty, "%sVty connection is timed out.%s", VTY_NEWLINE, VTY_NEWLINE);
2207
2208 /* Close connection. */
2209 vty->status = VTY_CLOSE;
2210 vty_close (vty);
2211
2212 return 0;
2213}
2214
2215/* Read up configuration file from file_name. */
2216static void
2217vty_read_file (FILE *confp)
2218{
2219 int ret;
2220 struct vty *vty;
2221
2222 vty = vty_new ();
2223 vty->fd = 0; /* stdout */
2224 vty->type = VTY_TERM;
2225 vty->node = CONFIG_NODE;
2226
2227 /* Execute configuration file */
2228 ret = config_from_file (vty, confp);
2229
paul7021c422003-07-15 12:52:22 +00002230 if ( !((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO)) )
paul718e3742002-12-13 20:15:29 +00002231 {
2232 switch (ret)
paul7021c422003-07-15 12:52:22 +00002233 {
2234 case CMD_ERR_AMBIGUOUS:
2235 fprintf (stderr, "Ambiguous command.\n");
2236 break;
2237 case CMD_ERR_NO_MATCH:
2238 fprintf (stderr, "There is no such command.\n");
2239 break;
2240 }
paul718e3742002-12-13 20:15:29 +00002241 fprintf (stderr, "Error occured during reading below line.\n%s\n",
2242 vty->buf);
2243 vty_close (vty);
2244 exit (1);
2245 }
2246
2247 vty_close (vty);
2248}
2249
ajs9fc7ebf2005-02-23 15:12:34 +00002250static FILE *
paul718e3742002-12-13 20:15:29 +00002251vty_use_backup_config (char *fullpath)
2252{
2253 char *fullpath_sav, *fullpath_tmp;
2254 FILE *ret = NULL;
2255 struct stat buf;
2256 int tmp, sav;
2257 int c;
2258 char buffer[512];
2259
2260 fullpath_sav = malloc (strlen (fullpath) + strlen (CONF_BACKUP_EXT) + 1);
2261 strcpy (fullpath_sav, fullpath);
2262 strcat (fullpath_sav, CONF_BACKUP_EXT);
2263 if (stat (fullpath_sav, &buf) == -1)
2264 {
2265 free (fullpath_sav);
2266 return NULL;
2267 }
2268
2269 fullpath_tmp = malloc (strlen (fullpath) + 8);
2270 sprintf (fullpath_tmp, "%s.XXXXXX", fullpath);
2271
2272 /* Open file to configuration write. */
2273 tmp = mkstemp (fullpath_tmp);
2274 if (tmp < 0)
2275 {
2276 free (fullpath_sav);
2277 free (fullpath_tmp);
2278 return NULL;
2279 }
2280
2281 sav = open (fullpath_sav, O_RDONLY);
2282 if (sav < 0)
2283 {
gdt3dbf9962003-12-22 20:18:18 +00002284 unlink (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002285 free (fullpath_sav);
2286 free (fullpath_tmp);
paul718e3742002-12-13 20:15:29 +00002287 return NULL;
2288 }
2289
2290 while((c = read (sav, buffer, 512)) > 0)
2291 write (tmp, buffer, c);
2292
2293 close (sav);
2294 close (tmp);
2295
gdtaa593d52003-12-22 20:15:53 +00002296 if (chmod(fullpath_tmp, CONFIGFILE_MASK) != 0)
2297 {
gdt3dbf9962003-12-22 20:18:18 +00002298 unlink (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002299 free (fullpath_sav);
2300 free (fullpath_tmp);
gdtaa593d52003-12-22 20:15:53 +00002301 return NULL;
2302 }
2303
paul718e3742002-12-13 20:15:29 +00002304 if (link (fullpath_tmp, fullpath) == 0)
2305 ret = fopen (fullpath, "r");
2306
2307 unlink (fullpath_tmp);
2308
2309 free (fullpath_sav);
2310 free (fullpath_tmp);
hasso12f6ea22005-03-07 08:35:39 +00002311 return ret;
paul718e3742002-12-13 20:15:29 +00002312}
2313
2314/* Read up configuration file from file_name. */
2315void
2316vty_read_config (char *config_file,
hasso320ec102004-06-20 19:54:37 +00002317 char *config_default_dir)
paul718e3742002-12-13 20:15:29 +00002318{
paulccc92352003-10-22 02:49:38 +00002319 char cwd[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +00002320 FILE *confp = NULL;
2321 char *fullpath;
paul05865c92005-10-26 05:49:54 +00002322 char *tmp = NULL;
paul718e3742002-12-13 20:15:29 +00002323
2324 /* If -f flag specified. */
2325 if (config_file != NULL)
2326 {
2327 if (! IS_DIRECTORY_SEP (config_file[0]))
hasso320ec102004-06-20 19:54:37 +00002328 {
2329 getcwd (cwd, MAXPATHLEN);
paul05865c92005-10-26 05:49:54 +00002330 tmp = XMALLOC (MTYPE_TMP,
hasso320ec102004-06-20 19:54:37 +00002331 strlen (cwd) + strlen (config_file) + 2);
paul05865c92005-10-26 05:49:54 +00002332 sprintf (tmp, "%s/%s", cwd, config_file);
2333 fullpath = tmp;
hasso320ec102004-06-20 19:54:37 +00002334 }
paul718e3742002-12-13 20:15:29 +00002335 else
hasso320ec102004-06-20 19:54:37 +00002336 fullpath = config_file;
paul718e3742002-12-13 20:15:29 +00002337
2338 confp = fopen (fullpath, "r");
2339
2340 if (confp == NULL)
hasso320ec102004-06-20 19:54:37 +00002341 {
paul3d1dc852005-04-05 00:45:23 +00002342 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2343 __func__, fullpath, safe_strerror (errno));
2344
hasso320ec102004-06-20 19:54:37 +00002345 confp = vty_use_backup_config (fullpath);
2346 if (confp)
2347 fprintf (stderr, "WARNING: using backup configuration file!\n");
2348 else
2349 {
2350 fprintf (stderr, "can't open configuration file [%s]\n",
paul3d1dc852005-04-05 00:45:23 +00002351 config_file);
hasso320ec102004-06-20 19:54:37 +00002352 exit(1);
2353 }
2354 }
paul718e3742002-12-13 20:15:29 +00002355 }
2356 else
2357 {
paul718e3742002-12-13 20:15:29 +00002358#ifdef VTYSH
hasso320ec102004-06-20 19:54:37 +00002359 int ret;
2360 struct stat conf_stat;
paul718e3742002-12-13 20:15:29 +00002361
hasso320ec102004-06-20 19:54:37 +00002362 /* !!!!PLEASE LEAVE!!!!
2363 * This is NEEDED for use with vtysh -b, or else you can get
2364 * a real configuration food fight with a lot garbage in the
2365 * merged configuration file it creates coming from the per
2366 * daemon configuration files. This also allows the daemons
2367 * to start if there default configuration file is not
2368 * present or ignore them, as needed when using vtysh -b to
2369 * configure the daemons at boot - MAG
2370 */
paul718e3742002-12-13 20:15:29 +00002371
hasso320ec102004-06-20 19:54:37 +00002372 /* Stat for vtysh Zebra.conf, if found startup and wait for
2373 * boot configuration
2374 */
paul718e3742002-12-13 20:15:29 +00002375
hasso320ec102004-06-20 19:54:37 +00002376 if ( strstr(config_default_dir, "vtysh") == NULL)
2377 {
2378 ret = stat (integrate_default, &conf_stat);
2379 if (ret >= 0)
2380 return;
2381 }
paul718e3742002-12-13 20:15:29 +00002382#endif /* VTYSH */
2383
hasso320ec102004-06-20 19:54:37 +00002384 confp = fopen (config_default_dir, "r");
2385 if (confp == NULL)
2386 {
paul3d1dc852005-04-05 00:45:23 +00002387 fprintf (stderr, "%s: failed to open configuration file %s: %s\n",
2388 __func__, config_default_dir, safe_strerror (errno));
2389
hasso320ec102004-06-20 19:54:37 +00002390 confp = vty_use_backup_config (config_default_dir);
2391 if (confp)
2392 {
2393 fprintf (stderr, "WARNING: using backup configuration file!\n");
2394 fullpath = config_default_dir;
2395 }
2396 else
2397 {
2398 fprintf (stderr, "can't open configuration file [%s]\n",
2399 config_default_dir);
2400 exit (1);
paul3d1dc852005-04-05 00:45:23 +00002401 }
hasso320ec102004-06-20 19:54:37 +00002402 }
paul718e3742002-12-13 20:15:29 +00002403 else
hasso320ec102004-06-20 19:54:37 +00002404 fullpath = config_default_dir;
2405 }
2406
paul718e3742002-12-13 20:15:29 +00002407 vty_read_file (confp);
2408
2409 fclose (confp);
2410
2411 host_config_set (fullpath);
paul05865c92005-10-26 05:49:54 +00002412
2413 if (tmp)
2414 XFREE (MTYPE_TMP, fullpath);
paul718e3742002-12-13 20:15:29 +00002415}
2416
2417/* Small utility function which output log to the VTY. */
2418void
ajs274a4a42004-12-07 15:39:31 +00002419vty_log (const char *level, const char *proto_str,
2420 const char *format, va_list va)
paul718e3742002-12-13 20:15:29 +00002421{
hasso8c328f12004-10-05 21:01:23 +00002422 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002423 struct vty *vty;
Paul Jakmaa4b30302006-05-28 08:18:38 +00002424
2425 if (!vtyvec)
2426 return;
paul718e3742002-12-13 20:15:29 +00002427
paul55468c82005-03-14 20:19:01 +00002428 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002429 if ((vty = vector_slot (vtyvec, i)) != NULL)
2430 if (vty->monitor)
ajsd246bd92004-11-23 17:35:08 +00002431 {
2432 va_list ac;
2433 va_copy(ac, va);
ajs274a4a42004-12-07 15:39:31 +00002434 vty_log_out (vty, level, proto_str, format, ac);
ajsd246bd92004-11-23 17:35:08 +00002435 va_end(ac);
2436 }
paul718e3742002-12-13 20:15:29 +00002437}
2438
ajs274a4a42004-12-07 15:39:31 +00002439/* Async-signal-safe version of vty_log for fixed strings. */
2440void
2441vty_log_fixed (const char *buf, size_t len)
2442{
2443 unsigned int i;
ajs9fc7ebf2005-02-23 15:12:34 +00002444 struct iovec iov[2];
2445
Paul Jakmaa4b30302006-05-28 08:18:38 +00002446 /* vty may not have been initialised */
2447 if (!vtyvec)
2448 return;
2449
ajs926fe8f2005-04-08 18:50:40 +00002450 iov[0].iov_base = (void *)buf;
ajs9fc7ebf2005-02-23 15:12:34 +00002451 iov[0].iov_len = len;
ajs926fe8f2005-04-08 18:50:40 +00002452 iov[1].iov_base = (void *)"\r\n";
ajs9fc7ebf2005-02-23 15:12:34 +00002453 iov[1].iov_len = 2;
ajs274a4a42004-12-07 15:39:31 +00002454
paul55468c82005-03-14 20:19:01 +00002455 for (i = 0; i < vector_active (vtyvec); i++)
ajs274a4a42004-12-07 15:39:31 +00002456 {
2457 struct vty *vty;
ajs9fc7ebf2005-02-23 15:12:34 +00002458 if (((vty = vector_slot (vtyvec, i)) != NULL) && vty->monitor)
2459 /* N.B. We don't care about the return code, since process is
2460 most likely just about to die anyway. */
2461 writev(vty->fd, iov, 2);
ajs274a4a42004-12-07 15:39:31 +00002462 }
2463}
2464
paul718e3742002-12-13 20:15:29 +00002465int
2466vty_config_lock (struct vty *vty)
2467{
2468 if (vty_config == 0)
2469 {
2470 vty->config = 1;
2471 vty_config = 1;
2472 }
2473 return vty->config;
2474}
2475
2476int
2477vty_config_unlock (struct vty *vty)
2478{
2479 if (vty_config == 1 && vty->config == 1)
2480 {
2481 vty->config = 0;
2482 vty_config = 0;
2483 }
2484 return vty->config;
2485}
2486
2487/* Master of the threads. */
paulb21b19c2003-06-15 01:28:29 +00002488static struct thread_master *master;
paul718e3742002-12-13 20:15:29 +00002489
2490static void
2491vty_event (enum event event, int sock, struct vty *vty)
2492{
2493 struct thread *vty_serv_thread;
2494
2495 switch (event)
2496 {
2497 case VTY_SERV:
2498 vty_serv_thread = thread_add_read (master, vty_accept, vty, sock);
2499 vector_set_index (Vvty_serv_thread, sock, vty_serv_thread);
2500 break;
2501#ifdef VTYSH
2502 case VTYSH_SERV:
2503 thread_add_read (master, vtysh_accept, vty, sock);
2504 break;
2505 case VTYSH_READ:
ajs49ff6d92004-11-04 19:26:16 +00002506 vty->t_read = thread_add_read (master, vtysh_read, vty, sock);
2507 break;
2508 case VTYSH_WRITE:
2509 vty->t_write = thread_add_write (master, vtysh_write, vty, sock);
paul718e3742002-12-13 20:15:29 +00002510 break;
2511#endif /* VTYSH */
2512 case VTY_READ:
2513 vty->t_read = thread_add_read (master, vty_read, vty, sock);
2514
2515 /* Time out treatment. */
2516 if (vty->v_timeout)
2517 {
2518 if (vty->t_timeout)
2519 thread_cancel (vty->t_timeout);
2520 vty->t_timeout =
2521 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2522 }
2523 break;
2524 case VTY_WRITE:
2525 if (! vty->t_write)
2526 vty->t_write = thread_add_write (master, vty_flush, vty, sock);
2527 break;
2528 case VTY_TIMEOUT_RESET:
2529 if (vty->t_timeout)
2530 {
2531 thread_cancel (vty->t_timeout);
2532 vty->t_timeout = NULL;
2533 }
2534 if (vty->v_timeout)
2535 {
2536 vty->t_timeout =
2537 thread_add_timer (master, vty_timeout, vty, vty->v_timeout);
2538 }
2539 break;
2540 }
2541}
2542
2543DEFUN (config_who,
2544 config_who_cmd,
2545 "who",
2546 "Display who is on vty\n")
2547{
hasso8c328f12004-10-05 21:01:23 +00002548 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002549 struct vty *v;
2550
paul55468c82005-03-14 20:19:01 +00002551 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002552 if ((v = vector_slot (vtyvec, i)) != NULL)
2553 vty_out (vty, "%svty[%d] connected from %s.%s",
2554 v->config ? "*" : " ",
2555 i, v->address, VTY_NEWLINE);
2556 return CMD_SUCCESS;
2557}
2558
2559/* Move to vty configuration mode. */
2560DEFUN (line_vty,
2561 line_vty_cmd,
2562 "line vty",
2563 "Configure a terminal line\n"
2564 "Virtual terminal\n")
2565{
2566 vty->node = VTY_NODE;
2567 return CMD_SUCCESS;
2568}
2569
2570/* Set time out value. */
ajs9fc7ebf2005-02-23 15:12:34 +00002571static int
paul9035efa2004-10-10 11:56:56 +00002572exec_timeout (struct vty *vty, const char *min_str, const char *sec_str)
paul718e3742002-12-13 20:15:29 +00002573{
2574 unsigned long timeout = 0;
2575
2576 /* min_str and sec_str are already checked by parser. So it must be
2577 all digit string. */
2578 if (min_str)
2579 {
2580 timeout = strtol (min_str, NULL, 10);
2581 timeout *= 60;
2582 }
2583 if (sec_str)
2584 timeout += strtol (sec_str, NULL, 10);
2585
2586 vty_timeout_val = timeout;
2587 vty->v_timeout = timeout;
2588 vty_event (VTY_TIMEOUT_RESET, 0, vty);
2589
2590
2591 return CMD_SUCCESS;
2592}
2593
2594DEFUN (exec_timeout_min,
2595 exec_timeout_min_cmd,
2596 "exec-timeout <0-35791>",
2597 "Set timeout value\n"
2598 "Timeout value in minutes\n")
2599{
2600 return exec_timeout (vty, argv[0], NULL);
2601}
2602
2603DEFUN (exec_timeout_sec,
2604 exec_timeout_sec_cmd,
2605 "exec-timeout <0-35791> <0-2147483>",
2606 "Set the EXEC timeout\n"
2607 "Timeout in minutes\n"
2608 "Timeout in seconds\n")
2609{
2610 return exec_timeout (vty, argv[0], argv[1]);
2611}
2612
2613DEFUN (no_exec_timeout,
2614 no_exec_timeout_cmd,
2615 "no exec-timeout",
2616 NO_STR
2617 "Set the EXEC timeout\n")
2618{
2619 return exec_timeout (vty, NULL, NULL);
2620}
2621
2622/* Set vty access class. */
2623DEFUN (vty_access_class,
2624 vty_access_class_cmd,
2625 "access-class WORD",
2626 "Filter connections based on an IP access list\n"
2627 "IP access list\n")
2628{
2629 if (vty_accesslist_name)
2630 XFREE(MTYPE_VTY, vty_accesslist_name);
2631
2632 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2633
2634 return CMD_SUCCESS;
2635}
2636
2637/* Clear vty access class. */
2638DEFUN (no_vty_access_class,
2639 no_vty_access_class_cmd,
2640 "no access-class [WORD]",
2641 NO_STR
2642 "Filter connections based on an IP access list\n"
2643 "IP access list\n")
2644{
2645 if (! vty_accesslist_name || (argc && strcmp(vty_accesslist_name, argv[0])))
2646 {
2647 vty_out (vty, "Access-class is not currently applied to vty%s",
2648 VTY_NEWLINE);
2649 return CMD_WARNING;
2650 }
2651
2652 XFREE(MTYPE_VTY, vty_accesslist_name);
2653
2654 vty_accesslist_name = NULL;
2655
2656 return CMD_SUCCESS;
2657}
2658
2659#ifdef HAVE_IPV6
2660/* Set vty access class. */
2661DEFUN (vty_ipv6_access_class,
2662 vty_ipv6_access_class_cmd,
2663 "ipv6 access-class WORD",
2664 IPV6_STR
2665 "Filter connections based on an IP access list\n"
2666 "IPv6 access list\n")
2667{
2668 if (vty_ipv6_accesslist_name)
2669 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2670
2671 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[0]);
2672
2673 return CMD_SUCCESS;
2674}
2675
2676/* Clear vty access class. */
2677DEFUN (no_vty_ipv6_access_class,
2678 no_vty_ipv6_access_class_cmd,
2679 "no ipv6 access-class [WORD]",
2680 NO_STR
2681 IPV6_STR
2682 "Filter connections based on an IP access list\n"
2683 "IPv6 access list\n")
2684{
2685 if (! vty_ipv6_accesslist_name ||
2686 (argc && strcmp(vty_ipv6_accesslist_name, argv[0])))
2687 {
2688 vty_out (vty, "IPv6 access-class is not currently applied to vty%s",
2689 VTY_NEWLINE);
2690 return CMD_WARNING;
2691 }
2692
2693 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2694
2695 vty_ipv6_accesslist_name = NULL;
2696
2697 return CMD_SUCCESS;
2698}
2699#endif /* HAVE_IPV6 */
2700
2701/* vty login. */
2702DEFUN (vty_login,
2703 vty_login_cmd,
2704 "login",
2705 "Enable password checking\n")
2706{
2707 no_password_check = 0;
2708 return CMD_SUCCESS;
2709}
2710
2711DEFUN (no_vty_login,
2712 no_vty_login_cmd,
2713 "no login",
2714 NO_STR
2715 "Enable password checking\n")
2716{
2717 no_password_check = 1;
2718 return CMD_SUCCESS;
2719}
2720
2721DEFUN (service_advanced_vty,
2722 service_advanced_vty_cmd,
2723 "service advanced-vty",
2724 "Set up miscellaneous service\n"
2725 "Enable advanced mode vty interface\n")
2726{
2727 host.advanced = 1;
2728 return CMD_SUCCESS;
2729}
2730
2731DEFUN (no_service_advanced_vty,
2732 no_service_advanced_vty_cmd,
2733 "no service advanced-vty",
2734 NO_STR
2735 "Set up miscellaneous service\n"
2736 "Enable advanced mode vty interface\n")
2737{
2738 host.advanced = 0;
2739 return CMD_SUCCESS;
2740}
2741
2742DEFUN (terminal_monitor,
2743 terminal_monitor_cmd,
2744 "terminal monitor",
2745 "Set terminal line parameters\n"
2746 "Copy debug output to the current terminal line\n")
2747{
2748 vty->monitor = 1;
2749 return CMD_SUCCESS;
2750}
2751
2752DEFUN (terminal_no_monitor,
2753 terminal_no_monitor_cmd,
2754 "terminal no monitor",
2755 "Set terminal line parameters\n"
2756 NO_STR
2757 "Copy debug output to the current terminal line\n")
2758{
2759 vty->monitor = 0;
2760 return CMD_SUCCESS;
2761}
2762
paul789f78a2006-01-17 17:42:03 +00002763ALIAS (terminal_no_monitor,
2764 no_terminal_monitor_cmd,
2765 "no terminal monitor",
2766 NO_STR
2767 "Set terminal line parameters\n"
2768 "Copy debug output to the current terminal line\n")
2769
paul718e3742002-12-13 20:15:29 +00002770DEFUN (show_history,
2771 show_history_cmd,
2772 "show history",
2773 SHOW_STR
2774 "Display the session command history\n")
2775{
2776 int index;
2777
2778 for (index = vty->hindex + 1; index != vty->hindex;)
2779 {
2780 if (index == VTY_MAXHIST)
2781 {
2782 index = 0;
2783 continue;
2784 }
2785
2786 if (vty->hist[index] != NULL)
2787 vty_out (vty, " %s%s", vty->hist[index], VTY_NEWLINE);
2788
2789 index++;
2790 }
2791
2792 return CMD_SUCCESS;
2793}
2794
2795/* Display current configuration. */
ajs9fc7ebf2005-02-23 15:12:34 +00002796static int
paul718e3742002-12-13 20:15:29 +00002797vty_config_write (struct vty *vty)
2798{
2799 vty_out (vty, "line vty%s", VTY_NEWLINE);
2800
2801 if (vty_accesslist_name)
2802 vty_out (vty, " access-class %s%s",
2803 vty_accesslist_name, VTY_NEWLINE);
2804
2805 if (vty_ipv6_accesslist_name)
2806 vty_out (vty, " ipv6 access-class %s%s",
2807 vty_ipv6_accesslist_name, VTY_NEWLINE);
2808
2809 /* exec-timeout */
2810 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
2811 vty_out (vty, " exec-timeout %ld %ld%s",
2812 vty_timeout_val / 60,
2813 vty_timeout_val % 60, VTY_NEWLINE);
2814
2815 /* login */
2816 if (no_password_check)
2817 vty_out (vty, " no login%s", VTY_NEWLINE);
2818
2819 vty_out (vty, "!%s", VTY_NEWLINE);
2820
2821 return CMD_SUCCESS;
2822}
2823
2824struct cmd_node vty_node =
2825{
2826 VTY_NODE,
2827 "%s(config-line)# ",
hassoe7168df2004-10-03 20:11:32 +00002828 1,
paul718e3742002-12-13 20:15:29 +00002829};
2830
2831/* Reset all VTY status. */
2832void
2833vty_reset ()
2834{
hasso8c328f12004-10-05 21:01:23 +00002835 unsigned int i;
paul718e3742002-12-13 20:15:29 +00002836 struct vty *vty;
2837 struct thread *vty_serv_thread;
2838
paul55468c82005-03-14 20:19:01 +00002839 for (i = 0; i < vector_active (vtyvec); i++)
paul718e3742002-12-13 20:15:29 +00002840 if ((vty = vector_slot (vtyvec, i)) != NULL)
2841 {
2842 buffer_reset (vty->obuf);
2843 vty->status = VTY_CLOSE;
2844 vty_close (vty);
2845 }
2846
paul55468c82005-03-14 20:19:01 +00002847 for (i = 0; i < vector_active (Vvty_serv_thread); i++)
paul718e3742002-12-13 20:15:29 +00002848 if ((vty_serv_thread = vector_slot (Vvty_serv_thread, i)) != NULL)
2849 {
2850 thread_cancel (vty_serv_thread);
2851 vector_slot (Vvty_serv_thread, i) = NULL;
2852 close (i);
2853 }
2854
2855 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
2856
2857 if (vty_accesslist_name)
2858 {
2859 XFREE(MTYPE_VTY, vty_accesslist_name);
2860 vty_accesslist_name = NULL;
2861 }
2862
2863 if (vty_ipv6_accesslist_name)
2864 {
2865 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
2866 vty_ipv6_accesslist_name = NULL;
2867 }
2868}
2869
ajs9fc7ebf2005-02-23 15:12:34 +00002870static void
2871vty_save_cwd (void)
paul718e3742002-12-13 20:15:29 +00002872{
paul79ad2792003-10-15 22:09:28 +00002873 char cwd[MAXPATHLEN];
paulccc92352003-10-22 02:49:38 +00002874 char *c;
paul718e3742002-12-13 20:15:29 +00002875
paulccc92352003-10-22 02:49:38 +00002876 c = getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002877
paulccc92352003-10-22 02:49:38 +00002878 if (!c)
paul79ad2792003-10-15 22:09:28 +00002879 {
2880 chdir (SYSCONFDIR);
paulccc92352003-10-22 02:49:38 +00002881 getcwd (cwd, MAXPATHLEN);
paul79ad2792003-10-15 22:09:28 +00002882 }
paul718e3742002-12-13 20:15:29 +00002883
2884 vty_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1);
2885 strcpy (vty_cwd, cwd);
2886}
2887
2888char *
2889vty_get_cwd ()
2890{
2891 return vty_cwd;
2892}
2893
2894int
2895vty_shell (struct vty *vty)
2896{
2897 return vty->type == VTY_SHELL ? 1 : 0;
2898}
2899
2900int
2901vty_shell_serv (struct vty *vty)
2902{
2903 return vty->type == VTY_SHELL_SERV ? 1 : 0;
2904}
2905
2906void
2907vty_init_vtysh ()
2908{
2909 vtyvec = vector_init (VECTOR_MIN_SIZE);
2910}
2911
2912/* Install vty's own commands like `who' command. */
2913void
paulb21b19c2003-06-15 01:28:29 +00002914vty_init (struct thread_master *master_thread)
paul718e3742002-12-13 20:15:29 +00002915{
2916 /* For further configuration read, preserve current directory. */
2917 vty_save_cwd ();
2918
2919 vtyvec = vector_init (VECTOR_MIN_SIZE);
2920
paulb21b19c2003-06-15 01:28:29 +00002921 master = master_thread;
2922
paul718e3742002-12-13 20:15:29 +00002923 /* Initilize server thread vector. */
2924 Vvty_serv_thread = vector_init (VECTOR_MIN_SIZE);
2925
2926 /* Install bgp top node. */
2927 install_node (&vty_node, vty_config_write);
2928
2929 install_element (VIEW_NODE, &config_who_cmd);
2930 install_element (VIEW_NODE, &show_history_cmd);
2931 install_element (ENABLE_NODE, &config_who_cmd);
2932 install_element (CONFIG_NODE, &line_vty_cmd);
2933 install_element (CONFIG_NODE, &service_advanced_vty_cmd);
2934 install_element (CONFIG_NODE, &no_service_advanced_vty_cmd);
2935 install_element (CONFIG_NODE, &show_history_cmd);
2936 install_element (ENABLE_NODE, &terminal_monitor_cmd);
2937 install_element (ENABLE_NODE, &terminal_no_monitor_cmd);
paul789f78a2006-01-17 17:42:03 +00002938 install_element (ENABLE_NODE, &no_terminal_monitor_cmd);
paul718e3742002-12-13 20:15:29 +00002939 install_element (ENABLE_NODE, &show_history_cmd);
2940
2941 install_default (VTY_NODE);
2942 install_element (VTY_NODE, &exec_timeout_min_cmd);
2943 install_element (VTY_NODE, &exec_timeout_sec_cmd);
2944 install_element (VTY_NODE, &no_exec_timeout_cmd);
2945 install_element (VTY_NODE, &vty_access_class_cmd);
2946 install_element (VTY_NODE, &no_vty_access_class_cmd);
2947 install_element (VTY_NODE, &vty_login_cmd);
2948 install_element (VTY_NODE, &no_vty_login_cmd);
2949#ifdef HAVE_IPV6
2950 install_element (VTY_NODE, &vty_ipv6_access_class_cmd);
2951 install_element (VTY_NODE, &no_vty_ipv6_access_class_cmd);
2952#endif /* HAVE_IPV6 */
2953}