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];
874 if (range[1] == '-') {
875 signed long min = 0, max = 0, val;
877 val = strtol(str, &endptr, 10);
882 p = strchr(range, '-');
885 if (p - range > DECIMAL_STRLEN_MAX)
887 strncpy(buf, range, p - range);
888 buf[p - range] = '\0';
889 min = -strtol(buf, &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 max = strtol(buf, &endptr, 10);
905 if (val < min || val > max)
908 unsigned long min, max, val;
910 val = strtoul(str, &endptr, 10);
915 p = strchr(range, '-');
918 if (p - range > DECIMAL_STRLEN_MAX)
920 strncpy(buf, range, p - range);
921 buf[p - range] = '\0';
922 min = strtoul(buf, &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 max = strtoul(buf, &endptr, 10);
938 if (val < min || val > max)
945 /* Make completion match and return match type flag. */
946 static enum match_type
947 cmd_filter_by_completion(char *command, vector v, unsigned int index)
951 struct cmd_element *cmd_element;
952 enum match_type match_type;
956 match_type = no_match;
958 /* If command and cmd_element string does not match set NULL to vector */
959 for (i = 0; i < vector_active(v); i++)
960 if ((cmd_element = vector_slot(v, i)) != NULL) {
961 if (index >= vector_active(cmd_element->strvec))
962 vector_slot(v, i) = NULL;
968 vector_slot(cmd_element->strvec, index);
970 for (j = 0; j < vector_active(descvec); j++)
971 if ((desc = vector_slot(descvec, j))) {
974 if (CMD_VARARG(str)) {
980 } else if (CMD_RANGE(str)) {
993 else if (CMD_IPV6(str)) {
1004 } else if (CMD_IPV6_PREFIX(str)) {
1005 if (cmd_ipv6_prefix_match(command)) {
1015 #endif /* HAVE_IPV6 */
1016 else if (CMD_IPV4(str)) {
1027 } else if (CMD_IPV4_PREFIX(str)) {
1028 if (cmd_ipv4_prefix_match(command)) {
1037 /* Check is this point's argument optional ? */
1040 CMD_VARIABLE(str)) {
1051 if (strcmp(command, str)
1066 vector_slot(v, i) = NULL;
1072 /* Filter vector by command character with index. */
1073 static enum match_type
1074 cmd_filter_by_string(char *command, vector v, unsigned int index)
1078 struct cmd_element *cmd_element;
1079 enum match_type match_type;
1083 match_type = no_match;
1085 /* If command and cmd_element string does not match set NULL to vector */
1086 for (i = 0; i < vector_active(v); i++)
1087 if ((cmd_element = vector_slot(v, i)) != NULL) {
1088 /* If given index is bigger than max string vector of command,
1090 if (index >= vector_active(cmd_element->strvec))
1091 vector_slot(v, i) = NULL;
1097 vector_slot(cmd_element->strvec, index);
1099 for (j = 0; j < vector_active(descvec); j++)
1100 if ((desc = vector_slot(descvec, j))) {
1103 if (CMD_VARARG(str)) {
1109 } else if (CMD_RANGE(str)) {
1121 else if (CMD_IPV6(str)) {
1132 } else if (CMD_IPV6_PREFIX(str)) {
1133 if (cmd_ipv6_prefix_match(command) == exact_match) {
1142 #endif /* HAVE_IPV6 */
1143 else if (CMD_IPV4(str)) {
1154 } else if (CMD_IPV4_PREFIX(str)) {
1155 if (cmd_ipv4_prefix_match(command) == exact_match) {
1163 } else if (CMD_OPTION(str)
1164 || CMD_VARIABLE(str))
1172 if (strcmp(command, str)
1181 vector_slot(v, i) = NULL;
1187 /* Check ambiguous match */
1189 is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1193 const char *str = NULL;
1194 struct cmd_element *cmd_element;
1195 const char *matched = NULL;
1199 for (i = 0; i < vector_active(v); i++)
1200 if ((cmd_element = vector_slot(v, i)) != NULL) {
1203 descvec = vector_slot(cmd_element->strvec, index);
1205 for (j = 0; j < vector_active(descvec); j++)
1206 if ((desc = vector_slot(descvec, j))) {
1207 enum match_type ret;
1215 || CMD_VARIABLE(str))
1216 && strcmp(command, str) == 0)
1222 || CMD_VARIABLE(str))
1223 && strncmp(command, str, strlen(command)) == 0) {
1227 return 1; /* There is ambiguous match. */
1250 case ipv6_prefix_match:
1252 cmd_ipv6_prefix_match
1253 (command)) != no_match) {
1254 if (ret == partly_match)
1255 return 2; /* There is incomplete match. */
1260 #endif /* HAVE_IPV6 */
1265 case ipv4_prefix_match:
1267 cmd_ipv4_prefix_match
1268 (command)) != no_match) {
1269 if (ret == partly_match)
1270 return 2; /* There is incomplete match. */
1277 || CMD_VARIABLE(str))
1286 vector_slot(v, i) = NULL;
1291 /* If src matches dst return dst string, otherwise return NULL */
1292 static const char *cmd_entry_function(const char *src, const char *dst)
1294 /* Skip variable arguments. */
1295 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1296 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1299 /* In case of 'command \t', given src is NULL string. */
1303 /* Matched with input string. */
1304 if (strncmp(src, dst, strlen(src)) == 0)
1310 /* If src matches dst return dst string, otherwise return NULL */
1311 /* This version will return the dst string always if it is
1312 CMD_VARIABLE for '?' key processing */
1313 static const char *cmd_entry_function_desc(const char *src, const char *dst)
1315 if (CMD_VARARG(dst))
1318 if (CMD_RANGE(dst)) {
1319 if (cmd_range_match(dst, src))
1325 if (CMD_IPV6(dst)) {
1326 if (cmd_ipv6_match(src))
1332 if (CMD_IPV6_PREFIX(dst)) {
1333 if (cmd_ipv6_prefix_match(src))
1338 #endif /* HAVE_IPV6 */
1340 if (CMD_IPV4(dst)) {
1341 if (cmd_ipv4_match(src))
1347 if (CMD_IPV4_PREFIX(dst)) {
1348 if (cmd_ipv4_prefix_match(src))
1354 /* Optional or variable commands always match on '?' */
1355 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1358 /* In case of 'command \t', given src is NULL string. */
1362 if (strncmp(src, dst, strlen(src)) == 0)
1368 /* Check same string element existence. If it isn't there return
1370 static int cmd_unique_string(vector v, const char *str)
1375 for (i = 0; i < vector_active(v); i++)
1376 if ((match = vector_slot(v, i)) != NULL)
1377 if (strcmp(match, str) == 0)
1382 /* Compare string to description vector. If there is same string
1383 return 1 else return 0. */
1384 static int desc_unique_string(vector v, const char *str)
1389 for (i = 0; i < vector_active(v); i++)
1390 if ((desc = vector_slot(v, i)) != NULL)
1391 if (strcmp(desc->cmd, str) == 0)
1396 static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1398 if (first_word != NULL &&
1399 node != AUTH_NODE &&
1400 node != VIEW_NODE &&
1401 node != AUTH_ENABLE_NODE &&
1402 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1407 /* '?' describe command support. */
1409 cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1413 #define INIT_MATCHVEC_SIZE 10
1415 struct cmd_element *cmd_element;
1418 enum match_type match;
1420 static struct desc desc_cr = { "<cr>", "" };
1423 if (vector_active(vline) == 0) {
1424 *status = CMD_ERR_NO_MATCH;
1427 index = vector_active(vline) - 1;
1429 /* Make copy vector of current node's command vector. */
1430 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1432 /* Prepare match vector */
1433 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1435 /* Filter commands. */
1436 /* Only words precedes current word will be checked in this loop. */
1437 for (i = 0; i < index; i++)
1438 if ((command = vector_slot(vline, i))) {
1440 cmd_filter_by_completion(command, cmd_vector, i);
1442 if (match == vararg_match) {
1443 struct cmd_element *cmd_element;
1447 for (j = 0; j < vector_active(cmd_vector); j++)
1449 vector_slot(cmd_vector, j)) != NULL
1452 (cmd_element->strvec))) {
1454 vector_slot(cmd_element->
1460 k < vector_active(descvec);
1463 vector_slot(descvec,
1465 vector_set(matchvec,
1470 vector_set(matchvec, &desc_cr);
1471 vector_free(cmd_vector);
1477 is_cmd_ambiguous(command, cmd_vector, i,
1479 vector_free(cmd_vector);
1480 *status = CMD_ERR_AMBIGUOUS;
1482 } else if (ret == 2) {
1483 vector_free(cmd_vector);
1484 *status = CMD_ERR_NO_MATCH;
1489 /* Prepare match vector */
1490 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1492 /* Make sure that cmd_vector is filtered based on current word */
1493 command = vector_slot(vline, index);
1495 match = cmd_filter_by_completion(command, cmd_vector, index);
1497 /* Make description vector. */
1498 for (i = 0; i < vector_active(cmd_vector); i++)
1499 if ((cmd_element = vector_slot(cmd_vector, i)) != NULL) {
1500 const char *string = NULL;
1501 vector strvec = cmd_element->strvec;
1503 /* if command is NULL, index may be equal to vector_active */
1504 if (command && index >= vector_active(strvec))
1505 vector_slot(cmd_vector, i) = NULL;
1507 /* Check if command is completed. */
1509 && index == vector_active(strvec)) {
1511 if (!desc_unique_string
1513 vector_set(matchvec, &desc_cr);
1517 vector_slot(strvec, index);
1520 for (j = 0; j < vector_active(descvec);
1523 vector_slot(descvec, j))) {
1525 cmd_entry_function_desc
1529 /* Uniqueness check */
1530 if (!desc_unique_string(matchvec, string))
1539 vector_free(cmd_vector);
1541 if (vector_slot(matchvec, 0) == NULL) {
1542 vector_free(matchvec);
1543 *status = CMD_ERR_NO_MATCH;
1545 *status = CMD_SUCCESS;
1550 vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1554 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1555 enum node_type onode;
1556 vector shifted_vline;
1560 vty->node = ENABLE_NODE;
1561 /* We can try it on enable node, cos' the vty is authenticated */
1563 shifted_vline = vector_init(vector_count(vline));
1565 for (index = 1; index < vector_active(vline); index++) {
1566 vector_set_index(shifted_vline, index - 1,
1567 vector_lookup(vline, index));
1570 ret = cmd_describe_command_real(shifted_vline, vty, status);
1572 vector_free(shifted_vline);
1577 return cmd_describe_command_real(vline, vty, status);
1580 /* Check LCD of matched command. */
1581 static int cmd_lcd(char **matched)
1589 if (matched[0] == NULL || matched[1] == NULL)
1592 for (i = 1; matched[i] != NULL; i++) {
1593 s1 = matched[i - 1];
1596 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1610 /* Command line completion support. */
1611 static char **cmd_complete_command_real(vector vline, struct vty *vty,
1615 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1616 #define INIT_MATCHVEC_SIZE 10
1618 struct cmd_element *cmd_element;
1626 if (vector_active(vline) == 0) {
1627 *status = CMD_ERR_NO_MATCH;
1630 index = vector_active(vline) - 1;
1632 /* First, filter by preceeding command string */
1633 for (i = 0; i < index; i++)
1634 if ((command = vector_slot(vline, i))) {
1635 enum match_type match;
1638 /* First try completion match, if there is exactly match return 1 */
1640 cmd_filter_by_completion(command, cmd_vector, i);
1642 /* If there is exact match then filter ambiguous match else check
1645 is_cmd_ambiguous(command, cmd_vector, i,
1647 vector_free(cmd_vector);
1648 *status = CMD_ERR_AMBIGUOUS;
1654 vector_free (cmd_vector);
1655 *status = CMD_ERR_NO_MATCH;
1661 /* Prepare match vector. */
1662 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1664 /* Now we got into completion */
1665 for (i = 0; i < vector_active(cmd_vector); i++)
1666 if ((cmd_element = vector_slot(cmd_vector, i))) {
1668 vector strvec = cmd_element->strvec;
1670 /* Check field length */
1671 if (index >= vector_active(strvec))
1672 vector_slot(cmd_vector, i) = NULL;
1676 descvec = vector_slot(strvec, index);
1677 for (j = 0; j < vector_active(descvec); j++)
1678 if ((desc = vector_slot(descvec, j))) {
1679 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1680 if (cmd_unique_string (matchvec, string))
1681 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1686 /* We don't need cmd_vector any more. */
1687 vector_free(cmd_vector);
1689 /* No matched command */
1690 if (vector_slot(matchvec, 0) == NULL) {
1691 vector_free(matchvec);
1693 /* In case of 'command \t' pattern. Do you need '?' command at
1694 the end of the line. */
1695 if (vector_slot(vline, index) == '\0')
1696 *status = CMD_ERR_NOTHING_TODO;
1698 *status = CMD_ERR_NO_MATCH;
1702 /* Only one matched */
1703 if (vector_slot(matchvec, 1) == NULL) {
1704 match_str = (char **)matchvec->index;
1705 vector_only_wrapper_free(matchvec);
1706 *status = CMD_COMPLETE_FULL_MATCH;
1709 /* Make it sure last element is NULL. */
1710 vector_set(matchvec, NULL);
1712 /* Check LCD of matched strings. */
1713 if (vector_slot(vline, index) != NULL) {
1714 lcd = cmd_lcd((char **)matchvec->index);
1717 int len = strlen(vector_slot(vline, index));
1722 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
1724 memcpy(lcdstr, matchvec->index[0], lcd);
1727 /* match_str = (char **) &lcdstr; */
1729 /* Free matchvec. */
1730 for (i = 0; i < vector_active(matchvec); i++) {
1731 if (vector_slot(matchvec, i))
1732 talloc_free(vector_slot(matchvec, i));
1734 vector_free(matchvec);
1736 /* Make new matchvec. */
1737 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1738 vector_set(matchvec, lcdstr);
1739 match_str = (char **)matchvec->index;
1740 vector_only_wrapper_free(matchvec);
1742 *status = CMD_COMPLETE_MATCH;
1748 match_str = (char **)matchvec->index;
1749 vector_only_wrapper_free(matchvec);
1750 *status = CMD_COMPLETE_LIST_MATCH;
1754 char **cmd_complete_command(vector vline, struct vty *vty, int *status)
1758 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1759 enum node_type onode;
1760 vector shifted_vline;
1764 vty->node = ENABLE_NODE;
1765 /* We can try it on enable node, cos' the vty is authenticated */
1767 shifted_vline = vector_init(vector_count(vline));
1769 for (index = 1; index < vector_active(vline); index++) {
1770 vector_set_index(shifted_vline, index - 1,
1771 vector_lookup(vline, index));
1774 ret = cmd_complete_command_real(shifted_vline, vty, status);
1776 vector_free(shifted_vline);
1781 return cmd_complete_command_real(vline, vty, status);
1784 /* return parent node */
1785 /* MUST eventually converge on CONFIG_NODE */
1786 enum node_type vty_go_parent(struct vty *vty)
1788 assert(vty->node > CONFIG_NODE);
1790 if (host.app_info->go_parent_cb)
1791 host.app_info->go_parent_cb(vty);
1793 vty->node = CONFIG_NODE;
1798 /* Execute command by argument vline vector. */
1800 cmd_execute_command_real(vector vline, struct vty *vty,
1801 struct cmd_element **cmd)
1806 struct cmd_element *cmd_element;
1807 struct cmd_element *matched_element;
1808 unsigned int matched_count, incomplete_count;
1810 const char *argv[CMD_ARGC_MAX];
1811 enum match_type match = 0;
1815 /* Make copy of command elements. */
1816 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1818 for (index = 0; index < vector_active(vline); index++)
1819 if ((command = vector_slot(vline, index))) {
1823 cmd_filter_by_completion(command, cmd_vector,
1826 if (match == vararg_match)
1830 is_cmd_ambiguous(command, cmd_vector, index, match);
1833 vector_free(cmd_vector);
1834 return CMD_ERR_AMBIGUOUS;
1835 } else if (ret == 2) {
1836 vector_free(cmd_vector);
1837 return CMD_ERR_NO_MATCH;
1841 /* Check matched count. */
1842 matched_element = NULL;
1844 incomplete_count = 0;
1846 for (i = 0; i < vector_active(cmd_vector); i++)
1847 if ((cmd_element = vector_slot(cmd_vector, i))) {
1848 if (match == vararg_match
1849 || index >= cmd_element->cmdsize) {
1850 matched_element = cmd_element;
1852 printf("DEBUG: %s\n", cmd_element->string);
1860 /* Finish of using cmd_vector. */
1861 vector_free(cmd_vector);
1863 /* To execute command, matched_count must be 1. */
1864 if (matched_count == 0) {
1865 if (incomplete_count)
1866 return CMD_ERR_INCOMPLETE;
1868 return CMD_ERR_NO_MATCH;
1871 if (matched_count > 1)
1872 return CMD_ERR_AMBIGUOUS;
1874 /* Argument treatment */
1878 for (i = 0; i < vector_active(vline); i++) {
1880 argv[argc++] = vector_slot(vline, i);
1883 vector_slot(matched_element->strvec, i);
1885 if (vector_active(descvec) == 1) {
1886 struct desc *desc = vector_slot(descvec, 0);
1888 if (CMD_VARARG(desc->cmd))
1891 if (varflag || CMD_VARIABLE(desc->cmd)
1892 || CMD_OPTION(desc->cmd))
1893 argv[argc++] = vector_slot(vline, i);
1895 argv[argc++] = vector_slot(vline, i);
1898 if (argc >= CMD_ARGC_MAX)
1899 return CMD_ERR_EXEED_ARGC_MAX;
1902 /* For vtysh execution. */
1904 *cmd = matched_element;
1906 if (matched_element->daemon)
1907 return CMD_SUCCESS_DAEMON;
1909 /* Execute matched command. */
1910 return (*matched_element->func) (matched_element, vty, argc, argv);
1914 cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
1917 int ret, saved_ret, tried = 0;
1918 enum node_type onode;
1922 oindex = vty->index;
1924 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1925 vector shifted_vline;
1928 vty->node = ENABLE_NODE;
1929 /* We can try it on enable node, cos' the vty is authenticated */
1931 shifted_vline = vector_init(vector_count(vline));
1933 for (index = 1; index < vector_active(vline); index++) {
1934 vector_set_index(shifted_vline, index - 1,
1935 vector_lookup(vline, index));
1938 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
1940 vector_free(shifted_vline);
1945 saved_ret = ret = cmd_execute_command_real(vline, vty, cmd);
1950 /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
1951 while (ret != CMD_SUCCESS && ret != CMD_WARNING
1952 && vty->node > CONFIG_NODE) {
1954 ret = cmd_execute_command_real(vline, vty, cmd);
1956 if (ret == CMD_SUCCESS || ret == CMD_WARNING) {
1957 /* succesfull command, leave the node as is */
1961 /* no command succeeded, reset the vty to the original node and
1962 return the error for this node */
1965 vty->index = oindex;
1970 /* Execute command by argument readline. */
1972 cmd_execute_command_strict(vector vline, struct vty *vty,
1973 struct cmd_element **cmd)
1978 struct cmd_element *cmd_element;
1979 struct cmd_element *matched_element;
1980 unsigned int matched_count, incomplete_count;
1982 const char *argv[CMD_ARGC_MAX];
1984 enum match_type match = 0;
1987 /* Make copy of command element */
1988 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1990 for (index = 0; index < vector_active(vline); index++)
1991 if ((command = vector_slot(vline, index))) {
1994 match = cmd_filter_by_string(vector_slot(vline, index),
1997 /* If command meets '.VARARG' then finish matching. */
1998 if (match == vararg_match)
2002 is_cmd_ambiguous(command, cmd_vector, index, match);
2004 vector_free(cmd_vector);
2005 return CMD_ERR_AMBIGUOUS;
2008 vector_free(cmd_vector);
2009 return CMD_ERR_NO_MATCH;
2013 /* Check matched count. */
2014 matched_element = NULL;
2016 incomplete_count = 0;
2017 for (i = 0; i < vector_active(cmd_vector); i++)
2018 if (vector_slot(cmd_vector, i) != NULL) {
2019 cmd_element = vector_slot(cmd_vector, i);
2021 if (match == vararg_match
2022 || index >= cmd_element->cmdsize) {
2023 matched_element = cmd_element;
2029 /* Finish of using cmd_vector. */
2030 vector_free(cmd_vector);
2032 /* To execute command, matched_count must be 1. */
2033 if (matched_count == 0) {
2034 if (incomplete_count)
2035 return CMD_ERR_INCOMPLETE;
2037 return CMD_ERR_NO_MATCH;
2040 if (matched_count > 1)
2041 return CMD_ERR_AMBIGUOUS;
2043 /* Argument treatment */
2047 for (i = 0; i < vector_active(vline); i++) {
2049 argv[argc++] = vector_slot(vline, i);
2052 vector_slot(matched_element->strvec, i);
2054 if (vector_active(descvec) == 1) {
2055 struct desc *desc = vector_slot(descvec, 0);
2057 if (CMD_VARARG(desc->cmd))
2060 if (varflag || CMD_VARIABLE(desc->cmd)
2061 || CMD_OPTION(desc->cmd))
2062 argv[argc++] = vector_slot(vline, i);
2064 argv[argc++] = vector_slot(vline, i);
2067 if (argc >= CMD_ARGC_MAX)
2068 return CMD_ERR_EXEED_ARGC_MAX;
2071 /* For vtysh execution. */
2073 *cmd = matched_element;
2075 if (matched_element->daemon)
2076 return CMD_SUCCESS_DAEMON;
2078 /* Now execute matched command */
2079 return (*matched_element->func) (matched_element, vty, argc, argv);
2082 /* Configration make from file. */
2083 int config_from_file(struct vty *vty, FILE * fp)
2088 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
2089 vline = cmd_make_strvec(vty->buf);
2091 /* In case of comment line */
2094 /* Execute configuration command : this is strict match */
2095 ret = cmd_execute_command_strict(vline, vty, NULL);
2097 /* Try again with setting node to CONFIG_NODE */
2098 while (ret != CMD_SUCCESS && ret != CMD_WARNING
2099 && ret != CMD_ERR_NOTHING_TODO
2100 && vty->node != CONFIG_NODE) {
2102 ret = cmd_execute_command_strict(vline, vty, NULL);
2105 cmd_free_strvec(vline);
2107 if (ret != CMD_SUCCESS && ret != CMD_WARNING
2108 && ret != CMD_ERR_NOTHING_TODO)
2114 /* Configration from terminal */
2115 DEFUN(config_terminal,
2116 config_terminal_cmd,
2117 "configure terminal",
2118 "Configuration from vty interface\n" "Configuration terminal\n")
2120 if (vty_config_lock(vty))
2121 vty->node = CONFIG_NODE;
2123 vty_out(vty, "VTY configuration is locked by other VTY%s",
2130 /* Enable command */
2131 DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2133 /* If enable password is NULL, change to ENABLE_NODE */
2134 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2135 vty->type == VTY_SHELL_SERV)
2136 vty->node = ENABLE_NODE;
2138 vty->node = AUTH_ENABLE_NODE;
2143 /* Disable command */
2145 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2147 if (vty->node == ENABLE_NODE)
2148 vty->node = VIEW_NODE;
2152 /* Down vty node level. */
2154 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2156 switch (vty->node) {
2159 if (0) //vty_shell (vty))
2162 vty->status = VTY_CLOSE;
2165 vty->node = ENABLE_NODE;
2166 vty_config_unlock(vty);
2169 vty->node = CONFIG_NODE;
2177 /* End of configuration. */
2179 config_end_cmd, "end", "End current mode and change to enable mode.")
2181 switch (vty->node) {
2184 /* Nothing to do. */
2188 vty_config_unlock(vty);
2189 vty->node = ENABLE_NODE;
2199 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2201 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2202 host.app_info->version,
2203 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2204 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
2209 /* Help display function for all node. */
2211 config_help_cmd, "help", "Description of the interactive help system\n")
2214 "This VTY provides advanced help features. When you need help,%s\
2215 anytime at the command line please press '?'.%s\
2217 If nothing matches, the help list will be empty and you must backup%s\
2218 until entering a '?' shows the available options.%s\
2219 Two styles of help are provided:%s\
2220 1. Full help is available when you are ready to enter a%s\
2221 command argument (e.g. 'show ?') and describes each possible%s\
2223 2. Partial help is provided when an abbreviated argument is entered%s\
2224 and you want to know what arguments match the input%s\
2225 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2226 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2230 /* Help display function for all node. */
2231 gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2234 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2235 struct cmd_element *cmd;
2237 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2238 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2239 && !(cmd->attr == CMD_ATTR_DEPRECATED
2240 || cmd->attr == CMD_ATTR_HIDDEN))
2241 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2245 /* Write current configuration into file. */
2246 DEFUN(config_write_file,
2247 config_write_file_cmd,
2249 "Write running configuration to memory, network, or terminal\n"
2250 "Write to configuration file\n")
2254 struct cmd_node *node;
2256 char *config_file_tmp = NULL;
2257 char *config_file_sav = NULL;
2258 struct vty *file_vty;
2260 /* Check and see if we are operating under vtysh configuration */
2261 if (host.config == NULL) {
2262 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2268 config_file = host.config;
2271 _talloc_zero(tall_vty_cmd_ctx,
2272 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2274 strcpy(config_file_sav, config_file);
2275 strcat(config_file_sav, CONF_BACKUP_EXT);
2277 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2279 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2281 /* Open file to configuration write. */
2282 fd = mkstemp(config_file_tmp);
2284 vty_out(vty, "Can't open configuration file %s.%s",
2285 config_file_tmp, VTY_NEWLINE);
2286 talloc_free(config_file_tmp);
2287 talloc_free(config_file_sav);
2291 /* Make vty for configuration file. */
2292 file_vty = vty_new();
2294 file_vty->type = VTY_FILE;
2296 /* Config file header print. */
2297 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
2298 host.app_info->name, host.app_info->version);
2299 //vty_time_print (file_vty, 1);
2300 vty_out(file_vty, "!\n");
2302 for (i = 0; i < vector_active(cmdvec); i++)
2303 if ((node = vector_slot(cmdvec, i)) && node->func) {
2304 if ((*node->func) (file_vty))
2305 vty_out(file_vty, "!\n");
2307 vty_close(file_vty);
2309 if (unlink(config_file_sav) != 0)
2310 if (errno != ENOENT) {
2312 "Can't unlink backup configuration file %s.%s",
2313 config_file_sav, VTY_NEWLINE);
2314 talloc_free(config_file_sav);
2315 talloc_free(config_file_tmp);
2316 unlink(config_file_tmp);
2319 if (link(config_file, config_file_sav) != 0) {
2320 vty_out(vty, "Can't backup old configuration file %s.%s",
2321 config_file_sav, VTY_NEWLINE);
2322 talloc_free(config_file_sav);
2323 talloc_free(config_file_tmp);
2324 unlink(config_file_tmp);
2328 if (unlink(config_file) != 0) {
2329 vty_out(vty, "Can't unlink configuration file %s.%s",
2330 config_file, VTY_NEWLINE);
2331 talloc_free(config_file_sav);
2332 talloc_free(config_file_tmp);
2333 unlink(config_file_tmp);
2336 if (link(config_file_tmp, config_file) != 0) {
2337 vty_out(vty, "Can't save configuration file %s.%s", config_file,
2339 talloc_free(config_file_sav);
2340 talloc_free(config_file_tmp);
2341 unlink(config_file_tmp);
2344 unlink(config_file_tmp);
2347 talloc_free(config_file_sav);
2348 talloc_free(config_file_tmp);
2350 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
2351 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2352 config_file, strerror(errno), errno, VTY_NEWLINE);
2356 vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE);
2360 ALIAS(config_write_file,
2362 "write", "Write running configuration to memory, network, or terminal\n")
2364 ALIAS(config_write_file,
2365 config_write_memory_cmd,
2367 "Write running configuration to memory, network, or terminal\n"
2368 "Write configuration to the file (same as write file)\n")
2370 ALIAS(config_write_file,
2371 copy_runningconfig_startupconfig_cmd,
2372 "copy running-config startup-config",
2373 "Copy configuration\n"
2374 "Copy running config to... \n"
2375 "Copy running config to startup config (same as write file)\n")
2377 /* Write current configuration into the terminal. */
2378 DEFUN(config_write_terminal,
2379 config_write_terminal_cmd,
2381 "Write running configuration to memory, network, or terminal\n"
2382 "Write to terminal\n")
2385 struct cmd_node *node;
2387 if (vty->type == VTY_SHELL_SERV) {
2388 for (i = 0; i < vector_active(cmdvec); i++)
2389 if ((node = vector_slot(cmdvec, i)) && node->func
2391 if ((*node->func) (vty))
2392 vty_out(vty, "!%s", VTY_NEWLINE);
2395 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2397 vty_out(vty, "!%s", VTY_NEWLINE);
2399 for (i = 0; i < vector_active(cmdvec); i++)
2400 if ((node = vector_slot(cmdvec, i)) && node->func) {
2401 if ((*node->func) (vty))
2402 vty_out(vty, "!%s", VTY_NEWLINE);
2404 vty_out(vty, "end%s", VTY_NEWLINE);
2409 /* Write current configuration into the terminal. */
2410 ALIAS(config_write_terminal,
2411 show_running_config_cmd,
2412 "show running-config", SHOW_STR "running configuration\n")
2414 /* Write startup configuration into the terminal. */
2415 DEFUN(show_startup_config,
2416 show_startup_config_cmd,
2417 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2422 confp = fopen(host.config, "r");
2423 if (confp == NULL) {
2424 vty_out(vty, "Can't open configuration file [%s]%s",
2425 host.config, VTY_NEWLINE);
2429 while (fgets(buf, BUFSIZ, confp)) {
2432 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2436 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2444 /* Hostname configuration */
2445 DEFUN(config_hostname,
2448 "Set system's network name\n" "This system's network name\n")
2450 if (!isalpha((int)*argv[0])) {
2451 vty_out(vty, "Please specify string starting with alphabet%s",
2457 talloc_free(host.name);
2459 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2463 DEFUN(config_no_hostname,
2465 "no hostname [HOSTNAME]",
2466 NO_STR "Reset system's network name\n" "Host name of this router\n")
2469 talloc_free(host.name);
2474 /* VTY interface password set. */
2475 DEFUN(config_password, password_cmd,
2476 "password (8|) WORD",
2477 "Assign the terminal connection password\n"
2478 "Specifies a HIDDEN password will follow\n"
2479 "dummy string \n" "The HIDDEN line password string\n")
2481 /* Argument check. */
2483 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2488 if (*argv[0] == '8') {
2490 talloc_free(host.password);
2491 host.password = NULL;
2492 if (host.password_encrypt)
2493 talloc_free(host.password_encrypt);
2494 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2497 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2502 if (!isalnum((int)*argv[0])) {
2504 "Please specify string starting with alphanumeric%s",
2510 talloc_free(host.password);
2511 host.password = NULL;
2515 if (host.password_encrypt)
2516 talloc_free(host.password_encrypt);
2517 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2520 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2525 ALIAS(config_password, password_text_cmd,
2527 "Assign the terminal connection password\n"
2528 "The UNENCRYPTED (cleartext) line password\n")
2530 /* VTY enable password set. */
2531 DEFUN(config_enable_password, enable_password_cmd,
2532 "enable password (8|) WORD",
2533 "Modify enable password parameters\n"
2534 "Assign the privileged level password\n"
2535 "Specifies a HIDDEN password will follow\n"
2536 "dummy string \n" "The HIDDEN 'enable' password string\n")
2538 /* Argument check. */
2540 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
2544 /* Crypt type is specified. */
2546 if (*argv[0] == '8') {
2548 talloc_free(host.enable);
2551 if (host.enable_encrypt)
2552 talloc_free(host.enable_encrypt);
2553 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
2557 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
2562 if (!isalnum((int)*argv[0])) {
2564 "Please specify string starting with alphanumeric%s",
2570 talloc_free(host.enable);
2573 /* Plain password input. */
2576 if (host.enable_encrypt)
2577 talloc_free(host.enable_encrypt);
2578 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
2581 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2586 ALIAS(config_enable_password,
2587 enable_password_text_cmd,
2588 "enable password LINE",
2589 "Modify enable password parameters\n"
2590 "Assign the privileged level password\n"
2591 "The UNENCRYPTED (cleartext) 'enable' password\n")
2593 /* VTY enable password delete. */
2594 DEFUN(no_config_enable_password, no_enable_password_cmd,
2595 "no enable password",
2597 "Modify enable password parameters\n"
2598 "Assign the privileged level password\n")
2601 talloc_free(host.enable);
2604 if (host.enable_encrypt)
2605 talloc_free(host.enable_encrypt);
2606 host.enable_encrypt = NULL;
2612 DEFUN(service_password_encrypt,
2613 service_password_encrypt_cmd,
2614 "service password-encryption",
2615 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2622 if (host.password) {
2623 if (host.password_encrypt)
2624 talloc_free(host.password_encrypt);
2625 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
2628 if (host.enable_encrypt)
2629 talloc_free(host.enable_encrypt);
2630 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
2636 DEFUN(no_service_password_encrypt,
2637 no_service_password_encrypt_cmd,
2638 "no service password-encryption",
2639 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
2646 if (host.password_encrypt)
2647 talloc_free(host.password_encrypt);
2648 host.password_encrypt = NULL;
2650 if (host.enable_encrypt)
2651 talloc_free(host.enable_encrypt);
2652 host.enable_encrypt = NULL;
2658 DEFUN(config_terminal_length, config_terminal_length_cmd,
2659 "terminal length <0-512>",
2660 "Set terminal line parameters\n"
2661 "Set number of lines on a screen\n"
2662 "Number of lines on screen (0 for no pausing)\n")
2665 char *endptr = NULL;
2667 lines = strtol(argv[0], &endptr, 10);
2668 if (lines < 0 || lines > 512 || *endptr != '\0') {
2669 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2677 DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
2678 "terminal no length",
2679 "Set terminal line parameters\n"
2680 NO_STR "Set number of lines on a screen\n")
2686 DEFUN(service_terminal_length, service_terminal_length_cmd,
2687 "service terminal-length <0-512>",
2688 "Set up miscellaneous service\n"
2689 "System wide terminal length configuration\n"
2690 "Number of lines of VTY (0 means no line control)\n")
2693 char *endptr = NULL;
2695 lines = strtol(argv[0], &endptr, 10);
2696 if (lines < 0 || lines > 512 || *endptr != '\0') {
2697 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
2705 DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
2706 "no service terminal-length [<0-512>]",
2708 "Set up miscellaneous service\n"
2709 "System wide terminal length configuration\n"
2710 "Number of lines of VTY (0 means no line control)\n")
2716 DEFUN_HIDDEN(do_echo,
2719 "Echo a message back to the vty\n" "The message to echo\n")
2723 vty_out(vty, "%s%s",
2725 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
2727 talloc_free(message);
2732 DEFUN(config_logmsg,
2734 "logmsg " LOG_LEVELS " .MESSAGE",
2735 "Send a message to enabled logging destinations\n"
2736 LOG_LEVEL_DESC "The message to send\n")
2741 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2742 return CMD_ERR_NO_MATCH;
2745 ((message = argv_concat(argv, argc, 1)) ? message : ""));
2747 talloc_free(message);
2753 "show logging", SHOW_STR "Show current logging configuration\n")
2755 struct zlog *zl = zlog_default;
2757 vty_out(vty, "Syslog logging: ");
2758 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
2759 vty_out(vty, "disabled");
2761 vty_out(vty, "level %s, facility %s, ident %s",
2762 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
2763 facility_name(zl->facility), zl->ident);
2764 vty_out(vty, "%s", VTY_NEWLINE);
2766 vty_out(vty, "Stdout logging: ");
2767 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
2768 vty_out(vty, "disabled");
2770 vty_out(vty, "level %s",
2771 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
2772 vty_out(vty, "%s", VTY_NEWLINE);
2774 vty_out(vty, "Monitor logging: ");
2775 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
2776 vty_out(vty, "disabled");
2778 vty_out(vty, "level %s",
2779 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
2780 vty_out(vty, "%s", VTY_NEWLINE);
2782 vty_out(vty, "File logging: ");
2783 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
2784 vty_out(vty, "disabled");
2786 vty_out(vty, "level %s, filename %s",
2787 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
2789 vty_out(vty, "%s", VTY_NEWLINE);
2791 vty_out(vty, "Protocol name: %s%s",
2792 zlog_proto_names[zl->protocol], VTY_NEWLINE);
2793 vty_out(vty, "Record priority: %s%s",
2794 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
2799 DEFUN(config_log_stdout,
2800 config_log_stdout_cmd,
2801 "log stdout", "Logging control\n" "Set stdout logging level\n")
2803 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
2807 DEFUN(config_log_stdout_level,
2808 config_log_stdout_level_cmd,
2809 "log stdout " LOG_LEVELS,
2810 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
2814 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2815 return CMD_ERR_NO_MATCH;
2816 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
2820 DEFUN(no_config_log_stdout,
2821 no_config_log_stdout_cmd,
2822 "no log stdout [LEVEL]",
2823 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
2825 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
2829 DEFUN(config_log_monitor,
2830 config_log_monitor_cmd,
2832 "Logging control\n" "Set terminal line (monitor) logging level\n")
2834 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
2838 DEFUN(config_log_monitor_level,
2839 config_log_monitor_level_cmd,
2840 "log monitor " LOG_LEVELS,
2842 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2846 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2847 return CMD_ERR_NO_MATCH;
2848 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
2852 DEFUN(no_config_log_monitor,
2853 no_config_log_monitor_cmd,
2854 "no log monitor [LEVEL]",
2857 "Disable terminal line (monitor) logging\n" "Logging level\n")
2859 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
2863 static int set_log_file(struct vty *vty, const char *fname, int loglevel)
2867 const char *fullpath;
2869 /* Path detection. */
2870 if (!IS_DIRECTORY_SEP(*fname)) {
2871 char cwd[MAXPATHLEN + 1];
2872 cwd[MAXPATHLEN] = '\0';
2874 if (getcwd(cwd, MAXPATHLEN) == NULL) {
2875 zlog_err("config_log_file: Unable to alloc mem!");
2879 if ((p = _talloc_zero(tall_vcmd_ctx,
2880 strlen(cwd) + strlen(fname) + 2),
2883 zlog_err("config_log_file: Unable to alloc mem!");
2886 sprintf(p, "%s/%s", cwd, fname);
2891 ret = zlog_set_file(NULL, fullpath, loglevel);
2897 vty_out(vty, "can't open logfile %s\n", fname);
2902 talloc_free(host.logfile);
2904 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
2909 DEFUN(config_log_file,
2910 config_log_file_cmd,
2911 "log file FILENAME",
2912 "Logging control\n" "Logging to file\n" "Logging filename\n")
2914 return set_log_file(vty, argv[0], zlog_default->default_lvl);
2917 DEFUN(config_log_file_level,
2918 config_log_file_level_cmd,
2919 "log file FILENAME " LOG_LEVELS,
2921 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
2925 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
2926 return CMD_ERR_NO_MATCH;
2927 return set_log_file(vty, argv[0], level);
2930 DEFUN(no_config_log_file,
2931 no_config_log_file_cmd,
2932 "no log file [FILENAME]",
2934 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
2936 zlog_reset_file(NULL);
2939 talloc_free(host.logfile);
2941 host.logfile = NULL;
2946 ALIAS(no_config_log_file,
2947 no_config_log_file_level_cmd,
2948 "no log file FILENAME LEVEL",
2951 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
2953 DEFUN(config_log_syslog,
2954 config_log_syslog_cmd,
2955 "log syslog", "Logging control\n" "Set syslog logging level\n")
2957 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2961 DEFUN(config_log_syslog_level,
2962 config_log_syslog_level_cmd,
2963 "log syslog " LOG_LEVELS,
2964 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
2968 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
2969 return CMD_ERR_NO_MATCH;
2970 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
2974 DEFUN_DEPRECATED(config_log_syslog_facility,
2975 config_log_syslog_facility_cmd,
2976 "log syslog facility " LOG_FACILITIES,
2978 "Logging goes to syslog\n"
2979 "(Deprecated) Facility parameter for syslog messages\n"
2984 if ((facility = facility_match(argv[0])) < 0)
2985 return CMD_ERR_NO_MATCH;
2987 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
2988 zlog_default->facility = facility;
2992 DEFUN(no_config_log_syslog,
2993 no_config_log_syslog_cmd,
2994 "no log syslog [LEVEL]",
2995 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
2997 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3001 ALIAS(no_config_log_syslog,
3002 no_config_log_syslog_facility_cmd,
3003 "no log syslog facility " LOG_FACILITIES,
3006 "Logging goes to syslog\n"
3007 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3009 DEFUN(config_log_facility,
3010 config_log_facility_cmd,
3011 "log facility " LOG_FACILITIES,
3013 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3017 if ((facility = facility_match(argv[0])) < 0)
3018 return CMD_ERR_NO_MATCH;
3019 zlog_default->facility = facility;
3023 DEFUN(no_config_log_facility,
3024 no_config_log_facility_cmd,
3025 "no log facility [FACILITY]",
3028 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3030 zlog_default->facility = LOG_DAEMON;
3034 DEFUN_DEPRECATED(config_log_trap,
3035 config_log_trap_cmd,
3036 "log trap " LOG_LEVELS,
3038 "(Deprecated) Set logging level and default for all destinations\n"
3044 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3045 return CMD_ERR_NO_MATCH;
3047 zlog_default->default_lvl = new_level;
3048 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3049 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3050 zlog_default->maxlvl[i] = new_level;
3054 DEFUN_DEPRECATED(no_config_log_trap,
3055 no_config_log_trap_cmd,
3056 "no log trap [LEVEL]",
3059 "Permit all logging information\n" "Logging level\n")
3061 zlog_default->default_lvl = LOG_DEBUG;
3065 DEFUN(config_log_record_priority,
3066 config_log_record_priority_cmd,
3067 "log record-priority",
3069 "Log the priority of the message within the message\n")
3071 zlog_default->record_priority = 1;
3075 DEFUN(no_config_log_record_priority,
3076 no_config_log_record_priority_cmd,
3077 "no log record-priority",
3080 "Do not log the priority of the message within the message\n")
3082 zlog_default->record_priority = 0;
3087 DEFUN(banner_motd_file,
3088 banner_motd_file_cmd,
3089 "banner motd file [FILE]",
3090 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3093 talloc_free(host.motdfile);
3094 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3099 DEFUN(banner_motd_default,
3100 banner_motd_default_cmd,
3101 "banner motd default",
3102 "Set banner string\n" "Strings for motd\n" "Default string\n")
3104 host.motd = default_motd;
3108 DEFUN(no_banner_motd,
3110 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3114 talloc_free(host.motdfile);
3115 host.motdfile = NULL;
3119 /* Set config filename. Called from vty.c */
3120 void host_config_set(const char *filename)
3122 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3125 void install_default(enum node_type node)
3127 install_element(node, &config_help_cmd);
3128 install_element(node, &config_list_cmd);
3130 install_element(node, &config_write_terminal_cmd);
3131 install_element(node, &config_write_file_cmd);
3132 install_element(node, &config_write_memory_cmd);
3133 install_element(node, &config_write_cmd);
3134 install_element(node, &show_running_config_cmd);
3137 /* Initialize command interface. Install basic nodes and commands. */
3138 void cmd_init(int terminal)
3140 /* Allocate initial top vector of commands. */
3141 cmdvec = vector_init(VECTOR_MIN_SIZE);
3143 /* Default host value settings. */
3145 host.password = NULL;
3147 host.logfile = NULL;
3150 host.motd = default_motd;
3151 host.motdfile = NULL;
3153 /* Install top nodes. */
3154 install_node(&view_node, NULL);
3155 install_node(&enable_node, NULL);
3156 install_node(&auth_node, NULL);
3157 install_node(&auth_enable_node, NULL);
3158 install_node(&config_node, config_write_host);
3160 /* Each node's basic commands. */
3161 install_element(VIEW_NODE, &show_version_cmd);
3163 install_element(VIEW_NODE, &config_list_cmd);
3164 install_element(VIEW_NODE, &config_exit_cmd);
3165 install_element(VIEW_NODE, &config_help_cmd);
3166 install_element(VIEW_NODE, &config_enable_cmd);
3167 install_element(VIEW_NODE, &config_terminal_length_cmd);
3168 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3169 install_element(VIEW_NODE, &echo_cmd);
3173 install_element(ENABLE_NODE, &config_exit_cmd);
3174 install_default(ENABLE_NODE);
3175 install_element(ENABLE_NODE, &config_disable_cmd);
3176 install_element(ENABLE_NODE, &config_terminal_cmd);
3177 install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd);
3179 install_element (ENABLE_NODE, &show_startup_config_cmd);
3180 install_element(ENABLE_NODE, &show_version_cmd);
3183 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3184 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3185 install_element(ENABLE_NODE, &echo_cmd);
3187 install_default(CONFIG_NODE);
3188 install_element(CONFIG_NODE, &config_exit_cmd);
3191 install_element(CONFIG_NODE, &hostname_cmd);
3192 install_element(CONFIG_NODE, &no_hostname_cmd);
3195 install_element(CONFIG_NODE, &password_cmd);
3196 install_element(CONFIG_NODE, &password_text_cmd);
3197 install_element(CONFIG_NODE, &enable_password_cmd);
3198 install_element(CONFIG_NODE, &enable_password_text_cmd);
3199 install_element(CONFIG_NODE, &no_enable_password_cmd);
3202 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3203 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3205 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3206 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3207 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3208 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3209 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);