blob: 08068560bc87915111ce2eaba87effef2301c606 [file] [log] [blame]
Harald Welte955049f2009-03-10 12:16:51 +00001
2#include <stdio.h>
3#include <stdarg.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <string.h>
7#include <errno.h>
8#include <ctype.h>
9#include <termios.h>
10
11#include <sys/utsname.h>
12#include <sys/param.h>
13
14#include <arpa/telnet.h>
15
16#include "cardshell.h"
17#include <vty/vty.h>
18#include <vty/command.h>
19#include <vty/buffer.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010020#include <osmocore/talloc.h>
Harald Welte955049f2009-03-10 12:16:51 +000021
Holger Hans Peter Freyther6d30a352009-08-10 08:10:26 +020022/* our callback, located in telnet_interface.c */
23void vty_event(enum event event, int sock, struct vty *vty);
24
Harald Welte955049f2009-03-10 12:16:51 +000025extern struct host host;
26
27/* Vector which store each vty structure. */
28static vector vtyvec;
29
30vector Vvty_serv_thread;
31
32char *vty_cwd = NULL;
33
34/* Configure lock. */
35static int vty_config;
36
37static int no_password_check = 1;
38
Harald Welte0224e4d2009-08-07 13:25:41 +020039void *tall_vty_ctx;
Harald Welte2477d932009-08-07 00:32:41 +020040
Harald Welte955049f2009-03-10 12:16:51 +000041static void vty_clear_buf(struct vty *vty)
42{
43 memset(vty->buf, 0, vty->max);
44}
45
46/* Allocate new vty struct. */
47struct vty *vty_new()
48{
Harald Welte2477d932009-08-07 00:32:41 +020049 struct vty *new = talloc_zero(tall_vty_ctx, struct vty);
Harald Welte955049f2009-03-10 12:16:51 +000050
51 if (!new)
52 goto out;
53
Holger Hans Peter Freyther1700c932010-04-30 12:26:52 +080054 new->obuf = buffer_new(new, 0); /* Use default buffer size. */
Harald Welte955049f2009-03-10 12:16:51 +000055 if (!new->obuf)
56 goto out_new;
Holger Hans Peter Freyther9e282de2010-04-30 12:18:32 +080057 new->buf = _talloc_zero(new, VTY_BUFSIZ, "vty_new->buf");
Harald Welte955049f2009-03-10 12:16:51 +000058 if (!new->buf)
59 goto out_obuf;
60
61 new->max = VTY_BUFSIZ;
62
63 return new;
64
65out_obuf:
Harald Welte2477d932009-08-07 00:32:41 +020066 buffer_free(new->obuf);
Harald Welte955049f2009-03-10 12:16:51 +000067out_new:
Harald Welte2477d932009-08-07 00:32:41 +020068 talloc_free(new);
Harald Welte955049f2009-03-10 12:16:51 +000069 new = NULL;
70out:
71 return new;
72}
73
74/* Authentication of vty */
75static void vty_auth(struct vty *vty, char *buf)
76{
77 char *passwd = NULL;
78 enum node_type next_node = 0;
79 int fail;
80 char *crypt(const char *, const char *);
81
82 switch (vty->node) {
83 case AUTH_NODE:
Harald Welted6cab812009-05-21 07:31:48 +000084#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +000085 if (host.encrypt)
86 passwd = host.password_encrypt;
87 else
Harald Welted6cab812009-05-21 07:31:48 +000088#endif
Harald Welte955049f2009-03-10 12:16:51 +000089 passwd = host.password;
90 if (host.advanced)
91 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
92 else
93 next_node = VIEW_NODE;
94 break;
95 case AUTH_ENABLE_NODE:
Harald Welted6cab812009-05-21 07:31:48 +000096#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +000097 if (host.encrypt)
98 passwd = host.enable_encrypt;
99 else
Harald Welted6cab812009-05-21 07:31:48 +0000100#endif
Harald Welte955049f2009-03-10 12:16:51 +0000101 passwd = host.enable;
102 next_node = ENABLE_NODE;
103 break;
104 }
105
106 if (passwd) {
Harald Welted6cab812009-05-21 07:31:48 +0000107#ifdef VTY_CRYPT_PW
Harald Welte955049f2009-03-10 12:16:51 +0000108 if (host.encrypt)
109 fail = strcmp(crypt(buf, passwd), passwd);
110 else
Harald Welted6cab812009-05-21 07:31:48 +0000111#endif
Harald Welte955049f2009-03-10 12:16:51 +0000112 fail = strcmp(buf, passwd);
113 } else
114 fail = 1;
115
116 if (!fail) {
117 vty->fail = 0;
118 vty->node = next_node; /* Success ! */
119 } else {
120 vty->fail++;
121 if (vty->fail >= 3) {
122 if (vty->node == AUTH_NODE) {
123 vty_out(vty,
124 "%% Bad passwords, too many failures!%s",
125 VTY_NEWLINE);
126 vty->status = VTY_CLOSE;
127 } else {
128 /* AUTH_ENABLE_NODE */
129 vty->fail = 0;
130 vty_out(vty,
131 "%% Bad enable passwords, too many failures!%s",
132 VTY_NEWLINE);
133 vty->node = VIEW_NODE;
134 }
135 }
136 }
137}
138
139/* Close vty interface. */
140void vty_close(struct vty *vty)
141{
142 int i;
143
Harald Welte2477d932009-08-07 00:32:41 +0200144 if (vty->obuf) {
145 /* Flush buffer. */
146 buffer_flush_all(vty->obuf, vty->fd);
Harald Welte955049f2009-03-10 12:16:51 +0000147
Harald Welte2477d932009-08-07 00:32:41 +0200148 /* Free input buffer. */
149 buffer_free(vty->obuf);
150 vty->obuf = NULL;
151 }
Harald Welte955049f2009-03-10 12:16:51 +0000152
153 /* Free command history. */
154 for (i = 0; i < VTY_MAXHIST; i++)
155 if (vty->hist[i])
Harald Welte2477d932009-08-07 00:32:41 +0200156 talloc_free(vty->hist[i]);
Harald Welte955049f2009-03-10 12:16:51 +0000157
158 /* Unset vector. */
159 vector_unset(vtyvec, vty->fd);
160
161 /* Close socket. */
162 if (vty->fd > 0)
163 close(vty->fd);
164
Harald Welte2477d932009-08-07 00:32:41 +0200165 if (vty->buf) {
166 talloc_free(vty->buf);
167 vty->buf = NULL;
168 }
Harald Welte955049f2009-03-10 12:16:51 +0000169
170 /* Check configure. */
171 vty_config_unlock(vty);
172
Holger Hans Peter Freyther22ca95c2010-04-30 11:33:08 +0800173 /* VTY_CLOSED is handled by the telnet_interface */
Harald Welte2477d932009-08-07 00:32:41 +0200174 vty_event(VTY_CLOSED, vty->fd, vty);
175
176 /* OK free vty. */
177 talloc_free(vty);
Harald Welte955049f2009-03-10 12:16:51 +0000178}
179
180int vty_shell(struct vty *vty)
181{
182 return vty->type == VTY_SHELL ? 1 : 0;
183}
184
185
186/* VTY standard output function. */
187int vty_out(struct vty *vty, const char *format, ...)
188{
189 va_list args;
190 int len = 0;
191 int size = 1024;
192 char buf[1024];
193 char *p = NULL;
194
195 if (vty_shell(vty)) {
196 va_start(args, format);
197 vprintf(format, args);
198 va_end(args);
199 } else {
200 /* Try to write to initial buffer. */
201 va_start(args, format);
202 len = vsnprintf(buf, sizeof buf, format, args);
203 va_end(args);
204
205 /* Initial buffer is not enough. */
206 if (len < 0 || len >= size) {
207 while (1) {
208 if (len > -1)
209 size = len + 1;
210 else
211 size = size * 2;
212
Holger Hans Peter Freyther9e282de2010-04-30 12:18:32 +0800213 p = talloc_realloc_size(vty, p, size);
Harald Welte955049f2009-03-10 12:16:51 +0000214 if (!p)
215 return -1;
216
217 va_start(args, format);
218 len = vsnprintf(p, size, format, args);
219 va_end(args);
220
221 if (len > -1 && len < size)
222 break;
223 }
224 }
225
226 /* When initial buffer is enough to store all output. */
227 if (!p)
228 p = buf;
229
230 /* Pointer p must point out buffer. */
231 buffer_put(vty->obuf, (u_char *) p, len);
232
233 /* If p is not different with buf, it is allocated buffer. */
234 if (p != buf)
Harald Welte2477d932009-08-07 00:32:41 +0200235 talloc_free(p);
Harald Welte955049f2009-03-10 12:16:51 +0000236 }
237
Harald Welte7cb7a732009-12-17 21:32:19 +0100238 vty_event(VTY_WRITE, vty->fd, vty);
239
Harald Welte955049f2009-03-10 12:16:51 +0000240 return len;
241}
242
243int vty_out_newline(struct vty *vty)
244{
245 char *p = vty_newline(vty);
246 buffer_put(vty->obuf, p, strlen(p));
Harald Weltec63e51d2009-03-10 19:46:16 +0000247 return 0;
Harald Welte955049f2009-03-10 12:16:51 +0000248}
249
250int vty_config_lock(struct vty *vty)
251{
252 if (vty_config == 0) {
253 vty->config = 1;
254 vty_config = 1;
255 }
256 return vty->config;
257}
258
259int vty_config_unlock(struct vty *vty)
260{
261 if (vty_config == 1 && vty->config == 1) {
262 vty->config = 0;
263 vty_config = 0;
264 }
265 return vty->config;
266}
267
Harald Welte955049f2009-03-10 12:16:51 +0000268/* Say hello to vty interface. */
269void vty_hello(struct vty *vty)
270{
271 if (host.motdfile) {
272 FILE *f;
273 char buf[4096];
274
275 f = fopen(host.motdfile, "r");
276 if (f) {
277 while (fgets(buf, sizeof(buf), f)) {
278 char *s;
279 /* work backwards to ignore trailling isspace() */
280 for (s = buf + strlen(buf);
281 (s > buf) && isspace(*(s - 1)); s--) ;
282 *s = '\0';
283 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
284 }
285 fclose(f);
286 } else
287 vty_out(vty, "MOTD file not found%s", VTY_NEWLINE);
288 } else if (host.motd)
Holger Hans Peter Freytherc423a122009-08-10 08:10:11 +0200289 vty_out(vty, "%s", host.motd);
Harald Welte955049f2009-03-10 12:16:51 +0000290}
291
292/* Put out prompt and wait input from user. */
293static void vty_prompt(struct vty *vty)
294{
295 struct utsname names;
296 const char *hostname;
297
298 if (vty->type == VTY_TERM) {
299 hostname = host.name;
300 if (!hostname) {
301 uname(&names);
302 hostname = names.nodename;
303 }
304 vty_out(vty, cmd_prompt(vty->node), hostname);
305 }
306}
307
308/* Command execution over the vty interface. */
309static int vty_command(struct vty *vty, char *buf)
310{
311 int ret;
312 vector vline;
313
314 /* Split readline string up into the vector */
315 vline = cmd_make_strvec(buf);
316
317 if (vline == NULL)
318 return CMD_SUCCESS;
319
320 ret = cmd_execute_command(vline, vty, NULL, 0);
321 if (ret != CMD_SUCCESS)
322 switch (ret) {
323 case CMD_WARNING:
324 if (vty->type == VTY_FILE)
325 vty_out(vty, "Warning...%s", VTY_NEWLINE);
326 break;
327 case CMD_ERR_AMBIGUOUS:
328 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
329 break;
330 case CMD_ERR_NO_MATCH:
331 vty_out(vty, "%% Unknown command.%s", VTY_NEWLINE);
332 break;
333 case CMD_ERR_INCOMPLETE:
334 vty_out(vty, "%% Command incomplete.%s", VTY_NEWLINE);
335 break;
336 }
337 cmd_free_strvec(vline);
338
339 return ret;
340}
341
342static const char telnet_backward_char = 0x08;
343static const char telnet_space_char = ' ';
344
345/* Basic function to write buffer to vty. */
346static void vty_write(struct vty *vty, const char *buf, size_t nbytes)
347{
348 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
349 return;
350
351 /* Should we do buffering here ? And make vty_flush (vty) ? */
352 buffer_put(vty->obuf, buf, nbytes);
353}
354
355/* Ensure length of input buffer. Is buffer is short, double it. */
356static void vty_ensure(struct vty *vty, int length)
357{
358 if (vty->max <= length) {
359 vty->max *= 2;
Holger Hans Peter Freyther9e282de2010-04-30 12:18:32 +0800360 vty->buf = talloc_realloc_size(vty, vty->buf, vty->max);
Harald Welte955049f2009-03-10 12:16:51 +0000361 // FIXME: check return
362 }
363}
364
365/* Basic function to insert character into vty. */
366static void vty_self_insert(struct vty *vty, char c)
367{
368 int i;
369 int length;
370
371 vty_ensure(vty, vty->length + 1);
372 length = vty->length - vty->cp;
373 memmove(&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
374 vty->buf[vty->cp] = c;
375
376 vty_write(vty, &vty->buf[vty->cp], length + 1);
377 for (i = 0; i < length; i++)
378 vty_write(vty, &telnet_backward_char, 1);
379
380 vty->cp++;
381 vty->length++;
382}
383
384/* Self insert character 'c' in overwrite mode. */
385static void vty_self_insert_overwrite(struct vty *vty, char c)
386{
387 vty_ensure(vty, vty->length + 1);
388 vty->buf[vty->cp++] = c;
389
390 if (vty->cp > vty->length)
391 vty->length++;
392
393 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
394 return;
395
396 vty_write(vty, &c, 1);
397}
398
399/* Insert a word into vty interface with overwrite mode. */
400static void vty_insert_word_overwrite(struct vty *vty, char *str)
401{
402 int len = strlen(str);
403 vty_write(vty, str, len);
404 strcpy(&vty->buf[vty->cp], str);
405 vty->cp += len;
406 vty->length = vty->cp;
407}
408
409/* Forward character. */
410static void vty_forward_char(struct vty *vty)
411{
412 if (vty->cp < vty->length) {
413 vty_write(vty, &vty->buf[vty->cp], 1);
414 vty->cp++;
415 }
416}
417
418/* Backward character. */
419static void vty_backward_char(struct vty *vty)
420{
421 if (vty->cp > 0) {
422 vty->cp--;
423 vty_write(vty, &telnet_backward_char, 1);
424 }
425}
426
427/* Move to the beginning of the line. */
428static void vty_beginning_of_line(struct vty *vty)
429{
430 while (vty->cp)
431 vty_backward_char(vty);
432}
433
434/* Move to the end of the line. */
435static void vty_end_of_line(struct vty *vty)
436{
437 while (vty->cp < vty->length)
438 vty_forward_char(vty);
439}
440
441/* Add current command line to the history buffer. */
442static void vty_hist_add(struct vty *vty)
443{
444 int index;
445
446 if (vty->length == 0)
447 return;
448
449 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
450
451 /* Ignore the same string as previous one. */
452 if (vty->hist[index])
453 if (strcmp(vty->buf, vty->hist[index]) == 0) {
454 vty->hp = vty->hindex;
455 return;
456 }
457
458 /* Insert history entry. */
459 if (vty->hist[vty->hindex])
Harald Welte2477d932009-08-07 00:32:41 +0200460 talloc_free(vty->hist[vty->hindex]);
Holger Hans Peter Freyther9e282de2010-04-30 12:18:32 +0800461 vty->hist[vty->hindex] = talloc_strdup(vty, vty->buf);
Harald Welte955049f2009-03-10 12:16:51 +0000462
463 /* History index rotation. */
464 vty->hindex++;
465 if (vty->hindex == VTY_MAXHIST)
466 vty->hindex = 0;
467
468 vty->hp = vty->hindex;
469}
470
471/* Get telnet window size. */
472static int
473vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
474{
475#ifdef TELNET_OPTION_DEBUG
476 int i;
477
478 for (i = 0; i < nbytes; i++)
479 {
480 switch (buf[i])
481 {
482 case IAC:
483 vty_out (vty, "IAC ");
484 break;
485 case WILL:
486 vty_out (vty, "WILL ");
487 break;
488 case WONT:
489 vty_out (vty, "WONT ");
490 break;
491 case DO:
492 vty_out (vty, "DO ");
493 break;
494 case DONT:
495 vty_out (vty, "DONT ");
496 break;
497 case SB:
498 vty_out (vty, "SB ");
499 break;
500 case SE:
501 vty_out (vty, "SE ");
502 break;
503 case TELOPT_ECHO:
504 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
505 break;
506 case TELOPT_SGA:
507 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
508 break;
509 case TELOPT_NAWS:
510 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
511 break;
512 default:
513 vty_out (vty, "%x ", buf[i]);
514 break;
515 }
516 }
517 vty_out (vty, "%s", VTY_NEWLINE);
518
519#endif /* TELNET_OPTION_DEBUG */
520
521 switch (buf[0])
522 {
523 case SB:
524 vty->sb_len = 0;
525 vty->iac_sb_in_progress = 1;
526 return 0;
527 break;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200528 case SE:
Harald Welte955049f2009-03-10 12:16:51 +0000529 {
530 if (!vty->iac_sb_in_progress)
531 return 0;
532
533 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
534 {
535 vty->iac_sb_in_progress = 0;
536 return 0;
537 }
538 switch (vty->sb_buf[0])
539 {
540 case TELOPT_NAWS:
541 if (vty->sb_len != TELNET_NAWS_SB_LEN)
542 vty_out(vty,"RFC 1073 violation detected: telnet NAWS option "
543 "should send %d characters, but we received %lu",
544 TELNET_NAWS_SB_LEN, (u_long)vty->sb_len);
545 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
546 vty_out(vty, "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
547 "too small to handle the telnet NAWS option",
548 (u_long)sizeof(vty->sb_buf), TELNET_NAWS_SB_LEN);
549 else
550 {
551 vty->width = ((vty->sb_buf[1] << 8)|vty->sb_buf[2]);
552 vty->height = ((vty->sb_buf[3] << 8)|vty->sb_buf[4]);
553#ifdef TELNET_OPTION_DEBUG
554 vty_out(vty, "TELNET NAWS window size negotiation completed: "
555 "width %d, height %d%s",
556 vty->width, vty->height, VTY_NEWLINE);
557#endif
558 }
559 break;
560 }
561 vty->iac_sb_in_progress = 0;
562 return 0;
563 break;
564 }
565 default:
566 break;
567 }
568 return 1;
569}
570
571/* Execute current command line. */
572static int vty_execute(struct vty *vty)
573{
574 int ret;
575
576 ret = CMD_SUCCESS;
577
578 switch (vty->node) {
579 case AUTH_NODE:
580 case AUTH_ENABLE_NODE:
581 vty_auth(vty, vty->buf);
582 break;
583 default:
584 ret = vty_command(vty, vty->buf);
585 if (vty->type == VTY_TERM)
586 vty_hist_add(vty);
587 break;
588 }
589
590 /* Clear command line buffer. */
591 vty->cp = vty->length = 0;
592 vty_clear_buf(vty);
593
594 if (vty->status != VTY_CLOSE)
595 vty_prompt(vty);
596
597 return ret;
598}
599
600/* Send WILL TELOPT_ECHO to remote server. */
601static void
602vty_will_echo (struct vty *vty)
603{
604 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
605 vty_out (vty, "%s", cmd);
606}
607
608/* Make suppress Go-Ahead telnet option. */
609static void
610vty_will_suppress_go_ahead (struct vty *vty)
611{
612 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
613 vty_out (vty, "%s", cmd);
614}
615
616/* Make don't use linemode over telnet. */
617static void
618vty_dont_linemode (struct vty *vty)
619{
620 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
621 vty_out (vty, "%s", cmd);
622}
623
624/* Use window size. */
625static void
626vty_do_window_size (struct vty *vty)
627{
628 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
629 vty_out (vty, "%s", cmd);
630}
631
632static void vty_kill_line_from_beginning(struct vty *);
633static void vty_redraw_line(struct vty *);
634
635/* Print command line history. This function is called from
636 vty_next_line and vty_previous_line. */
637static void vty_history_print(struct vty *vty)
638{
639 int length;
640
641 vty_kill_line_from_beginning(vty);
642
643 /* Get previous line from history buffer */
644 length = strlen(vty->hist[vty->hp]);
645 memcpy(vty->buf, vty->hist[vty->hp], length);
646 vty->cp = vty->length = length;
647
648 /* Redraw current line */
649 vty_redraw_line(vty);
650}
651
652/* Show next command line history. */
653static void vty_next_line(struct vty *vty)
654{
655 int try_index;
656
657 if (vty->hp == vty->hindex)
658 return;
659
660 /* Try is there history exist or not. */
661 try_index = vty->hp;
662 if (try_index == (VTY_MAXHIST - 1))
663 try_index = 0;
664 else
665 try_index++;
666
667 /* If there is not history return. */
668 if (vty->hist[try_index] == NULL)
669 return;
670 else
671 vty->hp = try_index;
672
673 vty_history_print(vty);
674}
675
676/* Show previous command line history. */
677static void vty_previous_line(struct vty *vty)
678{
679 int try_index;
680
681 try_index = vty->hp;
682 if (try_index == 0)
683 try_index = VTY_MAXHIST - 1;
684 else
685 try_index--;
686
687 if (vty->hist[try_index] == NULL)
688 return;
689 else
690 vty->hp = try_index;
691
692 vty_history_print(vty);
693}
694
695/* This function redraw all of the command line character. */
696static void vty_redraw_line(struct vty *vty)
697{
698 vty_write(vty, vty->buf, vty->length);
699 vty->cp = vty->length;
700}
701
702/* Forward word. */
703static void vty_forward_word(struct vty *vty)
704{
705 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
706 vty_forward_char(vty);
707
708 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
709 vty_forward_char(vty);
710}
711
712/* Backward word without skipping training space. */
713static void vty_backward_pure_word(struct vty *vty)
714{
715 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
716 vty_backward_char(vty);
717}
718
719/* Backward word. */
720static void vty_backward_word(struct vty *vty)
721{
722 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
723 vty_backward_char(vty);
724
725 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
726 vty_backward_char(vty);
727}
728
729/* When '^D' is typed at the beginning of the line we move to the down
730 level. */
731static void vty_down_level(struct vty *vty)
732{
733 vty_out(vty, "%s", VTY_NEWLINE);
734 (*config_exit_cmd.func) (NULL, vty, 0, NULL);
735 vty_prompt(vty);
736 vty->cp = 0;
737}
738
739/* When '^Z' is received from vty, move down to the enable mode. */
740static void vty_end_config(struct vty *vty)
741{
742 vty_out(vty, "%s", VTY_NEWLINE);
743
744 switch (vty->node) {
745 case VIEW_NODE:
746 case ENABLE_NODE:
747 /* Nothing to do. */
748 break;
749 case CONFIG_NODE:
Harald Welte955049f2009-03-10 12:16:51 +0000750 case VTY_NODE:
751 vty_config_unlock(vty);
752 vty->node = ENABLE_NODE;
753 break;
754 default:
755 /* Unknown node, we have to ignore it. */
756 break;
757 }
758
759 vty_prompt(vty);
760 vty->cp = 0;
761}
762
763/* Delete a charcter at the current point. */
764static void vty_delete_char(struct vty *vty)
765{
766 int i;
767 int size;
768
769 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
770 return;
771
772 if (vty->length == 0) {
773 vty_down_level(vty);
774 return;
775 }
776
777 if (vty->cp == vty->length)
778 return; /* completion need here? */
779
780 size = vty->length - vty->cp;
781
782 vty->length--;
783 memmove(&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
784 vty->buf[vty->length] = '\0';
785
786 vty_write(vty, &vty->buf[vty->cp], size - 1);
787 vty_write(vty, &telnet_space_char, 1);
788
789 for (i = 0; i < size; i++)
790 vty_write(vty, &telnet_backward_char, 1);
791}
792
793/* Delete a character before the point. */
794static void vty_delete_backward_char(struct vty *vty)
795{
796 if (vty->cp == 0)
797 return;
798
799 vty_backward_char(vty);
800 vty_delete_char(vty);
801}
802
803/* Kill rest of line from current point. */
804static void vty_kill_line(struct vty *vty)
805{
806 int i;
807 int size;
808
809 size = vty->length - vty->cp;
810
811 if (size == 0)
812 return;
813
814 for (i = 0; i < size; i++)
815 vty_write(vty, &telnet_space_char, 1);
816 for (i = 0; i < size; i++)
817 vty_write(vty, &telnet_backward_char, 1);
818
819 memset(&vty->buf[vty->cp], 0, size);
820 vty->length = vty->cp;
821}
822
823/* Kill line from the beginning. */
824static void vty_kill_line_from_beginning(struct vty *vty)
825{
826 vty_beginning_of_line(vty);
827 vty_kill_line(vty);
828}
829
830/* Delete a word before the point. */
831static void vty_forward_kill_word(struct vty *vty)
832{
833 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
834 vty_delete_char(vty);
835 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
836 vty_delete_char(vty);
837}
838
839/* Delete a word before the point. */
840static void vty_backward_kill_word(struct vty *vty)
841{
842 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
843 vty_delete_backward_char(vty);
844 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
845 vty_delete_backward_char(vty);
846}
847
848/* Transpose chars before or at the point. */
849static void vty_transpose_chars(struct vty *vty)
850{
851 char c1, c2;
852
853 /* If length is short or point is near by the beginning of line then
854 return. */
855 if (vty->length < 2 || vty->cp < 1)
856 return;
857
858 /* In case of point is located at the end of the line. */
859 if (vty->cp == vty->length) {
860 c1 = vty->buf[vty->cp - 1];
861 c2 = vty->buf[vty->cp - 2];
862
863 vty_backward_char(vty);
864 vty_backward_char(vty);
865 vty_self_insert_overwrite(vty, c1);
866 vty_self_insert_overwrite(vty, c2);
867 } else {
868 c1 = vty->buf[vty->cp];
869 c2 = vty->buf[vty->cp - 1];
870
871 vty_backward_char(vty);
872 vty_self_insert_overwrite(vty, c1);
873 vty_self_insert_overwrite(vty, c2);
874 }
875}
876
877/* Do completion at vty interface. */
878static void vty_complete_command(struct vty *vty)
879{
880 int i;
881 int ret;
882 char **matched = NULL;
883 vector vline;
884
885 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
886 return;
887
888 vline = cmd_make_strvec(vty->buf);
889 if (vline == NULL)
890 return;
891
892 /* In case of 'help \t'. */
893 if (isspace((int)vty->buf[vty->length - 1]))
894 vector_set(vline, '\0');
895
896 matched = cmd_complete_command(vline, vty, &ret);
897
898 cmd_free_strvec(vline);
899
900 vty_out(vty, "%s", VTY_NEWLINE);
901 switch (ret) {
902 case CMD_ERR_AMBIGUOUS:
903 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
904 vty_prompt(vty);
905 vty_redraw_line(vty);
906 break;
907 case CMD_ERR_NO_MATCH:
908 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
909 vty_prompt(vty);
910 vty_redraw_line(vty);
911 break;
912 case CMD_COMPLETE_FULL_MATCH:
913 vty_prompt(vty);
914 vty_redraw_line(vty);
915 vty_backward_pure_word(vty);
916 vty_insert_word_overwrite(vty, matched[0]);
917 vty_self_insert(vty, ' ');
Holger Hans Peter Freytherf1af3062010-04-30 13:18:05 +0800918 talloc_free(matched[0]);
Harald Welte955049f2009-03-10 12:16:51 +0000919 break;
920 case CMD_COMPLETE_MATCH:
921 vty_prompt(vty);
922 vty_redraw_line(vty);
923 vty_backward_pure_word(vty);
924 vty_insert_word_overwrite(vty, matched[0]);
Harald Welte2477d932009-08-07 00:32:41 +0200925 talloc_free(matched[0]);
Harald Welte955049f2009-03-10 12:16:51 +0000926 break;
927 case CMD_COMPLETE_LIST_MATCH:
928 for (i = 0; matched[i] != NULL; i++) {
929 if (i != 0 && ((i % 6) == 0))
930 vty_out(vty, "%s", VTY_NEWLINE);
931 vty_out(vty, "%-10s ", matched[i]);
Harald Welte2477d932009-08-07 00:32:41 +0200932 talloc_free(matched[i]);
Harald Welte955049f2009-03-10 12:16:51 +0000933 }
934 vty_out(vty, "%s", VTY_NEWLINE);
935
936 vty_prompt(vty);
937 vty_redraw_line(vty);
938 break;
939 case CMD_ERR_NOTHING_TODO:
940 vty_prompt(vty);
941 vty_redraw_line(vty);
942 break;
943 default:
944 break;
945 }
946 if (matched)
947 vector_only_index_free(matched);
948}
949
950static void
951vty_describe_fold(struct vty *vty, int cmd_width,
952 unsigned int desc_width, struct desc *desc)
953{
954 char *buf;
955 const char *cmd, *p;
956 int pos;
957
958 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
959
960 if (desc_width <= 0) {
961 vty_out(vty, " %-*s %s%s", cmd_width, cmd, desc->str,
962 VTY_NEWLINE);
963 return;
964 }
965
Holger Hans Peter Freyther9e282de2010-04-30 12:18:32 +0800966 buf = _talloc_zero(vty, strlen(desc->str) + 1, "describe_fold");
Harald Welte955049f2009-03-10 12:16:51 +0000967 if (!buf)
968 return;
969
970 for (p = desc->str; strlen(p) > desc_width; p += pos + 1) {
971 for (pos = desc_width; pos > 0; pos--)
972 if (*(p + pos) == ' ')
973 break;
974
975 if (pos == 0)
976 break;
977
978 strncpy(buf, p, pos);
979 buf[pos] = '\0';
980 vty_out(vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
981
982 cmd = "";
983 }
984
985 vty_out(vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
986
Harald Welte2477d932009-08-07 00:32:41 +0200987 talloc_free(buf);
Harald Welte955049f2009-03-10 12:16:51 +0000988}
989
990/* Describe matched command function. */
991static void vty_describe_command(struct vty *vty)
992{
993 int ret;
994 vector vline;
995 vector describe;
996 unsigned int i, width, desc_width;
997 struct desc *desc, *desc_cr = NULL;
998
999 vline = cmd_make_strvec(vty->buf);
1000
1001 /* In case of '> ?'. */
1002 if (vline == NULL) {
1003 vline = vector_init(1);
1004 vector_set(vline, '\0');
1005 } else if (isspace((int)vty->buf[vty->length - 1]))
1006 vector_set(vline, '\0');
1007
1008 describe = cmd_describe_command(vline, vty, &ret);
1009
1010 vty_out(vty, "%s", VTY_NEWLINE);
1011
1012 /* Ambiguous error. */
1013 switch (ret) {
1014 case CMD_ERR_AMBIGUOUS:
1015 cmd_free_strvec(vline);
1016 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
1017 vty_prompt(vty);
1018 vty_redraw_line(vty);
1019 return;
1020 break;
1021 case CMD_ERR_NO_MATCH:
1022 cmd_free_strvec(vline);
1023 vty_out(vty, "%% There is no matched command.%s", VTY_NEWLINE);
1024 vty_prompt(vty);
1025 vty_redraw_line(vty);
1026 return;
1027 break;
1028 }
1029
1030 /* Get width of command string. */
1031 width = 0;
1032 for (i = 0; i < vector_active(describe); i++)
1033 if ((desc = vector_slot(describe, i)) != NULL) {
1034 unsigned int len;
1035
1036 if (desc->cmd[0] == '\0')
1037 continue;
1038
1039 len = strlen(desc->cmd);
1040 if (desc->cmd[0] == '.')
1041 len--;
1042
1043 if (width < len)
1044 width = len;
1045 }
1046
1047 /* Get width of description string. */
1048 desc_width = vty->width - (width + 6);
1049
1050 /* Print out description. */
1051 for (i = 0; i < vector_active(describe); i++)
1052 if ((desc = vector_slot(describe, i)) != NULL) {
1053 if (desc->cmd[0] == '\0')
1054 continue;
1055
1056 if (strcmp(desc->cmd, "<cr>") == 0) {
1057 desc_cr = desc;
1058 continue;
1059 }
1060
1061 if (!desc->str)
1062 vty_out(vty, " %-s%s",
1063 desc->cmd[0] ==
1064 '.' ? desc->cmd + 1 : desc->cmd,
1065 VTY_NEWLINE);
1066 else if (desc_width >= strlen(desc->str))
1067 vty_out(vty, " %-*s %s%s", width,
1068 desc->cmd[0] ==
1069 '.' ? desc->cmd + 1 : desc->cmd,
1070 desc->str, VTY_NEWLINE);
1071 else
1072 vty_describe_fold(vty, width, desc_width, desc);
1073
1074#if 0
1075 vty_out(vty, " %-*s %s%s", width
1076 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1077 desc->str ? desc->str : "", VTY_NEWLINE);
1078#endif /* 0 */
1079 }
1080
1081 if ((desc = desc_cr)) {
1082 if (!desc->str)
1083 vty_out(vty, " %-s%s",
1084 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1085 VTY_NEWLINE);
1086 else if (desc_width >= strlen(desc->str))
1087 vty_out(vty, " %-*s %s%s", width,
1088 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1089 desc->str, VTY_NEWLINE);
1090 else
1091 vty_describe_fold(vty, width, desc_width, desc);
1092 }
1093
1094 cmd_free_strvec(vline);
1095 vector_free(describe);
1096
1097 vty_prompt(vty);
1098 vty_redraw_line(vty);
1099}
1100
1101/* ^C stop current input and do not add command line to the history. */
1102static void vty_stop_input(struct vty *vty)
1103{
1104 vty->cp = vty->length = 0;
1105 vty_clear_buf(vty);
1106 vty_out(vty, "%s", VTY_NEWLINE);
1107
1108 switch (vty->node) {
1109 case VIEW_NODE:
1110 case ENABLE_NODE:
1111 /* Nothing to do. */
1112 break;
1113 case CONFIG_NODE:
Harald Welte955049f2009-03-10 12:16:51 +00001114 case VTY_NODE:
1115 vty_config_unlock(vty);
1116 vty->node = ENABLE_NODE;
1117 break;
1118 default:
1119 /* Unknown node, we have to ignore it. */
1120 break;
1121 }
1122 vty_prompt(vty);
1123
1124 /* Set history pointer to the latest one. */
1125 vty->hp = vty->hindex;
1126}
1127
1128#define CONTROL(X) ((X) - '@')
1129#define VTY_NORMAL 0
1130#define VTY_PRE_ESCAPE 1
1131#define VTY_ESCAPE 2
1132
1133/* Escape character command map. */
1134static void vty_escape_map(unsigned char c, struct vty *vty)
1135{
1136 switch (c) {
1137 case ('A'):
1138 vty_previous_line(vty);
1139 break;
1140 case ('B'):
1141 vty_next_line(vty);
1142 break;
1143 case ('C'):
1144 vty_forward_char(vty);
1145 break;
1146 case ('D'):
1147 vty_backward_char(vty);
1148 break;
1149 default:
1150 break;
1151 }
1152
1153 /* Go back to normal mode. */
1154 vty->escape = VTY_NORMAL;
1155}
1156
1157/* Quit print out to the buffer. */
1158static void vty_buffer_reset(struct vty *vty)
1159{
1160 buffer_reset(vty->obuf);
1161 vty_prompt(vty);
1162 vty_redraw_line(vty);
1163}
1164
1165/* Read data via vty socket. */
1166int vty_read(struct vty *vty)
1167{
1168 int i;
1169 int nbytes;
1170 unsigned char buf[VTY_READ_BUFSIZ];
1171
1172 int vty_sock = vty->fd;
1173
1174 /* Read raw data from socket */
1175 if ((nbytes = read(vty->fd, buf, VTY_READ_BUFSIZ)) <= 0) {
1176 if (nbytes < 0) {
1177 if (ERRNO_IO_RETRY(errno)) {
1178 vty_event(VTY_READ, vty_sock, vty);
1179 return 0;
1180 }
1181 }
1182 buffer_reset(vty->obuf);
1183 vty->status = VTY_CLOSE;
1184 }
1185
1186 for (i = 0; i < nbytes; i++) {
1187 if (buf[i] == IAC) {
1188 if (!vty->iac) {
1189 vty->iac = 1;
1190 continue;
1191 } else {
1192 vty->iac = 0;
1193 }
1194 }
1195
1196 if (vty->iac_sb_in_progress && !vty->iac) {
1197 if (vty->sb_len < sizeof(vty->sb_buf))
1198 vty->sb_buf[vty->sb_len] = buf[i];
1199 vty->sb_len++;
1200 continue;
1201 }
1202
1203 if (vty->iac) {
1204 /* In case of telnet command */
1205 int ret = 0;
1206 ret = vty_telnet_option(vty, buf + i, nbytes - i);
1207 vty->iac = 0;
1208 i += ret;
1209 continue;
1210 }
1211
1212 if (vty->status == VTY_MORE) {
1213 switch (buf[i]) {
1214 case CONTROL('C'):
1215 case 'q':
1216 case 'Q':
1217 vty_buffer_reset(vty);
1218 break;
1219#if 0 /* More line does not work for "show ip bgp". */
1220 case '\n':
1221 case '\r':
1222 vty->status = VTY_MORELINE;
1223 break;
1224#endif
1225 default:
1226 break;
1227 }
1228 continue;
1229 }
1230
1231 /* Escape character. */
1232 if (vty->escape == VTY_ESCAPE) {
1233 vty_escape_map(buf[i], vty);
1234 continue;
1235 }
1236
1237 /* Pre-escape status. */
1238 if (vty->escape == VTY_PRE_ESCAPE) {
1239 switch (buf[i]) {
1240 case '[':
1241 vty->escape = VTY_ESCAPE;
1242 break;
1243 case 'b':
1244 vty_backward_word(vty);
1245 vty->escape = VTY_NORMAL;
1246 break;
1247 case 'f':
1248 vty_forward_word(vty);
1249 vty->escape = VTY_NORMAL;
1250 break;
1251 case 'd':
1252 vty_forward_kill_word(vty);
1253 vty->escape = VTY_NORMAL;
1254 break;
1255 case CONTROL('H'):
1256 case 0x7f:
1257 vty_backward_kill_word(vty);
1258 vty->escape = VTY_NORMAL;
1259 break;
1260 default:
1261 vty->escape = VTY_NORMAL;
1262 break;
1263 }
1264 continue;
1265 }
1266
1267 switch (buf[i]) {
1268 case CONTROL('A'):
1269 vty_beginning_of_line(vty);
1270 break;
1271 case CONTROL('B'):
1272 vty_backward_char(vty);
1273 break;
1274 case CONTROL('C'):
1275 vty_stop_input(vty);
1276 break;
1277 case CONTROL('D'):
1278 vty_delete_char(vty);
1279 break;
1280 case CONTROL('E'):
1281 vty_end_of_line(vty);
1282 break;
1283 case CONTROL('F'):
1284 vty_forward_char(vty);
1285 break;
1286 case CONTROL('H'):
1287 case 0x7f:
1288 vty_delete_backward_char(vty);
1289 break;
1290 case CONTROL('K'):
1291 vty_kill_line(vty);
1292 break;
1293 case CONTROL('N'):
1294 vty_next_line(vty);
1295 break;
1296 case CONTROL('P'):
1297 vty_previous_line(vty);
1298 break;
1299 case CONTROL('T'):
1300 vty_transpose_chars(vty);
1301 break;
1302 case CONTROL('U'):
1303 vty_kill_line_from_beginning(vty);
1304 break;
1305 case CONTROL('W'):
1306 vty_backward_kill_word(vty);
1307 break;
1308 case CONTROL('Z'):
1309 vty_end_config(vty);
1310 break;
1311 case '\n':
1312 case '\r':
1313 vty_out(vty, "%s", VTY_NEWLINE);
1314 vty_execute(vty);
1315 break;
1316 case '\t':
1317 vty_complete_command(vty);
1318 break;
1319 case '?':
1320 if (vty->node == AUTH_NODE
1321 || vty->node == AUTH_ENABLE_NODE)
1322 vty_self_insert(vty, buf[i]);
1323 else
1324 vty_describe_command(vty);
1325 break;
1326 case '\033':
1327 if (i + 1 < nbytes && buf[i + 1] == '[') {
1328 vty->escape = VTY_ESCAPE;
1329 i++;
1330 } else
1331 vty->escape = VTY_PRE_ESCAPE;
1332 break;
1333 default:
1334 if (buf[i] > 31 && buf[i] < 127)
1335 vty_self_insert(vty, buf[i]);
1336 break;
1337 }
1338 }
1339
1340 /* Check status. */
1341 if (vty->status == VTY_CLOSE)
1342 vty_close(vty);
1343 else {
1344 vty_event(VTY_WRITE, vty_sock, vty);
1345 vty_event(VTY_READ, vty_sock, vty);
1346 }
1347 return 0;
1348}
1349
Harald Weltec7c19822009-08-07 13:28:08 +02001350/* Read up configuration file */
1351static int
1352vty_read_file(FILE *confp)
1353{
1354 int ret;
1355 struct vty *vty;
1356
1357 vty = vty_new();
1358 vty->fd = 0;
1359 vty->type = VTY_FILE;
1360 vty->node = CONFIG_NODE;
1361
1362 ret = config_from_file(vty, confp);
1363
1364 if (ret != CMD_SUCCESS) {
1365 switch (ret) {
1366 case CMD_ERR_AMBIGUOUS:
1367 fprintf(stderr, "Ambiguous command.\n");
1368 break;
1369 case CMD_ERR_NO_MATCH:
Harald Welte42581822009-08-08 16:12:58 +02001370 fprintf(stderr, "There is no such command.\n");
Harald Weltec7c19822009-08-07 13:28:08 +02001371 break;
1372 }
1373 fprintf(stderr, "Error occurred during reading below "
1374 "line:\n%s\n", vty->buf);
1375 vty_close(vty);
1376 return -EINVAL;
1377 }
1378
1379 vty_close(vty);
1380 return 0;
1381}
1382
Harald Welte955049f2009-03-10 12:16:51 +00001383/* Create new vty structure. */
1384struct vty *
Harald Weltec63e51d2009-03-10 19:46:16 +00001385vty_create (int vty_sock, void *priv)
Harald Welte955049f2009-03-10 12:16:51 +00001386{
1387 struct vty *vty;
1388
1389 struct termios t;
1390
1391 tcgetattr(vty_sock, &t);
1392 cfmakeraw(&t);
1393 tcsetattr(vty_sock, TCSANOW, &t);
1394
1395 /* Allocate new vty structure and set up default values. */
1396 vty = vty_new ();
1397 vty->fd = vty_sock;
Harald Weltec63e51d2009-03-10 19:46:16 +00001398 vty->priv = priv;
Harald Welte955049f2009-03-10 12:16:51 +00001399 vty->type = VTY_TERM;
1400 if (no_password_check)
1401 {
1402 if (host.advanced)
1403 vty->node = ENABLE_NODE;
1404 else
1405 vty->node = VIEW_NODE;
1406 }
1407 else
1408 vty->node = AUTH_NODE;
1409 vty->fail = 0;
1410 vty->cp = 0;
1411 vty_clear_buf (vty);
1412 vty->length = 0;
1413 memset (vty->hist, 0, sizeof (vty->hist));
1414 vty->hp = 0;
1415 vty->hindex = 0;
1416 vector_set_index (vtyvec, vty_sock, vty);
1417 vty->status = VTY_NORMAL;
1418 if (host.lines >= 0)
1419 vty->lines = host.lines;
1420 else
1421 vty->lines = -1;
1422
1423 if (! no_password_check)
1424 {
1425 /* Vty is not available if password isn't set. */
1426 if (host.password == NULL && host.password_encrypt == NULL)
1427 {
1428 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1429 vty->status = VTY_CLOSE;
1430 vty_close (vty);
1431 return NULL;
1432 }
1433 }
1434
1435 /* Say hello to the world. */
1436 vty_hello (vty);
1437 if (! no_password_check)
1438 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1439
1440 /* Setting up terminal. */
1441 vty_will_echo (vty);
1442 vty_will_suppress_go_ahead (vty);
1443
1444 vty_dont_linemode (vty);
1445 vty_do_window_size (vty);
1446 /* vty_dont_lflow_ahead (vty); */
1447
1448 vty_prompt (vty);
1449
1450 /* Add read/write thread. */
1451 vty_event (VTY_WRITE, vty_sock, vty);
1452 vty_event (VTY_READ, vty_sock, vty);
1453
1454 return vty;
1455}
1456
1457DEFUN(config_who, config_who_cmd, "who", "Display who is on vty\n")
1458{
1459 unsigned int i;
1460 struct vty *v;
1461
1462 for (i = 0; i < vector_active(vtyvec); i++)
1463 if ((v = vector_slot(vtyvec, i)) != NULL)
1464 vty_out(vty, "%svty[%d] %s",
1465 v->config ? "*" : " ", i, VTY_NEWLINE);
1466 return CMD_SUCCESS;
1467}
1468
1469/* Move to vty configuration mode. */
1470DEFUN(line_vty,
1471 line_vty_cmd,
1472 "line vty", "Configure a terminal line\n" "Virtual terminal\n")
1473{
1474 vty->node = VTY_NODE;
1475 return CMD_SUCCESS;
1476}
1477
1478/* vty login. */
1479DEFUN(vty_login, vty_login_cmd, "login", "Enable password checking\n")
1480{
1481 no_password_check = 0;
1482 return CMD_SUCCESS;
1483}
1484
1485DEFUN(no_vty_login,
1486 no_vty_login_cmd, "no login", NO_STR "Enable password checking\n")
1487{
1488 no_password_check = 1;
1489 return CMD_SUCCESS;
1490}
1491
1492DEFUN(service_advanced_vty,
1493 service_advanced_vty_cmd,
1494 "service advanced-vty",
1495 "Set up miscellaneous service\n" "Enable advanced mode vty interface\n")
1496{
1497 host.advanced = 1;
1498 return CMD_SUCCESS;
1499}
1500
1501DEFUN(no_service_advanced_vty,
1502 no_service_advanced_vty_cmd,
1503 "no service advanced-vty",
1504 NO_STR
1505 "Set up miscellaneous service\n" "Enable advanced mode vty interface\n")
1506{
1507 host.advanced = 0;
1508 return CMD_SUCCESS;
1509}
1510
1511DEFUN(terminal_monitor,
1512 terminal_monitor_cmd,
1513 "terminal monitor",
1514 "Set terminal line parameters\n"
1515 "Copy debug output to the current terminal line\n")
1516{
1517 vty->monitor = 1;
1518 return CMD_SUCCESS;
1519}
1520
1521DEFUN(terminal_no_monitor,
1522 terminal_no_monitor_cmd,
1523 "terminal no monitor",
1524 "Set terminal line parameters\n"
1525 NO_STR "Copy debug output to the current terminal line\n")
1526{
1527 vty->monitor = 0;
1528 return CMD_SUCCESS;
1529}
1530
1531DEFUN(show_history,
1532 show_history_cmd,
1533 "show history", SHOW_STR "Display the session command history\n")
1534{
1535 int index;
1536
1537 for (index = vty->hindex + 1; index != vty->hindex;) {
1538 if (index == VTY_MAXHIST) {
1539 index = 0;
1540 continue;
1541 }
1542
1543 if (vty->hist[index] != NULL)
1544 vty_out(vty, " %s%s", vty->hist[index], VTY_NEWLINE);
1545
1546 index++;
1547 }
1548
1549 return CMD_SUCCESS;
1550}
1551
1552/* Display current configuration. */
1553static int vty_config_write(struct vty *vty)
1554{
1555 vty_out(vty, "line vty%s", VTY_NEWLINE);
1556
1557 /* login */
1558 if (no_password_check)
1559 vty_out(vty, " no login%s", VTY_NEWLINE);
1560
1561 vty_out(vty, "!%s", VTY_NEWLINE);
1562
1563 return CMD_SUCCESS;
1564}
1565
1566struct cmd_node vty_node = {
1567 VTY_NODE,
1568 "%s(config-line)# ",
1569 1,
1570};
1571
1572/* Reset all VTY status. */
1573void vty_reset()
1574{
1575 unsigned int i;
1576 struct vty *vty;
1577 struct thread *vty_serv_thread;
1578
1579 for (i = 0; i < vector_active(vtyvec); i++)
1580 if ((vty = vector_slot(vtyvec, i)) != NULL) {
1581 buffer_reset(vty->obuf);
1582 vty->status = VTY_CLOSE;
1583 vty_close(vty);
1584 }
1585
1586 for (i = 0; i < vector_active(Vvty_serv_thread); i++)
1587 if ((vty_serv_thread =
1588 vector_slot(Vvty_serv_thread, i)) != NULL) {
1589 //thread_cancel (vty_serv_thread);
1590 vector_slot(Vvty_serv_thread, i) = NULL;
1591 close(i);
1592 }
1593}
1594
1595static void vty_save_cwd(void)
1596{
1597 char cwd[MAXPATHLEN];
Holger Hans Peter Freyther6d30a352009-08-10 08:10:26 +02001598 char *c ;
Harald Welte955049f2009-03-10 12:16:51 +00001599
1600 c = getcwd(cwd, MAXPATHLEN);
1601
1602 if (!c) {
Holger Hans Peter Freyther6d30a352009-08-10 08:10:26 +02001603 if (chdir(SYSCONFDIR) != 0)
1604 perror("chdir failed");
1605 if (getcwd(cwd, MAXPATHLEN) == NULL)
1606 perror("getcwd failed");
Harald Welte955049f2009-03-10 12:16:51 +00001607 }
1608
Harald Welte2477d932009-08-07 00:32:41 +02001609 vty_cwd = _talloc_zero(tall_vty_ctx, strlen(cwd) + 1, "save_cwd");
Harald Welte955049f2009-03-10 12:16:51 +00001610 strcpy(vty_cwd, cwd);
1611}
1612
1613char *vty_get_cwd()
1614{
1615 return vty_cwd;
1616}
1617
1618int vty_shell_serv(struct vty *vty)
1619{
1620 return vty->type == VTY_SHELL_SERV ? 1 : 0;
1621}
1622
1623void vty_init_vtysh()
1624{
1625 vtyvec = vector_init(VECTOR_MIN_SIZE);
1626}
1627
Harald Welte0224e4d2009-08-07 13:25:41 +02001628extern void *tall_bsc_ctx;
Harald Welte955049f2009-03-10 12:16:51 +00001629/* Install vty's own commands like `who' command. */
1630void vty_init()
1631{
Harald Welte (local)d19e58b2009-08-15 02:30:58 +02001632 tall_vty_ctx = talloc_named_const(NULL, 0, "vty");
Harald Welte3cefa9a2009-12-24 10:51:56 +01001633 tall_vty_vec_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_vector");
1634 tall_vty_cmd_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_command");
Harald Welte0224e4d2009-08-07 13:25:41 +02001635
Harald Welte955049f2009-03-10 12:16:51 +00001636 /* For further configuration read, preserve current directory. */
1637 vty_save_cwd();
1638
1639 vtyvec = vector_init(VECTOR_MIN_SIZE);
1640
1641 /* Install bgp top node. */
1642 install_node(&vty_node, vty_config_write);
1643
1644 install_element(VIEW_NODE, &config_who_cmd);
1645 install_element(VIEW_NODE, &show_history_cmd);
1646 install_element(ENABLE_NODE, &config_who_cmd);
1647 install_element(CONFIG_NODE, &line_vty_cmd);
1648 install_element(CONFIG_NODE, &service_advanced_vty_cmd);
1649 install_element(CONFIG_NODE, &no_service_advanced_vty_cmd);
1650 install_element(CONFIG_NODE, &show_history_cmd);
1651 install_element(ENABLE_NODE, &terminal_monitor_cmd);
1652 install_element(ENABLE_NODE, &terminal_no_monitor_cmd);
1653 install_element(ENABLE_NODE, &show_history_cmd);
1654
1655 install_default(VTY_NODE);
Harald Welte955049f2009-03-10 12:16:51 +00001656 install_element(VTY_NODE, &vty_login_cmd);
1657 install_element(VTY_NODE, &no_vty_login_cmd);
Harald Welte955049f2009-03-10 12:16:51 +00001658}
Harald Welte2477d932009-08-07 00:32:41 +02001659
Harald Weltec7c19822009-08-07 13:28:08 +02001660int vty_read_config_file(const char *file_name)
Harald Welte2477d932009-08-07 00:32:41 +02001661{
Harald Weltec7c19822009-08-07 13:28:08 +02001662 FILE *cfile;
1663 int rc;
1664
1665 cfile = fopen(file_name, "r");
1666 if (!cfile)
1667 return -ENOENT;
1668
1669 rc = vty_read_file(cfile);
1670 fclose(cfile);
1671
1672 host_config_set(file_name);
1673
1674 return rc;
Harald Welte2477d932009-08-07 00:32:41 +02001675}