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 <osmocore/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 /* Sort each node's command element according to command string. */
146 struct cmd_node *cnode;
148 struct cmd_element *cmd_element;
150 for (i = 0; i < vector_active(cmdvec); i++)
151 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
152 vector cmd_vector = cnode->cmd_vector;
153 qsort(cmd_vector->index, vector_active(cmd_vector),
154 sizeof(void *), cmp_node);
156 for (j = 0; j < vector_active(cmd_vector); j++)
158 vector_slot(cmd_vector, j)) != NULL
159 && vector_active(cmd_element->strvec)) {
161 vector_slot(cmd_element->strvec,
163 (cmd_element->strvec) -
165 qsort(descvec->index,
166 vector_active(descvec),
167 sizeof(void *), cmp_desc);
172 /* Breaking up string into each command piece. I assume given
173 character is separated by a space character. Return value is a
174 vector which includes char ** data element. */
175 vector cmd_make_strvec(const char *string)
177 const char *cp, *start;
187 /* Skip white spaces. */
188 while (isspace((int)*cp) && *cp != '\0')
191 /* Return if there is only white spaces */
195 if (*cp == '!' || *cp == '#')
198 /* Prepare return vector. */
199 strvec = vector_init(VECTOR_MIN_SIZE);
201 /* Copy each command piece and set into vector. */
204 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
208 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
209 memcpy(token, start, strlen);
210 *(token + strlen) = '\0';
211 vector_set(strvec, token);
213 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
222 /* Free allocated string vector. */
223 void cmd_free_strvec(vector v)
231 for (i = 0; i < vector_active(v); i++)
232 if ((cp = vector_slot(v, i)) != NULL)
238 /* Fetch next description. Used in cmd_make_descvec(). */
239 static char *cmd_desc_str(const char **string)
241 const char *cp, *start;
250 /* Skip white spaces. */
251 while (isspace((int)*cp) && *cp != '\0')
254 /* Return if there is only white spaces */
260 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
264 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
265 memcpy(token, start, strlen);
266 *(token + strlen) = '\0';
273 /* New string vector. */
274 static vector cmd_make_descvec(const char *string, const char *descstr)
283 vector strvec = NULL;
292 allvec = vector_init(VECTOR_MIN_SIZE);
295 while (isspace((int)*cp) && *cp != '\0')
308 fprintf(stderr, "Command parse error!: %s\n",
315 while (isspace((int)*cp) && *cp != '\0')
329 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
330 || *cp == ')' || *cp == '|') && *cp != '\0')
335 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
336 memcpy(token, sp, len);
337 *(token + len) = '\0';
339 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
341 desc->str = cmd_desc_str(&dp);
345 strvec = vector_init(VECTOR_MIN_SIZE);
346 vector_set(allvec, strvec);
350 strvec = vector_init(VECTOR_MIN_SIZE);
351 vector_set(allvec, strvec);
353 vector_set(strvec, desc);
357 /* Count mandantory string vector size. This is to determine inputed
358 command has enough command length. */
359 static int cmd_cmdsize(vector strvec)
366 for (i = 0; i < vector_active(strvec); i++)
367 if ((descvec = vector_slot(strvec, i)) != NULL) {
368 if ((vector_active(descvec)) == 1
369 && (desc = vector_slot(descvec, 0)) != NULL) {
370 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
380 /* Return prompt character of specified node. */
381 const char *cmd_prompt(enum node_type node)
383 struct cmd_node *cnode;
385 cnode = vector_slot(cmdvec, node);
386 return cnode->prompt;
389 /* Install a command into a node. */
390 void install_element(enum node_type ntype, struct cmd_element *cmd)
392 struct cmd_node *cnode;
394 cnode = vector_slot(cmdvec, ntype);
398 "Command node %d doesn't exist, please check it\n",
403 vector_set(cnode->cmd_vector, cmd);
405 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
406 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
409 /* Install a command into VIEW and ENABLE node */
410 void install_element_ve(struct cmd_element *cmd)
412 install_element(VIEW_NODE, cmd);
413 install_element(ENABLE_NODE, cmd);
417 static unsigned char itoa64[] =
418 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
420 static void to64(char *s, long v, int n)
423 *s++ = itoa64[v & 0x3f];
428 static char *zencrypt(const char *passwd)
432 char *crypt(const char *, const char *);
434 gettimeofday(&tv, 0);
436 to64(&salt[0], random(), 3);
437 to64(&salt[3], tv.tv_usec, 3);
440 return crypt(passwd, salt);
444 /* This function write configuration of this host. */
445 static int config_write_host(struct vty *vty)
448 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
451 if (host.password_encrypt)
452 vty_out(vty, "password 8 %s%s", host.password_encrypt,
454 if (host.enable_encrypt)
455 vty_out(vty, "enable password 8 %s%s",
456 host.enable_encrypt, VTY_NEWLINE);
459 vty_out(vty, "password %s%s", host.password,
462 vty_out(vty, "enable password %s%s", host.enable,
467 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
470 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
473 vty_out(vty, "service terminal-length %d%s", host.lines,
477 vty_out(vty, "banner motd file %s%s", host.motdfile,
480 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
485 /* Utility function for getting command vector. */
486 static vector cmd_node_vector(vector v, enum node_type ntype)
488 struct cmd_node *cnode = vector_slot(v, ntype);
489 return cnode->cmd_vector;
492 /* Completion match types. */
506 static enum match_type cmd_ipv4_match(const char *str)
509 int dots = 0, nums = 0;
516 memset(buf, 0, sizeof(buf));
518 while (*str != '\0') {
523 if (*(str + 1) == '.')
526 if (*(str + 1) == '\0')
532 if (!isdigit((int)*str))
541 strncpy(buf, sp, str - sp);
559 static enum match_type cmd_ipv4_prefix_match(const char *str)
569 memset(buf, 0, sizeof(buf));
571 while (*str != '\0' && *str != '/') {
576 if (*(str + 1) == '.' || *(str + 1) == '/')
579 if (*(str + 1) == '\0')
586 if (!isdigit((int)*str))
595 strncpy(buf, sp, str - sp);
601 if (*(str + 1) == '\0')
606 } else if (*str == '\0')
617 while (*str != '\0') {
618 if (!isdigit((int)*str))
630 #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
631 #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
632 #define STATE_START 1
633 #define STATE_COLON 2
634 #define STATE_DOUBLE 3
637 #define STATE_SLASH 6
642 static enum match_type cmd_ipv6_match(const char *str)
644 int state = STATE_START;
645 int colons = 0, nums = 0, double_colon = 0;
646 const char *sp = NULL;
647 struct sockaddr_in6 sin6_dummy;
653 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
656 /* use inet_pton that has a better support,
657 * for example inet_pton can support the automatic addresses:
660 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
665 while (*str != '\0') {
669 if (*(str + 1) != ':' && *(str + 1) != '\0')
681 if (*(str + 1) == ':')
682 state = STATE_DOUBLE;
692 if (*(str + 1) == ':')
695 if (*(str + 1) != '\0')
705 if (*(str + 1) == ':' || *(str + 1) == '\0') {
712 if (*(str + 1) == '.')
739 static enum match_type cmd_ipv6_prefix_match(const char *str)
741 int state = STATE_START;
742 int colons = 0, nums = 0, double_colon = 0;
744 const char *sp = NULL;
750 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
753 while (*str != '\0' && state != STATE_MASK) {
757 if (*(str + 1) != ':' && *(str + 1) != '\0')
769 if (*(str + 1) == '/')
771 else if (*(str + 1) == ':')
772 state = STATE_DOUBLE;
782 if (*(str + 1) == ':')
785 if (*(str + 1) != '\0' && *(str + 1) != '/')
789 if (*(str + 1) == '/')
799 if (*(str + 1) == ':' || *(str + 1) == '.'
800 || *(str + 1) == '\0' || *(str + 1) == '/') {
804 for (; sp <= str; sp++)
810 if (*(str + 1) == ':')
812 else if (*(str + 1) == '.')
814 else if (*(str + 1) == '/')
822 if (*(str + 1) == '\0')
840 if (state < STATE_MASK)
843 mask = strtol(str, &endptr, 10);
847 if (mask < 0 || mask > 128)
850 /* I don't know why mask < 13 makes command match partly.
851 Forgive me to make this comments. I Want to set static default route
852 because of lack of function to originate default in ospf6d; sorry
861 #endif /* HAVE_IPV6 */
863 #define DECIMAL_STRLEN_MAX 10
865 static int cmd_range_match(const char *range, const char *str)
868 char buf[DECIMAL_STRLEN_MAX + 1];
870 unsigned long min, max, val;
875 val = strtoul(str, &endptr, 10);
880 p = strchr(range, '-');
883 if (p - range > DECIMAL_STRLEN_MAX)
885 strncpy(buf, range, p - range);
886 buf[p - range] = '\0';
887 min = strtoul(buf, &endptr, 10);
892 p = strchr(range, '>');
895 if (p - range > DECIMAL_STRLEN_MAX)
897 strncpy(buf, range, p - range);
898 buf[p - range] = '\0';
899 max = strtoul(buf, &endptr, 10);
903 if (val < min || val > max)
909 /* Make completion match and return match type flag. */
910 static enum match_type
911 cmd_filter_by_completion(char *command, vector v, unsigned int index)
915 struct cmd_element *cmd_element;
916 enum match_type match_type;
920 match_type = no_match;
922 /* If command and cmd_element string does not match set NULL to vector */
923 for (i = 0; i < vector_active(v); i++)
924 if ((cmd_element = vector_slot(v, i)) != NULL) {
925 if (index >= vector_active(cmd_element->strvec))
926 vector_slot(v, i) = NULL;
932 vector_slot(cmd_element->strvec, index);
934 for (j = 0; j < vector_active(descvec); j++)
935 if ((desc = vector_slot(descvec, j))) {
938 if (CMD_VARARG(str)) {
944 } else if (CMD_RANGE(str)) {
957 else if (CMD_IPV6(str)) {
968 } else if (CMD_IPV6_PREFIX(str)) {
969 if (cmd_ipv6_prefix_match(command)) {
979 #endif /* HAVE_IPV6 */
980 else if (CMD_IPV4(str)) {
991 } else if (CMD_IPV4_PREFIX(str)) {
992 if (cmd_ipv4_prefix_match(command)) {
1001 /* Check is this point's argument optional ? */
1004 CMD_VARIABLE(str)) {
1015 if (strcmp(command, str)
1030 vector_slot(v, i) = NULL;
1036 /* Filter vector by command character with index. */
1037 static enum match_type
1038 cmd_filter_by_string(char *command, vector v, unsigned int index)
1042 struct cmd_element *cmd_element;
1043 enum match_type match_type;
1047 match_type = no_match;
1049 /* If command and cmd_element string does not match set NULL to vector */
1050 for (i = 0; i < vector_active(v); i++)
1051 if ((cmd_element = vector_slot(v, i)) != NULL) {
1052 /* If given index is bigger than max string vector of command,
1054 if (index >= vector_active(cmd_element->strvec))
1055 vector_slot(v, i) = NULL;
1061 vector_slot(cmd_element->strvec, index);
1063 for (j = 0; j < vector_active(descvec); j++)
1064 if ((desc = vector_slot(descvec, j))) {
1067 if (CMD_VARARG(str)) {
1073 } else if (CMD_RANGE(str)) {
1085 else if (CMD_IPV6(str)) {
1096 } else if (CMD_IPV6_PREFIX(str)) {
1097 if (cmd_ipv6_prefix_match(command) == exact_match) {
1106 #endif /* HAVE_IPV6 */
1107 else if (CMD_IPV4(str)) {
1118 } else if (CMD_IPV4_PREFIX(str)) {
1119 if (cmd_ipv4_prefix_match(command) == exact_match) {
1127 } else if (CMD_OPTION(str)
1128 || CMD_VARIABLE(str))
1136 if (strcmp(command, str)
1145 vector_slot(v, i) = NULL;
1151 /* Check ambiguous match */
1153 is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1157 const char *str = NULL;
1158 struct cmd_element *cmd_element;
1159 const char *matched = NULL;
1163 for (i = 0; i < vector_active(v); i++)
1164 if ((cmd_element = vector_slot(v, i)) != NULL) {
1167 descvec = vector_slot(cmd_element->strvec, index);
1169 for (j = 0; j < vector_active(descvec); j++)
1170 if ((desc = vector_slot(descvec, j))) {
1171 enum match_type ret;
1179 || CMD_VARIABLE(str))
1180 && strcmp(command, str) == 0)
1186 || CMD_VARIABLE(str))
1187 && strncmp(command, str, strlen(command)) == 0) {
1191 return 1; /* There is ambiguous match. */
1214 case ipv6_prefix_match:
1216 cmd_ipv6_prefix_match
1217 (command)) != no_match) {
1218 if (ret == partly_match)
1219 return 2; /* There is incomplete match. */
1224 #endif /* HAVE_IPV6 */
1229 case ipv4_prefix_match:
1231 cmd_ipv4_prefix_match
1232 (command)) != no_match) {
1233 if (ret == partly_match)
1234 return 2; /* There is incomplete match. */
1241 || CMD_VARIABLE(str))
1250 vector_slot(v, i) = NULL;
1255 /* If src matches dst return dst string, otherwise return NULL */
1256 static const char *cmd_entry_function(const char *src, const char *dst)
1258 /* Skip variable arguments. */
1259 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1260 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1263 /* In case of 'command \t', given src is NULL string. */
1267 /* Matched with input string. */
1268 if (strncmp(src, dst, strlen(src)) == 0)
1274 /* If src matches dst return dst string, otherwise return NULL */
1275 /* This version will return the dst string always if it is
1276 CMD_VARIABLE for '?' key processing */
1277 static const char *cmd_entry_function_desc(const char *src, const char *dst)
1279 if (CMD_VARARG(dst))
1282 if (CMD_RANGE(dst)) {
1283 if (cmd_range_match(dst, src))
1289 if (CMD_IPV6(dst)) {
1290 if (cmd_ipv6_match(src))
1296 if (CMD_IPV6_PREFIX(dst)) {
1297 if (cmd_ipv6_prefix_match(src))
1302 #endif /* HAVE_IPV6 */
1304 if (CMD_IPV4(dst)) {
1305 if (cmd_ipv4_match(src))
1311 if (CMD_IPV4_PREFIX(dst)) {
1312 if (cmd_ipv4_prefix_match(src))
1318 /* Optional or variable commands always match on '?' */
1319 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1322 /* In case of 'command \t', given src is NULL string. */
1326 if (strncmp(src, dst, strlen(src)) == 0)
1332 /* Check same string element existence. If it isn't there return
1334 static int cmd_unique_string(vector v, const char *str)
1339 for (i = 0; i < vector_active(v); i++)
1340 if ((match = vector_slot(v, i)) != NULL)
1341 if (strcmp(match, str) == 0)
1346 /* Compare string to description vector. If there is same string
1347 return 1 else return 0. */
1348 static int desc_unique_string(vector v, const char *str)
1353 for (i = 0; i < vector_active(v); i++)
1354 if ((desc = vector_slot(v, i)) != NULL)
1355 if (strcmp(desc->cmd, str) == 0)
1360 static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1362 if (first_word != NULL &&
1363 node != AUTH_NODE &&
1364 node != VIEW_NODE &&
1365 node != AUTH_ENABLE_NODE &&
1366 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1371 /* '?' describe command support. */
1373 cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1377 #define INIT_MATCHVEC_SIZE 10
1379 struct cmd_element *cmd_element;
1382 enum match_type match;
1384 static struct desc desc_cr = { "<cr>", "" };
1387 if (vector_active(vline) == 0) {
1388 *status = CMD_ERR_NO_MATCH;
1391 index = vector_active(vline) - 1;
1393 /* Make copy vector of current node's command vector. */
1394 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1396 /* Prepare match vector */
1397 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1399 /* Filter commands. */
1400 /* Only words precedes current word will be checked in this loop. */
1401 for (i = 0; i < index; i++)
1402 if ((command = vector_slot(vline, i))) {
1404 cmd_filter_by_completion(command, cmd_vector, i);
1406 if (match == vararg_match) {
1407 struct cmd_element *cmd_element;
1411 for (j = 0; j < vector_active(cmd_vector); j++)
1413 vector_slot(cmd_vector, j)) != NULL
1416 (cmd_element->strvec))) {
1418 vector_slot(cmd_element->
1424 k < vector_active(descvec);
1427 vector_slot(descvec,
1429 vector_set(matchvec,
1434 vector_set(matchvec, &desc_cr);
1435 vector_free(cmd_vector);
1441 is_cmd_ambiguous(command, cmd_vector, i,
1443 vector_free(cmd_vector);
1444 *status = CMD_ERR_AMBIGUOUS;
1446 } else if (ret == 2) {
1447 vector_free(cmd_vector);
1448 *status = CMD_ERR_NO_MATCH;
1453 /* Prepare match vector */
1454 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1456 /* Make sure that cmd_vector is filtered based on current word */
1457 command = vector_slot(vline, index);
1459 match = cmd_filter_by_completion(command, cmd_vector, index);
1461 /* Make description vector. */
1462 for (i = 0; i < vector_active(cmd_vector); i++)
1463 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1464 const char *string = NULL;
1465 vector strvec = cmd_element->strvec;
1467 /* if command is NULL, index may be equal to vector_active */
1468 if (command && index >= vector_active(strvec))
1469 vector_slot(cmd_vector, i) = NULL;
1471 /* Check if command is completed. */
1473 && index == vector_active(strvec)) {
1475 if (!desc_unique_string
1477 vector_set(matchvec, &desc_cr);
1481 vector_slot(strvec, index);
1484 for (j = 0; j < vector_active(descvec);
1487 vector_slot(descvec, j))) {
1489 cmd_entry_function_desc
1493 /* Uniqueness check */
1494 if (!desc_unique_string(matchvec, string))
1503 vector_free(cmd_vector);
1505 if (vector_slot(matchvec, 0) == NULL) {
1506 vector_free(matchvec);
1507 *status = CMD_ERR_NO_MATCH;
1509 *status = CMD_SUCCESS;
1514 vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1518 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1519 enum node_type onode;
1520 vector shifted_vline;
1524 vty->node = ENABLE_NODE;
1525 /* We can try it on enable node, cos' the vty is authenticated */
1527 shifted_vline = vector_init(vector_count(vline));
1529 for (index = 1; index < vector_active(vline); index++) {
1530 vector_set_index(shifted_vline, index - 1,
1531 vector_lookup(vline, index));
1534 ret = cmd_describe_command_real(shifted_vline, vty, status);
1536 vector_free(shifted_vline);
1541 return cmd_describe_command_real(vline, vty, status);
1544 /* Check LCD of matched command. */
1545 static int cmd_lcd(char **matched)
1553 if (matched[0] == NULL || matched[1] == NULL)
1556 for (i = 1; matched[i] != NULL; i++) {
1557 s1 = matched[i - 1];
1560 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1574 /* Command line completion support. */
1575 static char **cmd_complete_command_real(vector vline, struct vty *vty,
1579 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1580 #define INIT_MATCHVEC_SIZE 10
1582 struct cmd_element *cmd_element;
1590 if (vector_active(vline) == 0) {
1591 *status = CMD_ERR_NO_MATCH;
1594 index = vector_active(vline) - 1;
1596 /* First, filter by preceeding command string */
1597 for (i = 0; i < index; i++)
1598 if ((command = vector_slot(vline, i))) {
1599 enum match_type match;
1602 /* First try completion match, if there is exactly match return 1 */
1604 cmd_filter_by_completion(command, cmd_vector, i);
1606 /* If there is exact match then filter ambiguous match else check
1609 is_cmd_ambiguous(command, cmd_vector, i,
1611 vector_free(cmd_vector);
1612 *status = CMD_ERR_AMBIGUOUS;
1618 vector_free (cmd_vector);
1619 *status = CMD_ERR_NO_MATCH;
1625 /* Prepare match vector. */
1626 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1628 /* Now we got into completion */
1629 for (i = 0; i < vector_active(cmd_vector); i++)
1630 if ((cmd_element = vector_slot(cmd_vector, i))) {
1632 vector strvec = cmd_element->strvec;
1634 /* Check field length */
1635 if (index >= vector_active(strvec))
1636 vector_slot(cmd_vector, i) = NULL;
1640 descvec = vector_slot(strvec, index);
1641 for (j = 0; j < vector_active(descvec); j++)
1642 if ((desc = vector_slot(descvec, j))) {
1643 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1644 if (cmd_unique_string (matchvec, string))
1645 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1650 /* We don't need cmd_vector any more. */
1651 vector_free(cmd_vector);
1653 /* No matched command */
1654 if (vector_slot(matchvec, 0) == NULL) {
1655 vector_free(matchvec);
1657 /* In case of 'command \t' pattern. Do you need '?' command at
1658 the end of the line. */
1659 if (vector_slot(vline, index) == '\0')
1660 *status = CMD_ERR_NOTHING_TODO;
1662 *status = CMD_ERR_NO_MATCH;
1666 /* Only one matched */
1667 if (vector_slot(matchvec, 1) == NULL) {
1668 match_str = (char **)matchvec->index;
1669 vector_only_wrapper_free(matchvec);
1670 *status = CMD_COMPLETE_FULL_MATCH;
1673 /* Make it sure last element is NULL. */
1674 vector_set(matchvec, NULL);
1676 /* Check LCD of matched strings. */
1677 if (vector_slot(vline, index) != NULL) {
1678 lcd = cmd_lcd((char **)matchvec->index);
1681 int len = strlen(vector_slot(vline, index));
1686 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1688 memcpy(lcdstr, matchvec->index[0], lcd);
1691 /* match_str = (char **) &lcdstr; */
1693 /* Free matchvec. */
1694 for (i = 0; i < vector_active(matchvec); i++) {
1695 if (vector_slot(matchvec, i))
1696 talloc_free(vector_slot(matchvec, i));
1698 vector_free(matchvec);
1700 /* Make new matchvec. */
1701 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1702 vector_set(matchvec, lcdstr);
1703 match_str = (char **)matchvec->index;
1704 vector_only_wrapper_free(matchvec);
1706 *status = CMD_COMPLETE_MATCH;
1712 match_str = (char **)matchvec->index;
1713 vector_only_wrapper_free(matchvec);
1714 *status = CMD_COMPLETE_LIST_MATCH;
1718 char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1722 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1723 enum node_type onode;
1724 vector shifted_vline;
1728 vty->node = ENABLE_NODE;
1729 /* We can try it on enable node, cos' the vty is authenticated */
1731 shifted_vline = vector_init(vector_count(vline));
1733 for (index = 1; index < vector_active(vline); index++) {
1734 vector_set_index(shifted_vline, index - 1,
1735 vector_lookup(vline, index));
1738 ret = cmd_complete_command_real(shifted_vline, vty, status);
1740 vector_free(shifted_vline);
1745 return cmd_complete_command_real(vline, vty, status);
1748 /* return parent node */
1749 /* MUST eventually converge on CONFIG_NODE */
1750 enum node_type vty_go_parent(struct vty *vty)
1752 assert(vty->node > CONFIG_NODE);
1754 if (host.app_info->go_parent_cb)
1755 host.app_info->go_parent_cb(vty);
1757 vty->node = CONFIG_NODE;
1762 /* Execute command by argument vline vector. */
1764 cmd_execute_command_real(vector vline, struct vty *vty,
1765 struct cmd_element **cmd)
1770 struct cmd_element *cmd_element;
1771 struct cmd_element *matched_element;
1772 unsigned int matched_count, incomplete_count;
1774 const char *argv[CMD_ARGC_MAX];
1775 enum match_type match = 0;
1779 /* Make copy of command elements. */
1780 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1782 for (index = 0; index < vector_active(vline); index++)
1783 if ((command = vector_slot(vline, index))) {
1787 cmd_filter_by_completion(command, cmd_vector,
1790 if (match == vararg_match)
1794 is_cmd_ambiguous(command, cmd_vector, index, match);
1797 vector_free(cmd_vector);
1798 return CMD_ERR_AMBIGUOUS;
1799 } else if (ret == 2) {
1800 vector_free(cmd_vector);
1801 return CMD_ERR_NO_MATCH;
1805 /* Check matched count. */
1806 matched_element = NULL;
1808 incomplete_count = 0;
1810 for (i = 0; i < vector_active(cmd_vector); i++)
1811 if ((cmd_element = vector_slot(cmd_vector, i))) {
1812 if (match == vararg_match
1813 || index >= cmd_element->cmdsize) {
1814 matched_element = cmd_element;
1816 printf("DEBUG: %s\n", cmd_element->string);
1824 /* Finish of using cmd_vector. */
1825 vector_free(cmd_vector);
1827 /* To execute command, matched_count must be 1. */
1828 if (matched_count == 0) {
1829 if (incomplete_count)
1830 return CMD_ERR_INCOMPLETE;
1832 return CMD_ERR_NO_MATCH;
1835 if (matched_count > 1)
1836 return CMD_ERR_AMBIGUOUS;
1838 /* Argument treatment */
1842 for (i = 0; i < vector_active(vline); i++) {
1844 argv[argc++] = vector_slot(vline, i);
1847 vector_slot(matched_element->strvec, i);
1849 if (vector_active(descvec) == 1) {
1850 struct desc *desc = vector_slot(descvec, 0);
1852 if (CMD_VARARG(desc->cmd))
1855 if (varflag || CMD_VARIABLE(desc->cmd)
1856 || CMD_OPTION(desc->cmd))
1857 argv[argc++] = vector_slot(vline, i);
1859 argv[argc++] = vector_slot(vline, i);
1862 if (argc >= CMD_ARGC_MAX)
1863 return CMD_ERR_EXEED_ARGC_MAX;
1866 /* For vtysh execution. */
1868 *cmd = matched_element;
1870 if (matched_element->daemon)
1871 return CMD_SUCCESS_DAEMON;
1873 /* Execute matched command. */
1874 return (*matched_element->func) (matched_element, vty, argc, argv);
1878 cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
1881 int ret, saved_ret, tried = 0;
1882 enum node_type onode;
1886 oindex = vty->index;
1888 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1889 vector shifted_vline;
1892 vty->node = ENABLE_NODE;
1893 /* We can try it on enable node, cos' the vty is authenticated */
1895 shifted_vline = vector_init(vector_count(vline));
1897 for (index = 1; index < vector_active(vline); index++) {
1898 vector_set_index(shifted_vline, index - 1,
1899 vector_lookup(vline, index));
1902 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
1904 vector_free(shifted_vline);
1909 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
1914 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1915 while (ret != CMD_SUCCESS && ret != CMD_WARNING
1916 && vty->node > CONFIG_NODE) {
1918 ret = cmd_execute_command_real(vline, vty, cmd);
1920 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
1921 /* succesfull command, leave the node as is */
1925 /* no command succeeded, reset the vty to the original node and
1926 return the error for this node */
1929 vty->index = oindex;
1934 /* Execute command by argument readline. */
1936 cmd_execute_command_strict(vector vline, struct vty *vty,
1937 struct cmd_element **cmd)
1942 struct cmd_element *cmd_element;
1943 struct cmd_element *matched_element;
1944 unsigned int matched_count, incomplete_count;
1946 const char *argv[CMD_ARGC_MAX];
1948 enum match_type match = 0;
1951 /* Make copy of command element */
1952 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1954 for (index = 0; index < vector_active(vline); index++)
1955 if ((command = vector_slot(vline, index))) {
1958 match = cmd_filter_by_string(vector_slot(vline, index),
1961 /* If command meets '.VARARG' then finish matching. */
1962 if (match == vararg_match)
1966 is_cmd_ambiguous(command, cmd_vector, index, match);
1968 vector_free(cmd_vector);
1969 return CMD_ERR_AMBIGUOUS;
1972 vector_free(cmd_vector);
1973 return CMD_ERR_NO_MATCH;
1977 /* Check matched count. */
1978 matched_element = NULL;
1980 incomplete_count = 0;
1981 for (i = 0; i < vector_active(cmd_vector); i++)
1982 if (vector_slot(cmd_vector, i) != NULL) {
1983 cmd_element = vector_slot(cmd_vector, i);
1985 if (match == vararg_match
1986 || index >= cmd_element->cmdsize) {
1987 matched_element = cmd_element;
1993 /* Finish of using cmd_vector. */
1994 vector_free(cmd_vector);
1996 /* To execute command, matched_count must be 1. */
1997 if (matched_count == 0) {
1998 if (incomplete_count)
1999 return CMD_ERR_INCOMPLETE;
2001 return CMD_ERR_NO_MATCH;
2004 if (matched_count > 1)
2005 return CMD_ERR_AMBIGUOUS;
2007 /* Argument treatment */
2011 for (i = 0; i < vector_active(vline); i++) {
2013 argv[argc++] = vector_slot(vline, i);
2016 vector_slot(matched_element->strvec, i);
2018 if (vector_active(descvec) == 1) {
2019 struct desc *desc = vector_slot(descvec, 0);
2021 if (CMD_VARARG(desc->cmd))
2024 if (varflag || CMD_VARIABLE(desc->cmd)
2025 || CMD_OPTION(desc->cmd))
2026 argv[argc++] = vector_slot(vline, i);
2028 argv[argc++] = vector_slot(vline, i);
2031 if (argc >= CMD_ARGC_MAX)
2032 return CMD_ERR_EXEED_ARGC_MAX;
2035 /* For vtysh execution. */
2037 *cmd = matched_element;
2039 if (matched_element->daemon)
2040 return CMD_SUCCESS_DAEMON;
2042 /* Now execute matched command */
2043 return (*matched_element->func) (matched_element, vty, argc, argv);
2046 /* Configration make from file. */
2047 int config_from_file(struct vty *vty, FILE * fp)
2052 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2053 vline = cmd_make_strvec(vty->buf);
2055 /* In case of comment line */
2058 /* Execute configuration command : this is strict match */
2059 ret = cmd_execute_command_strict(vline, vty, NULL);
2061 /* Try again with setting node to CONFIG_NODE */
2062 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2063 && ret != CMD_ERR_NOTHING_TODO
2064 && vty->node != CONFIG_NODE) {
2066 ret = cmd_execute_command_strict(vline, vty, NULL);
2069 cmd_free_strvec(vline);
2071 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2072 && ret != CMD_ERR_NOTHING_TODO)
2078 /* Configration from terminal */
2079 DEFUN(config_terminal,
2080 config_terminal_cmd,
2081 "configure terminal",
2082 "Configuration from vty interface\n" "Configuration terminal\n")
2084 if (vty_config_lock(vty))
2085 vty->node = CONFIG_NODE;
2087 vty_out(vty, "VTY configuration is locked by other VTY%s",
2094 /* Enable command */
2095 DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2097 /* If enable password is NULL, change to ENABLE_NODE */
2098 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2099 vty->type == VTY_SHELL_SERV)
2100 vty->node = ENABLE_NODE;
2102 vty->node = AUTH_ENABLE_NODE;
2107 /* Disable command */
2109 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2111 if (vty->node == ENABLE_NODE)
2112 vty->node = VIEW_NODE;
2116 /* Down vty node level. */
2118 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2120 switch (vty->node) {
2123 if (0) //vty_shell (vty))
2126 vty->status = VTY_CLOSE;
2129 vty->node = ENABLE_NODE;
2130 vty_config_unlock(vty);
2133 vty->node = CONFIG_NODE;
2141 /* End of configuration. */
2143 config_end_cmd, "end", "End current mode and change to enable mode.")
2145 switch (vty->node) {
2148 /* Nothing to do. */
2152 vty_config_unlock(vty);
2153 vty->node = ENABLE_NODE;
2163 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2165 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2166 host.app_info->version,
2167 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2168 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
2173 /* Help display function for all node. */
2175 config_help_cmd, "help", "Description of the interactive help system\n")
2178 "This VTY provides advanced help features. When you need help,%s\
2179 anytime at the command line please press '?'.%s\
2181 If nothing matches, the help list will be empty and you must backup%s\
2182 until entering a '?' shows the available options.%s\
2183 Two styles of help are provided:%s\
2184 1. Full help is available when you are ready to enter a%s\
2185 command argument (e.g. 'show ?') and describes each possible%s\
2187 2. Partial help is provided when an abbreviated argument is entered%s\
2188 and you want to know what arguments match the input%s\
2189 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2190 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2194 /* Help display function for all node. */
2195 gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2198 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2199 struct cmd_element *cmd;
2201 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2202 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2203 && !(cmd->attr == CMD_ATTR_DEPRECATED
2204 || cmd->attr == CMD_ATTR_HIDDEN))
2205 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2209 /* Write current configuration into file. */
2210 DEFUN(config_write_file,
2211 config_write_file_cmd,
2213 "Write running configuration to memory, network, or terminal\n"
2214 "Write to configuration file\n")
2218 struct cmd_node *node;
2220 char *config_file_tmp = NULL;
2221 char *config_file_sav = NULL;
2222 struct vty *file_vty;
2224 /* Check and see if we are operating under vtysh configuration */
2225 if (host.config == NULL) {
2226 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2232 config_file = host.config;
2235 _talloc_zero(tall_vty_cmd_ctx,
2236 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2238 strcpy(config_file_sav, config_file);
2239 strcat(config_file_sav, CONF_BACKUP_EXT);
2241 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2243 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2245 /* Open file to configuration write. */
2246 fd = mkstemp(config_file_tmp);
2248 vty_out(vty, "Can't open configuration file %s.%s",
2249 config_file_tmp, VTY_NEWLINE);
2250 talloc_free(config_file_tmp);
2251 talloc_free(config_file_sav);
2255 /* Make vty for configuration file. */
2256 file_vty = vty_new();
2258 file_vty->type = VTY_FILE;
2260 /* Config file header print. */
2261 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
2262 host.app_info->name, host.app_info->version);
2263 //vty_time_print (file_vty, 1);
2264 vty_out(file_vty, "!\n");
2266 for (i = 0; i < vector_active(cmdvec); i++)
2267 if ((node = vector_slot(cmdvec, i)) && node->func) {
2268 if ((*node->func) (file_vty))
2269 vty_out(file_vty, "!\n");
2271 vty_close(file_vty);
2273 if (unlink(config_file_sav) != 0)
2274 if (errno != ENOENT) {
2276 "Can't unlink backup configuration file %s.%s",
2277 config_file_sav, VTY_NEWLINE);
2278 talloc_free(config_file_sav);
2279 talloc_free(config_file_tmp);
2280 unlink(config_file_tmp);
2283 if (link(config_file, config_file_sav) != 0) {
2284 vty_out(vty, "Can't backup old configuration file %s.%s",
2285 config_file_sav, VTY_NEWLINE);
2286 talloc_free(config_file_sav);
2287 talloc_free(config_file_tmp);
2288 unlink(config_file_tmp);
2292 if (unlink(config_file) != 0) {
2293 vty_out(vty, "Can't unlink configuration file %s.%s",
2294 config_file, VTY_NEWLINE);
2295 talloc_free(config_file_sav);
2296 talloc_free(config_file_tmp);
2297 unlink(config_file_tmp);
2300 if (link(config_file_tmp, config_file) != 0) {
2301 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2303 talloc_free(config_file_sav);
2304 talloc_free(config_file_tmp);
2305 unlink(config_file_tmp);
2308 unlink(config_file_tmp);
2311 talloc_free(config_file_sav);
2312 talloc_free(config_file_tmp);
2314 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
2315 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2316 config_file, strerror(errno), errno, VTY_NEWLINE);
2320 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2324 ALIAS(config_write_file,
2326 "write", "Write running configuration to memory, network, or terminal\n")
2328 ALIAS(config_write_file,
2329 config_write_memory_cmd,
2331 "Write running configuration to memory, network, or terminal\n"
2332 "Write configuration to the file (same as write file)\n")
2334 ALIAS(config_write_file,
2335 copy_runningconfig_startupconfig_cmd,
2336 "copy running-config startup-config",
2337 "Copy configuration\n"
2338 "Copy running config to... \n"
2339 "Copy running config to startup config (same as write file)\n")
2341 /* Write current configuration into the terminal. */
2342 DEFUN(config_write_terminal,
2343 config_write_terminal_cmd,
2345 "Write running configuration to memory, network, or terminal\n"
2346 "Write to terminal\n")
2349 struct cmd_node *node;
2351 if (vty->type == VTY_SHELL_SERV) {
2352 for (i = 0; i < vector_active(cmdvec); i++)
2353 if ((node = vector_slot(cmdvec, i)) && node->func
2355 if ((*node->func) (vty))
2356 vty_out(vty, "!%s", VTY_NEWLINE);
2359 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2361 vty_out(vty, "!%s", VTY_NEWLINE);
2363 for (i = 0; i < vector_active(cmdvec); i++)
2364 if ((node = vector_slot(cmdvec, i)) && node->func) {
2365 if ((*node->func) (vty))
2366 vty_out(vty, "!%s", VTY_NEWLINE);
2368 vty_out(vty, "end%s", VTY_NEWLINE);
2373 /* Write current configuration into the terminal. */
2374 ALIAS(config_write_terminal,
2375 show_running_config_cmd,
2376 "show running-config", SHOW_STR "running configuration\n")
2378 /* Write startup configuration into the terminal. */
2379 DEFUN(show_startup_config,
2380 show_startup_config_cmd,
2381 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2386 confp = fopen(host.config, "r");
2387 if (confp == NULL) {
2388 vty_out(vty, "Can't open configuration file [%s]%s",
2389 host.config, VTY_NEWLINE);
2393 while (fgets(buf, BUFSIZ, confp)) {
2396 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2400 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2408 /* Hostname configuration */
2409 DEFUN(config_hostname,
2412 "Set system's network name\n" "This system's network name\n")
2414 if (!isalpha((int)*argv[0])) {
2415 vty_out(vty, "Please specify string starting with alphabet%s",
2421 talloc_free(host.name);
2423 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2427 DEFUN(config_no_hostname,
2429 "no hostname [HOSTNAME]",
2430 NO_STR "Reset system's network name\n" "Host name of this router\n")
2433 talloc_free(host.name);
2438 /* VTY interface password set. */
2439 DEFUN(config_password, password_cmd,
2440 "password (8|) WORD",
2441 "Assign the terminal connection password\n"
2442 "Specifies a HIDDEN password will follow\n"
2443 "dummy string \n" "The HIDDEN line password string\n")
2445 /* Argument check. */
2447 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2452 if (*argv[0] == '8') {
2454 talloc_free(host.password);
2455 host.password = NULL;
2456 if (host.password_encrypt)
2457 talloc_free(host.password_encrypt);
2458 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2461 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2466 if (!isalnum((int)*argv[0])) {
2468 "Please specify string starting with alphanumeric%s",
2474 talloc_free(host.password);
2475 host.password = NULL;
2479 if (host.password_encrypt)
2480 talloc_free(host.password_encrypt);
2481 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2484 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2489 ALIAS(config_password, password_text_cmd,
2491 "Assign the terminal connection password\n"
2492 "The UNENCRYPTED (cleartext) line password\n")
2494 /* VTY enable password set. */
2495 DEFUN(config_enable_password, enable_password_cmd,
2496 "enable password (8|) WORD",
2497 "Modify enable password parameters\n"
2498 "Assign the privileged level password\n"
2499 "Specifies a HIDDEN password will follow\n"
2500 "dummy string \n" "The HIDDEN 'enable' password string\n")
2502 /* Argument check. */
2504 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2508 /* Crypt type is specified. */
2510 if (*argv[0] == '8') {
2512 talloc_free(host.enable);
2515 if (host.enable_encrypt)
2516 talloc_free(host.enable_encrypt);
2517 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2521 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2526 if (!isalnum((int)*argv[0])) {
2528 "Please specify string starting with alphanumeric%s",
2534 talloc_free(host.enable);
2537 /* Plain password input. */
2540 if (host.enable_encrypt)
2541 talloc_free(host.enable_encrypt);
2542 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2545 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2550 ALIAS(config_enable_password,
2551 enable_password_text_cmd,
2552 "enable password LINE",
2553 "Modify enable password parameters\n"
2554 "Assign the privileged level password\n"
2555 "The UNENCRYPTED (cleartext) 'enable' password\n")
2557 /* VTY enable password delete. */
2558 DEFUN(no_config_enable_password, no_enable_password_cmd,
2559 "no enable password",
2561 "Modify enable password parameters\n"
2562 "Assign the privileged level password\n")
2565 talloc_free(host.enable);
2568 if (host.enable_encrypt)
2569 talloc_free(host.enable_encrypt);
2570 host.enable_encrypt = NULL;
2576 DEFUN(service_password_encrypt,
2577 service_password_encrypt_cmd,
2578 "service password-encryption",
2579 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2586 if (host.password) {
2587 if (host.password_encrypt)
2588 talloc_free(host.password_encrypt);
2589 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2592 if (host.enable_encrypt)
2593 talloc_free(host.enable_encrypt);
2594 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2600 DEFUN(no_service_password_encrypt,
2601 no_service_password_encrypt_cmd,
2602 "no service password-encryption",
2603 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2610 if (host.password_encrypt)
2611 talloc_free(host.password_encrypt);
2612 host.password_encrypt = NULL;
2614 if (host.enable_encrypt)
2615 talloc_free(host.enable_encrypt);
2616 host.enable_encrypt = NULL;
2622 DEFUN(config_terminal_length, config_terminal_length_cmd,
2623 "terminal length <0-512>",
2624 "Set terminal line parameters\n"
2625 "Set number of lines on a screen\n"
2626 "Number of lines on screen (0 for no pausing)\n")
2629 char *endptr = NULL;
2631 lines = strtol(argv[0], &endptr, 10);
2632 if (lines < 0 || lines > 512 || *endptr != '\0') {
2633 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2641 DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2642 "terminal no length",
2643 "Set terminal line parameters\n"
2644 NO_STR "Set number of lines on a screen\n")
2650 DEFUN(service_terminal_length, service_terminal_length_cmd,
2651 "service terminal-length <0-512>",
2652 "Set up miscellaneous service\n"
2653 "System wide terminal length configuration\n"
2654 "Number of lines of VTY (0 means no line control)\n")
2657 char *endptr = NULL;
2659 lines = strtol(argv[0], &endptr, 10);
2660 if (lines < 0 || lines > 512 || *endptr != '\0') {
2661 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2669 DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2670 "no service terminal-length [<0-512>]",
2672 "Set up miscellaneous service\n"
2673 "System wide terminal length configuration\n"
2674 "Number of lines of VTY (0 means no line control)\n")
2680 DEFUN_HIDDEN(do_echo,
2683 "Echo a message back to the vty\n" "The message to echo\n")
2687 vty_out(vty, "%s%s",
2689 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2691 talloc_free(message);
2696 DEFUN(config_logmsg,
2698 "logmsg " LOG_LEVELS " .MESSAGE",
2699 "Send a message to enabled logging destinations\n"
2700 LOG_LEVEL_DESC "The message to send\n")
2705 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2706 return CMD_ERR_NO_MATCH;
2709 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2711 talloc_free(message);
2717 "show logging", SHOW_STR "Show current logging configuration\n")
2719 struct zlog *zl = zlog_default;
2721 vty_out(vty, "Syslog logging: ");
2722 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2723 vty_out(vty, "disabled");
2725 vty_out(vty, "level %s, facility %s, ident %s",
2726 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2727 facility_name(zl->facility), zl->ident);
2728 vty_out(vty, "%s", VTY_NEWLINE);
2730 vty_out(vty, "Stdout logging: ");
2731 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2732 vty_out(vty, "disabled");
2734 vty_out(vty, "level %s",
2735 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2736 vty_out(vty, "%s", VTY_NEWLINE);
2738 vty_out(vty, "Monitor logging: ");
2739 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2740 vty_out(vty, "disabled");
2742 vty_out(vty, "level %s",
2743 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2744 vty_out(vty, "%s", VTY_NEWLINE);
2746 vty_out(vty, "File logging: ");
2747 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2748 vty_out(vty, "disabled");
2750 vty_out(vty, "level %s, filename %s",
2751 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2753 vty_out(vty, "%s", VTY_NEWLINE);
2755 vty_out(vty, "Protocol name: %s%s",
2756 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2757 vty_out(vty, "Record priority: %s%s",
2758 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2763 DEFUN(config_log_stdout,
2764 config_log_stdout_cmd,
2765 "log stdout", "Logging control\n" "Set stdout logging level\n")
2767 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2771 DEFUN(config_log_stdout_level,
2772 config_log_stdout_level_cmd,
2773 "log stdout " LOG_LEVELS,
2774 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2778 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2779 return CMD_ERR_NO_MATCH;
2780 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2784 DEFUN(no_config_log_stdout,
2785 no_config_log_stdout_cmd,
2786 "no log stdout [LEVEL]",
2787 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2789 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2793 DEFUN(config_log_monitor,
2794 config_log_monitor_cmd,
2796 "Logging control\n" "Set terminal line (monitor) logging level\n")
2798 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2802 DEFUN(config_log_monitor_level,
2803 config_log_monitor_level_cmd,
2804 "log monitor " LOG_LEVELS,
2806 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2810 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2811 return CMD_ERR_NO_MATCH;
2812 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
2816 DEFUN(no_config_log_monitor,
2817 no_config_log_monitor_cmd,
2818 "no log monitor [LEVEL]",
2821 "Disable terminal line (monitor) logging\n" "Logging level\n")
2823 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2827 static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2831 const char *fullpath;
2833 /* Path detection. */
2834 if (!IS_DIRECTORY_SEP(*fname)) {
2835 char cwd[MAXPATHLEN + 1];
2836 cwd[MAXPATHLEN] = '\0';
2838 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2839 zlog_err("config_log_file: Unable to alloc mem!");
2843 if ((p = _talloc_zero(tall_vcmd_ctx,
2844 strlen(cwd) + strlen(fname) + 2),
2847 zlog_err("config_log_file: Unable to alloc mem!");
2850 sprintf(p, "%s/%s", cwd, fname);
2855 ret = zlog_set_file(NULL, fullpath, loglevel);
2861 vty_out(vty, "can't open logfile %s\n", fname);
2866 talloc_free(host.logfile);
2868 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
2873 DEFUN(config_log_file,
2874 config_log_file_cmd,
2875 "log file FILENAME",
2876 "Logging control\n" "Logging to file\n" "Logging filename\n")
2878 return set_log_file(vty, argv[0], zlog_default->default_lvl);
2881 DEFUN(config_log_file_level,
2882 config_log_file_level_cmd,
2883 "log file FILENAME " LOG_LEVELS,
2885 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
2889 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
2890 return CMD_ERR_NO_MATCH;
2891 return set_log_file(vty, argv[0], level);
2894 DEFUN(no_config_log_file,
2895 no_config_log_file_cmd,
2896 "no log file [FILENAME]",
2898 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
2900 zlog_reset_file(NULL);
2903 talloc_free(host.logfile);
2905 host.logfile = NULL;
2910 ALIAS(no_config_log_file,
2911 no_config_log_file_level_cmd,
2912 "no log file FILENAME LEVEL",
2915 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
2917 DEFUN(config_log_syslog,
2918 config_log_syslog_cmd,
2919 "log syslog", "Logging control\n" "Set syslog logging level\n")
2921 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2925 DEFUN(config_log_syslog_level,
2926 config_log_syslog_level_cmd,
2927 "log syslog " LOG_LEVELS,
2928 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
2932 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2933 return CMD_ERR_NO_MATCH;
2934 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
2938 DEFUN_DEPRECATED(config_log_syslog_facility,
2939 config_log_syslog_facility_cmd,
2940 "log syslog facility " LOG_FACILITIES,
2942 "Logging goes to syslog\n"
2943 "(Deprecated) Facility parameter for syslog messages\n"
2948 if ((facility = facility_match(argv[0])) < 0)
2949 return CMD_ERR_NO_MATCH;
2951 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2952 zlog_default->facility = facility;
2956 DEFUN(no_config_log_syslog,
2957 no_config_log_syslog_cmd,
2958 "no log syslog [LEVEL]",
2959 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
2961 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
2965 ALIAS(no_config_log_syslog,
2966 no_config_log_syslog_facility_cmd,
2967 "no log syslog facility " LOG_FACILITIES,
2970 "Logging goes to syslog\n"
2971 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
2973 DEFUN(config_log_facility,
2974 config_log_facility_cmd,
2975 "log facility " LOG_FACILITIES,
2977 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
2981 if ((facility = facility_match(argv[0])) < 0)
2982 return CMD_ERR_NO_MATCH;
2983 zlog_default->facility = facility;
2987 DEFUN(no_config_log_facility,
2988 no_config_log_facility_cmd,
2989 "no log facility [FACILITY]",
2992 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
2994 zlog_default->facility = LOG_DAEMON;
2998 DEFUN_DEPRECATED(config_log_trap,
2999 config_log_trap_cmd,
3000 "log trap " LOG_LEVELS,
3002 "(Deprecated) Set logging level and default for all destinations\n"
3008 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3009 return CMD_ERR_NO_MATCH;
3011 zlog_default->default_lvl = new_level;
3012 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3013 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3014 zlog_default->maxlvl[i] = new_level;
3018 DEFUN_DEPRECATED(no_config_log_trap,
3019 no_config_log_trap_cmd,
3020 "no log trap [LEVEL]",
3023 "Permit all logging information\n" "Logging level\n")
3025 zlog_default->default_lvl = LOG_DEBUG;
3029 DEFUN(config_log_record_priority,
3030 config_log_record_priority_cmd,
3031 "log record-priority",
3033 "Log the priority of the message within the message\n")
3035 zlog_default->record_priority = 1;
3039 DEFUN(no_config_log_record_priority,
3040 no_config_log_record_priority_cmd,
3041 "no log record-priority",
3044 "Do not log the priority of the message within the message\n")
3046 zlog_default->record_priority = 0;
3051 DEFUN(banner_motd_file,
3052 banner_motd_file_cmd,
3053 "banner motd file [FILE]",
3054 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3057 talloc_free(host.motdfile);
3058 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3063 DEFUN(banner_motd_default,
3064 banner_motd_default_cmd,
3065 "banner motd default",
3066 "Set banner string\n" "Strings for motd\n" "Default string\n")
3068 host.motd = default_motd;
3072 DEFUN(no_banner_motd,
3074 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3078 talloc_free(host.motdfile);
3079 host.motdfile = NULL;
3083 /* Set config filename. Called from vty.c */
3084 void host_config_set(const char *filename)
3086 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3089 void install_default(enum node_type node)
3091 install_element(node, &config_help_cmd);
3092 install_element(node, &config_list_cmd);
3094 install_element(node, &config_write_terminal_cmd);
3095 install_element(node, &config_write_file_cmd);
3096 install_element(node, &config_write_memory_cmd);
3097 install_element(node, &config_write_cmd);
3098 install_element(node, &show_running_config_cmd);
3101 /* Initialize command interface. Install basic nodes and commands. */
3102 void cmd_init(int terminal)
3104 /* Allocate initial top vector of commands. */
3105 cmdvec = vector_init(VECTOR_MIN_SIZE);
3107 /* Default host value settings. */
3109 host.password = NULL;
3111 host.logfile = NULL;
3114 host.motd = default_motd;
3115 host.motdfile = NULL;
3117 /* Install top nodes. */
3118 install_node(&view_node, NULL);
3119 install_node(&enable_node, NULL);
3120 install_node(&auth_node, NULL);
3121 install_node(&auth_enable_node, NULL);
3122 install_node(&config_node, config_write_host);
3124 /* Each node's basic commands. */
3125 install_element(VIEW_NODE, &show_version_cmd);
3127 install_element(VIEW_NODE, &config_list_cmd);
3128 install_element(VIEW_NODE, &config_exit_cmd);
3129 install_element(VIEW_NODE, &config_help_cmd);
3130 install_element(VIEW_NODE, &config_enable_cmd);
3131 install_element(VIEW_NODE, &config_terminal_length_cmd);
3132 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3133 install_element(VIEW_NODE, &echo_cmd);
3137 install_element(ENABLE_NODE, &config_exit_cmd);
3138 install_default(ENABLE_NODE);
3139 install_element(ENABLE_NODE, &config_disable_cmd);
3140 install_element(ENABLE_NODE, &config_terminal_cmd);
3141 install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd);
3143 install_element (ENABLE_NODE, &show_startup_config_cmd);
3144 install_element(ENABLE_NODE, &show_version_cmd);
3147 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3148 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3149 install_element(ENABLE_NODE, &echo_cmd);
3151 install_default(CONFIG_NODE);
3152 install_element(CONFIG_NODE, &config_exit_cmd);
3155 install_element(CONFIG_NODE, &hostname_cmd);
3156 install_element(CONFIG_NODE, &no_hostname_cmd);
3159 install_element(CONFIG_NODE, &password_cmd);
3160 install_element(CONFIG_NODE, &password_text_cmd);
3161 install_element(CONFIG_NODE, &enable_password_cmd);
3162 install_element(CONFIG_NODE, &enable_password_text_cmd);
3163 install_element(CONFIG_NODE, &no_enable_password_cmd);
3166 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3167 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3169 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3170 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3171 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3172 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3173 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);