11 #include <sys/utsname.h>
12 #include <sys/param.h>
14 #include <arpa/telnet.h>
16 #include <osmocom/vty/vty.h>
17 #include <osmocom/vty/command.h>
18 #include <osmocom/vty/buffer.h>
19 #include <osmocom/core/talloc.h>
21 #define SYSCONFDIR "/usr/local/etc"
23 /* our callback, located in telnet_interface.c */
24 void vty_event(enum event event, int sock, struct vty *vty);
26 extern struct host host;
28 /* Vector which store each vty structure. */
31 vector Vvty_serv_thread;
36 static int vty_config;
38 static int no_password_check = 1;
42 static void vty_clear_buf(struct vty *vty)
44 memset(vty->buf, 0, vty->max);
47 /* Allocate new vty struct. */
50 struct vty *new = talloc_zero(tall_vty_ctx, struct vty);
55 new->obuf = buffer_new(new, 0); /* Use default buffer size. */
58 new->buf = _talloc_zero(new, VTY_BUFSIZ, "vty_new->buf");
62 new->max = VTY_BUFSIZ;
67 buffer_free(new->obuf);
75 /* Authentication of vty */
76 static void vty_auth(struct vty *vty, char *buf)
79 enum node_type next_node = 0;
81 char *crypt(const char *, const char *);
87 passwd = host.password_encrypt;
90 passwd = host.password;
92 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
94 next_node = VIEW_NODE;
96 case AUTH_ENABLE_NODE:
99 passwd = host.enable_encrypt;
102 passwd = host.enable;
103 next_node = ENABLE_NODE;
110 fail = strcmp(crypt(buf, passwd), passwd);
113 fail = strcmp(buf, passwd);
119 vty->node = next_node; /* Success ! */
122 if (vty->fail >= 3) {
123 if (vty->node == AUTH_NODE) {
125 "%% Bad passwords, too many failures!%s",
127 vty->status = VTY_CLOSE;
129 /* AUTH_ENABLE_NODE */
132 "%% Bad enable passwords, too many failures!%s",
134 vty->node = VIEW_NODE;
140 /* Close vty interface. */
141 void vty_close(struct vty *vty)
147 buffer_flush_all(vty->obuf, vty->fd);
149 /* Free input buffer. */
150 buffer_free(vty->obuf);
154 /* Free command history. */
155 for (i = 0; i < VTY_MAXHIST; i++)
157 talloc_free(vty->hist[i]);
160 vector_unset(vtyvec, vty->fd);
167 talloc_free(vty->buf);
171 /* Check configure. */
172 vty_config_unlock(vty);
174 /* VTY_CLOSED is handled by the telnet_interface */
175 vty_event(VTY_CLOSED, vty->fd, vty);
181 int vty_shell(struct vty *vty)
183 return vty->type == VTY_SHELL ? 1 : 0;
187 /* VTY standard output function. */
188 int vty_out(struct vty *vty, const char *format, ...)
196 if (vty_shell(vty)) {
197 va_start(args, format);
198 vprintf(format, args);
201 /* Try to write to initial buffer. */
202 va_start(args, format);
203 len = vsnprintf(buf, sizeof buf, format, args);
206 /* Initial buffer is not enough. */
207 if (len < 0 || len >= size) {
214 p = talloc_realloc_size(vty, p, size);
218 va_start(args, format);
219 len = vsnprintf(p, size, format, args);
222 if (len > -1 && len < size)
227 /* When initial buffer is enough to store all output. */
231 /* Pointer p must point out buffer. */
232 buffer_put(vty->obuf, (u_char *) p, len);
234 /* If p is not different with buf, it is allocated buffer. */
239 vty_event(VTY_WRITE, vty->fd, vty);
244 int vty_out_newline(struct vty *vty)
246 char *p = vty_newline(vty);
247 buffer_put(vty->obuf, p, strlen(p));
251 void *vty_current_index(struct vty *vty)
255 int vty_current_node(struct vty *vty)
260 int vty_config_lock(struct vty *vty)
262 if (vty_config == 0) {
269 int vty_config_unlock(struct vty *vty)
271 if (vty_config == 1 && vty->config == 1) {
278 /* Say hello to vty interface. */
279 void vty_hello(struct vty *vty)
285 f = fopen(host.motdfile, "r");
287 while (fgets(buf, sizeof(buf), f)) {
289 /* work backwards to ignore trailling isspace() */
290 for (s = buf + strlen(buf);
291 (s > buf) && isspace(*(s - 1)); s--) ;
293 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
297 vty_out(vty, "MOTD file not found%s", VTY_NEWLINE);
298 } else if (host.motd)
299 vty_out(vty, "%s", host.motd);
302 /* Put out prompt and wait input from user. */
303 static void vty_prompt(struct vty *vty)
305 struct utsname names;
306 const char *hostname;
308 if (vty->type == VTY_TERM) {
309 hostname = host.app_info->name;
312 hostname = names.nodename;
314 vty_out(vty, cmd_prompt(vty->node), hostname);
318 /* Command execution over the vty interface. */
319 static int vty_command(struct vty *vty, char *buf)
324 /* Split readline string up into the vector */
325 vline = cmd_make_strvec(buf);
330 ret = cmd_execute_command(vline, vty, NULL, 0);
331 if (ret != CMD_SUCCESS)
334 if (vty->type == VTY_FILE)
335 vty_out(vty, "Warning...%s", VTY_NEWLINE);
337 case CMD_ERR_AMBIGUOUS:
338 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
340 case CMD_ERR_NO_MATCH:
341 vty_out(vty, "%% Unknown command.%s", VTY_NEWLINE);
343 case CMD_ERR_INCOMPLETE:
344 vty_out(vty, "%% Command incomplete.%s", VTY_NEWLINE);
347 cmd_free_strvec(vline);
352 static const char telnet_backward_char = 0x08;
353 static const char telnet_space_char = ' ';
355 /* Basic function to write buffer to vty. */
356 static void vty_write(struct vty *vty, const char *buf, size_t nbytes)
358 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
361 /* Should we do buffering here ? And make vty_flush (vty) ? */
362 buffer_put(vty->obuf, buf, nbytes);
365 /* Ensure length of input buffer. Is buffer is short, double it. */
366 static void vty_ensure(struct vty *vty, int length)
368 if (vty->max <= length) {
370 vty->buf = talloc_realloc_size(vty, vty->buf, vty->max);
371 // FIXME: check return
375 /* Basic function to insert character into vty. */
376 static void vty_self_insert(struct vty *vty, char c)
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;
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);
394 /* Self insert character 'c' in overwrite mode. */
395 static void vty_self_insert_overwrite(struct vty *vty, char c)
397 vty_ensure(vty, vty->length + 1);
398 vty->buf[vty->cp++] = c;
400 if (vty->cp > vty->length)
403 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
406 vty_write(vty, &c, 1);
409 /* Insert a word into vty interface with overwrite mode. */
410 static void vty_insert_word_overwrite(struct vty *vty, char *str)
412 int len = strlen(str);
413 vty_write(vty, str, len);
414 strcpy(&vty->buf[vty->cp], str);
416 vty->length = vty->cp;
419 /* Forward character. */
420 static void vty_forward_char(struct vty *vty)
422 if (vty->cp < vty->length) {
423 vty_write(vty, &vty->buf[vty->cp], 1);
428 /* Backward character. */
429 static void vty_backward_char(struct vty *vty)
433 vty_write(vty, &telnet_backward_char, 1);
437 /* Move to the beginning of the line. */
438 static void vty_beginning_of_line(struct vty *vty)
441 vty_backward_char(vty);
444 /* Move to the end of the line. */
445 static void vty_end_of_line(struct vty *vty)
447 while (vty->cp < vty->length)
448 vty_forward_char(vty);
451 /* Add current command line to the history buffer. */
452 static void vty_hist_add(struct vty *vty)
456 if (vty->length == 0)
459 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
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;
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);
473 /* History index rotation. */
475 if (vty->hindex == VTY_MAXHIST)
478 vty->hp = vty->hindex;
481 /* Get telnet window size. */
483 vty_telnet_option (struct vty *vty, unsigned char *buf, int nbytes)
485 #ifdef TELNET_OPTION_DEBUG
488 for (i = 0; i < nbytes; i++)
493 vty_out (vty, "IAC ");
496 vty_out (vty, "WILL ");
499 vty_out (vty, "WONT ");
502 vty_out (vty, "DO ");
505 vty_out (vty, "DONT ");
508 vty_out (vty, "SB ");
511 vty_out (vty, "SE ");
514 vty_out (vty, "TELOPT_ECHO %s", VTY_NEWLINE);
517 vty_out (vty, "TELOPT_SGA %s", VTY_NEWLINE);
520 vty_out (vty, "TELOPT_NAWS %s", VTY_NEWLINE);
523 vty_out (vty, "%x ", buf[i]);
527 vty_out (vty, "%s", VTY_NEWLINE);
529 #endif /* TELNET_OPTION_DEBUG */
535 vty->iac_sb_in_progress = 1;
540 if (!vty->iac_sb_in_progress)
543 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0'))
545 vty->iac_sb_in_progress = 0;
548 switch (vty->sb_buf[0])
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);
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);
571 vty->iac_sb_in_progress = 0;
581 /* Execute current command line. */
582 static int vty_execute(struct vty *vty)
590 case AUTH_ENABLE_NODE:
591 vty_auth(vty, vty->buf);
594 ret = vty_command(vty, vty->buf);
595 if (vty->type == VTY_TERM)
600 /* Clear command line buffer. */
601 vty->cp = vty->length = 0;
604 if (vty->status != VTY_CLOSE)
610 /* Send WILL TELOPT_ECHO to remote server. */
612 vty_will_echo (struct vty *vty)
614 unsigned char cmd[] = { IAC, WILL, TELOPT_ECHO, '\0' };
615 vty_out (vty, "%s", cmd);
618 /* Make suppress Go-Ahead telnet option. */
620 vty_will_suppress_go_ahead (struct vty *vty)
622 unsigned char cmd[] = { IAC, WILL, TELOPT_SGA, '\0' };
623 vty_out (vty, "%s", cmd);
626 /* Make don't use linemode over telnet. */
628 vty_dont_linemode (struct vty *vty)
630 unsigned char cmd[] = { IAC, DONT, TELOPT_LINEMODE, '\0' };
631 vty_out (vty, "%s", cmd);
634 /* Use window size. */
636 vty_do_window_size (struct vty *vty)
638 unsigned char cmd[] = { IAC, DO, TELOPT_NAWS, '\0' };
639 vty_out (vty, "%s", cmd);
642 static void vty_kill_line_from_beginning(struct vty *);
643 static void vty_redraw_line(struct vty *);
645 /* Print command line history. This function is called from
646 vty_next_line and vty_previous_line. */
647 static void vty_history_print(struct vty *vty)
651 vty_kill_line_from_beginning(vty);
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;
658 /* Redraw current line */
659 vty_redraw_line(vty);
662 /* Show next command line history. */
663 static void vty_next_line(struct vty *vty)
667 if (vty->hp == vty->hindex)
670 /* Try is there history exist or not. */
672 if (try_index == (VTY_MAXHIST - 1))
677 /* If there is not history return. */
678 if (vty->hist[try_index] == NULL)
683 vty_history_print(vty);
686 /* Show previous command line history. */
687 static void vty_previous_line(struct vty *vty)
693 try_index = VTY_MAXHIST - 1;
697 if (vty->hist[try_index] == NULL)
702 vty_history_print(vty);
705 /* This function redraw all of the command line character. */
706 static void vty_redraw_line(struct vty *vty)
708 vty_write(vty, vty->buf, vty->length);
709 vty->cp = vty->length;
713 static void vty_forward_word(struct vty *vty)
715 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
716 vty_forward_char(vty);
718 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
719 vty_forward_char(vty);
722 /* Backward word without skipping training space. */
723 static void vty_backward_pure_word(struct vty *vty)
725 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
726 vty_backward_char(vty);
730 static void vty_backward_word(struct vty *vty)
732 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
733 vty_backward_char(vty);
735 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
736 vty_backward_char(vty);
739 /* When '^D' is typed at the beginning of the line we move to the down
741 static void vty_down_level(struct vty *vty)
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);
751 /* When '^Z' is received from vty, move down to the enable mode. */
752 static void vty_end_config(struct vty *vty)
754 vty_out(vty, "%s", VTY_NEWLINE);
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 */
765 vty_config_unlock(vty);
766 vty->node = ENABLE_NODE;
769 vty->node = CONFIG_NODE;
772 /* Unknown node, we have to ignore it. */
780 /* Delete a charcter at the current point. */
781 static void vty_delete_char(struct vty *vty)
786 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
789 if (vty->length == 0) {
794 if (vty->cp == vty->length)
795 return; /* completion need here? */
797 size = vty->length - vty->cp;
800 memmove(&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
801 vty->buf[vty->length] = '\0';
803 vty_write(vty, &vty->buf[vty->cp], size - 1);
804 vty_write(vty, &telnet_space_char, 1);
806 for (i = 0; i < size; i++)
807 vty_write(vty, &telnet_backward_char, 1);
810 /* Delete a character before the point. */
811 static void vty_delete_backward_char(struct vty *vty)
816 vty_backward_char(vty);
817 vty_delete_char(vty);
820 /* Kill rest of line from current point. */
821 static void vty_kill_line(struct vty *vty)
826 size = vty->length - vty->cp;
831 for (i = 0; i < size; i++)
832 vty_write(vty, &telnet_space_char, 1);
833 for (i = 0; i < size; i++)
834 vty_write(vty, &telnet_backward_char, 1);
836 memset(&vty->buf[vty->cp], 0, size);
837 vty->length = vty->cp;
840 /* Kill line from the beginning. */
841 static void vty_kill_line_from_beginning(struct vty *vty)
843 vty_beginning_of_line(vty);
847 /* Delete a word before the point. */
848 static void vty_forward_kill_word(struct vty *vty)
850 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
851 vty_delete_char(vty);
852 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
853 vty_delete_char(vty);
856 /* Delete a word before the point. */
857 static void vty_backward_kill_word(struct vty *vty)
859 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
860 vty_delete_backward_char(vty);
861 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
862 vty_delete_backward_char(vty);
865 /* Transpose chars before or at the point. */
866 static void vty_transpose_chars(struct vty *vty)
870 /* If length is short or point is near by the beginning of line then
872 if (vty->length < 2 || vty->cp < 1)
875 /* In case of point is located at the end of the line. */
876 if (vty->cp == vty->length) {
877 c1 = vty->buf[vty->cp - 1];
878 c2 = vty->buf[vty->cp - 2];
880 vty_backward_char(vty);
881 vty_backward_char(vty);
882 vty_self_insert_overwrite(vty, c1);
883 vty_self_insert_overwrite(vty, c2);
885 c1 = vty->buf[vty->cp];
886 c2 = vty->buf[vty->cp - 1];
888 vty_backward_char(vty);
889 vty_self_insert_overwrite(vty, c1);
890 vty_self_insert_overwrite(vty, c2);
894 /* Do completion at vty interface. */
895 static void vty_complete_command(struct vty *vty)
899 char **matched = NULL;
902 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
905 vline = cmd_make_strvec(vty->buf);
909 /* In case of 'help \t'. */
910 if (isspace((int)vty->buf[vty->length - 1]))
911 vector_set(vline, '\0');
913 matched = cmd_complete_command(vline, vty, &ret);
915 cmd_free_strvec(vline);
917 vty_out(vty, "%s", VTY_NEWLINE);
919 case CMD_ERR_AMBIGUOUS:
920 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
922 vty_redraw_line(vty);
924 case CMD_ERR_NO_MATCH:
925 /* vty_out (vty, "%% There is no matched command.%s", VTY_NEWLINE); */
927 vty_redraw_line(vty);
929 case CMD_COMPLETE_FULL_MATCH:
931 vty_redraw_line(vty);
932 vty_backward_pure_word(vty);
933 vty_insert_word_overwrite(vty, matched[0]);
934 vty_self_insert(vty, ' ');
935 talloc_free(matched[0]);
937 case CMD_COMPLETE_MATCH:
939 vty_redraw_line(vty);
940 vty_backward_pure_word(vty);
941 vty_insert_word_overwrite(vty, matched[0]);
942 talloc_free(matched[0]);
944 case CMD_COMPLETE_LIST_MATCH:
945 for (i = 0; matched[i] != NULL; i++) {
946 if (i != 0 && ((i % 6) == 0))
947 vty_out(vty, "%s", VTY_NEWLINE);
948 vty_out(vty, "%-10s ", matched[i]);
949 talloc_free(matched[i]);
951 vty_out(vty, "%s", VTY_NEWLINE);
954 vty_redraw_line(vty);
956 case CMD_ERR_NOTHING_TODO:
958 vty_redraw_line(vty);
964 vector_only_index_free(matched);
968 vty_describe_fold(struct vty *vty, int cmd_width,
969 unsigned int desc_width, struct desc *desc)
975 cmd = desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd;
977 if (desc_width <= 0) {
978 vty_out(vty, " %-*s %s%s", cmd_width, cmd, desc->str,
983 buf = _talloc_zero(vty, strlen(desc->str) + 1, "describe_fold");
987 for (p = desc->str; strlen(p) > desc_width; p += pos + 1) {
988 for (pos = desc_width; pos > 0; pos--)
989 if (*(p + pos) == ' ')
995 strncpy(buf, p, pos);
997 vty_out(vty, " %-*s %s%s", cmd_width, cmd, buf, VTY_NEWLINE);
1002 vty_out(vty, " %-*s %s%s", cmd_width, cmd, p, VTY_NEWLINE);
1007 /* Describe matched command function. */
1008 static void vty_describe_command(struct vty *vty)
1013 unsigned int i, width, desc_width;
1014 struct desc *desc, *desc_cr = NULL;
1016 vline = cmd_make_strvec(vty->buf);
1018 /* In case of '> ?'. */
1019 if (vline == NULL) {
1020 vline = vector_init(1);
1021 vector_set(vline, '\0');
1022 } else if (isspace((int)vty->buf[vty->length - 1]))
1023 vector_set(vline, '\0');
1025 describe = cmd_describe_command(vline, vty, &ret);
1027 vty_out(vty, "%s", VTY_NEWLINE);
1029 /* Ambiguous error. */
1031 case CMD_ERR_AMBIGUOUS:
1032 cmd_free_strvec(vline);
1033 vty_out(vty, "%% Ambiguous command.%s", VTY_NEWLINE);
1035 vty_redraw_line(vty);
1038 case CMD_ERR_NO_MATCH:
1039 cmd_free_strvec(vline);
1040 vty_out(vty, "%% There is no matched command.%s", VTY_NEWLINE);
1042 vty_redraw_line(vty);
1047 /* Get width of command string. */
1049 for (i = 0; i < vector_active(describe); i++)
1050 if ((desc = vector_slot(describe, i)) != NULL) {
1053 if (desc->cmd[0] == '\0')
1056 len = strlen(desc->cmd);
1057 if (desc->cmd[0] == '.')
1064 /* Get width of description string. */
1065 desc_width = vty->width - (width + 6);
1067 /* Print out description. */
1068 for (i = 0; i < vector_active(describe); i++)
1069 if ((desc = vector_slot(describe, i)) != NULL) {
1070 if (desc->cmd[0] == '\0')
1073 if (strcmp(desc->cmd, "<cr>") == 0) {
1079 vty_out(vty, " %-s%s",
1081 '.' ? desc->cmd + 1 : desc->cmd,
1083 else if (desc_width >= strlen(desc->str))
1084 vty_out(vty, " %-*s %s%s", width,
1086 '.' ? desc->cmd + 1 : desc->cmd,
1087 desc->str, VTY_NEWLINE);
1089 vty_describe_fold(vty, width, desc_width, desc);
1092 vty_out(vty, " %-*s %s%s", width
1093 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1094 desc->str ? desc->str : "", VTY_NEWLINE);
1098 if ((desc = desc_cr)) {
1100 vty_out(vty, " %-s%s",
1101 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1103 else if (desc_width >= strlen(desc->str))
1104 vty_out(vty, " %-*s %s%s", width,
1105 desc->cmd[0] == '.' ? desc->cmd + 1 : desc->cmd,
1106 desc->str, VTY_NEWLINE);
1108 vty_describe_fold(vty, width, desc_width, desc);
1111 cmd_free_strvec(vline);
1112 vector_free(describe);
1115 vty_redraw_line(vty);
1118 /* ^C stop current input and do not add command line to the history. */
1119 static void vty_stop_input(struct vty *vty)
1121 vty->cp = vty->length = 0;
1123 vty_out(vty, "%s", VTY_NEWLINE);
1125 switch (vty->node) {
1128 /* Nothing to do. */
1132 vty_config_unlock(vty);
1133 vty->node = ENABLE_NODE;
1136 vty->node = CONFIG_NODE;
1139 /* Unknown node, we have to ignore it. */
1144 /* Set history pointer to the latest one. */
1145 vty->hp = vty->hindex;
1148 #define CONTROL(X) ((X) - '@')
1149 #define VTY_NORMAL 0
1150 #define VTY_PRE_ESCAPE 1
1151 #define VTY_ESCAPE 2
1153 /* Escape character command map. */
1154 static void vty_escape_map(unsigned char c, struct vty *vty)
1158 vty_previous_line(vty);
1164 vty_forward_char(vty);
1167 vty_backward_char(vty);
1173 /* Go back to normal mode. */
1174 vty->escape = VTY_NORMAL;
1177 /* Quit print out to the buffer. */
1178 static void vty_buffer_reset(struct vty *vty)
1180 buffer_reset(vty->obuf);
1182 vty_redraw_line(vty);
1185 /* Read data via vty socket. */
1186 int vty_read(struct vty *vty)
1190 unsigned char buf[VTY_READ_BUFSIZ];
1192 int vty_sock = vty->fd;
1194 /* Read raw data from socket */
1195 if ((nbytes = read(vty->fd, buf, VTY_READ_BUFSIZ)) <= 0) {
1197 if (ERRNO_IO_RETRY(errno)) {
1198 vty_event(VTY_READ, vty_sock, vty);
1202 buffer_reset(vty->obuf);
1203 vty->status = VTY_CLOSE;
1206 for (i = 0; i < nbytes; i++) {
1207 if (buf[i] == IAC) {
1216 if (vty->iac_sb_in_progress && !vty->iac) {
1217 if (vty->sb_len < sizeof(vty->sb_buf))
1218 vty->sb_buf[vty->sb_len] = buf[i];
1224 /* In case of telnet command */
1226 ret = vty_telnet_option(vty, buf + i, nbytes - i);
1232 if (vty->status == VTY_MORE) {
1237 vty_buffer_reset(vty);
1239 #if 0 /* More line does not work for "show ip bgp". */
1242 vty->status = VTY_MORELINE;
1251 /* Escape character. */
1252 if (vty->escape == VTY_ESCAPE) {
1253 vty_escape_map(buf[i], vty);
1257 /* Pre-escape status. */
1258 if (vty->escape == VTY_PRE_ESCAPE) {
1261 vty->escape = VTY_ESCAPE;
1264 vty_backward_word(vty);
1265 vty->escape = VTY_NORMAL;
1268 vty_forward_word(vty);
1269 vty->escape = VTY_NORMAL;
1272 vty_forward_kill_word(vty);
1273 vty->escape = VTY_NORMAL;
1277 vty_backward_kill_word(vty);
1278 vty->escape = VTY_NORMAL;
1281 vty->escape = VTY_NORMAL;
1289 vty_beginning_of_line(vty);
1292 vty_backward_char(vty);
1295 vty_stop_input(vty);
1298 vty_delete_char(vty);
1301 vty_end_of_line(vty);
1304 vty_forward_char(vty);
1308 vty_delete_backward_char(vty);
1317 vty_previous_line(vty);
1320 vty_transpose_chars(vty);
1323 vty_kill_line_from_beginning(vty);
1326 vty_backward_kill_word(vty);
1329 vty_end_config(vty);
1333 vty_out(vty, "%s", VTY_NEWLINE);
1337 vty_complete_command(vty);
1340 if (vty->node == AUTH_NODE
1341 || vty->node == AUTH_ENABLE_NODE)
1342 vty_self_insert(vty, buf[i]);
1344 vty_describe_command(vty);
1347 if (i + 1 < nbytes && buf[i + 1] == '[') {
1348 vty->escape = VTY_ESCAPE;
1351 vty->escape = VTY_PRE_ESCAPE;
1354 if (buf[i] > 31 && buf[i] < 127)
1355 vty_self_insert(vty, buf[i]);
1361 if (vty->status == VTY_CLOSE)
1364 vty_event(VTY_WRITE, vty_sock, vty);
1365 vty_event(VTY_READ, vty_sock, vty);
1370 /* Read up configuration file */
1372 vty_read_file(FILE *confp, void *priv)
1379 vty->type = VTY_FILE;
1380 vty->node = CONFIG_NODE;
1383 ret = config_from_file(vty, confp);
1385 if (ret != CMD_SUCCESS) {
1387 case CMD_ERR_AMBIGUOUS:
1388 fprintf(stderr, "Ambiguous command.\n");
1390 case CMD_ERR_NO_MATCH:
1391 fprintf(stderr, "There is no such command.\n");
1394 fprintf(stderr, "Error occurred during reading below "
1395 "line:\n%s\n", vty->buf);
1404 /* Create new vty structure. */
1406 vty_create (int vty_sock, void *priv)
1412 tcgetattr(vty_sock, &t);
1414 tcsetattr(vty_sock, TCSANOW, &t);
1416 /* Allocate new vty structure and set up default values. */
1420 vty->type = VTY_TERM;
1421 if (no_password_check)
1424 vty->node = ENABLE_NODE;
1426 vty->node = VIEW_NODE;
1429 vty->node = AUTH_NODE;
1432 vty_clear_buf (vty);
1434 memset (vty->hist, 0, sizeof (vty->hist));
1437 vector_set_index (vtyvec, vty_sock, vty);
1438 vty->status = VTY_NORMAL;
1439 if (host.lines >= 0)
1440 vty->lines = host.lines;
1444 if (! no_password_check)
1446 /* Vty is not available if password isn't set. */
1447 if (host.password == NULL && host.password_encrypt == NULL)
1449 vty_out (vty, "Vty password is not set.%s", VTY_NEWLINE);
1450 vty->status = VTY_CLOSE;
1456 /* Say hello to the world. */
1458 if (! no_password_check)
1459 vty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
1461 /* Setting up terminal. */
1462 vty_will_echo (vty);
1463 vty_will_suppress_go_ahead (vty);
1465 vty_dont_linemode (vty);
1466 vty_do_window_size (vty);
1467 /* vty_dont_lflow_ahead (vty); */
1471 /* Add read/write thread. */
1472 vty_event (VTY_WRITE, vty_sock, vty);
1473 vty_event (VTY_READ, vty_sock, vty);
1478 DEFUN(config_who, config_who_cmd, "who", "Display who is on vty\n")
1483 for (i = 0; i < vector_active(vtyvec); i++)
1484 if ((v = vector_slot(vtyvec, i)) != NULL)
1485 vty_out(vty, "%svty[%d] %s",
1486 v->config ? "*" : " ", i, VTY_NEWLINE);
1490 /* Move to vty configuration mode. */
1493 "line vty", "Configure a terminal line\n" "Virtual terminal\n")
1495 vty->node = VTY_NODE;
1500 DEFUN(vty_login, vty_login_cmd, "login", "Enable password checking\n")
1502 no_password_check = 0;
1507 no_vty_login_cmd, "no login", NO_STR "Enable password checking\n")
1509 no_password_check = 1;
1513 DEFUN(service_advanced_vty,
1514 service_advanced_vty_cmd,
1515 "service advanced-vty",
1516 "Set up miscellaneous service\n" "Enable advanced mode vty interface\n")
1522 DEFUN(no_service_advanced_vty,
1523 no_service_advanced_vty_cmd,
1524 "no service advanced-vty",
1526 "Set up miscellaneous service\n" "Enable advanced mode vty interface\n")
1532 DEFUN(terminal_monitor,
1533 terminal_monitor_cmd,
1535 "Set terminal line parameters\n"
1536 "Copy debug output to the current terminal line\n")
1542 DEFUN(terminal_no_monitor,
1543 terminal_no_monitor_cmd,
1544 "terminal no monitor",
1545 "Set terminal line parameters\n"
1546 NO_STR "Copy debug output to the current terminal line\n")
1554 "show history", SHOW_STR "Display the session command history\n")
1558 for (index = vty->hindex + 1; index != vty->hindex;) {
1559 if (index == VTY_MAXHIST) {
1564 if (vty->hist[index] != NULL)
1565 vty_out(vty, " %s%s", vty->hist[index], VTY_NEWLINE);
1573 /* Display current configuration. */
1574 static int vty_config_write(struct vty *vty)
1576 vty_out(vty, "line vty%s", VTY_NEWLINE);
1579 if (no_password_check)
1580 vty_out(vty, " no login%s", VTY_NEWLINE);
1582 vty_out(vty, "!%s", VTY_NEWLINE);
1587 struct cmd_node vty_node = {
1589 "%s(config-line)# ",
1593 /* Reset all VTY status. */
1598 struct thread *vty_serv_thread;
1600 for (i = 0; i < vector_active(vtyvec); i++)
1601 if ((vty = vector_slot(vtyvec, i)) != NULL) {
1602 buffer_reset(vty->obuf);
1603 vty->status = VTY_CLOSE;
1607 for (i = 0; i < vector_active(Vvty_serv_thread); i++)
1608 if ((vty_serv_thread =
1609 vector_slot(Vvty_serv_thread, i)) != NULL) {
1610 //thread_cancel (vty_serv_thread);
1611 vector_slot(Vvty_serv_thread, i) = NULL;
1616 static void vty_save_cwd(void)
1618 char cwd[MAXPATHLEN];
1621 c = getcwd(cwd, MAXPATHLEN);
1624 if (chdir(SYSCONFDIR) != 0)
1625 perror("chdir failed");
1626 if (getcwd(cwd, MAXPATHLEN) == NULL)
1627 perror("getcwd failed");
1630 vty_cwd = _talloc_zero(tall_vty_ctx, strlen(cwd) + 1, "save_cwd");
1631 strcpy(vty_cwd, cwd);
1639 int vty_shell_serv(struct vty *vty)
1641 return vty->type == VTY_SHELL_SERV ? 1 : 0;
1644 void vty_init_vtysh()
1646 vtyvec = vector_init(VECTOR_MIN_SIZE);
1649 extern void *tall_bsc_ctx;
1650 /* Install vty's own commands like `who' command. */
1651 void vty_init(struct vty_app_info *app_info)
1653 tall_vty_ctx = talloc_named_const(app_info->tall_ctx, 0, "vty");
1654 tall_vty_vec_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_vector");
1655 tall_vty_cmd_ctx = talloc_named_const(tall_vty_ctx, 0, "vty_command");
1659 host.app_info = app_info;
1661 /* For further configuration read, preserve current directory. */
1664 vtyvec = vector_init(VECTOR_MIN_SIZE);
1666 /* Install bgp top node. */
1667 install_node(&vty_node, vty_config_write);
1669 install_element_ve(&config_who_cmd);
1670 install_element_ve(&show_history_cmd);
1671 install_element(CONFIG_NODE, &line_vty_cmd);
1672 install_element(CONFIG_NODE, &service_advanced_vty_cmd);
1673 install_element(CONFIG_NODE, &no_service_advanced_vty_cmd);
1674 install_element(CONFIG_NODE, &show_history_cmd);
1675 install_element(ENABLE_NODE, &terminal_monitor_cmd);
1676 install_element(ENABLE_NODE, &terminal_no_monitor_cmd);
1678 install_default(VTY_NODE);
1679 install_element(VTY_NODE, &vty_login_cmd);
1680 install_element(VTY_NODE, &no_vty_login_cmd);
1683 int vty_read_config_file(const char *file_name, void *priv)
1688 cfile = fopen(file_name, "r");
1692 rc = vty_read_file(cfile, priv);
1695 host_config_set(file_name);