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: |
Harald Welte | a67cbd6 | 2010-05-01 16:46:11 +0200 | [diff] [blame] | 2367 | case SGSN_NODE: |
Holger Hans Peter Freyther | 1ea8dbe | 2010-04-05 22:42:18 +0200 | [diff] [blame] | 2368 | vty->node = CONFIG_NODE; |
| 2369 | vty->index = NULL; |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2370 | default: |
| 2371 | break; |
| 2372 | } |
| 2373 | return CMD_SUCCESS; |
| 2374 | } |
| 2375 | |
| 2376 | /* quit is alias of exit. */ |
Harald Welte | f32cc4b | 2010-05-01 11:01:46 +0200 | [diff] [blame] | 2377 | gALIAS(config_exit, |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2378 | config_quit_cmd, "quit", "Exit current mode and down to previous mode\n") |
| 2379 | |
| 2380 | /* End of configuration. */ |
Harald Welte | f32cc4b | 2010-05-01 11:01:46 +0200 | [diff] [blame] | 2381 | gDEFUN(config_end, |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2382 | config_end_cmd, "end", "End current mode and change to enable mode.") |
| 2383 | { |
| 2384 | switch (vty->node) { |
| 2385 | case VIEW_NODE: |
| 2386 | case ENABLE_NODE: |
| 2387 | /* Nothing to do. */ |
| 2388 | break; |
| 2389 | case CONFIG_NODE: |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2390 | case VTY_NODE: |
| 2391 | vty_config_unlock(vty); |
| 2392 | vty->node = ENABLE_NODE; |
| 2393 | break; |
| 2394 | default: |
| 2395 | break; |
| 2396 | } |
| 2397 | return CMD_SUCCESS; |
| 2398 | } |
| 2399 | |
| 2400 | /* Show version. */ |
| 2401 | DEFUN(show_version, |
| 2402 | show_version_cmd, "show version", SHOW_STR "Displays program version\n") |
| 2403 | { |
| 2404 | vty_out(vty, "%s %s (%s).%s", QUAGGA_PROGNAME, QUAGGA_VERSION, |
| 2405 | host.name ? host.name : "", VTY_NEWLINE); |
| 2406 | vty_out(vty, "%s%s", QUAGGA_COPYRIGHT, VTY_NEWLINE); |
| 2407 | |
| 2408 | return CMD_SUCCESS; |
| 2409 | } |
| 2410 | |
| 2411 | /* Help display function for all node. */ |
Harald Welte | f32cc4b | 2010-05-01 11:01:46 +0200 | [diff] [blame] | 2412 | gDEFUN(config_help, |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2413 | config_help_cmd, "help", "Description of the interactive help system\n") |
| 2414 | { |
| 2415 | vty_out(vty, |
| 2416 | "This VTY provides advanced help features. When you need help,%s\ |
| 2417 | anytime at the command line please press '?'.%s\ |
| 2418 | %s\ |
| 2419 | If nothing matches, the help list will be empty and you must backup%s\ |
| 2420 | until entering a '?' shows the available options.%s\ |
| 2421 | Two styles of help are provided:%s\ |
| 2422 | 1. Full help is available when you are ready to enter a%s\ |
| 2423 | command argument (e.g. 'show ?') and describes each possible%s\ |
| 2424 | argument.%s\ |
| 2425 | 2. Partial help is provided when an abbreviated argument is entered%s\ |
| 2426 | and you want to know what arguments match the input%s\ |
| 2427 | (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 2428 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); |
| 2429 | return CMD_SUCCESS; |
| 2430 | } |
| 2431 | |
| 2432 | /* Help display function for all node. */ |
Harald Welte | f32cc4b | 2010-05-01 11:01:46 +0200 | [diff] [blame] | 2433 | gDEFUN(config_list, config_list_cmd, "list", "Print command list\n") |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2434 | { |
| 2435 | unsigned int i; |
| 2436 | struct cmd_node *cnode = vector_slot(cmdvec, vty->node); |
| 2437 | struct cmd_element *cmd; |
| 2438 | |
| 2439 | for (i = 0; i < vector_active(cnode->cmd_vector); i++) |
| 2440 | if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL |
| 2441 | && !(cmd->attr == CMD_ATTR_DEPRECATED |
| 2442 | || cmd->attr == CMD_ATTR_HIDDEN)) |
| 2443 | vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE); |
| 2444 | return CMD_SUCCESS; |
| 2445 | } |
| 2446 | |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2447 | /* Write current configuration into file. */ |
| 2448 | DEFUN(config_write_file, |
| 2449 | config_write_file_cmd, |
| 2450 | "write file", |
| 2451 | "Write running configuration to memory, network, or terminal\n" |
| 2452 | "Write to configuration file\n") |
| 2453 | { |
| 2454 | unsigned int i; |
| 2455 | int fd; |
| 2456 | struct cmd_node *node; |
| 2457 | char *config_file; |
| 2458 | char *config_file_tmp = NULL; |
| 2459 | char *config_file_sav = NULL; |
| 2460 | struct vty *file_vty; |
| 2461 | |
| 2462 | /* Check and see if we are operating under vtysh configuration */ |
| 2463 | if (host.config == NULL) { |
| 2464 | vty_out(vty, "Can't save to configuration file, using vtysh.%s", |
| 2465 | VTY_NEWLINE); |
| 2466 | return CMD_WARNING; |
| 2467 | } |
| 2468 | |
| 2469 | /* Get filename. */ |
| 2470 | config_file = host.config; |
| 2471 | |
| 2472 | config_file_sav = |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2473 | _talloc_zero(tall_vty_cmd_ctx, |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2474 | strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1, |
| 2475 | "config_file_sav"); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2476 | strcpy(config_file_sav, config_file); |
| 2477 | strcat(config_file_sav, CONF_BACKUP_EXT); |
| 2478 | |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2479 | 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] | 2480 | "config_file_tmp"); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2481 | sprintf(config_file_tmp, "%s.XXXXXX", config_file); |
| 2482 | |
| 2483 | /* Open file to configuration write. */ |
| 2484 | fd = mkstemp(config_file_tmp); |
| 2485 | if (fd < 0) { |
| 2486 | vty_out(vty, "Can't open configuration file %s.%s", |
| 2487 | config_file_tmp, VTY_NEWLINE); |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2488 | talloc_free(config_file_tmp); |
| 2489 | talloc_free(config_file_sav); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2490 | return CMD_WARNING; |
| 2491 | } |
| 2492 | |
| 2493 | /* Make vty for configuration file. */ |
| 2494 | file_vty = vty_new(); |
| 2495 | file_vty->fd = fd; |
| 2496 | file_vty->type = VTY_FILE; |
| 2497 | |
| 2498 | /* Config file header print. */ |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2499 | vty_out(file_vty, "!\n! OpenBSC configuration saved from vty\n! "); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2500 | //vty_time_print (file_vty, 1); |
| 2501 | vty_out(file_vty, "!\n"); |
| 2502 | |
| 2503 | for (i = 0; i < vector_active(cmdvec); i++) |
| 2504 | if ((node = vector_slot(cmdvec, i)) && node->func) { |
| 2505 | if ((*node->func) (file_vty)) |
| 2506 | vty_out(file_vty, "!\n"); |
| 2507 | } |
| 2508 | vty_close(file_vty); |
| 2509 | |
| 2510 | if (unlink(config_file_sav) != 0) |
| 2511 | if (errno != ENOENT) { |
| 2512 | vty_out(vty, |
| 2513 | "Can't unlink backup configuration file %s.%s", |
| 2514 | config_file_sav, VTY_NEWLINE); |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2515 | talloc_free(config_file_sav); |
| 2516 | talloc_free(config_file_tmp); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2517 | unlink(config_file_tmp); |
| 2518 | return CMD_WARNING; |
| 2519 | } |
| 2520 | if (link(config_file, config_file_sav) != 0) { |
| 2521 | vty_out(vty, "Can't backup old configuration file %s.%s", |
| 2522 | config_file_sav, VTY_NEWLINE); |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2523 | talloc_free(config_file_sav); |
| 2524 | talloc_free(config_file_tmp); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2525 | unlink(config_file_tmp); |
| 2526 | return CMD_WARNING; |
| 2527 | } |
| 2528 | sync(); |
| 2529 | if (unlink(config_file) != 0) { |
| 2530 | vty_out(vty, "Can't unlink configuration file %s.%s", |
| 2531 | config_file, VTY_NEWLINE); |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2532 | talloc_free(config_file_sav); |
| 2533 | talloc_free(config_file_tmp); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2534 | unlink(config_file_tmp); |
| 2535 | return CMD_WARNING; |
| 2536 | } |
| 2537 | if (link(config_file_tmp, config_file) != 0) { |
| 2538 | vty_out(vty, "Can't save configuration file %s.%s", config_file, |
| 2539 | VTY_NEWLINE); |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2540 | talloc_free(config_file_sav); |
| 2541 | talloc_free(config_file_tmp); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2542 | unlink(config_file_tmp); |
| 2543 | return CMD_WARNING; |
| 2544 | } |
| 2545 | unlink(config_file_tmp); |
| 2546 | sync(); |
| 2547 | |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2548 | talloc_free(config_file_sav); |
| 2549 | talloc_free(config_file_tmp); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2550 | |
Stefan Schmidt | a29216b | 2009-08-12 13:46:13 +0200 | [diff] [blame] | 2551 | if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) { |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2552 | vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s", |
Harald Welte | 4a3023d | 2009-08-06 18:50:10 +0200 | [diff] [blame] | 2553 | config_file, strerror(errno), errno, VTY_NEWLINE); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2554 | return CMD_WARNING; |
| 2555 | } |
| 2556 | |
| 2557 | vty_out(vty, "Configuration saved to %s%s", config_file, VTY_NEWLINE); |
| 2558 | return CMD_SUCCESS; |
| 2559 | } |
| 2560 | |
| 2561 | ALIAS(config_write_file, |
| 2562 | config_write_cmd, |
| 2563 | "write", "Write running configuration to memory, network, or terminal\n") |
| 2564 | |
| 2565 | ALIAS(config_write_file, |
| 2566 | config_write_memory_cmd, |
| 2567 | "write memory", |
| 2568 | "Write running configuration to memory, network, or terminal\n" |
| 2569 | "Write configuration to the file (same as write file)\n") |
| 2570 | |
| 2571 | ALIAS(config_write_file, |
| 2572 | copy_runningconfig_startupconfig_cmd, |
| 2573 | "copy running-config startup-config", |
| 2574 | "Copy configuration\n" |
| 2575 | "Copy running config to... \n" |
| 2576 | "Copy running config to startup config (same as write file)\n") |
| 2577 | |
| 2578 | /* Write current configuration into the terminal. */ |
| 2579 | DEFUN(config_write_terminal, |
| 2580 | config_write_terminal_cmd, |
| 2581 | "write terminal", |
| 2582 | "Write running configuration to memory, network, or terminal\n" |
| 2583 | "Write to terminal\n") |
| 2584 | { |
| 2585 | unsigned int i; |
| 2586 | struct cmd_node *node; |
| 2587 | |
| 2588 | if (vty->type == VTY_SHELL_SERV) { |
| 2589 | for (i = 0; i < vector_active(cmdvec); i++) |
| 2590 | if ((node = vector_slot(cmdvec, i)) && node->func |
| 2591 | && node->vtysh) { |
| 2592 | if ((*node->func) (vty)) |
| 2593 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 2594 | } |
| 2595 | } else { |
| 2596 | vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 2597 | VTY_NEWLINE); |
| 2598 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 2599 | |
| 2600 | for (i = 0; i < vector_active(cmdvec); i++) |
| 2601 | if ((node = vector_slot(cmdvec, i)) && node->func) { |
| 2602 | if ((*node->func) (vty)) |
| 2603 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 2604 | } |
| 2605 | vty_out(vty, "end%s", VTY_NEWLINE); |
| 2606 | } |
| 2607 | return CMD_SUCCESS; |
| 2608 | } |
| 2609 | |
| 2610 | /* Write current configuration into the terminal. */ |
| 2611 | ALIAS(config_write_terminal, |
| 2612 | show_running_config_cmd, |
| 2613 | "show running-config", SHOW_STR "running configuration\n") |
| 2614 | |
| 2615 | /* Write startup configuration into the terminal. */ |
| 2616 | DEFUN(show_startup_config, |
| 2617 | show_startup_config_cmd, |
| 2618 | "show startup-config", SHOW_STR "Contentes of startup configuration\n") |
| 2619 | { |
| 2620 | char buf[BUFSIZ]; |
| 2621 | FILE *confp; |
| 2622 | |
| 2623 | confp = fopen(host.config, "r"); |
| 2624 | if (confp == NULL) { |
| 2625 | vty_out(vty, "Can't open configuration file [%s]%s", |
| 2626 | host.config, VTY_NEWLINE); |
| 2627 | return CMD_WARNING; |
| 2628 | } |
| 2629 | |
| 2630 | while (fgets(buf, BUFSIZ, confp)) { |
| 2631 | char *cp = buf; |
| 2632 | |
| 2633 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 2634 | cp++; |
| 2635 | *cp = '\0'; |
| 2636 | |
| 2637 | vty_out(vty, "%s%s", buf, VTY_NEWLINE); |
| 2638 | } |
| 2639 | |
| 2640 | fclose(confp); |
| 2641 | |
| 2642 | return CMD_SUCCESS; |
| 2643 | } |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2644 | |
| 2645 | /* Hostname configuration */ |
| 2646 | DEFUN(config_hostname, |
| 2647 | hostname_cmd, |
| 2648 | "hostname WORD", |
| 2649 | "Set system's network name\n" "This system's network name\n") |
| 2650 | { |
| 2651 | if (!isalpha((int)*argv[0])) { |
| 2652 | vty_out(vty, "Please specify string starting with alphabet%s", |
| 2653 | VTY_NEWLINE); |
| 2654 | return CMD_WARNING; |
| 2655 | } |
| 2656 | |
| 2657 | if (host.name) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2658 | talloc_free(host.name); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2659 | |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2660 | host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2661 | return CMD_SUCCESS; |
| 2662 | } |
| 2663 | |
| 2664 | DEFUN(config_no_hostname, |
| 2665 | no_hostname_cmd, |
| 2666 | "no hostname [HOSTNAME]", |
| 2667 | NO_STR "Reset system's network name\n" "Host name of this router\n") |
| 2668 | { |
| 2669 | if (host.name) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2670 | talloc_free(host.name); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2671 | host.name = NULL; |
| 2672 | return CMD_SUCCESS; |
| 2673 | } |
| 2674 | |
| 2675 | /* VTY interface password set. */ |
| 2676 | DEFUN(config_password, password_cmd, |
| 2677 | "password (8|) WORD", |
| 2678 | "Assign the terminal connection password\n" |
| 2679 | "Specifies a HIDDEN password will follow\n" |
| 2680 | "dummy string \n" "The HIDDEN line password string\n") |
| 2681 | { |
| 2682 | /* Argument check. */ |
| 2683 | if (argc == 0) { |
| 2684 | vty_out(vty, "Please specify password.%s", VTY_NEWLINE); |
| 2685 | return CMD_WARNING; |
| 2686 | } |
| 2687 | |
| 2688 | if (argc == 2) { |
| 2689 | if (*argv[0] == '8') { |
| 2690 | if (host.password) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2691 | talloc_free(host.password); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2692 | host.password = NULL; |
| 2693 | if (host.password_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2694 | talloc_free(host.password_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2695 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2696 | return CMD_SUCCESS; |
| 2697 | } else { |
| 2698 | vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2699 | return CMD_WARNING; |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | if (!isalnum((int)*argv[0])) { |
| 2704 | vty_out(vty, |
| 2705 | "Please specify string starting with alphanumeric%s", |
| 2706 | VTY_NEWLINE); |
| 2707 | return CMD_WARNING; |
| 2708 | } |
| 2709 | |
| 2710 | if (host.password) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2711 | talloc_free(host.password); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2712 | host.password = NULL; |
| 2713 | |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2714 | #ifdef VTY_CRYPT_PW |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2715 | if (host.encrypt) { |
| 2716 | if (host.password_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2717 | talloc_free(host.password_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2718 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0])); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2719 | } else |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2720 | #endif |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2721 | host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2722 | |
| 2723 | return CMD_SUCCESS; |
| 2724 | } |
| 2725 | |
| 2726 | ALIAS(config_password, password_text_cmd, |
| 2727 | "password LINE", |
| 2728 | "Assign the terminal connection password\n" |
| 2729 | "The UNENCRYPTED (cleartext) line password\n") |
| 2730 | |
| 2731 | /* VTY enable password set. */ |
| 2732 | DEFUN(config_enable_password, enable_password_cmd, |
| 2733 | "enable password (8|) WORD", |
| 2734 | "Modify enable password parameters\n" |
| 2735 | "Assign the privileged level password\n" |
| 2736 | "Specifies a HIDDEN password will follow\n" |
| 2737 | "dummy string \n" "The HIDDEN 'enable' password string\n") |
| 2738 | { |
| 2739 | /* Argument check. */ |
| 2740 | if (argc == 0) { |
| 2741 | vty_out(vty, "Please specify password.%s", VTY_NEWLINE); |
| 2742 | return CMD_WARNING; |
| 2743 | } |
| 2744 | |
| 2745 | /* Crypt type is specified. */ |
| 2746 | if (argc == 2) { |
| 2747 | if (*argv[0] == '8') { |
| 2748 | if (host.enable) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2749 | talloc_free(host.enable); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2750 | host.enable = NULL; |
| 2751 | |
| 2752 | if (host.enable_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2753 | talloc_free(host.enable_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2754 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2755 | |
| 2756 | return CMD_SUCCESS; |
| 2757 | } else { |
| 2758 | vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 2759 | return CMD_WARNING; |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | if (!isalnum((int)*argv[0])) { |
| 2764 | vty_out(vty, |
| 2765 | "Please specify string starting with alphanumeric%s", |
| 2766 | VTY_NEWLINE); |
| 2767 | return CMD_WARNING; |
| 2768 | } |
| 2769 | |
| 2770 | if (host.enable) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2771 | talloc_free(host.enable); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2772 | host.enable = NULL; |
| 2773 | |
| 2774 | /* Plain password input. */ |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2775 | #ifdef VTY_CRYPT_PW |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2776 | if (host.encrypt) { |
| 2777 | if (host.enable_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2778 | talloc_free(host.enable_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2779 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0])); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2780 | } else |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2781 | #endif |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2782 | host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2783 | |
| 2784 | return CMD_SUCCESS; |
| 2785 | } |
| 2786 | |
| 2787 | ALIAS(config_enable_password, |
| 2788 | enable_password_text_cmd, |
| 2789 | "enable password LINE", |
| 2790 | "Modify enable password parameters\n" |
| 2791 | "Assign the privileged level password\n" |
| 2792 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 2793 | |
| 2794 | /* VTY enable password delete. */ |
| 2795 | DEFUN(no_config_enable_password, no_enable_password_cmd, |
| 2796 | "no enable password", |
| 2797 | NO_STR |
| 2798 | "Modify enable password parameters\n" |
| 2799 | "Assign the privileged level password\n") |
| 2800 | { |
| 2801 | if (host.enable) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2802 | talloc_free(host.enable); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2803 | host.enable = NULL; |
| 2804 | |
| 2805 | if (host.enable_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2806 | talloc_free(host.enable_encrypt); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2807 | host.enable_encrypt = NULL; |
| 2808 | |
| 2809 | return CMD_SUCCESS; |
| 2810 | } |
| 2811 | |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2812 | #ifdef VTY_CRYPT_PW |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2813 | DEFUN(service_password_encrypt, |
| 2814 | service_password_encrypt_cmd, |
| 2815 | "service password-encryption", |
| 2816 | "Set up miscellaneous service\n" "Enable encrypted passwords\n") |
| 2817 | { |
| 2818 | if (host.encrypt) |
| 2819 | return CMD_SUCCESS; |
| 2820 | |
| 2821 | host.encrypt = 1; |
| 2822 | |
| 2823 | if (host.password) { |
| 2824 | if (host.password_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2825 | talloc_free(host.password_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2826 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password)); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2827 | } |
| 2828 | if (host.enable) { |
| 2829 | if (host.enable_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2830 | talloc_free(host.enable_encrypt); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 2831 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable)); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | return CMD_SUCCESS; |
| 2835 | } |
| 2836 | |
| 2837 | DEFUN(no_service_password_encrypt, |
| 2838 | no_service_password_encrypt_cmd, |
| 2839 | "no service password-encryption", |
| 2840 | NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n") |
| 2841 | { |
| 2842 | if (!host.encrypt) |
| 2843 | return CMD_SUCCESS; |
| 2844 | |
| 2845 | host.encrypt = 0; |
| 2846 | |
| 2847 | if (host.password_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2848 | talloc_free(host.password_encrypt); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2849 | host.password_encrypt = NULL; |
| 2850 | |
| 2851 | if (host.enable_encrypt) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2852 | talloc_free(host.enable_encrypt); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2853 | host.enable_encrypt = NULL; |
| 2854 | |
| 2855 | return CMD_SUCCESS; |
| 2856 | } |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 2857 | #endif |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2858 | |
| 2859 | DEFUN(config_terminal_length, config_terminal_length_cmd, |
| 2860 | "terminal length <0-512>", |
| 2861 | "Set terminal line parameters\n" |
| 2862 | "Set number of lines on a screen\n" |
| 2863 | "Number of lines on screen (0 for no pausing)\n") |
| 2864 | { |
| 2865 | int lines; |
| 2866 | char *endptr = NULL; |
| 2867 | |
| 2868 | lines = strtol(argv[0], &endptr, 10); |
| 2869 | if (lines < 0 || lines > 512 || *endptr != '\0') { |
| 2870 | vty_out(vty, "length is malformed%s", VTY_NEWLINE); |
| 2871 | return CMD_WARNING; |
| 2872 | } |
| 2873 | vty->lines = lines; |
| 2874 | |
| 2875 | return CMD_SUCCESS; |
| 2876 | } |
| 2877 | |
| 2878 | DEFUN(config_terminal_no_length, config_terminal_no_length_cmd, |
| 2879 | "terminal no length", |
| 2880 | "Set terminal line parameters\n" |
| 2881 | NO_STR "Set number of lines on a screen\n") |
| 2882 | { |
| 2883 | vty->lines = -1; |
| 2884 | return CMD_SUCCESS; |
| 2885 | } |
| 2886 | |
| 2887 | DEFUN(service_terminal_length, service_terminal_length_cmd, |
| 2888 | "service terminal-length <0-512>", |
| 2889 | "Set up miscellaneous service\n" |
| 2890 | "System wide terminal length configuration\n" |
| 2891 | "Number of lines of VTY (0 means no line control)\n") |
| 2892 | { |
| 2893 | int lines; |
| 2894 | char *endptr = NULL; |
| 2895 | |
| 2896 | lines = strtol(argv[0], &endptr, 10); |
| 2897 | if (lines < 0 || lines > 512 || *endptr != '\0') { |
| 2898 | vty_out(vty, "length is malformed%s", VTY_NEWLINE); |
| 2899 | return CMD_WARNING; |
| 2900 | } |
| 2901 | host.lines = lines; |
| 2902 | |
| 2903 | return CMD_SUCCESS; |
| 2904 | } |
| 2905 | |
| 2906 | DEFUN(no_service_terminal_length, no_service_terminal_length_cmd, |
| 2907 | "no service terminal-length [<0-512>]", |
| 2908 | NO_STR |
| 2909 | "Set up miscellaneous service\n" |
| 2910 | "System wide terminal length configuration\n" |
| 2911 | "Number of lines of VTY (0 means no line control)\n") |
| 2912 | { |
| 2913 | host.lines = -1; |
| 2914 | return CMD_SUCCESS; |
| 2915 | } |
| 2916 | |
| 2917 | DEFUN_HIDDEN(do_echo, |
| 2918 | echo_cmd, |
| 2919 | "echo .MESSAGE", |
| 2920 | "Echo a message back to the vty\n" "The message to echo\n") |
| 2921 | { |
| 2922 | char *message; |
| 2923 | |
| 2924 | vty_out(vty, "%s%s", |
| 2925 | ((message = |
| 2926 | argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE); |
| 2927 | if (message) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2928 | talloc_free(message); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2929 | return CMD_SUCCESS; |
| 2930 | } |
| 2931 | |
| 2932 | #if 0 |
| 2933 | DEFUN(config_logmsg, |
| 2934 | config_logmsg_cmd, |
| 2935 | "logmsg " LOG_LEVELS " .MESSAGE", |
| 2936 | "Send a message to enabled logging destinations\n" |
| 2937 | LOG_LEVEL_DESC "The message to send\n") |
| 2938 | { |
| 2939 | int level; |
| 2940 | char *message; |
| 2941 | |
| 2942 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 2943 | return CMD_ERR_NO_MATCH; |
| 2944 | |
| 2945 | zlog(NULL, level, |
| 2946 | ((message = argv_concat(argv, argc, 1)) ? message : "")); |
| 2947 | if (message) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 2948 | talloc_free(message); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 2949 | return CMD_SUCCESS; |
| 2950 | } |
| 2951 | |
| 2952 | DEFUN(show_logging, |
| 2953 | show_logging_cmd, |
| 2954 | "show logging", SHOW_STR "Show current logging configuration\n") |
| 2955 | { |
| 2956 | struct zlog *zl = zlog_default; |
| 2957 | |
| 2958 | vty_out(vty, "Syslog logging: "); |
| 2959 | if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED) |
| 2960 | vty_out(vty, "disabled"); |
| 2961 | else |
| 2962 | vty_out(vty, "level %s, facility %s, ident %s", |
| 2963 | zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]], |
| 2964 | facility_name(zl->facility), zl->ident); |
| 2965 | vty_out(vty, "%s", VTY_NEWLINE); |
| 2966 | |
| 2967 | vty_out(vty, "Stdout logging: "); |
| 2968 | if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED) |
| 2969 | vty_out(vty, "disabled"); |
| 2970 | else |
| 2971 | vty_out(vty, "level %s", |
| 2972 | zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]); |
| 2973 | vty_out(vty, "%s", VTY_NEWLINE); |
| 2974 | |
| 2975 | vty_out(vty, "Monitor logging: "); |
| 2976 | if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 2977 | vty_out(vty, "disabled"); |
| 2978 | else |
| 2979 | vty_out(vty, "level %s", |
| 2980 | zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]); |
| 2981 | vty_out(vty, "%s", VTY_NEWLINE); |
| 2982 | |
| 2983 | vty_out(vty, "File logging: "); |
| 2984 | if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp) |
| 2985 | vty_out(vty, "disabled"); |
| 2986 | else |
| 2987 | vty_out(vty, "level %s, filename %s", |
| 2988 | zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]], |
| 2989 | zl->filename); |
| 2990 | vty_out(vty, "%s", VTY_NEWLINE); |
| 2991 | |
| 2992 | vty_out(vty, "Protocol name: %s%s", |
| 2993 | zlog_proto_names[zl->protocol], VTY_NEWLINE); |
| 2994 | vty_out(vty, "Record priority: %s%s", |
| 2995 | (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE); |
| 2996 | |
| 2997 | return CMD_SUCCESS; |
| 2998 | } |
| 2999 | |
| 3000 | DEFUN(config_log_stdout, |
| 3001 | config_log_stdout_cmd, |
| 3002 | "log stdout", "Logging control\n" "Set stdout logging level\n") |
| 3003 | { |
| 3004 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
| 3005 | return CMD_SUCCESS; |
| 3006 | } |
| 3007 | |
| 3008 | DEFUN(config_log_stdout_level, |
| 3009 | config_log_stdout_level_cmd, |
| 3010 | "log stdout " LOG_LEVELS, |
| 3011 | "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC) |
| 3012 | { |
| 3013 | int level; |
| 3014 | |
| 3015 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3016 | return CMD_ERR_NO_MATCH; |
| 3017 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, level); |
| 3018 | return CMD_SUCCESS; |
| 3019 | } |
| 3020 | |
| 3021 | DEFUN(no_config_log_stdout, |
| 3022 | no_config_log_stdout_cmd, |
| 3023 | "no log stdout [LEVEL]", |
| 3024 | NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n") |
| 3025 | { |
| 3026 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); |
| 3027 | return CMD_SUCCESS; |
| 3028 | } |
| 3029 | |
| 3030 | DEFUN(config_log_monitor, |
| 3031 | config_log_monitor_cmd, |
| 3032 | "log monitor", |
| 3033 | "Logging control\n" "Set terminal line (monitor) logging level\n") |
| 3034 | { |
| 3035 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); |
| 3036 | return CMD_SUCCESS; |
| 3037 | } |
| 3038 | |
| 3039 | DEFUN(config_log_monitor_level, |
| 3040 | config_log_monitor_level_cmd, |
| 3041 | "log monitor " LOG_LEVELS, |
| 3042 | "Logging control\n" |
| 3043 | "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC) |
| 3044 | { |
| 3045 | int level; |
| 3046 | |
| 3047 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3048 | return CMD_ERR_NO_MATCH; |
| 3049 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, level); |
| 3050 | return CMD_SUCCESS; |
| 3051 | } |
| 3052 | |
| 3053 | DEFUN(no_config_log_monitor, |
| 3054 | no_config_log_monitor_cmd, |
| 3055 | "no log monitor [LEVEL]", |
| 3056 | NO_STR |
| 3057 | "Logging control\n" |
| 3058 | "Disable terminal line (monitor) logging\n" "Logging level\n") |
| 3059 | { |
| 3060 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
| 3061 | return CMD_SUCCESS; |
| 3062 | } |
| 3063 | |
| 3064 | static int set_log_file(struct vty *vty, const char *fname, int loglevel) |
| 3065 | { |
| 3066 | int ret; |
| 3067 | char *p = NULL; |
| 3068 | const char *fullpath; |
| 3069 | |
| 3070 | /* Path detection. */ |
| 3071 | if (!IS_DIRECTORY_SEP(*fname)) { |
| 3072 | char cwd[MAXPATHLEN + 1]; |
| 3073 | cwd[MAXPATHLEN] = '\0'; |
| 3074 | |
| 3075 | if (getcwd(cwd, MAXPATHLEN) == NULL) { |
| 3076 | zlog_err("config_log_file: Unable to alloc mem!"); |
| 3077 | return CMD_WARNING; |
| 3078 | } |
| 3079 | |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3080 | if ((p = _talloc_zero(tall_vcmd_ctx, |
| 3081 | strlen(cwd) + strlen(fname) + 2), |
| 3082 | "set_log_file") |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3083 | == NULL) { |
| 3084 | zlog_err("config_log_file: Unable to alloc mem!"); |
| 3085 | return CMD_WARNING; |
| 3086 | } |
| 3087 | sprintf(p, "%s/%s", cwd, fname); |
| 3088 | fullpath = p; |
| 3089 | } else |
| 3090 | fullpath = fname; |
| 3091 | |
| 3092 | ret = zlog_set_file(NULL, fullpath, loglevel); |
| 3093 | |
| 3094 | if (p) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3095 | talloc_free(p); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3096 | |
| 3097 | if (!ret) { |
| 3098 | vty_out(vty, "can't open logfile %s\n", fname); |
| 3099 | return CMD_WARNING; |
| 3100 | } |
| 3101 | |
| 3102 | if (host.logfile) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3103 | talloc_free(host.logfile); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3104 | |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 3105 | host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3106 | |
| 3107 | return CMD_SUCCESS; |
| 3108 | } |
| 3109 | |
| 3110 | DEFUN(config_log_file, |
| 3111 | config_log_file_cmd, |
| 3112 | "log file FILENAME", |
| 3113 | "Logging control\n" "Logging to file\n" "Logging filename\n") |
| 3114 | { |
| 3115 | return set_log_file(vty, argv[0], zlog_default->default_lvl); |
| 3116 | } |
| 3117 | |
| 3118 | DEFUN(config_log_file_level, |
| 3119 | config_log_file_level_cmd, |
| 3120 | "log file FILENAME " LOG_LEVELS, |
| 3121 | "Logging control\n" |
| 3122 | "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC) |
| 3123 | { |
| 3124 | int level; |
| 3125 | |
| 3126 | if ((level = level_match(argv[1])) == ZLOG_DISABLED) |
| 3127 | return CMD_ERR_NO_MATCH; |
| 3128 | return set_log_file(vty, argv[0], level); |
| 3129 | } |
| 3130 | |
| 3131 | DEFUN(no_config_log_file, |
| 3132 | no_config_log_file_cmd, |
| 3133 | "no log file [FILENAME]", |
| 3134 | NO_STR |
| 3135 | "Logging control\n" "Cancel logging to file\n" "Logging file name\n") |
| 3136 | { |
| 3137 | zlog_reset_file(NULL); |
| 3138 | |
| 3139 | if (host.logfile) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3140 | talloc_free(host.logfile); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3141 | |
| 3142 | host.logfile = NULL; |
| 3143 | |
| 3144 | return CMD_SUCCESS; |
| 3145 | } |
| 3146 | |
| 3147 | ALIAS(no_config_log_file, |
| 3148 | no_config_log_file_level_cmd, |
| 3149 | "no log file FILENAME LEVEL", |
| 3150 | NO_STR |
| 3151 | "Logging control\n" |
| 3152 | "Cancel logging to file\n" "Logging file name\n" "Logging level\n") |
| 3153 | |
| 3154 | DEFUN(config_log_syslog, |
| 3155 | config_log_syslog_cmd, |
| 3156 | "log syslog", "Logging control\n" "Set syslog logging level\n") |
| 3157 | { |
| 3158 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
| 3159 | return CMD_SUCCESS; |
| 3160 | } |
| 3161 | |
| 3162 | DEFUN(config_log_syslog_level, |
| 3163 | config_log_syslog_level_cmd, |
| 3164 | "log syslog " LOG_LEVELS, |
| 3165 | "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC) |
| 3166 | { |
| 3167 | int level; |
| 3168 | |
| 3169 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3170 | return CMD_ERR_NO_MATCH; |
| 3171 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level); |
| 3172 | return CMD_SUCCESS; |
| 3173 | } |
| 3174 | |
| 3175 | DEFUN_DEPRECATED(config_log_syslog_facility, |
| 3176 | config_log_syslog_facility_cmd, |
| 3177 | "log syslog facility " LOG_FACILITIES, |
| 3178 | "Logging control\n" |
| 3179 | "Logging goes to syslog\n" |
| 3180 | "(Deprecated) Facility parameter for syslog messages\n" |
| 3181 | LOG_FACILITY_DESC) |
| 3182 | { |
| 3183 | int facility; |
| 3184 | |
| 3185 | if ((facility = facility_match(argv[0])) < 0) |
| 3186 | return CMD_ERR_NO_MATCH; |
| 3187 | |
| 3188 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
| 3189 | zlog_default->facility = facility; |
| 3190 | return CMD_SUCCESS; |
| 3191 | } |
| 3192 | |
| 3193 | DEFUN(no_config_log_syslog, |
| 3194 | no_config_log_syslog_cmd, |
| 3195 | "no log syslog [LEVEL]", |
| 3196 | NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n") |
| 3197 | { |
| 3198 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
| 3199 | return CMD_SUCCESS; |
| 3200 | } |
| 3201 | |
| 3202 | ALIAS(no_config_log_syslog, |
| 3203 | no_config_log_syslog_facility_cmd, |
| 3204 | "no log syslog facility " LOG_FACILITIES, |
| 3205 | NO_STR |
| 3206 | "Logging control\n" |
| 3207 | "Logging goes to syslog\n" |
| 3208 | "Facility parameter for syslog messages\n" LOG_FACILITY_DESC) |
| 3209 | |
| 3210 | DEFUN(config_log_facility, |
| 3211 | config_log_facility_cmd, |
| 3212 | "log facility " LOG_FACILITIES, |
| 3213 | "Logging control\n" |
| 3214 | "Facility parameter for syslog messages\n" LOG_FACILITY_DESC) |
| 3215 | { |
| 3216 | int facility; |
| 3217 | |
| 3218 | if ((facility = facility_match(argv[0])) < 0) |
| 3219 | return CMD_ERR_NO_MATCH; |
| 3220 | zlog_default->facility = facility; |
| 3221 | return CMD_SUCCESS; |
| 3222 | } |
| 3223 | |
| 3224 | DEFUN(no_config_log_facility, |
| 3225 | no_config_log_facility_cmd, |
| 3226 | "no log facility [FACILITY]", |
| 3227 | NO_STR |
| 3228 | "Logging control\n" |
| 3229 | "Reset syslog facility to default (daemon)\n" "Syslog facility\n") |
| 3230 | { |
| 3231 | zlog_default->facility = LOG_DAEMON; |
| 3232 | return CMD_SUCCESS; |
| 3233 | } |
| 3234 | |
| 3235 | DEFUN_DEPRECATED(config_log_trap, |
| 3236 | config_log_trap_cmd, |
| 3237 | "log trap " LOG_LEVELS, |
| 3238 | "Logging control\n" |
| 3239 | "(Deprecated) Set logging level and default for all destinations\n" |
| 3240 | LOG_LEVEL_DESC) |
| 3241 | { |
| 3242 | int new_level; |
| 3243 | int i; |
| 3244 | |
| 3245 | if ((new_level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3246 | return CMD_ERR_NO_MATCH; |
| 3247 | |
| 3248 | zlog_default->default_lvl = new_level; |
| 3249 | for (i = 0; i < ZLOG_NUM_DESTS; i++) |
| 3250 | if (zlog_default->maxlvl[i] != ZLOG_DISABLED) |
| 3251 | zlog_default->maxlvl[i] = new_level; |
| 3252 | return CMD_SUCCESS; |
| 3253 | } |
| 3254 | |
| 3255 | DEFUN_DEPRECATED(no_config_log_trap, |
| 3256 | no_config_log_trap_cmd, |
| 3257 | "no log trap [LEVEL]", |
| 3258 | NO_STR |
| 3259 | "Logging control\n" |
| 3260 | "Permit all logging information\n" "Logging level\n") |
| 3261 | { |
| 3262 | zlog_default->default_lvl = LOG_DEBUG; |
| 3263 | return CMD_SUCCESS; |
| 3264 | } |
| 3265 | |
| 3266 | DEFUN(config_log_record_priority, |
| 3267 | config_log_record_priority_cmd, |
| 3268 | "log record-priority", |
| 3269 | "Logging control\n" |
| 3270 | "Log the priority of the message within the message\n") |
| 3271 | { |
| 3272 | zlog_default->record_priority = 1; |
| 3273 | return CMD_SUCCESS; |
| 3274 | } |
| 3275 | |
| 3276 | DEFUN(no_config_log_record_priority, |
| 3277 | no_config_log_record_priority_cmd, |
| 3278 | "no log record-priority", |
| 3279 | NO_STR |
| 3280 | "Logging control\n" |
| 3281 | "Do not log the priority of the message within the message\n") |
| 3282 | { |
| 3283 | zlog_default->record_priority = 0; |
| 3284 | return CMD_SUCCESS; |
| 3285 | } |
| 3286 | #endif |
| 3287 | |
| 3288 | DEFUN(banner_motd_file, |
| 3289 | banner_motd_file_cmd, |
| 3290 | "banner motd file [FILE]", |
| 3291 | "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n") |
| 3292 | { |
| 3293 | if (host.motdfile) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3294 | talloc_free(host.motdfile); |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 3295 | host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3296 | |
| 3297 | return CMD_SUCCESS; |
| 3298 | } |
| 3299 | |
| 3300 | DEFUN(banner_motd_default, |
| 3301 | banner_motd_default_cmd, |
| 3302 | "banner motd default", |
| 3303 | "Set banner string\n" "Strings for motd\n" "Default string\n") |
| 3304 | { |
| 3305 | host.motd = default_motd; |
| 3306 | return CMD_SUCCESS; |
| 3307 | } |
| 3308 | |
| 3309 | DEFUN(no_banner_motd, |
| 3310 | no_banner_motd_cmd, |
| 3311 | "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n") |
| 3312 | { |
| 3313 | host.motd = NULL; |
| 3314 | if (host.motdfile) |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 3315 | talloc_free(host.motdfile); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3316 | host.motdfile = NULL; |
| 3317 | return CMD_SUCCESS; |
| 3318 | } |
| 3319 | |
| 3320 | /* Set config filename. Called from vty.c */ |
Holger Hans Peter Freyther | 656b129 | 2009-08-10 08:09:54 +0200 | [diff] [blame] | 3321 | void host_config_set(const char *filename) |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3322 | { |
Harald Welte | 3cefa9a | 2009-12-24 10:51:56 +0100 | [diff] [blame] | 3323 | host.config = talloc_strdup(tall_vty_cmd_ctx, filename); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | void install_default(enum node_type node) |
| 3327 | { |
| 3328 | install_element(node, &config_exit_cmd); |
| 3329 | install_element(node, &config_quit_cmd); |
| 3330 | install_element(node, &config_end_cmd); |
| 3331 | install_element(node, &config_help_cmd); |
| 3332 | install_element(node, &config_list_cmd); |
| 3333 | |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3334 | install_element(node, &config_write_terminal_cmd); |
| 3335 | install_element(node, &config_write_file_cmd); |
| 3336 | install_element(node, &config_write_memory_cmd); |
| 3337 | install_element(node, &config_write_cmd); |
| 3338 | install_element(node, &show_running_config_cmd); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | /* Initialize command interface. Install basic nodes and commands. */ |
| 3342 | void cmd_init(int terminal) |
| 3343 | { |
| 3344 | /* Allocate initial top vector of commands. */ |
| 3345 | cmdvec = vector_init(VECTOR_MIN_SIZE); |
| 3346 | |
| 3347 | /* Default host value settings. */ |
| 3348 | host.name = NULL; |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 3349 | host.password = NULL; |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3350 | host.enable = NULL; |
| 3351 | host.logfile = NULL; |
| 3352 | host.config = NULL; |
| 3353 | host.lines = -1; |
| 3354 | host.motd = default_motd; |
| 3355 | host.motdfile = NULL; |
| 3356 | |
| 3357 | /* Install top nodes. */ |
| 3358 | install_node(&view_node, NULL); |
| 3359 | install_node(&enable_node, NULL); |
| 3360 | install_node(&auth_node, NULL); |
| 3361 | install_node(&auth_enable_node, NULL); |
| 3362 | install_node(&config_node, config_write_host); |
| 3363 | |
| 3364 | /* Each node's basic commands. */ |
| 3365 | install_element(VIEW_NODE, &show_version_cmd); |
| 3366 | if (terminal) { |
| 3367 | install_element(VIEW_NODE, &config_list_cmd); |
| 3368 | install_element(VIEW_NODE, &config_exit_cmd); |
| 3369 | install_element(VIEW_NODE, &config_quit_cmd); |
| 3370 | install_element(VIEW_NODE, &config_help_cmd); |
| 3371 | install_element(VIEW_NODE, &config_enable_cmd); |
| 3372 | install_element(VIEW_NODE, &config_terminal_length_cmd); |
| 3373 | install_element(VIEW_NODE, &config_terminal_no_length_cmd); |
| 3374 | install_element(VIEW_NODE, &echo_cmd); |
| 3375 | } |
| 3376 | |
| 3377 | if (terminal) { |
| 3378 | install_default(ENABLE_NODE); |
| 3379 | install_element(ENABLE_NODE, &config_disable_cmd); |
| 3380 | install_element(ENABLE_NODE, &config_terminal_cmd); |
Harald Welte | 4a3023d | 2009-08-06 18:50:10 +0200 | [diff] [blame] | 3381 | install_element (ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3382 | } |
Harald Welte | 4a3023d | 2009-08-06 18:50:10 +0200 | [diff] [blame] | 3383 | install_element (ENABLE_NODE, &show_startup_config_cmd); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3384 | install_element(ENABLE_NODE, &show_version_cmd); |
| 3385 | |
| 3386 | if (terminal) { |
| 3387 | install_element(ENABLE_NODE, &config_terminal_length_cmd); |
| 3388 | install_element(ENABLE_NODE, &config_terminal_no_length_cmd); |
| 3389 | install_element(ENABLE_NODE, &echo_cmd); |
| 3390 | |
| 3391 | install_default(CONFIG_NODE); |
| 3392 | } |
| 3393 | |
| 3394 | install_element(CONFIG_NODE, &hostname_cmd); |
| 3395 | install_element(CONFIG_NODE, &no_hostname_cmd); |
| 3396 | |
| 3397 | if (terminal) { |
| 3398 | install_element(CONFIG_NODE, &password_cmd); |
| 3399 | install_element(CONFIG_NODE, &password_text_cmd); |
| 3400 | install_element(CONFIG_NODE, &enable_password_cmd); |
| 3401 | install_element(CONFIG_NODE, &enable_password_text_cmd); |
| 3402 | install_element(CONFIG_NODE, &no_enable_password_cmd); |
| 3403 | |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 3404 | #ifdef VTY_CRYPT_PW |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3405 | install_element(CONFIG_NODE, &service_password_encrypt_cmd); |
| 3406 | install_element(CONFIG_NODE, &no_service_password_encrypt_cmd); |
Harald Welte | d6cab81 | 2009-05-21 07:31:48 +0000 | [diff] [blame] | 3407 | #endif |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 3408 | install_element(CONFIG_NODE, &banner_motd_default_cmd); |
| 3409 | install_element(CONFIG_NODE, &banner_motd_file_cmd); |
| 3410 | install_element(CONFIG_NODE, &no_banner_motd_cmd); |
| 3411 | install_element(CONFIG_NODE, &service_terminal_length_cmd); |
| 3412 | install_element(CONFIG_NODE, &no_service_terminal_length_cmd); |
| 3413 | |
| 3414 | } |
| 3415 | srand(time(NULL)); |
| 3416 | } |