blob: e5efad2eb85c9229494bbf3e36d5826a6b9741ce [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>
40#include <sys/time.h>
41#include <sys/stat.h>
42
43#include <osmocom/vty/vector.h>
44#include <osmocom/vty/vty.h>
45#include <osmocom/vty/command.h>
46
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010047#include <osmocom/core/talloc.h>
Harald Weltea99d45a2015-11-12 13:48:23 +010048#include <osmocom/core/utils.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049
Harald Weltee881b1b2011-08-17 18:52:30 +020050/*! \addtogroup command
Harald Welte96e2a002017-06-12 21:44:18 +020051 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 * VTY command handling
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020053 *
54 * \file command.c */
Harald Welte7acb30c2011-08-17 17:13:48 +020055
Harald Welte3fb0b6f2010-05-19 19:02:52 +020056#define CONFIGFILE_MASK 022
57
58void *tall_vty_cmd_ctx;
59
60/* Command vector which includes some level of command lists. Normally
61 each daemon maintains each own cmdvec. */
62vector cmdvec;
63
64/* Host information structure. */
65struct host host;
66
67/* Standard command node structures. */
68struct cmd_node auth_node = {
69 AUTH_NODE,
70 "Password: ",
71};
72
73struct cmd_node view_node = {
74 VIEW_NODE,
75 "%s> ",
76};
77
78struct cmd_node auth_enable_node = {
79 AUTH_ENABLE_NODE,
80 "Password: ",
81};
82
83struct cmd_node enable_node = {
84 ENABLE_NODE,
85 "%s# ",
86};
87
88struct cmd_node config_node = {
89 CONFIG_NODE,
90 "%s(config)# ",
91 1
92};
93
94/* Default motd string. */
95const char *default_motd = "";
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097/*! print the version (and optionally copyright) information
Harald Welte7acb30c2011-08-17 17:13:48 +020098 *
99 * This is called from main when a daemon is invoked with -v or --version. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200100void print_version(int print_copyright)
101{
Harald Welte237f6242010-05-25 23:00:45 +0200102 printf("%s version %s\n", host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103 if (print_copyright)
Harald Welte237f6242010-05-25 23:00:45 +0200104 printf("\n%s\n", host.app_info->copyright);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105}
106
107/* Utility function to concatenate argv argument into a single string
108 with inserting ' ' character between each argument. */
109char *argv_concat(const char **argv, int argc, int shift)
110{
111 int i;
112 size_t len;
113 char *str;
114 char *p;
115
116 len = 0;
117 for (i = shift; i < argc; i++)
118 len += strlen(argv[i]) + 1;
119 if (!len)
120 return NULL;
121 p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
122 for (i = shift; i < argc; i++) {
123 size_t arglen;
124 memcpy(p, argv[i], (arglen = strlen(argv[i])));
125 p += arglen;
126 *p++ = ' ';
127 }
128 *(p - 1) = '\0';
129 return str;
130}
131
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200132/* Strip all characters from a string (prompt) except for alnum, '-' and '_'.
133 * For example used to derive a node->name from node->prompt if the user didn't provide a name;
134 * in turn, this name us used for XML IDs in 'show online-help'. */
135static const char *node_name_from_prompt(const char *prompt, char *name_buf, size_t name_buf_size)
136{
137 const char *pos;
138 int dest = 0;
139
140 if (!prompt || !*prompt)
141 return "";
142
143 for (pos = prompt; *pos && dest < (name_buf_size-1); pos++) {
144 if (pos[0] == '%' && pos[1]) {
145 /* skip "%s"; loop pos++ does the second one. */
146 pos++;
147 continue;
148 }
149 if (!(isalnum(pos[0]) || pos[0] == '-' || pos[0] == '_'))
150 continue;
151 name_buf[dest] = pos[0];
152 dest++;
153 }
154 name_buf[dest] = '\0';
155 return name_buf;
156}
157
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +0200158static void install_basic_node_commands(int node);
159
160/*! Install top node of command vector, without adding basic node commands. */
161static void install_node_bare(struct cmd_node *node, int (*func) (struct vty *))
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162{
163 vector_set_index(cmdvec, node->node, node);
164 node->func = func;
165 node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200166 if (!*node->name)
167 node_name_from_prompt(node->prompt, node->name, sizeof(node->name));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200168}
169
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +0200170/*! Install top node of command vector. */
171void install_node(struct cmd_node *node, int (*func) (struct vty *))
172{
173 install_node_bare(node, func);
174 install_basic_node_commands(node->node);
175}
176
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200177/* Compare two command's string. Used in sort_node (). */
178static int cmp_node(const void *p, const void *q)
179{
180 struct cmd_element *a = *(struct cmd_element **)p;
181 struct cmd_element *b = *(struct cmd_element **)q;
182
183 return strcmp(a->string, b->string);
184}
185
186static int cmp_desc(const void *p, const void *q)
187{
188 struct desc *a = *(struct desc **)p;
189 struct desc *b = *(struct desc **)q;
190
191 return strcmp(a->cmd, b->cmd);
192}
193
Jacob Erlbeck2442e092013-09-06 16:51:58 +0200194static int is_config_child(struct vty *vty)
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800195{
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800196 if (vty->node <= CONFIG_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800197 return 0;
Holger Hans Peter Freyther8304b1e2010-09-04 11:19:39 +0800198 else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800199 return 1;
200 else if (host.app_info->is_config_node)
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800201 return host.app_info->is_config_node(vty, vty->node);
Holger Hans Peter Freyther3e85e8d2010-08-26 14:37:10 +0800202 else
203 return vty->node > CONFIG_NODE;
Holger Hans Peter Freyther50cfb782010-08-25 13:23:53 +0800204}
205
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200206/*! Sort each node's command element according to command string. */
Harald Welte95b2b472011-07-16 11:58:09 +0200207void sort_node(void)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200208{
209 unsigned int i, j;
210 struct cmd_node *cnode;
211 vector descvec;
212 struct cmd_element *cmd_element;
213
214 for (i = 0; i < vector_active(cmdvec); i++)
215 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
216 vector cmd_vector = cnode->cmd_vector;
217 qsort(cmd_vector->index, vector_active(cmd_vector),
218 sizeof(void *), cmp_node);
219
220 for (j = 0; j < vector_active(cmd_vector); j++)
221 if ((cmd_element =
222 vector_slot(cmd_vector, j)) != NULL
223 && vector_active(cmd_element->strvec)) {
224 descvec =
225 vector_slot(cmd_element->strvec,
226 vector_active
227 (cmd_element->strvec) -
228 1);
229 qsort(descvec->index,
230 vector_active(descvec),
231 sizeof(void *), cmp_desc);
232 }
233 }
234}
235
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200236/*! Break up string in command tokens. Return leading indents.
237 * \param[in] string String to split.
238 * \param[out] indent If not NULL, return a talloc_strdup of indent characters.
239 * \param[out] strvec_p Returns vector of split tokens, must not be NULL.
240 * \returns CMD_SUCCESS or CMD_ERR_INVALID_INDENT
241 *
242 * If \a indent is passed non-NULL, only simple space ' ' indents are allowed,
243 * so that \a indent can simply return the count of leading spaces.
244 * Otherwise any isspace() characters are allowed for indenting (backwards compat).
245 */
246int cmd_make_strvec2(const char *string, char **indent, vector *strvec_p)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200247{
248 const char *cp, *start;
249 char *token;
250 int strlen;
251 vector strvec;
252
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200253 *strvec_p = NULL;
254 if (indent)
255 *indent = 0;
256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200257 if (string == NULL)
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200258 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200259
260 cp = string;
261
262 /* Skip white spaces. */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200263 while (isspace((int)*cp) && *cp != '\0') {
264 /* if we're counting indents, we need to be strict about them */
265 if (indent && (*cp != ' ') && (*cp != '\t')) {
266 /* Ignore blank lines, they appear as leading whitespace with line breaks. */
267 if (*cp == '\n' || *cp == '\r') {
268 cp++;
269 string = cp;
270 continue;
271 }
272 return CMD_ERR_INVALID_INDENT;
273 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200274 cp++;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200275 }
276
277 if (indent)
278 *indent = talloc_strndup(tall_vty_cmd_ctx, string, cp - string);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200279
280 /* Return if there is only white spaces */
281 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200282 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200283
284 if (*cp == '!' || *cp == '#')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200285 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200286
287 /* Prepare return vector. */
288 strvec = vector_init(VECTOR_MIN_SIZE);
289
290 /* Copy each command piece and set into vector. */
291 while (1) {
292 start = cp;
293 while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
294 *cp != '\0')
295 cp++;
296 strlen = cp - start;
297 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
298 memcpy(token, start, strlen);
299 *(token + strlen) = '\0';
300 vector_set(strvec, token);
301
302 while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
303 *cp != '\0')
304 cp++;
305
306 if (*cp == '\0')
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200307 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200308 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200309
310 *strvec_p = strvec;
311 return CMD_SUCCESS;
312}
313
314/*! Breaking up string into each command piece. I assume given
315 character is separated by a space character. Return value is a
316 vector which includes char ** data element. */
317vector cmd_make_strvec(const char *string)
318{
319 vector strvec;
320 cmd_make_strvec2(string, NULL, &strvec);
321 return strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200322}
323
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200324/*! Free allocated string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200325void cmd_free_strvec(vector v)
326{
327 unsigned int i;
328 char *cp;
329
330 if (!v)
331 return;
332
333 for (i = 0; i < vector_active(v); i++)
334 if ((cp = vector_slot(v, i)) != NULL)
335 talloc_free(cp);
336
337 vector_free(v);
338}
339
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200340/*! Fetch next description. Used in \ref cmd_make_descvec(). */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200341static char *cmd_desc_str(const char **string)
342{
343 const char *cp, *start;
344 char *token;
345 int strlen;
346
347 cp = *string;
348
349 if (cp == NULL)
350 return NULL;
351
352 /* Skip white spaces. */
353 while (isspace((int)*cp) && *cp != '\0')
354 cp++;
355
356 /* Return if there is only white spaces */
357 if (*cp == '\0')
358 return NULL;
359
360 start = cp;
361
362 while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
363 cp++;
364
365 strlen = cp - start;
366 token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
367 memcpy(token, start, strlen);
368 *(token + strlen) = '\0';
369
370 *string = cp;
371
372 return token;
373}
374
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200375/*! New string vector. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200376static vector cmd_make_descvec(const char *string, const char *descstr)
377{
378 int multiple = 0;
379 const char *sp;
380 char *token;
381 int len;
382 const char *cp;
383 const char *dp;
384 vector allvec;
385 vector strvec = NULL;
386 struct desc *desc;
387
388 cp = string;
389 dp = descstr;
390
391 if (cp == NULL)
392 return NULL;
393
394 allvec = vector_init(VECTOR_MIN_SIZE);
395
396 while (1) {
397 while (isspace((int)*cp) && *cp != '\0')
398 cp++;
399
400 if (*cp == '(') {
401 multiple = 1;
402 cp++;
403 }
404 if (*cp == ')') {
405 multiple = 0;
406 cp++;
407 }
408 if (*cp == '|') {
Harald Weltea99d45a2015-11-12 13:48:23 +0100409 OSMO_ASSERT(multiple);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200410 cp++;
411 }
412
413 while (isspace((int)*cp) && *cp != '\0')
414 cp++;
415
416 if (*cp == '(') {
417 multiple = 1;
418 cp++;
419 }
420
421 if (*cp == '\0')
422 return allvec;
423
424 sp = cp;
425
426 while (!
427 (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
428 || *cp == ')' || *cp == '|') && *cp != '\0')
429 cp++;
430
431 len = cp - sp;
432
433 token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
434 memcpy(token, sp, len);
435 *(token + len) = '\0';
436
437 desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
438 desc->cmd = token;
439 desc->str = cmd_desc_str(&dp);
440
441 if (multiple) {
442 if (multiple == 1) {
443 strvec = vector_init(VECTOR_MIN_SIZE);
444 vector_set(allvec, strvec);
445 }
446 multiple++;
447 } else {
448 strvec = vector_init(VECTOR_MIN_SIZE);
449 vector_set(allvec, strvec);
450 }
451 vector_set(strvec, desc);
452 }
453}
454
455/* Count mandantory string vector size. This is to determine inputed
456 command has enough command length. */
457static int cmd_cmdsize(vector strvec)
458{
459 unsigned int i;
460 int size = 0;
461 vector descvec;
462 struct desc *desc;
463
464 for (i = 0; i < vector_active(strvec); i++)
465 if ((descvec = vector_slot(strvec, i)) != NULL) {
466 if ((vector_active(descvec)) == 1
467 && (desc = vector_slot(descvec, 0)) != NULL) {
468 if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
469 return size;
470 else
471 size++;
472 } else
473 size++;
474 }
475 return size;
476}
477
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200478/*! Return prompt character of specified node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200479const char *cmd_prompt(enum node_type node)
480{
481 struct cmd_node *cnode;
482
483 cnode = vector_slot(cmdvec, node);
484 return cnode->prompt;
485}
486
Alexander Couzensad580ba2016-05-16 16:01:45 +0200487/*!
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200488 * escape all special asciidoc symbols
Alexander Couzensad580ba2016-05-16 16:01:45 +0200489 * \param unsafe string
490 * \return a new talloc char *
491 */
492char *osmo_asciidoc_escape(const char *inp)
493{
494 int _strlen;
495 char *out, *out_ptr;
496 int len = 0, i, j;
497
498 if (!inp)
499 return NULL;
500 _strlen = strlen(inp);
501
502 for (i = 0; i < _strlen; ++i) {
503 switch (inp[i]) {
504 case '|':
505 len += 2;
506 break;
507 default:
508 len += 1;
509 break;
510 }
511 }
512
513 out = talloc_size(NULL, len + 1);
514 if (!out)
515 return NULL;
516
517 out_ptr = out;
518
519#define ADD(out, str) \
520 for (j = 0; j < strlen(str); ++j) \
521 *(out++) = str[j];
522
523 for (i = 0; i < _strlen; ++i) {
524 switch (inp[i]) {
525 case '|':
526 ADD(out_ptr, "\\|");
527 break;
528 default:
529 *(out_ptr++) = inp[i];
530 break;
531 }
532 }
533
534#undef ADD
535
536 out_ptr[0] = '\0';
537 return out;
538}
539
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100540static char *xml_escape(const char *inp)
541{
542 int _strlen;
543 char *out, *out_ptr;
544 int len = 0, i, j;
545
546 if (!inp)
547 return NULL;
548 _strlen = strlen(inp);
549
550 for (i = 0; i < _strlen; ++i) {
551 switch (inp[i]) {
552 case '"':
553 len += 6;
554 break;
555 case '\'':
556 len += 6;
557 break;
558 case '<':
559 len += 4;
560 break;
561 case '>':
562 len += 4;
563 break;
564 case '&':
565 len += 5;
566 break;
567 default:
568 len += 1;
569 break;
570 }
571 }
572
573 out = talloc_size(NULL, len + 1);
574 if (!out)
575 return NULL;
576
577 out_ptr = out;
578
579#define ADD(out, str) \
580 for (j = 0; j < strlen(str); ++j) \
581 *(out++) = str[j];
582
583 for (i = 0; i < _strlen; ++i) {
584 switch (inp[i]) {
585 case '"':
586 ADD(out_ptr, "&quot;");
587 break;
588 case '\'':
589 ADD(out_ptr, "&apos;");
590 break;
591 case '<':
592 ADD(out_ptr, "&lt;");
593 break;
594 case '>':
595 ADD(out_ptr, "&gt;");
596 break;
597 case '&':
598 ADD(out_ptr, "&amp;");
599 break;
600 default:
601 *(out_ptr++) = inp[i];
602 break;
603 }
604 }
605
606#undef ADD
607
608 out_ptr[0] = '\0';
609 return out;
610}
611
612/*
613 * Write one cmd_element as XML to the given VTY.
614 */
615static int vty_dump_element(struct cmd_element *cmd, struct vty *vty)
616{
617 char *xml_string = xml_escape(cmd->string);
618
619 vty_out(vty, " <command id='%s'>%s", xml_string, VTY_NEWLINE);
620 vty_out(vty, " <params>%s", VTY_NEWLINE);
621
622 int j;
623 for (j = 0; j < vector_count(cmd->strvec); ++j) {
624 vector descvec = vector_slot(cmd->strvec, j);
625 int i;
626 for (i = 0; i < vector_active(descvec); ++i) {
627 char *xml_param, *xml_doc;
628 struct desc *desc = vector_slot(descvec, i);
629 if (desc == NULL)
630 continue;
631
632 xml_param = xml_escape(desc->cmd);
633 xml_doc = xml_escape(desc->str);
634 vty_out(vty, " <param name='%s' doc='%s' />%s",
635 xml_param, xml_doc, VTY_NEWLINE);
636 talloc_free(xml_param);
637 talloc_free(xml_doc);
638 }
639 }
640
641 vty_out(vty, " </params>%s", VTY_NEWLINE);
642 vty_out(vty, " </command>%s", VTY_NEWLINE);
643
644 talloc_free(xml_string);
645 return 0;
646}
647
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200648static bool vty_command_is_common(struct cmd_element *cmd);
649
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100650/*
651 * Dump all nodes and commands associated with a given node as XML to the VTY.
652 */
653static int vty_dump_nodes(struct vty *vty)
654{
655 int i, j;
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200656 int same_name_count;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100657
658 vty_out(vty, "<vtydoc xmlns='urn:osmocom:xml:libosmocore:vty:doc:1.0'>%s", VTY_NEWLINE);
659
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200660 /* Only once, list all common node commands. Use the CONFIG node to find common node commands. */
661 vty_out(vty, " <node id='_common_cmds_'>%s", VTY_NEWLINE);
662 vty_out(vty, " <name>Common Commands</name>%s", VTY_NEWLINE);
663 vty_out(vty, " <description>These commands are available on all VTY nodes. They are listed"
664 " here only once, to unclutter the VTY reference.</description>%s", VTY_NEWLINE);
665 for (i = 0; i < vector_active(cmdvec); ++i) {
666 struct cmd_node *cnode;
667 cnode = vector_slot(cmdvec, i);
668 if (!cnode)
669 continue;
670 if (cnode->node != CONFIG_NODE)
671 continue;
672
673 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
674 struct cmd_element *elem;
675 elem = vector_slot(cnode->cmd_vector, j);
676 if (!vty_command_is_common(elem))
677 continue;
678 vty_dump_element(elem, vty);
679 }
680 }
681 vty_out(vty, " </node>%s", VTY_NEWLINE);
682
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100683 for (i = 0; i < vector_active(cmdvec); ++i) {
684 struct cmd_node *cnode;
685 cnode = vector_slot(cmdvec, i);
686 if (!cnode)
687 continue;
688
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200689 /* De-dup node IDs: how many times has this same name been used before? Count the first
690 * occurence as _1 and omit that first suffix, so that the first occurence is called
691 * 'name', the second becomes 'name_2', then 'name_3', ... */
692 same_name_count = 1;
693 for (j = 0; j < i; ++j) {
694 struct cmd_node *cnode2;
695 cnode2 = vector_slot(cmdvec, j);
696 if (!cnode2)
697 continue;
698 if (strcmp(cnode->name, cnode2->name) == 0)
699 same_name_count ++;
700 }
701
702 vty_out(vty, " <node id='%s", cnode->name);
703 if (same_name_count > 1 || !*cnode->name)
704 vty_out(vty, "_%d", same_name_count);
705 vty_out(vty, "'>%s", VTY_NEWLINE);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100706
707 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
708 struct cmd_element *elem;
709 elem = vector_slot(cnode->cmd_vector, j);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200710 if (vty_command_is_common(elem))
711 continue;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100712 vty_dump_element(elem, vty);
713 }
714
715 vty_out(vty, " </node>%s", VTY_NEWLINE);
716 }
717
718 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
719
720 return 0;
721}
722
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200723/* Check if a command with given string exists at given node */
Harald Welteaddeaa32017-01-07 12:52:00 +0100724static int check_element_exists(struct cmd_node *cnode, const char *cmdstring)
725{
726 int i;
727
728 for (i = 0; i < vector_active(cnode->cmd_vector); ++i) {
729 struct cmd_element *elem;
730 elem = vector_slot(cnode->cmd_vector, i);
731 if (!elem->string)
732 continue;
733 if (!strcmp(elem->string, cmdstring))
734 return 1;
735 }
736 return 0;
737}
738
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200739/*! Install a command into a node
Harald Welte7acb30c2011-08-17 17:13:48 +0200740 * \param[in] ntype Node Type
741 * \param[cmd] element to be installed
742 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000743void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200744{
745 struct cmd_node *cnode;
746
747 cnode = vector_slot(cmdvec, ntype);
748
Harald Weltea99d45a2015-11-12 13:48:23 +0100749 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100750 /* ensure no _identical_ command has been registered at this
751 * node so far */
752 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200753
754 vector_set(cnode->cmd_vector, cmd);
755
756 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
757 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
758}
759
760/* Install a command into VIEW and ENABLE node */
761void install_element_ve(struct cmd_element *cmd)
762{
763 install_element(VIEW_NODE, cmd);
764 install_element(ENABLE_NODE, cmd);
765}
766
767#ifdef VTY_CRYPT_PW
768static unsigned char itoa64[] =
769 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
770
771static void to64(char *s, long v, int n)
772{
773 while (--n >= 0) {
774 *s++ = itoa64[v & 0x3f];
775 v >>= 6;
776 }
777}
778
779static char *zencrypt(const char *passwd)
780{
781 char salt[6];
782 struct timeval tv;
783 char *crypt(const char *, const char *);
784
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200785 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200786
787 to64(&salt[0], random(), 3);
788 to64(&salt[3], tv.tv_usec, 3);
789 salt[5] = '\0';
790
791 return crypt(passwd, salt);
792}
793#endif
794
795/* This function write configuration of this host. */
796static int config_write_host(struct vty *vty)
797{
798 if (host.name)
799 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
800
801 if (host.encrypt) {
802 if (host.password_encrypt)
803 vty_out(vty, "password 8 %s%s", host.password_encrypt,
804 VTY_NEWLINE);
805 if (host.enable_encrypt)
806 vty_out(vty, "enable password 8 %s%s",
807 host.enable_encrypt, VTY_NEWLINE);
808 } else {
809 if (host.password)
810 vty_out(vty, "password %s%s", host.password,
811 VTY_NEWLINE);
812 if (host.enable)
813 vty_out(vty, "enable password %s%s", host.enable,
814 VTY_NEWLINE);
815 }
816
817 if (host.advanced)
818 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
819
820 if (host.encrypt)
821 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
822
823 if (host.lines >= 0)
824 vty_out(vty, "service terminal-length %d%s", host.lines,
825 VTY_NEWLINE);
826
827 if (host.motdfile)
828 vty_out(vty, "banner motd file %s%s", host.motdfile,
829 VTY_NEWLINE);
830 else if (!host.motd)
831 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
832
833 return 1;
834}
835
836/* Utility function for getting command vector. */
837static vector cmd_node_vector(vector v, enum node_type ntype)
838{
839 struct cmd_node *cnode = vector_slot(v, ntype);
840 return cnode->cmd_vector;
841}
842
843/* Completion match types. */
844enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100845 no_match = 0,
846 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200847 extend_match,
848 ipv4_prefix_match,
849 ipv4_match,
850 ipv6_prefix_match,
851 ipv6_match,
852 range_match,
853 vararg_match,
854 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100855 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200856};
857
858static enum match_type cmd_ipv4_match(const char *str)
859{
860 const char *sp;
861 int dots = 0, nums = 0;
862 char buf[4];
863
864 if (str == NULL)
865 return partly_match;
866
867 for (;;) {
868 memset(buf, 0, sizeof(buf));
869 sp = str;
870 while (*str != '\0') {
871 if (*str == '.') {
872 if (dots >= 3)
873 return no_match;
874
875 if (*(str + 1) == '.')
876 return no_match;
877
878 if (*(str + 1) == '\0')
879 return partly_match;
880
881 dots++;
882 break;
883 }
884 if (!isdigit((int)*str))
885 return no_match;
886
887 str++;
888 }
889
890 if (str - sp > 3)
891 return no_match;
892
893 strncpy(buf, sp, str - sp);
894 if (atoi(buf) > 255)
895 return no_match;
896
897 nums++;
898
899 if (*str == '\0')
900 break;
901
902 str++;
903 }
904
905 if (nums < 4)
906 return partly_match;
907
908 return exact_match;
909}
910
911static enum match_type cmd_ipv4_prefix_match(const char *str)
912{
913 const char *sp;
914 int dots = 0;
915 char buf[4];
916
917 if (str == NULL)
918 return partly_match;
919
920 for (;;) {
921 memset(buf, 0, sizeof(buf));
922 sp = str;
923 while (*str != '\0' && *str != '/') {
924 if (*str == '.') {
925 if (dots == 3)
926 return no_match;
927
928 if (*(str + 1) == '.' || *(str + 1) == '/')
929 return no_match;
930
931 if (*(str + 1) == '\0')
932 return partly_match;
933
934 dots++;
935 break;
936 }
937
938 if (!isdigit((int)*str))
939 return no_match;
940
941 str++;
942 }
943
944 if (str - sp > 3)
945 return no_match;
946
947 strncpy(buf, sp, str - sp);
948 if (atoi(buf) > 255)
949 return no_match;
950
951 if (dots == 3) {
952 if (*str == '/') {
953 if (*(str + 1) == '\0')
954 return partly_match;
955
956 str++;
957 break;
958 } else if (*str == '\0')
959 return partly_match;
960 }
961
962 if (*str == '\0')
963 return partly_match;
964
965 str++;
966 }
967
968 sp = str;
969 while (*str != '\0') {
970 if (!isdigit((int)*str))
971 return no_match;
972
973 str++;
974 }
975
976 if (atoi(sp) > 32)
977 return no_match;
978
979 return exact_match;
980}
981
982#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
983#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
984#define STATE_START 1
985#define STATE_COLON 2
986#define STATE_DOUBLE 3
987#define STATE_ADDR 4
988#define STATE_DOT 5
989#define STATE_SLASH 6
990#define STATE_MASK 7
991
992#ifdef HAVE_IPV6
993
994static enum match_type cmd_ipv6_match(const char *str)
995{
996 int state = STATE_START;
997 int colons = 0, nums = 0, double_colon = 0;
998 const char *sp = NULL;
999 struct sockaddr_in6 sin6_dummy;
1000 int ret;
1001
1002 if (str == NULL)
1003 return partly_match;
1004
1005 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
1006 return no_match;
1007
1008 /* use inet_pton that has a better support,
1009 * for example inet_pton can support the automatic addresses:
1010 * ::1.2.3.4
1011 */
1012 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
1013
1014 if (ret == 1)
1015 return exact_match;
1016
1017 while (*str != '\0') {
1018 switch (state) {
1019 case STATE_START:
1020 if (*str == ':') {
1021 if (*(str + 1) != ':' && *(str + 1) != '\0')
1022 return no_match;
1023 colons--;
1024 state = STATE_COLON;
1025 } else {
1026 sp = str;
1027 state = STATE_ADDR;
1028 }
1029
1030 continue;
1031 case STATE_COLON:
1032 colons++;
1033 if (*(str + 1) == ':')
1034 state = STATE_DOUBLE;
1035 else {
1036 sp = str + 1;
1037 state = STATE_ADDR;
1038 }
1039 break;
1040 case STATE_DOUBLE:
1041 if (double_colon)
1042 return no_match;
1043
1044 if (*(str + 1) == ':')
1045 return no_match;
1046 else {
1047 if (*(str + 1) != '\0')
1048 colons++;
1049 sp = str + 1;
1050 state = STATE_ADDR;
1051 }
1052
1053 double_colon++;
1054 nums++;
1055 break;
1056 case STATE_ADDR:
1057 if (*(str + 1) == ':' || *(str + 1) == '\0') {
1058 if (str - sp > 3)
1059 return no_match;
1060
1061 nums++;
1062 state = STATE_COLON;
1063 }
1064 if (*(str + 1) == '.')
1065 state = STATE_DOT;
1066 break;
1067 case STATE_DOT:
1068 state = STATE_ADDR;
1069 break;
1070 default:
1071 break;
1072 }
1073
1074 if (nums > 8)
1075 return no_match;
1076
1077 if (colons > 7)
1078 return no_match;
1079
1080 str++;
1081 }
1082
1083#if 0
1084 if (nums < 11)
1085 return partly_match;
1086#endif /* 0 */
1087
1088 return exact_match;
1089}
1090
1091static enum match_type cmd_ipv6_prefix_match(const char *str)
1092{
1093 int state = STATE_START;
1094 int colons = 0, nums = 0, double_colon = 0;
1095 int mask;
1096 const char *sp = NULL;
1097 char *endptr = NULL;
1098
1099 if (str == NULL)
1100 return partly_match;
1101
1102 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
1103 return no_match;
1104
1105 while (*str != '\0' && state != STATE_MASK) {
1106 switch (state) {
1107 case STATE_START:
1108 if (*str == ':') {
1109 if (*(str + 1) != ':' && *(str + 1) != '\0')
1110 return no_match;
1111 colons--;
1112 state = STATE_COLON;
1113 } else {
1114 sp = str;
1115 state = STATE_ADDR;
1116 }
1117
1118 continue;
1119 case STATE_COLON:
1120 colons++;
1121 if (*(str + 1) == '/')
1122 return no_match;
1123 else if (*(str + 1) == ':')
1124 state = STATE_DOUBLE;
1125 else {
1126 sp = str + 1;
1127 state = STATE_ADDR;
1128 }
1129 break;
1130 case STATE_DOUBLE:
1131 if (double_colon)
1132 return no_match;
1133
1134 if (*(str + 1) == ':')
1135 return no_match;
1136 else {
1137 if (*(str + 1) != '\0' && *(str + 1) != '/')
1138 colons++;
1139 sp = str + 1;
1140
1141 if (*(str + 1) == '/')
1142 state = STATE_SLASH;
1143 else
1144 state = STATE_ADDR;
1145 }
1146
1147 double_colon++;
1148 nums += 1;
1149 break;
1150 case STATE_ADDR:
1151 if (*(str + 1) == ':' || *(str + 1) == '.'
1152 || *(str + 1) == '\0' || *(str + 1) == '/') {
1153 if (str - sp > 3)
1154 return no_match;
1155
1156 for (; sp <= str; sp++)
1157 if (*sp == '/')
1158 return no_match;
1159
1160 nums++;
1161
1162 if (*(str + 1) == ':')
1163 state = STATE_COLON;
1164 else if (*(str + 1) == '.')
1165 state = STATE_DOT;
1166 else if (*(str + 1) == '/')
1167 state = STATE_SLASH;
1168 }
1169 break;
1170 case STATE_DOT:
1171 state = STATE_ADDR;
1172 break;
1173 case STATE_SLASH:
1174 if (*(str + 1) == '\0')
1175 return partly_match;
1176
1177 state = STATE_MASK;
1178 break;
1179 default:
1180 break;
1181 }
1182
1183 if (nums > 11)
1184 return no_match;
1185
1186 if (colons > 7)
1187 return no_match;
1188
1189 str++;
1190 }
1191
1192 if (state < STATE_MASK)
1193 return partly_match;
1194
1195 mask = strtol(str, &endptr, 10);
1196 if (*endptr != '\0')
1197 return no_match;
1198
1199 if (mask < 0 || mask > 128)
1200 return no_match;
1201
1202/* I don't know why mask < 13 makes command match partly.
1203 Forgive me to make this comments. I Want to set static default route
1204 because of lack of function to originate default in ospf6d; sorry
1205 yasu
1206 if (mask < 13)
1207 return partly_match;
1208*/
1209
1210 return exact_match;
1211}
1212
1213#endif /* HAVE_IPV6 */
1214
1215#define DECIMAL_STRLEN_MAX 10
1216
1217static int cmd_range_match(const char *range, const char *str)
1218{
1219 char *p;
1220 char buf[DECIMAL_STRLEN_MAX + 1];
1221 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001222
1223 if (str == NULL)
1224 return 1;
1225
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001226 if (range[1] == '-') {
1227 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001228
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001229 val = strtol(str, &endptr, 10);
1230 if (*endptr != '\0')
1231 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001232
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001233 range += 2;
1234 p = strchr(range, '-');
1235 if (p == NULL)
1236 return 0;
1237 if (p - range > DECIMAL_STRLEN_MAX)
1238 return 0;
1239 strncpy(buf, range, p - range);
1240 buf[p - range] = '\0';
1241 min = -strtol(buf, &endptr, 10);
1242 if (*endptr != '\0')
1243 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001244
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001245 range = p + 1;
1246 p = strchr(range, '>');
1247 if (p == NULL)
1248 return 0;
1249 if (p - range > DECIMAL_STRLEN_MAX)
1250 return 0;
1251 strncpy(buf, range, p - range);
1252 buf[p - range] = '\0';
1253 max = strtol(buf, &endptr, 10);
1254 if (*endptr != '\0')
1255 return 0;
1256
1257 if (val < min || val > max)
1258 return 0;
1259 } else {
1260 unsigned long min, max, val;
1261
1262 val = strtoul(str, &endptr, 10);
1263 if (*endptr != '\0')
1264 return 0;
1265
1266 range++;
1267 p = strchr(range, '-');
1268 if (p == NULL)
1269 return 0;
1270 if (p - range > DECIMAL_STRLEN_MAX)
1271 return 0;
1272 strncpy(buf, range, p - range);
1273 buf[p - range] = '\0';
1274 min = strtoul(buf, &endptr, 10);
1275 if (*endptr != '\0')
1276 return 0;
1277
1278 range = p + 1;
1279 p = strchr(range, '>');
1280 if (p == NULL)
1281 return 0;
1282 if (p - range > DECIMAL_STRLEN_MAX)
1283 return 0;
1284 strncpy(buf, range, p - range);
1285 buf[p - range] = '\0';
1286 max = strtoul(buf, &endptr, 10);
1287 if (*endptr != '\0')
1288 return 0;
1289
1290 if (val < min || val > max)
1291 return 0;
1292 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001293
1294 return 1;
1295}
1296
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001297/* helper to retrieve the 'real' argument string from an optional argument */
1298static char *
1299cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001300{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001301 /* we've got "[blah]". We want to strip off the []s and redo the
1302 * match check for "blah"
1303 */
1304 size_t len = strlen(str);
1305 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001306
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001307 if (len < 3)
1308 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001309
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001310 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1311 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001312
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001313 memcpy(tmp, (str + 1), len - 2);
1314 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001315
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001316 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001317}
1318
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001319static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001320cmd_match(const char *str, const char *command,
1321 enum match_type min, bool recur)
1322{
1323
1324 if (recur && CMD_OPTION(str))
1325 {
1326 enum match_type ret;
1327 char *tmp = cmd_deopt(str);
1328
1329 /* this would be a bug in a command, however handle it gracefully
1330 * as it we only discover it if a user tries to run it
1331 */
1332 if (tmp == NULL)
1333 return no_match;
1334
1335 ret = cmd_match(tmp, command, min, false);
1336
1337 talloc_free(tmp);
1338
1339 return ret;
1340 }
1341 else if (CMD_VARARG(str))
1342 return vararg_match;
1343 else if (CMD_RANGE(str))
1344 {
1345 if (cmd_range_match(str, command))
1346 return range_match;
1347 }
1348#ifdef HAVE_IPV6
1349 else if (CMD_IPV6(str))
1350 {
1351 if (cmd_ipv6_match(command) >= min)
1352 return ipv6_match;
1353 }
1354 else if (CMD_IPV6_PREFIX(str))
1355 {
1356 if (cmd_ipv6_prefix_match(command) >= min)
1357 return ipv6_prefix_match;
1358 }
1359#endif /* HAVE_IPV6 */
1360 else if (CMD_IPV4(str))
1361 {
1362 if (cmd_ipv4_match(command) >= min)
1363 return ipv4_match;
1364 }
1365 else if (CMD_IPV4_PREFIX(str))
1366 {
1367 if (cmd_ipv4_prefix_match(command) >= min)
1368 return ipv4_prefix_match;
1369 }
1370 else if (CMD_VARIABLE(str))
1371 return extend_match;
1372 else if (strncmp(command, str, strlen(command)) == 0)
1373 {
1374 if (strcmp(command, str) == 0)
1375 return exact_match;
1376 else if (partly_match >= min)
1377 return partly_match;
1378 }
1379
1380 return no_match;
1381}
1382
1383/* Filter vector at the specified index and by the given command string, to
1384 * the desired matching level (thus allowing part matches), and return match
1385 * type flag.
1386 */
1387static enum match_type
1388cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001389{
1390 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001391 struct cmd_element *cmd_element;
1392 enum match_type match_type;
1393 vector descvec;
1394 struct desc *desc;
1395
1396 match_type = no_match;
1397
1398 /* If command and cmd_element string does not match set NULL to vector */
1399 for (i = 0; i < vector_active(v); i++)
1400 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001401 if (index >= vector_active(cmd_element->strvec))
1402 vector_slot(v, i) = NULL;
1403 else {
1404 unsigned int j;
1405 int matched = 0;
1406
1407 descvec =
1408 vector_slot(cmd_element->strvec, index);
1409
1410 for (j = 0; j < vector_active(descvec); j++)
1411 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001412 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001413
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001414 ret = cmd_match (desc->cmd, command, level, true);
1415
1416 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001417 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001418
1419 if (match_type < ret)
1420 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001421 }
1422 if (!matched)
1423 vector_slot(v, i) = NULL;
1424 }
1425 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001426
1427 if (match_type == no_match)
1428 return no_match;
1429
1430 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1431 * go again and filter out commands whose argument (at this index) is
1432 * 'weaker'. E.g., if we have 2 commands:
1433 *
1434 * foo bar <1-255>
1435 * foo bar BLAH
1436 *
1437 * and the command string is 'foo bar 10', then we will get here with with
1438 * 'range_match' being the strongest match. However, if 'BLAH' came
1439 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1440 *
1441 * If we don't do a 2nd pass and filter it out, the higher-layers will
1442 * consider this to be ambiguous.
1443 */
1444 for (i = 0; i < vector_active(v); i++)
1445 if ((cmd_element = vector_slot(v, i)) != NULL) {
1446 if (index >= vector_active(cmd_element->strvec))
1447 vector_slot(v, i) = NULL;
1448 else {
1449 unsigned int j;
1450 int matched = 0;
1451
1452 descvec =
1453 vector_slot(cmd_element->strvec, index);
1454
1455 for (j = 0; j < vector_active(descvec); j++)
1456 if ((desc = vector_slot(descvec, j))) {
1457 enum match_type ret;
1458
1459 ret = cmd_match(desc->cmd, command, any_match, true);
1460
1461 if (ret >= match_type)
1462 matched++;
1463 }
1464 if (!matched)
1465 vector_slot(v, i) = NULL;
1466 }
1467 }
1468
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001469 return match_type;
1470}
1471
1472/* Check ambiguous match */
1473static int
1474is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1475{
1476 unsigned int i;
1477 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001478 struct cmd_element *cmd_element;
1479 const char *matched = NULL;
1480 vector descvec;
1481 struct desc *desc;
1482
1483 for (i = 0; i < vector_active(v); i++)
1484 if ((cmd_element = vector_slot(v, i)) != NULL) {
1485 int match = 0;
1486
1487 descvec = vector_slot(cmd_element->strvec, index);
1488
1489 for (j = 0; j < vector_active(descvec); j++)
1490 if ((desc = vector_slot(descvec, j))) {
1491 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001492 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001493
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001494 if (CMD_OPTION(str))
1495 if ((str = cmd_deopt(str)) == NULL)
1496 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001497
1498 switch (type) {
1499 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001500 if (!(CMD_VARIABLE (str))
1501 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001502 match++;
1503 break;
1504 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001505 if (!(CMD_VARIABLE (str))
1506 && strncmp(command, str, strlen (command)) == 0)
1507 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001508 if (matched
1509 && strcmp(matched,
1510 str) != 0)
1511 return 1; /* There is ambiguous match. */
1512 else
1513 matched = str;
1514 match++;
1515 }
1516 break;
1517 case range_match:
1518 if (cmd_range_match
1519 (str, command)) {
1520 if (matched
1521 && strcmp(matched,
1522 str) != 0)
1523 return 1;
1524 else
1525 matched = str;
1526 match++;
1527 }
1528 break;
1529#ifdef HAVE_IPV6
1530 case ipv6_match:
1531 if (CMD_IPV6(str))
1532 match++;
1533 break;
1534 case ipv6_prefix_match:
1535 if ((ret =
1536 cmd_ipv6_prefix_match
1537 (command)) != no_match) {
1538 if (ret == partly_match)
1539 return 2; /* There is incomplete match. */
1540
1541 match++;
1542 }
1543 break;
1544#endif /* HAVE_IPV6 */
1545 case ipv4_match:
1546 if (CMD_IPV4(str))
1547 match++;
1548 break;
1549 case ipv4_prefix_match:
1550 if ((ret =
1551 cmd_ipv4_prefix_match
1552 (command)) != no_match) {
1553 if (ret == partly_match)
1554 return 2; /* There is incomplete match. */
1555
1556 match++;
1557 }
1558 break;
1559 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001560 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001561 match++;
1562 break;
1563 case no_match:
1564 default:
1565 break;
1566 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001567
1568 if (CMD_OPTION(desc->cmd))
1569 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001570 }
1571 if (!match)
1572 vector_slot(v, i) = NULL;
1573 }
1574 return 0;
1575}
1576
1577/* If src matches dst return dst string, otherwise return NULL */
1578static const char *cmd_entry_function(const char *src, const char *dst)
1579{
1580 /* Skip variable arguments. */
1581 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1582 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1583 return NULL;
1584
1585 /* In case of 'command \t', given src is NULL string. */
1586 if (src == NULL)
1587 return dst;
1588
1589 /* Matched with input string. */
1590 if (strncmp(src, dst, strlen(src)) == 0)
1591 return dst;
1592
1593 return NULL;
1594}
1595
1596/* If src matches dst return dst string, otherwise return NULL */
1597/* This version will return the dst string always if it is
1598 CMD_VARIABLE for '?' key processing */
1599static const char *cmd_entry_function_desc(const char *src, const char *dst)
1600{
1601 if (CMD_VARARG(dst))
1602 return dst;
1603
1604 if (CMD_RANGE(dst)) {
1605 if (cmd_range_match(dst, src))
1606 return dst;
1607 else
1608 return NULL;
1609 }
1610#ifdef HAVE_IPV6
1611 if (CMD_IPV6(dst)) {
1612 if (cmd_ipv6_match(src))
1613 return dst;
1614 else
1615 return NULL;
1616 }
1617
1618 if (CMD_IPV6_PREFIX(dst)) {
1619 if (cmd_ipv6_prefix_match(src))
1620 return dst;
1621 else
1622 return NULL;
1623 }
1624#endif /* HAVE_IPV6 */
1625
1626 if (CMD_IPV4(dst)) {
1627 if (cmd_ipv4_match(src))
1628 return dst;
1629 else
1630 return NULL;
1631 }
1632
1633 if (CMD_IPV4_PREFIX(dst)) {
1634 if (cmd_ipv4_prefix_match(src))
1635 return dst;
1636 else
1637 return NULL;
1638 }
1639
1640 /* Optional or variable commands always match on '?' */
1641 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1642 return dst;
1643
1644 /* In case of 'command \t', given src is NULL string. */
1645 if (src == NULL)
1646 return dst;
1647
1648 if (strncmp(src, dst, strlen(src)) == 0)
1649 return dst;
1650 else
1651 return NULL;
1652}
1653
1654/* Check same string element existence. If it isn't there return
1655 1. */
1656static int cmd_unique_string(vector v, const char *str)
1657{
1658 unsigned int i;
1659 char *match;
1660
1661 for (i = 0; i < vector_active(v); i++)
1662 if ((match = vector_slot(v, i)) != NULL)
1663 if (strcmp(match, str) == 0)
1664 return 0;
1665 return 1;
1666}
1667
1668/* Compare string to description vector. If there is same string
1669 return 1 else return 0. */
1670static int desc_unique_string(vector v, const char *str)
1671{
1672 unsigned int i;
1673 struct desc *desc;
1674
1675 for (i = 0; i < vector_active(v); i++)
1676 if ((desc = vector_slot(v, i)) != NULL)
1677 if (strcmp(desc->cmd, str) == 0)
1678 return 1;
1679 return 0;
1680}
1681
1682static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1683{
1684 if (first_word != NULL &&
1685 node != AUTH_NODE &&
1686 node != VIEW_NODE &&
1687 node != AUTH_ENABLE_NODE &&
1688 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1689 return 1;
1690 return 0;
1691}
1692
1693/* '?' describe command support. */
1694static vector
1695cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1696{
1697 unsigned int i;
1698 vector cmd_vector;
1699#define INIT_MATCHVEC_SIZE 10
1700 vector matchvec;
1701 struct cmd_element *cmd_element;
1702 unsigned int index;
1703 int ret;
1704 enum match_type match;
1705 char *command;
1706 static struct desc desc_cr = { "<cr>", "" };
1707
1708 /* Set index. */
1709 if (vector_active(vline) == 0) {
1710 *status = CMD_ERR_NO_MATCH;
1711 return NULL;
1712 } else
1713 index = vector_active(vline) - 1;
1714
1715 /* Make copy vector of current node's command vector. */
1716 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1717
1718 /* Prepare match vector */
1719 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1720
1721 /* Filter commands. */
1722 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001723 for (i = 0; i < index; i++) {
1724 command = vector_slot(vline, i);
1725 if (!command)
1726 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001727
Harald Welte80d30fe2013-02-12 11:08:57 +01001728 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001729
Harald Welte80d30fe2013-02-12 11:08:57 +01001730 if (match == vararg_match) {
1731 struct cmd_element *cmd_element;
1732 vector descvec;
1733 unsigned int j, k;
1734
1735 for (j = 0; j < vector_active(cmd_vector); j++)
1736 if ((cmd_element =
1737 vector_slot(cmd_vector, j)) != NULL
1738 &&
1739 (vector_active(cmd_element->strvec))) {
1740 descvec =
1741 vector_slot(cmd_element->
1742 strvec,
1743 vector_active
1744 (cmd_element->
1745 strvec) - 1);
1746 for (k = 0;
1747 k < vector_active(descvec);
1748 k++) {
1749 struct desc *desc =
1750 vector_slot(descvec,
1751 k);
1752 vector_set(matchvec,
1753 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001754 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001755 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001756
Harald Welte80d30fe2013-02-12 11:08:57 +01001757 vector_set(matchvec, &desc_cr);
1758 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001759
Harald Welte80d30fe2013-02-12 11:08:57 +01001760 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001761 }
1762
Harald Welte80d30fe2013-02-12 11:08:57 +01001763 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1764 match)) == 1) {
1765 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001766 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001767 *status = CMD_ERR_AMBIGUOUS;
1768 return NULL;
1769 } else if (ret == 2) {
1770 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001771 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001772 *status = CMD_ERR_NO_MATCH;
1773 return NULL;
1774 }
1775 }
1776
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001777 /* Prepare match vector */
1778 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1779
1780 /* Make sure that cmd_vector is filtered based on current word */
1781 command = vector_slot(vline, index);
1782 if (command)
Vadim Yanitskiy49a0dec2017-06-12 03:49:38 +07001783 cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001784
1785 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001786 for (i = 0; i < vector_active(cmd_vector); i++) {
1787 const char *string = NULL;
1788 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001789
Harald Welte80d30fe2013-02-12 11:08:57 +01001790 cmd_element = vector_slot(cmd_vector, i);
1791 if (!cmd_element)
1792 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001793
Harald Welted17aa592013-02-12 11:11:34 +01001794 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1795 continue;
1796
Harald Welte80d30fe2013-02-12 11:08:57 +01001797 strvec = cmd_element->strvec;
1798
1799 /* if command is NULL, index may be equal to vector_active */
1800 if (command && index >= vector_active(strvec))
1801 vector_slot(cmd_vector, i) = NULL;
1802 else {
1803 /* Check if command is completed. */
1804 if (command == NULL
1805 && index == vector_active(strvec)) {
1806 string = "<cr>";
1807 if (!desc_unique_string(matchvec, string))
1808 vector_set(matchvec, &desc_cr);
1809 } else {
1810 unsigned int j;
1811 vector descvec = vector_slot(strvec, index);
1812 struct desc *desc;
1813
1814 for (j = 0; j < vector_active(descvec); j++) {
1815 desc = vector_slot(descvec, j);
1816 if (!desc)
1817 continue;
1818 string = cmd_entry_function_desc
1819 (command, desc->cmd);
1820 if (!string)
1821 continue;
1822 /* Uniqueness check */
1823 if (!desc_unique_string(matchvec, string))
1824 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001825 }
1826 }
1827 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001828 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001829 vector_free(cmd_vector);
1830
1831 if (vector_slot(matchvec, 0) == NULL) {
1832 vector_free(matchvec);
1833 *status = CMD_ERR_NO_MATCH;
1834 } else
1835 *status = CMD_SUCCESS;
1836
1837 return matchvec;
1838}
1839
1840vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1841{
1842 vector ret;
1843
1844 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1845 enum node_type onode;
1846 vector shifted_vline;
1847 unsigned int index;
1848
1849 onode = vty->node;
1850 vty->node = ENABLE_NODE;
1851 /* We can try it on enable node, cos' the vty is authenticated */
1852
1853 shifted_vline = vector_init(vector_count(vline));
1854 /* use memcpy? */
1855 for (index = 1; index < vector_active(vline); index++) {
1856 vector_set_index(shifted_vline, index - 1,
1857 vector_lookup(vline, index));
1858 }
1859
1860 ret = cmd_describe_command_real(shifted_vline, vty, status);
1861
1862 vector_free(shifted_vline);
1863 vty->node = onode;
1864 return ret;
1865 }
1866
1867 return cmd_describe_command_real(vline, vty, status);
1868}
1869
1870/* Check LCD of matched command. */
1871static int cmd_lcd(char **matched)
1872{
1873 int i;
1874 int j;
1875 int lcd = -1;
1876 char *s1, *s2;
1877 char c1, c2;
1878
1879 if (matched[0] == NULL || matched[1] == NULL)
1880 return 0;
1881
1882 for (i = 1; matched[i] != NULL; i++) {
1883 s1 = matched[i - 1];
1884 s2 = matched[i];
1885
1886 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1887 if (c1 != c2)
1888 break;
1889
1890 if (lcd < 0)
1891 lcd = j;
1892 else {
1893 if (lcd > j)
1894 lcd = j;
1895 }
1896 }
1897 return lcd;
1898}
1899
1900/* Command line completion support. */
1901static char **cmd_complete_command_real(vector vline, struct vty *vty,
1902 int *status)
1903{
1904 unsigned int i;
1905 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1906#define INIT_MATCHVEC_SIZE 10
1907 vector matchvec;
1908 struct cmd_element *cmd_element;
1909 unsigned int index;
1910 char **match_str;
1911 struct desc *desc;
1912 vector descvec;
1913 char *command;
1914 int lcd;
1915
1916 if (vector_active(vline) == 0) {
1917 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001918 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001919 return NULL;
1920 } else
1921 index = vector_active(vline) - 1;
1922
1923 /* First, filter by preceeding command string */
1924 for (i = 0; i < index; i++)
1925 if ((command = vector_slot(vline, i))) {
1926 enum match_type match;
1927 int ret;
1928
1929 /* First try completion match, if there is exactly match return 1 */
1930 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001931 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001932
1933 /* If there is exact match then filter ambiguous match else check
1934 ambiguousness. */
1935 if ((ret =
1936 is_cmd_ambiguous(command, cmd_vector, i,
1937 match)) == 1) {
1938 vector_free(cmd_vector);
1939 *status = CMD_ERR_AMBIGUOUS;
1940 return NULL;
1941 }
1942 /*
1943 else if (ret == 2)
1944 {
1945 vector_free (cmd_vector);
1946 *status = CMD_ERR_NO_MATCH;
1947 return NULL;
1948 }
1949 */
1950 }
1951
1952 /* Prepare match vector. */
1953 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1954
1955 /* Now we got into completion */
1956 for (i = 0; i < vector_active(cmd_vector); i++)
1957 if ((cmd_element = vector_slot(cmd_vector, i))) {
1958 const char *string;
1959 vector strvec = cmd_element->strvec;
1960
1961 /* Check field length */
1962 if (index >= vector_active(strvec))
1963 vector_slot(cmd_vector, i) = NULL;
1964 else {
1965 unsigned int j;
1966
1967 descvec = vector_slot(strvec, index);
1968 for (j = 0; j < vector_active(descvec); j++)
1969 if ((desc = vector_slot(descvec, j))) {
1970 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1971 if (cmd_unique_string (matchvec, string))
1972 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1973 }
1974 }
1975 }
1976
1977 /* We don't need cmd_vector any more. */
1978 vector_free(cmd_vector);
1979
1980 /* No matched command */
1981 if (vector_slot(matchvec, 0) == NULL) {
1982 vector_free(matchvec);
1983
1984 /* In case of 'command \t' pattern. Do you need '?' command at
1985 the end of the line. */
Pau Espin Pedrol0f7bcb52017-11-02 19:08:14 +01001986 if (vector_slot(vline, index) == NULL)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001987 *status = CMD_ERR_NOTHING_TODO;
1988 else
1989 *status = CMD_ERR_NO_MATCH;
1990 return NULL;
1991 }
1992
1993 /* Only one matched */
1994 if (vector_slot(matchvec, 1) == NULL) {
1995 match_str = (char **)matchvec->index;
1996 vector_only_wrapper_free(matchvec);
1997 *status = CMD_COMPLETE_FULL_MATCH;
1998 return match_str;
1999 }
2000 /* Make it sure last element is NULL. */
2001 vector_set(matchvec, NULL);
2002
2003 /* Check LCD of matched strings. */
2004 if (vector_slot(vline, index) != NULL) {
2005 lcd = cmd_lcd((char **)matchvec->index);
2006
2007 if (lcd) {
2008 int len = strlen(vector_slot(vline, index));
2009
2010 if (len < lcd) {
2011 char *lcdstr;
2012
2013 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
2014 "complete-lcdstr");
2015 memcpy(lcdstr, matchvec->index[0], lcd);
2016 lcdstr[lcd] = '\0';
2017
2018 /* match_str = (char **) &lcdstr; */
2019
2020 /* Free matchvec. */
2021 for (i = 0; i < vector_active(matchvec); i++) {
2022 if (vector_slot(matchvec, i))
2023 talloc_free(vector_slot(matchvec, i));
2024 }
2025 vector_free(matchvec);
2026
2027 /* Make new matchvec. */
2028 matchvec = vector_init(INIT_MATCHVEC_SIZE);
2029 vector_set(matchvec, lcdstr);
2030 match_str = (char **)matchvec->index;
2031 vector_only_wrapper_free(matchvec);
2032
2033 *status = CMD_COMPLETE_MATCH;
2034 return match_str;
2035 }
2036 }
2037 }
2038
2039 match_str = (char **)matchvec->index;
2040 vector_only_wrapper_free(matchvec);
2041 *status = CMD_COMPLETE_LIST_MATCH;
2042 return match_str;
2043}
2044
2045char **cmd_complete_command(vector vline, struct vty *vty, int *status)
2046{
2047 char **ret;
2048
2049 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2050 enum node_type onode;
2051 vector shifted_vline;
2052 unsigned int index;
2053
2054 onode = vty->node;
2055 vty->node = ENABLE_NODE;
2056 /* We can try it on enable node, cos' the vty is authenticated */
2057
2058 shifted_vline = vector_init(vector_count(vline));
2059 /* use memcpy? */
2060 for (index = 1; index < vector_active(vline); index++) {
2061 vector_set_index(shifted_vline, index - 1,
2062 vector_lookup(vline, index));
2063 }
2064
2065 ret = cmd_complete_command_real(shifted_vline, vty, status);
2066
2067 vector_free(shifted_vline);
2068 vty->node = onode;
2069 return ret;
2070 }
2071
2072 return cmd_complete_command_real(vline, vty, status);
2073}
2074
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002075static struct vty_parent_node *vty_parent(struct vty *vty)
2076{
2077 return llist_first_entry_or_null(&vty->parent_nodes,
2078 struct vty_parent_node,
2079 entry);
2080}
2081
2082static bool vty_pop_parent(struct vty *vty)
2083{
2084 struct vty_parent_node *parent = vty_parent(vty);
2085 if (!parent)
2086 return false;
2087 llist_del(&parent->entry);
2088 vty->node = parent->node;
2089 vty->priv = parent->priv;
2090 if (vty->indent)
2091 talloc_free(vty->indent);
2092 vty->indent = parent->indent;
2093 talloc_free(parent);
2094 return true;
2095}
2096
2097static void vty_clear_parents(struct vty *vty)
2098{
2099 while (vty_pop_parent(vty));
2100}
2101
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002102/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002103/*
2104 * This function MUST eventually converge on a node when called repeatedly,
2105 * there must not be any cycles.
2106 * All 'config' nodes shall converge on CONFIG_NODE.
2107 * All other 'enable' nodes shall converge on ENABLE_NODE.
2108 * All 'view' only nodes shall converge on VIEW_NODE.
2109 * All other nodes shall converge on themselves or it must be ensured,
2110 * that the user's rights are not extended anyhow by calling this function.
2111 *
2112 * Note that these requirements also apply to all functions that are used
2113 * as go_parent_cb.
2114 * Note also that this function relies on the is_config_child callback to
2115 * recognize non-config nodes if go_parent_cb is not set.
2116 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00002117int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002118{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002119 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002120 case AUTH_NODE:
2121 case VIEW_NODE:
2122 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002123 case CONFIG_NODE:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002124 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002125 break;
2126
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002127 case AUTH_ENABLE_NODE:
2128 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002129 vty_clear_parents(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002130 break;
2131
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002132 case CFG_LOG_NODE:
2133 case VTY_NODE:
2134 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002135 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002136 break;
2137
2138 default:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002139 if (host.app_info->go_parent_cb) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002140 host.app_info->go_parent_cb(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002141 vty_pop_parent(vty);
2142 }
2143 else if (is_config_child(vty)) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002144 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002145 vty_clear_parents(vty);
2146 }
2147 else {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002148 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002149 vty_clear_parents(vty);
2150 }
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002151 break;
2152 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002153
2154 return vty->node;
2155}
2156
2157/* Execute command by argument vline vector. */
2158static int
2159cmd_execute_command_real(vector vline, struct vty *vty,
2160 struct cmd_element **cmd)
2161{
2162 unsigned int i;
2163 unsigned int index;
2164 vector cmd_vector;
2165 struct cmd_element *cmd_element;
2166 struct cmd_element *matched_element;
2167 unsigned int matched_count, incomplete_count;
2168 int argc;
2169 const char *argv[CMD_ARGC_MAX];
2170 enum match_type match = 0;
2171 int varflag;
2172 char *command;
2173
2174 /* Make copy of command elements. */
2175 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2176
2177 for (index = 0; index < vector_active(vline); index++)
2178 if ((command = vector_slot(vline, index))) {
2179 int ret;
2180
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002181 match = cmd_filter(command, cmd_vector, index,
2182 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002183
2184 if (match == vararg_match)
2185 break;
2186
2187 ret =
2188 is_cmd_ambiguous(command, cmd_vector, index, match);
2189
2190 if (ret == 1) {
2191 vector_free(cmd_vector);
2192 return CMD_ERR_AMBIGUOUS;
2193 } else if (ret == 2) {
2194 vector_free(cmd_vector);
2195 return CMD_ERR_NO_MATCH;
2196 }
2197 }
2198
2199 /* Check matched count. */
2200 matched_element = NULL;
2201 matched_count = 0;
2202 incomplete_count = 0;
2203
2204 for (i = 0; i < vector_active(cmd_vector); i++)
2205 if ((cmd_element = vector_slot(cmd_vector, i))) {
2206 if (match == vararg_match
2207 || index >= cmd_element->cmdsize) {
2208 matched_element = cmd_element;
2209#if 0
2210 printf("DEBUG: %s\n", cmd_element->string);
2211#endif
2212 matched_count++;
2213 } else {
2214 incomplete_count++;
2215 }
2216 }
2217
2218 /* Finish of using cmd_vector. */
2219 vector_free(cmd_vector);
2220
2221 /* To execute command, matched_count must be 1. */
2222 if (matched_count == 0) {
2223 if (incomplete_count)
2224 return CMD_ERR_INCOMPLETE;
2225 else
2226 return CMD_ERR_NO_MATCH;
2227 }
2228
2229 if (matched_count > 1)
2230 return CMD_ERR_AMBIGUOUS;
2231
2232 /* Argument treatment */
2233 varflag = 0;
2234 argc = 0;
2235
2236 for (i = 0; i < vector_active(vline); i++) {
2237 if (varflag)
2238 argv[argc++] = vector_slot(vline, i);
2239 else {
2240 vector descvec =
2241 vector_slot(matched_element->strvec, i);
2242
2243 if (vector_active(descvec) == 1) {
2244 struct desc *desc = vector_slot(descvec, 0);
2245
2246 if (CMD_VARARG(desc->cmd))
2247 varflag = 1;
2248
2249 if (varflag || CMD_VARIABLE(desc->cmd)
2250 || CMD_OPTION(desc->cmd))
2251 argv[argc++] = vector_slot(vline, i);
2252 } else
2253 argv[argc++] = vector_slot(vline, i);
2254 }
2255
2256 if (argc >= CMD_ARGC_MAX)
2257 return CMD_ERR_EXEED_ARGC_MAX;
2258 }
2259
2260 /* For vtysh execution. */
2261 if (cmd)
2262 *cmd = matched_element;
2263
2264 if (matched_element->daemon)
2265 return CMD_SUCCESS_DAEMON;
2266
2267 /* Execute matched command. */
2268 return (*matched_element->func) (matched_element, vty, argc, argv);
2269}
2270
2271int
2272cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2273 int vtysh)
2274{
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002275 int ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002276 enum node_type onode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002277
2278 onode = vty->node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002279
2280 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2281 vector shifted_vline;
2282 unsigned int index;
2283
2284 vty->node = ENABLE_NODE;
2285 /* We can try it on enable node, cos' the vty is authenticated */
2286
2287 shifted_vline = vector_init(vector_count(vline));
2288 /* use memcpy? */
2289 for (index = 1; index < vector_active(vline); index++) {
2290 vector_set_index(shifted_vline, index - 1,
2291 vector_lookup(vline, index));
2292 }
2293
2294 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2295
2296 vector_free(shifted_vline);
2297 vty->node = onode;
2298 return ret;
2299 }
2300
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002301 return cmd_execute_command_real(vline, vty, cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002302}
2303
2304/* Execute command by argument readline. */
2305int
2306cmd_execute_command_strict(vector vline, struct vty *vty,
2307 struct cmd_element **cmd)
2308{
2309 unsigned int i;
2310 unsigned int index;
2311 vector cmd_vector;
2312 struct cmd_element *cmd_element;
2313 struct cmd_element *matched_element;
2314 unsigned int matched_count, incomplete_count;
2315 int argc;
2316 const char *argv[CMD_ARGC_MAX];
2317 int varflag;
2318 enum match_type match = 0;
2319 char *command;
2320
2321 /* Make copy of command element */
2322 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2323
2324 for (index = 0; index < vector_active(vline); index++)
2325 if ((command = vector_slot(vline, index))) {
2326 int ret;
2327
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002328 match = cmd_filter(vector_slot(vline, index),
2329 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002330
2331 /* If command meets '.VARARG' then finish matching. */
2332 if (match == vararg_match)
2333 break;
2334
2335 ret =
2336 is_cmd_ambiguous(command, cmd_vector, index, match);
2337 if (ret == 1) {
2338 vector_free(cmd_vector);
2339 return CMD_ERR_AMBIGUOUS;
2340 }
2341 if (ret == 2) {
2342 vector_free(cmd_vector);
2343 return CMD_ERR_NO_MATCH;
2344 }
2345 }
2346
2347 /* Check matched count. */
2348 matched_element = NULL;
2349 matched_count = 0;
2350 incomplete_count = 0;
2351 for (i = 0; i < vector_active(cmd_vector); i++)
2352 if (vector_slot(cmd_vector, i) != NULL) {
2353 cmd_element = vector_slot(cmd_vector, i);
2354
2355 if (match == vararg_match
2356 || index >= cmd_element->cmdsize) {
2357 matched_element = cmd_element;
2358 matched_count++;
2359 } else
2360 incomplete_count++;
2361 }
2362
2363 /* Finish of using cmd_vector. */
2364 vector_free(cmd_vector);
2365
2366 /* To execute command, matched_count must be 1. */
2367 if (matched_count == 0) {
2368 if (incomplete_count)
2369 return CMD_ERR_INCOMPLETE;
2370 else
2371 return CMD_ERR_NO_MATCH;
2372 }
2373
2374 if (matched_count > 1)
2375 return CMD_ERR_AMBIGUOUS;
2376
2377 /* Argument treatment */
2378 varflag = 0;
2379 argc = 0;
2380
2381 for (i = 0; i < vector_active(vline); i++) {
2382 if (varflag)
2383 argv[argc++] = vector_slot(vline, i);
2384 else {
2385 vector descvec =
2386 vector_slot(matched_element->strvec, i);
2387
2388 if (vector_active(descvec) == 1) {
2389 struct desc *desc = vector_slot(descvec, 0);
2390
2391 if (CMD_VARARG(desc->cmd))
2392 varflag = 1;
2393
2394 if (varflag || CMD_VARIABLE(desc->cmd)
2395 || CMD_OPTION(desc->cmd))
2396 argv[argc++] = vector_slot(vline, i);
2397 } else
2398 argv[argc++] = vector_slot(vline, i);
2399 }
2400
2401 if (argc >= CMD_ARGC_MAX)
2402 return CMD_ERR_EXEED_ARGC_MAX;
2403 }
2404
2405 /* For vtysh execution. */
2406 if (cmd)
2407 *cmd = matched_element;
2408
2409 if (matched_element->daemon)
2410 return CMD_SUCCESS_DAEMON;
2411
2412 /* Now execute matched command */
2413 return (*matched_element->func) (matched_element, vty, argc, argv);
2414}
2415
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002416static inline size_t len(const char *str)
2417{
2418 return str? strlen(str) : 0;
2419}
2420
Neels Hofmeyr00b5ed32017-09-20 00:46:03 +02002421/*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a
2422 * is longer than b, a must start with exactly b, and vice versa.
2423 * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b.
2424 */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002425static int indent_cmp(const char *a, const char *b)
2426{
2427 size_t al, bl;
2428 al = len(a);
2429 bl = len(b);
2430 if (al > bl) {
2431 if (bl && strncmp(a, b, bl) != 0)
2432 return EINVAL;
2433 return 1;
2434 }
2435 /* al <= bl */
2436 if (al && strncmp(a, b, al) != 0)
2437 return EINVAL;
2438 return (al < bl)? -1 : 0;
2439}
2440
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002441/* Configration make from file. */
2442int config_from_file(struct vty *vty, FILE * fp)
2443{
2444 int ret;
2445 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002446 char *indent;
2447 int cmp;
2448 struct vty_parent_node this_node;
2449 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002450
2451 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002452 indent = NULL;
2453 vline = NULL;
2454 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002455
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002456 if (ret != CMD_SUCCESS)
2457 goto return_invalid_indent;
2458
2459 /* In case of comment or empty line */
2460 if (vline == NULL) {
2461 if (indent) {
2462 talloc_free(indent);
2463 indent = NULL;
2464 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002465 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002466 }
2467
Neels Hofmeyr43063632017-09-19 23:54:01 +02002468 /* We have a nonempty line. */
2469 if (!vty->indent) {
2470 /* We have just entered a node and expecting the first child to come up; but we
2471 * may also skip right back to a parent or ancestor level. */
2472 parent = vty_parent(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002473
Neels Hofmeyr43063632017-09-19 23:54:01 +02002474 /* If there is no parent, record any indentation we encounter. */
2475 cmp = parent ? indent_cmp(indent, parent->indent) : 1;
2476
2477 if (cmp == EINVAL)
2478 goto return_invalid_indent;
2479
2480 if (cmp <= 0) {
2481 /* We have gone right back to the parent level or higher, we are skipping
2482 * this child node level entirely. Pop the parent to go back to a node
2483 * that was actually there (to reinstate vty->indent) and re-use below
2484 * go-parent while-loop to find an accurate match of indent in the node
2485 * ancestry. */
2486 vty_go_parent(vty);
2487 } else {
2488 /* The indent is deeper than the just entered parent, record the new
2489 * indentation characters. */
2490 vty->indent = talloc_strdup(vty, indent);
2491 /* This *is* the new indentation. */
2492 cmp = 0;
2493 }
2494 } else {
2495 /* There is a known indentation for this node level, validate and detect node
2496 * exits. */
2497 cmp = indent_cmp(indent, vty->indent);
2498 if (cmp == EINVAL)
2499 goto return_invalid_indent;
2500 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002501
2502 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2503 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2504 while (cmp < 0) {
2505 vty_go_parent(vty);
2506 cmp = indent_cmp(indent, vty->indent);
2507 if (cmp == EINVAL)
2508 goto return_invalid_indent;
2509 }
2510
2511 /* More indent without having entered a child node level? Either the parent node's indent
2512 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2513 * or the indentation increased even though the vty command didn't enter a child. */
2514 if (cmp > 0)
2515 goto return_invalid_indent;
2516
2517 /* Remember the current node before the command possibly changes it. */
2518 this_node = (struct vty_parent_node){
2519 .node = vty->node,
2520 .priv = vty->priv,
2521 .indent = vty->indent,
2522 };
2523
2524 parent = vty_parent(vty);
2525 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002526 cmd_free_strvec(vline);
2527
2528 if (ret != CMD_SUCCESS && ret != CMD_WARNING
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002529 && ret != CMD_ERR_NOTHING_TODO) {
2530 if (indent) {
2531 talloc_free(indent);
2532 indent = NULL;
2533 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002534 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002535 }
2536
2537 /* If we have stepped down into a child node, push a parent frame.
2538 * The causality is such: we don't expect every single node entry implementation to push
2539 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2540 * a parent node. Hence if the node changed without the parent node changing, we must
2541 * have stepped into a child node (and now expect a deeper indent). */
2542 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2543 /* Push the parent node. */
2544 parent = talloc_zero(vty, struct vty_parent_node);
2545 *parent = this_node;
2546 llist_add(&parent->entry, &vty->parent_nodes);
2547
2548 /* The current talloc'ed vty->indent string will now be owned by this parent
2549 * struct. Indicate that we don't know what deeper indent characters the user
2550 * will choose. */
2551 vty->indent = NULL;
2552 }
2553
2554 if (indent) {
2555 talloc_free(indent);
2556 indent = NULL;
2557 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002558 }
2559 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002560
2561return_invalid_indent:
2562 if (vline)
2563 cmd_free_strvec(vline);
2564 if (indent) {
2565 talloc_free(indent);
2566 indent = NULL;
2567 }
2568 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002569}
2570
2571/* Configration from terminal */
2572DEFUN(config_terminal,
2573 config_terminal_cmd,
2574 "configure terminal",
2575 "Configuration from vty interface\n" "Configuration terminal\n")
2576{
2577 if (vty_config_lock(vty))
2578 vty->node = CONFIG_NODE;
2579 else {
2580 vty_out(vty, "VTY configuration is locked by other VTY%s",
2581 VTY_NEWLINE);
2582 return CMD_WARNING;
2583 }
2584 return CMD_SUCCESS;
2585}
2586
2587/* Enable command */
2588DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2589{
2590 /* If enable password is NULL, change to ENABLE_NODE */
2591 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2592 vty->type == VTY_SHELL_SERV)
2593 vty->node = ENABLE_NODE;
2594 else
2595 vty->node = AUTH_ENABLE_NODE;
2596
2597 return CMD_SUCCESS;
2598}
2599
2600/* Disable command */
2601DEFUN(disable,
2602 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2603{
2604 if (vty->node == ENABLE_NODE)
2605 vty->node = VIEW_NODE;
2606 return CMD_SUCCESS;
2607}
2608
2609/* Down vty node level. */
2610gDEFUN(config_exit,
2611 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2612{
2613 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002614 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002615 case VIEW_NODE:
2616 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002617 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002618 break;
2619 case CONFIG_NODE:
2620 vty->node = ENABLE_NODE;
2621 vty_config_unlock(vty);
2622 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002623 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002624 if (vty->node > CONFIG_NODE)
2625 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002626 break;
2627 }
2628 return CMD_SUCCESS;
2629}
2630
2631/* End of configuration. */
2632 gDEFUN(config_end,
2633 config_end_cmd, "end", "End current mode and change to enable mode.")
2634{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002635 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002636 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002637
2638 /* Repeatedly call go_parent until a top node is reached. */
2639 while (vty->node > CONFIG_NODE) {
2640 if (vty->node == last_node) {
2641 /* Ensure termination, this shouldn't happen. */
2642 break;
2643 }
2644 last_node = vty->node;
2645 vty_go_parent(vty);
2646 }
2647
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002648 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002649 if (vty->node > ENABLE_NODE)
2650 vty->node = ENABLE_NODE;
2651 vty->index = NULL;
2652 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002653 }
2654 return CMD_SUCCESS;
2655}
2656
2657/* Show version. */
2658DEFUN(show_version,
2659 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2660{
Harald Welte237f6242010-05-25 23:00:45 +02002661 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2662 host.app_info->version,
2663 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2664 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002665
2666 return CMD_SUCCESS;
2667}
2668
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002669DEFUN(show_online_help,
2670 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2671{
2672 vty_dump_nodes(vty);
2673 return CMD_SUCCESS;
2674}
2675
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002676/* Help display function for all node. */
2677gDEFUN(config_help,
2678 config_help_cmd, "help", "Description of the interactive help system\n")
2679{
2680 vty_out(vty,
2681 "This VTY provides advanced help features. When you need help,%s\
2682anytime at the command line please press '?'.%s\
2683%s\
2684If nothing matches, the help list will be empty and you must backup%s\
2685 until entering a '?' shows the available options.%s\
2686Two styles of help are provided:%s\
26871. Full help is available when you are ready to enter a%s\
2688command argument (e.g. 'show ?') and describes each possible%s\
2689argument.%s\
26902. Partial help is provided when an abbreviated argument is entered%s\
2691 and you want to know what arguments match the input%s\
2692 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2693 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2694 return CMD_SUCCESS;
2695}
2696
2697/* Help display function for all node. */
2698gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2699{
2700 unsigned int i;
2701 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2702 struct cmd_element *cmd;
2703
2704 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2705 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2706 && !(cmd->attr == CMD_ATTR_DEPRECATED
2707 || cmd->attr == CMD_ATTR_HIDDEN))
2708 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2709 return CMD_SUCCESS;
2710}
2711
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002712static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002713{
2714 unsigned int i;
2715 int fd;
2716 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002717 char *config_file_tmp = NULL;
2718 char *config_file_sav = NULL;
2719 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002720 struct stat st;
2721
2722 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002723
2724 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002725 config_file_sav =
2726 _talloc_zero(tall_vty_cmd_ctx,
2727 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2728 "config_file_sav");
2729 strcpy(config_file_sav, config_file);
2730 strcat(config_file_sav, CONF_BACKUP_EXT);
2731
2732 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2733 "config_file_tmp");
2734 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2735
2736 /* Open file to configuration write. */
2737 fd = mkstemp(config_file_tmp);
2738 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002739 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002740 talloc_free(config_file_tmp);
2741 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002742 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002743 }
2744
2745 /* Make vty for configuration file. */
2746 file_vty = vty_new();
2747 file_vty->fd = fd;
2748 file_vty->type = VTY_FILE;
2749
2750 /* Config file header print. */
2751 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002752 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002753 //vty_time_print (file_vty, 1);
2754 vty_out(file_vty, "!\n");
2755
2756 for (i = 0; i < vector_active(cmdvec); i++)
2757 if ((node = vector_slot(cmdvec, i)) && node->func) {
2758 if ((*node->func) (file_vty))
2759 vty_out(file_vty, "!\n");
2760 }
2761 vty_close(file_vty);
2762
2763 if (unlink(config_file_sav) != 0)
2764 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002765 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002766 talloc_free(config_file_sav);
2767 talloc_free(config_file_tmp);
2768 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002769 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002770 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002771
2772 /* Only link the .sav file if the original file exists */
2773 if (stat(config_file, &st) == 0) {
2774 if (link(config_file, config_file_sav) != 0) {
2775 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2776 talloc_free(config_file_sav);
2777 talloc_free(config_file_tmp);
2778 unlink(config_file_tmp);
2779 return -3;
2780 }
2781 sync();
2782 if (unlink(config_file) != 0) {
2783 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2784 talloc_free(config_file_sav);
2785 talloc_free(config_file_tmp);
2786 unlink(config_file_tmp);
2787 return -4;
2788 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002789 }
2790 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002791 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002792 talloc_free(config_file_sav);
2793 talloc_free(config_file_tmp);
2794 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002795 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002796 }
2797 unlink(config_file_tmp);
2798 sync();
2799
2800 talloc_free(config_file_sav);
2801 talloc_free(config_file_tmp);
2802
2803 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002804 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2805 return -6;
2806 }
2807
2808 return 0;
2809}
2810
2811
2812/* Write current configuration into file. */
2813DEFUN(config_write_file,
2814 config_write_file_cmd,
2815 "write file",
2816 "Write running configuration to memory, network, or terminal\n"
2817 "Write to configuration file\n")
2818{
2819 char *failed_file;
2820 int rc;
2821
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002822 if (host.app_info->config_is_consistent) {
2823 rc = host.app_info->config_is_consistent(vty);
2824 if (!rc) {
2825 vty_out(vty, "Configuration is not consistent%s",
2826 VTY_NEWLINE);
2827 return CMD_WARNING;
2828 }
2829 }
2830
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002831 if (host.config == NULL) {
2832 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2833 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002834 return CMD_WARNING;
2835 }
2836
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002837 rc = write_config_file(host.config, &failed_file);
2838 switch (rc) {
2839 case -1:
2840 vty_out(vty, "Can't open configuration file %s.%s",
2841 failed_file, VTY_NEWLINE);
2842 rc = CMD_WARNING;
2843 break;
2844 case -2:
2845 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2846 failed_file, VTY_NEWLINE);
2847 rc = CMD_WARNING;
2848 break;
2849 case -3:
2850 vty_out(vty, "Can't backup old configuration file %s.%s",
2851 failed_file, VTY_NEWLINE);
2852 rc = CMD_WARNING;
2853 break;
2854 case -4:
2855 vty_out(vty, "Can't unlink configuration file %s.%s",
2856 failed_file, VTY_NEWLINE);
2857 rc = CMD_WARNING;
2858 break;
2859 case -5:
2860 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2861 VTY_NEWLINE);
2862 rc = CMD_WARNING;
2863 break;
2864 case -6:
2865 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2866 failed_file, strerror(errno), errno, VTY_NEWLINE);
2867 rc = CMD_WARNING;
2868 break;
2869 default:
2870 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2871 rc = CMD_SUCCESS;
2872 break;
2873 }
2874
2875 talloc_free(failed_file);
2876 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002877}
2878
2879ALIAS(config_write_file,
2880 config_write_cmd,
2881 "write", "Write running configuration to memory, network, or terminal\n")
2882
2883 ALIAS(config_write_file,
2884 config_write_memory_cmd,
2885 "write memory",
2886 "Write running configuration to memory, network, or terminal\n"
2887 "Write configuration to the file (same as write file)\n")
2888
2889 ALIAS(config_write_file,
2890 copy_runningconfig_startupconfig_cmd,
2891 "copy running-config startup-config",
2892 "Copy configuration\n"
2893 "Copy running config to... \n"
2894 "Copy running config to startup config (same as write file)\n")
2895
2896/* Write current configuration into the terminal. */
2897 DEFUN(config_write_terminal,
2898 config_write_terminal_cmd,
2899 "write terminal",
2900 "Write running configuration to memory, network, or terminal\n"
2901 "Write to terminal\n")
2902{
2903 unsigned int i;
2904 struct cmd_node *node;
2905
2906 if (vty->type == VTY_SHELL_SERV) {
2907 for (i = 0; i < vector_active(cmdvec); i++)
2908 if ((node = vector_slot(cmdvec, i)) && node->func
2909 && node->vtysh) {
2910 if ((*node->func) (vty))
2911 vty_out(vty, "!%s", VTY_NEWLINE);
2912 }
2913 } else {
2914 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2915 VTY_NEWLINE);
2916 vty_out(vty, "!%s", VTY_NEWLINE);
2917
2918 for (i = 0; i < vector_active(cmdvec); i++)
2919 if ((node = vector_slot(cmdvec, i)) && node->func) {
2920 if ((*node->func) (vty))
2921 vty_out(vty, "!%s", VTY_NEWLINE);
2922 }
2923 vty_out(vty, "end%s", VTY_NEWLINE);
2924 }
2925 return CMD_SUCCESS;
2926}
2927
2928/* Write current configuration into the terminal. */
2929ALIAS(config_write_terminal,
2930 show_running_config_cmd,
2931 "show running-config", SHOW_STR "running configuration\n")
2932
2933/* Write startup configuration into the terminal. */
2934 DEFUN(show_startup_config,
2935 show_startup_config_cmd,
2936 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2937{
2938 char buf[BUFSIZ];
2939 FILE *confp;
2940
2941 confp = fopen(host.config, "r");
2942 if (confp == NULL) {
2943 vty_out(vty, "Can't open configuration file [%s]%s",
2944 host.config, VTY_NEWLINE);
2945 return CMD_WARNING;
2946 }
2947
2948 while (fgets(buf, BUFSIZ, confp)) {
2949 char *cp = buf;
2950
2951 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2952 cp++;
2953 *cp = '\0';
2954
2955 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2956 }
2957
2958 fclose(confp);
2959
2960 return CMD_SUCCESS;
2961}
2962
2963/* Hostname configuration */
2964DEFUN(config_hostname,
2965 hostname_cmd,
2966 "hostname WORD",
2967 "Set system's network name\n" "This system's network name\n")
2968{
2969 if (!isalpha((int)*argv[0])) {
2970 vty_out(vty, "Please specify string starting with alphabet%s",
2971 VTY_NEWLINE);
2972 return CMD_WARNING;
2973 }
2974
2975 if (host.name)
2976 talloc_free(host.name);
2977
2978 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2979 return CMD_SUCCESS;
2980}
2981
2982DEFUN(config_no_hostname,
2983 no_hostname_cmd,
2984 "no hostname [HOSTNAME]",
2985 NO_STR "Reset system's network name\n" "Host name of this router\n")
2986{
2987 if (host.name)
2988 talloc_free(host.name);
2989 host.name = NULL;
2990 return CMD_SUCCESS;
2991}
2992
2993/* VTY interface password set. */
2994DEFUN(config_password, password_cmd,
2995 "password (8|) WORD",
2996 "Assign the terminal connection password\n"
2997 "Specifies a HIDDEN password will follow\n"
2998 "dummy string \n" "The HIDDEN line password string\n")
2999{
3000 /* Argument check. */
3001 if (argc == 0) {
3002 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3003 return CMD_WARNING;
3004 }
3005
3006 if (argc == 2) {
3007 if (*argv[0] == '8') {
3008 if (host.password)
3009 talloc_free(host.password);
3010 host.password = NULL;
3011 if (host.password_encrypt)
3012 talloc_free(host.password_encrypt);
3013 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3014 return CMD_SUCCESS;
3015 } else {
3016 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3017 return CMD_WARNING;
3018 }
3019 }
3020
3021 if (!isalnum((int)*argv[0])) {
3022 vty_out(vty,
3023 "Please specify string starting with alphanumeric%s",
3024 VTY_NEWLINE);
3025 return CMD_WARNING;
3026 }
3027
3028 if (host.password)
3029 talloc_free(host.password);
3030 host.password = NULL;
3031
3032#ifdef VTY_CRYPT_PW
3033 if (host.encrypt) {
3034 if (host.password_encrypt)
3035 talloc_free(host.password_encrypt);
3036 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3037 } else
3038#endif
3039 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3040
3041 return CMD_SUCCESS;
3042}
3043
3044ALIAS(config_password, password_text_cmd,
3045 "password LINE",
3046 "Assign the terminal connection password\n"
3047 "The UNENCRYPTED (cleartext) line password\n")
3048
3049/* VTY enable password set. */
3050 DEFUN(config_enable_password, enable_password_cmd,
3051 "enable password (8|) WORD",
3052 "Modify enable password parameters\n"
3053 "Assign the privileged level password\n"
3054 "Specifies a HIDDEN password will follow\n"
3055 "dummy string \n" "The HIDDEN 'enable' password string\n")
3056{
3057 /* Argument check. */
3058 if (argc == 0) {
3059 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3060 return CMD_WARNING;
3061 }
3062
3063 /* Crypt type is specified. */
3064 if (argc == 2) {
3065 if (*argv[0] == '8') {
3066 if (host.enable)
3067 talloc_free(host.enable);
3068 host.enable = NULL;
3069
3070 if (host.enable_encrypt)
3071 talloc_free(host.enable_encrypt);
3072 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3073
3074 return CMD_SUCCESS;
3075 } else {
3076 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3077 return CMD_WARNING;
3078 }
3079 }
3080
3081 if (!isalnum((int)*argv[0])) {
3082 vty_out(vty,
3083 "Please specify string starting with alphanumeric%s",
3084 VTY_NEWLINE);
3085 return CMD_WARNING;
3086 }
3087
3088 if (host.enable)
3089 talloc_free(host.enable);
3090 host.enable = NULL;
3091
3092 /* Plain password input. */
3093#ifdef VTY_CRYPT_PW
3094 if (host.encrypt) {
3095 if (host.enable_encrypt)
3096 talloc_free(host.enable_encrypt);
3097 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3098 } else
3099#endif
3100 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3101
3102 return CMD_SUCCESS;
3103}
3104
3105ALIAS(config_enable_password,
3106 enable_password_text_cmd,
3107 "enable password LINE",
3108 "Modify enable password parameters\n"
3109 "Assign the privileged level password\n"
3110 "The UNENCRYPTED (cleartext) 'enable' password\n")
3111
3112/* VTY enable password delete. */
3113 DEFUN(no_config_enable_password, no_enable_password_cmd,
3114 "no enable password",
3115 NO_STR
3116 "Modify enable password parameters\n"
3117 "Assign the privileged level password\n")
3118{
3119 if (host.enable)
3120 talloc_free(host.enable);
3121 host.enable = NULL;
3122
3123 if (host.enable_encrypt)
3124 talloc_free(host.enable_encrypt);
3125 host.enable_encrypt = NULL;
3126
3127 return CMD_SUCCESS;
3128}
3129
3130#ifdef VTY_CRYPT_PW
3131DEFUN(service_password_encrypt,
3132 service_password_encrypt_cmd,
3133 "service password-encryption",
3134 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3135{
3136 if (host.encrypt)
3137 return CMD_SUCCESS;
3138
3139 host.encrypt = 1;
3140
3141 if (host.password) {
3142 if (host.password_encrypt)
3143 talloc_free(host.password_encrypt);
3144 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3145 }
3146 if (host.enable) {
3147 if (host.enable_encrypt)
3148 talloc_free(host.enable_encrypt);
3149 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3150 }
3151
3152 return CMD_SUCCESS;
3153}
3154
3155DEFUN(no_service_password_encrypt,
3156 no_service_password_encrypt_cmd,
3157 "no service password-encryption",
3158 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3159{
3160 if (!host.encrypt)
3161 return CMD_SUCCESS;
3162
3163 host.encrypt = 0;
3164
3165 if (host.password_encrypt)
3166 talloc_free(host.password_encrypt);
3167 host.password_encrypt = NULL;
3168
3169 if (host.enable_encrypt)
3170 talloc_free(host.enable_encrypt);
3171 host.enable_encrypt = NULL;
3172
3173 return CMD_SUCCESS;
3174}
3175#endif
3176
3177DEFUN(config_terminal_length, config_terminal_length_cmd,
3178 "terminal length <0-512>",
3179 "Set terminal line parameters\n"
3180 "Set number of lines on a screen\n"
3181 "Number of lines on screen (0 for no pausing)\n")
3182{
3183 int lines;
3184 char *endptr = NULL;
3185
3186 lines = strtol(argv[0], &endptr, 10);
3187 if (lines < 0 || lines > 512 || *endptr != '\0') {
3188 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3189 return CMD_WARNING;
3190 }
3191 vty->lines = lines;
3192
3193 return CMD_SUCCESS;
3194}
3195
3196DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3197 "terminal no length",
3198 "Set terminal line parameters\n"
3199 NO_STR "Set number of lines on a screen\n")
3200{
3201 vty->lines = -1;
3202 return CMD_SUCCESS;
3203}
3204
3205DEFUN(service_terminal_length, service_terminal_length_cmd,
3206 "service terminal-length <0-512>",
3207 "Set up miscellaneous service\n"
3208 "System wide terminal length configuration\n"
3209 "Number of lines of VTY (0 means no line control)\n")
3210{
3211 int lines;
3212 char *endptr = NULL;
3213
3214 lines = strtol(argv[0], &endptr, 10);
3215 if (lines < 0 || lines > 512 || *endptr != '\0') {
3216 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3217 return CMD_WARNING;
3218 }
3219 host.lines = lines;
3220
3221 return CMD_SUCCESS;
3222}
3223
3224DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3225 "no service terminal-length [<0-512>]",
3226 NO_STR
3227 "Set up miscellaneous service\n"
3228 "System wide terminal length configuration\n"
3229 "Number of lines of VTY (0 means no line control)\n")
3230{
3231 host.lines = -1;
3232 return CMD_SUCCESS;
3233}
3234
3235DEFUN_HIDDEN(do_echo,
3236 echo_cmd,
3237 "echo .MESSAGE",
3238 "Echo a message back to the vty\n" "The message to echo\n")
3239{
3240 char *message;
3241
3242 vty_out(vty, "%s%s",
3243 ((message =
3244 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3245 if (message)
3246 talloc_free(message);
3247 return CMD_SUCCESS;
3248}
3249
3250#if 0
3251DEFUN(config_logmsg,
3252 config_logmsg_cmd,
3253 "logmsg " LOG_LEVELS " .MESSAGE",
3254 "Send a message to enabled logging destinations\n"
3255 LOG_LEVEL_DESC "The message to send\n")
3256{
3257 int level;
3258 char *message;
3259
3260 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3261 return CMD_ERR_NO_MATCH;
3262
3263 zlog(NULL, level,
3264 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3265 if (message)
3266 talloc_free(message);
3267 return CMD_SUCCESS;
3268}
3269
3270DEFUN(show_logging,
3271 show_logging_cmd,
3272 "show logging", SHOW_STR "Show current logging configuration\n")
3273{
3274 struct zlog *zl = zlog_default;
3275
3276 vty_out(vty, "Syslog logging: ");
3277 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3278 vty_out(vty, "disabled");
3279 else
3280 vty_out(vty, "level %s, facility %s, ident %s",
3281 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3282 facility_name(zl->facility), zl->ident);
3283 vty_out(vty, "%s", VTY_NEWLINE);
3284
3285 vty_out(vty, "Stdout logging: ");
3286 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3287 vty_out(vty, "disabled");
3288 else
3289 vty_out(vty, "level %s",
3290 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3291 vty_out(vty, "%s", VTY_NEWLINE);
3292
3293 vty_out(vty, "Monitor logging: ");
3294 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3295 vty_out(vty, "disabled");
3296 else
3297 vty_out(vty, "level %s",
3298 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3299 vty_out(vty, "%s", VTY_NEWLINE);
3300
3301 vty_out(vty, "File logging: ");
3302 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3303 vty_out(vty, "disabled");
3304 else
3305 vty_out(vty, "level %s, filename %s",
3306 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3307 zl->filename);
3308 vty_out(vty, "%s", VTY_NEWLINE);
3309
3310 vty_out(vty, "Protocol name: %s%s",
3311 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3312 vty_out(vty, "Record priority: %s%s",
3313 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3314
3315 return CMD_SUCCESS;
3316}
3317
3318DEFUN(config_log_stdout,
3319 config_log_stdout_cmd,
3320 "log stdout", "Logging control\n" "Set stdout logging level\n")
3321{
3322 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3323 return CMD_SUCCESS;
3324}
3325
3326DEFUN(config_log_stdout_level,
3327 config_log_stdout_level_cmd,
3328 "log stdout " LOG_LEVELS,
3329 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3330{
3331 int level;
3332
3333 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3334 return CMD_ERR_NO_MATCH;
3335 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3336 return CMD_SUCCESS;
3337}
3338
3339DEFUN(no_config_log_stdout,
3340 no_config_log_stdout_cmd,
3341 "no log stdout [LEVEL]",
3342 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3343{
3344 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3345 return CMD_SUCCESS;
3346}
3347
3348DEFUN(config_log_monitor,
3349 config_log_monitor_cmd,
3350 "log monitor",
3351 "Logging control\n" "Set terminal line (monitor) logging level\n")
3352{
3353 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3354 return CMD_SUCCESS;
3355}
3356
3357DEFUN(config_log_monitor_level,
3358 config_log_monitor_level_cmd,
3359 "log monitor " LOG_LEVELS,
3360 "Logging control\n"
3361 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3362{
3363 int level;
3364
3365 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3366 return CMD_ERR_NO_MATCH;
3367 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3368 return CMD_SUCCESS;
3369}
3370
3371DEFUN(no_config_log_monitor,
3372 no_config_log_monitor_cmd,
3373 "no log monitor [LEVEL]",
3374 NO_STR
3375 "Logging control\n"
3376 "Disable terminal line (monitor) logging\n" "Logging level\n")
3377{
3378 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3379 return CMD_SUCCESS;
3380}
3381
3382static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3383{
3384 int ret;
3385 char *p = NULL;
3386 const char *fullpath;
3387
3388 /* Path detection. */
3389 if (!IS_DIRECTORY_SEP(*fname)) {
3390 char cwd[MAXPATHLEN + 1];
3391 cwd[MAXPATHLEN] = '\0';
3392
3393 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3394 zlog_err("config_log_file: Unable to alloc mem!");
3395 return CMD_WARNING;
3396 }
3397
3398 if ((p = _talloc_zero(tall_vcmd_ctx,
3399 strlen(cwd) + strlen(fname) + 2),
3400 "set_log_file")
3401 == NULL) {
3402 zlog_err("config_log_file: Unable to alloc mem!");
3403 return CMD_WARNING;
3404 }
3405 sprintf(p, "%s/%s", cwd, fname);
3406 fullpath = p;
3407 } else
3408 fullpath = fname;
3409
3410 ret = zlog_set_file(NULL, fullpath, loglevel);
3411
3412 if (p)
3413 talloc_free(p);
3414
3415 if (!ret) {
3416 vty_out(vty, "can't open logfile %s\n", fname);
3417 return CMD_WARNING;
3418 }
3419
3420 if (host.logfile)
3421 talloc_free(host.logfile);
3422
3423 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3424
3425 return CMD_SUCCESS;
3426}
3427
3428DEFUN(config_log_file,
3429 config_log_file_cmd,
3430 "log file FILENAME",
3431 "Logging control\n" "Logging to file\n" "Logging filename\n")
3432{
3433 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3434}
3435
3436DEFUN(config_log_file_level,
3437 config_log_file_level_cmd,
3438 "log file FILENAME " LOG_LEVELS,
3439 "Logging control\n"
3440 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3441{
3442 int level;
3443
3444 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3445 return CMD_ERR_NO_MATCH;
3446 return set_log_file(vty, argv[0], level);
3447}
3448
3449DEFUN(no_config_log_file,
3450 no_config_log_file_cmd,
3451 "no log file [FILENAME]",
3452 NO_STR
3453 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3454{
3455 zlog_reset_file(NULL);
3456
3457 if (host.logfile)
3458 talloc_free(host.logfile);
3459
3460 host.logfile = NULL;
3461
3462 return CMD_SUCCESS;
3463}
3464
3465ALIAS(no_config_log_file,
3466 no_config_log_file_level_cmd,
3467 "no log file FILENAME LEVEL",
3468 NO_STR
3469 "Logging control\n"
3470 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3471
3472 DEFUN(config_log_syslog,
3473 config_log_syslog_cmd,
3474 "log syslog", "Logging control\n" "Set syslog logging level\n")
3475{
3476 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3477 return CMD_SUCCESS;
3478}
3479
3480DEFUN(config_log_syslog_level,
3481 config_log_syslog_level_cmd,
3482 "log syslog " LOG_LEVELS,
3483 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3484{
3485 int level;
3486
3487 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3488 return CMD_ERR_NO_MATCH;
3489 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3490 return CMD_SUCCESS;
3491}
3492
3493DEFUN_DEPRECATED(config_log_syslog_facility,
3494 config_log_syslog_facility_cmd,
3495 "log syslog facility " LOG_FACILITIES,
3496 "Logging control\n"
3497 "Logging goes to syslog\n"
3498 "(Deprecated) Facility parameter for syslog messages\n"
3499 LOG_FACILITY_DESC)
3500{
3501 int facility;
3502
3503 if ((facility = facility_match(argv[0])) < 0)
3504 return CMD_ERR_NO_MATCH;
3505
3506 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3507 zlog_default->facility = facility;
3508 return CMD_SUCCESS;
3509}
3510
3511DEFUN(no_config_log_syslog,
3512 no_config_log_syslog_cmd,
3513 "no log syslog [LEVEL]",
3514 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3515{
3516 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3517 return CMD_SUCCESS;
3518}
3519
3520ALIAS(no_config_log_syslog,
3521 no_config_log_syslog_facility_cmd,
3522 "no log syslog facility " LOG_FACILITIES,
3523 NO_STR
3524 "Logging control\n"
3525 "Logging goes to syslog\n"
3526 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3527
3528 DEFUN(config_log_facility,
3529 config_log_facility_cmd,
3530 "log facility " LOG_FACILITIES,
3531 "Logging control\n"
3532 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3533{
3534 int facility;
3535
3536 if ((facility = facility_match(argv[0])) < 0)
3537 return CMD_ERR_NO_MATCH;
3538 zlog_default->facility = facility;
3539 return CMD_SUCCESS;
3540}
3541
3542DEFUN(no_config_log_facility,
3543 no_config_log_facility_cmd,
3544 "no log facility [FACILITY]",
3545 NO_STR
3546 "Logging control\n"
3547 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3548{
3549 zlog_default->facility = LOG_DAEMON;
3550 return CMD_SUCCESS;
3551}
3552
3553DEFUN_DEPRECATED(config_log_trap,
3554 config_log_trap_cmd,
3555 "log trap " LOG_LEVELS,
3556 "Logging control\n"
3557 "(Deprecated) Set logging level and default for all destinations\n"
3558 LOG_LEVEL_DESC)
3559{
3560 int new_level;
3561 int i;
3562
3563 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3564 return CMD_ERR_NO_MATCH;
3565
3566 zlog_default->default_lvl = new_level;
3567 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3568 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3569 zlog_default->maxlvl[i] = new_level;
3570 return CMD_SUCCESS;
3571}
3572
3573DEFUN_DEPRECATED(no_config_log_trap,
3574 no_config_log_trap_cmd,
3575 "no log trap [LEVEL]",
3576 NO_STR
3577 "Logging control\n"
3578 "Permit all logging information\n" "Logging level\n")
3579{
3580 zlog_default->default_lvl = LOG_DEBUG;
3581 return CMD_SUCCESS;
3582}
3583
3584DEFUN(config_log_record_priority,
3585 config_log_record_priority_cmd,
3586 "log record-priority",
3587 "Logging control\n"
3588 "Log the priority of the message within the message\n")
3589{
3590 zlog_default->record_priority = 1;
3591 return CMD_SUCCESS;
3592}
3593
3594DEFUN(no_config_log_record_priority,
3595 no_config_log_record_priority_cmd,
3596 "no log record-priority",
3597 NO_STR
3598 "Logging control\n"
3599 "Do not log the priority of the message within the message\n")
3600{
3601 zlog_default->record_priority = 0;
3602 return CMD_SUCCESS;
3603}
3604#endif
3605
3606DEFUN(banner_motd_file,
3607 banner_motd_file_cmd,
3608 "banner motd file [FILE]",
3609 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3610{
3611 if (host.motdfile)
3612 talloc_free(host.motdfile);
3613 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3614
3615 return CMD_SUCCESS;
3616}
3617
3618DEFUN(banner_motd_default,
3619 banner_motd_default_cmd,
3620 "banner motd default",
3621 "Set banner string\n" "Strings for motd\n" "Default string\n")
3622{
3623 host.motd = default_motd;
3624 return CMD_SUCCESS;
3625}
3626
3627DEFUN(no_banner_motd,
3628 no_banner_motd_cmd,
3629 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3630{
3631 host.motd = NULL;
3632 if (host.motdfile)
3633 talloc_free(host.motdfile);
3634 host.motdfile = NULL;
3635 return CMD_SUCCESS;
3636}
3637
3638/* Set config filename. Called from vty.c */
3639void host_config_set(const char *filename)
3640{
3641 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3642}
3643
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003644/*! Deprecated, now happens implicitly when calling install_node().
3645 * Users of the API may still attempt to call this function, hence
3646 * leave it here as a no-op. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003647void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003648{
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003649}
3650
3651/*! Deprecated, now happens implicitly when calling install_node().
3652 * Users of the API may still attempt to call this function, hence
3653 * leave it here as a no-op. */
3654void vty_install_default(int node)
3655{
3656}
3657
3658/*! Install common commands like 'exit' and 'list'. */
3659static void install_basic_node_commands(int node)
3660{
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003661 install_element(node, &config_help_cmd);
3662 install_element(node, &config_list_cmd);
3663
3664 install_element(node, &config_write_terminal_cmd);
3665 install_element(node, &config_write_file_cmd);
3666 install_element(node, &config_write_memory_cmd);
3667 install_element(node, &config_write_cmd);
3668 install_element(node, &show_running_config_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003669
3670 install_element(node, &config_exit_cmd);
3671
3672 if (node >= CONFIG_NODE) {
3673 /* It's not a top node. */
3674 install_element(node, &config_end_cmd);
3675 }
3676}
3677
Neels Hofmeyr69054e22017-10-19 02:44:57 +02003678/*! Return true if a node is installed by install_basic_node_commands(), so
3679 * that we can avoid repeating them for each and every node during 'show
3680 * running-config' */
3681static bool vty_command_is_common(struct cmd_element *cmd)
3682{
3683 if (cmd == &config_help_cmd
3684 || cmd == &config_list_cmd
3685 || cmd == &config_write_terminal_cmd
3686 || cmd == &config_write_file_cmd
3687 || cmd == &config_write_memory_cmd
3688 || cmd == &config_write_cmd
3689 || cmd == &show_running_config_cmd
3690 || cmd == &config_exit_cmd
3691 || cmd == &config_end_cmd)
3692 return true;
3693 return false;
3694}
3695
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003696/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003697 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003698 * \param[in] vty the vty of the code
3699 * \param[in] filename where to store the file
3700 * \return 0 in case of success.
3701 *
3702 * If the filename already exists create a filename.sav
3703 * version with the current code.
3704 *
3705 */
3706int osmo_vty_write_config_file(const char *filename)
3707{
3708 char *failed_file;
3709 int rc;
3710
3711 rc = write_config_file(filename, &failed_file);
3712 talloc_free(failed_file);
3713 return rc;
3714}
3715
3716/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003717 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003718 * \return 0 in case of success.
3719 *
3720 * If the filename already exists create a filename.sav
3721 * version with the current code.
3722 *
3723 */
3724int osmo_vty_save_config_file(void)
3725{
3726 char *failed_file;
3727 int rc;
3728
3729 if (host.config == NULL)
3730 return -7;
3731
3732 rc = write_config_file(host.config, &failed_file);
3733 talloc_free(failed_file);
3734 return rc;
3735}
3736
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003737/* Initialize command interface. Install basic nodes and commands. */
3738void cmd_init(int terminal)
3739{
3740 /* Allocate initial top vector of commands. */
3741 cmdvec = vector_init(VECTOR_MIN_SIZE);
3742
3743 /* Default host value settings. */
3744 host.name = NULL;
3745 host.password = NULL;
3746 host.enable = NULL;
3747 host.logfile = NULL;
3748 host.config = NULL;
3749 host.lines = -1;
3750 host.motd = default_motd;
3751 host.motdfile = NULL;
3752
3753 /* Install top nodes. */
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003754 install_node_bare(&view_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003755 install_node(&enable_node, NULL);
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003756 install_node_bare(&auth_node, NULL);
3757 install_node_bare(&auth_enable_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003758 install_node(&config_node, config_write_host);
3759
3760 /* Each node's basic commands. */
3761 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003762 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003763 if (terminal) {
3764 install_element(VIEW_NODE, &config_list_cmd);
3765 install_element(VIEW_NODE, &config_exit_cmd);
3766 install_element(VIEW_NODE, &config_help_cmd);
3767 install_element(VIEW_NODE, &config_enable_cmd);
3768 install_element(VIEW_NODE, &config_terminal_length_cmd);
3769 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3770 install_element(VIEW_NODE, &echo_cmd);
3771 }
3772
3773 if (terminal) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003774 install_element(ENABLE_NODE, &config_disable_cmd);
3775 install_element(ENABLE_NODE, &config_terminal_cmd);
3776 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3777 }
3778 install_element (ENABLE_NODE, &show_startup_config_cmd);
3779 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003780 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003781
3782 if (terminal) {
3783 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3784 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3785 install_element(ENABLE_NODE, &echo_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003786 }
3787
3788 install_element(CONFIG_NODE, &hostname_cmd);
3789 install_element(CONFIG_NODE, &no_hostname_cmd);
3790
3791 if (terminal) {
3792 install_element(CONFIG_NODE, &password_cmd);
3793 install_element(CONFIG_NODE, &password_text_cmd);
3794 install_element(CONFIG_NODE, &enable_password_cmd);
3795 install_element(CONFIG_NODE, &enable_password_text_cmd);
3796 install_element(CONFIG_NODE, &no_enable_password_cmd);
3797
3798#ifdef VTY_CRYPT_PW
3799 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3800 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3801#endif
3802 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3803 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3804 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3805 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3806 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3807
3808 }
3809 srand(time(NULL));
3810}
Harald Welte7acb30c2011-08-17 17:13:48 +02003811
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003812/*! @} */