blob: 1e76474f100c1a36b55c63cb719f3e2b8e57e269 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/*
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 Weltee08da972017-11-13 01:00:26 +09006 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 Welte3fb0b6f2010-05-19 19:02:52 +020012
13This file is part of GNU Zebra.
14
15GNU Zebra is free software; you can redistribute it and/or modify
16it under the terms of the GNU General Public License as published
17by the Free Software Foundation; either version 2, or (at your
18option) any later version.
19
20GNU Zebra is distributed in the hope that it will be useful, but
21WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23General Public License for more details.
24
25You should have received a copy of the GNU General Public License
26along with GNU Zebra; see the file COPYING. If not, write to the
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010027Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28Boston, MA 02110-1301, USA. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020029
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
Sylvain Munaut4d8eea42012-12-28 11:58:23 +010033#include <stdbool.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020034#include <syslog.h>
35#include <errno.h>
36#define _XOPEN_SOURCE
37#include <unistd.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020038#include <ctype.h>
39#include <time.h>
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +020040#include <limits.h>
Oliver Smith8a482fd2021-07-12 18:18:51 +020041#include <signal.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020042#include <sys/time.h>
43#include <sys/stat.h>
Oliver Smith8a482fd2021-07-12 18:18:51 +020044#include <sys/types.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020045
46#include <osmocom/vty/vector.h>
47#include <osmocom/vty/vty.h>
48#include <osmocom/vty/command.h>
49
Oliver Smith8a482fd2021-07-12 18:18:51 +020050#include <osmocom/core/logging.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010051#include <osmocom/core/talloc.h>
Oliver Smithd243c2a2021-07-09 17:19:32 +020052#include <osmocom/core/timer.h>
Harald Weltea99d45a2015-11-12 13:48:23 +010053#include <osmocom/core/utils.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020054
Ruben Undheim766f77c2018-11-18 13:02:47 +010055#ifndef MAXPATHLEN
56 #define MAXPATHLEN 4096
57#endif
58
59
Harald Weltee881b1b2011-08-17 18:52:30 +020060/*! \addtogroup command
Harald Welte96e2a002017-06-12 21:44:18 +020061 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 * VTY command handling
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020063 *
64 * \file command.c */
Harald Welte7acb30c2011-08-17 17:13:48 +020065
Harald Welte3fb0b6f2010-05-19 19:02:52 +020066#define CONFIGFILE_MASK 022
67
68void *tall_vty_cmd_ctx;
69
Oliver Smithd243c2a2021-07-09 17:19:32 +020070/* Set by on_dso_load_starttime() for "show uptime". */
71static struct timespec starttime;
72
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073/* Command vector which includes some level of command lists. Normally
74 each daemon maintains each own cmdvec. */
75vector cmdvec;
76
77/* Host information structure. */
78struct host host;
79
80/* Standard command node structures. */
81struct cmd_node auth_node = {
82 AUTH_NODE,
83 "Password: ",
Neels Hofmeyr3b681572017-12-09 03:54:32 +010084 .name = "auth",
Harald Welte3fb0b6f2010-05-19 19:02:52 +020085};
86
87struct cmd_node view_node = {
88 VIEW_NODE,
89 "%s> ",
Neels Hofmeyr3b681572017-12-09 03:54:32 +010090 .name = "view",
Harald Welte3fb0b6f2010-05-19 19:02:52 +020091};
92
93struct cmd_node auth_enable_node = {
94 AUTH_ENABLE_NODE,
95 "Password: ",
Neels Hofmeyr3b681572017-12-09 03:54:32 +010096 .name = "auth-enable",
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097};
98
99struct cmd_node enable_node = {
100 ENABLE_NODE,
101 "%s# ",
Neels Hofmeyr3b681572017-12-09 03:54:32 +0100102 .name = "enable",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103};
104
105struct cmd_node config_node = {
106 CONFIG_NODE,
107 "%s(config)# ",
108 1
109};
110
111/* Default motd string. */
112const char *default_motd = "";
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! print the version (and optionally copyright) information
Harald Welte7acb30c2011-08-17 17:13:48 +0200115 *
116 * This is called from main when a daemon is invoked with -v or --version. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117void print_version(int print_copyright)
118{
Harald Welte237f6242010-05-25 23:00:45 +0200119 printf("%s version %s\n", host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200120 if (print_copyright)
Harald Welte237f6242010-05-25 23:00:45 +0200121 printf("\n%s\n", host.app_info->copyright);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200122}
123
124/* Utility function to concatenate argv argument into a single string
125 with inserting ' ' character between each argument. */
126char *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 Hofmeyr657c5b62017-09-18 16:42:06 +0200149/* 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'. */
152static 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 Hofmeyrf4f23bd2017-09-20 15:39:37 +0200175static void install_basic_node_commands(int node);
176
177/*! Install top node of command vector, without adding basic node commands. */
178static void install_node_bare(struct cmd_node *node, int (*func) (struct vty *))
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200179{
180 vector_set_index(cmdvec, node->node, node);
181 node->func = func;
182 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200183 if (!*node->name)
184 node_name_from_prompt(node->prompt, node->name, sizeof(node->name));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200185}
186
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +0200187/*! Install top node of command vector. */
188void 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 Welte3fb0b6f2010-05-19 19:02:52 +0200194/* Compare two command's string. Used in sort_node (). */
195static 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
203static 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 Hofmeyr87e45502017-06-20 00:17:59 +0200211/*! Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200212void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200213{
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 Hofmeyr4a31ffa2017-09-07 03:08:06 +0200241/*! 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 */
251int cmd_make_strvec2(const char *string, char **indent, vector *strvec_p)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200252{
253 const char *cp, *start;
254 char *token;
255 int strlen;
256 vector strvec;
257
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200258 *strvec_p = NULL;
259 if (indent)
260 *indent = 0;
261
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200262 if (string == NULL)
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200263 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200264
265 cp = string;
266
267 /* Skip white spaces. */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200268 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 Welte3fb0b6f2010-05-19 19:02:52 +0200279 cp++;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200280 }
281
282 if (indent)
283 *indent = talloc_strndup(tall_vty_cmd_ctx, string, cp - string);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200284
285 /* Return if there is only white spaces */
286 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200287 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200288
289 if (*cp == '!' || *cp == '#')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200290 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200291
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 Hofmeyr4a31ffa2017-09-07 03:08:06 +0200312 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200313 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200314
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. */
322vector cmd_make_strvec(const char *string)
323{
324 vector strvec;
325 cmd_make_strvec2(string, NULL, &strvec);
326 return strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200327}
328
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200329/*! Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200330void 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 Hofmeyr87e45502017-06-20 00:17:59 +0200345/*! Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200346static 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 Hofmeyr87e45502017-06-20 00:17:59 +0200380/*! New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200381static vector cmd_make_descvec(const char *string, const char *descstr)
382{
383 int multiple = 0;
Neels Hofmeyrb55f4d22019-01-31 08:13:31 +0100384 int optional_brace = 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200385 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 Hofmeyrb55f4d22019-01-31 08:13:31 +0100406 /* Explicitly detect optional multi-choice braces like [(one|two)]. */
407 if (cp[0] == '[' && cp[1] == '(') {
408 optional_brace = 1;
409 cp++;
410 }
411
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200412 if (*cp == '(') {
413 multiple = 1;
414 cp++;
415 }
416 if (*cp == ')') {
417 multiple = 0;
418 cp++;
Neels Hofmeyrb55f4d22019-01-31 08:13:31 +0100419 if (*cp == ']')
420 cp++;
421 optional_brace = 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200422 }
423 if (*cp == '|') {
Harald Weltea99d45a2015-11-12 13:48:23 +0100424 OSMO_ASSERT(multiple);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200425 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 Hofmeyrb55f4d22019-01-31 08:13:31 +0100448 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 Welte3fb0b6f2010-05-19 19:02:52 +0200459
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. */
480static 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 Hofmeyrc1978092019-01-31 08:14:26 +0100489 if ((vector_active(descvec)) >= 1
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200490 && (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 Hofmeyr87e45502017-06-20 00:17:59 +0200501/*! Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200502const 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 Couzensad580ba2016-05-16 16:01:45 +0200510/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200511 * escape all special asciidoc symbols
Alexander Couzensad580ba2016-05-16 16:01:45 +0200512 * \param unsafe string
513 * \return a new talloc char *
514 */
515char *osmo_asciidoc_escape(const char *inp)
516{
517 int _strlen;
518 char *out, *out_ptr;
Pau Espin Pedrole1e1ec32019-06-19 14:55:45 +0200519 int len = 0, i;
Alexander Couzensad580ba2016-05-16 16:01:45 +0200520
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 Hofmeyrf2d323e2018-07-09 19:18:35 +0200536 out = talloc_size(tall_vty_cmd_ctx, len + 1);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200537 if (!out)
538 return NULL;
539
540 out_ptr = out;
541
Alexander Couzensad580ba2016-05-16 16:01:45 +0200542 for (i = 0; i < _strlen; ++i) {
543 switch (inp[i]) {
544 case '|':
Pau Espin Pedrole1e1ec32019-06-19 14:55:45 +0200545 /* Prepend escape character "\": */
546 *(out_ptr++) = '\\';
547 /* fall through */
Alexander Couzensad580ba2016-05-16 16:01:45 +0200548 default:
549 *(out_ptr++) = inp[i];
550 break;
551 }
552 }
553
Alexander Couzensad580ba2016-05-16 16:01:45 +0200554 out_ptr[0] = '\0';
555 return out;
556}
557
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100558static 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 Hofmeyrf2d323e2018-07-09 19:18:35 +0200591 out = talloc_size(tall_vty_cmd_ctx, len + 1);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100592 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, "&quot;");
605 break;
606 case '\'':
607 ADD(out_ptr, "&apos;");
608 break;
609 case '<':
610 ADD(out_ptr, "&lt;");
611 break;
612 case '>':
613 ADD(out_ptr, "&gt;");
614 break;
615 case '&':
616 ADD(out_ptr, "&amp;");
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 Hofmeyrcf70aa02020-05-14 16:51:26 +0200630typedef int (*print_func_t)(void *data, const char *fmt, ...);
631
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700632static const struct value_string cmd_attr_desc[] = {
633 { CMD_ATTR_DEPRECATED, "This command is deprecated" },
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700634 { CMD_ATTR_HIDDEN, "This command is hidden (check expert mode)" },
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700635 { CMD_ATTR_IMMEDIATE, "This command applies immediately" },
Vadim Yanitskiyceb3b392020-10-06 00:20:24 +0700636 { CMD_ATTR_NODE_EXIT, "This command applies on VTY node exit" },
Vadim Yanitskiy99d5c2d2020-10-06 00:22:06 +0700637 /* CMD_ATTR_LIB_COMMAND is intentionally skipped */
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700638 { 0, NULL }
639};
640
Vadim Yanitskiyf1fc9d32020-10-07 13:53:38 +0700641/* Public attributes (to be printed in the VTY / XML reference) */
642#define CMD_ATTR_PUBLIC_MASK \
Vadim Yanitskiy67608452020-10-23 20:37:32 +0700643 (CMD_ATTR_HIDDEN | CMD_ATTR_IMMEDIATE | CMD_ATTR_NODE_EXIT)
Vadim Yanitskiyf1fc9d32020-10-07 13:53:38 +0700644
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +0700645/* Get a flag character for a global VTY command attribute */
646static char cmd_attr_get_flag(unsigned int attr)
647{
648 switch (attr) {
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700649 case CMD_ATTR_HIDDEN:
650 return '^';
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +0700651 case CMD_ATTR_IMMEDIATE:
652 return '!';
653 case CMD_ATTR_NODE_EXIT:
654 return '@';
655 default:
656 return '.';
657 }
658}
659
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +0700660/* Description of attributes shared between the lib commands */
661static const char * const cmd_lib_attr_desc[32] = {
662 /* [OSMO_LIBNAME_LIB_ATTR_ATTRNAME] = \
663 * "Brief but meaningful description", */
Philipp Maier608b1a42020-10-05 20:54:51 +0200664 [OSMO_SCCP_LIB_ATTR_RSTRT_ASP] = \
665 "This command applies on ASP restart",
Philipp Maiera5218ea2020-10-08 17:30:42 +0200666 [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 Yanitskiyc0745eb2020-10-04 18:37:13 +0700670};
671
672/* Flag letters of attributes shared between the lib commands.
673 * NOTE: uppercase letters only, the rest is reserved for applications. */
674static const char cmd_lib_attr_letters[32] = {
675 /* [OSMO_LIBNAME_LIB_ATTR_ATTRNAME] = 'X', */
Philipp Maier608b1a42020-10-05 20:54:51 +0200676 [OSMO_SCCP_LIB_ATTR_RSTRT_ASP] = 'A',
Philipp Maiera5218ea2020-10-08 17:30:42 +0200677 [OSMO_ABIS_LIB_ATTR_IPA_NEW_LNK] = 'I',
678 [OSMO_ABIS_LIB_ATTR_LINE_UPD] = 'L',
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +0700679};
680
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100681/*
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200682 * Write one cmd_element as XML via a print_func_t.
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100683 */
Vadim Yanitskiyefe13422020-10-21 20:36:17 +0700684static int vty_dump_element(const struct cmd_element *cmd, print_func_t print_func,
685 void *data, const char *newline)
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100686{
687 char *xml_string = xml_escape(cmd->string);
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700688 unsigned int i;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100689
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200690 print_func(data, " <command id='%s'>%s", xml_string, newline);
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700691
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700692 /* Print global attributes and their description */
Vadim Yanitskiyf1fc9d32020-10-07 13:53:38 +0700693 if (cmd->attr & CMD_ATTR_PUBLIC_MASK) { /* ... only public ones */
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700694 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 Yanitskiy7e1a78f2020-10-07 13:13:07 +0700698 char flag;
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700699
700 if (~cmd->attr & cmd_attr_desc[i].value)
701 continue;
702
703 xml_att_desc = xml_escape(cmd_attr_desc[i].str);
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +0700704 print_func(data, " <attribute doc='%s'",
Vadim Yanitskiy7f6c87d2020-09-28 13:03:49 +0700705 xml_att_desc, newline);
706 talloc_free(xml_att_desc);
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +0700707
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 Yanitskiy7f6c87d2020-09-28 13:03:49 +0700712 }
713
714 print_func(data, " </attributes>%s", newline);
715 }
716
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700717 /* Print application specific attributes and their description */
718 if (cmd->usrattr != 0x00) { /* ... if at least one flag is set */
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +0700719 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 Yanitskiy34e94f32020-08-15 23:27:55 +0700731
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 Weltec296e292020-12-21 15:44:52 +0100737 if (~cmd->usrattr & ((unsigned)1 << i))
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700738 continue;
739
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +0700740 xml_att_desc = xml_escape(desc[i]);
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700741 print_func(data, " <attribute doc='%s'", xml_att_desc);
742 talloc_free(xml_att_desc);
743
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +0700744 if ((flag = letters[i]) != '\0')
Vadim Yanitskiy34e94f32020-08-15 23:27:55 +0700745 print_func(data, " flag='%c'", flag);
746 print_func(data, " />%s", newline);
747 }
748
749 print_func(data, " </attributes>%s", newline);
750 }
751
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200752 print_func(data, " <params>%s", newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100753
Vadim Yanitskiy478dd1b2020-08-18 18:40:16 +0700754 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 Freyther8297c812011-11-18 23:14:24 +0100758 char *xml_param, *xml_doc;
Vadim Yanitskiy478dd1b2020-08-18 18:40:16 +0700759 struct desc *desc = vector_slot(descvec, j);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100760 if (desc == NULL)
761 continue;
762
763 xml_param = xml_escape(desc->cmd);
764 xml_doc = xml_escape(desc->str);
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200765 print_func(data, " <param name='%s' doc='%s' />%s",
766 xml_param, xml_doc, newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100767 talloc_free(xml_param);
768 talloc_free(xml_doc);
769 }
770 }
771
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200772 print_func(data, " </params>%s", newline);
773 print_func(data, " </command>%s", newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100774
775 talloc_free(xml_string);
776 return 0;
777}
778
Vadim Yanitskiyefe13422020-10-21 20:36:17 +0700779static bool vty_command_is_common(const struct cmd_element *cmd);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200780
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100781/*
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200782 * Dump all nodes and commands associated with a given node as XML via a print_func_t.
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700783 *
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 Freyther8297c812011-11-18 23:14:24 +0100797 */
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700798static int vty_dump_nodes(print_func_t print_func, void *data, const char *newline,
799 unsigned char gflag_mask, bool match)
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100800{
801 int i, j;
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200802 int same_name_count;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100803
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200804 print_func(data, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100805
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200806 /* Only once, list all common node commands. Use the CONFIG node to find common node commands. */
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200807 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 Hofmeyr69054e22017-10-19 02:44:57 +0200811 for (i = 0; i < vector_active(cmdvec); ++i) {
Vadim Yanitskiyefe13422020-10-21 20:36:17 +0700812 const struct cmd_node *cnode = vector_slot(cmdvec, i);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200813 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 Yanitskiyefe13422020-10-21 20:36:17 +0700819 const struct cmd_element *elem = vector_slot(cnode->cmd_vector, j);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200820 if (!vty_command_is_common(elem))
821 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700822 if (!match && (elem->attr & gflag_mask) != 0x00)
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700823 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700824 if (match && (elem->attr & gflag_mask) == 0x00)
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700825 continue;
826 vty_dump_element(elem, print_func, data, newline);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200827 }
828 }
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200829 print_func(data, " </node>%s", newline);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200830
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100831 for (i = 0; i < vector_active(cmdvec); ++i) {
Vadim Yanitskiyefe13422020-10-21 20:36:17 +0700832 const struct cmd_node *cnode = vector_slot(cmdvec, i);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100833 if (!cnode)
834 continue;
Neels Hofmeyrf7162772017-10-22 02:31:33 +0200835 if (vector_active(cnode->cmd_vector) < 1)
836 continue;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100837
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200838 /* 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 Yanitskiyefe13422020-10-21 20:36:17 +0700843 const struct cmd_node *cnode2 = vector_slot(cmdvec, j);
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200844 if (!cnode2)
845 continue;
846 if (strcmp(cnode->name, cnode2->name) == 0)
847 same_name_count ++;
848 }
849
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200850 print_func(data, " <node id='%s", cnode->name);
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200851 if (same_name_count > 1 || !*cnode->name)
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200852 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 Freyther8297c812011-11-18 23:14:24 +0100855
856 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
Vadim Yanitskiyefe13422020-10-21 20:36:17 +0700857 const struct cmd_element *elem = vector_slot(cnode->cmd_vector, j);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200858 if (vty_command_is_common(elem))
859 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700860 if (!match && (elem->attr & gflag_mask) != 0x00)
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700861 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700862 if (match && (elem->attr & gflag_mask) == 0x00)
Vadim Yanitskiy72b90882020-10-21 05:07:34 +0700863 continue;
864 vty_dump_element(elem, print_func, data, newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100865 }
866
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200867 print_func(data, " </node>%s", newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100868 }
869
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200870 print_func(data, "</vtydoc>%s", newline);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100871
872 return 0;
873}
874
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200875static 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
886static int vty_dump_xml_ref_to_vty(struct vty *vty)
887{
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700888 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 Hofmeyrcf70aa02020-05-14 16:51:26 +0200892}
893
894static 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 Yanitskiy4165d042020-10-23 16:40:57 +0700904const struct value_string vty_ref_gen_mode_names[] = {
905 { VTY_REF_GEN_MODE_DEFAULT, "default" },
906 { VTY_REF_GEN_MODE_EXPERT, "expert" },
Vadim Yanitskiy7031ac12020-11-17 16:22:44 +0700907 { VTY_REF_GEN_MODE_HIDDEN, "hidden" },
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700908 { 0, NULL }
909};
910
911const 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 Yanitskiy7031ac12020-11-17 16:22:44 +0700914 { VTY_REF_GEN_MODE_HIDDEN, "hidden commands only" },
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700915 { 0, NULL }
916};
917
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200918/*! Print the XML reference of all VTY nodes to the given stream.
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700919 * \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 */
923int vty_dump_xml_ref_mode(FILE *stream, enum vty_ref_gen_mode mode)
924{
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700925 unsigned char gflag_mask;
Vadim Yanitskiy7031ac12020-11-17 16:22:44 +0700926 bool match = false;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700927
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700928 switch (mode) {
929 case VTY_REF_GEN_MODE_EXPERT:
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700930 /* All commands except deprecated */
931 gflag_mask = CMD_ATTR_DEPRECATED;
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700932 break;
Vadim Yanitskiy7031ac12020-11-17 16:22:44 +0700933 case VTY_REF_GEN_MODE_HIDDEN:
934 /* Only hidden commands */
935 gflag_mask = CMD_ATTR_HIDDEN;
936 match = true;
937 break;
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700938 case VTY_REF_GEN_MODE_DEFAULT:
939 default:
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700940 /* All commands except deprecated and hidden */
941 gflag_mask = CMD_ATTR_DEPRECATED | CMD_ATTR_HIDDEN;
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700942 break;
943 }
944
Vadim Yanitskiy7031ac12020-11-17 16:22:44 +0700945 return vty_dump_nodes(print_func_stream, stream, "\n", gflag_mask, match);
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700946}
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 Hofmeyrcf70aa02020-05-14 16:51:26 +0200955 */
956int vty_dump_xml_ref(FILE *stream)
957{
Vadim Yanitskiy4165d042020-10-23 16:40:57 +0700958 return vty_dump_xml_ref_mode(stream, VTY_REF_GEN_MODE_DEFAULT);
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +0200959}
960
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200961/* Check if a command with given string exists at given node */
Harald Welteaddeaa32017-01-07 12:52:00 +0100962static 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 Hofmeyr87e45502017-06-20 00:17:59 +0200977/*! Install a command into a node
Harald Welte7acb30c2011-08-17 17:13:48 +0200978 * \param[in] ntype Node Type
979 * \param[cmd] element to be installed
980 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000981void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200982{
983 struct cmd_node *cnode;
984
985 cnode = vector_slot(cmdvec, ntype);
986
Harald Weltea99d45a2015-11-12 13:48:23 +0100987 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100988 /* ensure no _identical_ command has been registered at this
989 * node so far */
990 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200991
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 Yanitskiy99d5c2d2020-10-06 00:22:06 +0700998/*! Install a library command into a node
999 * \param[in] ntype Node Type
1000 * \param[in] cmd element to be installed
1001 */
1002void 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 Welte3fb0b6f2010-05-19 19:02:52 +02001008/* Install a command into VIEW and ENABLE node */
1009void install_element_ve(struct cmd_element *cmd)
1010{
1011 install_element(VIEW_NODE, cmd);
1012 install_element(ENABLE_NODE, cmd);
1013}
1014
Vadim Yanitskiy99d5c2d2020-10-06 00:22:06 +07001015/* Install a library command into VIEW and ENABLE node */
1016void install_lib_element_ve(struct cmd_element *cmd)
1017{
1018 cmd->attr |= CMD_ATTR_LIB_COMMAND;
1019 install_element_ve(cmd);
1020}
1021
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001022#ifdef VTY_CRYPT_PW
1023static unsigned char itoa64[] =
1024 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1025
1026static void to64(char *s, long v, int n)
1027{
1028 while (--n >= 0) {
1029 *s++ = itoa64[v & 0x3f];
1030 v >>= 6;
1031 }
1032}
1033
1034static char *zencrypt(const char *passwd)
1035{
1036 char salt[6];
1037 struct timeval tv;
1038 char *crypt(const char *, const char *);
1039
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +02001040 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001041
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. */
1051static 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. */
1092static vector cmd_node_vector(vector v, enum node_type ntype)
1093{
1094 struct cmd_node *cnode = vector_slot(v, ntype);
Pau Espin Pedrol079149e2022-06-16 17:08:34 +02001095 OSMO_ASSERT(cnode != NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001096 return cnode->cmd_vector;
1097}
1098
1099/* Completion match types. */
1100enum match_type {
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001101 NO_MATCH = 0,
1102 ANY_MATCH,
1103 EXTEND_MATCH,
1104 IPV4_PREFIX_MATCH,
1105 IPV4_MATCH,
1106 IPV6_PREFIX_MATCH,
1107 IPV6_MATCH,
1108 RANGE_MATCH,
1109 VARARG_MATCH,
1110 PARTLY_MATCH,
1111 EXACT_MATCH,
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001112};
1113
1114static enum match_type cmd_ipv4_match(const char *str)
1115{
1116 const char *sp;
1117 int dots = 0, nums = 0;
1118 char buf[4];
1119
1120 if (str == NULL)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001121 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001122
1123 for (;;) {
1124 memset(buf, 0, sizeof(buf));
1125 sp = str;
1126 while (*str != '\0') {
1127 if (*str == '.') {
1128 if (dots >= 3)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001129 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001130
1131 if (*(str + 1) == '.')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001132 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001133
1134 if (*(str + 1) == '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001135 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001136
1137 dots++;
1138 break;
1139 }
1140 if (!isdigit((int)*str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001141 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001142
1143 str++;
1144 }
1145
1146 if (str - sp > 3)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001147 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001148
1149 strncpy(buf, sp, str - sp);
1150 if (atoi(buf) > 255)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001151 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001152
1153 nums++;
1154
1155 if (*str == '\0')
1156 break;
1157
1158 str++;
1159 }
1160
1161 if (nums < 4)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001162 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001163
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001164 return EXACT_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001165}
1166
1167static enum match_type cmd_ipv4_prefix_match(const char *str)
1168{
1169 const char *sp;
1170 int dots = 0;
1171 char buf[4];
1172
1173 if (str == NULL)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001174 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001175
1176 for (;;) {
1177 memset(buf, 0, sizeof(buf));
1178 sp = str;
1179 while (*str != '\0' && *str != '/') {
1180 if (*str == '.') {
1181 if (dots == 3)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001182 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001183
1184 if (*(str + 1) == '.' || *(str + 1) == '/')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001185 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001186
1187 if (*(str + 1) == '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001188 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001189
1190 dots++;
1191 break;
1192 }
1193
1194 if (!isdigit((int)*str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001195 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001196
1197 str++;
1198 }
1199
1200 if (str - sp > 3)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001201 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001202
1203 strncpy(buf, sp, str - sp);
1204 if (atoi(buf) > 255)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001205 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001206
1207 if (dots == 3) {
1208 if (*str == '/') {
1209 if (*(str + 1) == '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001210 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001211
1212 str++;
1213 break;
1214 } else if (*str == '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001215 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001216 }
1217
1218 if (*str == '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001219 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001220
1221 str++;
1222 }
1223
1224 sp = str;
1225 while (*str != '\0') {
1226 if (!isdigit((int)*str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001227 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001228
1229 str++;
1230 }
1231
1232 if (atoi(sp) > 32)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001233 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001234
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001235 return EXACT_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001236}
1237
1238#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
1239#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
1240#define STATE_START 1
1241#define STATE_COLON 2
1242#define STATE_DOUBLE 3
1243#define STATE_ADDR 4
1244#define STATE_DOT 5
1245#define STATE_SLASH 6
1246#define STATE_MASK 7
1247
1248#ifdef HAVE_IPV6
1249
1250static enum match_type cmd_ipv6_match(const char *str)
1251{
1252 int state = STATE_START;
1253 int colons = 0, nums = 0, double_colon = 0;
1254 const char *sp = NULL;
1255 struct sockaddr_in6 sin6_dummy;
1256 int ret;
1257
1258 if (str == NULL)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001259 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001260
1261 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001262 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001263
1264 /* use inet_pton that has a better support,
1265 * for example inet_pton can support the automatic addresses:
1266 * ::1.2.3.4
1267 */
1268 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
1269
1270 if (ret == 1)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001271 return EXACT_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001272
1273 while (*str != '\0') {
1274 switch (state) {
1275 case STATE_START:
1276 if (*str == ':') {
1277 if (*(str + 1) != ':' && *(str + 1) != '\0')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001278 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001279 colons--;
1280 state = STATE_COLON;
1281 } else {
1282 sp = str;
1283 state = STATE_ADDR;
1284 }
1285
1286 continue;
1287 case STATE_COLON:
1288 colons++;
1289 if (*(str + 1) == ':')
1290 state = STATE_DOUBLE;
1291 else {
1292 sp = str + 1;
1293 state = STATE_ADDR;
1294 }
1295 break;
1296 case STATE_DOUBLE:
1297 if (double_colon)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001298 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001299
1300 if (*(str + 1) == ':')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001301 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001302 else {
1303 if (*(str + 1) != '\0')
1304 colons++;
1305 sp = str + 1;
1306 state = STATE_ADDR;
1307 }
1308
1309 double_colon++;
1310 nums++;
1311 break;
1312 case STATE_ADDR:
1313 if (*(str + 1) == ':' || *(str + 1) == '\0') {
1314 if (str - sp > 3)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001315 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001316
1317 nums++;
1318 state = STATE_COLON;
1319 }
1320 if (*(str + 1) == '.')
1321 state = STATE_DOT;
1322 break;
1323 case STATE_DOT:
1324 state = STATE_ADDR;
1325 break;
1326 default:
1327 break;
1328 }
1329
1330 if (nums > 8)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001331 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001332
1333 if (colons > 7)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001334 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001335
1336 str++;
1337 }
1338
1339#if 0
1340 if (nums < 11)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001341 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001342#endif /* 0 */
1343
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001344 return EXACT_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001345}
1346
1347static enum match_type cmd_ipv6_prefix_match(const char *str)
1348{
1349 int state = STATE_START;
1350 int colons = 0, nums = 0, double_colon = 0;
1351 int mask;
1352 const char *sp = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001353
1354 if (str == NULL)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001355 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001356
1357 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001358 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001359
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 Pedrolde890992019-06-12 12:27:32 +02001365 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001366 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 Pedrolde890992019-06-12 12:27:32 +02001377 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001378 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 Pedrolde890992019-06-12 12:27:32 +02001387 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001388
1389 if (*(str + 1) == ':')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001390 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001391 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 Pedrolde890992019-06-12 12:27:32 +02001409 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001410
1411 for (; sp <= str; sp++)
1412 if (*sp == '/')
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001413 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001414
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 Pedrolde890992019-06-12 12:27:32 +02001430 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001431
1432 state = STATE_MASK;
1433 break;
1434 default:
1435 break;
1436 }
1437
1438 if (nums > 11)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001439 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001440
1441 if (colons > 7)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001442 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001443
1444 str++;
1445 }
1446
1447 if (state < STATE_MASK)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001448 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001449
Neels Hofmeyr34907fe2021-09-05 19:50:34 +02001450 if (osmo_str_to_int(&mask, str, 10, 0, 128))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001451 return NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001452
1453/* I don't know why mask < 13 makes command match partly.
1454 Forgive me to make this comments. I Want to set static default route
1455 because of lack of function to originate default in ospf6d; sorry
1456 yasu
1457 if (mask < 13)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001458 return PARTLY_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001459*/
1460
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001461 return EXACT_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001462}
1463
1464#endif /* HAVE_IPV6 */
1465
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001466
1467#if ULONG_MAX == 18446744073709551615UL
1468#define DECIMAL_STRLEN_MAX_UNSIGNED 20
1469#elif ULONG_MAX == 4294967295UL
1470#define DECIMAL_STRLEN_MAX_UNSIGNED 10
1471#else
1472#error "ULONG_MAX not defined!"
1473#endif
1474
1475#if LONG_MAX == 9223372036854775807L
1476#define DECIMAL_STRLEN_MAX_SIGNED 19
1477#elif LONG_MAX == 2147483647L
1478#define DECIMAL_STRLEN_MAX_SIGNED 10
1479#else
1480#error "LONG_MAX not defined!"
1481#endif
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001482
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001483/* This function is aimed at quickly guessing & filtering the numeric base a
1484 * string can contain, by no means validates the entire value.
1485 * Returns 16 if string follows pattern "({+,-}0x[digits])"
1486 * Returns 10 if string follows pattern "({+,-}[digits])"
1487 * Returns a negative value if something other is detected (error)
1488*/
1489static int check_base(const char *str)
1490{
1491 const char *ptr = str;
1492 /* Skip any space */
1493 while (isspace(*ptr)) ptr++;
1494
1495 /* Skip optional sign: */
1496 if (*ptr == '+' || *ptr == '-')
1497 ptr++;
1498 if (*ptr == '0') {
1499 ptr++;
1500 if (*ptr == '\0' || isdigit(*ptr))
1501 return 10;
1502 if (!(*ptr == 'x' || *ptr == 'X'))
1503 return -1;
1504 ptr++;
1505 if (isxdigit(*ptr))
1506 return 16;
1507 return -1;
1508 }
1509 return 10;
1510}
1511
Philipp Maiereabc6fd2021-06-15 11:08:23 +02001512int vty_cmd_range_match(const char *range, const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001513{
1514 char *p;
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001515 char buf[DECIMAL_STRLEN_MAX_UNSIGNED + 1];
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001516 char *endptr = NULL;
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001517 int min_base, max_base, val_base;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001518
1519 if (str == NULL)
1520 return 1;
1521
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001522 if ((val_base = check_base(str)) < 0)
1523 return 0;
1524
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001525 if (range[1] == '-') {
1526 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001527
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001528 val = strtol(str, &endptr, val_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001529 if (*endptr != '\0')
1530 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001531
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001532 range += 2;
1533 p = strchr(range, '-');
1534 if (p == NULL)
1535 return 0;
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001536 if (p - range > DECIMAL_STRLEN_MAX_SIGNED)
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001537 return 0;
1538 strncpy(buf, range, p - range);
1539 buf[p - range] = '\0';
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001540 if ((min_base = check_base(buf)) < 0)
1541 return 0;
1542 min = -strtol(buf, &endptr, min_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001543 if (*endptr != '\0')
1544 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001545
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001546 range = p + 1;
1547 p = strchr(range, '>');
1548 if (p == NULL)
1549 return 0;
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001550 if (p - range > DECIMAL_STRLEN_MAX_SIGNED)
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001551 return 0;
1552 strncpy(buf, range, p - range);
1553 buf[p - range] = '\0';
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001554 if ((max_base = check_base(buf)) < 0)
1555 return 0;
1556 max = strtol(buf, &endptr, max_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001557 if (*endptr != '\0')
1558 return 0;
1559
1560 if (val < min || val > max)
1561 return 0;
1562 } else {
1563 unsigned long min, max, val;
1564
Pau Espin Pedrola1847012020-07-28 17:44:48 +02001565 if (str[0] == '-')
1566 return 0;
1567
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001568 val = strtoul(str, &endptr, val_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001569 if (*endptr != '\0')
1570 return 0;
1571
1572 range++;
1573 p = strchr(range, '-');
1574 if (p == NULL)
1575 return 0;
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001576 if (p - range > DECIMAL_STRLEN_MAX_UNSIGNED)
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001577 return 0;
1578 strncpy(buf, range, p - range);
1579 buf[p - range] = '\0';
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001580 if ((min_base = check_base(buf)) < 0)
1581 return 0;
1582 min = strtoul(buf, &endptr, min_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001583 if (*endptr != '\0')
1584 return 0;
1585
1586 range = p + 1;
1587 p = strchr(range, '>');
1588 if (p == NULL)
1589 return 0;
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +02001590 if (p - range > DECIMAL_STRLEN_MAX_UNSIGNED)
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001591 return 0;
1592 strncpy(buf, range, p - range);
1593 buf[p - range] = '\0';
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001594 if ((max_base = check_base(buf)) < 0)
1595 return 0;
1596 max = strtoul(buf, &endptr, max_base);
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001597 if (*endptr != '\0')
1598 return 0;
1599
1600 if (val < min || val > max)
1601 return 0;
1602 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001603
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +02001604 /* Don't allow ranges specified by min and max with different bases */
1605 if (min_base != max_base)
1606 return 0;
1607 /* arg value passed must match the base of the range */
1608 if (min_base != val_base)
1609 return 0;
1610
1611 /* Everything's fine */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001612 return 1;
1613}
1614
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001615/* helper to retrieve the 'real' argument string from an optional argument */
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001616static char *cmd_deopt(void *ctx, const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001617{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001618 /* we've got "[blah]". We want to strip off the []s and redo the
1619 * match check for "blah"
1620 */
1621 size_t len = strlen(str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001622
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001623 if (len < 3)
1624 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001625
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001626 return talloc_strndup(ctx, str + 1, len - 2);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001627}
1628
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001629static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001630cmd_match(const char *str, const char *command,
1631 enum match_type min, bool recur)
1632{
1633
1634 if (recur && CMD_OPTION(str))
1635 {
1636 enum match_type ret;
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001637 char *tmp = cmd_deopt(tall_vty_cmd_ctx, str);
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001638
1639 /* this would be a bug in a command, however handle it gracefully
1640 * as it we only discover it if a user tries to run it
1641 */
1642 if (tmp == NULL)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001643 return NO_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001644
1645 ret = cmd_match(tmp, command, min, false);
1646
1647 talloc_free(tmp);
1648
1649 return ret;
1650 }
1651 else if (CMD_VARARG(str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001652 return VARARG_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001653 else if (CMD_RANGE(str))
1654 {
Philipp Maiereabc6fd2021-06-15 11:08:23 +02001655 if (vty_cmd_range_match(str, command))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001656 return RANGE_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001657 }
1658#ifdef HAVE_IPV6
1659 else if (CMD_IPV6(str))
1660 {
1661 if (cmd_ipv6_match(command) >= min)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001662 return IPV6_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001663 }
1664 else if (CMD_IPV6_PREFIX(str))
1665 {
1666 if (cmd_ipv6_prefix_match(command) >= min)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001667 return IPV6_PREFIX_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001668 }
1669#endif /* HAVE_IPV6 */
1670 else if (CMD_IPV4(str))
1671 {
1672 if (cmd_ipv4_match(command) >= min)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001673 return IPV4_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001674 }
1675 else if (CMD_IPV4_PREFIX(str))
1676 {
1677 if (cmd_ipv4_prefix_match(command) >= min)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001678 return IPV4_PREFIX_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001679 }
1680 else if (CMD_VARIABLE(str))
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001681 return EXTEND_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001682 else if (strncmp(command, str, strlen(command)) == 0)
1683 {
1684 if (strcmp(command, str) == 0)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001685 return EXACT_MATCH;
1686 else if (PARTLY_MATCH >= min)
1687 return PARTLY_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001688 }
1689
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001690 return NO_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001691}
1692
1693/* Filter vector at the specified index and by the given command string, to
1694 * the desired matching level (thus allowing part matches), and return match
1695 * type flag.
1696 */
1697static enum match_type
1698cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001699{
1700 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001701 struct cmd_element *cmd_element;
1702 enum match_type match_type;
1703 vector descvec;
1704 struct desc *desc;
1705
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001706 match_type = NO_MATCH;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001707
1708 /* If command and cmd_element string does not match set NULL to vector */
1709 for (i = 0; i < vector_active(v); i++)
1710 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001711 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))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001722 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001723
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001724 ret = cmd_match (desc->cmd, command, level, true);
1725
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001726 if (ret != NO_MATCH)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001727 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001728
1729 if (match_type < ret)
1730 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001731 }
1732 if (!matched)
1733 vector_slot(v, i) = NULL;
1734 }
1735 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001736
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001737 if (match_type == NO_MATCH)
1738 return NO_MATCH;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001739
1740 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1741 * go again and filter out commands whose argument (at this index) is
1742 * 'weaker'. E.g., if we have 2 commands:
1743 *
1744 * foo bar <1-255>
1745 * foo bar BLAH
1746 *
1747 * and the command string is 'foo bar 10', then we will get here with with
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001748 * 'RANGE_MATCH' being the strongest match. However, if 'BLAH' came
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001749 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1750 *
1751 * If we don't do a 2nd pass and filter it out, the higher-layers will
1752 * consider this to be ambiguous.
1753 */
1754 for (i = 0; i < vector_active(v); i++)
1755 if ((cmd_element = vector_slot(v, i)) != NULL) {
1756 if (index >= vector_active(cmd_element->strvec))
1757 vector_slot(v, i) = NULL;
1758 else {
1759 unsigned int j;
1760 int matched = 0;
1761
1762 descvec =
1763 vector_slot(cmd_element->strvec, index);
1764
1765 for (j = 0; j < vector_active(descvec); j++)
1766 if ((desc = vector_slot(descvec, j))) {
1767 enum match_type ret;
1768
Pau Espin Pedrolde890992019-06-12 12:27:32 +02001769 ret = cmd_match(desc->cmd, command, ANY_MATCH, true);
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001770
1771 if (ret >= match_type)
1772 matched++;
1773 }
1774 if (!matched)
1775 vector_slot(v, i) = NULL;
1776 }
1777 }
1778
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001779 return match_type;
1780}
1781
1782/* Check ambiguous match */
1783static int
1784is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1785{
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001786 int ret = 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001787 unsigned int i;
1788 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001789 struct cmd_element *cmd_element;
1790 const char *matched = NULL;
1791 vector descvec;
1792 struct desc *desc;
1793
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001794 /* In this loop, when a match is found, 'matched' points to it. If on a later iteration, an
1795 * identical match is found, the command is ambiguous. The trickiness is that a string may be
1796 * enclosed in '[str]' square brackets, which get removed by a talloc_strndup(), via cmd_deopt().
1797 * Such a string is usually needed for one loop iteration, except when 'matched' points to it. In
1798 * that case, the string must remain allocated until this function exits or another match comes
1799 * around. This is sufficiently confusing to justify a separate talloc tree to store all of the
1800 * odd allocations, and to free them all at the end. We are not expecting too many optional args
1801 * or ambiguities to cause a noticeable memory footprint from keeping all allocations. */
1802 void *cmd_deopt_ctx = NULL;
1803
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001804 for (i = 0; i < vector_active(v); i++) {
1805 cmd_element = vector_slot(v, i);
1806 if (!cmd_element)
1807 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001808
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001809 int match = 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001810
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001811 descvec = vector_slot(cmd_element->strvec, index);
1812
1813 for (j = 0; j < vector_active(descvec); j++) {
1814 desc = vector_slot(descvec, j);
1815 if (!desc)
1816 continue;
1817
1818 enum match_type mtype;
1819 const char *str = desc->cmd;
1820
1821 if (CMD_OPTION(str)) {
1822 if (!cmd_deopt_ctx)
1823 cmd_deopt_ctx =
1824 talloc_named_const(tall_vty_cmd_ctx, 0,
1825 __func__);
1826 str = cmd_deopt(cmd_deopt_ctx, str);
1827 if (str == NULL)
Pau Espin Pedrol47425262019-06-11 21:04:09 +02001828 continue;
Pau Espin Pedrol47425262019-06-11 21:04:09 +02001829 }
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001830
1831 switch (type) {
1832 case EXACT_MATCH:
1833 if (!(CMD_VARIABLE (str))
1834 && strcmp(command, str) == 0)
1835 match++;
1836 break;
1837 case PARTLY_MATCH:
1838 if (!(CMD_VARIABLE (str))
1839 && strncmp(command, str, strlen (command)) == 0)
1840 {
1841 if (matched
1842 && strcmp(matched,
1843 str) != 0) {
1844 ret = 1; /* There is ambiguous match. */
1845 goto free_and_return;
1846 } else
1847 matched = str;
1848 match++;
1849 }
1850 break;
1851 case RANGE_MATCH:
Philipp Maiereabc6fd2021-06-15 11:08:23 +02001852 if (vty_cmd_range_match
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001853 (str, command)) {
1854 if (matched
1855 && strcmp(matched,
1856 str) != 0) {
1857 ret = 1;
1858 goto free_and_return;
1859 } else
1860 matched = str;
1861 match++;
1862 }
1863 break;
1864#ifdef HAVE_IPV6
1865 case IPV6_MATCH:
1866 if (CMD_IPV6(str))
1867 match++;
1868 break;
1869 case IPV6_PREFIX_MATCH:
1870 if ((mtype =
1871 cmd_ipv6_prefix_match
1872 (command)) != NO_MATCH) {
1873 if (mtype == PARTLY_MATCH) {
1874 ret = 2; /* There is incomplete match. */
1875 goto free_and_return;
1876 }
1877
1878 match++;
1879 }
1880 break;
1881#endif /* HAVE_IPV6 */
1882 case IPV4_MATCH:
1883 if (CMD_IPV4(str))
1884 match++;
1885 break;
1886 case IPV4_PREFIX_MATCH:
1887 if ((mtype =
1888 cmd_ipv4_prefix_match
1889 (command)) != NO_MATCH) {
1890 if (mtype == PARTLY_MATCH) {
1891 ret = 2; /* There is incomplete match. */
1892 goto free_and_return;
1893 }
1894
1895 match++;
1896 }
1897 break;
1898 case EXTEND_MATCH:
1899 if (CMD_VARIABLE (str))
1900 match++;
1901 break;
1902 case NO_MATCH:
1903 default:
1904 break;
1905 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001906 }
Pau Espin Pedrol274ac4d2019-06-12 13:06:41 +02001907 if (!match)
1908 vector_slot(v, i) = NULL;
1909 }
Neels Hofmeyr5314c512018-07-09 23:22:21 +02001910
1911free_and_return:
1912 if (cmd_deopt_ctx)
1913 talloc_free(cmd_deopt_ctx);
1914 return ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001915}
1916
1917/* If src matches dst return dst string, otherwise return NULL */
1918static const char *cmd_entry_function(const char *src, const char *dst)
1919{
1920 /* Skip variable arguments. */
1921 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1922 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1923 return NULL;
1924
1925 /* In case of 'command \t', given src is NULL string. */
1926 if (src == NULL)
1927 return dst;
1928
1929 /* Matched with input string. */
1930 if (strncmp(src, dst, strlen(src)) == 0)
1931 return dst;
1932
1933 return NULL;
1934}
1935
1936/* If src matches dst return dst string, otherwise return NULL */
1937/* This version will return the dst string always if it is
1938 CMD_VARIABLE for '?' key processing */
1939static const char *cmd_entry_function_desc(const char *src, const char *dst)
1940{
1941 if (CMD_VARARG(dst))
1942 return dst;
1943
1944 if (CMD_RANGE(dst)) {
Philipp Maiereabc6fd2021-06-15 11:08:23 +02001945 if (vty_cmd_range_match(dst, src))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001946 return dst;
1947 else
1948 return NULL;
1949 }
1950#ifdef HAVE_IPV6
1951 if (CMD_IPV6(dst)) {
1952 if (cmd_ipv6_match(src))
1953 return dst;
1954 else
1955 return NULL;
1956 }
1957
1958 if (CMD_IPV6_PREFIX(dst)) {
1959 if (cmd_ipv6_prefix_match(src))
1960 return dst;
1961 else
1962 return NULL;
1963 }
1964#endif /* HAVE_IPV6 */
1965
1966 if (CMD_IPV4(dst)) {
1967 if (cmd_ipv4_match(src))
1968 return dst;
1969 else
1970 return NULL;
1971 }
1972
1973 if (CMD_IPV4_PREFIX(dst)) {
1974 if (cmd_ipv4_prefix_match(src))
1975 return dst;
1976 else
1977 return NULL;
1978 }
1979
1980 /* Optional or variable commands always match on '?' */
1981 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1982 return dst;
1983
1984 /* In case of 'command \t', given src is NULL string. */
1985 if (src == NULL)
1986 return dst;
1987
1988 if (strncmp(src, dst, strlen(src)) == 0)
1989 return dst;
1990 else
1991 return NULL;
1992}
1993
1994/* Check same string element existence. If it isn't there return
1995 1. */
1996static int cmd_unique_string(vector v, const char *str)
1997{
1998 unsigned int i;
1999 char *match;
2000
2001 for (i = 0; i < vector_active(v); i++)
2002 if ((match = vector_slot(v, i)) != NULL)
2003 if (strcmp(match, str) == 0)
2004 return 0;
2005 return 1;
2006}
2007
2008/* Compare string to description vector. If there is same string
2009 return 1 else return 0. */
2010static int desc_unique_string(vector v, const char *str)
2011{
2012 unsigned int i;
2013 struct desc *desc;
2014
2015 for (i = 0; i < vector_active(v); i++)
2016 if ((desc = vector_slot(v, i)) != NULL)
2017 if (strcmp(desc->cmd, str) == 0)
2018 return 1;
2019 return 0;
2020}
2021
2022static int cmd_try_do_shortcut(enum node_type node, char *first_word)
2023{
2024 if (first_word != NULL &&
2025 node != AUTH_NODE &&
2026 node != VIEW_NODE &&
2027 node != AUTH_ENABLE_NODE &&
2028 node != ENABLE_NODE && 0 == strcmp("do", first_word))
2029 return 1;
2030 return 0;
2031}
2032
2033/* '?' describe command support. */
2034static vector
2035cmd_describe_command_real(vector vline, struct vty *vty, int *status)
2036{
2037 unsigned int i;
2038 vector cmd_vector;
2039#define INIT_MATCHVEC_SIZE 10
2040 vector matchvec;
2041 struct cmd_element *cmd_element;
2042 unsigned int index;
2043 int ret;
2044 enum match_type match;
2045 char *command;
2046 static struct desc desc_cr = { "<cr>", "" };
2047
2048 /* Set index. */
2049 if (vector_active(vline) == 0) {
2050 *status = CMD_ERR_NO_MATCH;
2051 return NULL;
2052 } else
2053 index = vector_active(vline) - 1;
2054
2055 /* Make copy vector of current node's command vector. */
2056 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2057
2058 /* Prepare match vector */
2059 matchvec = vector_init(INIT_MATCHVEC_SIZE);
2060
2061 /* Filter commands. */
2062 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01002063 for (i = 0; i < index; i++) {
2064 command = vector_slot(vline, i);
2065 if (!command)
2066 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002067
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002068 match = cmd_filter(command, cmd_vector, i, ANY_MATCH);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002069
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002070 if (match == VARARG_MATCH) {
Harald Welte80d30fe2013-02-12 11:08:57 +01002071 struct cmd_element *cmd_element;
2072 vector descvec;
2073 unsigned int j, k;
2074
2075 for (j = 0; j < vector_active(cmd_vector); j++)
2076 if ((cmd_element =
2077 vector_slot(cmd_vector, j)) != NULL
2078 &&
2079 (vector_active(cmd_element->strvec))) {
2080 descvec =
2081 vector_slot(cmd_element->
2082 strvec,
2083 vector_active
2084 (cmd_element->
2085 strvec) - 1);
2086 for (k = 0;
2087 k < vector_active(descvec);
2088 k++) {
2089 struct desc *desc =
2090 vector_slot(descvec,
2091 k);
2092 vector_set(matchvec,
2093 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002094 }
Harald Welte80d30fe2013-02-12 11:08:57 +01002095 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002096
Harald Welte80d30fe2013-02-12 11:08:57 +01002097 vector_set(matchvec, &desc_cr);
2098 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002099
Harald Welte80d30fe2013-02-12 11:08:57 +01002100 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002101 }
2102
Harald Welte80d30fe2013-02-12 11:08:57 +01002103 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
2104 match)) == 1) {
2105 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02002106 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01002107 *status = CMD_ERR_AMBIGUOUS;
2108 return NULL;
2109 } else if (ret == 2) {
2110 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02002111 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01002112 *status = CMD_ERR_NO_MATCH;
2113 return NULL;
2114 }
2115 }
2116
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002117 /* Prepare match vector */
2118 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
2119
2120 /* Make sure that cmd_vector is filtered based on current word */
2121 command = vector_slot(vline, index);
2122 if (command)
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002123 cmd_filter(command, cmd_vector, index, ANY_MATCH);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002124
2125 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01002126 for (i = 0; i < vector_active(cmd_vector); i++) {
2127 const char *string = NULL;
2128 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002129
Harald Welte80d30fe2013-02-12 11:08:57 +01002130 cmd_element = vector_slot(cmd_vector, i);
2131 if (!cmd_element)
2132 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002133
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07002134 if (cmd_element->attr & CMD_ATTR_DEPRECATED)
2135 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07002136 if (!vty->expert_mode && (cmd_element->attr & CMD_ATTR_HIDDEN))
Harald Welted17aa592013-02-12 11:11:34 +01002137 continue;
2138
Harald Welte80d30fe2013-02-12 11:08:57 +01002139 strvec = cmd_element->strvec;
2140
2141 /* if command is NULL, index may be equal to vector_active */
2142 if (command && index >= vector_active(strvec))
2143 vector_slot(cmd_vector, i) = NULL;
2144 else {
2145 /* Check if command is completed. */
2146 if (command == NULL
2147 && index == vector_active(strvec)) {
2148 string = "<cr>";
2149 if (!desc_unique_string(matchvec, string))
2150 vector_set(matchvec, &desc_cr);
2151 } else {
2152 unsigned int j;
2153 vector descvec = vector_slot(strvec, index);
2154 struct desc *desc;
2155
2156 for (j = 0; j < vector_active(descvec); j++) {
2157 desc = vector_slot(descvec, j);
2158 if (!desc)
2159 continue;
2160 string = cmd_entry_function_desc
2161 (command, desc->cmd);
2162 if (!string)
2163 continue;
2164 /* Uniqueness check */
2165 if (!desc_unique_string(matchvec, string))
2166 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002167 }
2168 }
2169 }
Harald Welte80d30fe2013-02-12 11:08:57 +01002170 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002171 vector_free(cmd_vector);
2172
2173 if (vector_slot(matchvec, 0) == NULL) {
2174 vector_free(matchvec);
2175 *status = CMD_ERR_NO_MATCH;
2176 } else
2177 *status = CMD_SUCCESS;
2178
2179 return matchvec;
2180}
2181
2182vector cmd_describe_command(vector vline, struct vty * vty, int *status)
2183{
2184 vector ret;
2185
2186 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2187 enum node_type onode;
2188 vector shifted_vline;
2189 unsigned int index;
2190
2191 onode = vty->node;
2192 vty->node = ENABLE_NODE;
2193 /* We can try it on enable node, cos' the vty is authenticated */
2194
2195 shifted_vline = vector_init(vector_count(vline));
2196 /* use memcpy? */
2197 for (index = 1; index < vector_active(vline); index++) {
2198 vector_set_index(shifted_vline, index - 1,
2199 vector_lookup(vline, index));
2200 }
2201
2202 ret = cmd_describe_command_real(shifted_vline, vty, status);
2203
2204 vector_free(shifted_vline);
2205 vty->node = onode;
2206 return ret;
2207 }
2208
2209 return cmd_describe_command_real(vline, vty, status);
2210}
2211
2212/* Check LCD of matched command. */
2213static int cmd_lcd(char **matched)
2214{
2215 int i;
2216 int j;
2217 int lcd = -1;
2218 char *s1, *s2;
2219 char c1, c2;
2220
2221 if (matched[0] == NULL || matched[1] == NULL)
2222 return 0;
2223
2224 for (i = 1; matched[i] != NULL; i++) {
2225 s1 = matched[i - 1];
2226 s2 = matched[i];
2227
2228 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
2229 if (c1 != c2)
2230 break;
2231
2232 if (lcd < 0)
2233 lcd = j;
2234 else {
2235 if (lcd > j)
2236 lcd = j;
2237 }
2238 }
2239 return lcd;
2240}
2241
2242/* Command line completion support. */
2243static char **cmd_complete_command_real(vector vline, struct vty *vty,
2244 int *status)
2245{
2246 unsigned int i;
2247 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2248#define INIT_MATCHVEC_SIZE 10
2249 vector matchvec;
2250 struct cmd_element *cmd_element;
2251 unsigned int index;
2252 char **match_str;
2253 struct desc *desc;
2254 vector descvec;
2255 char *command;
2256 int lcd;
2257
2258 if (vector_active(vline) == 0) {
2259 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02002260 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002261 return NULL;
2262 } else
2263 index = vector_active(vline) - 1;
2264
2265 /* First, filter by preceeding command string */
2266 for (i = 0; i < index; i++)
2267 if ((command = vector_slot(vline, i))) {
2268 enum match_type match;
2269 int ret;
2270
2271 /* First try completion match, if there is exactly match return 1 */
2272 match =
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002273 cmd_filter(command, cmd_vector, i, ANY_MATCH);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002274
2275 /* If there is exact match then filter ambiguous match else check
2276 ambiguousness. */
2277 if ((ret =
2278 is_cmd_ambiguous(command, cmd_vector, i,
2279 match)) == 1) {
2280 vector_free(cmd_vector);
2281 *status = CMD_ERR_AMBIGUOUS;
2282 return NULL;
2283 }
2284 /*
2285 else if (ret == 2)
2286 {
2287 vector_free (cmd_vector);
2288 *status = CMD_ERR_NO_MATCH;
2289 return NULL;
2290 }
2291 */
2292 }
2293
2294 /* Prepare match vector. */
2295 matchvec = vector_init(INIT_MATCHVEC_SIZE);
2296
2297 /* Now we got into completion */
2298 for (i = 0; i < vector_active(cmd_vector); i++)
2299 if ((cmd_element = vector_slot(cmd_vector, i))) {
2300 const char *string;
2301 vector strvec = cmd_element->strvec;
2302
2303 /* Check field length */
2304 if (index >= vector_active(strvec))
2305 vector_slot(cmd_vector, i) = NULL;
2306 else {
2307 unsigned int j;
2308
2309 descvec = vector_slot(strvec, index);
2310 for (j = 0; j < vector_active(descvec); j++)
2311 if ((desc = vector_slot(descvec, j))) {
Neels Hofmeyr9ea9dd02019-01-31 08:15:23 +01002312 const char *cmd = desc->cmd;
2313 char *tmp = NULL;
2314
2315 if (CMD_OPTION(desc->cmd)) {
2316 tmp = cmd_deopt(tall_vty_cmd_ctx, desc->cmd);
2317 cmd = tmp;
2318 }
2319 if ((string = cmd_entry_function(vector_slot(vline, index), cmd)))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002320 if (cmd_unique_string (matchvec, string))
2321 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
Neels Hofmeyr9ea9dd02019-01-31 08:15:23 +01002322 if (tmp)
2323 talloc_free(tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002324 }
2325 }
2326 }
2327
2328 /* We don't need cmd_vector any more. */
2329 vector_free(cmd_vector);
2330
2331 /* No matched command */
2332 if (vector_slot(matchvec, 0) == NULL) {
2333 vector_free(matchvec);
2334
2335 /* In case of 'command \t' pattern. Do you need '?' command at
2336 the end of the line. */
Pau Espin Pedrol0f7bcb52017-11-02 19:08:14 +01002337 if (vector_slot(vline, index) == NULL)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002338 *status = CMD_ERR_NOTHING_TODO;
2339 else
2340 *status = CMD_ERR_NO_MATCH;
2341 return NULL;
2342 }
2343
2344 /* Only one matched */
2345 if (vector_slot(matchvec, 1) == NULL) {
2346 match_str = (char **)matchvec->index;
2347 vector_only_wrapper_free(matchvec);
2348 *status = CMD_COMPLETE_FULL_MATCH;
2349 return match_str;
2350 }
2351 /* Make it sure last element is NULL. */
2352 vector_set(matchvec, NULL);
2353
2354 /* Check LCD of matched strings. */
2355 if (vector_slot(vline, index) != NULL) {
2356 lcd = cmd_lcd((char **)matchvec->index);
2357
2358 if (lcd) {
2359 int len = strlen(vector_slot(vline, index));
2360
2361 if (len < lcd) {
2362 char *lcdstr;
2363
2364 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
2365 "complete-lcdstr");
2366 memcpy(lcdstr, matchvec->index[0], lcd);
2367 lcdstr[lcd] = '\0';
2368
2369 /* match_str = (char **) &lcdstr; */
2370
2371 /* Free matchvec. */
2372 for (i = 0; i < vector_active(matchvec); i++) {
2373 if (vector_slot(matchvec, i))
2374 talloc_free(vector_slot(matchvec, i));
2375 }
2376 vector_free(matchvec);
2377
2378 /* Make new matchvec. */
2379 matchvec = vector_init(INIT_MATCHVEC_SIZE);
2380 vector_set(matchvec, lcdstr);
2381 match_str = (char **)matchvec->index;
2382 vector_only_wrapper_free(matchvec);
2383
2384 *status = CMD_COMPLETE_MATCH;
2385 return match_str;
2386 }
2387 }
2388 }
2389
2390 match_str = (char **)matchvec->index;
2391 vector_only_wrapper_free(matchvec);
2392 *status = CMD_COMPLETE_LIST_MATCH;
2393 return match_str;
2394}
2395
2396char **cmd_complete_command(vector vline, struct vty *vty, int *status)
2397{
2398 char **ret;
2399
2400 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2401 enum node_type onode;
2402 vector shifted_vline;
2403 unsigned int index;
2404
2405 onode = vty->node;
2406 vty->node = ENABLE_NODE;
2407 /* We can try it on enable node, cos' the vty is authenticated */
2408
2409 shifted_vline = vector_init(vector_count(vline));
2410 /* use memcpy? */
2411 for (index = 1; index < vector_active(vline); index++) {
2412 vector_set_index(shifted_vline, index - 1,
2413 vector_lookup(vline, index));
2414 }
2415
2416 ret = cmd_complete_command_real(shifted_vline, vty, status);
2417
2418 vector_free(shifted_vline);
2419 vty->node = onode;
2420 return ret;
2421 }
2422
2423 return cmd_complete_command_real(vline, vty, status);
2424}
2425
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002426static struct vty_parent_node *vty_parent(struct vty *vty)
2427{
2428 return llist_first_entry_or_null(&vty->parent_nodes,
2429 struct vty_parent_node,
2430 entry);
2431}
2432
2433static bool vty_pop_parent(struct vty *vty)
2434{
2435 struct vty_parent_node *parent = vty_parent(vty);
2436 if (!parent)
2437 return false;
2438 llist_del(&parent->entry);
2439 vty->node = parent->node;
2440 vty->priv = parent->priv;
2441 if (vty->indent)
2442 talloc_free(vty->indent);
2443 vty->indent = parent->indent;
2444 talloc_free(parent);
2445 return true;
2446}
2447
2448static void vty_clear_parents(struct vty *vty)
2449{
2450 while (vty_pop_parent(vty));
2451}
2452
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002453/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002454/*
2455 * This function MUST eventually converge on a node when called repeatedly,
2456 * there must not be any cycles.
2457 * All 'config' nodes shall converge on CONFIG_NODE.
2458 * All other 'enable' nodes shall converge on ENABLE_NODE.
2459 * All 'view' only nodes shall converge on VIEW_NODE.
2460 * All other nodes shall converge on themselves or it must be ensured,
2461 * that the user's rights are not extended anyhow by calling this function.
2462 *
2463 * Note that these requirements also apply to all functions that are used
2464 * as go_parent_cb.
2465 * Note also that this function relies on the is_config_child callback to
2466 * recognize non-config nodes if go_parent_cb is not set.
2467 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00002468int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002469{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002470 switch (vty->node) {
Vadim Yanitskiyb0bbd722020-08-15 22:59:46 +07002471 case AUTH_NODE:
2472 case VIEW_NODE:
2473 case ENABLE_NODE:
2474 case CONFIG_NODE:
2475 vty_clear_parents(vty);
2476 break;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002477
Vadim Yanitskiyb0bbd722020-08-15 22:59:46 +07002478 case AUTH_ENABLE_NODE:
2479 vty->node = VIEW_NODE;
2480 vty_clear_parents(vty);
2481 break;
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002482
Vadim Yanitskiyb0bbd722020-08-15 22:59:46 +07002483 default:
2484 if (host.app_info->go_parent_cb)
2485 host.app_info->go_parent_cb(vty);
2486 vty_pop_parent(vty);
2487 break;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002488 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002489
2490 return vty->node;
2491}
2492
2493/* Execute command by argument vline vector. */
2494static int
2495cmd_execute_command_real(vector vline, struct vty *vty,
2496 struct cmd_element **cmd)
2497{
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002498 unsigned int i, j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002499 unsigned int index;
2500 vector cmd_vector;
2501 struct cmd_element *cmd_element;
2502 struct cmd_element *matched_element;
2503 unsigned int matched_count, incomplete_count;
2504 int argc;
2505 const char *argv[CMD_ARGC_MAX];
2506 enum match_type match = 0;
2507 int varflag;
2508 char *command;
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002509 int rc;
2510 /* Used for temporary storage of cmd_deopt() allocated arguments during
2511 argv[] generation */
2512 void *cmd_deopt_ctx = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002513
2514 /* Make copy of command elements. */
2515 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2516
Vadim Yanitskiyf5781c92019-06-01 00:27:22 +07002517 for (index = 0; index < vector_active(vline); index++) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002518 if ((command = vector_slot(vline, index))) {
2519 int ret;
2520
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002521 match = cmd_filter(command, cmd_vector, index,
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002522 ANY_MATCH);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002523
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002524 if (match == VARARG_MATCH)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002525 break;
2526
2527 ret =
2528 is_cmd_ambiguous(command, cmd_vector, index, match);
2529
2530 if (ret == 1) {
2531 vector_free(cmd_vector);
2532 return CMD_ERR_AMBIGUOUS;
2533 } else if (ret == 2) {
2534 vector_free(cmd_vector);
2535 return CMD_ERR_NO_MATCH;
2536 }
2537 }
Vadim Yanitskiyf5781c92019-06-01 00:27:22 +07002538 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002539
2540 /* Check matched count. */
2541 matched_element = NULL;
2542 matched_count = 0;
2543 incomplete_count = 0;
2544
Vadim Yanitskiyf5781c92019-06-01 00:27:22 +07002545 for (i = 0; i < vector_active(cmd_vector); i++) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002546 if ((cmd_element = vector_slot(cmd_vector, i))) {
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002547 if (match == VARARG_MATCH
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002548 || index >= cmd_element->cmdsize) {
2549 matched_element = cmd_element;
2550#if 0
2551 printf("DEBUG: %s\n", cmd_element->string);
2552#endif
2553 matched_count++;
2554 } else {
2555 incomplete_count++;
2556 }
2557 }
Vadim Yanitskiyf5781c92019-06-01 00:27:22 +07002558 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002559
2560 /* Finish of using cmd_vector. */
2561 vector_free(cmd_vector);
2562
2563 /* To execute command, matched_count must be 1. */
2564 if (matched_count == 0) {
2565 if (incomplete_count)
2566 return CMD_ERR_INCOMPLETE;
2567 else
2568 return CMD_ERR_NO_MATCH;
2569 }
2570
2571 if (matched_count > 1)
2572 return CMD_ERR_AMBIGUOUS;
2573
2574 /* Argument treatment */
2575 varflag = 0;
2576 argc = 0;
2577
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002578 cmd_deopt_ctx = talloc_named_const(tall_vty_cmd_ctx, 0, __func__);
2579
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002580 for (i = 0; i < vector_active(vline); i++) {
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002581 if (argc == CMD_ARGC_MAX) {
2582 rc = CMD_ERR_EXEED_ARGC_MAX;
2583 goto rc_free_deopt_ctx;
2584 }
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002585 if (varflag) {
2586 argv[argc++] = vector_slot(vline, i);
2587 continue;
2588 }
2589
2590 vector descvec = vector_slot(matched_element->strvec, i);
Pau Espin Pedrol32e67102019-06-12 18:35:46 +02002591 const char *tmp_cmd;
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002592
2593 if (vector_active(descvec) == 1) {
Pau Espin Pedrol32e67102019-06-12 18:35:46 +02002594 /* Single coice argument, no "(...|...)". */
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002595 struct desc *desc = vector_slot(descvec, 0);
2596
Pau Espin Pedrol32e67102019-06-12 18:35:46 +02002597 if (CMD_OPTION(desc->cmd)) {
2598 /* we need to first remove the [] chars, then check to see what's inside (var or token) */
2599 tmp_cmd = cmd_deopt(cmd_deopt_ctx, desc->cmd);
2600 } else {
2601 tmp_cmd = desc->cmd;
2602 }
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002603
Pau Espin Pedrol32e67102019-06-12 18:35:46 +02002604 if (CMD_VARARG(tmp_cmd))
2605 varflag = 1;
2606 if (varflag || CMD_VARIABLE(tmp_cmd))
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002607 argv[argc++] = vector_slot(vline, i);
Pau Espin Pedrol32e67102019-06-12 18:35:46 +02002608 else if (CMD_OPTION(desc->cmd))
2609 argv[argc++] = tmp_cmd;
2610 /* else : we don't want to add non-opt single-choice static args in argv[] */
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002611 } else {
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002612 /* multi choice argument. look up which choice
2613 the user meant (can only be one after
2614 filtering and checking for ambigous). For instance,
2615 if user typed "th" for "(two|three)" arg, we
2616 want to pass "three" in argv[]. */
2617 for (j = 0; j < vector_active(descvec); j++) {
2618 struct desc *desc = vector_slot(descvec, j);
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002619 if (!desc)
2620 continue;
2621 if (cmd_match(desc->cmd, vector_slot(vline, i), ANY_MATCH, true) == NO_MATCH)
2622 continue;
2623 if (CMD_OPTION(desc->cmd)) {
2624 /* we need to first remove the [] chars, then check to see what's inside (var or token) */
2625 tmp_cmd = cmd_deopt(cmd_deopt_ctx, desc->cmd);
2626 } else {
2627 tmp_cmd = desc->cmd;
2628 }
2629
2630 if(CMD_VARIABLE(tmp_cmd)) {
2631 argv[argc++] = vector_slot(vline, i);
2632 } else {
2633 argv[argc++] = tmp_cmd;
2634 }
2635 break;
2636 }
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002637 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002638 }
2639
2640 /* For vtysh execution. */
2641 if (cmd)
2642 *cmd = matched_element;
2643
2644 if (matched_element->daemon)
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002645 rc = CMD_SUCCESS_DAEMON;
Neels Hofmeyrd31de232019-10-31 16:09:23 +01002646 else {
2647 /* Execute matched command. */
2648 struct vty_parent_node this_node = {
2649 .node = vty->node,
2650 .priv = vty->priv,
2651 .indent = vty->indent,
2652 };
2653 struct vty_parent_node *parent = vty_parent(vty);
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002654 rc = (*matched_element->func) (matched_element, vty, argc, argv);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002655
Neels Hofmeyrd31de232019-10-31 16:09:23 +01002656 /* If we have stepped down into a child node, push a parent frame.
2657 * The causality is such: we don't expect every single node entry implementation to push
2658 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2659 * a parent node. Hence if the node changed without the parent node changing, we must
2660 * have stepped into a child node. */
2661 if (vty->node != this_node.node && parent == vty_parent(vty)
2662 && vty->node > CONFIG_NODE) {
2663 /* Push the parent node. */
2664 parent = talloc_zero(vty, struct vty_parent_node);
2665 *parent = this_node;
2666 llist_add(&parent->entry, &vty->parent_nodes);
2667 }
2668 }
2669
Pau Espin Pedrol7e1b03f2019-06-12 14:47:48 +02002670rc_free_deopt_ctx:
2671 /* Now after we called the command func, we can free temporary strings */
2672 talloc_free(cmd_deopt_ctx);
2673 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002674}
2675
2676int
2677cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2678 int vtysh)
2679{
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002680 int ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002681 enum node_type onode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002682
2683 onode = vty->node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002684
2685 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2686 vector shifted_vline;
2687 unsigned int index;
2688
2689 vty->node = ENABLE_NODE;
2690 /* We can try it on enable node, cos' the vty is authenticated */
2691
2692 shifted_vline = vector_init(vector_count(vline));
2693 /* use memcpy? */
2694 for (index = 1; index < vector_active(vline); index++) {
2695 vector_set_index(shifted_vline, index - 1,
2696 vector_lookup(vline, index));
2697 }
2698
2699 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2700
2701 vector_free(shifted_vline);
2702 vty->node = onode;
2703 return ret;
2704 }
2705
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002706 return cmd_execute_command_real(vline, vty, cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002707}
2708
2709/* Execute command by argument readline. */
2710int
2711cmd_execute_command_strict(vector vline, struct vty *vty,
2712 struct cmd_element **cmd)
2713{
2714 unsigned int i;
2715 unsigned int index;
2716 vector cmd_vector;
2717 struct cmd_element *cmd_element;
2718 struct cmd_element *matched_element;
2719 unsigned int matched_count, incomplete_count;
2720 int argc;
2721 const char *argv[CMD_ARGC_MAX];
2722 int varflag;
2723 enum match_type match = 0;
2724 char *command;
2725
2726 /* Make copy of command element */
2727 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2728
2729 for (index = 0; index < vector_active(vline); index++)
2730 if ((command = vector_slot(vline, index))) {
2731 int ret;
2732
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002733 match = cmd_filter(vector_slot(vline, index),
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002734 cmd_vector, index, EXACT_MATCH);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002735
2736 /* If command meets '.VARARG' then finish matching. */
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002737 if (match == VARARG_MATCH)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002738 break;
2739
2740 ret =
2741 is_cmd_ambiguous(command, cmd_vector, index, match);
2742 if (ret == 1) {
2743 vector_free(cmd_vector);
2744 return CMD_ERR_AMBIGUOUS;
2745 }
2746 if (ret == 2) {
2747 vector_free(cmd_vector);
2748 return CMD_ERR_NO_MATCH;
2749 }
2750 }
2751
2752 /* Check matched count. */
2753 matched_element = NULL;
2754 matched_count = 0;
2755 incomplete_count = 0;
2756 for (i = 0; i < vector_active(cmd_vector); i++)
2757 if (vector_slot(cmd_vector, i) != NULL) {
2758 cmd_element = vector_slot(cmd_vector, i);
2759
Pau Espin Pedrolde890992019-06-12 12:27:32 +02002760 if (match == VARARG_MATCH
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002761 || index >= cmd_element->cmdsize) {
2762 matched_element = cmd_element;
2763 matched_count++;
2764 } else
2765 incomplete_count++;
2766 }
2767
2768 /* Finish of using cmd_vector. */
2769 vector_free(cmd_vector);
2770
2771 /* To execute command, matched_count must be 1. */
2772 if (matched_count == 0) {
2773 if (incomplete_count)
2774 return CMD_ERR_INCOMPLETE;
2775 else
2776 return CMD_ERR_NO_MATCH;
2777 }
2778
2779 if (matched_count > 1)
2780 return CMD_ERR_AMBIGUOUS;
2781
2782 /* Argument treatment */
2783 varflag = 0;
2784 argc = 0;
2785
2786 for (i = 0; i < vector_active(vline); i++) {
Pau Espin Pedrolc17c6d62019-06-14 12:38:42 +02002787 if (argc == CMD_ARGC_MAX)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002788 return CMD_ERR_EXEED_ARGC_MAX;
Pau Espin Pedrol8930ace2019-06-12 17:51:34 +02002789 if (varflag) {
2790 argv[argc++] = vector_slot(vline, i);
2791 continue;
2792 }
2793
2794 vector descvec = vector_slot(matched_element->strvec, i);
2795
2796 if (vector_active(descvec) == 1) {
2797 struct desc *desc = vector_slot(descvec, 0);
2798
2799 if (CMD_VARARG(desc->cmd))
2800 varflag = 1;
2801
2802 if (varflag || CMD_VARIABLE(desc->cmd)
2803 || CMD_OPTION(desc->cmd))
2804 argv[argc++] = vector_slot(vline, i);
2805 } else {
2806 argv[argc++] = vector_slot(vline, i);
2807 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002808 }
2809
2810 /* For vtysh execution. */
2811 if (cmd)
2812 *cmd = matched_element;
2813
2814 if (matched_element->daemon)
2815 return CMD_SUCCESS_DAEMON;
2816
2817 /* Now execute matched command */
2818 return (*matched_element->func) (matched_element, vty, argc, argv);
2819}
2820
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002821static inline size_t len(const char *str)
2822{
2823 return str? strlen(str) : 0;
2824}
2825
Neels Hofmeyr00b5ed32017-09-20 00:46:03 +02002826/*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a
2827 * is longer than b, a must start with exactly b, and vice versa.
2828 * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b.
2829 */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002830static int indent_cmp(const char *a, const char *b)
2831{
2832 size_t al, bl;
2833 al = len(a);
2834 bl = len(b);
2835 if (al > bl) {
2836 if (bl && strncmp(a, b, bl) != 0)
2837 return EINVAL;
2838 return 1;
2839 }
2840 /* al <= bl */
2841 if (al && strncmp(a, b, al) != 0)
2842 return EINVAL;
2843 return (al < bl)? -1 : 0;
2844}
2845
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002846/* Configration make from file. */
2847int config_from_file(struct vty *vty, FILE * fp)
2848{
2849 int ret;
2850 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002851 char *indent;
2852 int cmp;
2853 struct vty_parent_node this_node;
2854 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002855
2856 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002857 indent = NULL;
2858 vline = NULL;
2859 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002860
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002861 if (ret != CMD_SUCCESS)
2862 goto return_invalid_indent;
2863
2864 /* In case of comment or empty line */
2865 if (vline == NULL) {
2866 if (indent) {
2867 talloc_free(indent);
2868 indent = NULL;
2869 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002870 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002871 }
2872
Neels Hofmeyr43063632017-09-19 23:54:01 +02002873 /* We have a nonempty line. */
2874 if (!vty->indent) {
2875 /* We have just entered a node and expecting the first child to come up; but we
2876 * may also skip right back to a parent or ancestor level. */
2877 parent = vty_parent(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002878
Neels Hofmeyr43063632017-09-19 23:54:01 +02002879 /* If there is no parent, record any indentation we encounter. */
2880 cmp = parent ? indent_cmp(indent, parent->indent) : 1;
2881
2882 if (cmp == EINVAL)
2883 goto return_invalid_indent;
2884
2885 if (cmp <= 0) {
2886 /* We have gone right back to the parent level or higher, we are skipping
2887 * this child node level entirely. Pop the parent to go back to a node
2888 * that was actually there (to reinstate vty->indent) and re-use below
2889 * go-parent while-loop to find an accurate match of indent in the node
2890 * ancestry. */
2891 vty_go_parent(vty);
2892 } else {
2893 /* The indent is deeper than the just entered parent, record the new
2894 * indentation characters. */
2895 vty->indent = talloc_strdup(vty, indent);
2896 /* This *is* the new indentation. */
2897 cmp = 0;
2898 }
2899 } else {
2900 /* There is a known indentation for this node level, validate and detect node
2901 * exits. */
2902 cmp = indent_cmp(indent, vty->indent);
2903 if (cmp == EINVAL)
2904 goto return_invalid_indent;
2905 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002906
2907 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2908 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2909 while (cmp < 0) {
2910 vty_go_parent(vty);
2911 cmp = indent_cmp(indent, vty->indent);
2912 if (cmp == EINVAL)
2913 goto return_invalid_indent;
2914 }
2915
2916 /* More indent without having entered a child node level? Either the parent node's indent
2917 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2918 * or the indentation increased even though the vty command didn't enter a child. */
2919 if (cmp > 0)
2920 goto return_invalid_indent;
2921
2922 /* Remember the current node before the command possibly changes it. */
2923 this_node = (struct vty_parent_node){
2924 .node = vty->node,
2925 .priv = vty->priv,
2926 .indent = vty->indent,
2927 };
2928
2929 parent = vty_parent(vty);
2930 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002931 cmd_free_strvec(vline);
2932
Pau Espin Pedrola0c81952019-10-22 18:38:01 +02002933 if (ret != CMD_SUCCESS && ret != CMD_ERR_NOTHING_TODO) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002934 if (indent) {
2935 talloc_free(indent);
2936 indent = NULL;
2937 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002938 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002939 }
2940
2941 /* If we have stepped down into a child node, push a parent frame.
2942 * The causality is such: we don't expect every single node entry implementation to push
2943 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2944 * a parent node. Hence if the node changed without the parent node changing, we must
2945 * have stepped into a child node (and now expect a deeper indent). */
2946 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2947 /* Push the parent node. */
2948 parent = talloc_zero(vty, struct vty_parent_node);
2949 *parent = this_node;
2950 llist_add(&parent->entry, &vty->parent_nodes);
2951
2952 /* The current talloc'ed vty->indent string will now be owned by this parent
2953 * struct. Indicate that we don't know what deeper indent characters the user
2954 * will choose. */
2955 vty->indent = NULL;
2956 }
2957
2958 if (indent) {
2959 talloc_free(indent);
2960 indent = NULL;
2961 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002962 }
Pau Espin Pedrol0fd0fe62019-10-11 14:10:31 +02002963 /* Make sure we call go_parent_cb for all remaining indent levels at the end of file */
2964 while (vty_parent(vty))
2965 vty_go_parent(vty);
2966
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002967 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002968
2969return_invalid_indent:
2970 if (vline)
2971 cmd_free_strvec(vline);
2972 if (indent) {
2973 talloc_free(indent);
2974 indent = NULL;
2975 }
2976 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002977}
2978
2979/* Configration from terminal */
2980DEFUN(config_terminal,
2981 config_terminal_cmd,
2982 "configure terminal",
2983 "Configuration from vty interface\n" "Configuration terminal\n")
2984{
2985 if (vty_config_lock(vty))
2986 vty->node = CONFIG_NODE;
2987 else {
2988 vty_out(vty, "VTY configuration is locked by other VTY%s",
2989 VTY_NEWLINE);
2990 return CMD_WARNING;
2991 }
2992 return CMD_SUCCESS;
2993}
2994
2995/* Enable command */
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07002996DEFUN(enable, config_enable_cmd,
2997 "enable [expert-mode]",
2998 "Turn on privileged mode command\n"
2999 "Enable the expert mode (show hidden commands)\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003000{
3001 /* If enable password is NULL, change to ENABLE_NODE */
3002 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
3003 vty->type == VTY_SHELL_SERV)
3004 vty->node = ENABLE_NODE;
3005 else
3006 vty->node = AUTH_ENABLE_NODE;
3007
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003008 vty->expert_mode = argc > 0;
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003009
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003010 return CMD_SUCCESS;
3011}
3012
3013/* Disable command */
3014DEFUN(disable,
3015 config_disable_cmd, "disable", "Turn off privileged mode command\n")
3016{
3017 if (vty->node == ENABLE_NODE)
3018 vty->node = VIEW_NODE;
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003019
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003020 vty->expert_mode = false;
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003021
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003022 return CMD_SUCCESS;
3023}
3024
3025/* Down vty node level. */
3026gDEFUN(config_exit,
3027 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
3028{
3029 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02003030 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003031 case VIEW_NODE:
3032 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01003033 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003034 break;
3035 case CONFIG_NODE:
3036 vty->node = ENABLE_NODE;
3037 vty_config_unlock(vty);
3038 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003039 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02003040 if (vty->node > CONFIG_NODE)
3041 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003042 break;
3043 }
3044 return CMD_SUCCESS;
3045}
3046
3047/* End of configuration. */
3048 gDEFUN(config_end,
3049 config_end_cmd, "end", "End current mode and change to enable mode.")
3050{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02003051 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02003052 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02003053
3054 /* Repeatedly call go_parent until a top node is reached. */
3055 while (vty->node > CONFIG_NODE) {
3056 if (vty->node == last_node) {
3057 /* Ensure termination, this shouldn't happen. */
3058 break;
3059 }
3060 last_node = vty->node;
3061 vty_go_parent(vty);
3062 }
3063
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003064 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02003065 if (vty->node > ENABLE_NODE)
3066 vty->node = ENABLE_NODE;
3067 vty->index = NULL;
3068 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003069 }
3070 return CMD_SUCCESS;
3071}
3072
Oliver Smith8a482fd2021-07-12 18:18:51 +02003073DEFUN(shutdown,
3074 shutdown_cmd, "shutdown", "Request a shutdown of the program\n")
3075{
3076 LOGP(DLGLOBAL, LOGL_INFO, "Shutdown requested from telnet\n");
3077 vty_out(vty, "%s is shutting down. Bye!%s", host.app_info->name, VTY_NEWLINE);
3078
3079 /* Same exit path as if it was killed by the service manager */
3080 kill(getpid(), SIGTERM);
3081
3082 return CMD_SUCCESS;
3083}
3084
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003085/* Show version. */
3086DEFUN(show_version,
3087 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
3088{
Harald Welte237f6242010-05-25 23:00:45 +02003089 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
3090 host.app_info->version,
3091 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
3092 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003093
3094 return CMD_SUCCESS;
3095}
3096
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003097DEFUN(show_online_help,
3098 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
3099{
Neels Hofmeyrcf70aa02020-05-14 16:51:26 +02003100 vty_dump_xml_ref_to_vty(vty);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003101 return CMD_SUCCESS;
3102}
3103
Oliver Smith0c78bc62021-07-12 17:28:36 +02003104DEFUN(show_pid,
3105 show_pid_cmd, "show pid", SHOW_STR "Displays the process ID\n")
3106{
3107 vty_out(vty, "%d%s", getpid(), VTY_NEWLINE);
3108 return CMD_SUCCESS;
3109}
3110
Oliver Smithd243c2a2021-07-09 17:19:32 +02003111DEFUN(show_uptime,
3112 show_uptime_cmd, "show uptime", SHOW_STR "Displays how long the program has been running\n")
3113{
Alexander Couzens06929162021-09-05 23:08:59 +02003114 vty_out(vty, "%s has been running for ", host.app_info->name);
3115 vty_out_uptime(vty, &starttime);
3116 vty_out_newline(vty);
Oliver Smithd243c2a2021-07-09 17:19:32 +02003117
3118 return CMD_SUCCESS;
3119}
3120
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003121/* Help display function for all node. */
3122gDEFUN(config_help,
3123 config_help_cmd, "help", "Description of the interactive help system\n")
3124{
Vadim Yanitskiy1d7ae152020-08-12 16:52:35 +07003125 vty_out(vty, "This VTY provides advanced help features. When you need help,%s"
3126 "anytime at the command line please press '?'.%s%s"
3127 "If nothing matches, the help list will be empty and you must backup%s"
3128 " until entering a '?' shows the available options.%s"
3129 "Two styles of help are provided:%s"
3130 "1. Full help is available when you are ready to enter a%s"
3131 "command argument (e.g. 'show ?') and describes each possible%s"
3132 "argument.%s"
3133 "2. Partial help is provided when an abbreviated argument is entered%s"
3134 " and you want to know what arguments match the input%s"
3135 " (e.g. 'show me?'.)%s%s",
3136 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
3137 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
3138 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
3139 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003140 return CMD_SUCCESS;
3141}
3142
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07003143enum {
3144 ATTR_TYPE_GLOBAL = (1 << 0),
3145 ATTR_TYPE_LIB = (1 << 1),
3146 ATTR_TYPE_APP = (1 << 2),
3147};
3148
3149static void print_attr_list(struct vty *vty, unsigned int attr_mask)
3150{
3151 const char *desc;
3152 unsigned int i;
3153 bool found;
3154 char flag;
3155
3156 if (attr_mask & ATTR_TYPE_GLOBAL) {
3157 vty_out(vty, " Global attributes:%s", VTY_NEWLINE);
3158
3159 for (i = 0; i < ARRAY_SIZE(cmd_attr_desc) - 1; i++) {
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003160 flag = cmd_attr_get_flag(cmd_attr_desc[i].value);
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07003161 desc = cmd_attr_desc[i].str;
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003162
3163 /* Skip attributes without flags */
3164 if (flag != '.')
3165 vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE);
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07003166 }
3167 }
3168
3169 if (attr_mask & ATTR_TYPE_LIB) {
3170 vty_out(vty, " Library specific attributes:%s", VTY_NEWLINE);
3171
3172 for (i = 0, found = false; i < _OSMO_CORE_LIB_ATTR_COUNT; i++) {
3173 if ((desc = cmd_lib_attr_desc[i]) == NULL)
3174 continue;
3175 found = true;
3176
3177 flag = cmd_lib_attr_letters[i];
3178 if (flag == '\0')
3179 flag = '.';
3180
3181 vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE);
3182 }
3183
3184 if (!found)
3185 vty_out(vty, " (no attributes)%s", VTY_NEWLINE);
3186 }
3187
3188 if (attr_mask & ATTR_TYPE_APP) {
3189 vty_out(vty, " Application specific attributes:%s", VTY_NEWLINE);
3190
3191 for (i = 0, found = false; i < VTY_CMD_USR_ATTR_NUM; i++) {
3192 if ((desc = host.app_info->usr_attr_desc[i]) == NULL)
3193 continue;
3194 found = true;
3195
3196 flag = host.app_info->usr_attr_letters[i];
3197 if (flag == '\0')
3198 flag = '.';
3199
3200 vty_out(vty, " %c %s%s", flag, desc, VTY_NEWLINE);
3201 }
3202
3203 if (!found)
3204 vty_out(vty, " (no attributes)%s", VTY_NEWLINE);
3205 }
3206}
3207
3208gDEFUN(show_vty_attr_all, show_vty_attr_all_cmd,
3209 "show vty-attributes",
3210 SHOW_STR "List of VTY attributes\n")
3211{
3212 print_attr_list(vty, 0xff);
3213 return CMD_SUCCESS;
3214}
3215
3216gDEFUN(show_vty_attr, show_vty_attr_cmd,
3217 "show vty-attributes (application|library|global)",
3218 SHOW_STR "List of VTY attributes\n"
3219 "Application specific attributes only\n"
3220 "Library specific attributes only\n"
3221 "Global attributes only\n")
3222{
3223 unsigned int attr_mask = 0;
3224
3225 if (argv[0][0] == 'g') /* global */
3226 attr_mask |= ATTR_TYPE_GLOBAL;
3227 else if (argv[0][0] == 'l') /* library */
3228 attr_mask |= ATTR_TYPE_LIB;
3229 else if (argv[0][0] == 'a') /* application */
3230 attr_mask |= ATTR_TYPE_APP;
3231
3232 print_attr_list(vty, attr_mask);
3233 return CMD_SUCCESS;
3234}
3235
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003236/* Compose flag bit-mask for all commands within the given node */
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003237static unsigned int node_flag_mask(const struct cmd_node *cnode, bool expert_mode)
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003238{
3239 unsigned int flag_mask = 0x00;
3240 unsigned int f, i;
3241
3242 for (f = 0; f < VTY_CMD_USR_ATTR_NUM; f++) {
3243 for (i = 0; i < vector_active(cnode->cmd_vector); i++) {
3244 const struct cmd_element *cmd;
3245 char flag_letter;
3246
3247 if ((cmd = vector_slot(cnode->cmd_vector, i)) == NULL)
3248 continue;
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003249 if (cmd->attr & CMD_ATTR_DEPRECATED)
3250 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003251 if (!expert_mode && (cmd->attr & CMD_ATTR_HIDDEN))
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003252 continue;
Pau Espin Pedrol715a6122020-10-12 15:47:05 +02003253 if (~cmd->usrattr & ((unsigned)1 << f))
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003254 continue;
3255
3256 if (cmd->attr & CMD_ATTR_LIB_COMMAND)
3257 flag_letter = cmd_lib_attr_letters[f];
3258 else
3259 flag_letter = host.app_info->usr_attr_letters[f];
3260
3261 if (flag_letter == '\0')
3262 continue;
3263
3264 flag_mask |= (1 << f);
3265 break;
3266 }
3267 }
3268
3269 return flag_mask;
3270}
3271
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003272/* Compose global flag char-mask for the given command (e.g. "!" or "@") */
3273static const char *cmd_gflag_mask(const struct cmd_element *cmd)
3274{
3275 static char char_mask[8 + 1];
3276 char *ptr = &char_mask[0];
3277
3278 /* Mutually exclusive global attributes */
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003279 if (cmd->attr & CMD_ATTR_HIDDEN)
3280 *(ptr++) = cmd_attr_get_flag(CMD_ATTR_HIDDEN);
3281 else if (cmd->attr & CMD_ATTR_IMMEDIATE)
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003282 *(ptr++) = cmd_attr_get_flag(CMD_ATTR_IMMEDIATE);
3283 else if (cmd->attr & CMD_ATTR_NODE_EXIT)
3284 *(ptr++) = cmd_attr_get_flag(CMD_ATTR_NODE_EXIT);
3285 else
3286 *(ptr++) = '.';
3287
3288 *ptr = '\0';
3289
3290 return char_mask;
3291}
3292
3293/* Compose app / lib flag char-mask for the given command (e.g. ".F.OB..") */
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003294static const char *cmd_flag_mask(const struct cmd_element *cmd,
3295 unsigned int flag_mask)
3296{
3297 static char char_mask[VTY_CMD_USR_ATTR_NUM + 1];
3298 char *ptr = &char_mask[0];
3299 char flag_letter;
3300 unsigned int f;
3301
3302 for (f = 0; f < VTY_CMD_USR_ATTR_NUM; f++) {
Pau Espin Pedrol715a6122020-10-12 15:47:05 +02003303 if (~flag_mask & ((unsigned)1 << f))
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003304 continue;
Pau Espin Pedrol715a6122020-10-12 15:47:05 +02003305 if (~cmd->usrattr & ((unsigned)1 << f)) {
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003306 *(ptr++) = '.';
3307 continue;
3308 }
3309
3310 if (cmd->attr & CMD_ATTR_LIB_COMMAND)
3311 flag_letter = cmd_lib_attr_letters[f];
3312 else
3313 flag_letter = host.app_info->usr_attr_letters[f];
3314
3315 *(ptr++) = flag_letter ? flag_letter : '.';
3316 }
3317
3318 *ptr = '\0';
3319
3320 return char_mask;
3321}
3322
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003323/* Help display function for all node. */
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003324gDEFUN(config_list, config_list_cmd,
3325 "list [with-flags]",
3326 "Print command list\n"
3327 "Also print the VTY attribute flags\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003328{
3329 unsigned int i;
3330 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003331 unsigned int flag_mask = 0x00;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003332 struct cmd_element *cmd;
3333
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003334 if (argc > 0)
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003335 flag_mask = node_flag_mask(cnode, vty->expert_mode);
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003336
Vadim Yanitskiy6f50e212020-08-12 17:32:18 +07003337 for (i = 0; i < vector_active(cnode->cmd_vector); i++) {
3338 if ((cmd = vector_slot(cnode->cmd_vector, i)) == NULL)
3339 continue;
Vadim Yanitskiy72b90882020-10-21 05:07:34 +07003340 if (cmd->attr & CMD_ATTR_DEPRECATED)
3341 continue;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +07003342 if (!vty->expert_mode && (cmd->attr & CMD_ATTR_HIDDEN))
Vadim Yanitskiy6f50e212020-08-12 17:32:18 +07003343 continue;
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003344 if (!argc)
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003345 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
3346 else {
Vadim Yanitskiy7e1a78f2020-10-07 13:13:07 +07003347 vty_out(vty, " %s %s %s%s",
3348 cmd_gflag_mask(cmd),
Vadim Yanitskiy75fec3a2020-10-06 00:13:44 +07003349 cmd_flag_mask(cmd, flag_mask),
3350 cmd->string, VTY_NEWLINE);
3351 }
Vadim Yanitskiy6f50e212020-08-12 17:32:18 +07003352 }
3353
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003354 return CMD_SUCCESS;
3355}
3356
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003357static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003358{
3359 unsigned int i;
3360 int fd;
3361 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003362 char *config_file_tmp = NULL;
3363 char *config_file_sav = NULL;
3364 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003365 struct stat st;
3366
3367 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003368
Neels Hofmeyrd65f3ea2018-03-28 16:12:45 +02003369 /* The string composition code here would be a case for talloc_asprintf(), but the pseudotalloc.c
3370 * talloc_asprintf() implementation would truncate a too-long path with "[...]", so doing it
3371 * manually instead. */
3372
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003373 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003374 config_file_sav =
3375 _talloc_zero(tall_vty_cmd_ctx,
3376 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
3377 "config_file_sav");
Neels Hofmeyrd65f3ea2018-03-28 16:12:45 +02003378 if (!config_file_sav)
3379 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003380 strcpy(config_file_sav, config_file);
3381 strcat(config_file_sav, CONF_BACKUP_EXT);
3382
3383 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
Neels Hofmeyrd65f3ea2018-03-28 16:12:45 +02003384 "config_file_tmp");
3385 if (!config_file_tmp) {
3386 talloc_free(config_file_sav);
3387 return -1;
3388 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003389 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
3390
3391 /* Open file to configuration write. */
3392 fd = mkstemp(config_file_tmp);
3393 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003394 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003395 talloc_free(config_file_tmp);
3396 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003397 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003398 }
3399
3400 /* Make vty for configuration file. */
3401 file_vty = vty_new();
3402 file_vty->fd = fd;
3403 file_vty->type = VTY_FILE;
3404
3405 /* Config file header print. */
3406 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02003407 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003408 //vty_time_print (file_vty, 1);
3409 vty_out(file_vty, "!\n");
3410
3411 for (i = 0; i < vector_active(cmdvec); i++)
3412 if ((node = vector_slot(cmdvec, i)) && node->func) {
3413 if ((*node->func) (file_vty))
3414 vty_out(file_vty, "!\n");
3415 }
3416 vty_close(file_vty);
3417
3418 if (unlink(config_file_sav) != 0)
3419 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003420 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003421 talloc_free(config_file_sav);
3422 talloc_free(config_file_tmp);
3423 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003424 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003425 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003426
3427 /* Only link the .sav file if the original file exists */
3428 if (stat(config_file, &st) == 0) {
3429 if (link(config_file, config_file_sav) != 0) {
3430 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
3431 talloc_free(config_file_sav);
3432 talloc_free(config_file_tmp);
3433 unlink(config_file_tmp);
3434 return -3;
3435 }
3436 sync();
3437 if (unlink(config_file) != 0) {
3438 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
3439 talloc_free(config_file_sav);
3440 talloc_free(config_file_tmp);
3441 unlink(config_file_tmp);
3442 return -4;
3443 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003444 }
3445 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003446 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003447 talloc_free(config_file_sav);
3448 talloc_free(config_file_tmp);
3449 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003450 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003451 }
3452 unlink(config_file_tmp);
3453 sync();
3454
3455 talloc_free(config_file_sav);
3456 talloc_free(config_file_tmp);
3457
3458 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003459 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
3460 return -6;
3461 }
3462
3463 return 0;
3464}
3465
3466
3467/* Write current configuration into file. */
3468DEFUN(config_write_file,
3469 config_write_file_cmd,
Pau Espin Pedroleda8b7b2019-09-26 19:30:47 +02003470 "write file [PATH]",
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003471 "Write running configuration to memory, network, or terminal\n"
Pau Espin Pedroleda8b7b2019-09-26 19:30:47 +02003472 "Write to configuration file\n"
3473 "Set file path to store the config, or replace if already exists\n")
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003474{
3475 char *failed_file;
3476 int rc;
3477
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01003478 if (host.app_info->config_is_consistent) {
3479 rc = host.app_info->config_is_consistent(vty);
3480 if (!rc) {
3481 vty_out(vty, "Configuration is not consistent%s",
3482 VTY_NEWLINE);
3483 return CMD_WARNING;
3484 }
3485 }
3486
Pau Espin Pedroleda8b7b2019-09-26 19:30:47 +02003487 if (argc == 1)
3488 host_config_set(argv[0]);
3489
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003490 if (host.config == NULL) {
3491 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
3492 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003493 return CMD_WARNING;
3494 }
3495
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003496 rc = write_config_file(host.config, &failed_file);
3497 switch (rc) {
3498 case -1:
3499 vty_out(vty, "Can't open configuration file %s.%s",
3500 failed_file, VTY_NEWLINE);
3501 rc = CMD_WARNING;
3502 break;
3503 case -2:
3504 vty_out(vty, "Can't unlink backup configuration file %s.%s",
3505 failed_file, VTY_NEWLINE);
3506 rc = CMD_WARNING;
3507 break;
3508 case -3:
3509 vty_out(vty, "Can't backup old configuration file %s.%s",
3510 failed_file, VTY_NEWLINE);
3511 rc = CMD_WARNING;
3512 break;
3513 case -4:
3514 vty_out(vty, "Can't unlink configuration file %s.%s",
3515 failed_file, VTY_NEWLINE);
3516 rc = CMD_WARNING;
3517 break;
3518 case -5:
3519 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
3520 VTY_NEWLINE);
3521 rc = CMD_WARNING;
3522 break;
3523 case -6:
3524 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
3525 failed_file, strerror(errno), errno, VTY_NEWLINE);
3526 rc = CMD_WARNING;
3527 break;
3528 default:
3529 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
3530 rc = CMD_SUCCESS;
3531 break;
3532 }
3533
3534 talloc_free(failed_file);
3535 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003536}
3537
3538ALIAS(config_write_file,
3539 config_write_cmd,
3540 "write", "Write running configuration to memory, network, or terminal\n")
3541
3542 ALIAS(config_write_file,
3543 config_write_memory_cmd,
3544 "write memory",
3545 "Write running configuration to memory, network, or terminal\n"
3546 "Write configuration to the file (same as write file)\n")
3547
3548 ALIAS(config_write_file,
3549 copy_runningconfig_startupconfig_cmd,
3550 "copy running-config startup-config",
3551 "Copy configuration\n"
3552 "Copy running config to... \n"
3553 "Copy running config to startup config (same as write file)\n")
3554
3555/* Write current configuration into the terminal. */
3556 DEFUN(config_write_terminal,
3557 config_write_terminal_cmd,
3558 "write terminal",
3559 "Write running configuration to memory, network, or terminal\n"
3560 "Write to terminal\n")
3561{
3562 unsigned int i;
3563 struct cmd_node *node;
3564
3565 if (vty->type == VTY_SHELL_SERV) {
3566 for (i = 0; i < vector_active(cmdvec); i++)
3567 if ((node = vector_slot(cmdvec, i)) && node->func
3568 && node->vtysh) {
3569 if ((*node->func) (vty))
3570 vty_out(vty, "!%s", VTY_NEWLINE);
3571 }
3572 } else {
3573 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
3574 VTY_NEWLINE);
3575 vty_out(vty, "!%s", VTY_NEWLINE);
3576
3577 for (i = 0; i < vector_active(cmdvec); i++)
3578 if ((node = vector_slot(cmdvec, i)) && node->func) {
3579 if ((*node->func) (vty))
3580 vty_out(vty, "!%s", VTY_NEWLINE);
3581 }
3582 vty_out(vty, "end%s", VTY_NEWLINE);
3583 }
3584 return CMD_SUCCESS;
3585}
3586
3587/* Write current configuration into the terminal. */
3588ALIAS(config_write_terminal,
3589 show_running_config_cmd,
3590 "show running-config", SHOW_STR "running configuration\n")
3591
3592/* Write startup configuration into the terminal. */
3593 DEFUN(show_startup_config,
3594 show_startup_config_cmd,
3595 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
3596{
3597 char buf[BUFSIZ];
3598 FILE *confp;
3599
3600 confp = fopen(host.config, "r");
3601 if (confp == NULL) {
3602 vty_out(vty, "Can't open configuration file [%s]%s",
3603 host.config, VTY_NEWLINE);
3604 return CMD_WARNING;
3605 }
3606
3607 while (fgets(buf, BUFSIZ, confp)) {
3608 char *cp = buf;
3609
3610 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
3611 cp++;
3612 *cp = '\0';
3613
3614 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
3615 }
3616
3617 fclose(confp);
3618
3619 return CMD_SUCCESS;
3620}
3621
3622/* Hostname configuration */
3623DEFUN(config_hostname,
3624 hostname_cmd,
3625 "hostname WORD",
3626 "Set system's network name\n" "This system's network name\n")
3627{
3628 if (!isalpha((int)*argv[0])) {
3629 vty_out(vty, "Please specify string starting with alphabet%s",
3630 VTY_NEWLINE);
3631 return CMD_WARNING;
3632 }
3633
3634 if (host.name)
3635 talloc_free(host.name);
3636
3637 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3638 return CMD_SUCCESS;
3639}
3640
3641DEFUN(config_no_hostname,
3642 no_hostname_cmd,
3643 "no hostname [HOSTNAME]",
3644 NO_STR "Reset system's network name\n" "Host name of this router\n")
3645{
3646 if (host.name)
3647 talloc_free(host.name);
3648 host.name = NULL;
3649 return CMD_SUCCESS;
3650}
3651
3652/* VTY interface password set. */
3653DEFUN(config_password, password_cmd,
3654 "password (8|) WORD",
3655 "Assign the terminal connection password\n"
3656 "Specifies a HIDDEN password will follow\n"
3657 "dummy string \n" "The HIDDEN line password string\n")
3658{
3659 /* Argument check. */
3660 if (argc == 0) {
3661 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3662 return CMD_WARNING;
3663 }
3664
3665 if (argc == 2) {
3666 if (*argv[0] == '8') {
3667 if (host.password)
3668 talloc_free(host.password);
3669 host.password = NULL;
3670 if (host.password_encrypt)
3671 talloc_free(host.password_encrypt);
3672 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3673 return CMD_SUCCESS;
3674 } else {
3675 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3676 return CMD_WARNING;
3677 }
3678 }
3679
3680 if (!isalnum((int)*argv[0])) {
3681 vty_out(vty,
3682 "Please specify string starting with alphanumeric%s",
3683 VTY_NEWLINE);
3684 return CMD_WARNING;
3685 }
3686
3687 if (host.password)
3688 talloc_free(host.password);
3689 host.password = NULL;
3690
3691#ifdef VTY_CRYPT_PW
3692 if (host.encrypt) {
3693 if (host.password_encrypt)
3694 talloc_free(host.password_encrypt);
3695 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3696 } else
3697#endif
3698 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3699
3700 return CMD_SUCCESS;
3701}
3702
3703ALIAS(config_password, password_text_cmd,
3704 "password LINE",
3705 "Assign the terminal connection password\n"
3706 "The UNENCRYPTED (cleartext) line password\n")
3707
3708/* VTY enable password set. */
3709 DEFUN(config_enable_password, enable_password_cmd,
3710 "enable password (8|) WORD",
3711 "Modify enable password parameters\n"
3712 "Assign the privileged level password\n"
3713 "Specifies a HIDDEN password will follow\n"
3714 "dummy string \n" "The HIDDEN 'enable' password string\n")
3715{
3716 /* Argument check. */
3717 if (argc == 0) {
3718 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3719 return CMD_WARNING;
3720 }
3721
3722 /* Crypt type is specified. */
3723 if (argc == 2) {
3724 if (*argv[0] == '8') {
3725 if (host.enable)
3726 talloc_free(host.enable);
3727 host.enable = NULL;
3728
3729 if (host.enable_encrypt)
3730 talloc_free(host.enable_encrypt);
3731 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3732
3733 return CMD_SUCCESS;
3734 } else {
3735 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3736 return CMD_WARNING;
3737 }
3738 }
3739
3740 if (!isalnum((int)*argv[0])) {
3741 vty_out(vty,
3742 "Please specify string starting with alphanumeric%s",
3743 VTY_NEWLINE);
3744 return CMD_WARNING;
3745 }
3746
3747 if (host.enable)
3748 talloc_free(host.enable);
3749 host.enable = NULL;
3750
3751 /* Plain password input. */
3752#ifdef VTY_CRYPT_PW
3753 if (host.encrypt) {
3754 if (host.enable_encrypt)
3755 talloc_free(host.enable_encrypt);
3756 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3757 } else
3758#endif
3759 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3760
3761 return CMD_SUCCESS;
3762}
3763
3764ALIAS(config_enable_password,
3765 enable_password_text_cmd,
3766 "enable password LINE",
3767 "Modify enable password parameters\n"
3768 "Assign the privileged level password\n"
3769 "The UNENCRYPTED (cleartext) 'enable' password\n")
3770
3771/* VTY enable password delete. */
3772 DEFUN(no_config_enable_password, no_enable_password_cmd,
3773 "no enable password",
3774 NO_STR
3775 "Modify enable password parameters\n"
3776 "Assign the privileged level password\n")
3777{
3778 if (host.enable)
3779 talloc_free(host.enable);
3780 host.enable = NULL;
3781
3782 if (host.enable_encrypt)
3783 talloc_free(host.enable_encrypt);
3784 host.enable_encrypt = NULL;
3785
3786 return CMD_SUCCESS;
3787}
3788
3789#ifdef VTY_CRYPT_PW
3790DEFUN(service_password_encrypt,
3791 service_password_encrypt_cmd,
3792 "service password-encryption",
3793 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3794{
3795 if (host.encrypt)
3796 return CMD_SUCCESS;
3797
3798 host.encrypt = 1;
3799
3800 if (host.password) {
3801 if (host.password_encrypt)
3802 talloc_free(host.password_encrypt);
3803 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3804 }
3805 if (host.enable) {
3806 if (host.enable_encrypt)
3807 talloc_free(host.enable_encrypt);
3808 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3809 }
3810
3811 return CMD_SUCCESS;
3812}
3813
3814DEFUN(no_service_password_encrypt,
3815 no_service_password_encrypt_cmd,
3816 "no service password-encryption",
3817 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3818{
3819 if (!host.encrypt)
3820 return CMD_SUCCESS;
3821
3822 host.encrypt = 0;
3823
3824 if (host.password_encrypt)
3825 talloc_free(host.password_encrypt);
3826 host.password_encrypt = NULL;
3827
3828 if (host.enable_encrypt)
3829 talloc_free(host.enable_encrypt);
3830 host.enable_encrypt = NULL;
3831
3832 return CMD_SUCCESS;
3833}
3834#endif
3835
3836DEFUN(config_terminal_length, config_terminal_length_cmd,
3837 "terminal length <0-512>",
3838 "Set terminal line parameters\n"
3839 "Set number of lines on a screen\n"
3840 "Number of lines on screen (0 for no pausing)\n")
3841{
Neels Hofmeyr34907fe2021-09-05 19:50:34 +02003842 vty->lines = atoi(argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003843 return CMD_SUCCESS;
3844}
3845
3846DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3847 "terminal no length",
3848 "Set terminal line parameters\n"
3849 NO_STR "Set number of lines on a screen\n")
3850{
3851 vty->lines = -1;
3852 return CMD_SUCCESS;
3853}
3854
3855DEFUN(service_terminal_length, service_terminal_length_cmd,
3856 "service terminal-length <0-512>",
3857 "Set up miscellaneous service\n"
3858 "System wide terminal length configuration\n"
3859 "Number of lines of VTY (0 means no line control)\n")
3860{
Neels Hofmeyr34907fe2021-09-05 19:50:34 +02003861 host.lines = atoi(argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003862 return CMD_SUCCESS;
3863}
3864
3865DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3866 "no service terminal-length [<0-512>]",
3867 NO_STR
3868 "Set up miscellaneous service\n"
3869 "System wide terminal length configuration\n"
3870 "Number of lines of VTY (0 means no line control)\n")
3871{
3872 host.lines = -1;
3873 return CMD_SUCCESS;
3874}
3875
3876DEFUN_HIDDEN(do_echo,
3877 echo_cmd,
3878 "echo .MESSAGE",
3879 "Echo a message back to the vty\n" "The message to echo\n")
3880{
3881 char *message;
3882
3883 vty_out(vty, "%s%s",
3884 ((message =
3885 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3886 if (message)
3887 talloc_free(message);
3888 return CMD_SUCCESS;
3889}
3890
3891#if 0
3892DEFUN(config_logmsg,
3893 config_logmsg_cmd,
3894 "logmsg " LOG_LEVELS " .MESSAGE",
3895 "Send a message to enabled logging destinations\n"
3896 LOG_LEVEL_DESC "The message to send\n")
3897{
3898 int level;
3899 char *message;
3900
3901 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3902 return CMD_ERR_NO_MATCH;
3903
3904 zlog(NULL, level,
3905 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3906 if (message)
3907 talloc_free(message);
3908 return CMD_SUCCESS;
3909}
3910
3911DEFUN(show_logging,
3912 show_logging_cmd,
3913 "show logging", SHOW_STR "Show current logging configuration\n")
3914{
3915 struct zlog *zl = zlog_default;
3916
3917 vty_out(vty, "Syslog logging: ");
3918 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3919 vty_out(vty, "disabled");
3920 else
3921 vty_out(vty, "level %s, facility %s, ident %s",
3922 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3923 facility_name(zl->facility), zl->ident);
3924 vty_out(vty, "%s", VTY_NEWLINE);
3925
3926 vty_out(vty, "Stdout logging: ");
3927 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3928 vty_out(vty, "disabled");
3929 else
3930 vty_out(vty, "level %s",
3931 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3932 vty_out(vty, "%s", VTY_NEWLINE);
3933
3934 vty_out(vty, "Monitor logging: ");
3935 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3936 vty_out(vty, "disabled");
3937 else
3938 vty_out(vty, "level %s",
3939 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3940 vty_out(vty, "%s", VTY_NEWLINE);
3941
3942 vty_out(vty, "File logging: ");
3943 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3944 vty_out(vty, "disabled");
3945 else
3946 vty_out(vty, "level %s, filename %s",
3947 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3948 zl->filename);
3949 vty_out(vty, "%s", VTY_NEWLINE);
3950
3951 vty_out(vty, "Protocol name: %s%s",
3952 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3953 vty_out(vty, "Record priority: %s%s",
3954 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3955
3956 return CMD_SUCCESS;
3957}
3958
3959DEFUN(config_log_stdout,
3960 config_log_stdout_cmd,
3961 "log stdout", "Logging control\n" "Set stdout logging level\n")
3962{
3963 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3964 return CMD_SUCCESS;
3965}
3966
3967DEFUN(config_log_stdout_level,
3968 config_log_stdout_level_cmd,
3969 "log stdout " LOG_LEVELS,
3970 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3971{
3972 int level;
3973
3974 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3975 return CMD_ERR_NO_MATCH;
3976 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3977 return CMD_SUCCESS;
3978}
3979
3980DEFUN(no_config_log_stdout,
3981 no_config_log_stdout_cmd,
3982 "no log stdout [LEVEL]",
3983 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3984{
3985 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3986 return CMD_SUCCESS;
3987}
3988
3989DEFUN(config_log_monitor,
3990 config_log_monitor_cmd,
3991 "log monitor",
3992 "Logging control\n" "Set terminal line (monitor) logging level\n")
3993{
3994 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3995 return CMD_SUCCESS;
3996}
3997
3998DEFUN(config_log_monitor_level,
3999 config_log_monitor_level_cmd,
4000 "log monitor " LOG_LEVELS,
4001 "Logging control\n"
4002 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
4003{
4004 int level;
4005
4006 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
4007 return CMD_ERR_NO_MATCH;
4008 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
4009 return CMD_SUCCESS;
4010}
4011
4012DEFUN(no_config_log_monitor,
4013 no_config_log_monitor_cmd,
4014 "no log monitor [LEVEL]",
4015 NO_STR
4016 "Logging control\n"
4017 "Disable terminal line (monitor) logging\n" "Logging level\n")
4018{
4019 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
4020 return CMD_SUCCESS;
4021}
4022
4023static int set_log_file(struct vty *vty, const char *fname, int loglevel)
4024{
4025 int ret;
4026 char *p = NULL;
4027 const char *fullpath;
4028
4029 /* Path detection. */
4030 if (!IS_DIRECTORY_SEP(*fname)) {
4031 char cwd[MAXPATHLEN + 1];
4032 cwd[MAXPATHLEN] = '\0';
4033
4034 if (getcwd(cwd, MAXPATHLEN) == NULL) {
4035 zlog_err("config_log_file: Unable to alloc mem!");
4036 return CMD_WARNING;
4037 }
4038
4039 if ((p = _talloc_zero(tall_vcmd_ctx,
4040 strlen(cwd) + strlen(fname) + 2),
4041 "set_log_file")
4042 == NULL) {
4043 zlog_err("config_log_file: Unable to alloc mem!");
4044 return CMD_WARNING;
4045 }
4046 sprintf(p, "%s/%s", cwd, fname);
4047 fullpath = p;
4048 } else
4049 fullpath = fname;
4050
4051 ret = zlog_set_file(NULL, fullpath, loglevel);
4052
4053 if (p)
4054 talloc_free(p);
4055
4056 if (!ret) {
4057 vty_out(vty, "can't open logfile %s\n", fname);
4058 return CMD_WARNING;
4059 }
4060
4061 if (host.logfile)
4062 talloc_free(host.logfile);
4063
4064 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
4065
4066 return CMD_SUCCESS;
4067}
4068
4069DEFUN(config_log_file,
4070 config_log_file_cmd,
4071 "log file FILENAME",
4072 "Logging control\n" "Logging to file\n" "Logging filename\n")
4073{
4074 return set_log_file(vty, argv[0], zlog_default->default_lvl);
4075}
4076
4077DEFUN(config_log_file_level,
4078 config_log_file_level_cmd,
4079 "log file FILENAME " LOG_LEVELS,
4080 "Logging control\n"
4081 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
4082{
4083 int level;
4084
4085 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
4086 return CMD_ERR_NO_MATCH;
4087 return set_log_file(vty, argv[0], level);
4088}
4089
4090DEFUN(no_config_log_file,
4091 no_config_log_file_cmd,
4092 "no log file [FILENAME]",
4093 NO_STR
4094 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
4095{
4096 zlog_reset_file(NULL);
4097
4098 if (host.logfile)
4099 talloc_free(host.logfile);
4100
4101 host.logfile = NULL;
4102
4103 return CMD_SUCCESS;
4104}
4105
4106ALIAS(no_config_log_file,
4107 no_config_log_file_level_cmd,
4108 "no log file FILENAME LEVEL",
4109 NO_STR
4110 "Logging control\n"
4111 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
4112
4113 DEFUN(config_log_syslog,
4114 config_log_syslog_cmd,
4115 "log syslog", "Logging control\n" "Set syslog logging level\n")
4116{
4117 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
4118 return CMD_SUCCESS;
4119}
4120
4121DEFUN(config_log_syslog_level,
4122 config_log_syslog_level_cmd,
4123 "log syslog " LOG_LEVELS,
4124 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
4125{
4126 int level;
4127
4128 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
4129 return CMD_ERR_NO_MATCH;
4130 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
4131 return CMD_SUCCESS;
4132}
4133
4134DEFUN_DEPRECATED(config_log_syslog_facility,
4135 config_log_syslog_facility_cmd,
4136 "log syslog facility " LOG_FACILITIES,
4137 "Logging control\n"
4138 "Logging goes to syslog\n"
4139 "(Deprecated) Facility parameter for syslog messages\n"
4140 LOG_FACILITY_DESC)
4141{
4142 int facility;
4143
4144 if ((facility = facility_match(argv[0])) < 0)
4145 return CMD_ERR_NO_MATCH;
4146
4147 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
4148 zlog_default->facility = facility;
4149 return CMD_SUCCESS;
4150}
4151
4152DEFUN(no_config_log_syslog,
4153 no_config_log_syslog_cmd,
4154 "no log syslog [LEVEL]",
4155 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
4156{
4157 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
4158 return CMD_SUCCESS;
4159}
4160
4161ALIAS(no_config_log_syslog,
4162 no_config_log_syslog_facility_cmd,
4163 "no log syslog facility " LOG_FACILITIES,
4164 NO_STR
4165 "Logging control\n"
4166 "Logging goes to syslog\n"
4167 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
4168
4169 DEFUN(config_log_facility,
4170 config_log_facility_cmd,
4171 "log facility " LOG_FACILITIES,
4172 "Logging control\n"
4173 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
4174{
4175 int facility;
4176
4177 if ((facility = facility_match(argv[0])) < 0)
4178 return CMD_ERR_NO_MATCH;
4179 zlog_default->facility = facility;
4180 return CMD_SUCCESS;
4181}
4182
4183DEFUN(no_config_log_facility,
4184 no_config_log_facility_cmd,
4185 "no log facility [FACILITY]",
4186 NO_STR
4187 "Logging control\n"
4188 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
4189{
4190 zlog_default->facility = LOG_DAEMON;
4191 return CMD_SUCCESS;
4192}
4193
4194DEFUN_DEPRECATED(config_log_trap,
4195 config_log_trap_cmd,
4196 "log trap " LOG_LEVELS,
4197 "Logging control\n"
4198 "(Deprecated) Set logging level and default for all destinations\n"
4199 LOG_LEVEL_DESC)
4200{
4201 int new_level;
4202 int i;
4203
4204 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
4205 return CMD_ERR_NO_MATCH;
4206
4207 zlog_default->default_lvl = new_level;
4208 for (i = 0; i < ZLOG_NUM_DESTS; i++)
4209 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
4210 zlog_default->maxlvl[i] = new_level;
4211 return CMD_SUCCESS;
4212}
4213
4214DEFUN_DEPRECATED(no_config_log_trap,
4215 no_config_log_trap_cmd,
4216 "no log trap [LEVEL]",
4217 NO_STR
4218 "Logging control\n"
4219 "Permit all logging information\n" "Logging level\n")
4220{
4221 zlog_default->default_lvl = LOG_DEBUG;
4222 return CMD_SUCCESS;
4223}
4224
4225DEFUN(config_log_record_priority,
4226 config_log_record_priority_cmd,
4227 "log record-priority",
4228 "Logging control\n"
4229 "Log the priority of the message within the message\n")
4230{
4231 zlog_default->record_priority = 1;
4232 return CMD_SUCCESS;
4233}
4234
4235DEFUN(no_config_log_record_priority,
4236 no_config_log_record_priority_cmd,
4237 "no log record-priority",
4238 NO_STR
4239 "Logging control\n"
4240 "Do not log the priority of the message within the message\n")
4241{
4242 zlog_default->record_priority = 0;
4243 return CMD_SUCCESS;
4244}
4245#endif
4246
4247DEFUN(banner_motd_file,
4248 banner_motd_file_cmd,
4249 "banner motd file [FILE]",
4250 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
4251{
4252 if (host.motdfile)
4253 talloc_free(host.motdfile);
4254 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
4255
4256 return CMD_SUCCESS;
4257}
4258
4259DEFUN(banner_motd_default,
4260 banner_motd_default_cmd,
4261 "banner motd default",
4262 "Set banner string\n" "Strings for motd\n" "Default string\n")
4263{
4264 host.motd = default_motd;
4265 return CMD_SUCCESS;
4266}
4267
4268DEFUN(no_banner_motd,
4269 no_banner_motd_cmd,
4270 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
4271{
4272 host.motd = NULL;
4273 if (host.motdfile)
4274 talloc_free(host.motdfile);
4275 host.motdfile = NULL;
4276 return CMD_SUCCESS;
4277}
4278
4279/* Set config filename. Called from vty.c */
4280void host_config_set(const char *filename)
4281{
4282 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
4283}
4284
Pau Espin Pedrolebb6c1f2021-05-17 18:54:21 +02004285const char *host_config_file(void)
4286{
4287 return host.config;
4288}
4289
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02004290/*! Deprecated, now happens implicitly when calling install_node().
4291 * Users of the API may still attempt to call this function, hence
4292 * leave it here as a no-op. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00004293void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004294{
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02004295}
4296
4297/*! Deprecated, now happens implicitly when calling install_node().
4298 * Users of the API may still attempt to call this function, hence
4299 * leave it here as a no-op. */
4300void vty_install_default(int node)
4301{
4302}
4303
4304/*! Install common commands like 'exit' and 'list'. */
4305static void install_basic_node_commands(int node)
4306{
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004307 install_lib_element(node, &config_help_cmd);
4308 install_lib_element(node, &config_list_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004309
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07004310 install_lib_element(node, &show_vty_attr_all_cmd);
4311 install_lib_element(node, &show_vty_attr_cmd);
4312
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004313 install_lib_element(node, &config_write_terminal_cmd);
4314 install_lib_element(node, &config_write_file_cmd);
4315 install_lib_element(node, &config_write_memory_cmd);
4316 install_lib_element(node, &config_write_cmd);
4317 install_lib_element(node, &show_running_config_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02004318
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004319 install_lib_element(node, &config_exit_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02004320
4321 if (node >= CONFIG_NODE) {
4322 /* It's not a top node. */
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004323 install_lib_element(node, &config_end_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02004324 }
4325}
4326
Neels Hofmeyr69054e22017-10-19 02:44:57 +02004327/*! Return true if a node is installed by install_basic_node_commands(), so
4328 * that we can avoid repeating them for each and every node during 'show
4329 * running-config' */
Vadim Yanitskiyefe13422020-10-21 20:36:17 +07004330static bool vty_command_is_common(const struct cmd_element *cmd)
Neels Hofmeyr69054e22017-10-19 02:44:57 +02004331{
4332 if (cmd == &config_help_cmd
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07004333 || cmd == &show_vty_attr_all_cmd
4334 || cmd == &show_vty_attr_cmd
Neels Hofmeyr69054e22017-10-19 02:44:57 +02004335 || cmd == &config_list_cmd
4336 || cmd == &config_write_terminal_cmd
4337 || cmd == &config_write_file_cmd
4338 || cmd == &config_write_memory_cmd
4339 || cmd == &config_write_cmd
4340 || cmd == &show_running_config_cmd
4341 || cmd == &config_exit_cmd
4342 || cmd == &config_end_cmd)
4343 return true;
4344 return false;
4345}
4346
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01004347/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02004348 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01004349 * \param[in] vty the vty of the code
4350 * \param[in] filename where to store the file
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 */
4357int osmo_vty_write_config_file(const char *filename)
4358{
4359 char *failed_file;
4360 int rc;
4361
4362 rc = write_config_file(filename, &failed_file);
4363 talloc_free(failed_file);
4364 return rc;
4365}
4366
4367/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02004368 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01004369 * \return 0 in case of success.
4370 *
4371 * If the filename already exists create a filename.sav
4372 * version with the current code.
4373 *
4374 */
4375int osmo_vty_save_config_file(void)
4376{
4377 char *failed_file;
4378 int rc;
4379
4380 if (host.config == NULL)
4381 return -7;
4382
4383 rc = write_config_file(host.config, &failed_file);
4384 talloc_free(failed_file);
4385 return rc;
4386}
4387
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004388/* Initialize command interface. Install basic nodes and commands. */
4389void cmd_init(int terminal)
4390{
4391 /* Allocate initial top vector of commands. */
4392 cmdvec = vector_init(VECTOR_MIN_SIZE);
4393
4394 /* Default host value settings. */
4395 host.name = NULL;
4396 host.password = NULL;
4397 host.enable = NULL;
4398 host.logfile = NULL;
4399 host.config = NULL;
4400 host.lines = -1;
4401 host.motd = default_motd;
4402 host.motdfile = NULL;
4403
4404 /* Install top nodes. */
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02004405 install_node_bare(&view_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004406 install_node(&enable_node, NULL);
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02004407 install_node_bare(&auth_node, NULL);
4408 install_node_bare(&auth_enable_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004409 install_node(&config_node, config_write_host);
4410
4411 /* Each node's basic commands. */
Oliver Smith0c78bc62021-07-12 17:28:36 +02004412 install_lib_element(VIEW_NODE, &show_pid_cmd);
Oliver Smithd243c2a2021-07-09 17:19:32 +02004413 install_lib_element(VIEW_NODE, &show_uptime_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004414 install_lib_element(VIEW_NODE, &show_version_cmd);
4415 install_lib_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004416 if (terminal) {
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004417 install_lib_element(VIEW_NODE, &config_list_cmd);
4418 install_lib_element(VIEW_NODE, &config_exit_cmd);
4419 install_lib_element(VIEW_NODE, &config_help_cmd);
Vadim Yanitskiy44c4d172020-10-05 01:30:23 +07004420 install_lib_element(VIEW_NODE, &show_vty_attr_all_cmd);
4421 install_lib_element(VIEW_NODE, &show_vty_attr_cmd);
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004422 install_lib_element(VIEW_NODE, &config_enable_cmd);
4423 install_lib_element(VIEW_NODE, &config_terminal_length_cmd);
4424 install_lib_element(VIEW_NODE, &config_terminal_no_length_cmd);
4425 install_lib_element(VIEW_NODE, &echo_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004426 }
4427
4428 if (terminal) {
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004429 install_lib_element(ENABLE_NODE, &config_disable_cmd);
4430 install_lib_element(ENABLE_NODE, &config_terminal_cmd);
4431 install_lib_element(ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
Oliver Smith8a482fd2021-07-12 18:18:51 +02004432 install_lib_element(ENABLE_NODE, &shutdown_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004433 }
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004434 install_lib_element(ENABLE_NODE, &show_startup_config_cmd);
4435 install_lib_element(ENABLE_NODE, &show_version_cmd);
4436 install_lib_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004437
4438 if (terminal) {
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004439 install_lib_element(ENABLE_NODE, &config_terminal_length_cmd);
4440 install_lib_element(ENABLE_NODE, &config_terminal_no_length_cmd);
4441 install_lib_element(ENABLE_NODE, &echo_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004442 }
4443
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004444 install_lib_element(CONFIG_NODE, &hostname_cmd);
4445 install_lib_element(CONFIG_NODE, &no_hostname_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004446
4447 if (terminal) {
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004448 install_lib_element(CONFIG_NODE, &password_cmd);
4449 install_lib_element(CONFIG_NODE, &password_text_cmd);
4450 install_lib_element(CONFIG_NODE, &enable_password_cmd);
4451 install_lib_element(CONFIG_NODE, &enable_password_text_cmd);
4452 install_lib_element(CONFIG_NODE, &no_enable_password_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004453
4454#ifdef VTY_CRYPT_PW
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004455 install_lib_element(CONFIG_NODE, &service_password_encrypt_cmd);
4456 install_lib_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004457#endif
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +07004458 install_lib_element(CONFIG_NODE, &banner_motd_default_cmd);
4459 install_lib_element(CONFIG_NODE, &banner_motd_file_cmd);
4460 install_lib_element(CONFIG_NODE, &no_banner_motd_cmd);
4461 install_lib_element(CONFIG_NODE, &service_terminal_length_cmd);
4462 install_lib_element(CONFIG_NODE, &no_service_terminal_length_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004463
4464 }
4465 srand(time(NULL));
4466}
Harald Welte7acb30c2011-08-17 17:13:48 +02004467
Oliver Smithd243c2a2021-07-09 17:19:32 +02004468static __attribute__((constructor)) void on_dso_load_starttime(void)
4469{
4470 osmo_clock_gettime(CLOCK_MONOTONIC, &starttime);
4471}
4472
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +07004473/* FIXME: execute this section in the unit test instead */
4474static __attribute__((constructor)) void on_dso_load(void)
4475{
4476 unsigned int i, j;
4477
4478 /* Check total number of the library specific attributes */
4479 OSMO_ASSERT(_OSMO_CORE_LIB_ATTR_COUNT < 32);
4480
4481 /* Check for duplicates in the list of library specific flags */
4482 for (i = 0; i < _OSMO_CORE_LIB_ATTR_COUNT; i++) {
4483 if (cmd_lib_attr_letters[i] == '\0')
4484 continue;
4485
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +07004486 /* Some flag characters are reserved for global attributes */
4487 const char rafc[] = VTY_CMD_ATTR_FLAGS_RESERVED;
4488 for (j = 0; j < ARRAY_SIZE(rafc); j++)
4489 OSMO_ASSERT(cmd_lib_attr_letters[i] != rafc[j]);
4490
Vadim Yanitskiyc0745eb2020-10-04 18:37:13 +07004491 /* Only upper case flag letters are allowed for libraries */
4492 OSMO_ASSERT(cmd_lib_attr_letters[i] >= 'A');
4493 OSMO_ASSERT(cmd_lib_attr_letters[i] <= 'Z');
4494
4495 for (j = i + 1; j < _OSMO_CORE_LIB_ATTR_COUNT; j++)
4496 OSMO_ASSERT(cmd_lib_attr_letters[i] != cmd_lib_attr_letters[j]);
4497 }
4498}
4499
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02004500/*! @} */