jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* Command interpreter routine for virtual terminal [aka TeletYpe] |
| 2 | Copyright (C) 1997, 98, 99 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published |
| 8 | by the Free Software Foundation; either version 2, or (at your |
| 9 | option) any later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the |
| 18 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "command.h" |
| 24 | #include "memory.h" |
| 25 | #include "log.h" |
| 26 | #include "version.h" |
| 27 | |
| 28 | /* Command vector which includes some level of command lists. Normally |
| 29 | each daemon maintains each own cmdvec. */ |
| 30 | vector cmdvec; |
| 31 | |
| 32 | /* Host information structure. */ |
| 33 | struct host host; |
| 34 | |
| 35 | /* Default motd string. */ |
| 36 | char *default_motd = |
| 37 | "\r\n\ |
| 38 | Hello, this is zebra (version " ZEBRA_VERSION ").\r\n\ |
| 39 | Copyright 1996-2002 Kunihiro Ishiguro.\r\n\ |
| 40 | \r\n"; |
| 41 | |
| 42 | /* Standard command node structures. */ |
| 43 | struct cmd_node auth_node = |
| 44 | { |
| 45 | AUTH_NODE, |
| 46 | "Password: ", |
| 47 | }; |
| 48 | |
| 49 | struct cmd_node view_node = |
| 50 | { |
| 51 | VIEW_NODE, |
| 52 | "%s> ", |
| 53 | }; |
| 54 | |
| 55 | struct cmd_node auth_enable_node = |
| 56 | { |
| 57 | AUTH_ENABLE_NODE, |
| 58 | "Password: ", |
| 59 | }; |
| 60 | |
| 61 | struct cmd_node enable_node = |
| 62 | { |
| 63 | ENABLE_NODE, |
| 64 | "%s# ", |
| 65 | }; |
| 66 | |
| 67 | struct cmd_node config_node = |
| 68 | { |
| 69 | CONFIG_NODE, |
| 70 | "%s(config)# ", |
| 71 | 1 |
| 72 | }; |
| 73 | |
| 74 | /* Utility function to concatenate argv argument into a single string |
| 75 | with inserting ' ' character between each argument. */ |
| 76 | char * |
| 77 | argv_concat (char **argv, int argc, int shift) |
| 78 | { |
| 79 | int i; |
| 80 | int len; |
| 81 | int index; |
| 82 | char *str; |
| 83 | |
| 84 | str = NULL; |
| 85 | index = 0; |
| 86 | |
| 87 | for (i = shift; i < argc; i++) |
| 88 | { |
| 89 | len = strlen (argv[i]); |
| 90 | |
| 91 | if (i == shift) |
| 92 | { |
| 93 | str = XSTRDUP (MTYPE_TMP, argv[i]); |
| 94 | index = len; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | str = XREALLOC (MTYPE_TMP, str, (index + len + 2)); |
| 99 | str[index++] = ' '; |
| 100 | memcpy (str + index, argv[i], len); |
| 101 | index += len; |
| 102 | str[index] = '\0'; |
| 103 | } |
| 104 | } |
| 105 | return str; |
| 106 | } |
| 107 | |
| 108 | /* Install top node of command vector. */ |
| 109 | void |
| 110 | install_node (struct cmd_node *node, |
| 111 | int (*func) (struct vty *)) |
| 112 | { |
| 113 | vector_set_index (cmdvec, node->node, node); |
| 114 | node->func = func; |
| 115 | node->cmd_vector = vector_init (VECTOR_MIN_SIZE); |
| 116 | } |
| 117 | |
| 118 | /* Compare two command's string. Used in sort_node (). */ |
| 119 | int |
| 120 | cmp_node (const void *p, const void *q) |
| 121 | { |
| 122 | struct cmd_element *a = *(struct cmd_element **)p; |
| 123 | struct cmd_element *b = *(struct cmd_element **)q; |
| 124 | |
| 125 | return strcmp (a->string, b->string); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | cmp_desc (const void *p, const void *q) |
| 130 | { |
| 131 | struct desc *a = *(struct desc **)p; |
| 132 | struct desc *b = *(struct desc **)q; |
| 133 | |
| 134 | return strcmp (a->cmd, b->cmd); |
| 135 | } |
| 136 | |
| 137 | /* Sort each node's command element according to command string. */ |
| 138 | void |
| 139 | sort_node () |
| 140 | { |
| 141 | int i, j; |
| 142 | struct cmd_node *cnode; |
| 143 | vector descvec; |
| 144 | struct cmd_element *cmd_element; |
| 145 | |
| 146 | for (i = 0; i < vector_max (cmdvec); i++) |
| 147 | if ((cnode = vector_slot (cmdvec, i)) != NULL) |
| 148 | { |
| 149 | vector cmd_vector = cnode->cmd_vector; |
| 150 | qsort (cmd_vector->index, cmd_vector->max, sizeof (void *), cmp_node); |
| 151 | |
| 152 | for (j = 0; j < vector_max (cmd_vector); j++) |
| 153 | if ((cmd_element = vector_slot (cmd_vector, j)) != NULL) |
| 154 | { |
| 155 | descvec = vector_slot (cmd_element->strvec, |
| 156 | vector_max (cmd_element->strvec) - 1); |
| 157 | qsort (descvec->index, descvec->max, sizeof (void *), cmp_desc); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* Breaking up string into each command piece. I assume given |
| 163 | character is separated by a space character. Return value is a |
| 164 | vector which includes char ** data element. */ |
| 165 | vector |
| 166 | cmd_make_strvec (char *string) |
| 167 | { |
| 168 | char *cp, *start, *token; |
| 169 | int strlen; |
| 170 | vector strvec; |
| 171 | |
| 172 | if (string == NULL) |
| 173 | return NULL; |
| 174 | |
| 175 | cp = string; |
| 176 | |
| 177 | /* Skip white spaces. */ |
| 178 | while (isspace ((int) *cp) && *cp != '\0') |
| 179 | cp++; |
| 180 | |
| 181 | /* Return if there is only white spaces */ |
| 182 | if (*cp == '\0') |
| 183 | return NULL; |
| 184 | |
| 185 | if (*cp == '!' || *cp == '#') |
| 186 | return NULL; |
| 187 | |
| 188 | /* Prepare return vector. */ |
| 189 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 190 | |
| 191 | /* Copy each command piece and set into vector. */ |
| 192 | while (1) |
| 193 | { |
| 194 | start = cp; |
| 195 | while (!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n') && |
| 196 | *cp != '\0') |
| 197 | cp++; |
| 198 | strlen = cp - start; |
| 199 | token = XMALLOC (MTYPE_STRVEC, strlen + 1); |
| 200 | memcpy (token, start, strlen); |
| 201 | *(token + strlen) = '\0'; |
| 202 | vector_set (strvec, token); |
| 203 | |
| 204 | while ((isspace ((int) *cp) || *cp == '\n' || *cp == '\r') && |
| 205 | *cp != '\0') |
| 206 | cp++; |
| 207 | |
| 208 | if (*cp == '\0') |
| 209 | return strvec; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* Free allocated string vector. */ |
| 214 | void |
| 215 | cmd_free_strvec (vector v) |
| 216 | { |
| 217 | int i; |
| 218 | char *cp; |
| 219 | |
| 220 | if (!v) |
| 221 | return; |
| 222 | |
| 223 | for (i = 0; i < vector_max (v); i++) |
| 224 | if ((cp = vector_slot (v, i)) != NULL) |
| 225 | XFREE (MTYPE_STRVEC, cp); |
| 226 | |
| 227 | vector_free (v); |
| 228 | } |
| 229 | |
| 230 | /* Fetch next description. Used in cmd_make_descvec(). */ |
| 231 | char * |
| 232 | cmd_desc_str (char **string) |
| 233 | { |
| 234 | char *cp, *start, *token; |
| 235 | int strlen; |
| 236 | |
| 237 | cp = *string; |
| 238 | |
| 239 | if (cp == NULL) |
| 240 | return NULL; |
| 241 | |
| 242 | /* Skip white spaces. */ |
| 243 | while (isspace ((int) *cp) && *cp != '\0') |
| 244 | cp++; |
| 245 | |
| 246 | /* Return if there is only white spaces */ |
| 247 | if (*cp == '\0') |
| 248 | return NULL; |
| 249 | |
| 250 | start = cp; |
| 251 | |
| 252 | while (!(*cp == '\r' || *cp == '\n') && *cp != '\0') |
| 253 | cp++; |
| 254 | |
| 255 | strlen = cp - start; |
| 256 | token = XMALLOC (MTYPE_STRVEC, strlen + 1); |
| 257 | memcpy (token, start, strlen); |
| 258 | *(token + strlen) = '\0'; |
| 259 | |
| 260 | *string = cp; |
| 261 | |
| 262 | return token; |
| 263 | } |
| 264 | |
| 265 | /* New string vector. */ |
| 266 | vector |
| 267 | cmd_make_descvec (char *string, char *descstr) |
| 268 | { |
| 269 | int multiple = 0; |
| 270 | char *sp; |
| 271 | char *token; |
| 272 | int len; |
| 273 | char *cp; |
| 274 | char *dp; |
| 275 | vector allvec; |
| 276 | vector strvec = NULL; |
| 277 | struct desc *desc; |
| 278 | |
| 279 | cp = string; |
| 280 | dp = descstr; |
| 281 | |
| 282 | if (cp == NULL) |
| 283 | return NULL; |
| 284 | |
| 285 | allvec = vector_init (VECTOR_MIN_SIZE); |
| 286 | |
| 287 | while (1) |
| 288 | { |
| 289 | while (isspace ((int) *cp) && *cp != '\0') |
| 290 | cp++; |
| 291 | |
| 292 | if (*cp == '(') |
| 293 | { |
| 294 | multiple = 1; |
| 295 | cp++; |
| 296 | } |
| 297 | if (*cp == ')') |
| 298 | { |
| 299 | multiple = 0; |
| 300 | cp++; |
| 301 | } |
| 302 | if (*cp == '|') |
| 303 | { |
| 304 | if (! multiple) |
| 305 | { |
| 306 | fprintf (stderr, "Command parse error!: %s\n", string); |
| 307 | exit (1); |
| 308 | } |
| 309 | cp++; |
| 310 | } |
| 311 | |
| 312 | while (isspace ((int) *cp) && *cp != '\0') |
| 313 | cp++; |
| 314 | |
| 315 | if (*cp == '(') |
| 316 | { |
| 317 | multiple = 1; |
| 318 | cp++; |
| 319 | } |
| 320 | |
| 321 | if (*cp == '\0') |
| 322 | return allvec; |
| 323 | |
| 324 | sp = cp; |
| 325 | |
| 326 | while (! (isspace ((int) *cp) || *cp == '\r' || *cp == '\n' || *cp == ')' || *cp == '|') && *cp != '\0') |
| 327 | cp++; |
| 328 | |
| 329 | len = cp - sp; |
| 330 | |
| 331 | token = XMALLOC (MTYPE_STRVEC, len + 1); |
| 332 | memcpy (token, sp, len); |
| 333 | *(token + len) = '\0'; |
| 334 | |
| 335 | desc = XCALLOC (MTYPE_DESC, sizeof (struct desc)); |
| 336 | desc->cmd = token; |
| 337 | desc->str = cmd_desc_str (&dp); |
| 338 | |
| 339 | if (multiple) |
| 340 | { |
| 341 | if (multiple == 1) |
| 342 | { |
| 343 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 344 | vector_set (allvec, strvec); |
| 345 | } |
| 346 | multiple++; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | strvec = vector_init (VECTOR_MIN_SIZE); |
| 351 | vector_set (allvec, strvec); |
| 352 | } |
| 353 | vector_set (strvec, desc); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* Count mandantory string vector size. This is to determine inputed |
| 358 | command has enough command length. */ |
| 359 | int |
| 360 | cmd_cmdsize (vector strvec) |
| 361 | { |
| 362 | int i; |
| 363 | char *str; |
| 364 | int size = 0; |
| 365 | vector descvec; |
| 366 | |
| 367 | for (i = 0; i < vector_max (strvec); i++) |
| 368 | { |
| 369 | descvec = vector_slot (strvec, i); |
| 370 | |
| 371 | if (vector_max (descvec) == 1) |
| 372 | { |
| 373 | struct desc *desc = vector_slot (descvec, 0); |
| 374 | |
| 375 | str = desc->cmd; |
| 376 | |
| 377 | if (str == NULL || CMD_OPTION (str)) |
| 378 | return size; |
| 379 | else |
| 380 | size++; |
| 381 | } |
| 382 | else |
| 383 | size++; |
| 384 | } |
| 385 | return size; |
| 386 | } |
| 387 | |
| 388 | /* Return prompt character of specified node. */ |
| 389 | char * |
| 390 | cmd_prompt (enum node_type node) |
| 391 | { |
| 392 | struct cmd_node *cnode; |
| 393 | |
| 394 | cnode = vector_slot (cmdvec, node); |
| 395 | return cnode->prompt; |
| 396 | } |
| 397 | |
| 398 | /* Install a command into a node. */ |
| 399 | void |
| 400 | install_element (enum node_type ntype, struct cmd_element *cmd) |
| 401 | { |
| 402 | struct cmd_node *cnode; |
| 403 | |
| 404 | cnode = vector_slot (cmdvec, ntype); |
| 405 | |
| 406 | if (cnode == NULL) |
| 407 | { |
| 408 | fprintf (stderr, "Command node %d doesn't exist, please check it\n", |
| 409 | ntype); |
| 410 | exit (1); |
| 411 | } |
| 412 | |
| 413 | vector_set (cnode->cmd_vector, cmd); |
| 414 | |
| 415 | cmd->strvec = cmd_make_descvec (cmd->string, cmd->doc); |
| 416 | cmd->cmdsize = cmd_cmdsize (cmd->strvec); |
| 417 | } |
| 418 | |
| 419 | static unsigned char itoa64[] = |
| 420 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 421 | |
| 422 | void |
| 423 | to64(char *s, long v, int n) |
| 424 | { |
| 425 | while (--n >= 0) |
| 426 | { |
| 427 | *s++ = itoa64[v&0x3f]; |
| 428 | v >>= 6; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | char *zencrypt (char *passwd) |
| 433 | { |
| 434 | char salt[6]; |
| 435 | struct timeval tv; |
| 436 | char *crypt (const char *, const char *); |
| 437 | |
| 438 | gettimeofday(&tv,0); |
| 439 | |
| 440 | to64(&salt[0], random(), 3); |
| 441 | to64(&salt[3], tv.tv_usec, 3); |
| 442 | salt[5] = '\0'; |
| 443 | |
| 444 | return crypt (passwd, salt); |
| 445 | } |
| 446 | |
| 447 | /* This function write configuration of this host. */ |
| 448 | int |
| 449 | config_write_host (struct vty *vty) |
| 450 | { |
| 451 | if (host.name) |
| 452 | vty_out (vty, "hostname %s%s", host.name, VTY_NEWLINE); |
| 453 | |
| 454 | if (host.encrypt) |
| 455 | { |
| 456 | if (host.password_encrypt) |
| 457 | vty_out (vty, "password 8 %s%s", host.password_encrypt, VTY_NEWLINE); |
| 458 | if (host.enable_encrypt) |
| 459 | vty_out (vty, "enable password 8 %s%s", host.enable_encrypt, VTY_NEWLINE); |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | if (host.password) |
| 464 | vty_out (vty, "password %s%s", host.password, VTY_NEWLINE); |
| 465 | if (host.enable) |
| 466 | vty_out (vty, "enable password %s%s", host.enable, VTY_NEWLINE); |
| 467 | } |
| 468 | |
| 469 | if (host.logfile) |
| 470 | vty_out (vty, "log file %s%s", host.logfile, VTY_NEWLINE); |
| 471 | |
| 472 | if (host.log_stdout) |
| 473 | vty_out (vty, "log stdout%s", VTY_NEWLINE); |
| 474 | |
| 475 | if (host.log_syslog) |
| 476 | vty_out (vty, "log syslog%s", VTY_NEWLINE); |
| 477 | |
| 478 | if (zlog_default->maskpri != LOG_DEBUG) |
| 479 | vty_out (vty, "log trap %s%s", zlog_priority[zlog_default->maskpri], VTY_NEWLINE); |
| 480 | |
| 481 | if (zlog_default->record_priority == 1) |
| 482 | vty_out (vty, "log record-priority%s", VTY_NEWLINE); |
| 483 | |
| 484 | if (host.advanced) |
| 485 | vty_out (vty, "service advanced-vty%s", VTY_NEWLINE); |
| 486 | |
| 487 | if (host.encrypt) |
| 488 | vty_out (vty, "service password-encryption%s", VTY_NEWLINE); |
| 489 | |
| 490 | if (host.lines >= 0) |
| 491 | vty_out (vty, "service terminal-length %d%s", host.lines, |
| 492 | VTY_NEWLINE); |
| 493 | |
| 494 | if (! host.motd) |
| 495 | vty_out (vty, "no banner motd%s", VTY_NEWLINE); |
| 496 | |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | /* Utility function for getting command vector. */ |
| 501 | vector |
| 502 | cmd_node_vector (vector v, enum node_type ntype) |
| 503 | { |
| 504 | struct cmd_node *cnode = vector_slot (v, ntype); |
| 505 | return cnode->cmd_vector; |
| 506 | } |
| 507 | |
| 508 | /* Filter command vector by symbol */ |
| 509 | int |
| 510 | cmd_filter_by_symbol (char *command, char *symbol) |
| 511 | { |
| 512 | int i, lim; |
| 513 | |
| 514 | if (strcmp (symbol, "IPV4_ADDRESS") == 0) |
| 515 | { |
| 516 | i = 0; |
| 517 | lim = strlen (command); |
| 518 | while (i < lim) |
| 519 | { |
| 520 | if (! (isdigit ((int) command[i]) || command[i] == '.' || command[i] == '/')) |
| 521 | return 1; |
| 522 | i++; |
| 523 | } |
| 524 | return 0; |
| 525 | } |
| 526 | if (strcmp (symbol, "STRING") == 0) |
| 527 | { |
| 528 | i = 0; |
| 529 | lim = strlen (command); |
| 530 | while (i < lim) |
| 531 | { |
| 532 | if (! (isalpha ((int) command[i]) || command[i] == '_' || command[i] == '-')) |
| 533 | return 1; |
| 534 | i++; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | if (strcmp (symbol, "IFNAME") == 0) |
| 539 | { |
| 540 | i = 0; |
| 541 | lim = strlen (command); |
| 542 | while (i < lim) |
| 543 | { |
| 544 | if (! isalnum ((int) command[i])) |
| 545 | return 1; |
| 546 | i++; |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* Completion match types. */ |
| 554 | enum match_type |
| 555 | { |
| 556 | no_match, |
| 557 | extend_match, |
| 558 | ipv4_prefix_match, |
| 559 | ipv4_match, |
| 560 | ipv6_prefix_match, |
| 561 | ipv6_match, |
| 562 | range_match, |
| 563 | vararg_match, |
| 564 | partly_match, |
| 565 | exact_match |
| 566 | }; |
| 567 | |
| 568 | enum match_type |
| 569 | cmd_ipv4_match (char *str) |
| 570 | { |
| 571 | char *sp; |
| 572 | int dots = 0, nums = 0; |
| 573 | char buf[4]; |
| 574 | |
| 575 | if (str == NULL) |
| 576 | return partly_match; |
| 577 | |
| 578 | for (;;) |
| 579 | { |
| 580 | memset (buf, 0, sizeof (buf)); |
| 581 | sp = str; |
| 582 | while (*str != '\0') |
| 583 | { |
| 584 | if (*str == '.') |
| 585 | { |
| 586 | if (dots >= 3) |
| 587 | return no_match; |
| 588 | |
| 589 | if (*(str + 1) == '.') |
| 590 | return no_match; |
| 591 | |
| 592 | if (*(str + 1) == '\0') |
| 593 | return partly_match; |
| 594 | |
| 595 | dots++; |
| 596 | break; |
| 597 | } |
| 598 | if (!isdigit ((int) *str)) |
| 599 | return no_match; |
| 600 | |
| 601 | str++; |
| 602 | } |
| 603 | |
| 604 | if (str - sp > 3) |
| 605 | return no_match; |
| 606 | |
| 607 | strncpy (buf, sp, str - sp); |
| 608 | if (atoi (buf) > 255) |
| 609 | return no_match; |
| 610 | |
| 611 | nums++; |
| 612 | |
| 613 | if (*str == '\0') |
| 614 | break; |
| 615 | |
| 616 | str++; |
| 617 | } |
| 618 | |
| 619 | if (nums < 4) |
| 620 | return partly_match; |
| 621 | |
| 622 | return exact_match; |
| 623 | } |
| 624 | |
| 625 | enum match_type |
| 626 | cmd_ipv4_prefix_match (char *str) |
| 627 | { |
| 628 | char *sp; |
| 629 | int dots = 0; |
| 630 | char buf[4]; |
| 631 | |
| 632 | if (str == NULL) |
| 633 | return partly_match; |
| 634 | |
| 635 | for (;;) |
| 636 | { |
| 637 | memset (buf, 0, sizeof (buf)); |
| 638 | sp = str; |
| 639 | while (*str != '\0' && *str != '/') |
| 640 | { |
| 641 | if (*str == '.') |
| 642 | { |
| 643 | if (dots == 3) |
| 644 | return no_match; |
| 645 | |
| 646 | if (*(str + 1) == '.' || *(str + 1) == '/') |
| 647 | return no_match; |
| 648 | |
| 649 | if (*(str + 1) == '\0') |
| 650 | return partly_match; |
| 651 | |
| 652 | dots++; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | if (!isdigit ((int) *str)) |
| 657 | return no_match; |
| 658 | |
| 659 | str++; |
| 660 | } |
| 661 | |
| 662 | if (str - sp > 3) |
| 663 | return no_match; |
| 664 | |
| 665 | strncpy (buf, sp, str - sp); |
| 666 | if (atoi (buf) > 255) |
| 667 | return no_match; |
| 668 | |
| 669 | if (dots == 3) |
| 670 | { |
| 671 | if (*str == '/') |
| 672 | { |
| 673 | if (*(str + 1) == '\0') |
| 674 | return partly_match; |
| 675 | |
| 676 | str++; |
| 677 | break; |
| 678 | } |
| 679 | else if (*str == '\0') |
| 680 | return partly_match; |
| 681 | } |
| 682 | |
| 683 | if (*str == '\0') |
| 684 | return partly_match; |
| 685 | |
| 686 | str++; |
| 687 | } |
| 688 | |
| 689 | sp = str; |
| 690 | while (*str != '\0') |
| 691 | { |
| 692 | if (!isdigit ((int) *str)) |
| 693 | return no_match; |
| 694 | |
| 695 | str++; |
| 696 | } |
| 697 | |
| 698 | if (atoi (sp) > 32) |
| 699 | return no_match; |
| 700 | |
| 701 | return exact_match; |
| 702 | } |
| 703 | |
| 704 | #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%" |
| 705 | #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/" |
| 706 | #define STATE_START 1 |
| 707 | #define STATE_COLON 2 |
| 708 | #define STATE_DOUBLE 3 |
| 709 | #define STATE_ADDR 4 |
| 710 | #define STATE_DOT 5 |
| 711 | #define STATE_SLASH 6 |
| 712 | #define STATE_MASK 7 |
| 713 | |
| 714 | enum match_type |
| 715 | cmd_ipv6_match (char *str) |
| 716 | { |
| 717 | int state = STATE_START; |
| 718 | int colons = 0, nums = 0, double_colon = 0; |
| 719 | char *sp = NULL; |
| 720 | |
| 721 | if (str == NULL) |
| 722 | return partly_match; |
| 723 | |
| 724 | if (strspn (str, IPV6_ADDR_STR) != strlen (str)) |
| 725 | return no_match; |
| 726 | |
| 727 | while (*str != '\0') |
| 728 | { |
| 729 | switch (state) |
| 730 | { |
| 731 | case STATE_START: |
| 732 | if (*str == ':') |
| 733 | { |
| 734 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
| 735 | return no_match; |
| 736 | colons--; |
| 737 | state = STATE_COLON; |
| 738 | } |
| 739 | else |
| 740 | { |
| 741 | sp = str; |
| 742 | state = STATE_ADDR; |
| 743 | } |
| 744 | |
| 745 | continue; |
| 746 | case STATE_COLON: |
| 747 | colons++; |
| 748 | if (*(str + 1) == ':') |
| 749 | state = STATE_DOUBLE; |
| 750 | else |
| 751 | { |
| 752 | sp = str + 1; |
| 753 | state = STATE_ADDR; |
| 754 | } |
| 755 | break; |
| 756 | case STATE_DOUBLE: |
| 757 | if (double_colon) |
| 758 | return no_match; |
| 759 | |
| 760 | if (*(str + 1) == ':') |
| 761 | return no_match; |
| 762 | else |
| 763 | { |
| 764 | if (*(str + 1) != '\0') |
| 765 | colons++; |
| 766 | sp = str + 1; |
| 767 | state = STATE_ADDR; |
| 768 | } |
| 769 | |
| 770 | double_colon++; |
| 771 | nums++; |
| 772 | break; |
| 773 | case STATE_ADDR: |
| 774 | if (*(str + 1) == ':' || *(str + 1) == '\0') |
| 775 | { |
| 776 | if (str - sp > 3) |
| 777 | return no_match; |
| 778 | |
| 779 | nums++; |
| 780 | state = STATE_COLON; |
| 781 | } |
| 782 | if (*(str + 1) == '.') |
| 783 | state = STATE_DOT; |
| 784 | break; |
| 785 | case STATE_DOT: |
| 786 | state = STATE_ADDR; |
| 787 | break; |
| 788 | default: |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | if (nums > 8) |
| 793 | return no_match; |
| 794 | |
| 795 | if (colons > 7) |
| 796 | return no_match; |
| 797 | |
| 798 | str++; |
| 799 | } |
| 800 | |
| 801 | #if 0 |
| 802 | if (nums < 11) |
| 803 | return partly_match; |
| 804 | #endif /* 0 */ |
| 805 | |
| 806 | return exact_match; |
| 807 | } |
| 808 | |
| 809 | enum match_type |
| 810 | cmd_ipv6_prefix_match (char *str) |
| 811 | { |
| 812 | int state = STATE_START; |
| 813 | int colons = 0, nums = 0, double_colon = 0; |
| 814 | int mask; |
| 815 | char *sp = NULL; |
| 816 | char *endptr = NULL; |
| 817 | |
| 818 | if (str == NULL) |
| 819 | return partly_match; |
| 820 | |
| 821 | if (strspn (str, IPV6_PREFIX_STR) != strlen (str)) |
| 822 | return no_match; |
| 823 | |
| 824 | while (*str != '\0' && state != STATE_MASK) |
| 825 | { |
| 826 | switch (state) |
| 827 | { |
| 828 | case STATE_START: |
| 829 | if (*str == ':') |
| 830 | { |
| 831 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
| 832 | return no_match; |
| 833 | colons--; |
| 834 | state = STATE_COLON; |
| 835 | } |
| 836 | else |
| 837 | { |
| 838 | sp = str; |
| 839 | state = STATE_ADDR; |
| 840 | } |
| 841 | |
| 842 | continue; |
| 843 | case STATE_COLON: |
| 844 | colons++; |
| 845 | if (*(str + 1) == '/') |
| 846 | return no_match; |
| 847 | else if (*(str + 1) == ':') |
| 848 | state = STATE_DOUBLE; |
| 849 | else |
| 850 | { |
| 851 | sp = str + 1; |
| 852 | state = STATE_ADDR; |
| 853 | } |
| 854 | break; |
| 855 | case STATE_DOUBLE: |
| 856 | if (double_colon) |
| 857 | return no_match; |
| 858 | |
| 859 | if (*(str + 1) == ':') |
| 860 | return no_match; |
| 861 | else |
| 862 | { |
| 863 | if (*(str + 1) != '\0' && *(str + 1) != '/') |
| 864 | colons++; |
| 865 | sp = str + 1; |
| 866 | |
| 867 | if (*(str + 1) == '/') |
| 868 | state = STATE_SLASH; |
| 869 | else |
| 870 | state = STATE_ADDR; |
| 871 | } |
| 872 | |
| 873 | double_colon++; |
| 874 | nums += 1; |
| 875 | break; |
| 876 | case STATE_ADDR: |
| 877 | if (*(str + 1) == ':' || *(str + 1) == '.' |
| 878 | || *(str + 1) == '\0' || *(str + 1) == '/') |
| 879 | { |
| 880 | if (str - sp > 3) |
| 881 | return no_match; |
| 882 | |
| 883 | for (; sp <= str; sp++) |
| 884 | if (*sp == '/') |
| 885 | return no_match; |
| 886 | |
| 887 | nums++; |
| 888 | |
| 889 | if (*(str + 1) == ':') |
| 890 | state = STATE_COLON; |
| 891 | else if (*(str + 1) == '.') |
| 892 | state = STATE_DOT; |
| 893 | else if (*(str + 1) == '/') |
| 894 | state = STATE_SLASH; |
| 895 | } |
| 896 | break; |
| 897 | case STATE_DOT: |
| 898 | state = STATE_ADDR; |
| 899 | break; |
| 900 | case STATE_SLASH: |
| 901 | if (*(str + 1) == '\0') |
| 902 | return partly_match; |
| 903 | |
| 904 | state = STATE_MASK; |
| 905 | break; |
| 906 | default: |
| 907 | break; |
| 908 | } |
| 909 | |
| 910 | if (nums > 11) |
| 911 | return no_match; |
| 912 | |
| 913 | if (colons > 7) |
| 914 | return no_match; |
| 915 | |
| 916 | str++; |
| 917 | } |
| 918 | |
| 919 | if (state < STATE_MASK) |
| 920 | return partly_match; |
| 921 | |
| 922 | mask = strtol (str, &endptr, 10); |
| 923 | if (*endptr != '\0') |
| 924 | return no_match; |
| 925 | |
| 926 | if (mask < 0 || mask > 128) |
| 927 | return no_match; |
| 928 | |
| 929 | /* I don't know why mask < 13 makes command match partly. |
| 930 | Forgive me to make this comments. I Want to set static default route |
| 931 | because of lack of function to originate default in ospf6d; sorry |
| 932 | yasu |
| 933 | if (mask < 13) |
| 934 | return partly_match; |
| 935 | */ |
| 936 | |
| 937 | return exact_match; |
| 938 | } |
| 939 | |
| 940 | #define DECIMAL_STRLEN_MAX 10 |
| 941 | |
| 942 | int |
| 943 | cmd_range_match (char *range, char *str) |
| 944 | { |
| 945 | char *p; |
| 946 | char buf[DECIMAL_STRLEN_MAX + 1]; |
| 947 | char *endptr = NULL; |
| 948 | unsigned long min, max, val; |
| 949 | |
| 950 | if (str == NULL) |
| 951 | return 1; |
| 952 | |
| 953 | val = strtoul (str, &endptr, 10); |
| 954 | if (*endptr != '\0') |
| 955 | return 0; |
| 956 | |
| 957 | range++; |
| 958 | p = strchr (range, '-'); |
| 959 | if (p == NULL) |
| 960 | return 0; |
| 961 | if (p - range > DECIMAL_STRLEN_MAX) |
| 962 | return 0; |
| 963 | strncpy (buf, range, p - range); |
| 964 | buf[p - range] = '\0'; |
| 965 | min = strtoul (buf, &endptr, 10); |
| 966 | if (*endptr != '\0') |
| 967 | return 0; |
| 968 | |
| 969 | range = p + 1; |
| 970 | p = strchr (range, '>'); |
| 971 | if (p == NULL) |
| 972 | return 0; |
| 973 | if (p - range > DECIMAL_STRLEN_MAX) |
| 974 | return 0; |
| 975 | strncpy (buf, range, p - range); |
| 976 | buf[p - range] = '\0'; |
| 977 | max = strtoul (buf, &endptr, 10); |
| 978 | if (*endptr != '\0') |
| 979 | return 0; |
| 980 | |
| 981 | if (val < min || val > max) |
| 982 | return 0; |
| 983 | |
| 984 | return 1; |
| 985 | } |
| 986 | |
| 987 | /* Make completion match and return match type flag. */ |
| 988 | enum match_type |
| 989 | cmd_filter_by_completion (char *command, vector v, int index) |
| 990 | { |
| 991 | int i; |
| 992 | char *str; |
| 993 | struct cmd_element *cmd_element; |
| 994 | enum match_type match_type; |
| 995 | vector descvec; |
| 996 | struct desc *desc; |
| 997 | |
| 998 | match_type = no_match; |
| 999 | |
| 1000 | /* If command and cmd_element string does not match set NULL to vector */ |
| 1001 | for (i = 0; i < vector_max (v); i++) |
| 1002 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1003 | { |
| 1004 | if (index >= vector_max (cmd_element->strvec)) |
| 1005 | vector_slot (v, i) = NULL; |
| 1006 | else |
| 1007 | { |
| 1008 | int j; |
| 1009 | int matched = 0; |
| 1010 | |
| 1011 | descvec = vector_slot (cmd_element->strvec, index); |
| 1012 | |
| 1013 | for (j = 0; j < vector_max (descvec); j++) |
| 1014 | { |
| 1015 | desc = vector_slot (descvec, j); |
| 1016 | str = desc->cmd; |
| 1017 | |
| 1018 | if (CMD_VARARG (str)) |
| 1019 | { |
| 1020 | if (match_type < vararg_match) |
| 1021 | match_type = vararg_match; |
| 1022 | matched++; |
| 1023 | } |
| 1024 | else if (CMD_RANGE (str)) |
| 1025 | { |
| 1026 | if (cmd_range_match (str, command)) |
| 1027 | { |
| 1028 | if (match_type < range_match) |
| 1029 | match_type = range_match; |
| 1030 | |
| 1031 | matched++; |
| 1032 | } |
| 1033 | } |
| 1034 | else if (CMD_IPV6 (str)) |
| 1035 | { |
| 1036 | if (cmd_ipv6_match (command)) |
| 1037 | { |
| 1038 | if (match_type < ipv6_match) |
| 1039 | match_type = ipv6_match; |
| 1040 | |
| 1041 | matched++; |
| 1042 | } |
| 1043 | } |
| 1044 | else if (CMD_IPV6_PREFIX (str)) |
| 1045 | { |
| 1046 | if (cmd_ipv6_prefix_match (command)) |
| 1047 | { |
| 1048 | if (match_type < ipv6_prefix_match) |
| 1049 | match_type = ipv6_prefix_match; |
| 1050 | |
| 1051 | matched++; |
| 1052 | } |
| 1053 | } |
| 1054 | else if (CMD_IPV4 (str)) |
| 1055 | { |
| 1056 | if (cmd_ipv4_match (command)) |
| 1057 | { |
| 1058 | if (match_type < ipv4_match) |
| 1059 | match_type = ipv4_match; |
| 1060 | |
| 1061 | matched++; |
| 1062 | } |
| 1063 | } |
| 1064 | else if (CMD_IPV4_PREFIX (str)) |
| 1065 | { |
| 1066 | if (cmd_ipv4_prefix_match (command)) |
| 1067 | { |
| 1068 | if (match_type < ipv4_prefix_match) |
| 1069 | match_type = ipv4_prefix_match; |
| 1070 | matched++; |
| 1071 | } |
| 1072 | } |
| 1073 | else |
| 1074 | /* Check is this point's argument optional ? */ |
| 1075 | if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1076 | { |
| 1077 | if (match_type < extend_match) |
| 1078 | match_type = extend_match; |
| 1079 | matched++; |
| 1080 | } |
| 1081 | else if (strncmp (command, str, strlen (command)) == 0) |
| 1082 | { |
| 1083 | if (strcmp (command, str) == 0) |
| 1084 | match_type = exact_match; |
| 1085 | else |
| 1086 | { |
| 1087 | if (match_type < partly_match) |
| 1088 | match_type = partly_match; |
| 1089 | } |
| 1090 | matched++; |
| 1091 | } |
| 1092 | } |
| 1093 | if (! matched) |
| 1094 | vector_slot (v, i) = NULL; |
| 1095 | } |
| 1096 | } |
| 1097 | return match_type; |
| 1098 | } |
| 1099 | |
| 1100 | /* Filter vector by command character with index. */ |
| 1101 | enum match_type |
| 1102 | cmd_filter_by_string (char *command, vector v, int index) |
| 1103 | { |
| 1104 | int i; |
| 1105 | char *str; |
| 1106 | struct cmd_element *cmd_element; |
| 1107 | enum match_type match_type; |
| 1108 | vector descvec; |
| 1109 | struct desc *desc; |
| 1110 | |
| 1111 | match_type = no_match; |
| 1112 | |
| 1113 | /* If command and cmd_element string does not match set NULL to vector */ |
| 1114 | for (i = 0; i < vector_max (v); i++) |
| 1115 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1116 | { |
| 1117 | /* If given index is bigger than max string vector of command, |
| 1118 | set NULL*/ |
| 1119 | if (index >= vector_max (cmd_element->strvec)) |
| 1120 | vector_slot (v, i) = NULL; |
| 1121 | else |
| 1122 | { |
| 1123 | int j; |
| 1124 | int matched = 0; |
| 1125 | |
| 1126 | descvec = vector_slot (cmd_element->strvec, index); |
| 1127 | |
| 1128 | for (j = 0; j < vector_max (descvec); j++) |
| 1129 | { |
| 1130 | desc = vector_slot (descvec, j); |
| 1131 | str = desc->cmd; |
| 1132 | |
| 1133 | if (CMD_VARARG (str)) |
| 1134 | { |
| 1135 | if (match_type < vararg_match) |
| 1136 | match_type = vararg_match; |
| 1137 | matched++; |
| 1138 | } |
| 1139 | else if (CMD_RANGE (str)) |
| 1140 | { |
| 1141 | if (cmd_range_match (str, command)) |
| 1142 | { |
| 1143 | if (match_type < range_match) |
| 1144 | match_type = range_match; |
| 1145 | matched++; |
| 1146 | } |
| 1147 | } |
| 1148 | else if (CMD_IPV6 (str)) |
| 1149 | { |
| 1150 | if (cmd_ipv6_match (command) == exact_match) |
| 1151 | { |
| 1152 | if (match_type < ipv6_match) |
| 1153 | match_type = ipv6_match; |
| 1154 | matched++; |
| 1155 | } |
| 1156 | } |
| 1157 | else if (CMD_IPV6_PREFIX (str)) |
| 1158 | { |
| 1159 | if (cmd_ipv6_prefix_match (command) == exact_match) |
| 1160 | { |
| 1161 | if (match_type < ipv6_prefix_match) |
| 1162 | match_type = ipv6_prefix_match; |
| 1163 | matched++; |
| 1164 | } |
| 1165 | } |
| 1166 | else if (CMD_IPV4 (str)) |
| 1167 | { |
| 1168 | if (cmd_ipv4_match (command) == exact_match) |
| 1169 | { |
| 1170 | if (match_type < ipv4_match) |
| 1171 | match_type = ipv4_match; |
| 1172 | matched++; |
| 1173 | } |
| 1174 | } |
| 1175 | else if (CMD_IPV4_PREFIX (str)) |
| 1176 | { |
| 1177 | if (cmd_ipv4_prefix_match (command) == exact_match) |
| 1178 | { |
| 1179 | if (match_type < ipv4_prefix_match) |
| 1180 | match_type = ipv4_prefix_match; |
| 1181 | matched++; |
| 1182 | } |
| 1183 | } |
| 1184 | else if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1185 | { |
| 1186 | if (match_type < extend_match) |
| 1187 | match_type = extend_match; |
| 1188 | matched++; |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | if (strcmp (command, str) == 0) |
| 1193 | { |
| 1194 | match_type = exact_match; |
| 1195 | matched++; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | if (! matched) |
| 1200 | vector_slot (v, i) = NULL; |
| 1201 | } |
| 1202 | } |
| 1203 | return match_type; |
| 1204 | } |
| 1205 | |
| 1206 | /* Check ambiguous match */ |
| 1207 | int |
| 1208 | is_cmd_ambiguous (char *command, vector v, int index, enum match_type type) |
| 1209 | { |
| 1210 | int i; |
| 1211 | int j; |
| 1212 | char *str = NULL; |
| 1213 | struct cmd_element *cmd_element; |
| 1214 | char *matched = NULL; |
| 1215 | vector descvec; |
| 1216 | struct desc *desc; |
| 1217 | |
| 1218 | for (i = 0; i < vector_max (v); i++) |
| 1219 | if ((cmd_element = vector_slot (v, i)) != NULL) |
| 1220 | { |
| 1221 | int match = 0; |
| 1222 | |
| 1223 | descvec = vector_slot (cmd_element->strvec, index); |
| 1224 | |
| 1225 | for (j = 0; j < vector_max (descvec); j++) |
| 1226 | { |
| 1227 | enum match_type ret; |
| 1228 | |
| 1229 | desc = vector_slot (descvec, j); |
| 1230 | str = desc->cmd; |
| 1231 | |
| 1232 | switch (type) |
| 1233 | { |
| 1234 | case exact_match: |
| 1235 | if (! (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1236 | && strcmp (command, str) == 0) |
| 1237 | match++; |
| 1238 | break; |
| 1239 | case partly_match: |
| 1240 | if (! (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1241 | && strncmp (command, str, strlen (command)) == 0) |
| 1242 | { |
| 1243 | if (matched && strcmp (matched, str) != 0) |
| 1244 | return 1; /* There is ambiguous match. */ |
| 1245 | else |
| 1246 | matched = str; |
| 1247 | match++; |
| 1248 | } |
| 1249 | break; |
| 1250 | case range_match: |
| 1251 | if (cmd_range_match (str, command)) |
| 1252 | { |
| 1253 | if (matched && strcmp (matched, str) != 0) |
| 1254 | return 1; |
| 1255 | else |
| 1256 | matched = str; |
| 1257 | match++; |
| 1258 | } |
| 1259 | break; |
| 1260 | case ipv6_match: |
| 1261 | if (CMD_IPV6 (str)) |
| 1262 | match++; |
| 1263 | break; |
| 1264 | case ipv6_prefix_match: |
| 1265 | if ((ret = cmd_ipv6_prefix_match (command)) != no_match) |
| 1266 | { |
| 1267 | if (ret == partly_match) |
| 1268 | return 2; /* There is incomplete match. */ |
| 1269 | |
| 1270 | match++; |
| 1271 | } |
| 1272 | break; |
| 1273 | case ipv4_match: |
| 1274 | if (CMD_IPV4 (str)) |
| 1275 | match++; |
| 1276 | break; |
| 1277 | case ipv4_prefix_match: |
| 1278 | if ((ret = cmd_ipv4_prefix_match (command)) != no_match) |
| 1279 | { |
| 1280 | if (ret == partly_match) |
| 1281 | return 2; /* There is incomplete match. */ |
| 1282 | |
| 1283 | match++; |
| 1284 | } |
| 1285 | break; |
| 1286 | case extend_match: |
| 1287 | if (CMD_OPTION (str) || CMD_VARIABLE (str)) |
| 1288 | match++; |
| 1289 | break; |
| 1290 | case no_match: |
| 1291 | default: |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | if (! match) |
| 1296 | vector_slot (v, i) = NULL; |
| 1297 | } |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1302 | char * |
| 1303 | cmd_entry_function (char *src, char *dst) |
| 1304 | { |
| 1305 | /* Skip variable arguments. */ |
| 1306 | if (CMD_OPTION (dst) || CMD_VARIABLE (dst) || CMD_VARARG (dst) || |
| 1307 | CMD_IPV4 (dst) || CMD_IPV4_PREFIX (dst) || CMD_RANGE (dst)) |
| 1308 | return NULL; |
| 1309 | |
| 1310 | /* In case of 'command \t', given src is NULL string. */ |
| 1311 | if (src == NULL) |
| 1312 | return dst; |
| 1313 | |
| 1314 | /* Matched with input string. */ |
| 1315 | if (strncmp (src, dst, strlen (src)) == 0) |
| 1316 | return dst; |
| 1317 | |
| 1318 | return NULL; |
| 1319 | } |
| 1320 | |
| 1321 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1322 | /* This version will return the dst string always if it is |
| 1323 | CMD_VARIABLE for '?' key processing */ |
| 1324 | char * |
| 1325 | cmd_entry_function_desc (char *src, char *dst) |
| 1326 | { |
| 1327 | if (CMD_VARARG (dst)) |
| 1328 | return dst; |
| 1329 | |
| 1330 | if (CMD_RANGE (dst)) |
| 1331 | { |
| 1332 | if (cmd_range_match (dst, src)) |
| 1333 | return dst; |
| 1334 | else |
| 1335 | return NULL; |
| 1336 | } |
| 1337 | |
| 1338 | if (CMD_IPV6 (dst)) |
| 1339 | { |
| 1340 | if (cmd_ipv6_match (src)) |
| 1341 | return dst; |
| 1342 | else |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | |
| 1346 | if (CMD_IPV6_PREFIX (dst)) |
| 1347 | { |
| 1348 | if (cmd_ipv6_prefix_match (src)) |
| 1349 | return dst; |
| 1350 | else |
| 1351 | return NULL; |
| 1352 | } |
| 1353 | |
| 1354 | if (CMD_IPV4 (dst)) |
| 1355 | { |
| 1356 | if (cmd_ipv4_match (src)) |
| 1357 | return dst; |
| 1358 | else |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
| 1362 | if (CMD_IPV4_PREFIX (dst)) |
| 1363 | { |
| 1364 | if (cmd_ipv4_prefix_match (src)) |
| 1365 | return dst; |
| 1366 | else |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | |
| 1370 | /* Optional or variable commands always match on '?' */ |
| 1371 | if (CMD_OPTION (dst) || CMD_VARIABLE (dst)) |
| 1372 | return dst; |
| 1373 | |
| 1374 | /* In case of 'command \t', given src is NULL string. */ |
| 1375 | if (src == NULL) |
| 1376 | return dst; |
| 1377 | |
| 1378 | if (strncmp (src, dst, strlen (src)) == 0) |
| 1379 | return dst; |
| 1380 | else |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | |
| 1384 | /* Check same string element existence. If it isn't there return |
| 1385 | 1. */ |
| 1386 | int |
| 1387 | cmd_unique_string (vector v, char *str) |
| 1388 | { |
| 1389 | int i; |
| 1390 | char *match; |
| 1391 | |
| 1392 | for (i = 0; i < vector_max (v); i++) |
| 1393 | if ((match = vector_slot (v, i)) != NULL) |
| 1394 | if (strcmp (match, str) == 0) |
| 1395 | return 0; |
| 1396 | return 1; |
| 1397 | } |
| 1398 | |
| 1399 | /* Compare string to description vector. If there is same string |
| 1400 | return 1 else return 0. */ |
| 1401 | int |
| 1402 | desc_unique_string (vector v, char *str) |
| 1403 | { |
| 1404 | int i; |
| 1405 | struct desc *desc; |
| 1406 | |
| 1407 | for (i = 0; i < vector_max (v); i++) |
| 1408 | if ((desc = vector_slot (v, i)) != NULL) |
| 1409 | if (strcmp (desc->cmd, str) == 0) |
| 1410 | return 1; |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | /* '?' describe command support. */ |
| 1415 | vector |
| 1416 | cmd_describe_command (vector vline, struct vty *vty, int *status) |
| 1417 | { |
| 1418 | int i; |
| 1419 | vector cmd_vector; |
| 1420 | #define INIT_MATCHVEC_SIZE 10 |
| 1421 | vector matchvec; |
| 1422 | struct cmd_element *cmd_element; |
| 1423 | int index; |
| 1424 | static struct desc desc_cr = { "<cr>", "" }; |
| 1425 | |
| 1426 | /* Set index. */ |
| 1427 | index = vector_max (vline) - 1; |
| 1428 | |
| 1429 | /* Make copy vector of current node's command vector. */ |
| 1430 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1431 | |
| 1432 | /* Prepare match vector */ |
| 1433 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1434 | |
| 1435 | /* Filter commands. */ |
| 1436 | for (i = 0; i < index; i++) |
| 1437 | { |
| 1438 | enum match_type match; |
| 1439 | char *command; |
| 1440 | int ret; |
| 1441 | |
| 1442 | command = vector_slot (vline, i); |
| 1443 | |
| 1444 | match = cmd_filter_by_completion (command, cmd_vector, i); |
| 1445 | |
| 1446 | if (match == vararg_match) |
| 1447 | { |
| 1448 | struct cmd_element *cmd_element; |
| 1449 | vector descvec; |
| 1450 | int j, k; |
| 1451 | |
| 1452 | for (j = 0; j < vector_max (cmd_vector); j++) |
| 1453 | if ((cmd_element = vector_slot (cmd_vector, j)) != NULL) |
| 1454 | { |
| 1455 | descvec = vector_slot (cmd_element->strvec, |
| 1456 | vector_max (cmd_element->strvec) - 1); |
| 1457 | for (k = 0; k < vector_max (descvec); k++) |
| 1458 | { |
| 1459 | struct desc *desc = vector_slot (descvec, k); |
| 1460 | vector_set (matchvec, desc); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | vector_set (matchvec, &desc_cr); |
| 1465 | |
| 1466 | vector_free (cmd_vector); |
| 1467 | |
| 1468 | return matchvec; |
| 1469 | } |
| 1470 | |
| 1471 | if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1) |
| 1472 | { |
| 1473 | vector_free (cmd_vector); |
| 1474 | *status = CMD_ERR_AMBIGUOUS; |
| 1475 | return NULL; |
| 1476 | } |
| 1477 | else if (ret == 2) |
| 1478 | { |
| 1479 | vector_free (cmd_vector); |
| 1480 | *status = CMD_ERR_NO_MATCH; |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* Prepare match vector */ |
| 1486 | /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */ |
| 1487 | |
| 1488 | /* Make description vector. */ |
| 1489 | for (i = 0; i < vector_max (cmd_vector); i++) |
| 1490 | if ((cmd_element = vector_slot (cmd_vector, i)) != NULL) |
| 1491 | { |
| 1492 | char *string = NULL; |
| 1493 | vector strvec = cmd_element->strvec; |
| 1494 | |
| 1495 | if (index > vector_max (strvec)) |
| 1496 | vector_slot (cmd_vector, i) = NULL; |
| 1497 | else |
| 1498 | { |
| 1499 | /* Check is command is completed. */ |
| 1500 | if (index == vector_max (strvec)) |
| 1501 | { |
| 1502 | string = "<cr>"; |
| 1503 | if (! desc_unique_string (matchvec, string)) |
| 1504 | vector_set (matchvec, &desc_cr); |
| 1505 | } |
| 1506 | else |
| 1507 | { |
| 1508 | int j; |
| 1509 | vector descvec = vector_slot (strvec, index); |
| 1510 | struct desc *desc; |
| 1511 | |
| 1512 | for (j = 0; j < vector_max (descvec); j++) |
| 1513 | { |
| 1514 | desc = vector_slot (descvec, j); |
| 1515 | string = cmd_entry_function_desc (vector_slot (vline, index), desc->cmd); |
| 1516 | if (string) |
| 1517 | { |
| 1518 | /* Uniqueness check */ |
| 1519 | if (! desc_unique_string (matchvec, string)) |
| 1520 | vector_set (matchvec, desc); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | vector_free (cmd_vector); |
| 1527 | |
| 1528 | if (vector_slot (matchvec, 0) == NULL) |
| 1529 | { |
| 1530 | vector_free (matchvec); |
| 1531 | *status= CMD_ERR_NO_MATCH; |
| 1532 | } |
| 1533 | else |
| 1534 | *status = CMD_SUCCESS; |
| 1535 | |
| 1536 | return matchvec; |
| 1537 | } |
| 1538 | |
| 1539 | /* Check LCD of matched command. */ |
| 1540 | int |
| 1541 | cmd_lcd (char **matched) |
| 1542 | { |
| 1543 | int i; |
| 1544 | int j; |
| 1545 | int lcd = -1; |
| 1546 | char *s1, *s2; |
| 1547 | char c1, c2; |
| 1548 | |
| 1549 | if (matched[0] == NULL || matched[1] == NULL) |
| 1550 | return 0; |
| 1551 | |
| 1552 | for (i = 1; matched[i] != NULL; i++) |
| 1553 | { |
| 1554 | s1 = matched[i - 1]; |
| 1555 | s2 = matched[i]; |
| 1556 | |
| 1557 | for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++) |
| 1558 | if (c1 != c2) |
| 1559 | break; |
| 1560 | |
| 1561 | if (lcd < 0) |
| 1562 | lcd = j; |
| 1563 | else |
| 1564 | { |
| 1565 | if (lcd > j) |
| 1566 | lcd = j; |
| 1567 | } |
| 1568 | } |
| 1569 | return lcd; |
| 1570 | } |
| 1571 | |
| 1572 | /* Command line completion support. */ |
| 1573 | char ** |
| 1574 | cmd_complete_command (vector vline, struct vty *vty, int *status) |
| 1575 | { |
| 1576 | int i; |
| 1577 | vector cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1578 | #define INIT_MATCHVEC_SIZE 10 |
| 1579 | vector matchvec; |
| 1580 | struct cmd_element *cmd_element; |
| 1581 | int index = vector_max (vline) - 1; |
| 1582 | char **match_str; |
| 1583 | struct desc *desc; |
| 1584 | vector descvec; |
| 1585 | char *command; |
| 1586 | int lcd; |
| 1587 | |
| 1588 | /* First, filter by preceeding command string */ |
| 1589 | for (i = 0; i < index; i++) |
| 1590 | { |
| 1591 | enum match_type match; |
| 1592 | int ret; |
| 1593 | |
| 1594 | command = vector_slot (vline, i); |
| 1595 | |
| 1596 | /* First try completion match, if there is exactly match return 1 */ |
| 1597 | match = cmd_filter_by_completion (command, cmd_vector, i); |
| 1598 | |
| 1599 | /* If there is exact match then filter ambiguous match else check |
| 1600 | ambiguousness. */ |
| 1601 | if ((ret = is_cmd_ambiguous (command, cmd_vector, i, match)) == 1) |
| 1602 | { |
| 1603 | vector_free (cmd_vector); |
| 1604 | *status = CMD_ERR_AMBIGUOUS; |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | /* |
| 1608 | else if (ret == 2) |
| 1609 | { |
| 1610 | vector_free (cmd_vector); |
| 1611 | *status = CMD_ERR_NO_MATCH; |
| 1612 | return NULL; |
| 1613 | } |
| 1614 | */ |
| 1615 | } |
| 1616 | |
| 1617 | /* Prepare match vector. */ |
| 1618 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1619 | |
| 1620 | /* Now we got into completion */ |
| 1621 | for (i = 0; i < vector_max (cmd_vector); i++) |
| 1622 | if ((cmd_element = vector_slot (cmd_vector, i)) != NULL) |
| 1623 | { |
| 1624 | char *string; |
| 1625 | vector strvec = cmd_element->strvec; |
| 1626 | |
| 1627 | /* Check field length */ |
| 1628 | if (index >= vector_max (strvec)) |
| 1629 | vector_slot (cmd_vector, i) = NULL; |
| 1630 | else |
| 1631 | { |
| 1632 | int j; |
| 1633 | |
| 1634 | descvec = vector_slot (strvec, index); |
| 1635 | for (j = 0; j < vector_max (descvec); j++) |
| 1636 | { |
| 1637 | desc = vector_slot (descvec, j); |
| 1638 | |
| 1639 | if ((string = cmd_entry_function (vector_slot (vline, index), |
| 1640 | desc->cmd))) |
| 1641 | if (cmd_unique_string (matchvec, string)) |
| 1642 | vector_set (matchvec, XSTRDUP (MTYPE_TMP, string)); |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | /* We don't need cmd_vector any more. */ |
| 1648 | vector_free (cmd_vector); |
| 1649 | |
| 1650 | /* No matched command */ |
| 1651 | if (vector_slot (matchvec, 0) == NULL) |
| 1652 | { |
| 1653 | vector_free (matchvec); |
| 1654 | |
| 1655 | /* In case of 'command \t' pattern. Do you need '?' command at |
| 1656 | the end of the line. */ |
| 1657 | if (vector_slot (vline, index) == '\0') |
| 1658 | *status = CMD_ERR_NOTHING_TODO; |
| 1659 | else |
| 1660 | *status = CMD_ERR_NO_MATCH; |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | /* Only one matched */ |
| 1665 | if (vector_slot (matchvec, 1) == NULL) |
| 1666 | { |
| 1667 | match_str = (char **) matchvec->index; |
| 1668 | vector_only_wrapper_free (matchvec); |
| 1669 | *status = CMD_COMPLETE_FULL_MATCH; |
| 1670 | return match_str; |
| 1671 | } |
| 1672 | /* Make it sure last element is NULL. */ |
| 1673 | vector_set (matchvec, NULL); |
| 1674 | |
| 1675 | /* Check LCD of matched strings. */ |
| 1676 | if (vector_slot (vline, index) != NULL) |
| 1677 | { |
| 1678 | lcd = cmd_lcd ((char **) matchvec->index); |
| 1679 | |
| 1680 | if (lcd) |
| 1681 | { |
| 1682 | int len = strlen (vector_slot (vline, index)); |
| 1683 | |
| 1684 | if (len < lcd) |
| 1685 | { |
| 1686 | char *lcdstr; |
| 1687 | |
| 1688 | lcdstr = XMALLOC (MTYPE_TMP, lcd + 1); |
| 1689 | memcpy (lcdstr, matchvec->index[0], lcd); |
| 1690 | lcdstr[lcd] = '\0'; |
| 1691 | |
| 1692 | /* match_str = (char **) &lcdstr; */ |
| 1693 | |
| 1694 | /* Free matchvec. */ |
| 1695 | for (i = 0; i < vector_max (matchvec); i++) |
| 1696 | { |
| 1697 | if (vector_slot (matchvec, i)) |
| 1698 | XFREE (MTYPE_TMP, vector_slot (matchvec, i)); |
| 1699 | } |
| 1700 | vector_free (matchvec); |
| 1701 | |
| 1702 | /* Make new matchvec. */ |
| 1703 | matchvec = vector_init (INIT_MATCHVEC_SIZE); |
| 1704 | vector_set (matchvec, lcdstr); |
| 1705 | match_str = (char **) matchvec->index; |
| 1706 | vector_only_wrapper_free (matchvec); |
| 1707 | |
| 1708 | *status = CMD_COMPLETE_MATCH; |
| 1709 | return match_str; |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | match_str = (char **) matchvec->index; |
| 1715 | vector_only_wrapper_free (matchvec); |
| 1716 | *status = CMD_COMPLETE_LIST_MATCH; |
| 1717 | return match_str; |
| 1718 | } |
| 1719 | |
| 1720 | /* Execute command by argument vline vector. */ |
| 1721 | int |
| 1722 | cmd_execute_command (vector vline, struct vty *vty, struct cmd_element **cmd) |
| 1723 | { |
| 1724 | int i; |
| 1725 | int index; |
| 1726 | vector cmd_vector; |
| 1727 | struct cmd_element *cmd_element; |
| 1728 | struct cmd_element *matched_element; |
| 1729 | unsigned int matched_count, incomplete_count; |
| 1730 | int argc; |
| 1731 | char *argv[CMD_ARGC_MAX]; |
| 1732 | enum match_type match = 0; |
| 1733 | int varflag; |
| 1734 | char *command; |
| 1735 | |
| 1736 | /* Make copy of command elements. */ |
| 1737 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1738 | |
| 1739 | for (index = 0; index < vector_max (vline); index++) |
| 1740 | { |
| 1741 | int ret; |
| 1742 | |
| 1743 | command = vector_slot (vline, index); |
| 1744 | |
| 1745 | match = cmd_filter_by_completion (command, cmd_vector, index); |
| 1746 | |
| 1747 | if (match == vararg_match) |
| 1748 | break; |
| 1749 | |
| 1750 | ret = is_cmd_ambiguous (command, cmd_vector, index, match); |
| 1751 | |
| 1752 | if (ret == 1) |
| 1753 | { |
| 1754 | vector_free (cmd_vector); |
| 1755 | return CMD_ERR_AMBIGUOUS; |
| 1756 | } |
| 1757 | else if (ret == 2) |
| 1758 | { |
| 1759 | vector_free (cmd_vector); |
| 1760 | return CMD_ERR_NO_MATCH; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | /* Check matched count. */ |
| 1765 | matched_element = NULL; |
| 1766 | matched_count = 0; |
| 1767 | incomplete_count = 0; |
| 1768 | |
| 1769 | for (i = 0; i < vector_max (cmd_vector); i++) |
| 1770 | if (vector_slot (cmd_vector,i) != NULL) |
| 1771 | { |
| 1772 | cmd_element = vector_slot (cmd_vector,i); |
| 1773 | |
| 1774 | if (match == vararg_match || index >= cmd_element->cmdsize) |
| 1775 | { |
| 1776 | matched_element = cmd_element; |
| 1777 | #if 0 |
| 1778 | printf ("DEBUG: %s\n", cmd_element->string); |
| 1779 | #endif |
| 1780 | matched_count++; |
| 1781 | } |
| 1782 | else |
| 1783 | { |
| 1784 | incomplete_count++; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | /* Finish of using cmd_vector. */ |
| 1789 | vector_free (cmd_vector); |
| 1790 | |
| 1791 | /* To execute command, matched_count must be 1.*/ |
| 1792 | if (matched_count == 0) |
| 1793 | { |
| 1794 | if (incomplete_count) |
| 1795 | return CMD_ERR_INCOMPLETE; |
| 1796 | else |
| 1797 | return CMD_ERR_NO_MATCH; |
| 1798 | } |
| 1799 | |
| 1800 | if (matched_count > 1) |
| 1801 | return CMD_ERR_AMBIGUOUS; |
| 1802 | |
| 1803 | /* Argument treatment */ |
| 1804 | varflag = 0; |
| 1805 | argc = 0; |
| 1806 | |
| 1807 | for (i = 0; i < vector_max (vline); i++) |
| 1808 | { |
| 1809 | if (varflag) |
| 1810 | argv[argc++] = vector_slot (vline, i); |
| 1811 | else |
| 1812 | { |
| 1813 | vector descvec = vector_slot (matched_element->strvec, i); |
| 1814 | |
| 1815 | if (vector_max (descvec) == 1) |
| 1816 | { |
| 1817 | struct desc *desc = vector_slot (descvec, 0); |
| 1818 | char *str = desc->cmd; |
| 1819 | |
| 1820 | if (CMD_VARARG (str)) |
| 1821 | varflag = 1; |
| 1822 | |
| 1823 | if (varflag || CMD_VARIABLE (str) || CMD_OPTION (str)) |
| 1824 | argv[argc++] = vector_slot (vline, i); |
| 1825 | } |
| 1826 | else |
| 1827 | argv[argc++] = vector_slot (vline, i); |
| 1828 | } |
| 1829 | |
| 1830 | if (argc >= CMD_ARGC_MAX) |
| 1831 | return CMD_ERR_EXEED_ARGC_MAX; |
| 1832 | } |
| 1833 | |
| 1834 | /* For vtysh execution. */ |
| 1835 | if (cmd) |
| 1836 | *cmd = matched_element; |
| 1837 | |
| 1838 | if (matched_element->daemon) |
| 1839 | return CMD_SUCCESS_DAEMON; |
| 1840 | |
| 1841 | /* Execute matched command. */ |
| 1842 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 1843 | } |
| 1844 | |
| 1845 | /* Execute command by argument readline. */ |
| 1846 | int |
| 1847 | cmd_execute_command_strict (vector vline, struct vty *vty, |
| 1848 | struct cmd_element **cmd) |
| 1849 | { |
| 1850 | int i; |
| 1851 | int index; |
| 1852 | vector cmd_vector; |
| 1853 | struct cmd_element *cmd_element; |
| 1854 | struct cmd_element *matched_element; |
| 1855 | unsigned int matched_count, incomplete_count; |
| 1856 | int argc; |
| 1857 | char *argv[CMD_ARGC_MAX]; |
| 1858 | int varflag; |
| 1859 | enum match_type match = 0; |
| 1860 | char *command; |
| 1861 | |
| 1862 | /* Make copy of command element */ |
| 1863 | cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node)); |
| 1864 | |
| 1865 | for (index = 0; index < vector_max (vline); index++) |
| 1866 | { |
| 1867 | int ret; |
| 1868 | |
| 1869 | command = vector_slot (vline, index); |
| 1870 | |
| 1871 | match = cmd_filter_by_string (vector_slot (vline, index), |
| 1872 | cmd_vector, index); |
| 1873 | |
| 1874 | /* If command meets '.VARARG' then finish matching. */ |
| 1875 | if (match == vararg_match) |
| 1876 | break; |
| 1877 | |
| 1878 | ret = is_cmd_ambiguous (command, cmd_vector, index, match); |
| 1879 | if (ret == 1) |
| 1880 | { |
| 1881 | vector_free (cmd_vector); |
| 1882 | return CMD_ERR_AMBIGUOUS; |
| 1883 | } |
| 1884 | if (ret == 2) |
| 1885 | { |
| 1886 | vector_free (cmd_vector); |
| 1887 | return CMD_ERR_NO_MATCH; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* Check matched count. */ |
| 1892 | matched_element = NULL; |
| 1893 | matched_count = 0; |
| 1894 | incomplete_count = 0; |
| 1895 | for (i = 0; i < vector_max (cmd_vector); i++) |
| 1896 | if (vector_slot (cmd_vector,i) != NULL) |
| 1897 | { |
| 1898 | cmd_element = vector_slot (cmd_vector,i); |
| 1899 | |
| 1900 | if (match == vararg_match || index >= cmd_element->cmdsize) |
| 1901 | { |
| 1902 | matched_element = cmd_element; |
| 1903 | matched_count++; |
| 1904 | } |
| 1905 | else |
| 1906 | incomplete_count++; |
| 1907 | } |
| 1908 | |
| 1909 | /* Finish of using cmd_vector. */ |
| 1910 | vector_free (cmd_vector); |
| 1911 | |
| 1912 | /* To execute command, matched_count must be 1.*/ |
| 1913 | if (matched_count == 0) |
| 1914 | { |
| 1915 | if (incomplete_count) |
| 1916 | return CMD_ERR_INCOMPLETE; |
| 1917 | else |
| 1918 | return CMD_ERR_NO_MATCH; |
| 1919 | } |
| 1920 | |
| 1921 | if (matched_count > 1) |
| 1922 | return CMD_ERR_AMBIGUOUS; |
| 1923 | |
| 1924 | /* Argument treatment */ |
| 1925 | varflag = 0; |
| 1926 | argc = 0; |
| 1927 | |
| 1928 | for (i = 0; i < vector_max (vline); i++) |
| 1929 | { |
| 1930 | if (varflag) |
| 1931 | argv[argc++] = vector_slot (vline, i); |
| 1932 | else |
| 1933 | { |
| 1934 | vector descvec = vector_slot (matched_element->strvec, i); |
| 1935 | |
| 1936 | if (vector_max (descvec) == 1) |
| 1937 | { |
| 1938 | struct desc *desc = vector_slot (descvec, 0); |
| 1939 | char *str = desc->cmd; |
| 1940 | |
| 1941 | if (CMD_VARARG (str)) |
| 1942 | varflag = 1; |
| 1943 | |
| 1944 | if (varflag || CMD_VARIABLE (str) || CMD_OPTION (str)) |
| 1945 | argv[argc++] = vector_slot (vline, i); |
| 1946 | } |
| 1947 | else |
| 1948 | argv[argc++] = vector_slot (vline, i); |
| 1949 | } |
| 1950 | |
| 1951 | if (argc >= CMD_ARGC_MAX) |
| 1952 | return CMD_ERR_EXEED_ARGC_MAX; |
| 1953 | } |
| 1954 | |
| 1955 | /* For vtysh execution. */ |
| 1956 | if (cmd) |
| 1957 | *cmd = matched_element; |
| 1958 | |
| 1959 | if (matched_element->daemon) |
| 1960 | return CMD_SUCCESS_DAEMON; |
| 1961 | |
| 1962 | /* Now execute matched command */ |
| 1963 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 1964 | } |
| 1965 | |
| 1966 | /* Configration make from file. */ |
| 1967 | int |
| 1968 | config_from_file (struct vty *vty, FILE *fp) |
| 1969 | { |
| 1970 | int ret; |
| 1971 | vector vline; |
| 1972 | |
| 1973 | while (fgets (vty->buf, VTY_BUFSIZ, fp)) |
| 1974 | { |
| 1975 | vline = cmd_make_strvec (vty->buf); |
| 1976 | |
| 1977 | /* In case of comment line */ |
| 1978 | if (vline == NULL) |
| 1979 | continue; |
| 1980 | /* Execute configuration command : this is strict match */ |
| 1981 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 1982 | |
| 1983 | /* Try again with setting node to CONFIG_NODE */ |
| 1984 | if (ret != CMD_SUCCESS && ret != CMD_WARNING) |
| 1985 | { |
| 1986 | if (vty->node == KEYCHAIN_KEY_NODE) |
| 1987 | { |
| 1988 | vty->node = KEYCHAIN_NODE; |
| 1989 | |
| 1990 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 1991 | |
| 1992 | if (ret != CMD_SUCCESS && ret != CMD_WARNING) |
| 1993 | { |
| 1994 | vty->node = CONFIG_NODE; |
| 1995 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | { |
| 2000 | vty->node = CONFIG_NODE; |
| 2001 | ret = cmd_execute_command_strict (vline, vty, NULL); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | cmd_free_strvec (vline); |
| 2006 | |
| 2007 | if (ret != CMD_SUCCESS && ret != CMD_WARNING) |
| 2008 | return ret; |
| 2009 | } |
| 2010 | return CMD_SUCCESS; |
| 2011 | } |
| 2012 | |
| 2013 | /* Configration from terminal */ |
| 2014 | DEFUN (config_terminal, |
| 2015 | config_terminal_cmd, |
| 2016 | "configure terminal", |
| 2017 | "Configuration from vty interface\n" |
| 2018 | "Configuration terminal\n") |
| 2019 | { |
| 2020 | if (vty_config_lock (vty)) |
| 2021 | vty->node = CONFIG_NODE; |
| 2022 | else |
| 2023 | { |
| 2024 | vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE); |
| 2025 | return CMD_WARNING; |
| 2026 | } |
| 2027 | return CMD_SUCCESS; |
| 2028 | } |
| 2029 | |
| 2030 | /* Enable command */ |
| 2031 | DEFUN (enable, |
| 2032 | config_enable_cmd, |
| 2033 | "enable", |
| 2034 | "Turn on privileged mode command\n") |
| 2035 | { |
| 2036 | /* If enable password is NULL, change to ENABLE_NODE */ |
| 2037 | if ((host.enable == NULL && host.enable_encrypt == NULL) || |
| 2038 | vty->type == VTY_SHELL_SERV) |
| 2039 | vty->node = ENABLE_NODE; |
| 2040 | else |
| 2041 | vty->node = AUTH_ENABLE_NODE; |
| 2042 | |
| 2043 | return CMD_SUCCESS; |
| 2044 | } |
| 2045 | |
| 2046 | /* Disable command */ |
| 2047 | DEFUN (disable, |
| 2048 | config_disable_cmd, |
| 2049 | "disable", |
| 2050 | "Turn off privileged mode command\n") |
| 2051 | { |
| 2052 | if (vty->node == ENABLE_NODE) |
| 2053 | vty->node = VIEW_NODE; |
| 2054 | return CMD_SUCCESS; |
| 2055 | } |
| 2056 | |
| 2057 | /* Down vty node level. */ |
| 2058 | DEFUN (config_exit, |
| 2059 | config_exit_cmd, |
| 2060 | "exit", |
| 2061 | "Exit current mode and down to previous mode\n") |
| 2062 | { |
| 2063 | switch (vty->node) |
| 2064 | { |
| 2065 | case VIEW_NODE: |
| 2066 | case ENABLE_NODE: |
| 2067 | if (vty_shell (vty)) |
| 2068 | exit (0); |
| 2069 | else |
| 2070 | vty->status = VTY_CLOSE; |
| 2071 | break; |
| 2072 | case CONFIG_NODE: |
| 2073 | vty->node = ENABLE_NODE; |
| 2074 | vty_config_unlock (vty); |
| 2075 | break; |
| 2076 | case INTERFACE_NODE: |
| 2077 | case ZEBRA_NODE: |
| 2078 | case BGP_NODE: |
| 2079 | case RIP_NODE: |
| 2080 | case RIPNG_NODE: |
| 2081 | case OSPF_NODE: |
| 2082 | case OSPF6_NODE: |
| 2083 | case ISIS_NODE: |
| 2084 | case KEYCHAIN_NODE: |
| 2085 | case MASC_NODE: |
| 2086 | case RMAP_NODE: |
| 2087 | case VTY_NODE: |
| 2088 | vty->node = CONFIG_NODE; |
| 2089 | break; |
| 2090 | case BGP_VPNV4_NODE: |
| 2091 | case BGP_IPV4_NODE: |
| 2092 | case BGP_IPV4M_NODE: |
| 2093 | case BGP_IPV6_NODE: |
| 2094 | vty->node = BGP_NODE; |
| 2095 | break; |
| 2096 | case KEYCHAIN_KEY_NODE: |
| 2097 | vty->node = KEYCHAIN_NODE; |
| 2098 | break; |
| 2099 | default: |
| 2100 | break; |
| 2101 | } |
| 2102 | return CMD_SUCCESS; |
| 2103 | } |
| 2104 | |
| 2105 | /* quit is alias of exit. */ |
| 2106 | ALIAS (config_exit, |
| 2107 | config_quit_cmd, |
| 2108 | "quit", |
| 2109 | "Exit current mode and down to previous mode\n") |
| 2110 | |
| 2111 | /* End of configuration. */ |
| 2112 | DEFUN (config_end, |
| 2113 | config_end_cmd, |
| 2114 | "end", |
| 2115 | "End current mode and change to enable mode.") |
| 2116 | { |
| 2117 | switch (vty->node) |
| 2118 | { |
| 2119 | case VIEW_NODE: |
| 2120 | case ENABLE_NODE: |
| 2121 | /* Nothing to do. */ |
| 2122 | break; |
| 2123 | case CONFIG_NODE: |
| 2124 | case INTERFACE_NODE: |
| 2125 | case ZEBRA_NODE: |
| 2126 | case RIP_NODE: |
| 2127 | case RIPNG_NODE: |
| 2128 | case BGP_NODE: |
| 2129 | case BGP_VPNV4_NODE: |
| 2130 | case BGP_IPV4_NODE: |
| 2131 | case BGP_IPV4M_NODE: |
| 2132 | case BGP_IPV6_NODE: |
| 2133 | case RMAP_NODE: |
| 2134 | case OSPF_NODE: |
| 2135 | case OSPF6_NODE: |
| 2136 | case ISIS_NODE: |
| 2137 | case KEYCHAIN_NODE: |
| 2138 | case KEYCHAIN_KEY_NODE: |
| 2139 | case MASC_NODE: |
| 2140 | case VTY_NODE: |
| 2141 | vty_config_unlock (vty); |
| 2142 | vty->node = ENABLE_NODE; |
| 2143 | break; |
| 2144 | default: |
| 2145 | break; |
| 2146 | } |
| 2147 | return CMD_SUCCESS; |
| 2148 | } |
| 2149 | |
| 2150 | /* Show version. */ |
| 2151 | DEFUN (show_version, |
| 2152 | show_version_cmd, |
| 2153 | "show version", |
| 2154 | SHOW_STR |
| 2155 | "Displays zebra version\n") |
| 2156 | { |
| 2157 | vty_out (vty, "Zebra %s (%s).%s", ZEBRA_VERSION, |
| 2158 | host_name, |
| 2159 | VTY_NEWLINE); |
| 2160 | vty_out (vty, "Copyright 1996-2002, Kunihiro Ishiguro.%s", VTY_NEWLINE); |
| 2161 | |
| 2162 | return CMD_SUCCESS; |
| 2163 | } |
| 2164 | |
| 2165 | /* Help display function for all node. */ |
| 2166 | DEFUN (config_help, |
| 2167 | config_help_cmd, |
| 2168 | "help", |
| 2169 | "Description of the interactive help system\n") |
| 2170 | { |
| 2171 | vty_out (vty, |
| 2172 | "Zebra VTY provides advanced help feature. When you need help,%s\ |
| 2173 | anytime at the command line please press '?'.%s\ |
| 2174 | %s\ |
| 2175 | If nothing matches, the help list will be empty and you must backup%s\ |
| 2176 | until entering a '?' shows the available options.%s\ |
| 2177 | Two styles of help are provided:%s\ |
| 2178 | 1. Full help is available when you are ready to enter a%s\ |
| 2179 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 2180 | argument.%s\ |
| 2181 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 2182 | and you want to know what arguments match the input%s\ |
| 2183 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2184 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2185 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 2186 | return CMD_SUCCESS; |
| 2187 | } |
| 2188 | |
| 2189 | /* Help display function for all node. */ |
| 2190 | DEFUN (config_list, |
| 2191 | config_list_cmd, |
| 2192 | "list", |
| 2193 | "Print command list\n") |
| 2194 | { |
| 2195 | int i; |
| 2196 | struct cmd_node *cnode = vector_slot (cmdvec, vty->node); |
| 2197 | struct cmd_element *cmd; |
| 2198 | |
| 2199 | for (i = 0; i < vector_max (cnode->cmd_vector); i++) |
| 2200 | if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL) |
| 2201 | vty_out (vty, " %s%s", cmd->string, |
| 2202 | VTY_NEWLINE); |
| 2203 | return CMD_SUCCESS; |
| 2204 | } |
| 2205 | |
| 2206 | /* Write current configuration into file. */ |
| 2207 | DEFUN (config_write_file, |
| 2208 | config_write_file_cmd, |
| 2209 | "write file", |
| 2210 | "Write running configuration to memory, network, or terminal\n" |
| 2211 | "Write to configuration file\n") |
| 2212 | { |
| 2213 | int i; |
| 2214 | int fd; |
| 2215 | struct cmd_node *node; |
| 2216 | char *config_file; |
| 2217 | char *config_file_tmp = NULL; |
| 2218 | char *config_file_sav = NULL; |
| 2219 | struct vty *file_vty; |
| 2220 | |
| 2221 | /* Check and see if we are operating under vtysh configuration */ |
| 2222 | if (host.config == NULL) |
| 2223 | { |
| 2224 | vty_out (vty, "Can't save to configuration file, using vtysh.%s", |
| 2225 | VTY_NEWLINE); |
| 2226 | return CMD_WARNING; |
| 2227 | } |
| 2228 | |
| 2229 | /* Get filename. */ |
| 2230 | config_file = host.config; |
| 2231 | |
| 2232 | config_file_sav = malloc (strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); |
| 2233 | strcpy (config_file_sav, config_file); |
| 2234 | strcat (config_file_sav, CONF_BACKUP_EXT); |
| 2235 | |
| 2236 | |
| 2237 | config_file_tmp = malloc (strlen (config_file) + 8); |
| 2238 | sprintf (config_file_tmp, "%s.XXXXXX", config_file); |
| 2239 | |
| 2240 | /* Open file to configuration write. */ |
| 2241 | fd = mkstemp (config_file_tmp); |
| 2242 | if (fd < 0) |
| 2243 | { |
| 2244 | vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, |
| 2245 | VTY_NEWLINE); |
| 2246 | free (config_file_tmp); |
| 2247 | free (config_file_sav); |
| 2248 | return CMD_WARNING; |
| 2249 | } |
| 2250 | |
| 2251 | /* Make vty for configuration file. */ |
| 2252 | file_vty = vty_new (); |
| 2253 | file_vty->fd = fd; |
| 2254 | file_vty->type = VTY_FILE; |
| 2255 | |
| 2256 | /* Config file header print. */ |
| 2257 | vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! "); |
| 2258 | vty_time_print (file_vty, 1); |
| 2259 | vty_out (file_vty, "!\n"); |
| 2260 | |
| 2261 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2262 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2263 | { |
| 2264 | if ((*node->func) (file_vty)) |
| 2265 | vty_out (file_vty, "!\n"); |
| 2266 | } |
| 2267 | vty_close (file_vty); |
| 2268 | |
| 2269 | if (unlink (config_file_sav) != 0) |
| 2270 | if (errno != ENOENT) |
| 2271 | { |
| 2272 | vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, |
| 2273 | VTY_NEWLINE); |
| 2274 | free (config_file_sav); |
| 2275 | free (config_file_tmp); |
| 2276 | unlink (config_file_tmp); |
| 2277 | return CMD_WARNING; |
| 2278 | } |
| 2279 | if (link (config_file, config_file_sav) != 0) |
| 2280 | { |
| 2281 | vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, |
| 2282 | VTY_NEWLINE); |
| 2283 | free (config_file_sav); |
| 2284 | free (config_file_tmp); |
| 2285 | unlink (config_file_tmp); |
| 2286 | return CMD_WARNING; |
| 2287 | } |
| 2288 | sync (); |
| 2289 | if (unlink (config_file) != 0) |
| 2290 | { |
| 2291 | vty_out (vty, "Can't unlink configuration file %s.%s", config_file, |
| 2292 | VTY_NEWLINE); |
| 2293 | free (config_file_sav); |
| 2294 | free (config_file_tmp); |
| 2295 | unlink (config_file_tmp); |
| 2296 | return CMD_WARNING; |
| 2297 | } |
| 2298 | if (link (config_file_tmp, config_file) != 0) |
| 2299 | { |
| 2300 | vty_out (vty, "Can't save configuration file %s.%s", config_file, |
| 2301 | VTY_NEWLINE); |
| 2302 | free (config_file_sav); |
| 2303 | free (config_file_tmp); |
| 2304 | unlink (config_file_tmp); |
| 2305 | return CMD_WARNING; |
| 2306 | } |
| 2307 | unlink (config_file_tmp); |
| 2308 | sync (); |
| 2309 | |
| 2310 | free (config_file_sav); |
| 2311 | free (config_file_tmp); |
| 2312 | vty_out (vty, "Configuration saved to %s%s", config_file, |
| 2313 | VTY_NEWLINE); |
| 2314 | return CMD_SUCCESS; |
| 2315 | } |
| 2316 | |
| 2317 | ALIAS (config_write_file, |
| 2318 | config_write_cmd, |
| 2319 | "write", |
| 2320 | "Write running configuration to memory, network, or terminal\n") |
| 2321 | |
| 2322 | ALIAS (config_write_file, |
| 2323 | config_write_memory_cmd, |
| 2324 | "write memory", |
| 2325 | "Write running configuration to memory, network, or terminal\n" |
| 2326 | "Write configuration to the file (same as write file)\n") |
| 2327 | |
| 2328 | ALIAS (config_write_file, |
| 2329 | copy_runningconfig_startupconfig_cmd, |
| 2330 | "copy running-config startup-config", |
| 2331 | "Copy configuration\n" |
| 2332 | "Copy running config to... \n" |
| 2333 | "Copy running config to startup config (same as write file)\n") |
| 2334 | |
| 2335 | /* Write current configuration into the terminal. */ |
| 2336 | DEFUN (config_write_terminal, |
| 2337 | config_write_terminal_cmd, |
| 2338 | "write terminal", |
| 2339 | "Write running configuration to memory, network, or terminal\n" |
| 2340 | "Write to terminal\n") |
| 2341 | { |
| 2342 | int i; |
| 2343 | struct cmd_node *node; |
| 2344 | |
| 2345 | if (vty->type == VTY_SHELL_SERV) |
| 2346 | { |
| 2347 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2348 | if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh) |
| 2349 | { |
| 2350 | if ((*node->func) (vty)) |
| 2351 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2352 | } |
| 2353 | } |
| 2354 | else |
| 2355 | { |
| 2356 | vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 2357 | VTY_NEWLINE); |
| 2358 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2359 | |
| 2360 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2361 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2362 | { |
| 2363 | if ((*node->func) (vty)) |
| 2364 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2365 | } |
| 2366 | vty_out (vty, "end%s",VTY_NEWLINE); |
| 2367 | } |
| 2368 | return CMD_SUCCESS; |
| 2369 | } |
| 2370 | |
| 2371 | /* Write current configuration into the terminal. */ |
| 2372 | ALIAS (config_write_terminal, |
| 2373 | show_running_config_cmd, |
| 2374 | "show running-config", |
| 2375 | SHOW_STR |
| 2376 | "running configuration\n") |
| 2377 | |
| 2378 | /* Write startup configuration into the terminal. */ |
| 2379 | DEFUN (show_startup_config, |
| 2380 | show_startup_config_cmd, |
| 2381 | "show startup-config", |
| 2382 | SHOW_STR |
| 2383 | "Contentes of startup configuration\n") |
| 2384 | { |
| 2385 | char buf[BUFSIZ]; |
| 2386 | FILE *confp; |
| 2387 | |
| 2388 | confp = fopen (host.config, "r"); |
| 2389 | if (confp == NULL) |
| 2390 | { |
| 2391 | vty_out (vty, "Can't open configuration file [%s]%s", |
| 2392 | host.config, VTY_NEWLINE); |
| 2393 | return CMD_WARNING; |
| 2394 | } |
| 2395 | |
| 2396 | while (fgets (buf, BUFSIZ, confp)) |
| 2397 | { |
| 2398 | char *cp = buf; |
| 2399 | |
| 2400 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 2401 | cp++; |
| 2402 | *cp = '\0'; |
| 2403 | |
| 2404 | vty_out (vty, "%s%s", buf, VTY_NEWLINE); |
| 2405 | } |
| 2406 | |
| 2407 | fclose (confp); |
| 2408 | |
| 2409 | return CMD_SUCCESS; |
| 2410 | } |
| 2411 | |
| 2412 | /* Hostname configuration */ |
| 2413 | DEFUN (config_hostname, |
| 2414 | hostname_cmd, |
| 2415 | "hostname WORD", |
| 2416 | "Set system's network name\n" |
| 2417 | "This system's network name\n") |
| 2418 | { |
| 2419 | if (!isalpha((int) *argv[0])) |
| 2420 | { |
| 2421 | vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE); |
| 2422 | return CMD_WARNING; |
| 2423 | } |
| 2424 | |
| 2425 | if (host.name) |
| 2426 | XFREE (0, host.name); |
| 2427 | |
| 2428 | host.name = strdup (argv[0]); |
| 2429 | return CMD_SUCCESS; |
| 2430 | } |
| 2431 | |
| 2432 | DEFUN (config_no_hostname, |
| 2433 | no_hostname_cmd, |
| 2434 | "no hostname [HOSTNAME]", |
| 2435 | NO_STR |
| 2436 | "Reset system's network name\n" |
| 2437 | "Host name of this router\n") |
| 2438 | { |
| 2439 | if (host.name) |
| 2440 | XFREE (0, host.name); |
| 2441 | host.name = NULL; |
| 2442 | return CMD_SUCCESS; |
| 2443 | } |
| 2444 | |
| 2445 | /* VTY interface password set. */ |
| 2446 | DEFUN (config_password, password_cmd, |
| 2447 | "password (8|) WORD", |
| 2448 | "Assign the terminal connection password\n" |
| 2449 | "Specifies a HIDDEN password will follow\n" |
| 2450 | "dummy string \n" |
| 2451 | "The HIDDEN line password string\n") |
| 2452 | { |
| 2453 | /* Argument check. */ |
| 2454 | if (argc == 0) |
| 2455 | { |
| 2456 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2457 | return CMD_WARNING; |
| 2458 | } |
| 2459 | |
| 2460 | if (argc == 2) |
| 2461 | { |
| 2462 | if (*argv[0] == '8') |
| 2463 | { |
| 2464 | if (host.password) |
| 2465 | XFREE (0, host.password); |
| 2466 | host.password = NULL; |
| 2467 | if (host.password_encrypt) |
| 2468 | XFREE (0, host.password_encrypt); |
| 2469 | host.password_encrypt = XSTRDUP (0, strdup (argv[1])); |
| 2470 | return CMD_SUCCESS; |
| 2471 | } |
| 2472 | else |
| 2473 | { |
| 2474 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2475 | return CMD_WARNING; |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | if (!isalnum ((int) *argv[0])) |
| 2480 | { |
| 2481 | vty_out (vty, |
| 2482 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2483 | return CMD_WARNING; |
| 2484 | } |
| 2485 | |
| 2486 | if (host.password) |
| 2487 | XFREE (0, host.password); |
| 2488 | host.password = NULL; |
| 2489 | |
| 2490 | if (host.encrypt) |
| 2491 | { |
| 2492 | if (host.password_encrypt) |
| 2493 | XFREE (0, host.password_encrypt); |
| 2494 | host.password_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2495 | } |
| 2496 | else |
| 2497 | host.password = XSTRDUP (0, argv[0]); |
| 2498 | |
| 2499 | return CMD_SUCCESS; |
| 2500 | } |
| 2501 | |
| 2502 | ALIAS (config_password, password_text_cmd, |
| 2503 | "password LINE", |
| 2504 | "Assign the terminal connection password\n" |
| 2505 | "The UNENCRYPTED (cleartext) line password\n") |
| 2506 | |
| 2507 | /* VTY enable password set. */ |
| 2508 | DEFUN (config_enable_password, enable_password_cmd, |
| 2509 | "enable password (8|) WORD", |
| 2510 | "Modify enable password parameters\n" |
| 2511 | "Assign the privileged level password\n" |
| 2512 | "Specifies a HIDDEN password will follow\n" |
| 2513 | "dummy string \n" |
| 2514 | "The HIDDEN 'enable' password string\n") |
| 2515 | { |
| 2516 | /* Argument check. */ |
| 2517 | if (argc == 0) |
| 2518 | { |
| 2519 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2520 | return CMD_WARNING; |
| 2521 | } |
| 2522 | |
| 2523 | /* Crypt type is specified. */ |
| 2524 | if (argc == 2) |
| 2525 | { |
| 2526 | if (*argv[0] == '8') |
| 2527 | { |
| 2528 | if (host.enable) |
| 2529 | XFREE (0, host.enable); |
| 2530 | host.enable = NULL; |
| 2531 | |
| 2532 | if (host.enable_encrypt) |
| 2533 | XFREE (0, host.enable_encrypt); |
| 2534 | host.enable_encrypt = XSTRDUP (0, argv[1]); |
| 2535 | |
| 2536 | return CMD_SUCCESS; |
| 2537 | } |
| 2538 | else |
| 2539 | { |
| 2540 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2541 | return CMD_WARNING; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (!isalnum ((int) *argv[0])) |
| 2546 | { |
| 2547 | vty_out (vty, |
| 2548 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2549 | return CMD_WARNING; |
| 2550 | } |
| 2551 | |
| 2552 | if (host.enable) |
| 2553 | XFREE (0, host.enable); |
| 2554 | host.enable = NULL; |
| 2555 | |
| 2556 | /* Plain password input. */ |
| 2557 | if (host.encrypt) |
| 2558 | { |
| 2559 | if (host.enable_encrypt) |
| 2560 | XFREE (0, host.enable_encrypt); |
| 2561 | host.enable_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2562 | } |
| 2563 | else |
| 2564 | host.enable = XSTRDUP (0, argv[0]); |
| 2565 | |
| 2566 | return CMD_SUCCESS; |
| 2567 | } |
| 2568 | |
| 2569 | ALIAS (config_enable_password, |
| 2570 | enable_password_text_cmd, |
| 2571 | "enable password LINE", |
| 2572 | "Modify enable password parameters\n" |
| 2573 | "Assign the privileged level password\n" |
| 2574 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 2575 | |
| 2576 | /* VTY enable password delete. */ |
| 2577 | DEFUN (no_config_enable_password, no_enable_password_cmd, |
| 2578 | "no enable password", |
| 2579 | NO_STR |
| 2580 | "Modify enable password parameters\n" |
| 2581 | "Assign the privileged level password\n") |
| 2582 | { |
| 2583 | if (host.enable) |
| 2584 | XFREE (0, host.enable); |
| 2585 | host.enable = NULL; |
| 2586 | |
| 2587 | if (host.enable_encrypt) |
| 2588 | XFREE (0, host.enable_encrypt); |
| 2589 | host.enable_encrypt = NULL; |
| 2590 | |
| 2591 | return CMD_SUCCESS; |
| 2592 | } |
| 2593 | |
| 2594 | DEFUN (service_password_encrypt, |
| 2595 | service_password_encrypt_cmd, |
| 2596 | "service password-encryption", |
| 2597 | "Set up miscellaneous service\n" |
| 2598 | "Enable encrypted passwords\n") |
| 2599 | { |
| 2600 | if (host.encrypt) |
| 2601 | return CMD_SUCCESS; |
| 2602 | |
| 2603 | host.encrypt = 1; |
| 2604 | |
| 2605 | if (host.password) |
| 2606 | { |
| 2607 | if (host.password_encrypt) |
| 2608 | XFREE (0, host.password_encrypt); |
| 2609 | host.password_encrypt = XSTRDUP (0, zencrypt (host.password)); |
| 2610 | } |
| 2611 | if (host.enable) |
| 2612 | { |
| 2613 | if (host.enable_encrypt) |
| 2614 | XFREE (0, host.enable_encrypt); |
| 2615 | host.enable_encrypt = XSTRDUP (0, zencrypt (host.enable)); |
| 2616 | } |
| 2617 | |
| 2618 | return CMD_SUCCESS; |
| 2619 | } |
| 2620 | |
| 2621 | DEFUN (no_service_password_encrypt, |
| 2622 | no_service_password_encrypt_cmd, |
| 2623 | "no service password-encryption", |
| 2624 | NO_STR |
| 2625 | "Set up miscellaneous service\n" |
| 2626 | "Enable encrypted passwords\n") |
| 2627 | { |
| 2628 | if (! host.encrypt) |
| 2629 | return CMD_SUCCESS; |
| 2630 | |
| 2631 | host.encrypt = 0; |
| 2632 | |
| 2633 | if (host.password_encrypt) |
| 2634 | XFREE (0, host.password_encrypt); |
| 2635 | host.password_encrypt = NULL; |
| 2636 | |
| 2637 | if (host.enable_encrypt) |
| 2638 | XFREE (0, host.enable_encrypt); |
| 2639 | host.enable_encrypt = NULL; |
| 2640 | |
| 2641 | return CMD_SUCCESS; |
| 2642 | } |
| 2643 | |
| 2644 | DEFUN (config_terminal_length, config_terminal_length_cmd, |
| 2645 | "terminal length <0-512>", |
| 2646 | "Set terminal line parameters\n" |
| 2647 | "Set number of lines on a screen\n" |
| 2648 | "Number of lines on screen (0 for no pausing)\n") |
| 2649 | { |
| 2650 | int lines; |
| 2651 | char *endptr = NULL; |
| 2652 | |
| 2653 | lines = strtol (argv[0], &endptr, 10); |
| 2654 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2655 | { |
| 2656 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2657 | return CMD_WARNING; |
| 2658 | } |
| 2659 | vty->lines = lines; |
| 2660 | |
| 2661 | return CMD_SUCCESS; |
| 2662 | } |
| 2663 | |
| 2664 | DEFUN (config_terminal_no_length, config_terminal_no_length_cmd, |
| 2665 | "terminal no length", |
| 2666 | "Set terminal line parameters\n" |
| 2667 | NO_STR |
| 2668 | "Set number of lines on a screen\n") |
| 2669 | { |
| 2670 | vty->lines = -1; |
| 2671 | return CMD_SUCCESS; |
| 2672 | } |
| 2673 | |
| 2674 | DEFUN (service_terminal_length, service_terminal_length_cmd, |
| 2675 | "service terminal-length <0-512>", |
| 2676 | "Set up miscellaneous service\n" |
| 2677 | "System wide terminal length configuration\n" |
| 2678 | "Number of lines of VTY (0 means no line control)\n") |
| 2679 | { |
| 2680 | int lines; |
| 2681 | char *endptr = NULL; |
| 2682 | |
| 2683 | lines = strtol (argv[0], &endptr, 10); |
| 2684 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2685 | { |
| 2686 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2687 | return CMD_WARNING; |
| 2688 | } |
| 2689 | host.lines = lines; |
| 2690 | |
| 2691 | return CMD_SUCCESS; |
| 2692 | } |
| 2693 | |
| 2694 | DEFUN (no_service_terminal_length, no_service_terminal_length_cmd, |
| 2695 | "no service terminal-length [<0-512>]", |
| 2696 | NO_STR |
| 2697 | "Set up miscellaneous service\n" |
| 2698 | "System wide terminal length configuration\n" |
| 2699 | "Number of lines of VTY (0 means no line control)\n") |
| 2700 | { |
| 2701 | host.lines = -1; |
| 2702 | return CMD_SUCCESS; |
| 2703 | } |
| 2704 | |
| 2705 | DEFUN (config_log_stdout, |
| 2706 | config_log_stdout_cmd, |
| 2707 | "log stdout", |
| 2708 | "Logging control\n" |
| 2709 | "Logging goes to stdout\n") |
| 2710 | { |
| 2711 | zlog_set_flag (NULL, ZLOG_STDOUT); |
| 2712 | host.log_stdout = 1; |
| 2713 | return CMD_SUCCESS; |
| 2714 | } |
| 2715 | |
| 2716 | DEFUN (no_config_log_stdout, |
| 2717 | no_config_log_stdout_cmd, |
| 2718 | "no log stdout", |
| 2719 | NO_STR |
| 2720 | "Logging control\n" |
| 2721 | "Cancel logging to stdout\n") |
| 2722 | { |
| 2723 | zlog_reset_flag (NULL, ZLOG_STDOUT); |
| 2724 | host.log_stdout = 0; |
| 2725 | return CMD_SUCCESS; |
| 2726 | } |
| 2727 | |
| 2728 | DEFUN (config_log_file, |
| 2729 | config_log_file_cmd, |
| 2730 | "log file FILENAME", |
| 2731 | "Logging control\n" |
| 2732 | "Logging to file\n" |
| 2733 | "Logging filename\n") |
| 2734 | { |
| 2735 | int ret; |
| 2736 | char *cwd; |
| 2737 | char *fullpath; |
| 2738 | |
| 2739 | /* Path detection. */ |
| 2740 | if (! IS_DIRECTORY_SEP (*argv[0])) |
| 2741 | { |
| 2742 | cwd = getcwd (NULL, MAXPATHLEN); |
| 2743 | fullpath = XMALLOC (MTYPE_TMP, |
| 2744 | strlen (cwd) + strlen (argv[0]) + 2); |
| 2745 | sprintf (fullpath, "%s/%s", cwd, argv[0]); |
| 2746 | } |
| 2747 | else |
| 2748 | fullpath = argv[0]; |
| 2749 | |
| 2750 | ret = zlog_set_file (NULL, ZLOG_FILE, fullpath); |
| 2751 | |
| 2752 | if (!ret) |
| 2753 | { |
| 2754 | vty_out (vty, "can't open logfile %s\n", argv[0]); |
| 2755 | return CMD_WARNING; |
| 2756 | } |
| 2757 | |
| 2758 | if (host.logfile) |
| 2759 | XFREE (MTYPE_TMP, host.logfile); |
| 2760 | |
| 2761 | host.logfile = strdup (argv[0]); |
| 2762 | |
| 2763 | return CMD_SUCCESS; |
| 2764 | } |
| 2765 | |
| 2766 | DEFUN (no_config_log_file, |
| 2767 | no_config_log_file_cmd, |
| 2768 | "no log file [FILENAME]", |
| 2769 | NO_STR |
| 2770 | "Logging control\n" |
| 2771 | "Cancel logging to file\n" |
| 2772 | "Logging file name\n") |
| 2773 | { |
| 2774 | zlog_reset_file (NULL); |
| 2775 | |
| 2776 | if (host.logfile) |
| 2777 | XFREE (MTYPE_TMP, host.logfile); |
| 2778 | |
| 2779 | host.logfile = NULL; |
| 2780 | |
| 2781 | return CMD_SUCCESS; |
| 2782 | } |
| 2783 | |
| 2784 | DEFUN (config_log_syslog, |
| 2785 | config_log_syslog_cmd, |
| 2786 | "log syslog", |
| 2787 | "Logging control\n" |
| 2788 | "Logging goes to syslog\n") |
| 2789 | { |
| 2790 | zlog_set_flag (NULL, ZLOG_SYSLOG); |
| 2791 | host.log_syslog = 1; |
| 2792 | return CMD_SUCCESS; |
| 2793 | } |
| 2794 | |
| 2795 | DEFUN (no_config_log_syslog, |
| 2796 | no_config_log_syslog_cmd, |
| 2797 | "no log syslog", |
| 2798 | NO_STR |
| 2799 | "Logging control\n" |
| 2800 | "Cancel logging to syslog\n") |
| 2801 | { |
| 2802 | zlog_reset_flag (NULL, ZLOG_SYSLOG); |
| 2803 | host.log_syslog = 0; |
| 2804 | return CMD_SUCCESS; |
| 2805 | } |
| 2806 | |
| 2807 | DEFUN (config_log_trap, |
| 2808 | config_log_trap_cmd, |
| 2809 | "log trap (emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)", |
| 2810 | "Logging control\n" |
| 2811 | "Limit logging to specifed level\n") |
| 2812 | { |
| 2813 | int new_level ; |
| 2814 | |
| 2815 | for ( new_level = 0 ; zlog_priority [new_level] != NULL ; new_level ++ ) |
| 2816 | { |
| 2817 | if ( strcmp ( argv[0], zlog_priority [new_level] ) == 0 ) |
| 2818 | /* found new logging level */ |
| 2819 | { |
| 2820 | zlog_default->maskpri = new_level; |
| 2821 | return CMD_SUCCESS; |
| 2822 | } |
| 2823 | } |
| 2824 | return CMD_ERR_NO_MATCH; |
| 2825 | } |
| 2826 | |
| 2827 | DEFUN (no_config_log_trap, |
| 2828 | no_config_log_trap_cmd, |
| 2829 | "no log trap", |
| 2830 | NO_STR |
| 2831 | "Logging control\n" |
| 2832 | "Permit all logging information\n") |
| 2833 | { |
| 2834 | zlog_default->maskpri = LOG_DEBUG; |
| 2835 | return CMD_SUCCESS; |
| 2836 | } |
| 2837 | |
| 2838 | DEFUN (config_log_record_priority, |
| 2839 | config_log_record_priority_cmd, |
| 2840 | "log record-priority", |
| 2841 | "Logging control\n" |
| 2842 | "Log the priority of the message within the message\n") |
| 2843 | { |
| 2844 | zlog_default->record_priority = 1 ; |
| 2845 | return CMD_SUCCESS; |
| 2846 | } |
| 2847 | |
| 2848 | DEFUN (no_config_log_record_priority, |
| 2849 | no_config_log_record_priority_cmd, |
| 2850 | "no log record-priority", |
| 2851 | NO_STR |
| 2852 | "Logging control\n" |
| 2853 | "Do not log the priority of the message within the message\n") |
| 2854 | { |
| 2855 | zlog_default->record_priority = 0 ; |
| 2856 | return CMD_SUCCESS; |
| 2857 | } |
| 2858 | |
| 2859 | |
| 2860 | DEFUN (banner_motd_default, |
| 2861 | banner_motd_default_cmd, |
| 2862 | "banner motd default", |
| 2863 | "Set banner string\n" |
| 2864 | "Strings for motd\n" |
| 2865 | "Default string\n") |
| 2866 | { |
| 2867 | host.motd = default_motd; |
| 2868 | return CMD_SUCCESS; |
| 2869 | } |
| 2870 | |
| 2871 | DEFUN (no_banner_motd, |
| 2872 | no_banner_motd_cmd, |
| 2873 | "no banner motd", |
| 2874 | NO_STR |
| 2875 | "Set banner string\n" |
| 2876 | "Strings for motd\n") |
| 2877 | { |
| 2878 | host.motd = NULL; |
| 2879 | return CMD_SUCCESS; |
| 2880 | } |
| 2881 | |
| 2882 | /* Set config filename. Called from vty.c */ |
| 2883 | void |
| 2884 | host_config_set (char *filename) |
| 2885 | { |
| 2886 | host.config = strdup (filename); |
| 2887 | } |
| 2888 | |
| 2889 | void |
| 2890 | install_default (enum node_type node) |
| 2891 | { |
| 2892 | install_element (node, &config_exit_cmd); |
| 2893 | install_element (node, &config_quit_cmd); |
| 2894 | install_element (node, &config_end_cmd); |
| 2895 | install_element (node, &config_help_cmd); |
| 2896 | install_element (node, &config_list_cmd); |
| 2897 | |
| 2898 | install_element (node, &config_write_terminal_cmd); |
| 2899 | install_element (node, &config_write_file_cmd); |
| 2900 | install_element (node, &config_write_memory_cmd); |
| 2901 | install_element (node, &config_write_cmd); |
| 2902 | } |
| 2903 | |
| 2904 | /* Initialize command interface. Install basic nodes and commands. */ |
| 2905 | void |
| 2906 | cmd_init (int terminal) |
| 2907 | { |
| 2908 | /* Allocate initial top vector of commands. */ |
| 2909 | cmdvec = vector_init (VECTOR_MIN_SIZE); |
| 2910 | |
| 2911 | /* Default host value settings. */ |
| 2912 | host.name = NULL; |
| 2913 | host.password = NULL; |
| 2914 | host.enable = NULL; |
| 2915 | host.logfile = NULL; |
| 2916 | host.config = NULL; |
| 2917 | host.lines = -1; |
| 2918 | host.motd = default_motd; |
| 2919 | |
| 2920 | /* Install top nodes. */ |
| 2921 | install_node (&view_node, NULL); |
| 2922 | install_node (&enable_node, NULL); |
| 2923 | install_node (&auth_node, NULL); |
| 2924 | install_node (&auth_enable_node, NULL); |
| 2925 | install_node (&config_node, config_write_host); |
| 2926 | |
| 2927 | /* Each node's basic commands. */ |
| 2928 | install_element (VIEW_NODE, &show_version_cmd); |
| 2929 | if (terminal) |
| 2930 | { |
| 2931 | install_element (VIEW_NODE, &config_list_cmd); |
| 2932 | install_element (VIEW_NODE, &config_exit_cmd); |
| 2933 | install_element (VIEW_NODE, &config_quit_cmd); |
| 2934 | install_element (VIEW_NODE, &config_help_cmd); |
| 2935 | install_element (VIEW_NODE, &config_enable_cmd); |
| 2936 | install_element (VIEW_NODE, &config_terminal_length_cmd); |
| 2937 | install_element (VIEW_NODE, &config_terminal_no_length_cmd); |
| 2938 | } |
| 2939 | |
| 2940 | if (terminal) |
| 2941 | { |
| 2942 | install_default (ENABLE_NODE); |
| 2943 | install_element (ENABLE_NODE, &config_disable_cmd); |
| 2944 | install_element (ENABLE_NODE, &config_terminal_cmd); |
| 2945 | install_element (ENABLE_NODE, &show_running_config_cmd); |
| 2946 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
| 2947 | } |
| 2948 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
| 2949 | install_element (ENABLE_NODE, &show_version_cmd); |
| 2950 | install_element (ENABLE_NODE, &config_terminal_length_cmd); |
| 2951 | install_element (ENABLE_NODE, &config_terminal_no_length_cmd); |
| 2952 | |
| 2953 | if (terminal) |
| 2954 | install_default (CONFIG_NODE); |
| 2955 | install_element (CONFIG_NODE, &hostname_cmd); |
| 2956 | install_element (CONFIG_NODE, &no_hostname_cmd); |
| 2957 | install_element (CONFIG_NODE, &password_cmd); |
| 2958 | install_element (CONFIG_NODE, &password_text_cmd); |
| 2959 | install_element (CONFIG_NODE, &enable_password_cmd); |
| 2960 | install_element (CONFIG_NODE, &enable_password_text_cmd); |
| 2961 | install_element (CONFIG_NODE, &no_enable_password_cmd); |
| 2962 | if (terminal) |
| 2963 | { |
| 2964 | install_element (CONFIG_NODE, &config_log_stdout_cmd); |
| 2965 | install_element (CONFIG_NODE, &no_config_log_stdout_cmd); |
| 2966 | install_element (CONFIG_NODE, &config_log_file_cmd); |
| 2967 | install_element (CONFIG_NODE, &no_config_log_file_cmd); |
| 2968 | install_element (CONFIG_NODE, &config_log_syslog_cmd); |
| 2969 | install_element (CONFIG_NODE, &no_config_log_syslog_cmd); |
| 2970 | install_element (CONFIG_NODE, &config_log_trap_cmd); |
| 2971 | install_element (CONFIG_NODE, &no_config_log_trap_cmd); |
| 2972 | install_element (CONFIG_NODE, &config_log_record_priority_cmd); |
| 2973 | install_element (CONFIG_NODE, &no_config_log_record_priority_cmd); |
| 2974 | install_element (CONFIG_NODE, &service_password_encrypt_cmd); |
| 2975 | install_element (CONFIG_NODE, &no_service_password_encrypt_cmd); |
| 2976 | install_element (CONFIG_NODE, &banner_motd_default_cmd); |
| 2977 | install_element (CONFIG_NODE, &no_banner_motd_cmd); |
| 2978 | install_element (CONFIG_NODE, &service_terminal_length_cmd); |
| 2979 | install_element (CONFIG_NODE, &no_service_terminal_length_cmd); |
| 2980 | } |
| 2981 | |
| 2982 | srand(time(NULL)); |
| 2983 | } |