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