Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [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 |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | Copyright (C) 2010-2011 Holger Hans Peter Freyther <zecke@selfish.org> |
| 7 | Copyright (C) 2012 Sylvain Munaut <tnt@246tNt.com> |
| 8 | Copyright (C) 2013,2015 Harald Welte <laforge@gnumonks.org> |
| 9 | Copyright (C) 2013,2017 sysmocom - s.f.m.c. GmbH |
| 10 | |
| 11 | SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 12 | |
| 13 | This file is part of GNU Zebra. |
| 14 | |
| 15 | GNU Zebra is free software; you can redistribute it and/or modify |
| 16 | it under the terms of the GNU General Public License as published |
| 17 | by the Free Software Foundation; either version 2, or (at your |
| 18 | option) any later version. |
| 19 | |
| 20 | GNU Zebra is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with GNU Zebra; see the file COPYING. If not, write to the |
Jaroslav Škarvada | 2b82c1c | 2015-11-11 16:02:54 +0100 | [diff] [blame] | 27 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 28 | Boston, MA 02110-1301, USA. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 33 | #include <stdbool.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 34 | #include <syslog.h> |
| 35 | #include <errno.h> |
| 36 | #define _XOPEN_SOURCE |
| 37 | #include <unistd.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 38 | #include <ctype.h> |
| 39 | #include <time.h> |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 40 | #include <limits.h> |
Oliver Smith | 8a482fd | 2021-07-12 18:18:51 +0200 | [diff] [blame] | 41 | #include <signal.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 42 | #include <sys/time.h> |
| 43 | #include <sys/stat.h> |
Oliver Smith | 8a482fd | 2021-07-12 18:18:51 +0200 | [diff] [blame] | 44 | #include <sys/types.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 45 | |
| 46 | #include <osmocom/vty/vector.h> |
| 47 | #include <osmocom/vty/vty.h> |
| 48 | #include <osmocom/vty/command.h> |
| 49 | |
Oliver Smith | 8a482fd | 2021-07-12 18:18:51 +0200 | [diff] [blame] | 50 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 51 | #include <osmocom/core/talloc.h> |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 52 | #include <osmocom/core/timer.h> |
Harald Welte | a99d45a | 2015-11-12 13:48:23 +0100 | [diff] [blame] | 53 | #include <osmocom/core/utils.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 54 | |
Ruben Undheim | 766f77c | 2018-11-18 13:02:47 +0100 | [diff] [blame] | 55 | #ifndef MAXPATHLEN |
| 56 | #define MAXPATHLEN 4096 |
| 57 | #endif |
| 58 | |
| 59 | |
Harald Welte | e881b1b | 2011-08-17 18:52:30 +0200 | [diff] [blame] | 60 | /*! \addtogroup command |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 61 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | * VTY command handling |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 63 | * |
| 64 | * \file command.c */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 65 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 66 | #define CONFIGFILE_MASK 022 |
| 67 | |
| 68 | void *tall_vty_cmd_ctx; |
| 69 | |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 70 | /* Set by on_dso_load_starttime() for "show uptime". */ |
| 71 | static struct timespec starttime; |
| 72 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 73 | /* Command vector which includes some level of command lists. Normally |
| 74 | each daemon maintains each own cmdvec. */ |
| 75 | vector cmdvec; |
| 76 | |
| 77 | /* Host information structure. */ |
| 78 | struct host host; |
| 79 | |
| 80 | /* Standard command node structures. */ |
| 81 | struct cmd_node auth_node = { |
| 82 | AUTH_NODE, |
| 83 | "Password: ", |
Neels Hofmeyr | 3b68157 | 2017-12-09 03:54:32 +0100 | [diff] [blame] | 84 | .name = "auth", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct cmd_node view_node = { |
| 88 | VIEW_NODE, |
| 89 | "%s> ", |
Neels Hofmeyr | 3b68157 | 2017-12-09 03:54:32 +0100 | [diff] [blame] | 90 | .name = "view", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct cmd_node auth_enable_node = { |
| 94 | AUTH_ENABLE_NODE, |
| 95 | "Password: ", |
Neels Hofmeyr | 3b68157 | 2017-12-09 03:54:32 +0100 | [diff] [blame] | 96 | .name = "auth-enable", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct cmd_node enable_node = { |
| 100 | ENABLE_NODE, |
| 101 | "%s# ", |
Neels Hofmeyr | 3b68157 | 2017-12-09 03:54:32 +0100 | [diff] [blame] | 102 | .name = "enable", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct cmd_node config_node = { |
| 106 | CONFIG_NODE, |
| 107 | "%s(config)# ", |
| 108 | 1 |
| 109 | }; |
| 110 | |
| 111 | /* Default motd string. */ |
| 112 | const char *default_motd = ""; |
| 113 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 114 | /*! print the version (and optionally copyright) information |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 115 | * |
| 116 | * This is called from main when a daemon is invoked with -v or --version. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 117 | void print_version(int print_copyright) |
| 118 | { |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 119 | printf("%s version %s\n", host.app_info->name, host.app_info->version); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 120 | if (print_copyright) |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 121 | printf("\n%s\n", host.app_info->copyright); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Utility function to concatenate argv argument into a single string |
| 125 | with inserting ' ' character between each argument. */ |
| 126 | char *argv_concat(const char **argv, int argc, int shift) |
| 127 | { |
| 128 | int i; |
| 129 | size_t len; |
| 130 | char *str; |
| 131 | char *p; |
| 132 | |
| 133 | len = 0; |
| 134 | for (i = shift; i < argc; i++) |
| 135 | len += strlen(argv[i]) + 1; |
| 136 | if (!len) |
| 137 | return NULL; |
| 138 | p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat"); |
| 139 | for (i = shift; i < argc; i++) { |
| 140 | size_t arglen; |
| 141 | memcpy(p, argv[i], (arglen = strlen(argv[i]))); |
| 142 | p += arglen; |
| 143 | *p++ = ' '; |
| 144 | } |
| 145 | *(p - 1) = '\0'; |
| 146 | return str; |
| 147 | } |
| 148 | |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 149 | /* Strip all characters from a string (prompt) except for alnum, '-' and '_'. |
| 150 | * For example used to derive a node->name from node->prompt if the user didn't provide a name; |
| 151 | * in turn, this name us used for XML IDs in 'show online-help'. */ |
| 152 | static const char *node_name_from_prompt(const char *prompt, char *name_buf, size_t name_buf_size) |
| 153 | { |
| 154 | const char *pos; |
| 155 | int dest = 0; |
| 156 | |
| 157 | if (!prompt || !*prompt) |
| 158 | return ""; |
| 159 | |
| 160 | for (pos = prompt; *pos && dest < (name_buf_size-1); pos++) { |
| 161 | if (pos[0] == '%' && pos[1]) { |
| 162 | /* skip "%s"; loop pos++ does the second one. */ |
| 163 | pos++; |
| 164 | continue; |
| 165 | } |
| 166 | if (!(isalnum(pos[0]) || pos[0] == '-' || pos[0] == '_')) |
| 167 | continue; |
| 168 | name_buf[dest] = pos[0]; |
| 169 | dest++; |
| 170 | } |
| 171 | name_buf[dest] = '\0'; |
| 172 | return name_buf; |
| 173 | } |
| 174 | |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 175 | static void install_basic_node_commands(int node); |
| 176 | |
| 177 | /*! Install top node of command vector, without adding basic node commands. */ |
| 178 | static void install_node_bare(struct cmd_node *node, int (*func) (struct vty *)) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 179 | { |
| 180 | vector_set_index(cmdvec, node->node, node); |
| 181 | node->func = func; |
| 182 | node->cmd_vector = vector_init(VECTOR_MIN_SIZE); |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 183 | if (!*node->name) |
| 184 | node_name_from_prompt(node->prompt, node->name, sizeof(node->name)); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 187 | /*! Install top node of command vector. */ |
| 188 | void install_node(struct cmd_node *node, int (*func) (struct vty *)) |
| 189 | { |
| 190 | install_node_bare(node, func); |
| 191 | install_basic_node_commands(node->node); |
| 192 | } |
| 193 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 194 | /* Compare two command's string. Used in sort_node (). */ |
| 195 | static int cmp_node(const void *p, const void *q) |
| 196 | { |
| 197 | struct cmd_element *a = *(struct cmd_element **)p; |
| 198 | struct cmd_element *b = *(struct cmd_element **)q; |
| 199 | |
| 200 | return strcmp(a->string, b->string); |
| 201 | } |
| 202 | |
| 203 | static int cmp_desc(const void *p, const void *q) |
| 204 | { |
| 205 | struct desc *a = *(struct desc **)p; |
| 206 | struct desc *b = *(struct desc **)q; |
| 207 | |
| 208 | return strcmp(a->cmd, b->cmd); |
| 209 | } |
| 210 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 211 | /*! Sort each node's command element according to command string. */ |
Harald Welte | 95b2b47 | 2011-07-16 11:58:09 +0200 | [diff] [blame] | 212 | void sort_node(void) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 213 | { |
| 214 | unsigned int i, j; |
| 215 | struct cmd_node *cnode; |
| 216 | vector descvec; |
| 217 | struct cmd_element *cmd_element; |
| 218 | |
| 219 | for (i = 0; i < vector_active(cmdvec); i++) |
| 220 | if ((cnode = vector_slot(cmdvec, i)) != NULL) { |
| 221 | vector cmd_vector = cnode->cmd_vector; |
| 222 | qsort(cmd_vector->index, vector_active(cmd_vector), |
| 223 | sizeof(void *), cmp_node); |
| 224 | |
| 225 | for (j = 0; j < vector_active(cmd_vector); j++) |
| 226 | if ((cmd_element = |
| 227 | vector_slot(cmd_vector, j)) != NULL |
| 228 | && vector_active(cmd_element->strvec)) { |
| 229 | descvec = |
| 230 | vector_slot(cmd_element->strvec, |
| 231 | vector_active |
| 232 | (cmd_element->strvec) - |
| 233 | 1); |
| 234 | qsort(descvec->index, |
| 235 | vector_active(descvec), |
| 236 | sizeof(void *), cmp_desc); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 241 | /*! Break up string in command tokens. Return leading indents. |
| 242 | * \param[in] string String to split. |
| 243 | * \param[out] indent If not NULL, return a talloc_strdup of indent characters. |
| 244 | * \param[out] strvec_p Returns vector of split tokens, must not be NULL. |
| 245 | * \returns CMD_SUCCESS or CMD_ERR_INVALID_INDENT |
| 246 | * |
| 247 | * If \a indent is passed non-NULL, only simple space ' ' indents are allowed, |
| 248 | * so that \a indent can simply return the count of leading spaces. |
| 249 | * Otherwise any isspace() characters are allowed for indenting (backwards compat). |
| 250 | */ |
| 251 | int cmd_make_strvec2(const char *string, char **indent, vector *strvec_p) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 252 | { |
| 253 | const char *cp, *start; |
| 254 | char *token; |
| 255 | int strlen; |
| 256 | vector strvec; |
| 257 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 258 | *strvec_p = NULL; |
| 259 | if (indent) |
| 260 | *indent = 0; |
| 261 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 262 | if (string == NULL) |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 263 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 264 | |
| 265 | cp = string; |
| 266 | |
| 267 | /* Skip white spaces. */ |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 268 | while (isspace((int)*cp) && *cp != '\0') { |
| 269 | /* if we're counting indents, we need to be strict about them */ |
| 270 | if (indent && (*cp != ' ') && (*cp != '\t')) { |
| 271 | /* Ignore blank lines, they appear as leading whitespace with line breaks. */ |
| 272 | if (*cp == '\n' || *cp == '\r') { |
| 273 | cp++; |
| 274 | string = cp; |
| 275 | continue; |
| 276 | } |
| 277 | return CMD_ERR_INVALID_INDENT; |
| 278 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 279 | cp++; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (indent) |
| 283 | *indent = talloc_strndup(tall_vty_cmd_ctx, string, cp - string); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 284 | |
| 285 | /* Return if there is only white spaces */ |
| 286 | if (*cp == '\0') |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 287 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 288 | |
| 289 | if (*cp == '!' || *cp == '#') |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 290 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 291 | |
| 292 | /* Prepare return vector. */ |
| 293 | strvec = vector_init(VECTOR_MIN_SIZE); |
| 294 | |
| 295 | /* Copy each command piece and set into vector. */ |
| 296 | while (1) { |
| 297 | start = cp; |
| 298 | while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') && |
| 299 | *cp != '\0') |
| 300 | cp++; |
| 301 | strlen = cp - start; |
| 302 | token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec"); |
| 303 | memcpy(token, start, strlen); |
| 304 | *(token + strlen) = '\0'; |
| 305 | vector_set(strvec, token); |
| 306 | |
| 307 | while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') && |
| 308 | *cp != '\0') |
| 309 | cp++; |
| 310 | |
| 311 | if (*cp == '\0') |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 312 | break; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 313 | } |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 314 | |
| 315 | *strvec_p = strvec; |
| 316 | return CMD_SUCCESS; |
| 317 | } |
| 318 | |
| 319 | /*! Breaking up string into each command piece. I assume given |
| 320 | character is separated by a space character. Return value is a |
| 321 | vector which includes char ** data element. */ |
| 322 | vector cmd_make_strvec(const char *string) |
| 323 | { |
| 324 | vector strvec; |
| 325 | cmd_make_strvec2(string, NULL, &strvec); |
| 326 | return strvec; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 329 | /*! Free allocated string vector. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 330 | void cmd_free_strvec(vector v) |
| 331 | { |
| 332 | unsigned int i; |
| 333 | char *cp; |
| 334 | |
| 335 | if (!v) |
| 336 | return; |
| 337 | |
| 338 | for (i = 0; i < vector_active(v); i++) |
| 339 | if ((cp = vector_slot(v, i)) != NULL) |
| 340 | talloc_free(cp); |
| 341 | |
| 342 | vector_free(v); |
| 343 | } |
| 344 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 345 | /*! Fetch next description. Used in \ref cmd_make_descvec(). */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 346 | static char *cmd_desc_str(const char **string) |
| 347 | { |
| 348 | const char *cp, *start; |
| 349 | char *token; |
| 350 | int strlen; |
| 351 | |
| 352 | cp = *string; |
| 353 | |
| 354 | if (cp == NULL) |
| 355 | return NULL; |
| 356 | |
| 357 | /* Skip white spaces. */ |
| 358 | while (isspace((int)*cp) && *cp != '\0') |
| 359 | cp++; |
| 360 | |
| 361 | /* Return if there is only white spaces */ |
| 362 | if (*cp == '\0') |
| 363 | return NULL; |
| 364 | |
| 365 | start = cp; |
| 366 | |
| 367 | while (!(*cp == '\r' || *cp == '\n') && *cp != '\0') |
| 368 | cp++; |
| 369 | |
| 370 | strlen = cp - start; |
| 371 | token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str"); |
| 372 | memcpy(token, start, strlen); |
| 373 | *(token + strlen) = '\0'; |
| 374 | |
| 375 | *string = cp; |
| 376 | |
| 377 | return token; |
| 378 | } |
| 379 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 380 | /*! New string vector. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 381 | static vector cmd_make_descvec(const char *string, const char *descstr) |
| 382 | { |
| 383 | int multiple = 0; |
Neels Hofmeyr | b55f4d2 | 2019-01-31 08:13:31 +0100 | [diff] [blame] | 384 | int optional_brace = 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 385 | const char *sp; |
| 386 | char *token; |
| 387 | int len; |
| 388 | const char *cp; |
| 389 | const char *dp; |
| 390 | vector allvec; |
| 391 | vector strvec = NULL; |
| 392 | struct desc *desc; |
| 393 | |
| 394 | cp = string; |
| 395 | dp = descstr; |
| 396 | |
| 397 | if (cp == NULL) |
| 398 | return NULL; |
| 399 | |
| 400 | allvec = vector_init(VECTOR_MIN_SIZE); |
| 401 | |
| 402 | while (1) { |
| 403 | while (isspace((int)*cp) && *cp != '\0') |
| 404 | cp++; |
| 405 | |
Neels Hofmeyr | b55f4d2 | 2019-01-31 08:13:31 +0100 | [diff] [blame] | 406 | /* Explicitly detect optional multi-choice braces like [(one|two)]. */ |
| 407 | if (cp[0] == '[' && cp[1] == '(') { |
| 408 | optional_brace = 1; |
| 409 | cp++; |
| 410 | } |
| 411 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 412 | if (*cp == '(') { |
| 413 | multiple = 1; |
| 414 | cp++; |
| 415 | } |
| 416 | if (*cp == ')') { |
| 417 | multiple = 0; |
| 418 | cp++; |
Neels Hofmeyr | b55f4d2 | 2019-01-31 08:13:31 +0100 | [diff] [blame] | 419 | if (*cp == ']') |
| 420 | cp++; |
| 421 | optional_brace = 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 422 | } |
| 423 | if (*cp == '|') { |
Harald Welte | a99d45a | 2015-11-12 13:48:23 +0100 | [diff] [blame] | 424 | OSMO_ASSERT(multiple); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 425 | cp++; |
| 426 | } |
| 427 | |
| 428 | while (isspace((int)*cp) && *cp != '\0') |
| 429 | cp++; |
| 430 | |
| 431 | if (*cp == '(') { |
| 432 | multiple = 1; |
| 433 | cp++; |
| 434 | } |
| 435 | |
| 436 | if (*cp == '\0') |
| 437 | return allvec; |
| 438 | |
| 439 | sp = cp; |
| 440 | |
| 441 | while (! |
| 442 | (isspace((int)*cp) || *cp == '\r' || *cp == '\n' |
| 443 | || *cp == ')' || *cp == '|') && *cp != '\0') |
| 444 | cp++; |
| 445 | |
| 446 | len = cp - sp; |
| 447 | |
Neels Hofmeyr | b55f4d2 | 2019-01-31 08:13:31 +0100 | [diff] [blame] | 448 | token = _talloc_zero(tall_vty_cmd_ctx, len + (optional_brace? 2 : 0) + 1, "cmd_make_descvec"); |
| 449 | if (optional_brace) { |
| 450 | /* Place each individual multi-choice token in its own square braces */ |
| 451 | token[0] = '['; |
| 452 | memcpy(token + 1, sp, len); |
| 453 | token[1 + len] = ']'; |
| 454 | token[2 + len] = '\0'; |
| 455 | } else { |
| 456 | memcpy(token, sp, len); |
| 457 | *(token + len) = '\0'; |
| 458 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 459 | |
| 460 | desc = talloc_zero(tall_vty_cmd_ctx, struct desc); |
| 461 | desc->cmd = token; |
| 462 | desc->str = cmd_desc_str(&dp); |
| 463 | |
| 464 | if (multiple) { |
| 465 | if (multiple == 1) { |
| 466 | strvec = vector_init(VECTOR_MIN_SIZE); |
| 467 | vector_set(allvec, strvec); |
| 468 | } |
| 469 | multiple++; |
| 470 | } else { |
| 471 | strvec = vector_init(VECTOR_MIN_SIZE); |
| 472 | vector_set(allvec, strvec); |
| 473 | } |
| 474 | vector_set(strvec, desc); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /* Count mandantory string vector size. This is to determine inputed |
| 479 | command has enough command length. */ |
| 480 | static int cmd_cmdsize(vector strvec) |
| 481 | { |
| 482 | unsigned int i; |
| 483 | int size = 0; |
| 484 | vector descvec; |
| 485 | struct desc *desc; |
| 486 | |
| 487 | for (i = 0; i < vector_active(strvec); i++) |
| 488 | if ((descvec = vector_slot(strvec, i)) != NULL) { |
Neels Hofmeyr | c197809 | 2019-01-31 08:14:26 +0100 | [diff] [blame] | 489 | if ((vector_active(descvec)) >= 1 |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 490 | && (desc = vector_slot(descvec, 0)) != NULL) { |
| 491 | if (desc->cmd == NULL || CMD_OPTION(desc->cmd)) |
| 492 | return size; |
| 493 | else |
| 494 | size++; |
| 495 | } else |
| 496 | size++; |
| 497 | } |
| 498 | return size; |
| 499 | } |
| 500 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 501 | /*! Return prompt character of specified node. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 502 | const char *cmd_prompt(enum node_type node) |
| 503 | { |
| 504 | struct cmd_node *cnode; |
| 505 | |
| 506 | cnode = vector_slot(cmdvec, node); |
| 507 | return cnode->prompt; |
| 508 | } |
| 509 | |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 510 | /*! |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 511 | * escape all special asciidoc symbols |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 512 | * \param unsafe string |
| 513 | * \return a new talloc char * |
| 514 | */ |
| 515 | char *osmo_asciidoc_escape(const char *inp) |
| 516 | { |
| 517 | int _strlen; |
| 518 | char *out, *out_ptr; |
Pau Espin Pedrol | e1e1ec3 | 2019-06-19 14:55:45 +0200 | [diff] [blame] | 519 | int len = 0, i; |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 520 | |
| 521 | if (!inp) |
| 522 | return NULL; |
| 523 | _strlen = strlen(inp); |
| 524 | |
| 525 | for (i = 0; i < _strlen; ++i) { |
| 526 | switch (inp[i]) { |
| 527 | case '|': |
| 528 | len += 2; |
| 529 | break; |
| 530 | default: |
| 531 | len += 1; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
Neels Hofmeyr | f2d323e | 2018-07-09 19:18:35 +0200 | [diff] [blame] | 536 | out = talloc_size(tall_vty_cmd_ctx, len + 1); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 537 | if (!out) |
| 538 | return NULL; |
| 539 | |
| 540 | out_ptr = out; |
| 541 | |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 542 | for (i = 0; i < _strlen; ++i) { |
| 543 | switch (inp[i]) { |
| 544 | case '|': |
Pau Espin Pedrol | e1e1ec3 | 2019-06-19 14:55:45 +0200 | [diff] [blame] | 545 | /* Prepend escape character "\": */ |
| 546 | *(out_ptr++) = '\\'; |
| 547 | /* fall through */ |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 548 | default: |
| 549 | *(out_ptr++) = inp[i]; |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 554 | out_ptr[0] = '\0'; |
| 555 | return out; |
| 556 | } |
| 557 | |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 558 | static char *xml_escape(const char *inp) |
| 559 | { |
| 560 | int _strlen; |
| 561 | char *out, *out_ptr; |
| 562 | int len = 0, i, j; |
| 563 | |
| 564 | if (!inp) |
| 565 | return NULL; |
| 566 | _strlen = strlen(inp); |
| 567 | |
| 568 | for (i = 0; i < _strlen; ++i) { |
| 569 | switch (inp[i]) { |
| 570 | case '"': |
| 571 | len += 6; |
| 572 | break; |
| 573 | case '\'': |
| 574 | len += 6; |
| 575 | break; |
| 576 | case '<': |
| 577 | len += 4; |
| 578 | break; |
| 579 | case '>': |
| 580 | len += 4; |
| 581 | break; |
| 582 | case '&': |
| 583 | len += 5; |
| 584 | break; |
| 585 | default: |
| 586 | len += 1; |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
Neels Hofmeyr | f2d323e | 2018-07-09 19:18:35 +0200 | [diff] [blame] | 591 | out = talloc_size(tall_vty_cmd_ctx, len + 1); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 592 | if (!out) |
| 593 | return NULL; |
| 594 | |
| 595 | out_ptr = out; |
| 596 | |
| 597 | #define ADD(out, str) \ |
| 598 | for (j = 0; j < strlen(str); ++j) \ |
| 599 | *(out++) = str[j]; |
| 600 | |
| 601 | for (i = 0; i < _strlen; ++i) { |
| 602 | switch (inp[i]) { |
| 603 | case '"': |
| 604 | ADD(out_ptr, """); |
| 605 | break; |
| 606 | case '\'': |
| 607 | ADD(out_ptr, "'"); |
| 608 | break; |
| 609 | case '<': |
| 610 | ADD(out_ptr, "<"); |
| 611 | break; |
| 612 | case '>': |
| 613 | ADD(out_ptr, ">"); |
| 614 | break; |
| 615 | case '&': |
| 616 | ADD(out_ptr, "&"); |
| 617 | break; |
| 618 | default: |
| 619 | *(out_ptr++) = inp[i]; |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | #undef ADD |
| 625 | |
| 626 | out_ptr[0] = '\0'; |
| 627 | return out; |
| 628 | } |
| 629 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 630 | typedef int (*print_func_t)(void *data, const char *fmt, ...); |
| 631 | |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 632 | static const struct value_string cmd_attr_desc[] = { |
| 633 | { CMD_ATTR_DEPRECATED, "This command is deprecated" }, |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 634 | { CMD_ATTR_HIDDEN, "This command is hidden (check expert mode)" }, |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 635 | { CMD_ATTR_IMMEDIATE, "This command applies immediately" }, |
Vadim Yanitskiy | ceb3b39 | 2020-10-06 00:20:24 +0700 | [diff] [blame] | 636 | { CMD_ATTR_NODE_EXIT, "This command applies on VTY node exit" }, |
Vadim Yanitskiy | 99d5c2d | 2020-10-06 00:22:06 +0700 | [diff] [blame] | 637 | /* CMD_ATTR_LIB_COMMAND is intentionally skipped */ |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 638 | { 0, NULL } |
| 639 | }; |
| 640 | |
Vadim Yanitskiy | f1fc9d3 | 2020-10-07 13:53:38 +0700 | [diff] [blame] | 641 | /* Public attributes (to be printed in the VTY / XML reference) */ |
| 642 | #define CMD_ATTR_PUBLIC_MASK \ |
Vadim Yanitskiy | 6760845 | 2020-10-23 20:37:32 +0700 | [diff] [blame] | 643 | (CMD_ATTR_HIDDEN | CMD_ATTR_IMMEDIATE | CMD_ATTR_NODE_EXIT) |
Vadim Yanitskiy | f1fc9d3 | 2020-10-07 13:53:38 +0700 | [diff] [blame] | 644 | |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 645 | /* Get a flag character for a global VTY command attribute */ |
| 646 | static char cmd_attr_get_flag(unsigned int attr) |
| 647 | { |
| 648 | switch (attr) { |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 649 | case CMD_ATTR_HIDDEN: |
| 650 | return '^'; |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 651 | case CMD_ATTR_IMMEDIATE: |
| 652 | return '!'; |
| 653 | case CMD_ATTR_NODE_EXIT: |
| 654 | return '@'; |
| 655 | default: |
| 656 | return '.'; |
| 657 | } |
| 658 | } |
| 659 | |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 660 | /* Description of attributes shared between the lib commands */ |
| 661 | static const char * const cmd_lib_attr_desc[32] = { |
| 662 | /* [OSMO_LIBNAME_LIB_ATTR_ATTRNAME] = \ |
| 663 | * "Brief but meaningful description", */ |
Philipp Maier | 608b1a4 | 2020-10-05 20:54:51 +0200 | [diff] [blame] | 664 | [OSMO_SCCP_LIB_ATTR_RSTRT_ASP] = \ |
| 665 | "This command applies on ASP restart", |
Philipp Maier | a5218ea | 2020-10-08 17:30:42 +0200 | [diff] [blame] | 666 | [OSMO_ABIS_LIB_ATTR_IPA_NEW_LNK] = \ |
| 667 | "This command applies on IPA link establishment", |
| 668 | [OSMO_ABIS_LIB_ATTR_LINE_UPD] = \ |
| 669 | "This command applies on E1 line update", |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | /* Flag letters of attributes shared between the lib commands. |
| 673 | * NOTE: uppercase letters only, the rest is reserved for applications. */ |
| 674 | static const char cmd_lib_attr_letters[32] = { |
| 675 | /* [OSMO_LIBNAME_LIB_ATTR_ATTRNAME] = 'X', */ |
Philipp Maier | 608b1a4 | 2020-10-05 20:54:51 +0200 | [diff] [blame] | 676 | [OSMO_SCCP_LIB_ATTR_RSTRT_ASP] = 'A', |
Philipp Maier | a5218ea | 2020-10-08 17:30:42 +0200 | [diff] [blame] | 677 | [OSMO_ABIS_LIB_ATTR_IPA_NEW_LNK] = 'I', |
| 678 | [OSMO_ABIS_LIB_ATTR_LINE_UPD] = 'L', |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 679 | }; |
| 680 | |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 681 | /* |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 682 | * Write one cmd_element as XML via a print_func_t. |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 683 | */ |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 684 | static int vty_dump_element(const struct cmd_element *cmd, print_func_t print_func, |
| 685 | void *data, const char *newline) |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 686 | { |
| 687 | char *xml_string = xml_escape(cmd->string); |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 688 | unsigned int i; |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 689 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 690 | print_func(data, " <command id='%s'>%s", xml_string, newline); |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 691 | |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 692 | /* Print global attributes and their description */ |
Vadim Yanitskiy | f1fc9d3 | 2020-10-07 13:53:38 +0700 | [diff] [blame] | 693 | if (cmd->attr & CMD_ATTR_PUBLIC_MASK) { /* ... only public ones */ |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 694 | print_func(data, " <attributes scope='global'>%s", newline); |
| 695 | |
| 696 | for (i = 0; i < ARRAY_SIZE(cmd_attr_desc) - 1; i++) { |
| 697 | char *xml_att_desc; |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 698 | char flag; |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 699 | |
| 700 | if (~cmd->attr & cmd_attr_desc[i].value) |
| 701 | continue; |
| 702 | |
| 703 | xml_att_desc = xml_escape(cmd_attr_desc[i].str); |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 704 | print_func(data, " <attribute doc='%s'", |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 705 | xml_att_desc, newline); |
| 706 | talloc_free(xml_att_desc); |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 707 | |
| 708 | flag = cmd_attr_get_flag(cmd_attr_desc[i].value); |
| 709 | if (flag != '.') |
| 710 | print_func(data, " flag='%c'", flag); |
| 711 | print_func(data, " />%s", newline); |
Vadim Yanitskiy | 7f6c87d | 2020-09-28 13:03:49 +0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | print_func(data, " </attributes>%s", newline); |
| 715 | } |
| 716 | |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 717 | /* Print application specific attributes and their description */ |
| 718 | if (cmd->usrattr != 0x00) { /* ... if at least one flag is set */ |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 719 | const char * const *desc; |
| 720 | const char *letters; |
| 721 | |
| 722 | if (cmd->attr & CMD_ATTR_LIB_COMMAND) { |
| 723 | print_func(data, " <attributes scope='library'>%s", newline); |
| 724 | letters = &cmd_lib_attr_letters[0]; |
| 725 | desc = &cmd_lib_attr_desc[0]; |
| 726 | } else { |
| 727 | print_func(data, " <attributes scope='application'>%s", newline); |
| 728 | letters = &host.app_info->usr_attr_letters[0]; |
| 729 | desc = &host.app_info->usr_attr_desc[0]; |
| 730 | } |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 731 | |
| 732 | for (i = 0; i < ARRAY_SIZE(host.app_info->usr_attr_desc); i++) { |
| 733 | char *xml_att_desc; |
| 734 | char flag; |
| 735 | |
| 736 | /* Skip attribute if *not* set */ |
Harald Welte | c296e29 | 2020-12-21 15:44:52 +0100 | [diff] [blame] | 737 | if (~cmd->usrattr & ((unsigned)1 << i)) |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 738 | continue; |
| 739 | |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 740 | xml_att_desc = xml_escape(desc[i]); |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 741 | print_func(data, " <attribute doc='%s'", xml_att_desc); |
| 742 | talloc_free(xml_att_desc); |
| 743 | |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 744 | if ((flag = letters[i]) != '\0') |
Vadim Yanitskiy | 34e94f3 | 2020-08-15 23:27:55 +0700 | [diff] [blame] | 745 | print_func(data, " flag='%c'", flag); |
| 746 | print_func(data, " />%s", newline); |
| 747 | } |
| 748 | |
| 749 | print_func(data, " </attributes>%s", newline); |
| 750 | } |
| 751 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 752 | print_func(data, " <params>%s", newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 753 | |
Vadim Yanitskiy | 478dd1b | 2020-08-18 18:40:16 +0700 | [diff] [blame] | 754 | for (i = 0; i < vector_count(cmd->strvec); ++i) { |
| 755 | vector descvec = vector_slot(cmd->strvec, i); |
| 756 | int j; |
| 757 | for (j = 0; j < vector_active(descvec); ++j) { |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 758 | char *xml_param, *xml_doc; |
Vadim Yanitskiy | 478dd1b | 2020-08-18 18:40:16 +0700 | [diff] [blame] | 759 | struct desc *desc = vector_slot(descvec, j); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 760 | if (desc == NULL) |
| 761 | continue; |
| 762 | |
| 763 | xml_param = xml_escape(desc->cmd); |
| 764 | xml_doc = xml_escape(desc->str); |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 765 | print_func(data, " <param name='%s' doc='%s' />%s", |
| 766 | xml_param, xml_doc, newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 767 | talloc_free(xml_param); |
| 768 | talloc_free(xml_doc); |
| 769 | } |
| 770 | } |
| 771 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 772 | print_func(data, " </params>%s", newline); |
| 773 | print_func(data, " </command>%s", newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 774 | |
| 775 | talloc_free(xml_string); |
| 776 | return 0; |
| 777 | } |
| 778 | |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 779 | static bool vty_command_is_common(const struct cmd_element *cmd); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 780 | |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 781 | /* |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 782 | * Dump all nodes and commands associated with a given node as XML via a print_func_t. |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 783 | * |
| 784 | * (gflag_mask, match = false) - print only those commands with non-matching flags. |
| 785 | * (gflag_mask, match = true) - print only those commands with matching flags. |
| 786 | * |
| 787 | * Some examples: |
| 788 | * |
| 789 | * Print all commands except deprecated and hidden (default mode): |
| 790 | * (CMD_ATTR_DEPRECATED | CMD_ATTR_HIDDEN, false) |
| 791 | * Print only deprecated and hidden commands: |
| 792 | * (CMD_ATTR_DEPRECATED | CMD_ATTR_HIDDEN, true) |
| 793 | * Print all commands except deprecated (expert mode): |
| 794 | * (CMD_ATTR_DEPRECATED, false) |
| 795 | * Print only hidden commands: |
| 796 | * (CMD_ATTR_HIDDEN, true) |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 797 | */ |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 798 | static int vty_dump_nodes(print_func_t print_func, void *data, const char *newline, |
| 799 | unsigned char gflag_mask, bool match) |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 800 | { |
| 801 | int i, j; |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 802 | int same_name_count; |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 803 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 804 | print_func(data, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 805 | |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 806 | /* Only once, list all common node commands. Use the CONFIG node to find common node commands. */ |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 807 | print_func(data, " <node id='_common_cmds_'>%s", newline); |
| 808 | print_func(data, " <name>Common Commands</name>%s", newline); |
| 809 | print_func(data, " <description>These commands are available on all VTY nodes. They are listed" |
| 810 | " here only once, to unclutter the VTY reference.</description>%s", newline); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 811 | for (i = 0; i < vector_active(cmdvec); ++i) { |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 812 | const struct cmd_node *cnode = vector_slot(cmdvec, i); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 813 | if (!cnode) |
| 814 | continue; |
| 815 | if (cnode->node != CONFIG_NODE) |
| 816 | continue; |
| 817 | |
| 818 | for (j = 0; j < vector_active(cnode->cmd_vector); ++j) { |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 819 | const struct cmd_element *elem = vector_slot(cnode->cmd_vector, j); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 820 | if (!vty_command_is_common(elem)) |
| 821 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 822 | if (!match && (elem->attr & gflag_mask) != 0x00) |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 823 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 824 | if (match && (elem->attr & gflag_mask) == 0x00) |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 825 | continue; |
| 826 | vty_dump_element(elem, print_func, data, newline); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 827 | } |
| 828 | } |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 829 | print_func(data, " </node>%s", newline); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 830 | |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 831 | for (i = 0; i < vector_active(cmdvec); ++i) { |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 832 | const struct cmd_node *cnode = vector_slot(cmdvec, i); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 833 | if (!cnode) |
| 834 | continue; |
Neels Hofmeyr | f716277 | 2017-10-22 02:31:33 +0200 | [diff] [blame] | 835 | if (vector_active(cnode->cmd_vector) < 1) |
| 836 | continue; |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 837 | |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 838 | /* De-dup node IDs: how many times has this same name been used before? Count the first |
| 839 | * occurence as _1 and omit that first suffix, so that the first occurence is called |
| 840 | * 'name', the second becomes 'name_2', then 'name_3', ... */ |
| 841 | same_name_count = 1; |
| 842 | for (j = 0; j < i; ++j) { |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 843 | const struct cmd_node *cnode2 = vector_slot(cmdvec, j); |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 844 | if (!cnode2) |
| 845 | continue; |
| 846 | if (strcmp(cnode->name, cnode2->name) == 0) |
| 847 | same_name_count ++; |
| 848 | } |
| 849 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 850 | print_func(data, " <node id='%s", cnode->name); |
Neels Hofmeyr | 657c5b6 | 2017-09-18 16:42:06 +0200 | [diff] [blame] | 851 | if (same_name_count > 1 || !*cnode->name) |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 852 | print_func(data, "_%d", same_name_count); |
| 853 | print_func(data, "'>%s", newline); |
| 854 | print_func(data, " <name>%s</name>%s", cnode->name, newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 855 | |
| 856 | for (j = 0; j < vector_active(cnode->cmd_vector); ++j) { |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 857 | const struct cmd_element *elem = vector_slot(cnode->cmd_vector, j); |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 858 | if (vty_command_is_common(elem)) |
| 859 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 860 | if (!match && (elem->attr & gflag_mask) != 0x00) |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 861 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 862 | if (match && (elem->attr & gflag_mask) == 0x00) |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 863 | continue; |
| 864 | vty_dump_element(elem, print_func, data, newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 867 | print_func(data, " </node>%s", newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 870 | print_func(data, "</vtydoc>%s", newline); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 875 | static int print_func_vty(void *data, const char *format, ...) |
| 876 | { |
| 877 | struct vty *vty = data; |
| 878 | va_list args; |
| 879 | int rc; |
| 880 | va_start(args, format); |
| 881 | rc = vty_out_va(vty, format, args); |
| 882 | va_end(args); |
| 883 | return rc; |
| 884 | } |
| 885 | |
| 886 | static int vty_dump_xml_ref_to_vty(struct vty *vty) |
| 887 | { |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 888 | unsigned char gflag_mask = CMD_ATTR_DEPRECATED; |
| 889 | if (!vty->expert_mode) |
| 890 | gflag_mask |= CMD_ATTR_HIDDEN; |
| 891 | return vty_dump_nodes(print_func_vty, vty, VTY_NEWLINE, gflag_mask, false); |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static int print_func_stream(void *data, const char *format, ...) |
| 895 | { |
| 896 | va_list args; |
| 897 | int rc; |
| 898 | va_start(args, format); |
| 899 | rc = vfprintf((FILE*)data, format, args); |
| 900 | va_end(args); |
| 901 | return rc; |
| 902 | } |
| 903 | |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 904 | const struct value_string vty_ref_gen_mode_names[] = { |
| 905 | { VTY_REF_GEN_MODE_DEFAULT, "default" }, |
| 906 | { VTY_REF_GEN_MODE_EXPERT, "expert" }, |
Vadim Yanitskiy | 7031ac1 | 2020-11-17 16:22:44 +0700 | [diff] [blame] | 907 | { VTY_REF_GEN_MODE_HIDDEN, "hidden" }, |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 908 | { 0, NULL } |
| 909 | }; |
| 910 | |
| 911 | const struct value_string vty_ref_gen_mode_desc[] = { |
| 912 | { VTY_REF_GEN_MODE_DEFAULT, "all commands except deprecated and hidden" }, |
| 913 | { VTY_REF_GEN_MODE_EXPERT, "all commands including hidden, excluding deprecated" }, |
Vadim Yanitskiy | 7031ac1 | 2020-11-17 16:22:44 +0700 | [diff] [blame] | 914 | { VTY_REF_GEN_MODE_HIDDEN, "hidden commands only" }, |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 915 | { 0, NULL } |
| 916 | }; |
| 917 | |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 918 | /*! Print the XML reference of all VTY nodes to the given stream. |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 919 | * \param[out] stream Output stream to print the XML reference to. |
| 920 | * \param[in] mode The XML reference generation mode. |
| 921 | * \returns always 0 for now, no errors possible. |
| 922 | */ |
| 923 | int vty_dump_xml_ref_mode(FILE *stream, enum vty_ref_gen_mode mode) |
| 924 | { |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 925 | unsigned char gflag_mask; |
Vadim Yanitskiy | 7031ac1 | 2020-11-17 16:22:44 +0700 | [diff] [blame] | 926 | bool match = false; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 927 | |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 928 | switch (mode) { |
| 929 | case VTY_REF_GEN_MODE_EXPERT: |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 930 | /* All commands except deprecated */ |
| 931 | gflag_mask = CMD_ATTR_DEPRECATED; |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 932 | break; |
Vadim Yanitskiy | 7031ac1 | 2020-11-17 16:22:44 +0700 | [diff] [blame] | 933 | case VTY_REF_GEN_MODE_HIDDEN: |
| 934 | /* Only hidden commands */ |
| 935 | gflag_mask = CMD_ATTR_HIDDEN; |
| 936 | match = true; |
| 937 | break; |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 938 | case VTY_REF_GEN_MODE_DEFAULT: |
| 939 | default: |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 940 | /* All commands except deprecated and hidden */ |
| 941 | gflag_mask = CMD_ATTR_DEPRECATED | CMD_ATTR_HIDDEN; |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 942 | break; |
| 943 | } |
| 944 | |
Vadim Yanitskiy | 7031ac1 | 2020-11-17 16:22:44 +0700 | [diff] [blame] | 945 | return vty_dump_nodes(print_func_stream, stream, "\n", gflag_mask, match); |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /*! Print the XML reference of all VTY nodes to the given stream. |
| 949 | * \param[out] stream Output stream to print the XML reference to. |
| 950 | * \returns always 0 for now, no errors possible. |
| 951 | * |
| 952 | * NOTE: this function is deprecated because it does not allow to |
| 953 | * specify the XML reference generation mode (default mode |
| 954 | * is hard-coded). Use vty_dump_xml_ref_mode() instead. |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 955 | */ |
| 956 | int vty_dump_xml_ref(FILE *stream) |
| 957 | { |
Vadim Yanitskiy | 4165d04 | 2020-10-23 16:40:57 +0700 | [diff] [blame] | 958 | return vty_dump_xml_ref_mode(stream, VTY_REF_GEN_MODE_DEFAULT); |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 961 | /* Check if a command with given string exists at given node */ |
Harald Welte | addeaa3 | 2017-01-07 12:52:00 +0100 | [diff] [blame] | 962 | static int check_element_exists(struct cmd_node *cnode, const char *cmdstring) |
| 963 | { |
| 964 | int i; |
| 965 | |
| 966 | for (i = 0; i < vector_active(cnode->cmd_vector); ++i) { |
| 967 | struct cmd_element *elem; |
| 968 | elem = vector_slot(cnode->cmd_vector, i); |
| 969 | if (!elem->string) |
| 970 | continue; |
| 971 | if (!strcmp(elem->string, cmdstring)) |
| 972 | return 1; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 977 | /*! Install a command into a node |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 978 | * \param[in] ntype Node Type |
| 979 | * \param[cmd] element to be installed |
| 980 | */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 981 | void install_element(int ntype, struct cmd_element *cmd) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 982 | { |
| 983 | struct cmd_node *cnode; |
| 984 | |
| 985 | cnode = vector_slot(cmdvec, ntype); |
| 986 | |
Harald Welte | a99d45a | 2015-11-12 13:48:23 +0100 | [diff] [blame] | 987 | OSMO_ASSERT(cnode); |
Harald Welte | addeaa3 | 2017-01-07 12:52:00 +0100 | [diff] [blame] | 988 | /* ensure no _identical_ command has been registered at this |
| 989 | * node so far */ |
| 990 | OSMO_ASSERT(!check_element_exists(cnode, cmd->string)); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 991 | |
| 992 | vector_set(cnode->cmd_vector, cmd); |
| 993 | |
| 994 | cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc); |
| 995 | cmd->cmdsize = cmd_cmdsize(cmd->strvec); |
| 996 | } |
| 997 | |
Vadim Yanitskiy | 99d5c2d | 2020-10-06 00:22:06 +0700 | [diff] [blame] | 998 | /*! Install a library command into a node |
| 999 | * \param[in] ntype Node Type |
| 1000 | * \param[in] cmd element to be installed |
| 1001 | */ |
| 1002 | void install_lib_element(int ntype, struct cmd_element *cmd) |
| 1003 | { |
| 1004 | cmd->attr |= CMD_ATTR_LIB_COMMAND; |
| 1005 | install_element(ntype, cmd); |
| 1006 | } |
| 1007 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1008 | /* Install a command into VIEW and ENABLE node */ |
| 1009 | void install_element_ve(struct cmd_element *cmd) |
| 1010 | { |
| 1011 | install_element(VIEW_NODE, cmd); |
| 1012 | install_element(ENABLE_NODE, cmd); |
| 1013 | } |
| 1014 | |
Vadim Yanitskiy | 99d5c2d | 2020-10-06 00:22:06 +0700 | [diff] [blame] | 1015 | /* Install a library command into VIEW and ENABLE node */ |
| 1016 | void install_lib_element_ve(struct cmd_element *cmd) |
| 1017 | { |
| 1018 | cmd->attr |= CMD_ATTR_LIB_COMMAND; |
| 1019 | install_element_ve(cmd); |
| 1020 | } |
| 1021 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1022 | #ifdef VTY_CRYPT_PW |
| 1023 | static unsigned char itoa64[] = |
| 1024 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 1025 | |
| 1026 | static void to64(char *s, long v, int n) |
| 1027 | { |
| 1028 | while (--n >= 0) { |
| 1029 | *s++ = itoa64[v & 0x3f]; |
| 1030 | v >>= 6; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | static char *zencrypt(const char *passwd) |
| 1035 | { |
| 1036 | char salt[6]; |
| 1037 | struct timeval tv; |
| 1038 | char *crypt(const char *, const char *); |
| 1039 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 1040 | osmo_gettimeofday(&tv, 0); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1041 | |
| 1042 | to64(&salt[0], random(), 3); |
| 1043 | to64(&salt[3], tv.tv_usec, 3); |
| 1044 | salt[5] = '\0'; |
| 1045 | |
| 1046 | return crypt(passwd, salt); |
| 1047 | } |
| 1048 | #endif |
| 1049 | |
| 1050 | /* This function write configuration of this host. */ |
| 1051 | static int config_write_host(struct vty *vty) |
| 1052 | { |
| 1053 | if (host.name) |
| 1054 | vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE); |
| 1055 | |
| 1056 | if (host.encrypt) { |
| 1057 | if (host.password_encrypt) |
| 1058 | vty_out(vty, "password 8 %s%s", host.password_encrypt, |
| 1059 | VTY_NEWLINE); |
| 1060 | if (host.enable_encrypt) |
| 1061 | vty_out(vty, "enable password 8 %s%s", |
| 1062 | host.enable_encrypt, VTY_NEWLINE); |
| 1063 | } else { |
| 1064 | if (host.password) |
| 1065 | vty_out(vty, "password %s%s", host.password, |
| 1066 | VTY_NEWLINE); |
| 1067 | if (host.enable) |
| 1068 | vty_out(vty, "enable password %s%s", host.enable, |
| 1069 | VTY_NEWLINE); |
| 1070 | } |
| 1071 | |
| 1072 | if (host.advanced) |
| 1073 | vty_out(vty, "service advanced-vty%s", VTY_NEWLINE); |
| 1074 | |
| 1075 | if (host.encrypt) |
| 1076 | vty_out(vty, "service password-encryption%s", VTY_NEWLINE); |
| 1077 | |
| 1078 | if (host.lines >= 0) |
| 1079 | vty_out(vty, "service terminal-length %d%s", host.lines, |
| 1080 | VTY_NEWLINE); |
| 1081 | |
| 1082 | if (host.motdfile) |
| 1083 | vty_out(vty, "banner motd file %s%s", host.motdfile, |
| 1084 | VTY_NEWLINE); |
| 1085 | else if (!host.motd) |
| 1086 | vty_out(vty, "no banner motd%s", VTY_NEWLINE); |
| 1087 | |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
| 1091 | /* Utility function for getting command vector. */ |
| 1092 | static vector cmd_node_vector(vector v, enum node_type ntype) |
| 1093 | { |
| 1094 | struct cmd_node *cnode = vector_slot(v, ntype); |
| 1095 | return cnode->cmd_vector; |
| 1096 | } |
| 1097 | |
| 1098 | /* Completion match types. */ |
| 1099 | enum match_type { |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1100 | NO_MATCH = 0, |
| 1101 | ANY_MATCH, |
| 1102 | EXTEND_MATCH, |
| 1103 | IPV4_PREFIX_MATCH, |
| 1104 | IPV4_MATCH, |
| 1105 | IPV6_PREFIX_MATCH, |
| 1106 | IPV6_MATCH, |
| 1107 | RANGE_MATCH, |
| 1108 | VARARG_MATCH, |
| 1109 | PARTLY_MATCH, |
| 1110 | EXACT_MATCH, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1111 | }; |
| 1112 | |
| 1113 | static enum match_type cmd_ipv4_match(const char *str) |
| 1114 | { |
| 1115 | const char *sp; |
| 1116 | int dots = 0, nums = 0; |
| 1117 | char buf[4]; |
| 1118 | |
| 1119 | if (str == NULL) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1120 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1121 | |
| 1122 | for (;;) { |
| 1123 | memset(buf, 0, sizeof(buf)); |
| 1124 | sp = str; |
| 1125 | while (*str != '\0') { |
| 1126 | if (*str == '.') { |
| 1127 | if (dots >= 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1128 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1129 | |
| 1130 | if (*(str + 1) == '.') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1131 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1132 | |
| 1133 | if (*(str + 1) == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1134 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1135 | |
| 1136 | dots++; |
| 1137 | break; |
| 1138 | } |
| 1139 | if (!isdigit((int)*str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1140 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1141 | |
| 1142 | str++; |
| 1143 | } |
| 1144 | |
| 1145 | if (str - sp > 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1146 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1147 | |
| 1148 | strncpy(buf, sp, str - sp); |
| 1149 | if (atoi(buf) > 255) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1150 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1151 | |
| 1152 | nums++; |
| 1153 | |
| 1154 | if (*str == '\0') |
| 1155 | break; |
| 1156 | |
| 1157 | str++; |
| 1158 | } |
| 1159 | |
| 1160 | if (nums < 4) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1161 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1162 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1163 | return EXACT_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | static enum match_type cmd_ipv4_prefix_match(const char *str) |
| 1167 | { |
| 1168 | const char *sp; |
| 1169 | int dots = 0; |
| 1170 | char buf[4]; |
| 1171 | |
| 1172 | if (str == NULL) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1173 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1174 | |
| 1175 | for (;;) { |
| 1176 | memset(buf, 0, sizeof(buf)); |
| 1177 | sp = str; |
| 1178 | while (*str != '\0' && *str != '/') { |
| 1179 | if (*str == '.') { |
| 1180 | if (dots == 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1181 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1182 | |
| 1183 | if (*(str + 1) == '.' || *(str + 1) == '/') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1184 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1185 | |
| 1186 | if (*(str + 1) == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1187 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1188 | |
| 1189 | dots++; |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | if (!isdigit((int)*str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1194 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1195 | |
| 1196 | str++; |
| 1197 | } |
| 1198 | |
| 1199 | if (str - sp > 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1200 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1201 | |
| 1202 | strncpy(buf, sp, str - sp); |
| 1203 | if (atoi(buf) > 255) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1204 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1205 | |
| 1206 | if (dots == 3) { |
| 1207 | if (*str == '/') { |
| 1208 | if (*(str + 1) == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1209 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1210 | |
| 1211 | str++; |
| 1212 | break; |
| 1213 | } else if (*str == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1214 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | if (*str == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1218 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1219 | |
| 1220 | str++; |
| 1221 | } |
| 1222 | |
| 1223 | sp = str; |
| 1224 | while (*str != '\0') { |
| 1225 | if (!isdigit((int)*str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1226 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1227 | |
| 1228 | str++; |
| 1229 | } |
| 1230 | |
| 1231 | if (atoi(sp) > 32) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1232 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1233 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1234 | return EXACT_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | #define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%" |
| 1238 | #define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/" |
| 1239 | #define STATE_START 1 |
| 1240 | #define STATE_COLON 2 |
| 1241 | #define STATE_DOUBLE 3 |
| 1242 | #define STATE_ADDR 4 |
| 1243 | #define STATE_DOT 5 |
| 1244 | #define STATE_SLASH 6 |
| 1245 | #define STATE_MASK 7 |
| 1246 | |
| 1247 | #ifdef HAVE_IPV6 |
| 1248 | |
| 1249 | static enum match_type cmd_ipv6_match(const char *str) |
| 1250 | { |
| 1251 | int state = STATE_START; |
| 1252 | int colons = 0, nums = 0, double_colon = 0; |
| 1253 | const char *sp = NULL; |
| 1254 | struct sockaddr_in6 sin6_dummy; |
| 1255 | int ret; |
| 1256 | |
| 1257 | if (str == NULL) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1258 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1259 | |
| 1260 | if (strspn(str, IPV6_ADDR_STR) != strlen(str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1261 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1262 | |
| 1263 | /* use inet_pton that has a better support, |
| 1264 | * for example inet_pton can support the automatic addresses: |
| 1265 | * ::1.2.3.4 |
| 1266 | */ |
| 1267 | ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr); |
| 1268 | |
| 1269 | if (ret == 1) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1270 | return EXACT_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1271 | |
| 1272 | while (*str != '\0') { |
| 1273 | switch (state) { |
| 1274 | case STATE_START: |
| 1275 | if (*str == ':') { |
| 1276 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1277 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1278 | colons--; |
| 1279 | state = STATE_COLON; |
| 1280 | } else { |
| 1281 | sp = str; |
| 1282 | state = STATE_ADDR; |
| 1283 | } |
| 1284 | |
| 1285 | continue; |
| 1286 | case STATE_COLON: |
| 1287 | colons++; |
| 1288 | if (*(str + 1) == ':') |
| 1289 | state = STATE_DOUBLE; |
| 1290 | else { |
| 1291 | sp = str + 1; |
| 1292 | state = STATE_ADDR; |
| 1293 | } |
| 1294 | break; |
| 1295 | case STATE_DOUBLE: |
| 1296 | if (double_colon) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1297 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1298 | |
| 1299 | if (*(str + 1) == ':') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1300 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1301 | else { |
| 1302 | if (*(str + 1) != '\0') |
| 1303 | colons++; |
| 1304 | sp = str + 1; |
| 1305 | state = STATE_ADDR; |
| 1306 | } |
| 1307 | |
| 1308 | double_colon++; |
| 1309 | nums++; |
| 1310 | break; |
| 1311 | case STATE_ADDR: |
| 1312 | if (*(str + 1) == ':' || *(str + 1) == '\0') { |
| 1313 | if (str - sp > 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1314 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1315 | |
| 1316 | nums++; |
| 1317 | state = STATE_COLON; |
| 1318 | } |
| 1319 | if (*(str + 1) == '.') |
| 1320 | state = STATE_DOT; |
| 1321 | break; |
| 1322 | case STATE_DOT: |
| 1323 | state = STATE_ADDR; |
| 1324 | break; |
| 1325 | default: |
| 1326 | break; |
| 1327 | } |
| 1328 | |
| 1329 | if (nums > 8) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1330 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1331 | |
| 1332 | if (colons > 7) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1333 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1334 | |
| 1335 | str++; |
| 1336 | } |
| 1337 | |
| 1338 | #if 0 |
| 1339 | if (nums < 11) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1340 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1341 | #endif /* 0 */ |
| 1342 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1343 | return EXACT_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static enum match_type cmd_ipv6_prefix_match(const char *str) |
| 1347 | { |
| 1348 | int state = STATE_START; |
| 1349 | int colons = 0, nums = 0, double_colon = 0; |
| 1350 | int mask; |
| 1351 | const char *sp = NULL; |
| 1352 | char *endptr = NULL; |
| 1353 | |
| 1354 | if (str == NULL) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1355 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1356 | |
| 1357 | if (strspn(str, IPV6_PREFIX_STR) != strlen(str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1358 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1359 | |
| 1360 | while (*str != '\0' && state != STATE_MASK) { |
| 1361 | switch (state) { |
| 1362 | case STATE_START: |
| 1363 | if (*str == ':') { |
| 1364 | if (*(str + 1) != ':' && *(str + 1) != '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1365 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1366 | colons--; |
| 1367 | state = STATE_COLON; |
| 1368 | } else { |
| 1369 | sp = str; |
| 1370 | state = STATE_ADDR; |
| 1371 | } |
| 1372 | |
| 1373 | continue; |
| 1374 | case STATE_COLON: |
| 1375 | colons++; |
| 1376 | if (*(str + 1) == '/') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1377 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1378 | else if (*(str + 1) == ':') |
| 1379 | state = STATE_DOUBLE; |
| 1380 | else { |
| 1381 | sp = str + 1; |
| 1382 | state = STATE_ADDR; |
| 1383 | } |
| 1384 | break; |
| 1385 | case STATE_DOUBLE: |
| 1386 | if (double_colon) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1387 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1388 | |
| 1389 | if (*(str + 1) == ':') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1390 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1391 | else { |
| 1392 | if (*(str + 1) != '\0' && *(str + 1) != '/') |
| 1393 | colons++; |
| 1394 | sp = str + 1; |
| 1395 | |
| 1396 | if (*(str + 1) == '/') |
| 1397 | state = STATE_SLASH; |
| 1398 | else |
| 1399 | state = STATE_ADDR; |
| 1400 | } |
| 1401 | |
| 1402 | double_colon++; |
| 1403 | nums += 1; |
| 1404 | break; |
| 1405 | case STATE_ADDR: |
| 1406 | if (*(str + 1) == ':' || *(str + 1) == '.' |
| 1407 | || *(str + 1) == '\0' || *(str + 1) == '/') { |
| 1408 | if (str - sp > 3) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1409 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1410 | |
| 1411 | for (; sp <= str; sp++) |
| 1412 | if (*sp == '/') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1413 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1414 | |
| 1415 | nums++; |
| 1416 | |
| 1417 | if (*(str + 1) == ':') |
| 1418 | state = STATE_COLON; |
| 1419 | else if (*(str + 1) == '.') |
| 1420 | state = STATE_DOT; |
| 1421 | else if (*(str + 1) == '/') |
| 1422 | state = STATE_SLASH; |
| 1423 | } |
| 1424 | break; |
| 1425 | case STATE_DOT: |
| 1426 | state = STATE_ADDR; |
| 1427 | break; |
| 1428 | case STATE_SLASH: |
| 1429 | if (*(str + 1) == '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1430 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1431 | |
| 1432 | state = STATE_MASK; |
| 1433 | break; |
| 1434 | default: |
| 1435 | break; |
| 1436 | } |
| 1437 | |
| 1438 | if (nums > 11) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1439 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1440 | |
| 1441 | if (colons > 7) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1442 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1443 | |
| 1444 | str++; |
| 1445 | } |
| 1446 | |
| 1447 | if (state < STATE_MASK) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1448 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1449 | |
| 1450 | mask = strtol(str, &endptr, 10); |
| 1451 | if (*endptr != '\0') |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1452 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1453 | |
| 1454 | if (mask < 0 || mask > 128) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1455 | return NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1456 | |
| 1457 | /* I don't know why mask < 13 makes command match partly. |
| 1458 | Forgive me to make this comments. I Want to set static default route |
| 1459 | because of lack of function to originate default in ospf6d; sorry |
| 1460 | yasu |
| 1461 | if (mask < 13) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1462 | return PARTLY_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1463 | */ |
| 1464 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1465 | return EXACT_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | #endif /* HAVE_IPV6 */ |
| 1469 | |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1470 | |
| 1471 | #if ULONG_MAX == 18446744073709551615UL |
| 1472 | #define DECIMAL_STRLEN_MAX_UNSIGNED 20 |
| 1473 | #elif ULONG_MAX == 4294967295UL |
| 1474 | #define DECIMAL_STRLEN_MAX_UNSIGNED 10 |
| 1475 | #else |
| 1476 | #error "ULONG_MAX not defined!" |
| 1477 | #endif |
| 1478 | |
| 1479 | #if LONG_MAX == 9223372036854775807L |
| 1480 | #define DECIMAL_STRLEN_MAX_SIGNED 19 |
| 1481 | #elif LONG_MAX == 2147483647L |
| 1482 | #define DECIMAL_STRLEN_MAX_SIGNED 10 |
| 1483 | #else |
| 1484 | #error "LONG_MAX not defined!" |
| 1485 | #endif |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1486 | |
Philipp Maier | eabc6fd | 2021-06-15 11:08:23 +0200 | [diff] [blame] | 1487 | int vty_cmd_range_match(const char *range, const char *str) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1488 | { |
| 1489 | char *p; |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1490 | char buf[DECIMAL_STRLEN_MAX_UNSIGNED + 1]; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1491 | char *endptr = NULL; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1492 | |
| 1493 | if (str == NULL) |
| 1494 | return 1; |
| 1495 | |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1496 | if (range[1] == '-') { |
| 1497 | signed long min = 0, max = 0, val; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1498 | |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1499 | val = strtol(str, &endptr, 10); |
| 1500 | if (*endptr != '\0') |
| 1501 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1502 | |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1503 | range += 2; |
| 1504 | p = strchr(range, '-'); |
| 1505 | if (p == NULL) |
| 1506 | return 0; |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1507 | if (p - range > DECIMAL_STRLEN_MAX_SIGNED) |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1508 | return 0; |
| 1509 | strncpy(buf, range, p - range); |
| 1510 | buf[p - range] = '\0'; |
| 1511 | min = -strtol(buf, &endptr, 10); |
| 1512 | if (*endptr != '\0') |
| 1513 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1514 | |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1515 | range = p + 1; |
| 1516 | p = strchr(range, '>'); |
| 1517 | if (p == NULL) |
| 1518 | return 0; |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1519 | if (p - range > DECIMAL_STRLEN_MAX_SIGNED) |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1520 | return 0; |
| 1521 | strncpy(buf, range, p - range); |
| 1522 | buf[p - range] = '\0'; |
| 1523 | max = strtol(buf, &endptr, 10); |
| 1524 | if (*endptr != '\0') |
| 1525 | return 0; |
| 1526 | |
| 1527 | if (val < min || val > max) |
| 1528 | return 0; |
| 1529 | } else { |
| 1530 | unsigned long min, max, val; |
| 1531 | |
Pau Espin Pedrol | a184701 | 2020-07-28 17:44:48 +0200 | [diff] [blame] | 1532 | if (str[0] == '-') |
| 1533 | return 0; |
| 1534 | |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1535 | val = strtoul(str, &endptr, 10); |
| 1536 | if (*endptr != '\0') |
| 1537 | return 0; |
| 1538 | |
| 1539 | range++; |
| 1540 | p = strchr(range, '-'); |
| 1541 | if (p == NULL) |
| 1542 | return 0; |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1543 | if (p - range > DECIMAL_STRLEN_MAX_UNSIGNED) |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1544 | return 0; |
| 1545 | strncpy(buf, range, p - range); |
| 1546 | buf[p - range] = '\0'; |
| 1547 | min = strtoul(buf, &endptr, 10); |
| 1548 | if (*endptr != '\0') |
| 1549 | return 0; |
| 1550 | |
| 1551 | range = p + 1; |
| 1552 | p = strchr(range, '>'); |
| 1553 | if (p == NULL) |
| 1554 | return 0; |
Pau Espin Pedrol | 9fdc871 | 2020-07-30 14:56:38 +0200 | [diff] [blame] | 1555 | if (p - range > DECIMAL_STRLEN_MAX_UNSIGNED) |
Andreas Eversberg | 33f0fc3 | 2010-07-13 13:50:39 +0200 | [diff] [blame] | 1556 | return 0; |
| 1557 | strncpy(buf, range, p - range); |
| 1558 | buf[p - range] = '\0'; |
| 1559 | max = strtoul(buf, &endptr, 10); |
| 1560 | if (*endptr != '\0') |
| 1561 | return 0; |
| 1562 | |
| 1563 | if (val < min || val > max) |
| 1564 | return 0; |
| 1565 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1566 | |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1570 | /* helper to retrieve the 'real' argument string from an optional argument */ |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1571 | static char *cmd_deopt(void *ctx, const char *str) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1572 | { |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1573 | /* we've got "[blah]". We want to strip off the []s and redo the |
| 1574 | * match check for "blah" |
| 1575 | */ |
| 1576 | size_t len = strlen(str); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1577 | |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1578 | if (len < 3) |
| 1579 | return NULL; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1580 | |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1581 | return talloc_strndup(ctx, str + 1, len - 2); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1584 | static enum match_type |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1585 | cmd_match(const char *str, const char *command, |
| 1586 | enum match_type min, bool recur) |
| 1587 | { |
| 1588 | |
| 1589 | if (recur && CMD_OPTION(str)) |
| 1590 | { |
| 1591 | enum match_type ret; |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1592 | char *tmp = cmd_deopt(tall_vty_cmd_ctx, str); |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1593 | |
| 1594 | /* this would be a bug in a command, however handle it gracefully |
| 1595 | * as it we only discover it if a user tries to run it |
| 1596 | */ |
| 1597 | if (tmp == NULL) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1598 | return NO_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1599 | |
| 1600 | ret = cmd_match(tmp, command, min, false); |
| 1601 | |
| 1602 | talloc_free(tmp); |
| 1603 | |
| 1604 | return ret; |
| 1605 | } |
| 1606 | else if (CMD_VARARG(str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1607 | return VARARG_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1608 | else if (CMD_RANGE(str)) |
| 1609 | { |
Philipp Maier | eabc6fd | 2021-06-15 11:08:23 +0200 | [diff] [blame] | 1610 | if (vty_cmd_range_match(str, command)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1611 | return RANGE_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1612 | } |
| 1613 | #ifdef HAVE_IPV6 |
| 1614 | else if (CMD_IPV6(str)) |
| 1615 | { |
| 1616 | if (cmd_ipv6_match(command) >= min) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1617 | return IPV6_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1618 | } |
| 1619 | else if (CMD_IPV6_PREFIX(str)) |
| 1620 | { |
| 1621 | if (cmd_ipv6_prefix_match(command) >= min) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1622 | return IPV6_PREFIX_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1623 | } |
| 1624 | #endif /* HAVE_IPV6 */ |
| 1625 | else if (CMD_IPV4(str)) |
| 1626 | { |
| 1627 | if (cmd_ipv4_match(command) >= min) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1628 | return IPV4_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1629 | } |
| 1630 | else if (CMD_IPV4_PREFIX(str)) |
| 1631 | { |
| 1632 | if (cmd_ipv4_prefix_match(command) >= min) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1633 | return IPV4_PREFIX_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1634 | } |
| 1635 | else if (CMD_VARIABLE(str)) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1636 | return EXTEND_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1637 | else if (strncmp(command, str, strlen(command)) == 0) |
| 1638 | { |
| 1639 | if (strcmp(command, str) == 0) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1640 | return EXACT_MATCH; |
| 1641 | else if (PARTLY_MATCH >= min) |
| 1642 | return PARTLY_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1645 | return NO_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* Filter vector at the specified index and by the given command string, to |
| 1649 | * the desired matching level (thus allowing part matches), and return match |
| 1650 | * type flag. |
| 1651 | */ |
| 1652 | static enum match_type |
| 1653 | cmd_filter(char *command, vector v, unsigned int index, enum match_type level) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1654 | { |
| 1655 | unsigned int i; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1656 | struct cmd_element *cmd_element; |
| 1657 | enum match_type match_type; |
| 1658 | vector descvec; |
| 1659 | struct desc *desc; |
| 1660 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1661 | match_type = NO_MATCH; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* If command and cmd_element string does not match set NULL to vector */ |
| 1664 | for (i = 0; i < vector_active(v); i++) |
| 1665 | if ((cmd_element = vector_slot(v, i)) != NULL) { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1666 | if (index >= vector_active(cmd_element->strvec)) |
| 1667 | vector_slot(v, i) = NULL; |
| 1668 | else { |
| 1669 | unsigned int j; |
| 1670 | int matched = 0; |
| 1671 | |
| 1672 | descvec = |
| 1673 | vector_slot(cmd_element->strvec, index); |
| 1674 | |
| 1675 | for (j = 0; j < vector_active(descvec); j++) |
| 1676 | if ((desc = vector_slot(descvec, j))) { |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1677 | enum match_type ret; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1678 | |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1679 | ret = cmd_match (desc->cmd, command, level, true); |
| 1680 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1681 | if (ret != NO_MATCH) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1682 | matched++; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1683 | |
| 1684 | if (match_type < ret) |
| 1685 | match_type = ret; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1686 | } |
| 1687 | if (!matched) |
| 1688 | vector_slot(v, i) = NULL; |
| 1689 | } |
| 1690 | } |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1691 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1692 | if (match_type == NO_MATCH) |
| 1693 | return NO_MATCH; |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1694 | |
| 1695 | /* 2nd pass: We now know the 'strongest' match type for the index, so we |
| 1696 | * go again and filter out commands whose argument (at this index) is |
| 1697 | * 'weaker'. E.g., if we have 2 commands: |
| 1698 | * |
| 1699 | * foo bar <1-255> |
| 1700 | * foo bar BLAH |
| 1701 | * |
| 1702 | * and the command string is 'foo bar 10', then we will get here with with |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1703 | * 'RANGE_MATCH' being the strongest match. However, if 'BLAH' came |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1704 | * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10"). |
| 1705 | * |
| 1706 | * If we don't do a 2nd pass and filter it out, the higher-layers will |
| 1707 | * consider this to be ambiguous. |
| 1708 | */ |
| 1709 | for (i = 0; i < vector_active(v); i++) |
| 1710 | if ((cmd_element = vector_slot(v, i)) != NULL) { |
| 1711 | if (index >= vector_active(cmd_element->strvec)) |
| 1712 | vector_slot(v, i) = NULL; |
| 1713 | else { |
| 1714 | unsigned int j; |
| 1715 | int matched = 0; |
| 1716 | |
| 1717 | descvec = |
| 1718 | vector_slot(cmd_element->strvec, index); |
| 1719 | |
| 1720 | for (j = 0; j < vector_active(descvec); j++) |
| 1721 | if ((desc = vector_slot(descvec, j))) { |
| 1722 | enum match_type ret; |
| 1723 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 1724 | ret = cmd_match(desc->cmd, command, ANY_MATCH, true); |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 1725 | |
| 1726 | if (ret >= match_type) |
| 1727 | matched++; |
| 1728 | } |
| 1729 | if (!matched) |
| 1730 | vector_slot(v, i) = NULL; |
| 1731 | } |
| 1732 | } |
| 1733 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1734 | return match_type; |
| 1735 | } |
| 1736 | |
| 1737 | /* Check ambiguous match */ |
| 1738 | static int |
| 1739 | is_cmd_ambiguous(char *command, vector v, int index, enum match_type type) |
| 1740 | { |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1741 | int ret = 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1742 | unsigned int i; |
| 1743 | unsigned int j; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1744 | struct cmd_element *cmd_element; |
| 1745 | const char *matched = NULL; |
| 1746 | vector descvec; |
| 1747 | struct desc *desc; |
| 1748 | |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1749 | /* In this loop, when a match is found, 'matched' points to it. If on a later iteration, an |
| 1750 | * identical match is found, the command is ambiguous. The trickiness is that a string may be |
| 1751 | * enclosed in '[str]' square brackets, which get removed by a talloc_strndup(), via cmd_deopt(). |
| 1752 | * Such a string is usually needed for one loop iteration, except when 'matched' points to it. In |
| 1753 | * that case, the string must remain allocated until this function exits or another match comes |
| 1754 | * around. This is sufficiently confusing to justify a separate talloc tree to store all of the |
| 1755 | * odd allocations, and to free them all at the end. We are not expecting too many optional args |
| 1756 | * or ambiguities to cause a noticeable memory footprint from keeping all allocations. */ |
| 1757 | void *cmd_deopt_ctx = NULL; |
| 1758 | |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1759 | for (i = 0; i < vector_active(v); i++) { |
| 1760 | cmd_element = vector_slot(v, i); |
| 1761 | if (!cmd_element) |
| 1762 | continue; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1763 | |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1764 | int match = 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1765 | |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1766 | descvec = vector_slot(cmd_element->strvec, index); |
| 1767 | |
| 1768 | for (j = 0; j < vector_active(descvec); j++) { |
| 1769 | desc = vector_slot(descvec, j); |
| 1770 | if (!desc) |
| 1771 | continue; |
| 1772 | |
| 1773 | enum match_type mtype; |
| 1774 | const char *str = desc->cmd; |
| 1775 | |
| 1776 | if (CMD_OPTION(str)) { |
| 1777 | if (!cmd_deopt_ctx) |
| 1778 | cmd_deopt_ctx = |
| 1779 | talloc_named_const(tall_vty_cmd_ctx, 0, |
| 1780 | __func__); |
| 1781 | str = cmd_deopt(cmd_deopt_ctx, str); |
| 1782 | if (str == NULL) |
Pau Espin Pedrol | 4742526 | 2019-06-11 21:04:09 +0200 | [diff] [blame] | 1783 | continue; |
Pau Espin Pedrol | 4742526 | 2019-06-11 21:04:09 +0200 | [diff] [blame] | 1784 | } |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1785 | |
| 1786 | switch (type) { |
| 1787 | case EXACT_MATCH: |
| 1788 | if (!(CMD_VARIABLE (str)) |
| 1789 | && strcmp(command, str) == 0) |
| 1790 | match++; |
| 1791 | break; |
| 1792 | case PARTLY_MATCH: |
| 1793 | if (!(CMD_VARIABLE (str)) |
| 1794 | && strncmp(command, str, strlen (command)) == 0) |
| 1795 | { |
| 1796 | if (matched |
| 1797 | && strcmp(matched, |
| 1798 | str) != 0) { |
| 1799 | ret = 1; /* There is ambiguous match. */ |
| 1800 | goto free_and_return; |
| 1801 | } else |
| 1802 | matched = str; |
| 1803 | match++; |
| 1804 | } |
| 1805 | break; |
| 1806 | case RANGE_MATCH: |
Philipp Maier | eabc6fd | 2021-06-15 11:08:23 +0200 | [diff] [blame] | 1807 | if (vty_cmd_range_match |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1808 | (str, command)) { |
| 1809 | if (matched |
| 1810 | && strcmp(matched, |
| 1811 | str) != 0) { |
| 1812 | ret = 1; |
| 1813 | goto free_and_return; |
| 1814 | } else |
| 1815 | matched = str; |
| 1816 | match++; |
| 1817 | } |
| 1818 | break; |
| 1819 | #ifdef HAVE_IPV6 |
| 1820 | case IPV6_MATCH: |
| 1821 | if (CMD_IPV6(str)) |
| 1822 | match++; |
| 1823 | break; |
| 1824 | case IPV6_PREFIX_MATCH: |
| 1825 | if ((mtype = |
| 1826 | cmd_ipv6_prefix_match |
| 1827 | (command)) != NO_MATCH) { |
| 1828 | if (mtype == PARTLY_MATCH) { |
| 1829 | ret = 2; /* There is incomplete match. */ |
| 1830 | goto free_and_return; |
| 1831 | } |
| 1832 | |
| 1833 | match++; |
| 1834 | } |
| 1835 | break; |
| 1836 | #endif /* HAVE_IPV6 */ |
| 1837 | case IPV4_MATCH: |
| 1838 | if (CMD_IPV4(str)) |
| 1839 | match++; |
| 1840 | break; |
| 1841 | case IPV4_PREFIX_MATCH: |
| 1842 | if ((mtype = |
| 1843 | cmd_ipv4_prefix_match |
| 1844 | (command)) != NO_MATCH) { |
| 1845 | if (mtype == PARTLY_MATCH) { |
| 1846 | ret = 2; /* There is incomplete match. */ |
| 1847 | goto free_and_return; |
| 1848 | } |
| 1849 | |
| 1850 | match++; |
| 1851 | } |
| 1852 | break; |
| 1853 | case EXTEND_MATCH: |
| 1854 | if (CMD_VARIABLE (str)) |
| 1855 | match++; |
| 1856 | break; |
| 1857 | case NO_MATCH: |
| 1858 | default: |
| 1859 | break; |
| 1860 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1861 | } |
Pau Espin Pedrol | 274ac4d | 2019-06-12 13:06:41 +0200 | [diff] [blame] | 1862 | if (!match) |
| 1863 | vector_slot(v, i) = NULL; |
| 1864 | } |
Neels Hofmeyr | 5314c51 | 2018-07-09 23:22:21 +0200 | [diff] [blame] | 1865 | |
| 1866 | free_and_return: |
| 1867 | if (cmd_deopt_ctx) |
| 1868 | talloc_free(cmd_deopt_ctx); |
| 1869 | return ret; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1873 | static const char *cmd_entry_function(const char *src, const char *dst) |
| 1874 | { |
| 1875 | /* Skip variable arguments. */ |
| 1876 | if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) || |
| 1877 | CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst)) |
| 1878 | return NULL; |
| 1879 | |
| 1880 | /* In case of 'command \t', given src is NULL string. */ |
| 1881 | if (src == NULL) |
| 1882 | return dst; |
| 1883 | |
| 1884 | /* Matched with input string. */ |
| 1885 | if (strncmp(src, dst, strlen(src)) == 0) |
| 1886 | return dst; |
| 1887 | |
| 1888 | return NULL; |
| 1889 | } |
| 1890 | |
| 1891 | /* If src matches dst return dst string, otherwise return NULL */ |
| 1892 | /* This version will return the dst string always if it is |
| 1893 | CMD_VARIABLE for '?' key processing */ |
| 1894 | static const char *cmd_entry_function_desc(const char *src, const char *dst) |
| 1895 | { |
| 1896 | if (CMD_VARARG(dst)) |
| 1897 | return dst; |
| 1898 | |
| 1899 | if (CMD_RANGE(dst)) { |
Philipp Maier | eabc6fd | 2021-06-15 11:08:23 +0200 | [diff] [blame] | 1900 | if (vty_cmd_range_match(dst, src)) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1901 | return dst; |
| 1902 | else |
| 1903 | return NULL; |
| 1904 | } |
| 1905 | #ifdef HAVE_IPV6 |
| 1906 | if (CMD_IPV6(dst)) { |
| 1907 | if (cmd_ipv6_match(src)) |
| 1908 | return dst; |
| 1909 | else |
| 1910 | return NULL; |
| 1911 | } |
| 1912 | |
| 1913 | if (CMD_IPV6_PREFIX(dst)) { |
| 1914 | if (cmd_ipv6_prefix_match(src)) |
| 1915 | return dst; |
| 1916 | else |
| 1917 | return NULL; |
| 1918 | } |
| 1919 | #endif /* HAVE_IPV6 */ |
| 1920 | |
| 1921 | if (CMD_IPV4(dst)) { |
| 1922 | if (cmd_ipv4_match(src)) |
| 1923 | return dst; |
| 1924 | else |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | |
| 1928 | if (CMD_IPV4_PREFIX(dst)) { |
| 1929 | if (cmd_ipv4_prefix_match(src)) |
| 1930 | return dst; |
| 1931 | else |
| 1932 | return NULL; |
| 1933 | } |
| 1934 | |
| 1935 | /* Optional or variable commands always match on '?' */ |
| 1936 | if (CMD_OPTION(dst) || CMD_VARIABLE(dst)) |
| 1937 | return dst; |
| 1938 | |
| 1939 | /* In case of 'command \t', given src is NULL string. */ |
| 1940 | if (src == NULL) |
| 1941 | return dst; |
| 1942 | |
| 1943 | if (strncmp(src, dst, strlen(src)) == 0) |
| 1944 | return dst; |
| 1945 | else |
| 1946 | return NULL; |
| 1947 | } |
| 1948 | |
| 1949 | /* Check same string element existence. If it isn't there return |
| 1950 | 1. */ |
| 1951 | static int cmd_unique_string(vector v, const char *str) |
| 1952 | { |
| 1953 | unsigned int i; |
| 1954 | char *match; |
| 1955 | |
| 1956 | for (i = 0; i < vector_active(v); i++) |
| 1957 | if ((match = vector_slot(v, i)) != NULL) |
| 1958 | if (strcmp(match, str) == 0) |
| 1959 | return 0; |
| 1960 | return 1; |
| 1961 | } |
| 1962 | |
| 1963 | /* Compare string to description vector. If there is same string |
| 1964 | return 1 else return 0. */ |
| 1965 | static int desc_unique_string(vector v, const char *str) |
| 1966 | { |
| 1967 | unsigned int i; |
| 1968 | struct desc *desc; |
| 1969 | |
| 1970 | for (i = 0; i < vector_active(v); i++) |
| 1971 | if ((desc = vector_slot(v, i)) != NULL) |
| 1972 | if (strcmp(desc->cmd, str) == 0) |
| 1973 | return 1; |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
| 1977 | static int cmd_try_do_shortcut(enum node_type node, char *first_word) |
| 1978 | { |
| 1979 | if (first_word != NULL && |
| 1980 | node != AUTH_NODE && |
| 1981 | node != VIEW_NODE && |
| 1982 | node != AUTH_ENABLE_NODE && |
| 1983 | node != ENABLE_NODE && 0 == strcmp("do", first_word)) |
| 1984 | return 1; |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
| 1988 | /* '?' describe command support. */ |
| 1989 | static vector |
| 1990 | cmd_describe_command_real(vector vline, struct vty *vty, int *status) |
| 1991 | { |
| 1992 | unsigned int i; |
| 1993 | vector cmd_vector; |
| 1994 | #define INIT_MATCHVEC_SIZE 10 |
| 1995 | vector matchvec; |
| 1996 | struct cmd_element *cmd_element; |
| 1997 | unsigned int index; |
| 1998 | int ret; |
| 1999 | enum match_type match; |
| 2000 | char *command; |
| 2001 | static struct desc desc_cr = { "<cr>", "" }; |
| 2002 | |
| 2003 | /* Set index. */ |
| 2004 | if (vector_active(vline) == 0) { |
| 2005 | *status = CMD_ERR_NO_MATCH; |
| 2006 | return NULL; |
| 2007 | } else |
| 2008 | index = vector_active(vline) - 1; |
| 2009 | |
| 2010 | /* Make copy vector of current node's command vector. */ |
| 2011 | cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node)); |
| 2012 | |
| 2013 | /* Prepare match vector */ |
| 2014 | matchvec = vector_init(INIT_MATCHVEC_SIZE); |
| 2015 | |
| 2016 | /* Filter commands. */ |
| 2017 | /* Only words precedes current word will be checked in this loop. */ |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2018 | for (i = 0; i < index; i++) { |
| 2019 | command = vector_slot(vline, i); |
| 2020 | if (!command) |
| 2021 | continue; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2022 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2023 | match = cmd_filter(command, cmd_vector, i, ANY_MATCH); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2024 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2025 | if (match == VARARG_MATCH) { |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2026 | struct cmd_element *cmd_element; |
| 2027 | vector descvec; |
| 2028 | unsigned int j, k; |
| 2029 | |
| 2030 | for (j = 0; j < vector_active(cmd_vector); j++) |
| 2031 | if ((cmd_element = |
| 2032 | vector_slot(cmd_vector, j)) != NULL |
| 2033 | && |
| 2034 | (vector_active(cmd_element->strvec))) { |
| 2035 | descvec = |
| 2036 | vector_slot(cmd_element-> |
| 2037 | strvec, |
| 2038 | vector_active |
| 2039 | (cmd_element-> |
| 2040 | strvec) - 1); |
| 2041 | for (k = 0; |
| 2042 | k < vector_active(descvec); |
| 2043 | k++) { |
| 2044 | struct desc *desc = |
| 2045 | vector_slot(descvec, |
| 2046 | k); |
| 2047 | vector_set(matchvec, |
| 2048 | desc); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2049 | } |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2050 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2051 | |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2052 | vector_set(matchvec, &desc_cr); |
| 2053 | vector_free(cmd_vector); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2054 | |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2055 | return matchvec; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2058 | if ((ret = is_cmd_ambiguous(command, cmd_vector, i, |
| 2059 | match)) == 1) { |
| 2060 | vector_free(cmd_vector); |
Holger Hans Peter Freyther | 047213b | 2013-07-03 09:32:37 +0200 | [diff] [blame] | 2061 | vector_free(matchvec); |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2062 | *status = CMD_ERR_AMBIGUOUS; |
| 2063 | return NULL; |
| 2064 | } else if (ret == 2) { |
| 2065 | vector_free(cmd_vector); |
Holger Hans Peter Freyther | 047213b | 2013-07-03 09:32:37 +0200 | [diff] [blame] | 2066 | vector_free(matchvec); |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2067 | *status = CMD_ERR_NO_MATCH; |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | } |
| 2071 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2072 | /* Prepare match vector */ |
| 2073 | /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */ |
| 2074 | |
| 2075 | /* Make sure that cmd_vector is filtered based on current word */ |
| 2076 | command = vector_slot(vline, index); |
| 2077 | if (command) |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2078 | cmd_filter(command, cmd_vector, index, ANY_MATCH); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2079 | |
| 2080 | /* Make description vector. */ |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2081 | for (i = 0; i < vector_active(cmd_vector); i++) { |
| 2082 | const char *string = NULL; |
| 2083 | vector strvec; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2084 | |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2085 | cmd_element = vector_slot(cmd_vector, i); |
| 2086 | if (!cmd_element) |
| 2087 | continue; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2088 | |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 2089 | if (cmd_element->attr & CMD_ATTR_DEPRECATED) |
| 2090 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 2091 | if (!vty->expert_mode && (cmd_element->attr & CMD_ATTR_HIDDEN)) |
Harald Welte | d17aa59 | 2013-02-12 11:11:34 +0100 | [diff] [blame] | 2092 | continue; |
| 2093 | |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2094 | strvec = cmd_element->strvec; |
| 2095 | |
| 2096 | /* if command is NULL, index may be equal to vector_active */ |
| 2097 | if (command && index >= vector_active(strvec)) |
| 2098 | vector_slot(cmd_vector, i) = NULL; |
| 2099 | else { |
| 2100 | /* Check if command is completed. */ |
| 2101 | if (command == NULL |
| 2102 | && index == vector_active(strvec)) { |
| 2103 | string = "<cr>"; |
| 2104 | if (!desc_unique_string(matchvec, string)) |
| 2105 | vector_set(matchvec, &desc_cr); |
| 2106 | } else { |
| 2107 | unsigned int j; |
| 2108 | vector descvec = vector_slot(strvec, index); |
| 2109 | struct desc *desc; |
| 2110 | |
| 2111 | for (j = 0; j < vector_active(descvec); j++) { |
| 2112 | desc = vector_slot(descvec, j); |
| 2113 | if (!desc) |
| 2114 | continue; |
| 2115 | string = cmd_entry_function_desc |
| 2116 | (command, desc->cmd); |
| 2117 | if (!string) |
| 2118 | continue; |
| 2119 | /* Uniqueness check */ |
| 2120 | if (!desc_unique_string(matchvec, string)) |
| 2121 | vector_set(matchvec, desc); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | } |
Harald Welte | 80d30fe | 2013-02-12 11:08:57 +0100 | [diff] [blame] | 2125 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2126 | vector_free(cmd_vector); |
| 2127 | |
| 2128 | if (vector_slot(matchvec, 0) == NULL) { |
| 2129 | vector_free(matchvec); |
| 2130 | *status = CMD_ERR_NO_MATCH; |
| 2131 | } else |
| 2132 | *status = CMD_SUCCESS; |
| 2133 | |
| 2134 | return matchvec; |
| 2135 | } |
| 2136 | |
| 2137 | vector cmd_describe_command(vector vline, struct vty * vty, int *status) |
| 2138 | { |
| 2139 | vector ret; |
| 2140 | |
| 2141 | if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) { |
| 2142 | enum node_type onode; |
| 2143 | vector shifted_vline; |
| 2144 | unsigned int index; |
| 2145 | |
| 2146 | onode = vty->node; |
| 2147 | vty->node = ENABLE_NODE; |
| 2148 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2149 | |
| 2150 | shifted_vline = vector_init(vector_count(vline)); |
| 2151 | /* use memcpy? */ |
| 2152 | for (index = 1; index < vector_active(vline); index++) { |
| 2153 | vector_set_index(shifted_vline, index - 1, |
| 2154 | vector_lookup(vline, index)); |
| 2155 | } |
| 2156 | |
| 2157 | ret = cmd_describe_command_real(shifted_vline, vty, status); |
| 2158 | |
| 2159 | vector_free(shifted_vline); |
| 2160 | vty->node = onode; |
| 2161 | return ret; |
| 2162 | } |
| 2163 | |
| 2164 | return cmd_describe_command_real(vline, vty, status); |
| 2165 | } |
| 2166 | |
| 2167 | /* Check LCD of matched command. */ |
| 2168 | static int cmd_lcd(char **matched) |
| 2169 | { |
| 2170 | int i; |
| 2171 | int j; |
| 2172 | int lcd = -1; |
| 2173 | char *s1, *s2; |
| 2174 | char c1, c2; |
| 2175 | |
| 2176 | if (matched[0] == NULL || matched[1] == NULL) |
| 2177 | return 0; |
| 2178 | |
| 2179 | for (i = 1; matched[i] != NULL; i++) { |
| 2180 | s1 = matched[i - 1]; |
| 2181 | s2 = matched[i]; |
| 2182 | |
| 2183 | for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++) |
| 2184 | if (c1 != c2) |
| 2185 | break; |
| 2186 | |
| 2187 | if (lcd < 0) |
| 2188 | lcd = j; |
| 2189 | else { |
| 2190 | if (lcd > j) |
| 2191 | lcd = j; |
| 2192 | } |
| 2193 | } |
| 2194 | return lcd; |
| 2195 | } |
| 2196 | |
| 2197 | /* Command line completion support. */ |
| 2198 | static char **cmd_complete_command_real(vector vline, struct vty *vty, |
| 2199 | int *status) |
| 2200 | { |
| 2201 | unsigned int i; |
| 2202 | vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node)); |
| 2203 | #define INIT_MATCHVEC_SIZE 10 |
| 2204 | vector matchvec; |
| 2205 | struct cmd_element *cmd_element; |
| 2206 | unsigned int index; |
| 2207 | char **match_str; |
| 2208 | struct desc *desc; |
| 2209 | vector descvec; |
| 2210 | char *command; |
| 2211 | int lcd; |
| 2212 | |
| 2213 | if (vector_active(vline) == 0) { |
| 2214 | *status = CMD_ERR_NO_MATCH; |
Holger Hans Peter Freyther | 047213b | 2013-07-03 09:32:37 +0200 | [diff] [blame] | 2215 | vector_free(cmd_vector); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2216 | return NULL; |
| 2217 | } else |
| 2218 | index = vector_active(vline) - 1; |
| 2219 | |
| 2220 | /* First, filter by preceeding command string */ |
| 2221 | for (i = 0; i < index; i++) |
| 2222 | if ((command = vector_slot(vline, i))) { |
| 2223 | enum match_type match; |
| 2224 | int ret; |
| 2225 | |
| 2226 | /* First try completion match, if there is exactly match return 1 */ |
| 2227 | match = |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2228 | cmd_filter(command, cmd_vector, i, ANY_MATCH); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2229 | |
| 2230 | /* If there is exact match then filter ambiguous match else check |
| 2231 | ambiguousness. */ |
| 2232 | if ((ret = |
| 2233 | is_cmd_ambiguous(command, cmd_vector, i, |
| 2234 | match)) == 1) { |
| 2235 | vector_free(cmd_vector); |
| 2236 | *status = CMD_ERR_AMBIGUOUS; |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | /* |
| 2240 | else if (ret == 2) |
| 2241 | { |
| 2242 | vector_free (cmd_vector); |
| 2243 | *status = CMD_ERR_NO_MATCH; |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | */ |
| 2247 | } |
| 2248 | |
| 2249 | /* Prepare match vector. */ |
| 2250 | matchvec = vector_init(INIT_MATCHVEC_SIZE); |
| 2251 | |
| 2252 | /* Now we got into completion */ |
| 2253 | for (i = 0; i < vector_active(cmd_vector); i++) |
| 2254 | if ((cmd_element = vector_slot(cmd_vector, i))) { |
| 2255 | const char *string; |
| 2256 | vector strvec = cmd_element->strvec; |
| 2257 | |
| 2258 | /* Check field length */ |
| 2259 | if (index >= vector_active(strvec)) |
| 2260 | vector_slot(cmd_vector, i) = NULL; |
| 2261 | else { |
| 2262 | unsigned int j; |
| 2263 | |
| 2264 | descvec = vector_slot(strvec, index); |
| 2265 | for (j = 0; j < vector_active(descvec); j++) |
| 2266 | if ((desc = vector_slot(descvec, j))) { |
Neels Hofmeyr | 9ea9dd0 | 2019-01-31 08:15:23 +0100 | [diff] [blame] | 2267 | const char *cmd = desc->cmd; |
| 2268 | char *tmp = NULL; |
| 2269 | |
| 2270 | if (CMD_OPTION(desc->cmd)) { |
| 2271 | tmp = cmd_deopt(tall_vty_cmd_ctx, desc->cmd); |
| 2272 | cmd = tmp; |
| 2273 | } |
| 2274 | if ((string = cmd_entry_function(vector_slot(vline, index), cmd))) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2275 | if (cmd_unique_string (matchvec, string)) |
| 2276 | vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string)); |
Neels Hofmeyr | 9ea9dd0 | 2019-01-31 08:15:23 +0100 | [diff] [blame] | 2277 | if (tmp) |
| 2278 | talloc_free(tmp); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | /* We don't need cmd_vector any more. */ |
| 2284 | vector_free(cmd_vector); |
| 2285 | |
| 2286 | /* No matched command */ |
| 2287 | if (vector_slot(matchvec, 0) == NULL) { |
| 2288 | vector_free(matchvec); |
| 2289 | |
| 2290 | /* In case of 'command \t' pattern. Do you need '?' command at |
| 2291 | the end of the line. */ |
Pau Espin Pedrol | 0f7bcb5 | 2017-11-02 19:08:14 +0100 | [diff] [blame] | 2292 | if (vector_slot(vline, index) == NULL) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2293 | *status = CMD_ERR_NOTHING_TODO; |
| 2294 | else |
| 2295 | *status = CMD_ERR_NO_MATCH; |
| 2296 | return NULL; |
| 2297 | } |
| 2298 | |
| 2299 | /* Only one matched */ |
| 2300 | if (vector_slot(matchvec, 1) == NULL) { |
| 2301 | match_str = (char **)matchvec->index; |
| 2302 | vector_only_wrapper_free(matchvec); |
| 2303 | *status = CMD_COMPLETE_FULL_MATCH; |
| 2304 | return match_str; |
| 2305 | } |
| 2306 | /* Make it sure last element is NULL. */ |
| 2307 | vector_set(matchvec, NULL); |
| 2308 | |
| 2309 | /* Check LCD of matched strings. */ |
| 2310 | if (vector_slot(vline, index) != NULL) { |
| 2311 | lcd = cmd_lcd((char **)matchvec->index); |
| 2312 | |
| 2313 | if (lcd) { |
| 2314 | int len = strlen(vector_slot(vline, index)); |
| 2315 | |
| 2316 | if (len < lcd) { |
| 2317 | char *lcdstr; |
| 2318 | |
| 2319 | lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1, |
| 2320 | "complete-lcdstr"); |
| 2321 | memcpy(lcdstr, matchvec->index[0], lcd); |
| 2322 | lcdstr[lcd] = '\0'; |
| 2323 | |
| 2324 | /* match_str = (char **) &lcdstr; */ |
| 2325 | |
| 2326 | /* Free matchvec. */ |
| 2327 | for (i = 0; i < vector_active(matchvec); i++) { |
| 2328 | if (vector_slot(matchvec, i)) |
| 2329 | talloc_free(vector_slot(matchvec, i)); |
| 2330 | } |
| 2331 | vector_free(matchvec); |
| 2332 | |
| 2333 | /* Make new matchvec. */ |
| 2334 | matchvec = vector_init(INIT_MATCHVEC_SIZE); |
| 2335 | vector_set(matchvec, lcdstr); |
| 2336 | match_str = (char **)matchvec->index; |
| 2337 | vector_only_wrapper_free(matchvec); |
| 2338 | |
| 2339 | *status = CMD_COMPLETE_MATCH; |
| 2340 | return match_str; |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | match_str = (char **)matchvec->index; |
| 2346 | vector_only_wrapper_free(matchvec); |
| 2347 | *status = CMD_COMPLETE_LIST_MATCH; |
| 2348 | return match_str; |
| 2349 | } |
| 2350 | |
| 2351 | char **cmd_complete_command(vector vline, struct vty *vty, int *status) |
| 2352 | { |
| 2353 | char **ret; |
| 2354 | |
| 2355 | if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) { |
| 2356 | enum node_type onode; |
| 2357 | vector shifted_vline; |
| 2358 | unsigned int index; |
| 2359 | |
| 2360 | onode = vty->node; |
| 2361 | vty->node = ENABLE_NODE; |
| 2362 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2363 | |
| 2364 | shifted_vline = vector_init(vector_count(vline)); |
| 2365 | /* use memcpy? */ |
| 2366 | for (index = 1; index < vector_active(vline); index++) { |
| 2367 | vector_set_index(shifted_vline, index - 1, |
| 2368 | vector_lookup(vline, index)); |
| 2369 | } |
| 2370 | |
| 2371 | ret = cmd_complete_command_real(shifted_vline, vty, status); |
| 2372 | |
| 2373 | vector_free(shifted_vline); |
| 2374 | vty->node = onode; |
| 2375 | return ret; |
| 2376 | } |
| 2377 | |
| 2378 | return cmd_complete_command_real(vline, vty, status); |
| 2379 | } |
| 2380 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2381 | static struct vty_parent_node *vty_parent(struct vty *vty) |
| 2382 | { |
| 2383 | return llist_first_entry_or_null(&vty->parent_nodes, |
| 2384 | struct vty_parent_node, |
| 2385 | entry); |
| 2386 | } |
| 2387 | |
| 2388 | static bool vty_pop_parent(struct vty *vty) |
| 2389 | { |
| 2390 | struct vty_parent_node *parent = vty_parent(vty); |
| 2391 | if (!parent) |
| 2392 | return false; |
| 2393 | llist_del(&parent->entry); |
| 2394 | vty->node = parent->node; |
| 2395 | vty->priv = parent->priv; |
| 2396 | if (vty->indent) |
| 2397 | talloc_free(vty->indent); |
| 2398 | vty->indent = parent->indent; |
| 2399 | talloc_free(parent); |
| 2400 | return true; |
| 2401 | } |
| 2402 | |
| 2403 | static void vty_clear_parents(struct vty *vty) |
| 2404 | { |
| 2405 | while (vty_pop_parent(vty)); |
| 2406 | } |
| 2407 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2408 | /* return parent node */ |
Jacob Erlbeck | b3657e1 | 2013-09-10 14:04:54 +0200 | [diff] [blame] | 2409 | /* |
| 2410 | * This function MUST eventually converge on a node when called repeatedly, |
| 2411 | * there must not be any cycles. |
| 2412 | * All 'config' nodes shall converge on CONFIG_NODE. |
| 2413 | * All other 'enable' nodes shall converge on ENABLE_NODE. |
| 2414 | * All 'view' only nodes shall converge on VIEW_NODE. |
| 2415 | * All other nodes shall converge on themselves or it must be ensured, |
| 2416 | * that the user's rights are not extended anyhow by calling this function. |
| 2417 | * |
| 2418 | * Note that these requirements also apply to all functions that are used |
| 2419 | * as go_parent_cb. |
| 2420 | * Note also that this function relies on the is_config_child callback to |
| 2421 | * recognize non-config nodes if go_parent_cb is not set. |
| 2422 | */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 2423 | int vty_go_parent(struct vty *vty) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2424 | { |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 2425 | switch (vty->node) { |
Vadim Yanitskiy | b0bbd72 | 2020-08-15 22:59:46 +0700 | [diff] [blame] | 2426 | case AUTH_NODE: |
| 2427 | case VIEW_NODE: |
| 2428 | case ENABLE_NODE: |
| 2429 | case CONFIG_NODE: |
| 2430 | vty_clear_parents(vty); |
| 2431 | break; |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 2432 | |
Vadim Yanitskiy | b0bbd72 | 2020-08-15 22:59:46 +0700 | [diff] [blame] | 2433 | case AUTH_ENABLE_NODE: |
| 2434 | vty->node = VIEW_NODE; |
| 2435 | vty_clear_parents(vty); |
| 2436 | break; |
Jacob Erlbeck | b3657e1 | 2013-09-10 14:04:54 +0200 | [diff] [blame] | 2437 | |
Vadim Yanitskiy | b0bbd72 | 2020-08-15 22:59:46 +0700 | [diff] [blame] | 2438 | default: |
| 2439 | if (host.app_info->go_parent_cb) |
| 2440 | host.app_info->go_parent_cb(vty); |
| 2441 | vty_pop_parent(vty); |
| 2442 | break; |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 2443 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2444 | |
| 2445 | return vty->node; |
| 2446 | } |
| 2447 | |
| 2448 | /* Execute command by argument vline vector. */ |
| 2449 | static int |
| 2450 | cmd_execute_command_real(vector vline, struct vty *vty, |
| 2451 | struct cmd_element **cmd) |
| 2452 | { |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2453 | unsigned int i, j; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2454 | unsigned int index; |
| 2455 | vector cmd_vector; |
| 2456 | struct cmd_element *cmd_element; |
| 2457 | struct cmd_element *matched_element; |
| 2458 | unsigned int matched_count, incomplete_count; |
| 2459 | int argc; |
| 2460 | const char *argv[CMD_ARGC_MAX]; |
| 2461 | enum match_type match = 0; |
| 2462 | int varflag; |
| 2463 | char *command; |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2464 | int rc; |
| 2465 | /* Used for temporary storage of cmd_deopt() allocated arguments during |
| 2466 | argv[] generation */ |
| 2467 | void *cmd_deopt_ctx = NULL; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2468 | |
| 2469 | /* Make copy of command elements. */ |
| 2470 | cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node)); |
| 2471 | |
Vadim Yanitskiy | f5781c9 | 2019-06-01 00:27:22 +0700 | [diff] [blame] | 2472 | for (index = 0; index < vector_active(vline); index++) { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2473 | if ((command = vector_slot(vline, index))) { |
| 2474 | int ret; |
| 2475 | |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 2476 | match = cmd_filter(command, cmd_vector, index, |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2477 | ANY_MATCH); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2478 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2479 | if (match == VARARG_MATCH) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2480 | break; |
| 2481 | |
| 2482 | ret = |
| 2483 | is_cmd_ambiguous(command, cmd_vector, index, match); |
| 2484 | |
| 2485 | if (ret == 1) { |
| 2486 | vector_free(cmd_vector); |
| 2487 | return CMD_ERR_AMBIGUOUS; |
| 2488 | } else if (ret == 2) { |
| 2489 | vector_free(cmd_vector); |
| 2490 | return CMD_ERR_NO_MATCH; |
| 2491 | } |
| 2492 | } |
Vadim Yanitskiy | f5781c9 | 2019-06-01 00:27:22 +0700 | [diff] [blame] | 2493 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2494 | |
| 2495 | /* Check matched count. */ |
| 2496 | matched_element = NULL; |
| 2497 | matched_count = 0; |
| 2498 | incomplete_count = 0; |
| 2499 | |
Vadim Yanitskiy | f5781c9 | 2019-06-01 00:27:22 +0700 | [diff] [blame] | 2500 | for (i = 0; i < vector_active(cmd_vector); i++) { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2501 | if ((cmd_element = vector_slot(cmd_vector, i))) { |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2502 | if (match == VARARG_MATCH |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2503 | || index >= cmd_element->cmdsize) { |
| 2504 | matched_element = cmd_element; |
| 2505 | #if 0 |
| 2506 | printf("DEBUG: %s\n", cmd_element->string); |
| 2507 | #endif |
| 2508 | matched_count++; |
| 2509 | } else { |
| 2510 | incomplete_count++; |
| 2511 | } |
| 2512 | } |
Vadim Yanitskiy | f5781c9 | 2019-06-01 00:27:22 +0700 | [diff] [blame] | 2513 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2514 | |
| 2515 | /* Finish of using cmd_vector. */ |
| 2516 | vector_free(cmd_vector); |
| 2517 | |
| 2518 | /* To execute command, matched_count must be 1. */ |
| 2519 | if (matched_count == 0) { |
| 2520 | if (incomplete_count) |
| 2521 | return CMD_ERR_INCOMPLETE; |
| 2522 | else |
| 2523 | return CMD_ERR_NO_MATCH; |
| 2524 | } |
| 2525 | |
| 2526 | if (matched_count > 1) |
| 2527 | return CMD_ERR_AMBIGUOUS; |
| 2528 | |
| 2529 | /* Argument treatment */ |
| 2530 | varflag = 0; |
| 2531 | argc = 0; |
| 2532 | |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2533 | cmd_deopt_ctx = talloc_named_const(tall_vty_cmd_ctx, 0, __func__); |
| 2534 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2535 | for (i = 0; i < vector_active(vline); i++) { |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2536 | if (argc == CMD_ARGC_MAX) { |
| 2537 | rc = CMD_ERR_EXEED_ARGC_MAX; |
| 2538 | goto rc_free_deopt_ctx; |
| 2539 | } |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2540 | if (varflag) { |
| 2541 | argv[argc++] = vector_slot(vline, i); |
| 2542 | continue; |
| 2543 | } |
| 2544 | |
| 2545 | vector descvec = vector_slot(matched_element->strvec, i); |
Pau Espin Pedrol | 32e6710 | 2019-06-12 18:35:46 +0200 | [diff] [blame] | 2546 | const char *tmp_cmd; |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2547 | |
| 2548 | if (vector_active(descvec) == 1) { |
Pau Espin Pedrol | 32e6710 | 2019-06-12 18:35:46 +0200 | [diff] [blame] | 2549 | /* Single coice argument, no "(...|...)". */ |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2550 | struct desc *desc = vector_slot(descvec, 0); |
| 2551 | |
Pau Espin Pedrol | 32e6710 | 2019-06-12 18:35:46 +0200 | [diff] [blame] | 2552 | if (CMD_OPTION(desc->cmd)) { |
| 2553 | /* we need to first remove the [] chars, then check to see what's inside (var or token) */ |
| 2554 | tmp_cmd = cmd_deopt(cmd_deopt_ctx, desc->cmd); |
| 2555 | } else { |
| 2556 | tmp_cmd = desc->cmd; |
| 2557 | } |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2558 | |
Pau Espin Pedrol | 32e6710 | 2019-06-12 18:35:46 +0200 | [diff] [blame] | 2559 | if (CMD_VARARG(tmp_cmd)) |
| 2560 | varflag = 1; |
| 2561 | if (varflag || CMD_VARIABLE(tmp_cmd)) |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2562 | argv[argc++] = vector_slot(vline, i); |
Pau Espin Pedrol | 32e6710 | 2019-06-12 18:35:46 +0200 | [diff] [blame] | 2563 | else if (CMD_OPTION(desc->cmd)) |
| 2564 | argv[argc++] = tmp_cmd; |
| 2565 | /* else : we don't want to add non-opt single-choice static args in argv[] */ |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2566 | } else { |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2567 | /* multi choice argument. look up which choice |
| 2568 | the user meant (can only be one after |
| 2569 | filtering and checking for ambigous). For instance, |
| 2570 | if user typed "th" for "(two|three)" arg, we |
| 2571 | want to pass "three" in argv[]. */ |
| 2572 | for (j = 0; j < vector_active(descvec); j++) { |
| 2573 | struct desc *desc = vector_slot(descvec, j); |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2574 | if (!desc) |
| 2575 | continue; |
| 2576 | if (cmd_match(desc->cmd, vector_slot(vline, i), ANY_MATCH, true) == NO_MATCH) |
| 2577 | continue; |
| 2578 | if (CMD_OPTION(desc->cmd)) { |
| 2579 | /* we need to first remove the [] chars, then check to see what's inside (var or token) */ |
| 2580 | tmp_cmd = cmd_deopt(cmd_deopt_ctx, desc->cmd); |
| 2581 | } else { |
| 2582 | tmp_cmd = desc->cmd; |
| 2583 | } |
| 2584 | |
| 2585 | if(CMD_VARIABLE(tmp_cmd)) { |
| 2586 | argv[argc++] = vector_slot(vline, i); |
| 2587 | } else { |
| 2588 | argv[argc++] = tmp_cmd; |
| 2589 | } |
| 2590 | break; |
| 2591 | } |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2592 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | /* For vtysh execution. */ |
| 2596 | if (cmd) |
| 2597 | *cmd = matched_element; |
| 2598 | |
| 2599 | if (matched_element->daemon) |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2600 | rc = CMD_SUCCESS_DAEMON; |
Neels Hofmeyr | d31de23 | 2019-10-31 16:09:23 +0100 | [diff] [blame] | 2601 | else { |
| 2602 | /* Execute matched command. */ |
| 2603 | struct vty_parent_node this_node = { |
| 2604 | .node = vty->node, |
| 2605 | .priv = vty->priv, |
| 2606 | .indent = vty->indent, |
| 2607 | }; |
| 2608 | struct vty_parent_node *parent = vty_parent(vty); |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2609 | rc = (*matched_element->func) (matched_element, vty, argc, argv); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2610 | |
Neels Hofmeyr | d31de23 | 2019-10-31 16:09:23 +0100 | [diff] [blame] | 2611 | /* If we have stepped down into a child node, push a parent frame. |
| 2612 | * The causality is such: we don't expect every single node entry implementation to push |
| 2613 | * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop* |
| 2614 | * a parent node. Hence if the node changed without the parent node changing, we must |
| 2615 | * have stepped into a child node. */ |
| 2616 | if (vty->node != this_node.node && parent == vty_parent(vty) |
| 2617 | && vty->node > CONFIG_NODE) { |
| 2618 | /* Push the parent node. */ |
| 2619 | parent = talloc_zero(vty, struct vty_parent_node); |
| 2620 | *parent = this_node; |
| 2621 | llist_add(&parent->entry, &vty->parent_nodes); |
| 2622 | } |
| 2623 | } |
| 2624 | |
Pau Espin Pedrol | 7e1b03f | 2019-06-12 14:47:48 +0200 | [diff] [blame] | 2625 | rc_free_deopt_ctx: |
| 2626 | /* Now after we called the command func, we can free temporary strings */ |
| 2627 | talloc_free(cmd_deopt_ctx); |
| 2628 | return rc; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | int |
| 2632 | cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd, |
| 2633 | int vtysh) |
| 2634 | { |
Neels Hofmeyr | d64b6ae | 2017-09-07 04:52:05 +0200 | [diff] [blame] | 2635 | int ret; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2636 | enum node_type onode; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2637 | |
| 2638 | onode = vty->node; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2639 | |
| 2640 | if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) { |
| 2641 | vector shifted_vline; |
| 2642 | unsigned int index; |
| 2643 | |
| 2644 | vty->node = ENABLE_NODE; |
| 2645 | /* We can try it on enable node, cos' the vty is authenticated */ |
| 2646 | |
| 2647 | shifted_vline = vector_init(vector_count(vline)); |
| 2648 | /* use memcpy? */ |
| 2649 | for (index = 1; index < vector_active(vline); index++) { |
| 2650 | vector_set_index(shifted_vline, index - 1, |
| 2651 | vector_lookup(vline, index)); |
| 2652 | } |
| 2653 | |
| 2654 | ret = cmd_execute_command_real(shifted_vline, vty, cmd); |
| 2655 | |
| 2656 | vector_free(shifted_vline); |
| 2657 | vty->node = onode; |
| 2658 | return ret; |
| 2659 | } |
| 2660 | |
Neels Hofmeyr | d64b6ae | 2017-09-07 04:52:05 +0200 | [diff] [blame] | 2661 | return cmd_execute_command_real(vline, vty, cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /* Execute command by argument readline. */ |
| 2665 | int |
| 2666 | cmd_execute_command_strict(vector vline, struct vty *vty, |
| 2667 | struct cmd_element **cmd) |
| 2668 | { |
| 2669 | unsigned int i; |
| 2670 | unsigned int index; |
| 2671 | vector cmd_vector; |
| 2672 | struct cmd_element *cmd_element; |
| 2673 | struct cmd_element *matched_element; |
| 2674 | unsigned int matched_count, incomplete_count; |
| 2675 | int argc; |
| 2676 | const char *argv[CMD_ARGC_MAX]; |
| 2677 | int varflag; |
| 2678 | enum match_type match = 0; |
| 2679 | char *command; |
| 2680 | |
| 2681 | /* Make copy of command element */ |
| 2682 | cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node)); |
| 2683 | |
| 2684 | for (index = 0; index < vector_active(vline); index++) |
| 2685 | if ((command = vector_slot(vline, index))) { |
| 2686 | int ret; |
| 2687 | |
Sylvain Munaut | 4d8eea4 | 2012-12-28 11:58:23 +0100 | [diff] [blame] | 2688 | match = cmd_filter(vector_slot(vline, index), |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2689 | cmd_vector, index, EXACT_MATCH); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2690 | |
| 2691 | /* If command meets '.VARARG' then finish matching. */ |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2692 | if (match == VARARG_MATCH) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2693 | break; |
| 2694 | |
| 2695 | ret = |
| 2696 | is_cmd_ambiguous(command, cmd_vector, index, match); |
| 2697 | if (ret == 1) { |
| 2698 | vector_free(cmd_vector); |
| 2699 | return CMD_ERR_AMBIGUOUS; |
| 2700 | } |
| 2701 | if (ret == 2) { |
| 2702 | vector_free(cmd_vector); |
| 2703 | return CMD_ERR_NO_MATCH; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | /* Check matched count. */ |
| 2708 | matched_element = NULL; |
| 2709 | matched_count = 0; |
| 2710 | incomplete_count = 0; |
| 2711 | for (i = 0; i < vector_active(cmd_vector); i++) |
| 2712 | if (vector_slot(cmd_vector, i) != NULL) { |
| 2713 | cmd_element = vector_slot(cmd_vector, i); |
| 2714 | |
Pau Espin Pedrol | de89099 | 2019-06-12 12:27:32 +0200 | [diff] [blame] | 2715 | if (match == VARARG_MATCH |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2716 | || index >= cmd_element->cmdsize) { |
| 2717 | matched_element = cmd_element; |
| 2718 | matched_count++; |
| 2719 | } else |
| 2720 | incomplete_count++; |
| 2721 | } |
| 2722 | |
| 2723 | /* Finish of using cmd_vector. */ |
| 2724 | vector_free(cmd_vector); |
| 2725 | |
| 2726 | /* To execute command, matched_count must be 1. */ |
| 2727 | if (matched_count == 0) { |
| 2728 | if (incomplete_count) |
| 2729 | return CMD_ERR_INCOMPLETE; |
| 2730 | else |
| 2731 | return CMD_ERR_NO_MATCH; |
| 2732 | } |
| 2733 | |
| 2734 | if (matched_count > 1) |
| 2735 | return CMD_ERR_AMBIGUOUS; |
| 2736 | |
| 2737 | /* Argument treatment */ |
| 2738 | varflag = 0; |
| 2739 | argc = 0; |
| 2740 | |
| 2741 | for (i = 0; i < vector_active(vline); i++) { |
Pau Espin Pedrol | c17c6d6 | 2019-06-14 12:38:42 +0200 | [diff] [blame] | 2742 | if (argc == CMD_ARGC_MAX) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2743 | return CMD_ERR_EXEED_ARGC_MAX; |
Pau Espin Pedrol | 8930ace | 2019-06-12 17:51:34 +0200 | [diff] [blame] | 2744 | if (varflag) { |
| 2745 | argv[argc++] = vector_slot(vline, i); |
| 2746 | continue; |
| 2747 | } |
| 2748 | |
| 2749 | vector descvec = vector_slot(matched_element->strvec, i); |
| 2750 | |
| 2751 | if (vector_active(descvec) == 1) { |
| 2752 | struct desc *desc = vector_slot(descvec, 0); |
| 2753 | |
| 2754 | if (CMD_VARARG(desc->cmd)) |
| 2755 | varflag = 1; |
| 2756 | |
| 2757 | if (varflag || CMD_VARIABLE(desc->cmd) |
| 2758 | || CMD_OPTION(desc->cmd)) |
| 2759 | argv[argc++] = vector_slot(vline, i); |
| 2760 | } else { |
| 2761 | argv[argc++] = vector_slot(vline, i); |
| 2762 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | /* For vtysh execution. */ |
| 2766 | if (cmd) |
| 2767 | *cmd = matched_element; |
| 2768 | |
| 2769 | if (matched_element->daemon) |
| 2770 | return CMD_SUCCESS_DAEMON; |
| 2771 | |
| 2772 | /* Now execute matched command */ |
| 2773 | return (*matched_element->func) (matched_element, vty, argc, argv); |
| 2774 | } |
| 2775 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2776 | static inline size_t len(const char *str) |
| 2777 | { |
| 2778 | return str? strlen(str) : 0; |
| 2779 | } |
| 2780 | |
Neels Hofmeyr | 00b5ed3 | 2017-09-20 00:46:03 +0200 | [diff] [blame] | 2781 | /*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a |
| 2782 | * is longer than b, a must start with exactly b, and vice versa. |
| 2783 | * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b. |
| 2784 | */ |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2785 | static int indent_cmp(const char *a, const char *b) |
| 2786 | { |
| 2787 | size_t al, bl; |
| 2788 | al = len(a); |
| 2789 | bl = len(b); |
| 2790 | if (al > bl) { |
| 2791 | if (bl && strncmp(a, b, bl) != 0) |
| 2792 | return EINVAL; |
| 2793 | return 1; |
| 2794 | } |
| 2795 | /* al <= bl */ |
| 2796 | if (al && strncmp(a, b, al) != 0) |
| 2797 | return EINVAL; |
| 2798 | return (al < bl)? -1 : 0; |
| 2799 | } |
| 2800 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2801 | /* Configration make from file. */ |
| 2802 | int config_from_file(struct vty *vty, FILE * fp) |
| 2803 | { |
| 2804 | int ret; |
| 2805 | vector vline; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2806 | char *indent; |
| 2807 | int cmp; |
| 2808 | struct vty_parent_node this_node; |
| 2809 | struct vty_parent_node *parent; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2810 | |
| 2811 | while (fgets(vty->buf, VTY_BUFSIZ, fp)) { |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2812 | indent = NULL; |
| 2813 | vline = NULL; |
| 2814 | ret = cmd_make_strvec2(vty->buf, &indent, &vline); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2815 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2816 | if (ret != CMD_SUCCESS) |
| 2817 | goto return_invalid_indent; |
| 2818 | |
| 2819 | /* In case of comment or empty line */ |
| 2820 | if (vline == NULL) { |
| 2821 | if (indent) { |
| 2822 | talloc_free(indent); |
| 2823 | indent = NULL; |
| 2824 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2825 | continue; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
Neels Hofmeyr | 4306363 | 2017-09-19 23:54:01 +0200 | [diff] [blame] | 2828 | /* We have a nonempty line. */ |
| 2829 | if (!vty->indent) { |
| 2830 | /* We have just entered a node and expecting the first child to come up; but we |
| 2831 | * may also skip right back to a parent or ancestor level. */ |
| 2832 | parent = vty_parent(vty); |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2833 | |
Neels Hofmeyr | 4306363 | 2017-09-19 23:54:01 +0200 | [diff] [blame] | 2834 | /* If there is no parent, record any indentation we encounter. */ |
| 2835 | cmp = parent ? indent_cmp(indent, parent->indent) : 1; |
| 2836 | |
| 2837 | if (cmp == EINVAL) |
| 2838 | goto return_invalid_indent; |
| 2839 | |
| 2840 | if (cmp <= 0) { |
| 2841 | /* We have gone right back to the parent level or higher, we are skipping |
| 2842 | * this child node level entirely. Pop the parent to go back to a node |
| 2843 | * that was actually there (to reinstate vty->indent) and re-use below |
| 2844 | * go-parent while-loop to find an accurate match of indent in the node |
| 2845 | * ancestry. */ |
| 2846 | vty_go_parent(vty); |
| 2847 | } else { |
| 2848 | /* The indent is deeper than the just entered parent, record the new |
| 2849 | * indentation characters. */ |
| 2850 | vty->indent = talloc_strdup(vty, indent); |
| 2851 | /* This *is* the new indentation. */ |
| 2852 | cmp = 0; |
| 2853 | } |
| 2854 | } else { |
| 2855 | /* There is a known indentation for this node level, validate and detect node |
| 2856 | * exits. */ |
| 2857 | cmp = indent_cmp(indent, vty->indent); |
| 2858 | if (cmp == EINVAL) |
| 2859 | goto return_invalid_indent; |
| 2860 | } |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2861 | |
| 2862 | /* Less indent: go up the parent nodes to find matching amount of less indent. When this |
| 2863 | * loop exits, we want to have found an exact match, i.e. cmp == 0. */ |
| 2864 | while (cmp < 0) { |
| 2865 | vty_go_parent(vty); |
| 2866 | cmp = indent_cmp(indent, vty->indent); |
| 2867 | if (cmp == EINVAL) |
| 2868 | goto return_invalid_indent; |
| 2869 | } |
| 2870 | |
| 2871 | /* More indent without having entered a child node level? Either the parent node's indent |
| 2872 | * wasn't hit exactly (e.g. there's a space more than the parent level had further above) |
| 2873 | * or the indentation increased even though the vty command didn't enter a child. */ |
| 2874 | if (cmp > 0) |
| 2875 | goto return_invalid_indent; |
| 2876 | |
| 2877 | /* Remember the current node before the command possibly changes it. */ |
| 2878 | this_node = (struct vty_parent_node){ |
| 2879 | .node = vty->node, |
| 2880 | .priv = vty->priv, |
| 2881 | .indent = vty->indent, |
| 2882 | }; |
| 2883 | |
| 2884 | parent = vty_parent(vty); |
| 2885 | ret = cmd_execute_command_strict(vline, vty, NULL); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2886 | cmd_free_strvec(vline); |
| 2887 | |
Pau Espin Pedrol | a0c8195 | 2019-10-22 18:38:01 +0200 | [diff] [blame] | 2888 | if (ret != CMD_SUCCESS && ret != CMD_ERR_NOTHING_TODO) { |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2889 | if (indent) { |
| 2890 | talloc_free(indent); |
| 2891 | indent = NULL; |
| 2892 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2893 | return ret; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | /* If we have stepped down into a child node, push a parent frame. |
| 2897 | * The causality is such: we don't expect every single node entry implementation to push |
| 2898 | * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop* |
| 2899 | * a parent node. Hence if the node changed without the parent node changing, we must |
| 2900 | * have stepped into a child node (and now expect a deeper indent). */ |
| 2901 | if (vty->node != this_node.node && parent == vty_parent(vty)) { |
| 2902 | /* Push the parent node. */ |
| 2903 | parent = talloc_zero(vty, struct vty_parent_node); |
| 2904 | *parent = this_node; |
| 2905 | llist_add(&parent->entry, &vty->parent_nodes); |
| 2906 | |
| 2907 | /* The current talloc'ed vty->indent string will now be owned by this parent |
| 2908 | * struct. Indicate that we don't know what deeper indent characters the user |
| 2909 | * will choose. */ |
| 2910 | vty->indent = NULL; |
| 2911 | } |
| 2912 | |
| 2913 | if (indent) { |
| 2914 | talloc_free(indent); |
| 2915 | indent = NULL; |
| 2916 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2917 | } |
Pau Espin Pedrol | 0fd0fe6 | 2019-10-11 14:10:31 +0200 | [diff] [blame] | 2918 | /* Make sure we call go_parent_cb for all remaining indent levels at the end of file */ |
| 2919 | while (vty_parent(vty)) |
| 2920 | vty_go_parent(vty); |
| 2921 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2922 | return CMD_SUCCESS; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 2923 | |
| 2924 | return_invalid_indent: |
| 2925 | if (vline) |
| 2926 | cmd_free_strvec(vline); |
| 2927 | if (indent) { |
| 2928 | talloc_free(indent); |
| 2929 | indent = NULL; |
| 2930 | } |
| 2931 | return CMD_ERR_INVALID_INDENT; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | /* Configration from terminal */ |
| 2935 | DEFUN(config_terminal, |
| 2936 | config_terminal_cmd, |
| 2937 | "configure terminal", |
| 2938 | "Configuration from vty interface\n" "Configuration terminal\n") |
| 2939 | { |
| 2940 | if (vty_config_lock(vty)) |
| 2941 | vty->node = CONFIG_NODE; |
| 2942 | else { |
| 2943 | vty_out(vty, "VTY configuration is locked by other VTY%s", |
| 2944 | VTY_NEWLINE); |
| 2945 | return CMD_WARNING; |
| 2946 | } |
| 2947 | return CMD_SUCCESS; |
| 2948 | } |
| 2949 | |
| 2950 | /* Enable command */ |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 2951 | DEFUN(enable, config_enable_cmd, |
| 2952 | "enable [expert-mode]", |
| 2953 | "Turn on privileged mode command\n" |
| 2954 | "Enable the expert mode (show hidden commands)\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2955 | { |
| 2956 | /* If enable password is NULL, change to ENABLE_NODE */ |
| 2957 | if ((host.enable == NULL && host.enable_encrypt == NULL) || |
| 2958 | vty->type == VTY_SHELL_SERV) |
| 2959 | vty->node = ENABLE_NODE; |
| 2960 | else |
| 2961 | vty->node = AUTH_ENABLE_NODE; |
| 2962 | |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 2963 | vty->expert_mode = argc > 0; |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 2964 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2965 | return CMD_SUCCESS; |
| 2966 | } |
| 2967 | |
| 2968 | /* Disable command */ |
| 2969 | DEFUN(disable, |
| 2970 | config_disable_cmd, "disable", "Turn off privileged mode command\n") |
| 2971 | { |
| 2972 | if (vty->node == ENABLE_NODE) |
| 2973 | vty->node = VIEW_NODE; |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 2974 | |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 2975 | vty->expert_mode = false; |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 2976 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2977 | return CMD_SUCCESS; |
| 2978 | } |
| 2979 | |
| 2980 | /* Down vty node level. */ |
| 2981 | gDEFUN(config_exit, |
| 2982 | config_exit_cmd, "exit", "Exit current mode and down to previous mode\n") |
| 2983 | { |
| 2984 | switch (vty->node) { |
Jacob Erlbeck | b3657e1 | 2013-09-10 14:04:54 +0200 | [diff] [blame] | 2985 | case AUTH_NODE: |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2986 | case VIEW_NODE: |
| 2987 | case ENABLE_NODE: |
Harald Welte | a99d45a | 2015-11-12 13:48:23 +0100 | [diff] [blame] | 2988 | vty->status = VTY_CLOSE; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2989 | break; |
| 2990 | case CONFIG_NODE: |
| 2991 | vty->node = ENABLE_NODE; |
| 2992 | vty_config_unlock(vty); |
| 2993 | break; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2994 | default: |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 2995 | if (vty->node > CONFIG_NODE) |
| 2996 | vty_go_parent (vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2997 | break; |
| 2998 | } |
| 2999 | return CMD_SUCCESS; |
| 3000 | } |
| 3001 | |
| 3002 | /* End of configuration. */ |
| 3003 | gDEFUN(config_end, |
| 3004 | config_end_cmd, "end", "End current mode and change to enable mode.") |
| 3005 | { |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 3006 | if (vty->node > ENABLE_NODE) { |
Jacob Erlbeck | 2349721 | 2013-09-10 09:07:31 +0200 | [diff] [blame] | 3007 | int last_node = CONFIG_NODE; |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 3008 | |
| 3009 | /* Repeatedly call go_parent until a top node is reached. */ |
| 3010 | while (vty->node > CONFIG_NODE) { |
| 3011 | if (vty->node == last_node) { |
| 3012 | /* Ensure termination, this shouldn't happen. */ |
| 3013 | break; |
| 3014 | } |
| 3015 | last_node = vty->node; |
| 3016 | vty_go_parent(vty); |
| 3017 | } |
| 3018 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3019 | vty_config_unlock(vty); |
Jacob Erlbeck | 7eed053 | 2013-09-06 16:51:59 +0200 | [diff] [blame] | 3020 | if (vty->node > ENABLE_NODE) |
| 3021 | vty->node = ENABLE_NODE; |
| 3022 | vty->index = NULL; |
| 3023 | vty->index_sub = NULL; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3024 | } |
| 3025 | return CMD_SUCCESS; |
| 3026 | } |
| 3027 | |
Oliver Smith | 8a482fd | 2021-07-12 18:18:51 +0200 | [diff] [blame] | 3028 | DEFUN(shutdown, |
| 3029 | shutdown_cmd, "shutdown", "Request a shutdown of the program\n") |
| 3030 | { |
| 3031 | LOGP(DLGLOBAL, LOGL_INFO, "Shutdown requested from telnet\n"); |
| 3032 | vty_out(vty, "%s is shutting down. Bye!%s", host.app_info->name, VTY_NEWLINE); |
| 3033 | |
| 3034 | /* Same exit path as if it was killed by the service manager */ |
| 3035 | kill(getpid(), SIGTERM); |
| 3036 | |
| 3037 | return CMD_SUCCESS; |
| 3038 | } |
| 3039 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3040 | /* Show version. */ |
| 3041 | DEFUN(show_version, |
| 3042 | show_version_cmd, "show version", SHOW_STR "Displays program version\n") |
| 3043 | { |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 3044 | vty_out(vty, "%s %s (%s).%s", host.app_info->name, |
| 3045 | host.app_info->version, |
| 3046 | host.app_info->name ? host.app_info->name : "", VTY_NEWLINE); |
| 3047 | vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3048 | |
| 3049 | return CMD_SUCCESS; |
| 3050 | } |
| 3051 | |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 3052 | DEFUN(show_online_help, |
| 3053 | show_online_help_cmd, "show online-help", SHOW_STR "Online help\n") |
| 3054 | { |
Neels Hofmeyr | cf70aa0 | 2020-05-14 16:51:26 +0200 | [diff] [blame] | 3055 | vty_dump_xml_ref_to_vty(vty); |
Holger Hans Peter Freyther | 8297c81 | 2011-11-18 23:14:24 +0100 | [diff] [blame] | 3056 | return CMD_SUCCESS; |
| 3057 | } |
| 3058 | |
Oliver Smith | 0c78bc6 | 2021-07-12 17:28:36 +0200 | [diff] [blame] | 3059 | DEFUN(show_pid, |
| 3060 | show_pid_cmd, "show pid", SHOW_STR "Displays the process ID\n") |
| 3061 | { |
| 3062 | vty_out(vty, "%d%s", getpid(), VTY_NEWLINE); |
| 3063 | return CMD_SUCCESS; |
| 3064 | } |
| 3065 | |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 3066 | DEFUN(show_uptime, |
| 3067 | show_uptime_cmd, "show uptime", SHOW_STR "Displays how long the program has been running\n") |
| 3068 | { |
| 3069 | struct timespec now; |
Oliver Smith | 95a03e5 | 2021-07-19 09:00:01 +0200 | [diff] [blame] | 3070 | struct timespec uptime; |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 3071 | |
Oliver Smith | 95a03e5 | 2021-07-19 09:00:01 +0200 | [diff] [blame] | 3072 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 3073 | timespecsub(&now, &starttime, &uptime); |
| 3074 | |
| 3075 | int d = uptime.tv_sec / (3600 * 24); |
| 3076 | int h = uptime.tv_sec / 3600 % 24; |
| 3077 | int m = uptime.tv_sec / 60 % 60; |
| 3078 | int s = uptime.tv_sec % 60; |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 3079 | |
| 3080 | vty_out(vty, "%s has been running for %dd %dh %dm %ds%s", host.app_info->name, d, h, m, s, VTY_NEWLINE); |
| 3081 | |
| 3082 | return CMD_SUCCESS; |
| 3083 | } |
| 3084 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3085 | /* Help display function for all node. */ |
| 3086 | gDEFUN(config_help, |
| 3087 | config_help_cmd, "help", "Description of the interactive help system\n") |
| 3088 | { |
Vadim Yanitskiy | 1d7ae15 | 2020-08-12 16:52:35 +0700 | [diff] [blame] | 3089 | vty_out(vty, "This VTY provides advanced help features. When you need help,%s" |
| 3090 | "anytime at the command line please press '?'.%s%s" |
| 3091 | "If nothing matches, the help list will be empty and you must backup%s" |
| 3092 | " until entering a '?' shows the available options.%s" |
| 3093 | "Two styles of help are provided:%s" |
| 3094 | "1. Full help is available when you are ready to enter a%s" |
| 3095 | "command argument (e.g. 'show ?') and describes each possible%s" |
| 3096 | "argument.%s" |
| 3097 | "2. Partial help is provided when an abbreviated argument is entered%s" |
| 3098 | " and you want to know what arguments match the input%s" |
| 3099 | " (e.g. 'show me?'.)%s%s", |
| 3100 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 3101 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 3102 | VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, |
| 3103 | VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3104 | return CMD_SUCCESS; |
| 3105 | } |
| 3106 | |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 3107 | enum { |
| 3108 | ATTR_TYPE_GLOBAL = (1 << 0), |
| 3109 | ATTR_TYPE_LIB = (1 << 1), |
| 3110 | ATTR_TYPE_APP = (1 << 2), |
| 3111 | }; |
| 3112 | |
| 3113 | static void print_attr_list(struct vty *vty, unsigned int attr_mask) |
| 3114 | { |
| 3115 | const char *desc; |
| 3116 | unsigned int i; |
| 3117 | bool found; |
| 3118 | char flag; |
| 3119 | |
| 3120 | if (attr_mask & ATTR_TYPE_GLOBAL) { |
| 3121 | vty_out(vty, " Global attributes:%s", VTY_NEWLINE); |
| 3122 | |
| 3123 | for (i = 0; i < ARRAY_SIZE(cmd_attr_desc) - 1; i++) { |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3124 | flag = cmd_attr_get_flag(cmd_attr_desc[i].value); |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 3125 | desc = cmd_attr_desc[i].str; |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3126 | |
| 3127 | /* Skip attributes without flags */ |
| 3128 | if (flag != '.') |
| 3129 | vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE); |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (attr_mask & ATTR_TYPE_LIB) { |
| 3134 | vty_out(vty, " Library specific attributes:%s", VTY_NEWLINE); |
| 3135 | |
| 3136 | for (i = 0, found = false; i < _OSMO_CORE_LIB_ATTR_COUNT; i++) { |
| 3137 | if ((desc = cmd_lib_attr_desc[i]) == NULL) |
| 3138 | continue; |
| 3139 | found = true; |
| 3140 | |
| 3141 | flag = cmd_lib_attr_letters[i]; |
| 3142 | if (flag == '\0') |
| 3143 | flag = '.'; |
| 3144 | |
| 3145 | vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE); |
| 3146 | } |
| 3147 | |
| 3148 | if (!found) |
| 3149 | vty_out(vty, " (no attributes)%s", VTY_NEWLINE); |
| 3150 | } |
| 3151 | |
| 3152 | if (attr_mask & ATTR_TYPE_APP) { |
| 3153 | vty_out(vty, " Application specific attributes:%s", VTY_NEWLINE); |
| 3154 | |
| 3155 | for (i = 0, found = false; i < VTY_CMD_USR_ATTR_NUM; i++) { |
| 3156 | if ((desc = host.app_info->usr_attr_desc[i]) == NULL) |
| 3157 | continue; |
| 3158 | found = true; |
| 3159 | |
| 3160 | flag = host.app_info->usr_attr_letters[i]; |
| 3161 | if (flag == '\0') |
| 3162 | flag = '.'; |
| 3163 | |
| 3164 | vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE); |
| 3165 | } |
| 3166 | |
| 3167 | if (!found) |
| 3168 | vty_out(vty, " (no attributes)%s", VTY_NEWLINE); |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | gDEFUN(show_vty_attr_all, show_vty_attr_all_cmd, |
| 3173 | "show vty-attributes", |
| 3174 | SHOW_STR "List of VTY attributes\n") |
| 3175 | { |
| 3176 | print_attr_list(vty, 0xff); |
| 3177 | return CMD_SUCCESS; |
| 3178 | } |
| 3179 | |
| 3180 | gDEFUN(show_vty_attr, show_vty_attr_cmd, |
| 3181 | "show vty-attributes (application|library|global)", |
| 3182 | SHOW_STR "List of VTY attributes\n" |
| 3183 | "Application specific attributes only\n" |
| 3184 | "Library specific attributes only\n" |
| 3185 | "Global attributes only\n") |
| 3186 | { |
| 3187 | unsigned int attr_mask = 0; |
| 3188 | |
| 3189 | if (argv[0][0] == 'g') /* global */ |
| 3190 | attr_mask |= ATTR_TYPE_GLOBAL; |
| 3191 | else if (argv[0][0] == 'l') /* library */ |
| 3192 | attr_mask |= ATTR_TYPE_LIB; |
| 3193 | else if (argv[0][0] == 'a') /* application */ |
| 3194 | attr_mask |= ATTR_TYPE_APP; |
| 3195 | |
| 3196 | print_attr_list(vty, attr_mask); |
| 3197 | return CMD_SUCCESS; |
| 3198 | } |
| 3199 | |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3200 | /* Compose flag bit-mask for all commands within the given node */ |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 3201 | static unsigned int node_flag_mask(const struct cmd_node *cnode, bool expert_mode) |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3202 | { |
| 3203 | unsigned int flag_mask = 0x00; |
| 3204 | unsigned int f, i; |
| 3205 | |
| 3206 | for (f = 0; f < VTY_CMD_USR_ATTR_NUM; f++) { |
| 3207 | for (i = 0; i < vector_active(cnode->cmd_vector); i++) { |
| 3208 | const struct cmd_element *cmd; |
| 3209 | char flag_letter; |
| 3210 | |
| 3211 | if ((cmd = vector_slot(cnode->cmd_vector, i)) == NULL) |
| 3212 | continue; |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 3213 | if (cmd->attr & CMD_ATTR_DEPRECATED) |
| 3214 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 3215 | if (!expert_mode && (cmd->attr & CMD_ATTR_HIDDEN)) |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3216 | continue; |
Pau Espin Pedrol | 715a612 | 2020-10-12 15:47:05 +0200 | [diff] [blame] | 3217 | if (~cmd->usrattr & ((unsigned)1 << f)) |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3218 | continue; |
| 3219 | |
| 3220 | if (cmd->attr & CMD_ATTR_LIB_COMMAND) |
| 3221 | flag_letter = cmd_lib_attr_letters[f]; |
| 3222 | else |
| 3223 | flag_letter = host.app_info->usr_attr_letters[f]; |
| 3224 | |
| 3225 | if (flag_letter == '\0') |
| 3226 | continue; |
| 3227 | |
| 3228 | flag_mask |= (1 << f); |
| 3229 | break; |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | return flag_mask; |
| 3234 | } |
| 3235 | |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3236 | /* Compose global flag char-mask for the given command (e.g. "!" or "@") */ |
| 3237 | static const char *cmd_gflag_mask(const struct cmd_element *cmd) |
| 3238 | { |
| 3239 | static char char_mask[8 + 1]; |
| 3240 | char *ptr = &char_mask[0]; |
| 3241 | |
| 3242 | /* Mutually exclusive global attributes */ |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 3243 | if (cmd->attr & CMD_ATTR_HIDDEN) |
| 3244 | *(ptr++) = cmd_attr_get_flag(CMD_ATTR_HIDDEN); |
| 3245 | else if (cmd->attr & CMD_ATTR_IMMEDIATE) |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3246 | *(ptr++) = cmd_attr_get_flag(CMD_ATTR_IMMEDIATE); |
| 3247 | else if (cmd->attr & CMD_ATTR_NODE_EXIT) |
| 3248 | *(ptr++) = cmd_attr_get_flag(CMD_ATTR_NODE_EXIT); |
| 3249 | else |
| 3250 | *(ptr++) = '.'; |
| 3251 | |
| 3252 | *ptr = '\0'; |
| 3253 | |
| 3254 | return char_mask; |
| 3255 | } |
| 3256 | |
| 3257 | /* Compose app / lib flag char-mask for the given command (e.g. ".F.OB..") */ |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3258 | static const char *cmd_flag_mask(const struct cmd_element *cmd, |
| 3259 | unsigned int flag_mask) |
| 3260 | { |
| 3261 | static char char_mask[VTY_CMD_USR_ATTR_NUM + 1]; |
| 3262 | char *ptr = &char_mask[0]; |
| 3263 | char flag_letter; |
| 3264 | unsigned int f; |
| 3265 | |
| 3266 | for (f = 0; f < VTY_CMD_USR_ATTR_NUM; f++) { |
Pau Espin Pedrol | 715a612 | 2020-10-12 15:47:05 +0200 | [diff] [blame] | 3267 | if (~flag_mask & ((unsigned)1 << f)) |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3268 | continue; |
Pau Espin Pedrol | 715a612 | 2020-10-12 15:47:05 +0200 | [diff] [blame] | 3269 | if (~cmd->usrattr & ((unsigned)1 << f)) { |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3270 | *(ptr++) = '.'; |
| 3271 | continue; |
| 3272 | } |
| 3273 | |
| 3274 | if (cmd->attr & CMD_ATTR_LIB_COMMAND) |
| 3275 | flag_letter = cmd_lib_attr_letters[f]; |
| 3276 | else |
| 3277 | flag_letter = host.app_info->usr_attr_letters[f]; |
| 3278 | |
| 3279 | *(ptr++) = flag_letter ? flag_letter : '.'; |
| 3280 | } |
| 3281 | |
| 3282 | *ptr = '\0'; |
| 3283 | |
| 3284 | return char_mask; |
| 3285 | } |
| 3286 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3287 | /* Help display function for all node. */ |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3288 | gDEFUN(config_list, config_list_cmd, |
| 3289 | "list [with-flags]", |
| 3290 | "Print command list\n" |
| 3291 | "Also print the VTY attribute flags\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3292 | { |
| 3293 | unsigned int i; |
| 3294 | struct cmd_node *cnode = vector_slot(cmdvec, vty->node); |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3295 | unsigned int flag_mask = 0x00; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3296 | struct cmd_element *cmd; |
| 3297 | |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3298 | if (argc > 0) |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 3299 | flag_mask = node_flag_mask(cnode, vty->expert_mode); |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3300 | |
Vadim Yanitskiy | 6f50e21 | 2020-08-12 17:32:18 +0700 | [diff] [blame] | 3301 | for (i = 0; i < vector_active(cnode->cmd_vector); i++) { |
| 3302 | if ((cmd = vector_slot(cnode->cmd_vector, i)) == NULL) |
| 3303 | continue; |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 3304 | if (cmd->attr & CMD_ATTR_DEPRECATED) |
| 3305 | continue; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 3306 | if (!vty->expert_mode && (cmd->attr & CMD_ATTR_HIDDEN)) |
Vadim Yanitskiy | 6f50e21 | 2020-08-12 17:32:18 +0700 | [diff] [blame] | 3307 | continue; |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3308 | if (!argc) |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3309 | vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE); |
| 3310 | else { |
Vadim Yanitskiy | 7e1a78f | 2020-10-07 13:13:07 +0700 | [diff] [blame] | 3311 | vty_out(vty, " %s %s %s%s", |
| 3312 | cmd_gflag_mask(cmd), |
Vadim Yanitskiy | 75fec3a | 2020-10-06 00:13:44 +0700 | [diff] [blame] | 3313 | cmd_flag_mask(cmd, flag_mask), |
| 3314 | cmd->string, VTY_NEWLINE); |
| 3315 | } |
Vadim Yanitskiy | 6f50e21 | 2020-08-12 17:32:18 +0700 | [diff] [blame] | 3316 | } |
| 3317 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3318 | return CMD_SUCCESS; |
| 3319 | } |
| 3320 | |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3321 | static int write_config_file(const char *config_file, char **outpath) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3322 | { |
| 3323 | unsigned int i; |
| 3324 | int fd; |
| 3325 | struct cmd_node *node; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3326 | char *config_file_tmp = NULL; |
| 3327 | char *config_file_sav = NULL; |
| 3328 | struct vty *file_vty; |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3329 | struct stat st; |
| 3330 | |
| 3331 | *outpath = NULL; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3332 | |
Neels Hofmeyr | d65f3ea | 2018-03-28 16:12:45 +0200 | [diff] [blame] | 3333 | /* The string composition code here would be a case for talloc_asprintf(), but the pseudotalloc.c |
| 3334 | * talloc_asprintf() implementation would truncate a too-long path with "[...]", so doing it |
| 3335 | * manually instead. */ |
| 3336 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3337 | /* Check and see if we are operating under vtysh configuration */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3338 | config_file_sav = |
| 3339 | _talloc_zero(tall_vty_cmd_ctx, |
| 3340 | strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1, |
| 3341 | "config_file_sav"); |
Neels Hofmeyr | d65f3ea | 2018-03-28 16:12:45 +0200 | [diff] [blame] | 3342 | if (!config_file_sav) |
| 3343 | return -1; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3344 | strcpy(config_file_sav, config_file); |
| 3345 | strcat(config_file_sav, CONF_BACKUP_EXT); |
| 3346 | |
| 3347 | config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8, |
Neels Hofmeyr | d65f3ea | 2018-03-28 16:12:45 +0200 | [diff] [blame] | 3348 | "config_file_tmp"); |
| 3349 | if (!config_file_tmp) { |
| 3350 | talloc_free(config_file_sav); |
| 3351 | return -1; |
| 3352 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3353 | sprintf(config_file_tmp, "%s.XXXXXX", config_file); |
| 3354 | |
| 3355 | /* Open file to configuration write. */ |
| 3356 | fd = mkstemp(config_file_tmp); |
| 3357 | if (fd < 0) { |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3358 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3359 | talloc_free(config_file_tmp); |
| 3360 | talloc_free(config_file_sav); |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3361 | return -1; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* Make vty for configuration file. */ |
| 3365 | file_vty = vty_new(); |
| 3366 | file_vty->fd = fd; |
| 3367 | file_vty->type = VTY_FILE; |
| 3368 | |
| 3369 | /* Config file header print. */ |
| 3370 | vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!", |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 3371 | host.app_info->name, host.app_info->version); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3372 | //vty_time_print (file_vty, 1); |
| 3373 | vty_out(file_vty, "!\n"); |
| 3374 | |
| 3375 | for (i = 0; i < vector_active(cmdvec); i++) |
| 3376 | if ((node = vector_slot(cmdvec, i)) && node->func) { |
| 3377 | if ((*node->func) (file_vty)) |
| 3378 | vty_out(file_vty, "!\n"); |
| 3379 | } |
| 3380 | vty_close(file_vty); |
| 3381 | |
| 3382 | if (unlink(config_file_sav) != 0) |
| 3383 | if (errno != ENOENT) { |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3384 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3385 | talloc_free(config_file_sav); |
| 3386 | talloc_free(config_file_tmp); |
| 3387 | unlink(config_file_tmp); |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3388 | return -2; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3389 | } |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3390 | |
| 3391 | /* Only link the .sav file if the original file exists */ |
| 3392 | if (stat(config_file, &st) == 0) { |
| 3393 | if (link(config_file, config_file_sav) != 0) { |
| 3394 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav); |
| 3395 | talloc_free(config_file_sav); |
| 3396 | talloc_free(config_file_tmp); |
| 3397 | unlink(config_file_tmp); |
| 3398 | return -3; |
| 3399 | } |
| 3400 | sync(); |
| 3401 | if (unlink(config_file) != 0) { |
| 3402 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file); |
| 3403 | talloc_free(config_file_sav); |
| 3404 | talloc_free(config_file_tmp); |
| 3405 | unlink(config_file_tmp); |
| 3406 | return -4; |
| 3407 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3408 | } |
| 3409 | if (link(config_file_tmp, config_file) != 0) { |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3410 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3411 | talloc_free(config_file_sav); |
| 3412 | talloc_free(config_file_tmp); |
| 3413 | unlink(config_file_tmp); |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3414 | return -5; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3415 | } |
| 3416 | unlink(config_file_tmp); |
| 3417 | sync(); |
| 3418 | |
| 3419 | talloc_free(config_file_sav); |
| 3420 | talloc_free(config_file_tmp); |
| 3421 | |
| 3422 | if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) { |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3423 | *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file); |
| 3424 | return -6; |
| 3425 | } |
| 3426 | |
| 3427 | return 0; |
| 3428 | } |
| 3429 | |
| 3430 | |
| 3431 | /* Write current configuration into file. */ |
| 3432 | DEFUN(config_write_file, |
| 3433 | config_write_file_cmd, |
Pau Espin Pedrol | eda8b7b | 2019-09-26 19:30:47 +0200 | [diff] [blame] | 3434 | "write file [PATH]", |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3435 | "Write running configuration to memory, network, or terminal\n" |
Pau Espin Pedrol | eda8b7b | 2019-09-26 19:30:47 +0200 | [diff] [blame] | 3436 | "Write to configuration file\n" |
| 3437 | "Set file path to store the config, or replace if already exists\n") |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3438 | { |
| 3439 | char *failed_file; |
| 3440 | int rc; |
| 3441 | |
Holger Hans Peter Freyther | 9f0f978 | 2014-11-21 10:40:07 +0100 | [diff] [blame] | 3442 | if (host.app_info->config_is_consistent) { |
| 3443 | rc = host.app_info->config_is_consistent(vty); |
| 3444 | if (!rc) { |
| 3445 | vty_out(vty, "Configuration is not consistent%s", |
| 3446 | VTY_NEWLINE); |
| 3447 | return CMD_WARNING; |
| 3448 | } |
| 3449 | } |
| 3450 | |
Pau Espin Pedrol | eda8b7b | 2019-09-26 19:30:47 +0200 | [diff] [blame] | 3451 | if (argc == 1) |
| 3452 | host_config_set(argv[0]); |
| 3453 | |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3454 | if (host.config == NULL) { |
| 3455 | vty_out(vty, "Can't save to configuration file, using vtysh.%s", |
| 3456 | VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3457 | return CMD_WARNING; |
| 3458 | } |
| 3459 | |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 3460 | rc = write_config_file(host.config, &failed_file); |
| 3461 | switch (rc) { |
| 3462 | case -1: |
| 3463 | vty_out(vty, "Can't open configuration file %s.%s", |
| 3464 | failed_file, VTY_NEWLINE); |
| 3465 | rc = CMD_WARNING; |
| 3466 | break; |
| 3467 | case -2: |
| 3468 | vty_out(vty, "Can't unlink backup configuration file %s.%s", |
| 3469 | failed_file, VTY_NEWLINE); |
| 3470 | rc = CMD_WARNING; |
| 3471 | break; |
| 3472 | case -3: |
| 3473 | vty_out(vty, "Can't backup old configuration file %s.%s", |
| 3474 | failed_file, VTY_NEWLINE); |
| 3475 | rc = CMD_WARNING; |
| 3476 | break; |
| 3477 | case -4: |
| 3478 | vty_out(vty, "Can't unlink configuration file %s.%s", |
| 3479 | failed_file, VTY_NEWLINE); |
| 3480 | rc = CMD_WARNING; |
| 3481 | break; |
| 3482 | case -5: |
| 3483 | vty_out(vty, "Can't save configuration file %s.%s", failed_file, |
| 3484 | VTY_NEWLINE); |
| 3485 | rc = CMD_WARNING; |
| 3486 | break; |
| 3487 | case -6: |
| 3488 | vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s", |
| 3489 | failed_file, strerror(errno), errno, VTY_NEWLINE); |
| 3490 | rc = CMD_WARNING; |
| 3491 | break; |
| 3492 | default: |
| 3493 | vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE); |
| 3494 | rc = CMD_SUCCESS; |
| 3495 | break; |
| 3496 | } |
| 3497 | |
| 3498 | talloc_free(failed_file); |
| 3499 | return rc; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | ALIAS(config_write_file, |
| 3503 | config_write_cmd, |
| 3504 | "write", "Write running configuration to memory, network, or terminal\n") |
| 3505 | |
| 3506 | ALIAS(config_write_file, |
| 3507 | config_write_memory_cmd, |
| 3508 | "write memory", |
| 3509 | "Write running configuration to memory, network, or terminal\n" |
| 3510 | "Write configuration to the file (same as write file)\n") |
| 3511 | |
| 3512 | ALIAS(config_write_file, |
| 3513 | copy_runningconfig_startupconfig_cmd, |
| 3514 | "copy running-config startup-config", |
| 3515 | "Copy configuration\n" |
| 3516 | "Copy running config to... \n" |
| 3517 | "Copy running config to startup config (same as write file)\n") |
| 3518 | |
| 3519 | /* Write current configuration into the terminal. */ |
| 3520 | DEFUN(config_write_terminal, |
| 3521 | config_write_terminal_cmd, |
| 3522 | "write terminal", |
| 3523 | "Write running configuration to memory, network, or terminal\n" |
| 3524 | "Write to terminal\n") |
| 3525 | { |
| 3526 | unsigned int i; |
| 3527 | struct cmd_node *node; |
| 3528 | |
| 3529 | if (vty->type == VTY_SHELL_SERV) { |
| 3530 | for (i = 0; i < vector_active(cmdvec); i++) |
| 3531 | if ((node = vector_slot(cmdvec, i)) && node->func |
| 3532 | && node->vtysh) { |
| 3533 | if ((*node->func) (vty)) |
| 3534 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 3535 | } |
| 3536 | } else { |
| 3537 | vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE, |
| 3538 | VTY_NEWLINE); |
| 3539 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 3540 | |
| 3541 | for (i = 0; i < vector_active(cmdvec); i++) |
| 3542 | if ((node = vector_slot(cmdvec, i)) && node->func) { |
| 3543 | if ((*node->func) (vty)) |
| 3544 | vty_out(vty, "!%s", VTY_NEWLINE); |
| 3545 | } |
| 3546 | vty_out(vty, "end%s", VTY_NEWLINE); |
| 3547 | } |
| 3548 | return CMD_SUCCESS; |
| 3549 | } |
| 3550 | |
| 3551 | /* Write current configuration into the terminal. */ |
| 3552 | ALIAS(config_write_terminal, |
| 3553 | show_running_config_cmd, |
| 3554 | "show running-config", SHOW_STR "running configuration\n") |
| 3555 | |
| 3556 | /* Write startup configuration into the terminal. */ |
| 3557 | DEFUN(show_startup_config, |
| 3558 | show_startup_config_cmd, |
| 3559 | "show startup-config", SHOW_STR "Contentes of startup configuration\n") |
| 3560 | { |
| 3561 | char buf[BUFSIZ]; |
| 3562 | FILE *confp; |
| 3563 | |
| 3564 | confp = fopen(host.config, "r"); |
| 3565 | if (confp == NULL) { |
| 3566 | vty_out(vty, "Can't open configuration file [%s]%s", |
| 3567 | host.config, VTY_NEWLINE); |
| 3568 | return CMD_WARNING; |
| 3569 | } |
| 3570 | |
| 3571 | while (fgets(buf, BUFSIZ, confp)) { |
| 3572 | char *cp = buf; |
| 3573 | |
| 3574 | while (*cp != '\r' && *cp != '\n' && *cp != '\0') |
| 3575 | cp++; |
| 3576 | *cp = '\0'; |
| 3577 | |
| 3578 | vty_out(vty, "%s%s", buf, VTY_NEWLINE); |
| 3579 | } |
| 3580 | |
| 3581 | fclose(confp); |
| 3582 | |
| 3583 | return CMD_SUCCESS; |
| 3584 | } |
| 3585 | |
| 3586 | /* Hostname configuration */ |
| 3587 | DEFUN(config_hostname, |
| 3588 | hostname_cmd, |
| 3589 | "hostname WORD", |
| 3590 | "Set system's network name\n" "This system's network name\n") |
| 3591 | { |
| 3592 | if (!isalpha((int)*argv[0])) { |
| 3593 | vty_out(vty, "Please specify string starting with alphabet%s", |
| 3594 | VTY_NEWLINE); |
| 3595 | return CMD_WARNING; |
| 3596 | } |
| 3597 | |
| 3598 | if (host.name) |
| 3599 | talloc_free(host.name); |
| 3600 | |
| 3601 | host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
| 3602 | return CMD_SUCCESS; |
| 3603 | } |
| 3604 | |
| 3605 | DEFUN(config_no_hostname, |
| 3606 | no_hostname_cmd, |
| 3607 | "no hostname [HOSTNAME]", |
| 3608 | NO_STR "Reset system's network name\n" "Host name of this router\n") |
| 3609 | { |
| 3610 | if (host.name) |
| 3611 | talloc_free(host.name); |
| 3612 | host.name = NULL; |
| 3613 | return CMD_SUCCESS; |
| 3614 | } |
| 3615 | |
| 3616 | /* VTY interface password set. */ |
| 3617 | DEFUN(config_password, password_cmd, |
| 3618 | "password (8|) WORD", |
| 3619 | "Assign the terminal connection password\n" |
| 3620 | "Specifies a HIDDEN password will follow\n" |
| 3621 | "dummy string \n" "The HIDDEN line password string\n") |
| 3622 | { |
| 3623 | /* Argument check. */ |
| 3624 | if (argc == 0) { |
| 3625 | vty_out(vty, "Please specify password.%s", VTY_NEWLINE); |
| 3626 | return CMD_WARNING; |
| 3627 | } |
| 3628 | |
| 3629 | if (argc == 2) { |
| 3630 | if (*argv[0] == '8') { |
| 3631 | if (host.password) |
| 3632 | talloc_free(host.password); |
| 3633 | host.password = NULL; |
| 3634 | if (host.password_encrypt) |
| 3635 | talloc_free(host.password_encrypt); |
| 3636 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]); |
| 3637 | return CMD_SUCCESS; |
| 3638 | } else { |
| 3639 | vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3640 | return CMD_WARNING; |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | if (!isalnum((int)*argv[0])) { |
| 3645 | vty_out(vty, |
| 3646 | "Please specify string starting with alphanumeric%s", |
| 3647 | VTY_NEWLINE); |
| 3648 | return CMD_WARNING; |
| 3649 | } |
| 3650 | |
| 3651 | if (host.password) |
| 3652 | talloc_free(host.password); |
| 3653 | host.password = NULL; |
| 3654 | |
| 3655 | #ifdef VTY_CRYPT_PW |
| 3656 | if (host.encrypt) { |
| 3657 | if (host.password_encrypt) |
| 3658 | talloc_free(host.password_encrypt); |
| 3659 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0])); |
| 3660 | } else |
| 3661 | #endif |
| 3662 | host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
| 3663 | |
| 3664 | return CMD_SUCCESS; |
| 3665 | } |
| 3666 | |
| 3667 | ALIAS(config_password, password_text_cmd, |
| 3668 | "password LINE", |
| 3669 | "Assign the terminal connection password\n" |
| 3670 | "The UNENCRYPTED (cleartext) line password\n") |
| 3671 | |
| 3672 | /* VTY enable password set. */ |
| 3673 | DEFUN(config_enable_password, enable_password_cmd, |
| 3674 | "enable password (8|) WORD", |
| 3675 | "Modify enable password parameters\n" |
| 3676 | "Assign the privileged level password\n" |
| 3677 | "Specifies a HIDDEN password will follow\n" |
| 3678 | "dummy string \n" "The HIDDEN 'enable' password string\n") |
| 3679 | { |
| 3680 | /* Argument check. */ |
| 3681 | if (argc == 0) { |
| 3682 | vty_out(vty, "Please specify password.%s", VTY_NEWLINE); |
| 3683 | return CMD_WARNING; |
| 3684 | } |
| 3685 | |
| 3686 | /* Crypt type is specified. */ |
| 3687 | if (argc == 2) { |
| 3688 | if (*argv[0] == '8') { |
| 3689 | if (host.enable) |
| 3690 | talloc_free(host.enable); |
| 3691 | host.enable = NULL; |
| 3692 | |
| 3693 | if (host.enable_encrypt) |
| 3694 | talloc_free(host.enable_encrypt); |
| 3695 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]); |
| 3696 | |
| 3697 | return CMD_SUCCESS; |
| 3698 | } else { |
| 3699 | vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE); |
| 3700 | return CMD_WARNING; |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | if (!isalnum((int)*argv[0])) { |
| 3705 | vty_out(vty, |
| 3706 | "Please specify string starting with alphanumeric%s", |
| 3707 | VTY_NEWLINE); |
| 3708 | return CMD_WARNING; |
| 3709 | } |
| 3710 | |
| 3711 | if (host.enable) |
| 3712 | talloc_free(host.enable); |
| 3713 | host.enable = NULL; |
| 3714 | |
| 3715 | /* Plain password input. */ |
| 3716 | #ifdef VTY_CRYPT_PW |
| 3717 | if (host.encrypt) { |
| 3718 | if (host.enable_encrypt) |
| 3719 | talloc_free(host.enable_encrypt); |
| 3720 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0])); |
| 3721 | } else |
| 3722 | #endif |
| 3723 | host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
| 3724 | |
| 3725 | return CMD_SUCCESS; |
| 3726 | } |
| 3727 | |
| 3728 | ALIAS(config_enable_password, |
| 3729 | enable_password_text_cmd, |
| 3730 | "enable password LINE", |
| 3731 | "Modify enable password parameters\n" |
| 3732 | "Assign the privileged level password\n" |
| 3733 | "The UNENCRYPTED (cleartext) 'enable' password\n") |
| 3734 | |
| 3735 | /* VTY enable password delete. */ |
| 3736 | DEFUN(no_config_enable_password, no_enable_password_cmd, |
| 3737 | "no enable password", |
| 3738 | NO_STR |
| 3739 | "Modify enable password parameters\n" |
| 3740 | "Assign the privileged level password\n") |
| 3741 | { |
| 3742 | if (host.enable) |
| 3743 | talloc_free(host.enable); |
| 3744 | host.enable = NULL; |
| 3745 | |
| 3746 | if (host.enable_encrypt) |
| 3747 | talloc_free(host.enable_encrypt); |
| 3748 | host.enable_encrypt = NULL; |
| 3749 | |
| 3750 | return CMD_SUCCESS; |
| 3751 | } |
| 3752 | |
| 3753 | #ifdef VTY_CRYPT_PW |
| 3754 | DEFUN(service_password_encrypt, |
| 3755 | service_password_encrypt_cmd, |
| 3756 | "service password-encryption", |
| 3757 | "Set up miscellaneous service\n" "Enable encrypted passwords\n") |
| 3758 | { |
| 3759 | if (host.encrypt) |
| 3760 | return CMD_SUCCESS; |
| 3761 | |
| 3762 | host.encrypt = 1; |
| 3763 | |
| 3764 | if (host.password) { |
| 3765 | if (host.password_encrypt) |
| 3766 | talloc_free(host.password_encrypt); |
| 3767 | host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password)); |
| 3768 | } |
| 3769 | if (host.enable) { |
| 3770 | if (host.enable_encrypt) |
| 3771 | talloc_free(host.enable_encrypt); |
| 3772 | host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable)); |
| 3773 | } |
| 3774 | |
| 3775 | return CMD_SUCCESS; |
| 3776 | } |
| 3777 | |
| 3778 | DEFUN(no_service_password_encrypt, |
| 3779 | no_service_password_encrypt_cmd, |
| 3780 | "no service password-encryption", |
| 3781 | NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n") |
| 3782 | { |
| 3783 | if (!host.encrypt) |
| 3784 | return CMD_SUCCESS; |
| 3785 | |
| 3786 | host.encrypt = 0; |
| 3787 | |
| 3788 | if (host.password_encrypt) |
| 3789 | talloc_free(host.password_encrypt); |
| 3790 | host.password_encrypt = NULL; |
| 3791 | |
| 3792 | if (host.enable_encrypt) |
| 3793 | talloc_free(host.enable_encrypt); |
| 3794 | host.enable_encrypt = NULL; |
| 3795 | |
| 3796 | return CMD_SUCCESS; |
| 3797 | } |
| 3798 | #endif |
| 3799 | |
| 3800 | DEFUN(config_terminal_length, config_terminal_length_cmd, |
| 3801 | "terminal length <0-512>", |
| 3802 | "Set terminal line parameters\n" |
| 3803 | "Set number of lines on a screen\n" |
| 3804 | "Number of lines on screen (0 for no pausing)\n") |
| 3805 | { |
| 3806 | int lines; |
| 3807 | char *endptr = NULL; |
| 3808 | |
| 3809 | lines = strtol(argv[0], &endptr, 10); |
| 3810 | if (lines < 0 || lines > 512 || *endptr != '\0') { |
| 3811 | vty_out(vty, "length is malformed%s", VTY_NEWLINE); |
| 3812 | return CMD_WARNING; |
| 3813 | } |
| 3814 | vty->lines = lines; |
| 3815 | |
| 3816 | return CMD_SUCCESS; |
| 3817 | } |
| 3818 | |
| 3819 | DEFUN(config_terminal_no_length, config_terminal_no_length_cmd, |
| 3820 | "terminal no length", |
| 3821 | "Set terminal line parameters\n" |
| 3822 | NO_STR "Set number of lines on a screen\n") |
| 3823 | { |
| 3824 | vty->lines = -1; |
| 3825 | return CMD_SUCCESS; |
| 3826 | } |
| 3827 | |
| 3828 | DEFUN(service_terminal_length, service_terminal_length_cmd, |
| 3829 | "service terminal-length <0-512>", |
| 3830 | "Set up miscellaneous service\n" |
| 3831 | "System wide terminal length configuration\n" |
| 3832 | "Number of lines of VTY (0 means no line control)\n") |
| 3833 | { |
| 3834 | int lines; |
| 3835 | char *endptr = NULL; |
| 3836 | |
| 3837 | lines = strtol(argv[0], &endptr, 10); |
| 3838 | if (lines < 0 || lines > 512 || *endptr != '\0') { |
| 3839 | vty_out(vty, "length is malformed%s", VTY_NEWLINE); |
| 3840 | return CMD_WARNING; |
| 3841 | } |
| 3842 | host.lines = lines; |
| 3843 | |
| 3844 | return CMD_SUCCESS; |
| 3845 | } |
| 3846 | |
| 3847 | DEFUN(no_service_terminal_length, no_service_terminal_length_cmd, |
| 3848 | "no service terminal-length [<0-512>]", |
| 3849 | NO_STR |
| 3850 | "Set up miscellaneous service\n" |
| 3851 | "System wide terminal length configuration\n" |
| 3852 | "Number of lines of VTY (0 means no line control)\n") |
| 3853 | { |
| 3854 | host.lines = -1; |
| 3855 | return CMD_SUCCESS; |
| 3856 | } |
| 3857 | |
| 3858 | DEFUN_HIDDEN(do_echo, |
| 3859 | echo_cmd, |
| 3860 | "echo .MESSAGE", |
| 3861 | "Echo a message back to the vty\n" "The message to echo\n") |
| 3862 | { |
| 3863 | char *message; |
| 3864 | |
| 3865 | vty_out(vty, "%s%s", |
| 3866 | ((message = |
| 3867 | argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE); |
| 3868 | if (message) |
| 3869 | talloc_free(message); |
| 3870 | return CMD_SUCCESS; |
| 3871 | } |
| 3872 | |
| 3873 | #if 0 |
| 3874 | DEFUN(config_logmsg, |
| 3875 | config_logmsg_cmd, |
| 3876 | "logmsg " LOG_LEVELS " .MESSAGE", |
| 3877 | "Send a message to enabled logging destinations\n" |
| 3878 | LOG_LEVEL_DESC "The message to send\n") |
| 3879 | { |
| 3880 | int level; |
| 3881 | char *message; |
| 3882 | |
| 3883 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3884 | return CMD_ERR_NO_MATCH; |
| 3885 | |
| 3886 | zlog(NULL, level, |
| 3887 | ((message = argv_concat(argv, argc, 1)) ? message : "")); |
| 3888 | if (message) |
| 3889 | talloc_free(message); |
| 3890 | return CMD_SUCCESS; |
| 3891 | } |
| 3892 | |
| 3893 | DEFUN(show_logging, |
| 3894 | show_logging_cmd, |
| 3895 | "show logging", SHOW_STR "Show current logging configuration\n") |
| 3896 | { |
| 3897 | struct zlog *zl = zlog_default; |
| 3898 | |
| 3899 | vty_out(vty, "Syslog logging: "); |
| 3900 | if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED) |
| 3901 | vty_out(vty, "disabled"); |
| 3902 | else |
| 3903 | vty_out(vty, "level %s, facility %s, ident %s", |
| 3904 | zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]], |
| 3905 | facility_name(zl->facility), zl->ident); |
| 3906 | vty_out(vty, "%s", VTY_NEWLINE); |
| 3907 | |
| 3908 | vty_out(vty, "Stdout logging: "); |
| 3909 | if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED) |
| 3910 | vty_out(vty, "disabled"); |
| 3911 | else |
| 3912 | vty_out(vty, "level %s", |
| 3913 | zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]); |
| 3914 | vty_out(vty, "%s", VTY_NEWLINE); |
| 3915 | |
| 3916 | vty_out(vty, "Monitor logging: "); |
| 3917 | if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED) |
| 3918 | vty_out(vty, "disabled"); |
| 3919 | else |
| 3920 | vty_out(vty, "level %s", |
| 3921 | zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]); |
| 3922 | vty_out(vty, "%s", VTY_NEWLINE); |
| 3923 | |
| 3924 | vty_out(vty, "File logging: "); |
| 3925 | if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp) |
| 3926 | vty_out(vty, "disabled"); |
| 3927 | else |
| 3928 | vty_out(vty, "level %s, filename %s", |
| 3929 | zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]], |
| 3930 | zl->filename); |
| 3931 | vty_out(vty, "%s", VTY_NEWLINE); |
| 3932 | |
| 3933 | vty_out(vty, "Protocol name: %s%s", |
| 3934 | zlog_proto_names[zl->protocol], VTY_NEWLINE); |
| 3935 | vty_out(vty, "Record priority: %s%s", |
| 3936 | (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE); |
| 3937 | |
| 3938 | return CMD_SUCCESS; |
| 3939 | } |
| 3940 | |
| 3941 | DEFUN(config_log_stdout, |
| 3942 | config_log_stdout_cmd, |
| 3943 | "log stdout", "Logging control\n" "Set stdout logging level\n") |
| 3944 | { |
| 3945 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); |
| 3946 | return CMD_SUCCESS; |
| 3947 | } |
| 3948 | |
| 3949 | DEFUN(config_log_stdout_level, |
| 3950 | config_log_stdout_level_cmd, |
| 3951 | "log stdout " LOG_LEVELS, |
| 3952 | "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC) |
| 3953 | { |
| 3954 | int level; |
| 3955 | |
| 3956 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3957 | return CMD_ERR_NO_MATCH; |
| 3958 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, level); |
| 3959 | return CMD_SUCCESS; |
| 3960 | } |
| 3961 | |
| 3962 | DEFUN(no_config_log_stdout, |
| 3963 | no_config_log_stdout_cmd, |
| 3964 | "no log stdout [LEVEL]", |
| 3965 | NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n") |
| 3966 | { |
| 3967 | zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); |
| 3968 | return CMD_SUCCESS; |
| 3969 | } |
| 3970 | |
| 3971 | DEFUN(config_log_monitor, |
| 3972 | config_log_monitor_cmd, |
| 3973 | "log monitor", |
| 3974 | "Logging control\n" "Set terminal line (monitor) logging level\n") |
| 3975 | { |
| 3976 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); |
| 3977 | return CMD_SUCCESS; |
| 3978 | } |
| 3979 | |
| 3980 | DEFUN(config_log_monitor_level, |
| 3981 | config_log_monitor_level_cmd, |
| 3982 | "log monitor " LOG_LEVELS, |
| 3983 | "Logging control\n" |
| 3984 | "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC) |
| 3985 | { |
| 3986 | int level; |
| 3987 | |
| 3988 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 3989 | return CMD_ERR_NO_MATCH; |
| 3990 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, level); |
| 3991 | return CMD_SUCCESS; |
| 3992 | } |
| 3993 | |
| 3994 | DEFUN(no_config_log_monitor, |
| 3995 | no_config_log_monitor_cmd, |
| 3996 | "no log monitor [LEVEL]", |
| 3997 | NO_STR |
| 3998 | "Logging control\n" |
| 3999 | "Disable terminal line (monitor) logging\n" "Logging level\n") |
| 4000 | { |
| 4001 | zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); |
| 4002 | return CMD_SUCCESS; |
| 4003 | } |
| 4004 | |
| 4005 | static int set_log_file(struct vty *vty, const char *fname, int loglevel) |
| 4006 | { |
| 4007 | int ret; |
| 4008 | char *p = NULL; |
| 4009 | const char *fullpath; |
| 4010 | |
| 4011 | /* Path detection. */ |
| 4012 | if (!IS_DIRECTORY_SEP(*fname)) { |
| 4013 | char cwd[MAXPATHLEN + 1]; |
| 4014 | cwd[MAXPATHLEN] = '\0'; |
| 4015 | |
| 4016 | if (getcwd(cwd, MAXPATHLEN) == NULL) { |
| 4017 | zlog_err("config_log_file: Unable to alloc mem!"); |
| 4018 | return CMD_WARNING; |
| 4019 | } |
| 4020 | |
| 4021 | if ((p = _talloc_zero(tall_vcmd_ctx, |
| 4022 | strlen(cwd) + strlen(fname) + 2), |
| 4023 | "set_log_file") |
| 4024 | == NULL) { |
| 4025 | zlog_err("config_log_file: Unable to alloc mem!"); |
| 4026 | return CMD_WARNING; |
| 4027 | } |
| 4028 | sprintf(p, "%s/%s", cwd, fname); |
| 4029 | fullpath = p; |
| 4030 | } else |
| 4031 | fullpath = fname; |
| 4032 | |
| 4033 | ret = zlog_set_file(NULL, fullpath, loglevel); |
| 4034 | |
| 4035 | if (p) |
| 4036 | talloc_free(p); |
| 4037 | |
| 4038 | if (!ret) { |
| 4039 | vty_out(vty, "can't open logfile %s\n", fname); |
| 4040 | return CMD_WARNING; |
| 4041 | } |
| 4042 | |
| 4043 | if (host.logfile) |
| 4044 | talloc_free(host.logfile); |
| 4045 | |
| 4046 | host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname); |
| 4047 | |
| 4048 | return CMD_SUCCESS; |
| 4049 | } |
| 4050 | |
| 4051 | DEFUN(config_log_file, |
| 4052 | config_log_file_cmd, |
| 4053 | "log file FILENAME", |
| 4054 | "Logging control\n" "Logging to file\n" "Logging filename\n") |
| 4055 | { |
| 4056 | return set_log_file(vty, argv[0], zlog_default->default_lvl); |
| 4057 | } |
| 4058 | |
| 4059 | DEFUN(config_log_file_level, |
| 4060 | config_log_file_level_cmd, |
| 4061 | "log file FILENAME " LOG_LEVELS, |
| 4062 | "Logging control\n" |
| 4063 | "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC) |
| 4064 | { |
| 4065 | int level; |
| 4066 | |
| 4067 | if ((level = level_match(argv[1])) == ZLOG_DISABLED) |
| 4068 | return CMD_ERR_NO_MATCH; |
| 4069 | return set_log_file(vty, argv[0], level); |
| 4070 | } |
| 4071 | |
| 4072 | DEFUN(no_config_log_file, |
| 4073 | no_config_log_file_cmd, |
| 4074 | "no log file [FILENAME]", |
| 4075 | NO_STR |
| 4076 | "Logging control\n" "Cancel logging to file\n" "Logging file name\n") |
| 4077 | { |
| 4078 | zlog_reset_file(NULL); |
| 4079 | |
| 4080 | if (host.logfile) |
| 4081 | talloc_free(host.logfile); |
| 4082 | |
| 4083 | host.logfile = NULL; |
| 4084 | |
| 4085 | return CMD_SUCCESS; |
| 4086 | } |
| 4087 | |
| 4088 | ALIAS(no_config_log_file, |
| 4089 | no_config_log_file_level_cmd, |
| 4090 | "no log file FILENAME LEVEL", |
| 4091 | NO_STR |
| 4092 | "Logging control\n" |
| 4093 | "Cancel logging to file\n" "Logging file name\n" "Logging level\n") |
| 4094 | |
| 4095 | DEFUN(config_log_syslog, |
| 4096 | config_log_syslog_cmd, |
| 4097 | "log syslog", "Logging control\n" "Set syslog logging level\n") |
| 4098 | { |
| 4099 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
| 4100 | return CMD_SUCCESS; |
| 4101 | } |
| 4102 | |
| 4103 | DEFUN(config_log_syslog_level, |
| 4104 | config_log_syslog_level_cmd, |
| 4105 | "log syslog " LOG_LEVELS, |
| 4106 | "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC) |
| 4107 | { |
| 4108 | int level; |
| 4109 | |
| 4110 | if ((level = level_match(argv[0])) == ZLOG_DISABLED) |
| 4111 | return CMD_ERR_NO_MATCH; |
| 4112 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level); |
| 4113 | return CMD_SUCCESS; |
| 4114 | } |
| 4115 | |
| 4116 | DEFUN_DEPRECATED(config_log_syslog_facility, |
| 4117 | config_log_syslog_facility_cmd, |
| 4118 | "log syslog facility " LOG_FACILITIES, |
| 4119 | "Logging control\n" |
| 4120 | "Logging goes to syslog\n" |
| 4121 | "(Deprecated) Facility parameter for syslog messages\n" |
| 4122 | LOG_FACILITY_DESC) |
| 4123 | { |
| 4124 | int facility; |
| 4125 | |
| 4126 | if ((facility = facility_match(argv[0])) < 0) |
| 4127 | return CMD_ERR_NO_MATCH; |
| 4128 | |
| 4129 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); |
| 4130 | zlog_default->facility = facility; |
| 4131 | return CMD_SUCCESS; |
| 4132 | } |
| 4133 | |
| 4134 | DEFUN(no_config_log_syslog, |
| 4135 | no_config_log_syslog_cmd, |
| 4136 | "no log syslog [LEVEL]", |
| 4137 | NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n") |
| 4138 | { |
| 4139 | zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); |
| 4140 | return CMD_SUCCESS; |
| 4141 | } |
| 4142 | |
| 4143 | ALIAS(no_config_log_syslog, |
| 4144 | no_config_log_syslog_facility_cmd, |
| 4145 | "no log syslog facility " LOG_FACILITIES, |
| 4146 | NO_STR |
| 4147 | "Logging control\n" |
| 4148 | "Logging goes to syslog\n" |
| 4149 | "Facility parameter for syslog messages\n" LOG_FACILITY_DESC) |
| 4150 | |
| 4151 | DEFUN(config_log_facility, |
| 4152 | config_log_facility_cmd, |
| 4153 | "log facility " LOG_FACILITIES, |
| 4154 | "Logging control\n" |
| 4155 | "Facility parameter for syslog messages\n" LOG_FACILITY_DESC) |
| 4156 | { |
| 4157 | int facility; |
| 4158 | |
| 4159 | if ((facility = facility_match(argv[0])) < 0) |
| 4160 | return CMD_ERR_NO_MATCH; |
| 4161 | zlog_default->facility = facility; |
| 4162 | return CMD_SUCCESS; |
| 4163 | } |
| 4164 | |
| 4165 | DEFUN(no_config_log_facility, |
| 4166 | no_config_log_facility_cmd, |
| 4167 | "no log facility [FACILITY]", |
| 4168 | NO_STR |
| 4169 | "Logging control\n" |
| 4170 | "Reset syslog facility to default (daemon)\n" "Syslog facility\n") |
| 4171 | { |
| 4172 | zlog_default->facility = LOG_DAEMON; |
| 4173 | return CMD_SUCCESS; |
| 4174 | } |
| 4175 | |
| 4176 | DEFUN_DEPRECATED(config_log_trap, |
| 4177 | config_log_trap_cmd, |
| 4178 | "log trap " LOG_LEVELS, |
| 4179 | "Logging control\n" |
| 4180 | "(Deprecated) Set logging level and default for all destinations\n" |
| 4181 | LOG_LEVEL_DESC) |
| 4182 | { |
| 4183 | int new_level; |
| 4184 | int i; |
| 4185 | |
| 4186 | if ((new_level = level_match(argv[0])) == ZLOG_DISABLED) |
| 4187 | return CMD_ERR_NO_MATCH; |
| 4188 | |
| 4189 | zlog_default->default_lvl = new_level; |
| 4190 | for (i = 0; i < ZLOG_NUM_DESTS; i++) |
| 4191 | if (zlog_default->maxlvl[i] != ZLOG_DISABLED) |
| 4192 | zlog_default->maxlvl[i] = new_level; |
| 4193 | return CMD_SUCCESS; |
| 4194 | } |
| 4195 | |
| 4196 | DEFUN_DEPRECATED(no_config_log_trap, |
| 4197 | no_config_log_trap_cmd, |
| 4198 | "no log trap [LEVEL]", |
| 4199 | NO_STR |
| 4200 | "Logging control\n" |
| 4201 | "Permit all logging information\n" "Logging level\n") |
| 4202 | { |
| 4203 | zlog_default->default_lvl = LOG_DEBUG; |
| 4204 | return CMD_SUCCESS; |
| 4205 | } |
| 4206 | |
| 4207 | DEFUN(config_log_record_priority, |
| 4208 | config_log_record_priority_cmd, |
| 4209 | "log record-priority", |
| 4210 | "Logging control\n" |
| 4211 | "Log the priority of the message within the message\n") |
| 4212 | { |
| 4213 | zlog_default->record_priority = 1; |
| 4214 | return CMD_SUCCESS; |
| 4215 | } |
| 4216 | |
| 4217 | DEFUN(no_config_log_record_priority, |
| 4218 | no_config_log_record_priority_cmd, |
| 4219 | "no log record-priority", |
| 4220 | NO_STR |
| 4221 | "Logging control\n" |
| 4222 | "Do not log the priority of the message within the message\n") |
| 4223 | { |
| 4224 | zlog_default->record_priority = 0; |
| 4225 | return CMD_SUCCESS; |
| 4226 | } |
| 4227 | #endif |
| 4228 | |
| 4229 | DEFUN(banner_motd_file, |
| 4230 | banner_motd_file_cmd, |
| 4231 | "banner motd file [FILE]", |
| 4232 | "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n") |
| 4233 | { |
| 4234 | if (host.motdfile) |
| 4235 | talloc_free(host.motdfile); |
| 4236 | host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]); |
| 4237 | |
| 4238 | return CMD_SUCCESS; |
| 4239 | } |
| 4240 | |
| 4241 | DEFUN(banner_motd_default, |
| 4242 | banner_motd_default_cmd, |
| 4243 | "banner motd default", |
| 4244 | "Set banner string\n" "Strings for motd\n" "Default string\n") |
| 4245 | { |
| 4246 | host.motd = default_motd; |
| 4247 | return CMD_SUCCESS; |
| 4248 | } |
| 4249 | |
| 4250 | DEFUN(no_banner_motd, |
| 4251 | no_banner_motd_cmd, |
| 4252 | "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n") |
| 4253 | { |
| 4254 | host.motd = NULL; |
| 4255 | if (host.motdfile) |
| 4256 | talloc_free(host.motdfile); |
| 4257 | host.motdfile = NULL; |
| 4258 | return CMD_SUCCESS; |
| 4259 | } |
| 4260 | |
| 4261 | /* Set config filename. Called from vty.c */ |
| 4262 | void host_config_set(const char *filename) |
| 4263 | { |
| 4264 | host.config = talloc_strdup(tall_vty_cmd_ctx, filename); |
| 4265 | } |
| 4266 | |
Pau Espin Pedrol | ebb6c1f | 2021-05-17 18:54:21 +0200 | [diff] [blame] | 4267 | const char *host_config_file(void) |
| 4268 | { |
| 4269 | return host.config; |
| 4270 | } |
| 4271 | |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 4272 | /*! Deprecated, now happens implicitly when calling install_node(). |
| 4273 | * Users of the API may still attempt to call this function, hence |
| 4274 | * leave it here as a no-op. */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 4275 | void install_default(int node) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4276 | { |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | /*! Deprecated, now happens implicitly when calling install_node(). |
| 4280 | * Users of the API may still attempt to call this function, hence |
| 4281 | * leave it here as a no-op. */ |
| 4282 | void vty_install_default(int node) |
| 4283 | { |
| 4284 | } |
| 4285 | |
| 4286 | /*! Install common commands like 'exit' and 'list'. */ |
| 4287 | static void install_basic_node_commands(int node) |
| 4288 | { |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4289 | install_lib_element(node, &config_help_cmd); |
| 4290 | install_lib_element(node, &config_list_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4291 | |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 4292 | install_lib_element(node, &show_vty_attr_all_cmd); |
| 4293 | install_lib_element(node, &show_vty_attr_cmd); |
| 4294 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4295 | install_lib_element(node, &config_write_terminal_cmd); |
| 4296 | install_lib_element(node, &config_write_file_cmd); |
| 4297 | install_lib_element(node, &config_write_memory_cmd); |
| 4298 | install_lib_element(node, &config_write_cmd); |
| 4299 | install_lib_element(node, &show_running_config_cmd); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 4300 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4301 | install_lib_element(node, &config_exit_cmd); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 4302 | |
| 4303 | if (node >= CONFIG_NODE) { |
| 4304 | /* It's not a top node. */ |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4305 | install_lib_element(node, &config_end_cmd); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 4306 | } |
| 4307 | } |
| 4308 | |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 4309 | /*! Return true if a node is installed by install_basic_node_commands(), so |
| 4310 | * that we can avoid repeating them for each and every node during 'show |
| 4311 | * running-config' */ |
Vadim Yanitskiy | efe1342 | 2020-10-21 20:36:17 +0700 | [diff] [blame] | 4312 | static bool vty_command_is_common(const struct cmd_element *cmd) |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 4313 | { |
| 4314 | if (cmd == &config_help_cmd |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 4315 | || cmd == &show_vty_attr_all_cmd |
| 4316 | || cmd == &show_vty_attr_cmd |
Neels Hofmeyr | 69054e2 | 2017-10-19 02:44:57 +0200 | [diff] [blame] | 4317 | || cmd == &config_list_cmd |
| 4318 | || cmd == &config_write_terminal_cmd |
| 4319 | || cmd == &config_write_file_cmd |
| 4320 | || cmd == &config_write_memory_cmd |
| 4321 | || cmd == &config_write_cmd |
| 4322 | || cmd == &show_running_config_cmd |
| 4323 | || cmd == &config_exit_cmd |
| 4324 | || cmd == &config_end_cmd) |
| 4325 | return true; |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 4329 | /** |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 4330 | * Write the current running config to a given file |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 4331 | * \param[in] vty the vty of the code |
| 4332 | * \param[in] filename where to store the file |
| 4333 | * \return 0 in case of success. |
| 4334 | * |
| 4335 | * If the filename already exists create a filename.sav |
| 4336 | * version with the current code. |
| 4337 | * |
| 4338 | */ |
| 4339 | int osmo_vty_write_config_file(const char *filename) |
| 4340 | { |
| 4341 | char *failed_file; |
| 4342 | int rc; |
| 4343 | |
| 4344 | rc = write_config_file(filename, &failed_file); |
| 4345 | talloc_free(failed_file); |
| 4346 | return rc; |
| 4347 | } |
| 4348 | |
| 4349 | /** |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 4350 | * Save the current state to the config file |
Holger Hans Peter Freyther | 738f133 | 2012-03-24 18:26:24 +0100 | [diff] [blame] | 4351 | * \return 0 in case of success. |
| 4352 | * |
| 4353 | * If the filename already exists create a filename.sav |
| 4354 | * version with the current code. |
| 4355 | * |
| 4356 | */ |
| 4357 | int osmo_vty_save_config_file(void) |
| 4358 | { |
| 4359 | char *failed_file; |
| 4360 | int rc; |
| 4361 | |
| 4362 | if (host.config == NULL) |
| 4363 | return -7; |
| 4364 | |
| 4365 | rc = write_config_file(host.config, &failed_file); |
| 4366 | talloc_free(failed_file); |
| 4367 | return rc; |
| 4368 | } |
| 4369 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4370 | /* Initialize command interface. Install basic nodes and commands. */ |
| 4371 | void cmd_init(int terminal) |
| 4372 | { |
| 4373 | /* Allocate initial top vector of commands. */ |
| 4374 | cmdvec = vector_init(VECTOR_MIN_SIZE); |
| 4375 | |
| 4376 | /* Default host value settings. */ |
| 4377 | host.name = NULL; |
| 4378 | host.password = NULL; |
| 4379 | host.enable = NULL; |
| 4380 | host.logfile = NULL; |
| 4381 | host.config = NULL; |
| 4382 | host.lines = -1; |
| 4383 | host.motd = default_motd; |
| 4384 | host.motdfile = NULL; |
| 4385 | |
| 4386 | /* Install top nodes. */ |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 4387 | install_node_bare(&view_node, NULL); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4388 | install_node(&enable_node, NULL); |
Neels Hofmeyr | f4f23bd | 2017-09-20 15:39:37 +0200 | [diff] [blame] | 4389 | install_node_bare(&auth_node, NULL); |
| 4390 | install_node_bare(&auth_enable_node, NULL); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4391 | install_node(&config_node, config_write_host); |
| 4392 | |
| 4393 | /* Each node's basic commands. */ |
Oliver Smith | 0c78bc6 | 2021-07-12 17:28:36 +0200 | [diff] [blame] | 4394 | install_lib_element(VIEW_NODE, &show_pid_cmd); |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 4395 | install_lib_element(VIEW_NODE, &show_uptime_cmd); |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4396 | install_lib_element(VIEW_NODE, &show_version_cmd); |
| 4397 | install_lib_element(VIEW_NODE, &show_online_help_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4398 | if (terminal) { |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4399 | install_lib_element(VIEW_NODE, &config_list_cmd); |
| 4400 | install_lib_element(VIEW_NODE, &config_exit_cmd); |
| 4401 | install_lib_element(VIEW_NODE, &config_help_cmd); |
Vadim Yanitskiy | 44c4d17 | 2020-10-05 01:30:23 +0700 | [diff] [blame] | 4402 | install_lib_element(VIEW_NODE, &show_vty_attr_all_cmd); |
| 4403 | install_lib_element(VIEW_NODE, &show_vty_attr_cmd); |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4404 | install_lib_element(VIEW_NODE, &config_enable_cmd); |
| 4405 | install_lib_element(VIEW_NODE, &config_terminal_length_cmd); |
| 4406 | install_lib_element(VIEW_NODE, &config_terminal_no_length_cmd); |
| 4407 | install_lib_element(VIEW_NODE, &echo_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | if (terminal) { |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4411 | install_lib_element(ENABLE_NODE, &config_disable_cmd); |
| 4412 | install_lib_element(ENABLE_NODE, &config_terminal_cmd); |
| 4413 | install_lib_element(ENABLE_NODE, ©_runningconfig_startupconfig_cmd); |
Oliver Smith | 8a482fd | 2021-07-12 18:18:51 +0200 | [diff] [blame] | 4414 | install_lib_element(ENABLE_NODE, &shutdown_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4415 | } |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4416 | install_lib_element(ENABLE_NODE, &show_startup_config_cmd); |
| 4417 | install_lib_element(ENABLE_NODE, &show_version_cmd); |
| 4418 | install_lib_element(ENABLE_NODE, &show_online_help_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4419 | |
| 4420 | if (terminal) { |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4421 | install_lib_element(ENABLE_NODE, &config_terminal_length_cmd); |
| 4422 | install_lib_element(ENABLE_NODE, &config_terminal_no_length_cmd); |
| 4423 | install_lib_element(ENABLE_NODE, &echo_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4424 | } |
| 4425 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4426 | install_lib_element(CONFIG_NODE, &hostname_cmd); |
| 4427 | install_lib_element(CONFIG_NODE, &no_hostname_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4428 | |
| 4429 | if (terminal) { |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4430 | install_lib_element(CONFIG_NODE, &password_cmd); |
| 4431 | install_lib_element(CONFIG_NODE, &password_text_cmd); |
| 4432 | install_lib_element(CONFIG_NODE, &enable_password_cmd); |
| 4433 | install_lib_element(CONFIG_NODE, &enable_password_text_cmd); |
| 4434 | install_lib_element(CONFIG_NODE, &no_enable_password_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4435 | |
| 4436 | #ifdef VTY_CRYPT_PW |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4437 | install_lib_element(CONFIG_NODE, &service_password_encrypt_cmd); |
| 4438 | install_lib_element(CONFIG_NODE, &no_service_password_encrypt_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4439 | #endif |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 4440 | install_lib_element(CONFIG_NODE, &banner_motd_default_cmd); |
| 4441 | install_lib_element(CONFIG_NODE, &banner_motd_file_cmd); |
| 4442 | install_lib_element(CONFIG_NODE, &no_banner_motd_cmd); |
| 4443 | install_lib_element(CONFIG_NODE, &service_terminal_length_cmd); |
| 4444 | install_lib_element(CONFIG_NODE, &no_service_terminal_length_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4445 | |
| 4446 | } |
| 4447 | srand(time(NULL)); |
| 4448 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 4449 | |
Oliver Smith | d243c2a | 2021-07-09 17:19:32 +0200 | [diff] [blame] | 4450 | static __attribute__((constructor)) void on_dso_load_starttime(void) |
| 4451 | { |
| 4452 | osmo_clock_gettime(CLOCK_MONOTONIC, &starttime); |
| 4453 | } |
| 4454 | |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 4455 | /* FIXME: execute this section in the unit test instead */ |
| 4456 | static __attribute__((constructor)) void on_dso_load(void) |
| 4457 | { |
| 4458 | unsigned int i, j; |
| 4459 | |
| 4460 | /* Check total number of the library specific attributes */ |
| 4461 | OSMO_ASSERT(_OSMO_CORE_LIB_ATTR_COUNT < 32); |
| 4462 | |
| 4463 | /* Check for duplicates in the list of library specific flags */ |
| 4464 | for (i = 0; i < _OSMO_CORE_LIB_ATTR_COUNT; i++) { |
| 4465 | if (cmd_lib_attr_letters[i] == '\0') |
| 4466 | continue; |
| 4467 | |
Vadim Yanitskiy | ef4c597 | 2020-10-07 13:44:31 +0700 | [diff] [blame] | 4468 | /* Some flag characters are reserved for global attributes */ |
| 4469 | const char rafc[] = VTY_CMD_ATTR_FLAGS_RESERVED; |
| 4470 | for (j = 0; j < ARRAY_SIZE(rafc); j++) |
| 4471 | OSMO_ASSERT(cmd_lib_attr_letters[i] != rafc[j]); |
| 4472 | |
Vadim Yanitskiy | c0745eb | 2020-10-04 18:37:13 +0700 | [diff] [blame] | 4473 | /* Only upper case flag letters are allowed for libraries */ |
| 4474 | OSMO_ASSERT(cmd_lib_attr_letters[i] >= 'A'); |
| 4475 | OSMO_ASSERT(cmd_lib_attr_letters[i] <= 'Z'); |
| 4476 | |
| 4477 | for (j = i + 1; j < _OSMO_CORE_LIB_ATTR_COUNT; j++) |
| 4478 | OSMO_ASSERT(cmd_lib_attr_letters[i] != cmd_lib_attr_letters[j]); |
| 4479 | } |
| 4480 | } |
| 4481 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 4482 | /*! @} */ |