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