2 $Id: command.c,v 1.47 2005/04/25 16:26:42 paul Exp $
4 Command interpreter routine for virtual terminal [aka TeletYpe]
5 Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
7 This file is part of GNU Zebra.
9 GNU Zebra is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published
11 by the Free Software Foundation; either version 2, or (at your
12 option) any later version.
14 GNU Zebra is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Zebra; see the file COPYING. If not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
37 #include <osmocom/vty/vector.h>
38 #include <osmocom/vty/vty.h>
39 #include <osmocom/vty/command.h>
41 #include <osmocom/core/talloc.h>
43 #define CONFIGFILE_MASK 022
45 void *tall_vty_cmd_ctx;
47 /* Command vector which includes some level of command lists. Normally
48 each daemon maintains each own cmdvec. */
51 /* Host information structure. */
54 /* Standard command node structures. */
55 struct cmd_node auth_node = {
60 struct cmd_node view_node = {
65 struct cmd_node auth_enable_node = {
70 struct cmd_node enable_node = {
75 struct cmd_node config_node = {
81 /* Default motd string. */
82 const char *default_motd = "";
84 /* This is called from main when a daemon is invoked with -v or --version. */
85 void print_version(int print_copyright)
87 printf("%s version %s\n", host.app_info->name, host.app_info->version);
89 printf("\n%s\n", host.app_info->copyright);
92 /* Utility function to concatenate argv argument into a single string
93 with inserting ' ' character between each argument. */
94 char *argv_concat(const char **argv, int argc, int shift)
102 for (i = shift; i < argc; i++)
103 len += strlen(argv[i]) + 1;
106 p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
107 for (i = shift; i < argc; i++) {
109 memcpy(p, argv[i], (arglen = strlen(argv[i])));
117 /* Install top node of command vector. */
118 void install_node(struct cmd_node *node, int (*func) (struct vty *))
120 vector_set_index(cmdvec, node->node, node);
122 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
125 /* Compare two command's string. Used in sort_node (). */
126 static int cmp_node(const void *p, const void *q)
128 struct cmd_element *a = *(struct cmd_element **)p;
129 struct cmd_element *b = *(struct cmd_element **)q;
131 return strcmp(a->string, b->string);
134 static int cmp_desc(const void *p, const void *q)
136 struct desc *a = *(struct desc **)p;
137 struct desc *b = *(struct desc **)q;
139 return strcmp(a->cmd, b->cmd);
142 static int is_config(struct vty *vty)
144 if (vty->node <= CONFIG_NODE)
146 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
148 else if (host.app_info->is_config_node)
149 return host.app_info->is_config_node(vty, vty->node);
151 return vty->node > CONFIG_NODE;
154 /* Sort each node's command element according to command string. */
158 struct cmd_node *cnode;
160 struct cmd_element *cmd_element;
162 for (i = 0; i < vector_active(cmdvec); i++)
163 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
164 vector cmd_vector = cnode->cmd_vector;
165 qsort(cmd_vector->index, vector_active(cmd_vector),
166 sizeof(void *), cmp_node);
168 for (j = 0; j < vector_active(cmd_vector); j++)
170 vector_slot(cmd_vector, j)) != NULL
171 && vector_active(cmd_element->strvec)) {
173 vector_slot(cmd_element->strvec,
175 (cmd_element->strvec) -
177 qsort(descvec->index,
178 vector_active(descvec),
179 sizeof(void *), cmp_desc);
184 /* Breaking up string into each command piece. I assume given
185 character is separated by a space character. Return value is a
186 vector which includes char ** data element. */
187 vector cmd_make_strvec(const char *string)
189 const char *cp, *start;
199 /* Skip white spaces. */
200 while (isspace((int)*cp) && *cp != '\0')
203 /* Return if there is only white spaces */
207 if (*cp == '!' || *cp == '#')
210 /* Prepare return vector. */
211 strvec = vector_init(VECTOR_MIN_SIZE);
213 /* Copy each command piece and set into vector. */
216 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
220 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
221 memcpy(token, start, strlen);
222 *(token + strlen) = '\0';
223 vector_set(strvec, token);
225 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
234 /* Free allocated string vector. */
235 void cmd_free_strvec(vector v)
243 for (i = 0; i < vector_active(v); i++)
244 if ((cp = vector_slot(v, i)) != NULL)
250 /* Fetch next description. Used in cmd_make_descvec(). */
251 static char *cmd_desc_str(const char **string)
253 const char *cp, *start;
262 /* Skip white spaces. */
263 while (isspace((int)*cp) && *cp != '\0')
266 /* Return if there is only white spaces */
272 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
276 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
277 memcpy(token, start, strlen);
278 *(token + strlen) = '\0';
285 /* New string vector. */
286 static vector cmd_make_descvec(const char *string, const char *descstr)
295 vector strvec = NULL;
304 allvec = vector_init(VECTOR_MIN_SIZE);
307 while (isspace((int)*cp) && *cp != '\0')
320 fprintf(stderr, "Command parse error!: %s\n",
327 while (isspace((int)*cp) && *cp != '\0')
341 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
342 || *cp == ')' || *cp == '|') && *cp != '\0')
347 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
348 memcpy(token, sp, len);
349 *(token + len) = '\0';
351 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
353 desc->str = cmd_desc_str(&dp);
357 strvec = vector_init(VECTOR_MIN_SIZE);
358 vector_set(allvec, strvec);
362 strvec = vector_init(VECTOR_MIN_SIZE);
363 vector_set(allvec, strvec);
365 vector_set(strvec, desc);
369 /* Count mandantory string vector size. This is to determine inputed
370 command has enough command length. */
371 static int cmd_cmdsize(vector strvec)
378 for (i = 0; i < vector_active(strvec); i++)
379 if ((descvec = vector_slot(strvec, i)) != NULL) {
380 if ((vector_active(descvec)) == 1
381 && (desc = vector_slot(descvec, 0)) != NULL) {
382 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
392 /* Return prompt character of specified node. */
393 const char *cmd_prompt(enum node_type node)
395 struct cmd_node *cnode;
397 cnode = vector_slot(cmdvec, node);
398 return cnode->prompt;
401 /* Install a command into a node. */
402 void install_element(enum node_type ntype, struct cmd_element *cmd)
404 struct cmd_node *cnode;
406 cnode = vector_slot(cmdvec, ntype);
410 "Command node %d doesn't exist, please check it\n",
415 vector_set(cnode->cmd_vector, cmd);
417 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
418 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
421 /* Install a command into VIEW and ENABLE node */
422 void install_element_ve(struct cmd_element *cmd)
424 install_element(VIEW_NODE, cmd);
425 install_element(ENABLE_NODE, cmd);
429 static unsigned char itoa64[] =
430 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
432 static void to64(char *s, long v, int n)
435 *s++ = itoa64[v & 0x3f];
440 static char *zencrypt(const char *passwd)
444 char *crypt(const char *, const char *);
446 gettimeofday(&tv, 0);
448 to64(&salt[0], random(), 3);
449 to64(&salt[3], tv.tv_usec, 3);
452 return crypt(passwd, salt);
456 /* This function write configuration of this host. */
457 static int config_write_host(struct vty *vty)
460 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
463 if (host.password_encrypt)
464 vty_out(vty, "password 8 %s%s", host.password_encrypt,
466 if (host.enable_encrypt)
467 vty_out(vty, "enable password 8 %s%s",
468 host.enable_encrypt, VTY_NEWLINE);
471 vty_out(vty, "password %s%s", host.password,
474 vty_out(vty, "enable password %s%s", host.enable,
479 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
482 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
485 vty_out(vty, "service terminal-length %d%s", host.lines,
489 vty_out(vty, "banner motd file %s%s", host.motdfile,
492 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
497 /* Utility function for getting command vector. */
498 static vector cmd_node_vector(vector v, enum node_type ntype)
500 struct cmd_node *cnode = vector_slot(v, ntype);
501 return cnode->cmd_vector;
504 /* Completion match types. */
518 static enum match_type cmd_ipv4_match(const char *str)
521 int dots = 0, nums = 0;
528 memset(buf, 0, sizeof(buf));
530 while (*str != '\0') {
535 if (*(str + 1) == '.')
538 if (*(str + 1) == '\0')
544 if (!isdigit((int)*str))
553 strncpy(buf, sp, str - sp);
571 static enum match_type cmd_ipv4_prefix_match(const char *str)
581 memset(buf, 0, sizeof(buf));
583 while (*str != '\0' && *str != '/') {
588 if (*(str + 1) == '.' || *(str + 1) == '/')
591 if (*(str + 1) == '\0')
598 if (!isdigit((int)*str))
607 strncpy(buf, sp, str - sp);
613 if (*(str + 1) == '\0')
618 } else if (*str == '\0')
629 while (*str != '\0') {
630 if (!isdigit((int)*str))
642 #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
643 #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
644 #define STATE_START 1
645 #define STATE_COLON 2
646 #define STATE_DOUBLE 3
649 #define STATE_SLASH 6
654 static enum match_type cmd_ipv6_match(const char *str)
656 int state = STATE_START;
657 int colons = 0, nums = 0, double_colon = 0;
658 const char *sp = NULL;
659 struct sockaddr_in6 sin6_dummy;
665 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
668 /* use inet_pton that has a better support,
669 * for example inet_pton can support the automatic addresses:
672 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
677 while (*str != '\0') {
681 if (*(str + 1) != ':' && *(str + 1) != '\0')
693 if (*(str + 1) == ':')
694 state = STATE_DOUBLE;
704 if (*(str + 1) == ':')
707 if (*(str + 1) != '\0')
717 if (*(str + 1) == ':' || *(str + 1) == '\0') {
724 if (*(str + 1) == '.')
751 static enum match_type cmd_ipv6_prefix_match(const char *str)
753 int state = STATE_START;
754 int colons = 0, nums = 0, double_colon = 0;
756 const char *sp = NULL;
762 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
765 while (*str != '\0' && state != STATE_MASK) {
769 if (*(str + 1) != ':' && *(str + 1) != '\0')
781 if (*(str + 1) == '/')
783 else if (*(str + 1) == ':')
784 state = STATE_DOUBLE;
794 if (*(str + 1) == ':')
797 if (*(str + 1) != '\0' && *(str + 1) != '/')
801 if (*(str + 1) == '/')
811 if (*(str + 1) == ':' || *(str + 1) == '.'
812 || *(str + 1) == '\0' || *(str + 1) == '/') {
816 for (; sp <= str; sp++)
822 if (*(str + 1) == ':')
824 else if (*(str + 1) == '.')
826 else if (*(str + 1) == '/')
834 if (*(str + 1) == '\0')
852 if (state < STATE_MASK)
855 mask = strtol(str, &endptr, 10);
859 if (mask < 0 || mask > 128)
862 /* I don't know why mask < 13 makes command match partly.
863 Forgive me to make this comments. I Want to set static default route
864 because of lack of function to originate default in ospf6d; sorry
873 #endif /* HAVE_IPV6 */
875 #define DECIMAL_STRLEN_MAX 10
877 static int cmd_range_match(const char *range, const char *str)
880 char buf[DECIMAL_STRLEN_MAX + 1];
886 if (range[1] == '-') {
887 signed long min = 0, max = 0, val;
889 val = strtol(str, &endptr, 10);
894 p = strchr(range, '-');
897 if (p - range > DECIMAL_STRLEN_MAX)
899 strncpy(buf, range, p - range);
900 buf[p - range] = '\0';
901 min = -strtol(buf, &endptr, 10);
906 p = strchr(range, '>');
909 if (p - range > DECIMAL_STRLEN_MAX)
911 strncpy(buf, range, p - range);
912 buf[p - range] = '\0';
913 max = strtol(buf, &endptr, 10);
917 if (val < min || val > max)
920 unsigned long min, max, val;
922 val = strtoul(str, &endptr, 10);
927 p = strchr(range, '-');
930 if (p - range > DECIMAL_STRLEN_MAX)
932 strncpy(buf, range, p - range);
933 buf[p - range] = '\0';
934 min = strtoul(buf, &endptr, 10);
939 p = strchr(range, '>');
942 if (p - range > DECIMAL_STRLEN_MAX)
944 strncpy(buf, range, p - range);
945 buf[p - range] = '\0';
946 max = strtoul(buf, &endptr, 10);
950 if (val < min || val > max)
957 /* Make completion match and return match type flag. */
958 static enum match_type
959 cmd_filter_by_completion(char *command, vector v, unsigned int index)
963 struct cmd_element *cmd_element;
964 enum match_type match_type;
968 match_type = no_match;
970 /* If command and cmd_element string does not match set NULL to vector */
971 for (i = 0; i < vector_active(v); i++)
972 if ((cmd_element = vector_slot(v, i)) != NULL) {
973 if (index >= vector_active(cmd_element->strvec))
974 vector_slot(v, i) = NULL;
980 vector_slot(cmd_element->strvec, index);
982 for (j = 0; j < vector_active(descvec); j++)
983 if ((desc = vector_slot(descvec, j))) {
986 if (CMD_VARARG(str)) {
992 } else if (CMD_RANGE(str)) {
1005 else if (CMD_IPV6(str)) {
1016 } else if (CMD_IPV6_PREFIX(str)) {
1017 if (cmd_ipv6_prefix_match(command)) {
1027 #endif /* HAVE_IPV6 */
1028 else if (CMD_IPV4(str)) {
1039 } else if (CMD_IPV4_PREFIX(str)) {
1040 if (cmd_ipv4_prefix_match(command)) {
1049 /* Check is this point's argument optional ? */
1052 CMD_VARIABLE(str)) {
1063 if (strcmp(command, str)
1078 vector_slot(v, i) = NULL;
1084 /* Filter vector by command character with index. */
1085 static enum match_type
1086 cmd_filter_by_string(char *command, vector v, unsigned int index)
1090 struct cmd_element *cmd_element;
1091 enum match_type match_type;
1095 match_type = no_match;
1097 /* If command and cmd_element string does not match set NULL to vector */
1098 for (i = 0; i < vector_active(v); i++)
1099 if ((cmd_element = vector_slot(v, i)) != NULL) {
1100 /* If given index is bigger than max string vector of command,
1102 if (index >= vector_active(cmd_element->strvec))
1103 vector_slot(v, i) = NULL;
1109 vector_slot(cmd_element->strvec, index);
1111 for (j = 0; j < vector_active(descvec); j++)
1112 if ((desc = vector_slot(descvec, j))) {
1115 if (CMD_VARARG(str)) {
1121 } else if (CMD_RANGE(str)) {
1133 else if (CMD_IPV6(str)) {
1144 } else if (CMD_IPV6_PREFIX(str)) {
1145 if (cmd_ipv6_prefix_match(command) == exact_match) {
1154 #endif /* HAVE_IPV6 */
1155 else if (CMD_IPV4(str)) {
1166 } else if (CMD_IPV4_PREFIX(str)) {
1167 if (cmd_ipv4_prefix_match(command) == exact_match) {
1175 } else if (CMD_OPTION(str)
1176 || CMD_VARIABLE(str))
1184 if (strcmp(command, str)
1193 vector_slot(v, i) = NULL;
1199 /* Check ambiguous match */
1201 is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1205 const char *str = NULL;
1206 struct cmd_element *cmd_element;
1207 const char *matched = NULL;
1211 for (i = 0; i < vector_active(v); i++)
1212 if ((cmd_element = vector_slot(v, i)) != NULL) {
1215 descvec = vector_slot(cmd_element->strvec, index);
1217 for (j = 0; j < vector_active(descvec); j++)
1218 if ((desc = vector_slot(descvec, j))) {
1219 enum match_type ret;
1227 || CMD_VARIABLE(str))
1228 && strcmp(command, str) == 0)
1234 || CMD_VARIABLE(str))
1235 && strncmp(command, str, strlen(command)) == 0) {
1239 return 1; /* There is ambiguous match. */
1262 case ipv6_prefix_match:
1264 cmd_ipv6_prefix_match
1265 (command)) != no_match) {
1266 if (ret == partly_match)
1267 return 2; /* There is incomplete match. */
1272 #endif /* HAVE_IPV6 */
1277 case ipv4_prefix_match:
1279 cmd_ipv4_prefix_match
1280 (command)) != no_match) {
1281 if (ret == partly_match)
1282 return 2; /* There is incomplete match. */
1289 || CMD_VARIABLE(str))
1298 vector_slot(v, i) = NULL;
1303 /* If src matches dst return dst string, otherwise return NULL */
1304 static const char *cmd_entry_function(const char *src, const char *dst)
1306 /* Skip variable arguments. */
1307 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1308 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1311 /* In case of 'command \t', given src is NULL string. */
1315 /* Matched with input string. */
1316 if (strncmp(src, dst, strlen(src)) == 0)
1322 /* If src matches dst return dst string, otherwise return NULL */
1323 /* This version will return the dst string always if it is
1324 CMD_VARIABLE for '?' key processing */
1325 static const char *cmd_entry_function_desc(const char *src, const char *dst)
1327 if (CMD_VARARG(dst))
1330 if (CMD_RANGE(dst)) {
1331 if (cmd_range_match(dst, src))
1337 if (CMD_IPV6(dst)) {
1338 if (cmd_ipv6_match(src))
1344 if (CMD_IPV6_PREFIX(dst)) {
1345 if (cmd_ipv6_prefix_match(src))
1350 #endif /* HAVE_IPV6 */
1352 if (CMD_IPV4(dst)) {
1353 if (cmd_ipv4_match(src))
1359 if (CMD_IPV4_PREFIX(dst)) {
1360 if (cmd_ipv4_prefix_match(src))
1366 /* Optional or variable commands always match on '?' */
1367 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1370 /* In case of 'command \t', given src is NULL string. */
1374 if (strncmp(src, dst, strlen(src)) == 0)
1380 /* Check same string element existence. If it isn't there return
1382 static int cmd_unique_string(vector v, const char *str)
1387 for (i = 0; i < vector_active(v); i++)
1388 if ((match = vector_slot(v, i)) != NULL)
1389 if (strcmp(match, str) == 0)
1394 /* Compare string to description vector. If there is same string
1395 return 1 else return 0. */
1396 static int desc_unique_string(vector v, const char *str)
1401 for (i = 0; i < vector_active(v); i++)
1402 if ((desc = vector_slot(v, i)) != NULL)
1403 if (strcmp(desc->cmd, str) == 0)
1408 static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1410 if (first_word != NULL &&
1411 node != AUTH_NODE &&
1412 node != VIEW_NODE &&
1413 node != AUTH_ENABLE_NODE &&
1414 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1419 /* '?' describe command support. */
1421 cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1425 #define INIT_MATCHVEC_SIZE 10
1427 struct cmd_element *cmd_element;
1430 enum match_type match;
1432 static struct desc desc_cr = { "<cr>", "" };
1435 if (vector_active(vline) == 0) {
1436 *status = CMD_ERR_NO_MATCH;
1439 index = vector_active(vline) - 1;
1441 /* Make copy vector of current node's command vector. */
1442 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1444 /* Prepare match vector */
1445 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1447 /* Filter commands. */
1448 /* Only words precedes current word will be checked in this loop. */
1449 for (i = 0; i < index; i++)
1450 if ((command = vector_slot(vline, i))) {
1452 cmd_filter_by_completion(command, cmd_vector, i);
1454 if (match == vararg_match) {
1455 struct cmd_element *cmd_element;
1459 for (j = 0; j < vector_active(cmd_vector); j++)
1461 vector_slot(cmd_vector, j)) != NULL
1464 (cmd_element->strvec))) {
1466 vector_slot(cmd_element->
1472 k < vector_active(descvec);
1475 vector_slot(descvec,
1477 vector_set(matchvec,
1482 vector_set(matchvec, &desc_cr);
1483 vector_free(cmd_vector);
1489 is_cmd_ambiguous(command, cmd_vector, i,
1491 vector_free(cmd_vector);
1492 *status = CMD_ERR_AMBIGUOUS;
1494 } else if (ret == 2) {
1495 vector_free(cmd_vector);
1496 *status = CMD_ERR_NO_MATCH;
1501 /* Prepare match vector */
1502 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1504 /* Make sure that cmd_vector is filtered based on current word */
1505 command = vector_slot(vline, index);
1507 match = cmd_filter_by_completion(command, cmd_vector, index);
1509 /* Make description vector. */
1510 for (i = 0; i < vector_active(cmd_vector); i++)
1511 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1512 const char *string = NULL;
1513 vector strvec = cmd_element->strvec;
1515 /* if command is NULL, index may be equal to vector_active */
1516 if (command && index >= vector_active(strvec))
1517 vector_slot(cmd_vector, i) = NULL;
1519 /* Check if command is completed. */
1521 && index == vector_active(strvec)) {
1523 if (!desc_unique_string
1525 vector_set(matchvec, &desc_cr);
1529 vector_slot(strvec, index);
1532 for (j = 0; j < vector_active(descvec);
1535 vector_slot(descvec, j))) {
1537 cmd_entry_function_desc
1541 /* Uniqueness check */
1542 if (!desc_unique_string(matchvec, string))
1551 vector_free(cmd_vector);
1553 if (vector_slot(matchvec, 0) == NULL) {
1554 vector_free(matchvec);
1555 *status = CMD_ERR_NO_MATCH;
1557 *status = CMD_SUCCESS;
1562 vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1566 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1567 enum node_type onode;
1568 vector shifted_vline;
1572 vty->node = ENABLE_NODE;
1573 /* We can try it on enable node, cos' the vty is authenticated */
1575 shifted_vline = vector_init(vector_count(vline));
1577 for (index = 1; index < vector_active(vline); index++) {
1578 vector_set_index(shifted_vline, index - 1,
1579 vector_lookup(vline, index));
1582 ret = cmd_describe_command_real(shifted_vline, vty, status);
1584 vector_free(shifted_vline);
1589 return cmd_describe_command_real(vline, vty, status);
1592 /* Check LCD of matched command. */
1593 static int cmd_lcd(char **matched)
1601 if (matched[0] == NULL || matched[1] == NULL)
1604 for (i = 1; matched[i] != NULL; i++) {
1605 s1 = matched[i - 1];
1608 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1622 /* Command line completion support. */
1623 static char **cmd_complete_command_real(vector vline, struct vty *vty,
1627 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1628 #define INIT_MATCHVEC_SIZE 10
1630 struct cmd_element *cmd_element;
1638 if (vector_active(vline) == 0) {
1639 *status = CMD_ERR_NO_MATCH;
1642 index = vector_active(vline) - 1;
1644 /* First, filter by preceeding command string */
1645 for (i = 0; i < index; i++)
1646 if ((command = vector_slot(vline, i))) {
1647 enum match_type match;
1650 /* First try completion match, if there is exactly match return 1 */
1652 cmd_filter_by_completion(command, cmd_vector, i);
1654 /* If there is exact match then filter ambiguous match else check
1657 is_cmd_ambiguous(command, cmd_vector, i,
1659 vector_free(cmd_vector);
1660 *status = CMD_ERR_AMBIGUOUS;
1666 vector_free (cmd_vector);
1667 *status = CMD_ERR_NO_MATCH;
1673 /* Prepare match vector. */
1674 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1676 /* Now we got into completion */
1677 for (i = 0; i < vector_active(cmd_vector); i++)
1678 if ((cmd_element = vector_slot(cmd_vector, i))) {
1680 vector strvec = cmd_element->strvec;
1682 /* Check field length */
1683 if (index >= vector_active(strvec))
1684 vector_slot(cmd_vector, i) = NULL;
1688 descvec = vector_slot(strvec, index);
1689 for (j = 0; j < vector_active(descvec); j++)
1690 if ((desc = vector_slot(descvec, j))) {
1691 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1692 if (cmd_unique_string (matchvec, string))
1693 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1698 /* We don't need cmd_vector any more. */
1699 vector_free(cmd_vector);
1701 /* No matched command */
1702 if (vector_slot(matchvec, 0) == NULL) {
1703 vector_free(matchvec);
1705 /* In case of 'command \t' pattern. Do you need '?' command at
1706 the end of the line. */
1707 if (vector_slot(vline, index) == '\0')
1708 *status = CMD_ERR_NOTHING_TODO;
1710 *status = CMD_ERR_NO_MATCH;
1714 /* Only one matched */
1715 if (vector_slot(matchvec, 1) == NULL) {
1716 match_str = (char **)matchvec->index;
1717 vector_only_wrapper_free(matchvec);
1718 *status = CMD_COMPLETE_FULL_MATCH;
1721 /* Make it sure last element is NULL. */
1722 vector_set(matchvec, NULL);
1724 /* Check LCD of matched strings. */
1725 if (vector_slot(vline, index) != NULL) {
1726 lcd = cmd_lcd((char **)matchvec->index);
1729 int len = strlen(vector_slot(vline, index));
1734 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1736 memcpy(lcdstr, matchvec->index[0], lcd);
1739 /* match_str = (char **) &lcdstr; */
1741 /* Free matchvec. */
1742 for (i = 0; i < vector_active(matchvec); i++) {
1743 if (vector_slot(matchvec, i))
1744 talloc_free(vector_slot(matchvec, i));
1746 vector_free(matchvec);
1748 /* Make new matchvec. */
1749 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1750 vector_set(matchvec, lcdstr);
1751 match_str = (char **)matchvec->index;
1752 vector_only_wrapper_free(matchvec);
1754 *status = CMD_COMPLETE_MATCH;
1760 match_str = (char **)matchvec->index;
1761 vector_only_wrapper_free(matchvec);
1762 *status = CMD_COMPLETE_LIST_MATCH;
1766 char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1770 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1771 enum node_type onode;
1772 vector shifted_vline;
1776 vty->node = ENABLE_NODE;
1777 /* We can try it on enable node, cos' the vty is authenticated */
1779 shifted_vline = vector_init(vector_count(vline));
1781 for (index = 1; index < vector_active(vline); index++) {
1782 vector_set_index(shifted_vline, index - 1,
1783 vector_lookup(vline, index));
1786 ret = cmd_complete_command_real(shifted_vline, vty, status);
1788 vector_free(shifted_vline);
1793 return cmd_complete_command_real(vline, vty, status);
1796 /* return parent node */
1797 /* MUST eventually converge on CONFIG_NODE */
1798 enum node_type vty_go_parent(struct vty *vty)
1800 assert(vty->node > CONFIG_NODE);
1802 if (host.app_info->go_parent_cb)
1803 host.app_info->go_parent_cb(vty);
1805 vty->node = CONFIG_NODE;
1810 /* Execute command by argument vline vector. */
1812 cmd_execute_command_real(vector vline, struct vty *vty,
1813 struct cmd_element **cmd)
1818 struct cmd_element *cmd_element;
1819 struct cmd_element *matched_element;
1820 unsigned int matched_count, incomplete_count;
1822 const char *argv[CMD_ARGC_MAX];
1823 enum match_type match = 0;
1827 /* Make copy of command elements. */
1828 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1830 for (index = 0; index < vector_active(vline); index++)
1831 if ((command = vector_slot(vline, index))) {
1835 cmd_filter_by_completion(command, cmd_vector,
1838 if (match == vararg_match)
1842 is_cmd_ambiguous(command, cmd_vector, index, match);
1845 vector_free(cmd_vector);
1846 return CMD_ERR_AMBIGUOUS;
1847 } else if (ret == 2) {
1848 vector_free(cmd_vector);
1849 return CMD_ERR_NO_MATCH;
1853 /* Check matched count. */
1854 matched_element = NULL;
1856 incomplete_count = 0;
1858 for (i = 0; i < vector_active(cmd_vector); i++)
1859 if ((cmd_element = vector_slot(cmd_vector, i))) {
1860 if (match == vararg_match
1861 || index >= cmd_element->cmdsize) {
1862 matched_element = cmd_element;
1864 printf("DEBUG: %s\n", cmd_element->string);
1872 /* Finish of using cmd_vector. */
1873 vector_free(cmd_vector);
1875 /* To execute command, matched_count must be 1. */
1876 if (matched_count == 0) {
1877 if (incomplete_count)
1878 return CMD_ERR_INCOMPLETE;
1880 return CMD_ERR_NO_MATCH;
1883 if (matched_count > 1)
1884 return CMD_ERR_AMBIGUOUS;
1886 /* Argument treatment */
1890 for (i = 0; i < vector_active(vline); i++) {
1892 argv[argc++] = vector_slot(vline, i);
1895 vector_slot(matched_element->strvec, i);
1897 if (vector_active(descvec) == 1) {
1898 struct desc *desc = vector_slot(descvec, 0);
1900 if (CMD_VARARG(desc->cmd))
1903 if (varflag || CMD_VARIABLE(desc->cmd)
1904 || CMD_OPTION(desc->cmd))
1905 argv[argc++] = vector_slot(vline, i);
1907 argv[argc++] = vector_slot(vline, i);
1910 if (argc >= CMD_ARGC_MAX)
1911 return CMD_ERR_EXEED_ARGC_MAX;
1914 /* For vtysh execution. */
1916 *cmd = matched_element;
1918 if (matched_element->daemon)
1919 return CMD_SUCCESS_DAEMON;
1921 /* Execute matched command. */
1922 return (*matched_element->func) (matched_element, vty, argc, argv);
1926 cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
1929 int ret, saved_ret, tried = 0;
1930 enum node_type onode;
1934 oindex = vty->index;
1936 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1937 vector shifted_vline;
1940 vty->node = ENABLE_NODE;
1941 /* We can try it on enable node, cos' the vty is authenticated */
1943 shifted_vline = vector_init(vector_count(vline));
1945 for (index = 1; index < vector_active(vline); index++) {
1946 vector_set_index(shifted_vline, index - 1,
1947 vector_lookup(vline, index));
1950 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
1952 vector_free(shifted_vline);
1957 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
1962 /* Go to parent for config nodes to attempt to find the right command */
1963 while (ret != CMD_SUCCESS && ret != CMD_WARNING
1964 && is_config(vty)) {
1966 ret = cmd_execute_command_real(vline, vty, cmd);
1968 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
1969 /* succesfull command, leave the node as is */
1973 /* no command succeeded, reset the vty to the original node and
1974 return the error for this node */
1977 vty->index = oindex;
1982 /* Execute command by argument readline. */
1984 cmd_execute_command_strict(vector vline, struct vty *vty,
1985 struct cmd_element **cmd)
1990 struct cmd_element *cmd_element;
1991 struct cmd_element *matched_element;
1992 unsigned int matched_count, incomplete_count;
1994 const char *argv[CMD_ARGC_MAX];
1996 enum match_type match = 0;
1999 /* Make copy of command element */
2000 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2002 for (index = 0; index < vector_active(vline); index++)
2003 if ((command = vector_slot(vline, index))) {
2006 match = cmd_filter_by_string(vector_slot(vline, index),
2009 /* If command meets '.VARARG' then finish matching. */
2010 if (match == vararg_match)
2014 is_cmd_ambiguous(command, cmd_vector, index, match);
2016 vector_free(cmd_vector);
2017 return CMD_ERR_AMBIGUOUS;
2020 vector_free(cmd_vector);
2021 return CMD_ERR_NO_MATCH;
2025 /* Check matched count. */
2026 matched_element = NULL;
2028 incomplete_count = 0;
2029 for (i = 0; i < vector_active(cmd_vector); i++)
2030 if (vector_slot(cmd_vector, i) != NULL) {
2031 cmd_element = vector_slot(cmd_vector, i);
2033 if (match == vararg_match
2034 || index >= cmd_element->cmdsize) {
2035 matched_element = cmd_element;
2041 /* Finish of using cmd_vector. */
2042 vector_free(cmd_vector);
2044 /* To execute command, matched_count must be 1. */
2045 if (matched_count == 0) {
2046 if (incomplete_count)
2047 return CMD_ERR_INCOMPLETE;
2049 return CMD_ERR_NO_MATCH;
2052 if (matched_count > 1)
2053 return CMD_ERR_AMBIGUOUS;
2055 /* Argument treatment */
2059 for (i = 0; i < vector_active(vline); i++) {
2061 argv[argc++] = vector_slot(vline, i);
2064 vector_slot(matched_element->strvec, i);
2066 if (vector_active(descvec) == 1) {
2067 struct desc *desc = vector_slot(descvec, 0);
2069 if (CMD_VARARG(desc->cmd))
2072 if (varflag || CMD_VARIABLE(desc->cmd)
2073 || CMD_OPTION(desc->cmd))
2074 argv[argc++] = vector_slot(vline, i);
2076 argv[argc++] = vector_slot(vline, i);
2079 if (argc >= CMD_ARGC_MAX)
2080 return CMD_ERR_EXEED_ARGC_MAX;
2083 /* For vtysh execution. */
2085 *cmd = matched_element;
2087 if (matched_element->daemon)
2088 return CMD_SUCCESS_DAEMON;
2090 /* Now execute matched command */
2091 return (*matched_element->func) (matched_element, vty, argc, argv);
2094 /* Configration make from file. */
2095 int config_from_file(struct vty *vty, FILE * fp)
2100 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2101 vline = cmd_make_strvec(vty->buf);
2103 /* In case of comment line */
2106 /* Execute configuration command : this is strict match */
2107 ret = cmd_execute_command_strict(vline, vty, NULL);
2109 /* Try again with setting node to CONFIG_NODE */
2110 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2111 && ret != CMD_ERR_NOTHING_TODO
2112 && vty->node != CONFIG_NODE && is_config(vty)) {
2114 ret = cmd_execute_command_strict(vline, vty, NULL);
2117 cmd_free_strvec(vline);
2119 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2120 && ret != CMD_ERR_NOTHING_TODO)
2126 /* Configration from terminal */
2127 DEFUN(config_terminal,
2128 config_terminal_cmd,
2129 "configure terminal",
2130 "Configuration from vty interface\n" "Configuration terminal\n")
2132 if (vty_config_lock(vty))
2133 vty->node = CONFIG_NODE;
2135 vty_out(vty, "VTY configuration is locked by other VTY%s",
2142 /* Enable command */
2143 DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2145 /* If enable password is NULL, change to ENABLE_NODE */
2146 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2147 vty->type == VTY_SHELL_SERV)
2148 vty->node = ENABLE_NODE;
2150 vty->node = AUTH_ENABLE_NODE;
2155 /* Disable command */
2157 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2159 if (vty->node == ENABLE_NODE)
2160 vty->node = VIEW_NODE;
2164 /* Down vty node level. */
2166 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2168 switch (vty->node) {
2171 if (0) //vty_shell (vty))
2174 vty->status = VTY_CLOSE;
2177 vty->node = ENABLE_NODE;
2178 vty_config_unlock(vty);
2181 vty->node = CONFIG_NODE;
2184 vty->node = CONFIG_NODE;
2192 /* End of configuration. */
2194 config_end_cmd, "end", "End current mode and change to enable mode.")
2196 switch (vty->node) {
2199 /* Nothing to do. */
2204 vty_config_unlock(vty);
2205 vty->node = ENABLE_NODE;
2215 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2217 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2218 host.app_info->version,
2219 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2220 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
2225 /* Help display function for all node. */
2227 config_help_cmd, "help", "Description of the interactive help system\n")
2230 "This VTY provides advanced help features. When you need help,%s\
2231 anytime at the command line please press '?'.%s\
2233 If nothing matches, the help list will be empty and you must backup%s\
2234 until entering a '?' shows the available options.%s\
2235 Two styles of help are provided:%s\
2236 1. Full help is available when you are ready to enter a%s\
2237 command argument (e.g. 'show ?') and describes each possible%s\
2239 2. Partial help is provided when an abbreviated argument is entered%s\
2240 and you want to know what arguments match the input%s\
2241 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2242 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2246 /* Help display function for all node. */
2247 gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2250 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2251 struct cmd_element *cmd;
2253 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2254 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2255 && !(cmd->attr == CMD_ATTR_DEPRECATED
2256 || cmd->attr == CMD_ATTR_HIDDEN))
2257 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2261 /* Write current configuration into file. */
2262 DEFUN(config_write_file,
2263 config_write_file_cmd,
2265 "Write running configuration to memory, network, or terminal\n"
2266 "Write to configuration file\n")
2270 struct cmd_node *node;
2272 char *config_file_tmp = NULL;
2273 char *config_file_sav = NULL;
2274 struct vty *file_vty;
2276 /* Check and see if we are operating under vtysh configuration */
2277 if (host.config == NULL) {
2278 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2284 config_file = host.config;
2287 _talloc_zero(tall_vty_cmd_ctx,
2288 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2290 strcpy(config_file_sav, config_file);
2291 strcat(config_file_sav, CONF_BACKUP_EXT);
2293 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2295 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2297 /* Open file to configuration write. */
2298 fd = mkstemp(config_file_tmp);
2300 vty_out(vty, "Can't open configuration file %s.%s",
2301 config_file_tmp, VTY_NEWLINE);
2302 talloc_free(config_file_tmp);
2303 talloc_free(config_file_sav);
2307 /* Make vty for configuration file. */
2308 file_vty = vty_new();
2310 file_vty->type = VTY_FILE;
2312 /* Config file header print. */
2313 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
2314 host.app_info->name, host.app_info->version);
2315 //vty_time_print (file_vty, 1);
2316 vty_out(file_vty, "!\n");
2318 for (i = 0; i < vector_active(cmdvec); i++)
2319 if ((node = vector_slot(cmdvec, i)) && node->func) {
2320 if ((*node->func) (file_vty))
2321 vty_out(file_vty, "!\n");
2323 vty_close(file_vty);
2325 if (unlink(config_file_sav) != 0)
2326 if (errno != ENOENT) {
2328 "Can't unlink backup configuration file %s.%s",
2329 config_file_sav, VTY_NEWLINE);
2330 talloc_free(config_file_sav);
2331 talloc_free(config_file_tmp);
2332 unlink(config_file_tmp);
2335 if (link(config_file, config_file_sav) != 0) {
2336 vty_out(vty, "Can't backup old configuration file %s.%s",
2337 config_file_sav, VTY_NEWLINE);
2338 talloc_free(config_file_sav);
2339 talloc_free(config_file_tmp);
2340 unlink(config_file_tmp);
2344 if (unlink(config_file) != 0) {
2345 vty_out(vty, "Can't unlink configuration file %s.%s",
2346 config_file, VTY_NEWLINE);
2347 talloc_free(config_file_sav);
2348 talloc_free(config_file_tmp);
2349 unlink(config_file_tmp);
2352 if (link(config_file_tmp, config_file) != 0) {
2353 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2355 talloc_free(config_file_sav);
2356 talloc_free(config_file_tmp);
2357 unlink(config_file_tmp);
2360 unlink(config_file_tmp);
2363 talloc_free(config_file_sav);
2364 talloc_free(config_file_tmp);
2366 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
2367 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2368 config_file, strerror(errno), errno, VTY_NEWLINE);
2372 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2376 ALIAS(config_write_file,
2378 "write", "Write running configuration to memory, network, or terminal\n")
2380 ALIAS(config_write_file,
2381 config_write_memory_cmd,
2383 "Write running configuration to memory, network, or terminal\n"
2384 "Write configuration to the file (same as write file)\n")
2386 ALIAS(config_write_file,
2387 copy_runningconfig_startupconfig_cmd,
2388 "copy running-config startup-config",
2389 "Copy configuration\n"
2390 "Copy running config to... \n"
2391 "Copy running config to startup config (same as write file)\n")
2393 /* Write current configuration into the terminal. */
2394 DEFUN(config_write_terminal,
2395 config_write_terminal_cmd,
2397 "Write running configuration to memory, network, or terminal\n"
2398 "Write to terminal\n")
2401 struct cmd_node *node;
2403 if (vty->type == VTY_SHELL_SERV) {
2404 for (i = 0; i < vector_active(cmdvec); i++)
2405 if ((node = vector_slot(cmdvec, i)) && node->func
2407 if ((*node->func) (vty))
2408 vty_out(vty, "!%s", VTY_NEWLINE);
2411 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2413 vty_out(vty, "!%s", VTY_NEWLINE);
2415 for (i = 0; i < vector_active(cmdvec); i++)
2416 if ((node = vector_slot(cmdvec, i)) && node->func) {
2417 if ((*node->func) (vty))
2418 vty_out(vty, "!%s", VTY_NEWLINE);
2420 vty_out(vty, "end%s", VTY_NEWLINE);
2425 /* Write current configuration into the terminal. */
2426 ALIAS(config_write_terminal,
2427 show_running_config_cmd,
2428 "show running-config", SHOW_STR "running configuration\n")
2430 /* Write startup configuration into the terminal. */
2431 DEFUN(show_startup_config,
2432 show_startup_config_cmd,
2433 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2438 confp = fopen(host.config, "r");
2439 if (confp == NULL) {
2440 vty_out(vty, "Can't open configuration file [%s]%s",
2441 host.config, VTY_NEWLINE);
2445 while (fgets(buf, BUFSIZ, confp)) {
2448 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2452 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2460 /* Hostname configuration */
2461 DEFUN(config_hostname,
2464 "Set system's network name\n" "This system's network name\n")
2466 if (!isalpha((int)*argv[0])) {
2467 vty_out(vty, "Please specify string starting with alphabet%s",
2473 talloc_free(host.name);
2475 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2479 DEFUN(config_no_hostname,
2481 "no hostname [HOSTNAME]",
2482 NO_STR "Reset system's network name\n" "Host name of this router\n")
2485 talloc_free(host.name);
2490 /* VTY interface password set. */
2491 DEFUN(config_password, password_cmd,
2492 "password (8|) WORD",
2493 "Assign the terminal connection password\n"
2494 "Specifies a HIDDEN password will follow\n"
2495 "dummy string \n" "The HIDDEN line password string\n")
2497 /* Argument check. */
2499 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2504 if (*argv[0] == '8') {
2506 talloc_free(host.password);
2507 host.password = NULL;
2508 if (host.password_encrypt)
2509 talloc_free(host.password_encrypt);
2510 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2513 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2518 if (!isalnum((int)*argv[0])) {
2520 "Please specify string starting with alphanumeric%s",
2526 talloc_free(host.password);
2527 host.password = NULL;
2531 if (host.password_encrypt)
2532 talloc_free(host.password_encrypt);
2533 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2536 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2541 ALIAS(config_password, password_text_cmd,
2543 "Assign the terminal connection password\n"
2544 "The UNENCRYPTED (cleartext) line password\n")
2546 /* VTY enable password set. */
2547 DEFUN(config_enable_password, enable_password_cmd,
2548 "enable password (8|) WORD",
2549 "Modify enable password parameters\n"
2550 "Assign the privileged level password\n"
2551 "Specifies a HIDDEN password will follow\n"
2552 "dummy string \n" "The HIDDEN 'enable' password string\n")
2554 /* Argument check. */
2556 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2560 /* Crypt type is specified. */
2562 if (*argv[0] == '8') {
2564 talloc_free(host.enable);
2567 if (host.enable_encrypt)
2568 talloc_free(host.enable_encrypt);
2569 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2573 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2578 if (!isalnum((int)*argv[0])) {
2580 "Please specify string starting with alphanumeric%s",
2586 talloc_free(host.enable);
2589 /* Plain password input. */
2592 if (host.enable_encrypt)
2593 talloc_free(host.enable_encrypt);
2594 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2597 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2602 ALIAS(config_enable_password,
2603 enable_password_text_cmd,
2604 "enable password LINE",
2605 "Modify enable password parameters\n"
2606 "Assign the privileged level password\n"
2607 "The UNENCRYPTED (cleartext) 'enable' password\n")
2609 /* VTY enable password delete. */
2610 DEFUN(no_config_enable_password, no_enable_password_cmd,
2611 "no enable password",
2613 "Modify enable password parameters\n"
2614 "Assign the privileged level password\n")
2617 talloc_free(host.enable);
2620 if (host.enable_encrypt)
2621 talloc_free(host.enable_encrypt);
2622 host.enable_encrypt = NULL;
2628 DEFUN(service_password_encrypt,
2629 service_password_encrypt_cmd,
2630 "service password-encryption",
2631 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2638 if (host.password) {
2639 if (host.password_encrypt)
2640 talloc_free(host.password_encrypt);
2641 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2644 if (host.enable_encrypt)
2645 talloc_free(host.enable_encrypt);
2646 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2652 DEFUN(no_service_password_encrypt,
2653 no_service_password_encrypt_cmd,
2654 "no service password-encryption",
2655 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2662 if (host.password_encrypt)
2663 talloc_free(host.password_encrypt);
2664 host.password_encrypt = NULL;
2666 if (host.enable_encrypt)
2667 talloc_free(host.enable_encrypt);
2668 host.enable_encrypt = NULL;
2674 DEFUN(config_terminal_length, config_terminal_length_cmd,
2675 "terminal length <0-512>",
2676 "Set terminal line parameters\n"
2677 "Set number of lines on a screen\n"
2678 "Number of lines on screen (0 for no pausing)\n")
2681 char *endptr = NULL;
2683 lines = strtol(argv[0], &endptr, 10);
2684 if (lines < 0 || lines > 512 || *endptr != '\0') {
2685 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2693 DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2694 "terminal no length",
2695 "Set terminal line parameters\n"
2696 NO_STR "Set number of lines on a screen\n")
2702 DEFUN(service_terminal_length, service_terminal_length_cmd,
2703 "service terminal-length <0-512>",
2704 "Set up miscellaneous service\n"
2705 "System wide terminal length configuration\n"
2706 "Number of lines of VTY (0 means no line control)\n")
2709 char *endptr = NULL;
2711 lines = strtol(argv[0], &endptr, 10);
2712 if (lines < 0 || lines > 512 || *endptr != '\0') {
2713 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2721 DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2722 "no service terminal-length [<0-512>]",
2724 "Set up miscellaneous service\n"
2725 "System wide terminal length configuration\n"
2726 "Number of lines of VTY (0 means no line control)\n")
2732 DEFUN_HIDDEN(do_echo,
2735 "Echo a message back to the vty\n" "The message to echo\n")
2739 vty_out(vty, "%s%s",
2741 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2743 talloc_free(message);
2748 DEFUN(config_logmsg,
2750 "logmsg " LOG_LEVELS " .MESSAGE",
2751 "Send a message to enabled logging destinations\n"
2752 LOG_LEVEL_DESC "The message to send\n")
2757 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2758 return CMD_ERR_NO_MATCH;
2761 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2763 talloc_free(message);
2769 "show logging", SHOW_STR "Show current logging configuration\n")
2771 struct zlog *zl = zlog_default;
2773 vty_out(vty, "Syslog logging: ");
2774 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2775 vty_out(vty, "disabled");
2777 vty_out(vty, "level %s, facility %s, ident %s",
2778 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2779 facility_name(zl->facility), zl->ident);
2780 vty_out(vty, "%s", VTY_NEWLINE);
2782 vty_out(vty, "Stdout logging: ");
2783 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2784 vty_out(vty, "disabled");
2786 vty_out(vty, "level %s",
2787 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2788 vty_out(vty, "%s", VTY_NEWLINE);
2790 vty_out(vty, "Monitor logging: ");
2791 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2792 vty_out(vty, "disabled");
2794 vty_out(vty, "level %s",
2795 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2796 vty_out(vty, "%s", VTY_NEWLINE);
2798 vty_out(vty, "File logging: ");
2799 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2800 vty_out(vty, "disabled");
2802 vty_out(vty, "level %s, filename %s",
2803 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2805 vty_out(vty, "%s", VTY_NEWLINE);
2807 vty_out(vty, "Protocol name: %s%s",
2808 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2809 vty_out(vty, "Record priority: %s%s",
2810 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2815 DEFUN(config_log_stdout,
2816 config_log_stdout_cmd,
2817 "log stdout", "Logging control\n" "Set stdout logging level\n")
2819 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2823 DEFUN(config_log_stdout_level,
2824 config_log_stdout_level_cmd,
2825 "log stdout " LOG_LEVELS,
2826 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2830 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2831 return CMD_ERR_NO_MATCH;
2832 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2836 DEFUN(no_config_log_stdout,
2837 no_config_log_stdout_cmd,
2838 "no log stdout [LEVEL]",
2839 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2841 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2845 DEFUN(config_log_monitor,
2846 config_log_monitor_cmd,
2848 "Logging control\n" "Set terminal line (monitor) logging level\n")
2850 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2854 DEFUN(config_log_monitor_level,
2855 config_log_monitor_level_cmd,
2856 "log monitor " LOG_LEVELS,
2858 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2862 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2863 return CMD_ERR_NO_MATCH;
2864 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
2868 DEFUN(no_config_log_monitor,
2869 no_config_log_monitor_cmd,
2870 "no log monitor [LEVEL]",
2873 "Disable terminal line (monitor) logging\n" "Logging level\n")
2875 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2879 static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2883 const char *fullpath;
2885 /* Path detection. */
2886 if (!IS_DIRECTORY_SEP(*fname)) {
2887 char cwd[MAXPATHLEN + 1];
2888 cwd[MAXPATHLEN] = '\0';
2890 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2891 zlog_err("config_log_file: Unable to alloc mem!");
2895 if ((p = _talloc_zero(tall_vcmd_ctx,
2896 strlen(cwd) + strlen(fname) + 2),
2899 zlog_err("config_log_file: Unable to alloc mem!");
2902 sprintf(p, "%s/%s", cwd, fname);
2907 ret = zlog_set_file(NULL, fullpath, loglevel);
2913 vty_out(vty, "can't open logfile %s\n", fname);
2918 talloc_free(host.logfile);
2920 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
2925 DEFUN(config_log_file,
2926 config_log_file_cmd,
2927 "log file FILENAME",
2928 "Logging control\n" "Logging to file\n" "Logging filename\n")
2930 return set_log_file(vty, argv[0], zlog_default->default_lvl);
2933 DEFUN(config_log_file_level,
2934 config_log_file_level_cmd,
2935 "log file FILENAME " LOG_LEVELS,
2937 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
2941 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
2942 return CMD_ERR_NO_MATCH;
2943 return set_log_file(vty, argv[0], level);
2946 DEFUN(no_config_log_file,
2947 no_config_log_file_cmd,
2948 "no log file [FILENAME]",
2950 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
2952 zlog_reset_file(NULL);
2955 talloc_free(host.logfile);
2957 host.logfile = NULL;
2962 ALIAS(no_config_log_file,
2963 no_config_log_file_level_cmd,
2964 "no log file FILENAME LEVEL",
2967 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
2969 DEFUN(config_log_syslog,
2970 config_log_syslog_cmd,
2971 "log syslog", "Logging control\n" "Set syslog logging level\n")
2973 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2977 DEFUN(config_log_syslog_level,
2978 config_log_syslog_level_cmd,
2979 "log syslog " LOG_LEVELS,
2980 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
2984 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2985 return CMD_ERR_NO_MATCH;
2986 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
2990 DEFUN_DEPRECATED(config_log_syslog_facility,
2991 config_log_syslog_facility_cmd,
2992 "log syslog facility " LOG_FACILITIES,
2994 "Logging goes to syslog\n"
2995 "(Deprecated) Facility parameter for syslog messages\n"
3000 if ((facility = facility_match(argv[0])) < 0)
3001 return CMD_ERR_NO_MATCH;
3003 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3004 zlog_default->facility = facility;
3008 DEFUN(no_config_log_syslog,
3009 no_config_log_syslog_cmd,
3010 "no log syslog [LEVEL]",
3011 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3013 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3017 ALIAS(no_config_log_syslog,
3018 no_config_log_syslog_facility_cmd,
3019 "no log syslog facility " LOG_FACILITIES,
3022 "Logging goes to syslog\n"
3023 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3025 DEFUN(config_log_facility,
3026 config_log_facility_cmd,
3027 "log facility " LOG_FACILITIES,
3029 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3033 if ((facility = facility_match(argv[0])) < 0)
3034 return CMD_ERR_NO_MATCH;
3035 zlog_default->facility = facility;
3039 DEFUN(no_config_log_facility,
3040 no_config_log_facility_cmd,
3041 "no log facility [FACILITY]",
3044 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3046 zlog_default->facility = LOG_DAEMON;
3050 DEFUN_DEPRECATED(config_log_trap,
3051 config_log_trap_cmd,
3052 "log trap " LOG_LEVELS,
3054 "(Deprecated) Set logging level and default for all destinations\n"
3060 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3061 return CMD_ERR_NO_MATCH;
3063 zlog_default->default_lvl = new_level;
3064 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3065 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3066 zlog_default->maxlvl[i] = new_level;
3070 DEFUN_DEPRECATED(no_config_log_trap,
3071 no_config_log_trap_cmd,
3072 "no log trap [LEVEL]",
3075 "Permit all logging information\n" "Logging level\n")
3077 zlog_default->default_lvl = LOG_DEBUG;
3081 DEFUN(config_log_record_priority,
3082 config_log_record_priority_cmd,
3083 "log record-priority",
3085 "Log the priority of the message within the message\n")
3087 zlog_default->record_priority = 1;
3091 DEFUN(no_config_log_record_priority,
3092 no_config_log_record_priority_cmd,
3093 "no log record-priority",
3096 "Do not log the priority of the message within the message\n")
3098 zlog_default->record_priority = 0;
3103 DEFUN(banner_motd_file,
3104 banner_motd_file_cmd,
3105 "banner motd file [FILE]",
3106 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3109 talloc_free(host.motdfile);
3110 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3115 DEFUN(banner_motd_default,
3116 banner_motd_default_cmd,
3117 "banner motd default",
3118 "Set banner string\n" "Strings for motd\n" "Default string\n")
3120 host.motd = default_motd;
3124 DEFUN(no_banner_motd,
3126 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3130 talloc_free(host.motdfile);
3131 host.motdfile = NULL;
3135 /* Set config filename. Called from vty.c */
3136 void host_config_set(const char *filename)
3138 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3141 void install_default(enum node_type node)
3143 install_element(node, &config_help_cmd);
3144 install_element(node, &config_list_cmd);
3146 install_element(node, &config_write_terminal_cmd);
3147 install_element(node, &config_write_file_cmd);
3148 install_element(node, &config_write_memory_cmd);
3149 install_element(node, &config_write_cmd);
3150 install_element(node, &show_running_config_cmd);
3153 /* Initialize command interface. Install basic nodes and commands. */
3154 void cmd_init(int terminal)
3156 /* Allocate initial top vector of commands. */
3157 cmdvec = vector_init(VECTOR_MIN_SIZE);
3159 /* Default host value settings. */
3161 host.password = NULL;
3163 host.logfile = NULL;
3166 host.motd = default_motd;
3167 host.motdfile = NULL;
3169 /* Install top nodes. */
3170 install_node(&view_node, NULL);
3171 install_node(&enable_node, NULL);
3172 install_node(&auth_node, NULL);
3173 install_node(&auth_enable_node, NULL);
3174 install_node(&config_node, config_write_host);
3176 /* Each node's basic commands. */
3177 install_element(VIEW_NODE, &show_version_cmd);
3179 install_element(VIEW_NODE, &config_list_cmd);
3180 install_element(VIEW_NODE, &config_exit_cmd);
3181 install_element(VIEW_NODE, &config_help_cmd);
3182 install_element(VIEW_NODE, &config_enable_cmd);
3183 install_element(VIEW_NODE, &config_terminal_length_cmd);
3184 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3185 install_element(VIEW_NODE, &echo_cmd);
3189 install_element(ENABLE_NODE, &config_exit_cmd);
3190 install_default(ENABLE_NODE);
3191 install_element(ENABLE_NODE, &config_disable_cmd);
3192 install_element(ENABLE_NODE, &config_terminal_cmd);
3193 install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd);
3195 install_element (ENABLE_NODE, &show_startup_config_cmd);
3196 install_element(ENABLE_NODE, &show_version_cmd);
3199 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3200 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3201 install_element(ENABLE_NODE, &echo_cmd);
3203 install_default(CONFIG_NODE);
3204 install_element(CONFIG_NODE, &config_exit_cmd);
3207 install_element(CONFIG_NODE, &hostname_cmd);
3208 install_element(CONFIG_NODE, &no_hostname_cmd);
3211 install_element(CONFIG_NODE, &password_cmd);
3212 install_element(CONFIG_NODE, &password_text_cmd);
3213 install_element(CONFIG_NODE, &enable_password_cmd);
3214 install_element(CONFIG_NODE, &enable_password_text_cmd);
3215 install_element(CONFIG_NODE, &no_enable_password_cmd);
3218 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3219 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3221 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3222 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3223 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3224 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3225 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);