paul | 718e374 | 2002-12-13 20:15:29 +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 KEYCHAIN_NODE: |
| 2084 | case MASC_NODE: |
| 2085 | case RMAP_NODE: |
| 2086 | case VTY_NODE: |
| 2087 | vty->node = CONFIG_NODE; |
| 2088 | break; |
| 2089 | case BGP_VPNV4_NODE: |
| 2090 | case BGP_IPV4_NODE: |
| 2091 | case BGP_IPV4M_NODE: |
| 2092 | case BGP_IPV6_NODE: |
| 2093 | vty->node = BGP_NODE; |
| 2094 | break; |
| 2095 | case KEYCHAIN_KEY_NODE: |
| 2096 | vty->node = KEYCHAIN_NODE; |
| 2097 | break; |
| 2098 | default: |
| 2099 | break; |
| 2100 | } |
| 2101 | return CMD_SUCCESS; |
| 2102 | } |
| 2103 | |
| 2104 | /* quit is alias of exit. */ |
| 2105 | ALIAS (config_exit, |
| 2106 | config_quit_cmd, |
| 2107 | "quit", |
| 2108 | "Exit current mode and down to previous mode\n") |
| 2109 | |
| 2110 | /* End of configuration. */ |
| 2111 | DEFUN (config_end, |
| 2112 | config_end_cmd, |
| 2113 | "end", |
| 2114 | "End current mode and change to enable mode.") |
| 2115 | { |
| 2116 | switch (vty->node) |
| 2117 | { |
| 2118 | case VIEW_NODE: |
| 2119 | case ENABLE_NODE: |
| 2120 | /* Nothing to do. */ |
| 2121 | break; |
| 2122 | case CONFIG_NODE: |
| 2123 | case INTERFACE_NODE: |
| 2124 | case ZEBRA_NODE: |
| 2125 | case RIP_NODE: |
| 2126 | case RIPNG_NODE: |
| 2127 | case BGP_NODE: |
| 2128 | case BGP_VPNV4_NODE: |
| 2129 | case BGP_IPV4_NODE: |
| 2130 | case BGP_IPV4M_NODE: |
| 2131 | case BGP_IPV6_NODE: |
| 2132 | case RMAP_NODE: |
| 2133 | case OSPF_NODE: |
| 2134 | case OSPF6_NODE: |
| 2135 | case KEYCHAIN_NODE: |
| 2136 | case KEYCHAIN_KEY_NODE: |
| 2137 | case MASC_NODE: |
| 2138 | case VTY_NODE: |
| 2139 | vty_config_unlock (vty); |
| 2140 | vty->node = ENABLE_NODE; |
| 2141 | break; |
| 2142 | default: |
| 2143 | break; |
| 2144 | } |
| 2145 | return CMD_SUCCESS; |
| 2146 | } |
| 2147 | |
| 2148 | /* Show version. */ |
| 2149 | DEFUN (show_version, |
| 2150 | show_version_cmd, |
| 2151 | "show version", |
| 2152 | SHOW_STR |
| 2153 | "Displays zebra version\n") |
| 2154 | { |
| 2155 | vty_out (vty, "Zebra %s (%s).%s", ZEBRA_VERSION, |
| 2156 | host_name, |
| 2157 | VTY_NEWLINE); |
| 2158 | vty_out (vty, "Copyright 1996-2002, Kunihiro Ishiguro.%s", VTY_NEWLINE); |
| 2159 | |
| 2160 | return CMD_SUCCESS; |
| 2161 | } |
| 2162 | |
| 2163 | /* Help display function for all node. */ |
| 2164 | DEFUN (config_help, |
| 2165 | config_help_cmd, |
| 2166 | "help", |
| 2167 | "Description of the interactive help system\n") |
| 2168 | { |
| 2169 | vty_out (vty, |
| 2170 | "Zebra VTY provides advanced help feature. When you need help,%s\ |
| 2171 | anytime at the command line please press '?'.%s\ |
| 2172 | %s\ |
| 2173 | If nothing matches, the help list will be empty and you must backup%s\ |
| 2174 | until entering a '?' shows the available options.%s\ |
| 2175 | Two styles of help are provided:%s\ |
| 2176 | 1. Full help is available when you are ready to enter a%s\ |
| 2177 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 2178 | argument.%s\ |
| 2179 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 2180 | and you want to know what arguments match the input%s\ |
| 2181 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2182 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2183 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 2184 | return CMD_SUCCESS; |
| 2185 | } |
| 2186 | |
| 2187 | /* Help display function for all node. */ |
| 2188 | DEFUN (config_list, |
| 2189 | config_list_cmd, |
| 2190 | "list", |
| 2191 | "Print command list\n") |
| 2192 | { |
| 2193 | int i; |
| 2194 | struct cmd_node *cnode = vector_slot (cmdvec, vty->node); |
| 2195 | struct cmd_element *cmd; |
| 2196 | |
| 2197 | for (i = 0; i < vector_max (cnode->cmd_vector); i++) |
| 2198 | if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL) |
| 2199 | vty_out (vty, " %s%s", cmd->string, |
| 2200 | VTY_NEWLINE); |
| 2201 | return CMD_SUCCESS; |
| 2202 | } |
| 2203 | |
| 2204 | /* Write current configuration into file. */ |
| 2205 | DEFUN (config_write_file, |
| 2206 | config_write_file_cmd, |
| 2207 | "write file", |
| 2208 | "Write running configuration to memory, network, or terminal\n" |
| 2209 | "Write to configuration file\n") |
| 2210 | { |
| 2211 | int i; |
| 2212 | int fd; |
| 2213 | struct cmd_node *node; |
| 2214 | char *config_file; |
| 2215 | char *config_file_tmp = NULL; |
| 2216 | char *config_file_sav = NULL; |
| 2217 | struct vty *file_vty; |
| 2218 | |
| 2219 | /* Check and see if we are operating under vtysh configuration */ |
| 2220 | if (host.config == NULL) |
| 2221 | { |
| 2222 | vty_out (vty, "Can't save to configuration file, using vtysh.%s", |
| 2223 | VTY_NEWLINE); |
| 2224 | return CMD_WARNING; |
| 2225 | } |
| 2226 | |
| 2227 | /* Get filename. */ |
| 2228 | config_file = host.config; |
| 2229 | |
| 2230 | config_file_sav = malloc (strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); |
| 2231 | strcpy (config_file_sav, config_file); |
| 2232 | strcat (config_file_sav, CONF_BACKUP_EXT); |
| 2233 | |
| 2234 | |
| 2235 | config_file_tmp = malloc (strlen (config_file) + 8); |
| 2236 | sprintf (config_file_tmp, "%s.XXXXXX", config_file); |
| 2237 | |
| 2238 | /* Open file to configuration write. */ |
| 2239 | fd = mkstemp (config_file_tmp); |
| 2240 | if (fd < 0) |
| 2241 | { |
| 2242 | vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, |
| 2243 | VTY_NEWLINE); |
| 2244 | free (config_file_tmp); |
| 2245 | free (config_file_sav); |
| 2246 | return CMD_WARNING; |
| 2247 | } |
| 2248 | |
| 2249 | /* Make vty for configuration file. */ |
| 2250 | file_vty = vty_new (); |
| 2251 | file_vty->fd = fd; |
| 2252 | file_vty->type = VTY_FILE; |
| 2253 | |
| 2254 | /* Config file header print. */ |
| 2255 | vty_out (file_vty, "!\n! Zebra configuration saved from vty\n! "); |
| 2256 | vty_time_print (file_vty, 1); |
| 2257 | vty_out (file_vty, "!\n"); |
| 2258 | |
| 2259 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2260 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2261 | { |
| 2262 | if ((*node->func) (file_vty)) |
| 2263 | vty_out (file_vty, "!\n"); |
| 2264 | } |
| 2265 | vty_close (file_vty); |
| 2266 | |
| 2267 | if (unlink (config_file_sav) != 0) |
| 2268 | if (errno != ENOENT) |
| 2269 | { |
| 2270 | vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, |
| 2271 | VTY_NEWLINE); |
| 2272 | free (config_file_sav); |
| 2273 | free (config_file_tmp); |
| 2274 | unlink (config_file_tmp); |
| 2275 | return CMD_WARNING; |
| 2276 | } |
| 2277 | if (link (config_file, config_file_sav) != 0) |
| 2278 | { |
| 2279 | vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, |
| 2280 | VTY_NEWLINE); |
| 2281 | free (config_file_sav); |
| 2282 | free (config_file_tmp); |
| 2283 | unlink (config_file_tmp); |
| 2284 | return CMD_WARNING; |
| 2285 | } |
| 2286 | sync (); |
| 2287 | if (unlink (config_file) != 0) |
| 2288 | { |
| 2289 | vty_out (vty, "Can't unlink configuration file %s.%s", config_file, |
| 2290 | VTY_NEWLINE); |
| 2291 | free (config_file_sav); |
| 2292 | free (config_file_tmp); |
| 2293 | unlink (config_file_tmp); |
| 2294 | return CMD_WARNING; |
| 2295 | } |
| 2296 | if (link (config_file_tmp, config_file) != 0) |
| 2297 | { |
| 2298 | vty_out (vty, "Can't save configuration file %s.%s", config_file, |
| 2299 | VTY_NEWLINE); |
| 2300 | free (config_file_sav); |
| 2301 | free (config_file_tmp); |
| 2302 | unlink (config_file_tmp); |
| 2303 | return CMD_WARNING; |
| 2304 | } |
| 2305 | unlink (config_file_tmp); |
| 2306 | sync (); |
| 2307 | |
| 2308 | free (config_file_sav); |
| 2309 | free (config_file_tmp); |
| 2310 | vty_out (vty, "Configuration saved to %s%s", config_file, |
| 2311 | VTY_NEWLINE); |
| 2312 | return CMD_SUCCESS; |
| 2313 | } |
| 2314 | |
| 2315 | ALIAS (config_write_file, |
| 2316 | config_write_cmd, |
| 2317 | "write", |
| 2318 | "Write running configuration to memory, network, or terminal\n") |
| 2319 | |
| 2320 | ALIAS (config_write_file, |
| 2321 | config_write_memory_cmd, |
| 2322 | "write memory", |
| 2323 | "Write running configuration to memory, network, or terminal\n" |
| 2324 | "Write configuration to the file (same as write file)\n") |
| 2325 | |
| 2326 | ALIAS (config_write_file, |
| 2327 | copy_runningconfig_startupconfig_cmd, |
| 2328 | "copy running-config startup-config", |
| 2329 | "Copy configuration\n" |
| 2330 | "Copy running config to... \n" |
| 2331 | "Copy running config to startup config (same as write file)\n") |
| 2332 | |
| 2333 | /* Write current configuration into the terminal. */ |
| 2334 | DEFUN (config_write_terminal, |
| 2335 | config_write_terminal_cmd, |
| 2336 | "write terminal", |
| 2337 | "Write running configuration to memory, network, or terminal\n" |
| 2338 | "Write to terminal\n") |
| 2339 | { |
| 2340 | int i; |
| 2341 | struct cmd_node *node; |
| 2342 | |
| 2343 | if (vty->type == VTY_SHELL_SERV) |
| 2344 | { |
| 2345 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2346 | if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh) |
| 2347 | { |
| 2348 | if ((*node->func) (vty)) |
| 2349 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2350 | } |
| 2351 | } |
| 2352 | else |
| 2353 | { |
| 2354 | vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 2355 | VTY_NEWLINE); |
| 2356 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2357 | |
| 2358 | for (i = 0; i < vector_max (cmdvec); i++) |
| 2359 | if ((node = vector_slot (cmdvec, i)) && node->func) |
| 2360 | { |
| 2361 | if ((*node->func) (vty)) |
| 2362 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2363 | } |
| 2364 | vty_out (vty, "end%s",VTY_NEWLINE); |
| 2365 | } |
| 2366 | return CMD_SUCCESS; |
| 2367 | } |
| 2368 | |
| 2369 | /* Write current configuration into the terminal. */ |
| 2370 | ALIAS (config_write_terminal, |
| 2371 | show_running_config_cmd, |
| 2372 | "show running-config", |
| 2373 | SHOW_STR |
| 2374 | "running configuration\n") |
| 2375 | |
| 2376 | /* Write startup configuration into the terminal. */ |
| 2377 | DEFUN (show_startup_config, |
| 2378 | show_startup_config_cmd, |
| 2379 | "show startup-config", |
| 2380 | SHOW_STR |
| 2381 | "Contentes of startup configuration\n") |
| 2382 | { |
| 2383 | char buf[BUFSIZ]; |
| 2384 | FILE *confp; |
| 2385 | |
| 2386 | confp = fopen (host.config, "r"); |
| 2387 | if (confp == NULL) |
| 2388 | { |
| 2389 | vty_out (vty, "Can't open configuration file [%s]%s", |
| 2390 | host.config, VTY_NEWLINE); |
| 2391 | return CMD_WARNING; |
| 2392 | } |
| 2393 | |
| 2394 | while (fgets (buf, BUFSIZ, confp)) |
| 2395 | { |
| 2396 | char *cp = buf; |
| 2397 | |
| 2398 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 2399 | cp++; |
| 2400 | *cp = '\0'; |
| 2401 | |
| 2402 | vty_out (vty, "%s%s", buf, VTY_NEWLINE); |
| 2403 | } |
| 2404 | |
| 2405 | fclose (confp); |
| 2406 | |
| 2407 | return CMD_SUCCESS; |
| 2408 | } |
| 2409 | |
| 2410 | /* Hostname configuration */ |
| 2411 | DEFUN (config_hostname, |
| 2412 | hostname_cmd, |
| 2413 | "hostname WORD", |
| 2414 | "Set system's network name\n" |
| 2415 | "This system's network name\n") |
| 2416 | { |
| 2417 | if (!isalpha((int) *argv[0])) |
| 2418 | { |
| 2419 | vty_out (vty, "Please specify string starting with alphabet%s", VTY_NEWLINE); |
| 2420 | return CMD_WARNING; |
| 2421 | } |
| 2422 | |
| 2423 | if (host.name) |
| 2424 | XFREE (0, host.name); |
| 2425 | |
| 2426 | host.name = strdup (argv[0]); |
| 2427 | return CMD_SUCCESS; |
| 2428 | } |
| 2429 | |
| 2430 | DEFUN (config_no_hostname, |
| 2431 | no_hostname_cmd, |
| 2432 | "no hostname [HOSTNAME]", |
| 2433 | NO_STR |
| 2434 | "Reset system's network name\n" |
| 2435 | "Host name of this router\n") |
| 2436 | { |
| 2437 | if (host.name) |
| 2438 | XFREE (0, host.name); |
| 2439 | host.name = NULL; |
| 2440 | return CMD_SUCCESS; |
| 2441 | } |
| 2442 | |
| 2443 | /* VTY interface password set. */ |
| 2444 | DEFUN (config_password, password_cmd, |
| 2445 | "password (8|) WORD", |
| 2446 | "Assign the terminal connection password\n" |
| 2447 | "Specifies a HIDDEN password will follow\n" |
| 2448 | "dummy string \n" |
| 2449 | "The HIDDEN line password string\n") |
| 2450 | { |
| 2451 | /* Argument check. */ |
| 2452 | if (argc == 0) |
| 2453 | { |
| 2454 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2455 | return CMD_WARNING; |
| 2456 | } |
| 2457 | |
| 2458 | if (argc == 2) |
| 2459 | { |
| 2460 | if (*argv[0] == '8') |
| 2461 | { |
| 2462 | if (host.password) |
| 2463 | XFREE (0, host.password); |
| 2464 | host.password = NULL; |
| 2465 | if (host.password_encrypt) |
| 2466 | XFREE (0, host.password_encrypt); |
| 2467 | host.password_encrypt = XSTRDUP (0, strdup (argv[1])); |
| 2468 | return CMD_SUCCESS; |
| 2469 | } |
| 2470 | else |
| 2471 | { |
| 2472 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2473 | return CMD_WARNING; |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | if (!isalnum ((int) *argv[0])) |
| 2478 | { |
| 2479 | vty_out (vty, |
| 2480 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2481 | return CMD_WARNING; |
| 2482 | } |
| 2483 | |
| 2484 | if (host.password) |
| 2485 | XFREE (0, host.password); |
| 2486 | host.password = NULL; |
| 2487 | |
| 2488 | if (host.encrypt) |
| 2489 | { |
| 2490 | if (host.password_encrypt) |
| 2491 | XFREE (0, host.password_encrypt); |
| 2492 | host.password_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2493 | } |
| 2494 | else |
| 2495 | host.password = XSTRDUP (0, argv[0]); |
| 2496 | |
| 2497 | return CMD_SUCCESS; |
| 2498 | } |
| 2499 | |
| 2500 | ALIAS (config_password, password_text_cmd, |
| 2501 | "password LINE", |
| 2502 | "Assign the terminal connection password\n" |
| 2503 | "The UNENCRYPTED (cleartext) line password\n") |
| 2504 | |
| 2505 | /* VTY enable password set. */ |
| 2506 | DEFUN (config_enable_password, enable_password_cmd, |
| 2507 | "enable password (8|) WORD", |
| 2508 | "Modify enable password parameters\n" |
| 2509 | "Assign the privileged level password\n" |
| 2510 | "Specifies a HIDDEN password will follow\n" |
| 2511 | "dummy string \n" |
| 2512 | "The HIDDEN 'enable' password string\n") |
| 2513 | { |
| 2514 | /* Argument check. */ |
| 2515 | if (argc == 0) |
| 2516 | { |
| 2517 | vty_out (vty, "Please specify password.%s", VTY_NEWLINE); |
| 2518 | return CMD_WARNING; |
| 2519 | } |
| 2520 | |
| 2521 | /* Crypt type is specified. */ |
| 2522 | if (argc == 2) |
| 2523 | { |
| 2524 | if (*argv[0] == '8') |
| 2525 | { |
| 2526 | if (host.enable) |
| 2527 | XFREE (0, host.enable); |
| 2528 | host.enable = NULL; |
| 2529 | |
| 2530 | if (host.enable_encrypt) |
| 2531 | XFREE (0, host.enable_encrypt); |
| 2532 | host.enable_encrypt = XSTRDUP (0, argv[1]); |
| 2533 | |
| 2534 | return CMD_SUCCESS; |
| 2535 | } |
| 2536 | else |
| 2537 | { |
| 2538 | vty_out (vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2539 | return CMD_WARNING; |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | if (!isalnum ((int) *argv[0])) |
| 2544 | { |
| 2545 | vty_out (vty, |
| 2546 | "Please specify string starting with alphanumeric%s", VTY_NEWLINE); |
| 2547 | return CMD_WARNING; |
| 2548 | } |
| 2549 | |
| 2550 | if (host.enable) |
| 2551 | XFREE (0, host.enable); |
| 2552 | host.enable = NULL; |
| 2553 | |
| 2554 | /* Plain password input. */ |
| 2555 | if (host.encrypt) |
| 2556 | { |
| 2557 | if (host.enable_encrypt) |
| 2558 | XFREE (0, host.enable_encrypt); |
| 2559 | host.enable_encrypt = XSTRDUP (0, zencrypt (argv[0])); |
| 2560 | } |
| 2561 | else |
| 2562 | host.enable = XSTRDUP (0, argv[0]); |
| 2563 | |
| 2564 | return CMD_SUCCESS; |
| 2565 | } |
| 2566 | |
| 2567 | ALIAS (config_enable_password, |
| 2568 | enable_password_text_cmd, |
| 2569 | "enable password LINE", |
| 2570 | "Modify enable password parameters\n" |
| 2571 | "Assign the privileged level password\n" |
| 2572 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 2573 | |
| 2574 | /* VTY enable password delete. */ |
| 2575 | DEFUN (no_config_enable_password, no_enable_password_cmd, |
| 2576 | "no enable password", |
| 2577 | NO_STR |
| 2578 | "Modify enable password parameters\n" |
| 2579 | "Assign the privileged level password\n") |
| 2580 | { |
| 2581 | if (host.enable) |
| 2582 | XFREE (0, host.enable); |
| 2583 | host.enable = NULL; |
| 2584 | |
| 2585 | if (host.enable_encrypt) |
| 2586 | XFREE (0, host.enable_encrypt); |
| 2587 | host.enable_encrypt = NULL; |
| 2588 | |
| 2589 | return CMD_SUCCESS; |
| 2590 | } |
| 2591 | |
| 2592 | DEFUN (service_password_encrypt, |
| 2593 | service_password_encrypt_cmd, |
| 2594 | "service password-encryption", |
| 2595 | "Set up miscellaneous service\n" |
| 2596 | "Enable encrypted passwords\n") |
| 2597 | { |
| 2598 | if (host.encrypt) |
| 2599 | return CMD_SUCCESS; |
| 2600 | |
| 2601 | host.encrypt = 1; |
| 2602 | |
| 2603 | if (host.password) |
| 2604 | { |
| 2605 | if (host.password_encrypt) |
| 2606 | XFREE (0, host.password_encrypt); |
| 2607 | host.password_encrypt = XSTRDUP (0, zencrypt (host.password)); |
| 2608 | } |
| 2609 | if (host.enable) |
| 2610 | { |
| 2611 | if (host.enable_encrypt) |
| 2612 | XFREE (0, host.enable_encrypt); |
| 2613 | host.enable_encrypt = XSTRDUP (0, zencrypt (host.enable)); |
| 2614 | } |
| 2615 | |
| 2616 | return CMD_SUCCESS; |
| 2617 | } |
| 2618 | |
| 2619 | DEFUN (no_service_password_encrypt, |
| 2620 | no_service_password_encrypt_cmd, |
| 2621 | "no service password-encryption", |
| 2622 | NO_STR |
| 2623 | "Set up miscellaneous service\n" |
| 2624 | "Enable encrypted passwords\n") |
| 2625 | { |
| 2626 | if (! host.encrypt) |
| 2627 | return CMD_SUCCESS; |
| 2628 | |
| 2629 | host.encrypt = 0; |
| 2630 | |
| 2631 | if (host.password_encrypt) |
| 2632 | XFREE (0, host.password_encrypt); |
| 2633 | host.password_encrypt = NULL; |
| 2634 | |
| 2635 | if (host.enable_encrypt) |
| 2636 | XFREE (0, host.enable_encrypt); |
| 2637 | host.enable_encrypt = NULL; |
| 2638 | |
| 2639 | return CMD_SUCCESS; |
| 2640 | } |
| 2641 | |
| 2642 | DEFUN (config_terminal_length, config_terminal_length_cmd, |
| 2643 | "terminal length <0-512>", |
| 2644 | "Set terminal line parameters\n" |
| 2645 | "Set number of lines on a screen\n" |
| 2646 | "Number of lines on screen (0 for no pausing)\n") |
| 2647 | { |
| 2648 | int lines; |
| 2649 | char *endptr = NULL; |
| 2650 | |
| 2651 | lines = strtol (argv[0], &endptr, 10); |
| 2652 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2653 | { |
| 2654 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2655 | return CMD_WARNING; |
| 2656 | } |
| 2657 | vty->lines = lines; |
| 2658 | |
| 2659 | return CMD_SUCCESS; |
| 2660 | } |
| 2661 | |
| 2662 | DEFUN (config_terminal_no_length, config_terminal_no_length_cmd, |
| 2663 | "terminal no length", |
| 2664 | "Set terminal line parameters\n" |
| 2665 | NO_STR |
| 2666 | "Set number of lines on a screen\n") |
| 2667 | { |
| 2668 | vty->lines = -1; |
| 2669 | return CMD_SUCCESS; |
| 2670 | } |
| 2671 | |
| 2672 | DEFUN (service_terminal_length, service_terminal_length_cmd, |
| 2673 | "service terminal-length <0-512>", |
| 2674 | "Set up miscellaneous service\n" |
| 2675 | "System wide terminal length configuration\n" |
| 2676 | "Number of lines of VTY (0 means no line control)\n") |
| 2677 | { |
| 2678 | int lines; |
| 2679 | char *endptr = NULL; |
| 2680 | |
| 2681 | lines = strtol (argv[0], &endptr, 10); |
| 2682 | if (lines < 0 || lines > 512 || *endptr != '\0') |
| 2683 | { |
| 2684 | vty_out (vty, "length is malformed%s", VTY_NEWLINE); |
| 2685 | return CMD_WARNING; |
| 2686 | } |
| 2687 | host.lines = lines; |
| 2688 | |
| 2689 | return CMD_SUCCESS; |
| 2690 | } |
| 2691 | |
| 2692 | DEFUN (no_service_terminal_length, no_service_terminal_length_cmd, |
| 2693 | "no service terminal-length [<0-512>]", |
| 2694 | NO_STR |
| 2695 | "Set up miscellaneous service\n" |
| 2696 | "System wide terminal length configuration\n" |
| 2697 | "Number of lines of VTY (0 means no line control)\n") |
| 2698 | { |
| 2699 | host.lines = -1; |
| 2700 | return CMD_SUCCESS; |
| 2701 | } |
| 2702 | |
| 2703 | DEFUN (config_log_stdout, |
| 2704 | config_log_stdout_cmd, |
| 2705 | "log stdout", |
| 2706 | "Logging control\n" |
| 2707 | "Logging goes to stdout\n") |
| 2708 | { |
| 2709 | zlog_set_flag (NULL, ZLOG_STDOUT); |
| 2710 | host.log_stdout = 1; |
| 2711 | return CMD_SUCCESS; |
| 2712 | } |
| 2713 | |
| 2714 | DEFUN (no_config_log_stdout, |
| 2715 | no_config_log_stdout_cmd, |
| 2716 | "no log stdout", |
| 2717 | NO_STR |
| 2718 | "Logging control\n" |
| 2719 | "Cancel logging to stdout\n") |
| 2720 | { |
| 2721 | zlog_reset_flag (NULL, ZLOG_STDOUT); |
| 2722 | host.log_stdout = 0; |
| 2723 | return CMD_SUCCESS; |
| 2724 | } |
| 2725 | |
| 2726 | DEFUN (config_log_file, |
| 2727 | config_log_file_cmd, |
| 2728 | "log file FILENAME", |
| 2729 | "Logging control\n" |
| 2730 | "Logging to file\n" |
| 2731 | "Logging filename\n") |
| 2732 | { |
| 2733 | int ret; |
| 2734 | char *cwd; |
| 2735 | char *fullpath; |
| 2736 | |
| 2737 | /* Path detection. */ |
| 2738 | if (! IS_DIRECTORY_SEP (*argv[0])) |
| 2739 | { |
| 2740 | cwd = getcwd (NULL, MAXPATHLEN); |
| 2741 | fullpath = XMALLOC (MTYPE_TMP, |
| 2742 | strlen (cwd) + strlen (argv[0]) + 2); |
| 2743 | sprintf (fullpath, "%s/%s", cwd, argv[0]); |
| 2744 | } |
| 2745 | else |
| 2746 | fullpath = argv[0]; |
| 2747 | |
| 2748 | ret = zlog_set_file (NULL, ZLOG_FILE, fullpath); |
| 2749 | |
| 2750 | if (!ret) |
| 2751 | { |
| 2752 | vty_out (vty, "can't open logfile %s\n", argv[0]); |
| 2753 | return CMD_WARNING; |
| 2754 | } |
| 2755 | |
| 2756 | if (host.logfile) |
| 2757 | XFREE (MTYPE_TMP, host.logfile); |
| 2758 | |
| 2759 | host.logfile = strdup (argv[0]); |
| 2760 | |
| 2761 | return CMD_SUCCESS; |
| 2762 | } |
| 2763 | |
| 2764 | DEFUN (no_config_log_file, |
| 2765 | no_config_log_file_cmd, |
| 2766 | "no log file [FILENAME]", |
| 2767 | NO_STR |
| 2768 | "Logging control\n" |
| 2769 | "Cancel logging to file\n" |
| 2770 | "Logging file name\n") |
| 2771 | { |
| 2772 | zlog_reset_file (NULL); |
| 2773 | |
| 2774 | if (host.logfile) |
| 2775 | XFREE (MTYPE_TMP, host.logfile); |
| 2776 | |
| 2777 | host.logfile = NULL; |
| 2778 | |
| 2779 | return CMD_SUCCESS; |
| 2780 | } |
| 2781 | |
| 2782 | DEFUN (config_log_syslog, |
| 2783 | config_log_syslog_cmd, |
| 2784 | "log syslog", |
| 2785 | "Logging control\n" |
| 2786 | "Logging goes to syslog\n") |
| 2787 | { |
| 2788 | zlog_set_flag (NULL, ZLOG_SYSLOG); |
| 2789 | host.log_syslog = 1; |
| 2790 | return CMD_SUCCESS; |
| 2791 | } |
| 2792 | |
| 2793 | DEFUN (no_config_log_syslog, |
| 2794 | no_config_log_syslog_cmd, |
| 2795 | "no log syslog", |
| 2796 | NO_STR |
| 2797 | "Logging control\n" |
| 2798 | "Cancel logging to syslog\n") |
| 2799 | { |
| 2800 | zlog_reset_flag (NULL, ZLOG_SYSLOG); |
| 2801 | host.log_syslog = 0; |
| 2802 | return CMD_SUCCESS; |
| 2803 | } |
| 2804 | |
| 2805 | DEFUN (config_log_trap, |
| 2806 | config_log_trap_cmd, |
| 2807 | "log trap (emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)", |
| 2808 | "Logging control\n" |
| 2809 | "Limit logging to specifed level\n") |
| 2810 | { |
| 2811 | int new_level ; |
| 2812 | |
| 2813 | for ( new_level = 0 ; zlog_priority [new_level] != NULL ; new_level ++ ) |
| 2814 | { |
| 2815 | if ( strcmp ( argv[0], zlog_priority [new_level] ) == 0 ) |
| 2816 | /* found new logging level */ |
| 2817 | { |
| 2818 | zlog_default->maskpri = new_level; |
| 2819 | return CMD_SUCCESS; |
| 2820 | } |
| 2821 | } |
| 2822 | return CMD_ERR_NO_MATCH; |
| 2823 | } |
| 2824 | |
| 2825 | DEFUN (no_config_log_trap, |
| 2826 | no_config_log_trap_cmd, |
| 2827 | "no log trap", |
| 2828 | NO_STR |
| 2829 | "Logging control\n" |
| 2830 | "Permit all logging information\n") |
| 2831 | { |
| 2832 | zlog_default->maskpri = LOG_DEBUG; |
| 2833 | return CMD_SUCCESS; |
| 2834 | } |
| 2835 | |
| 2836 | DEFUN (config_log_record_priority, |
| 2837 | config_log_record_priority_cmd, |
| 2838 | "log record-priority", |
| 2839 | "Logging control\n" |
| 2840 | "Log the priority of the message within the message\n") |
| 2841 | { |
| 2842 | zlog_default->record_priority = 1 ; |
| 2843 | return CMD_SUCCESS; |
| 2844 | } |
| 2845 | |
| 2846 | DEFUN (no_config_log_record_priority, |
| 2847 | no_config_log_record_priority_cmd, |
| 2848 | "no log record-priority", |
| 2849 | NO_STR |
| 2850 | "Logging control\n" |
| 2851 | "Do not log the priority of the message within the message\n") |
| 2852 | { |
| 2853 | zlog_default->record_priority = 0 ; |
| 2854 | return CMD_SUCCESS; |
| 2855 | } |
| 2856 | |
| 2857 | |
| 2858 | DEFUN (banner_motd_default, |
| 2859 | banner_motd_default_cmd, |
| 2860 | "banner motd default", |
| 2861 | "Set banner string\n" |
| 2862 | "Strings for motd\n" |
| 2863 | "Default string\n") |
| 2864 | { |
| 2865 | host.motd = default_motd; |
| 2866 | return CMD_SUCCESS; |
| 2867 | } |
| 2868 | |
| 2869 | DEFUN (no_banner_motd, |
| 2870 | no_banner_motd_cmd, |
| 2871 | "no banner motd", |
| 2872 | NO_STR |
| 2873 | "Set banner string\n" |
| 2874 | "Strings for motd\n") |
| 2875 | { |
| 2876 | host.motd = NULL; |
| 2877 | return CMD_SUCCESS; |
| 2878 | } |
| 2879 | |
| 2880 | /* Set config filename. Called from vty.c */ |
| 2881 | void |
| 2882 | host_config_set (char *filename) |
| 2883 | { |
| 2884 | host.config = strdup (filename); |
| 2885 | } |
| 2886 | |
| 2887 | void |
| 2888 | install_default (enum node_type node) |
| 2889 | { |
| 2890 | install_element (node, &config_exit_cmd); |
| 2891 | install_element (node, &config_quit_cmd); |
| 2892 | install_element (node, &config_end_cmd); |
| 2893 | install_element (node, &config_help_cmd); |
| 2894 | install_element (node, &config_list_cmd); |
| 2895 | |
| 2896 | install_element (node, &config_write_terminal_cmd); |
| 2897 | install_element (node, &config_write_file_cmd); |
| 2898 | install_element (node, &config_write_memory_cmd); |
| 2899 | install_element (node, &config_write_cmd); |
| 2900 | install_element (node, &show_running_config_cmd); |
| 2901 | } |
| 2902 | |
| 2903 | /* Initialize command interface. Install basic nodes and commands. */ |
| 2904 | void |
| 2905 | cmd_init (int terminal) |
| 2906 | { |
| 2907 | /* Allocate initial top vector of commands. */ |
| 2908 | cmdvec = vector_init (VECTOR_MIN_SIZE); |
| 2909 | |
| 2910 | /* Default host value settings. */ |
| 2911 | host.name = NULL; |
| 2912 | host.password = NULL; |
| 2913 | host.enable = NULL; |
| 2914 | host.logfile = NULL; |
| 2915 | host.config = NULL; |
| 2916 | host.lines = -1; |
| 2917 | host.motd = default_motd; |
| 2918 | |
| 2919 | /* Install top nodes. */ |
| 2920 | install_node (&view_node, NULL); |
| 2921 | install_node (&enable_node, NULL); |
| 2922 | install_node (&auth_node, NULL); |
| 2923 | install_node (&auth_enable_node, NULL); |
| 2924 | install_node (&config_node, config_write_host); |
| 2925 | |
| 2926 | /* Each node's basic commands. */ |
| 2927 | install_element (VIEW_NODE, &show_version_cmd); |
| 2928 | if (terminal) |
| 2929 | { |
| 2930 | install_element (VIEW_NODE, &config_list_cmd); |
| 2931 | install_element (VIEW_NODE, &config_exit_cmd); |
| 2932 | install_element (VIEW_NODE, &config_quit_cmd); |
| 2933 | install_element (VIEW_NODE, &config_help_cmd); |
| 2934 | install_element (VIEW_NODE, &config_enable_cmd); |
| 2935 | install_element (VIEW_NODE, &config_terminal_length_cmd); |
| 2936 | install_element (VIEW_NODE, &config_terminal_no_length_cmd); |
| 2937 | } |
| 2938 | |
| 2939 | if (terminal) |
| 2940 | { |
| 2941 | install_default (ENABLE_NODE); |
| 2942 | install_element (ENABLE_NODE, &config_disable_cmd); |
| 2943 | install_element (ENABLE_NODE, &config_terminal_cmd); |
| 2944 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
| 2945 | } |
| 2946 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
| 2947 | install_element (ENABLE_NODE, &show_version_cmd); |
| 2948 | install_element (ENABLE_NODE, &config_terminal_length_cmd); |
| 2949 | install_element (ENABLE_NODE, &config_terminal_no_length_cmd); |
| 2950 | |
| 2951 | if (terminal) |
| 2952 | install_default (CONFIG_NODE); |
| 2953 | install_element (CONFIG_NODE, &hostname_cmd); |
| 2954 | install_element (CONFIG_NODE, &no_hostname_cmd); |
| 2955 | install_element (CONFIG_NODE, &password_cmd); |
| 2956 | install_element (CONFIG_NODE, &password_text_cmd); |
| 2957 | install_element (CONFIG_NODE, &enable_password_cmd); |
| 2958 | install_element (CONFIG_NODE, &enable_password_text_cmd); |
| 2959 | install_element (CONFIG_NODE, &no_enable_password_cmd); |
| 2960 | if (terminal) |
| 2961 | { |
| 2962 | install_element (CONFIG_NODE, &config_log_stdout_cmd); |
| 2963 | install_element (CONFIG_NODE, &no_config_log_stdout_cmd); |
| 2964 | install_element (CONFIG_NODE, &config_log_file_cmd); |
| 2965 | install_element (CONFIG_NODE, &no_config_log_file_cmd); |
| 2966 | install_element (CONFIG_NODE, &config_log_syslog_cmd); |
| 2967 | install_element (CONFIG_NODE, &no_config_log_syslog_cmd); |
| 2968 | install_element (CONFIG_NODE, &config_log_trap_cmd); |
| 2969 | install_element (CONFIG_NODE, &no_config_log_trap_cmd); |
| 2970 | install_element (CONFIG_NODE, &config_log_record_priority_cmd); |
| 2971 | install_element (CONFIG_NODE, &no_config_log_record_priority_cmd); |
| 2972 | install_element (CONFIG_NODE, &service_password_encrypt_cmd); |
| 2973 | install_element (CONFIG_NODE, &no_service_password_encrypt_cmd); |
| 2974 | install_element (CONFIG_NODE, &banner_motd_default_cmd); |
| 2975 | install_element (CONFIG_NODE, &no_banner_motd_cmd); |
| 2976 | install_element (CONFIG_NODE, &service_terminal_length_cmd); |
| 2977 | install_element (CONFIG_NODE, &no_service_terminal_length_cmd); |
| 2978 | } |
| 2979 | |
| 2980 | srand(time(NULL)); |
| 2981 | } |