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