blob: 73957115024053ac00322e447b02b808993f15d5 [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;
Neels Hofmeyrf7162772017-10-22 02:31:33 +0200688 if (vector_active(cnode->cmd_vector) < 1)
689 continue;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100690
Neels Hofmeyr657c5b62017-09-18 16:42:06 +0200691 /* De-dup node IDs: how many times has this same name been used before? Count the first
692 * occurence as _1 and omit that first suffix, so that the first occurence is called
693 * 'name', the second becomes 'name_2', then 'name_3', ... */
694 same_name_count = 1;
695 for (j = 0; j < i; ++j) {
696 struct cmd_node *cnode2;
697 cnode2 = vector_slot(cmdvec, j);
698 if (!cnode2)
699 continue;
700 if (strcmp(cnode->name, cnode2->name) == 0)
701 same_name_count ++;
702 }
703
704 vty_out(vty, " <node id='%s", cnode->name);
705 if (same_name_count > 1 || !*cnode->name)
706 vty_out(vty, "_%d", same_name_count);
707 vty_out(vty, "'>%s", VTY_NEWLINE);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100708
709 for (j = 0; j < vector_active(cnode->cmd_vector); ++j) {
710 struct cmd_element *elem;
711 elem = vector_slot(cnode->cmd_vector, j);
Neels Hofmeyr69054e22017-10-19 02:44:57 +0200712 if (vty_command_is_common(elem))
713 continue;
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +0100714 vty_dump_element(elem, vty);
715 }
716
717 vty_out(vty, " </node>%s", VTY_NEWLINE);
718 }
719
720 vty_out(vty, "</vtydoc>%s", VTY_NEWLINE);
721
722 return 0;
723}
724
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200725/* Check if a command with given string exists at given node */
Harald Welteaddeaa32017-01-07 12:52:00 +0100726static int check_element_exists(struct cmd_node *cnode, const char *cmdstring)
727{
728 int i;
729
730 for (i = 0; i < vector_active(cnode->cmd_vector); ++i) {
731 struct cmd_element *elem;
732 elem = vector_slot(cnode->cmd_vector, i);
733 if (!elem->string)
734 continue;
735 if (!strcmp(elem->string, cmdstring))
736 return 1;
737 }
738 return 0;
739}
740
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200741/*! Install a command into a node
Harald Welte7acb30c2011-08-17 17:13:48 +0200742 * \param[in] ntype Node Type
743 * \param[cmd] element to be installed
744 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000745void install_element(int ntype, struct cmd_element *cmd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200746{
747 struct cmd_node *cnode;
748
749 cnode = vector_slot(cmdvec, ntype);
750
Harald Weltea99d45a2015-11-12 13:48:23 +0100751 OSMO_ASSERT(cnode);
Harald Welteaddeaa32017-01-07 12:52:00 +0100752 /* ensure no _identical_ command has been registered at this
753 * node so far */
754 OSMO_ASSERT(!check_element_exists(cnode, cmd->string));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200755
756 vector_set(cnode->cmd_vector, cmd);
757
758 cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
759 cmd->cmdsize = cmd_cmdsize(cmd->strvec);
760}
761
762/* Install a command into VIEW and ENABLE node */
763void install_element_ve(struct cmd_element *cmd)
764{
765 install_element(VIEW_NODE, cmd);
766 install_element(ENABLE_NODE, cmd);
767}
768
769#ifdef VTY_CRYPT_PW
770static unsigned char itoa64[] =
771 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
772
773static void to64(char *s, long v, int n)
774{
775 while (--n >= 0) {
776 *s++ = itoa64[v & 0x3f];
777 v >>= 6;
778 }
779}
780
781static char *zencrypt(const char *passwd)
782{
783 char salt[6];
784 struct timeval tv;
785 char *crypt(const char *, const char *);
786
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200787 osmo_gettimeofday(&tv, 0);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200788
789 to64(&salt[0], random(), 3);
790 to64(&salt[3], tv.tv_usec, 3);
791 salt[5] = '\0';
792
793 return crypt(passwd, salt);
794}
795#endif
796
797/* This function write configuration of this host. */
798static int config_write_host(struct vty *vty)
799{
800 if (host.name)
801 vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
802
803 if (host.encrypt) {
804 if (host.password_encrypt)
805 vty_out(vty, "password 8 %s%s", host.password_encrypt,
806 VTY_NEWLINE);
807 if (host.enable_encrypt)
808 vty_out(vty, "enable password 8 %s%s",
809 host.enable_encrypt, VTY_NEWLINE);
810 } else {
811 if (host.password)
812 vty_out(vty, "password %s%s", host.password,
813 VTY_NEWLINE);
814 if (host.enable)
815 vty_out(vty, "enable password %s%s", host.enable,
816 VTY_NEWLINE);
817 }
818
819 if (host.advanced)
820 vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
821
822 if (host.encrypt)
823 vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
824
825 if (host.lines >= 0)
826 vty_out(vty, "service terminal-length %d%s", host.lines,
827 VTY_NEWLINE);
828
829 if (host.motdfile)
830 vty_out(vty, "banner motd file %s%s", host.motdfile,
831 VTY_NEWLINE);
832 else if (!host.motd)
833 vty_out(vty, "no banner motd%s", VTY_NEWLINE);
834
835 return 1;
836}
837
838/* Utility function for getting command vector. */
839static vector cmd_node_vector(vector v, enum node_type ntype)
840{
841 struct cmd_node *cnode = vector_slot(v, ntype);
842 return cnode->cmd_vector;
843}
844
845/* Completion match types. */
846enum match_type {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100847 no_match = 0,
848 any_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200849 extend_match,
850 ipv4_prefix_match,
851 ipv4_match,
852 ipv6_prefix_match,
853 ipv6_match,
854 range_match,
855 vararg_match,
856 partly_match,
Sylvain Munaut4d8eea42012-12-28 11:58:23 +0100857 exact_match,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200858};
859
860static enum match_type cmd_ipv4_match(const char *str)
861{
862 const char *sp;
863 int dots = 0, nums = 0;
864 char buf[4];
865
866 if (str == NULL)
867 return partly_match;
868
869 for (;;) {
870 memset(buf, 0, sizeof(buf));
871 sp = str;
872 while (*str != '\0') {
873 if (*str == '.') {
874 if (dots >= 3)
875 return no_match;
876
877 if (*(str + 1) == '.')
878 return no_match;
879
880 if (*(str + 1) == '\0')
881 return partly_match;
882
883 dots++;
884 break;
885 }
886 if (!isdigit((int)*str))
887 return no_match;
888
889 str++;
890 }
891
892 if (str - sp > 3)
893 return no_match;
894
895 strncpy(buf, sp, str - sp);
896 if (atoi(buf) > 255)
897 return no_match;
898
899 nums++;
900
901 if (*str == '\0')
902 break;
903
904 str++;
905 }
906
907 if (nums < 4)
908 return partly_match;
909
910 return exact_match;
911}
912
913static enum match_type cmd_ipv4_prefix_match(const char *str)
914{
915 const char *sp;
916 int dots = 0;
917 char buf[4];
918
919 if (str == NULL)
920 return partly_match;
921
922 for (;;) {
923 memset(buf, 0, sizeof(buf));
924 sp = str;
925 while (*str != '\0' && *str != '/') {
926 if (*str == '.') {
927 if (dots == 3)
928 return no_match;
929
930 if (*(str + 1) == '.' || *(str + 1) == '/')
931 return no_match;
932
933 if (*(str + 1) == '\0')
934 return partly_match;
935
936 dots++;
937 break;
938 }
939
940 if (!isdigit((int)*str))
941 return no_match;
942
943 str++;
944 }
945
946 if (str - sp > 3)
947 return no_match;
948
949 strncpy(buf, sp, str - sp);
950 if (atoi(buf) > 255)
951 return no_match;
952
953 if (dots == 3) {
954 if (*str == '/') {
955 if (*(str + 1) == '\0')
956 return partly_match;
957
958 str++;
959 break;
960 } else if (*str == '\0')
961 return partly_match;
962 }
963
964 if (*str == '\0')
965 return partly_match;
966
967 str++;
968 }
969
970 sp = str;
971 while (*str != '\0') {
972 if (!isdigit((int)*str))
973 return no_match;
974
975 str++;
976 }
977
978 if (atoi(sp) > 32)
979 return no_match;
980
981 return exact_match;
982}
983
984#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
985#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
986#define STATE_START 1
987#define STATE_COLON 2
988#define STATE_DOUBLE 3
989#define STATE_ADDR 4
990#define STATE_DOT 5
991#define STATE_SLASH 6
992#define STATE_MASK 7
993
994#ifdef HAVE_IPV6
995
996static enum match_type cmd_ipv6_match(const char *str)
997{
998 int state = STATE_START;
999 int colons = 0, nums = 0, double_colon = 0;
1000 const char *sp = NULL;
1001 struct sockaddr_in6 sin6_dummy;
1002 int ret;
1003
1004 if (str == NULL)
1005 return partly_match;
1006
1007 if (strspn(str, IPV6_ADDR_STR) != strlen(str))
1008 return no_match;
1009
1010 /* use inet_pton that has a better support,
1011 * for example inet_pton can support the automatic addresses:
1012 * ::1.2.3.4
1013 */
1014 ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
1015
1016 if (ret == 1)
1017 return exact_match;
1018
1019 while (*str != '\0') {
1020 switch (state) {
1021 case STATE_START:
1022 if (*str == ':') {
1023 if (*(str + 1) != ':' && *(str + 1) != '\0')
1024 return no_match;
1025 colons--;
1026 state = STATE_COLON;
1027 } else {
1028 sp = str;
1029 state = STATE_ADDR;
1030 }
1031
1032 continue;
1033 case STATE_COLON:
1034 colons++;
1035 if (*(str + 1) == ':')
1036 state = STATE_DOUBLE;
1037 else {
1038 sp = str + 1;
1039 state = STATE_ADDR;
1040 }
1041 break;
1042 case STATE_DOUBLE:
1043 if (double_colon)
1044 return no_match;
1045
1046 if (*(str + 1) == ':')
1047 return no_match;
1048 else {
1049 if (*(str + 1) != '\0')
1050 colons++;
1051 sp = str + 1;
1052 state = STATE_ADDR;
1053 }
1054
1055 double_colon++;
1056 nums++;
1057 break;
1058 case STATE_ADDR:
1059 if (*(str + 1) == ':' || *(str + 1) == '\0') {
1060 if (str - sp > 3)
1061 return no_match;
1062
1063 nums++;
1064 state = STATE_COLON;
1065 }
1066 if (*(str + 1) == '.')
1067 state = STATE_DOT;
1068 break;
1069 case STATE_DOT:
1070 state = STATE_ADDR;
1071 break;
1072 default:
1073 break;
1074 }
1075
1076 if (nums > 8)
1077 return no_match;
1078
1079 if (colons > 7)
1080 return no_match;
1081
1082 str++;
1083 }
1084
1085#if 0
1086 if (nums < 11)
1087 return partly_match;
1088#endif /* 0 */
1089
1090 return exact_match;
1091}
1092
1093static enum match_type cmd_ipv6_prefix_match(const char *str)
1094{
1095 int state = STATE_START;
1096 int colons = 0, nums = 0, double_colon = 0;
1097 int mask;
1098 const char *sp = NULL;
1099 char *endptr = NULL;
1100
1101 if (str == NULL)
1102 return partly_match;
1103
1104 if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
1105 return no_match;
1106
1107 while (*str != '\0' && state != STATE_MASK) {
1108 switch (state) {
1109 case STATE_START:
1110 if (*str == ':') {
1111 if (*(str + 1) != ':' && *(str + 1) != '\0')
1112 return no_match;
1113 colons--;
1114 state = STATE_COLON;
1115 } else {
1116 sp = str;
1117 state = STATE_ADDR;
1118 }
1119
1120 continue;
1121 case STATE_COLON:
1122 colons++;
1123 if (*(str + 1) == '/')
1124 return no_match;
1125 else if (*(str + 1) == ':')
1126 state = STATE_DOUBLE;
1127 else {
1128 sp = str + 1;
1129 state = STATE_ADDR;
1130 }
1131 break;
1132 case STATE_DOUBLE:
1133 if (double_colon)
1134 return no_match;
1135
1136 if (*(str + 1) == ':')
1137 return no_match;
1138 else {
1139 if (*(str + 1) != '\0' && *(str + 1) != '/')
1140 colons++;
1141 sp = str + 1;
1142
1143 if (*(str + 1) == '/')
1144 state = STATE_SLASH;
1145 else
1146 state = STATE_ADDR;
1147 }
1148
1149 double_colon++;
1150 nums += 1;
1151 break;
1152 case STATE_ADDR:
1153 if (*(str + 1) == ':' || *(str + 1) == '.'
1154 || *(str + 1) == '\0' || *(str + 1) == '/') {
1155 if (str - sp > 3)
1156 return no_match;
1157
1158 for (; sp <= str; sp++)
1159 if (*sp == '/')
1160 return no_match;
1161
1162 nums++;
1163
1164 if (*(str + 1) == ':')
1165 state = STATE_COLON;
1166 else if (*(str + 1) == '.')
1167 state = STATE_DOT;
1168 else if (*(str + 1) == '/')
1169 state = STATE_SLASH;
1170 }
1171 break;
1172 case STATE_DOT:
1173 state = STATE_ADDR;
1174 break;
1175 case STATE_SLASH:
1176 if (*(str + 1) == '\0')
1177 return partly_match;
1178
1179 state = STATE_MASK;
1180 break;
1181 default:
1182 break;
1183 }
1184
1185 if (nums > 11)
1186 return no_match;
1187
1188 if (colons > 7)
1189 return no_match;
1190
1191 str++;
1192 }
1193
1194 if (state < STATE_MASK)
1195 return partly_match;
1196
1197 mask = strtol(str, &endptr, 10);
1198 if (*endptr != '\0')
1199 return no_match;
1200
1201 if (mask < 0 || mask > 128)
1202 return no_match;
1203
1204/* I don't know why mask < 13 makes command match partly.
1205 Forgive me to make this comments. I Want to set static default route
1206 because of lack of function to originate default in ospf6d; sorry
1207 yasu
1208 if (mask < 13)
1209 return partly_match;
1210*/
1211
1212 return exact_match;
1213}
1214
1215#endif /* HAVE_IPV6 */
1216
1217#define DECIMAL_STRLEN_MAX 10
1218
1219static int cmd_range_match(const char *range, const char *str)
1220{
1221 char *p;
1222 char buf[DECIMAL_STRLEN_MAX + 1];
1223 char *endptr = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001224
1225 if (str == NULL)
1226 return 1;
1227
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001228 if (range[1] == '-') {
1229 signed long min = 0, max = 0, val;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001230
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001231 val = strtol(str, &endptr, 10);
1232 if (*endptr != '\0')
1233 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001234
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001235 range += 2;
1236 p = strchr(range, '-');
1237 if (p == NULL)
1238 return 0;
1239 if (p - range > DECIMAL_STRLEN_MAX)
1240 return 0;
1241 strncpy(buf, range, p - range);
1242 buf[p - range] = '\0';
1243 min = -strtol(buf, &endptr, 10);
1244 if (*endptr != '\0')
1245 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001246
Andreas Eversberg33f0fc32010-07-13 13:50:39 +02001247 range = p + 1;
1248 p = strchr(range, '>');
1249 if (p == NULL)
1250 return 0;
1251 if (p - range > DECIMAL_STRLEN_MAX)
1252 return 0;
1253 strncpy(buf, range, p - range);
1254 buf[p - range] = '\0';
1255 max = strtol(buf, &endptr, 10);
1256 if (*endptr != '\0')
1257 return 0;
1258
1259 if (val < min || val > max)
1260 return 0;
1261 } else {
1262 unsigned long min, max, val;
1263
1264 val = strtoul(str, &endptr, 10);
1265 if (*endptr != '\0')
1266 return 0;
1267
1268 range++;
1269 p = strchr(range, '-');
1270 if (p == NULL)
1271 return 0;
1272 if (p - range > DECIMAL_STRLEN_MAX)
1273 return 0;
1274 strncpy(buf, range, p - range);
1275 buf[p - range] = '\0';
1276 min = strtoul(buf, &endptr, 10);
1277 if (*endptr != '\0')
1278 return 0;
1279
1280 range = p + 1;
1281 p = strchr(range, '>');
1282 if (p == NULL)
1283 return 0;
1284 if (p - range > DECIMAL_STRLEN_MAX)
1285 return 0;
1286 strncpy(buf, range, p - range);
1287 buf[p - range] = '\0';
1288 max = strtoul(buf, &endptr, 10);
1289 if (*endptr != '\0')
1290 return 0;
1291
1292 if (val < min || val > max)
1293 return 0;
1294 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001295
1296 return 1;
1297}
1298
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001299/* helper to retrieve the 'real' argument string from an optional argument */
1300static char *
1301cmd_deopt(const char *str)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001302{
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001303 /* we've got "[blah]". We want to strip off the []s and redo the
1304 * match check for "blah"
1305 */
1306 size_t len = strlen(str);
1307 char *tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001308
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001309 if (len < 3)
1310 return NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001311
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001312 /* tmp will hold a string of len-2 chars, so 'len' size is fine */
1313 tmp = talloc_size(NULL, len);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001314
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001315 memcpy(tmp, (str + 1), len - 2);
1316 tmp[len - 2] = '\0';
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001317
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001318 return tmp;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001319}
1320
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001321static enum match_type
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001322cmd_match(const char *str, const char *command,
1323 enum match_type min, bool recur)
1324{
1325
1326 if (recur && CMD_OPTION(str))
1327 {
1328 enum match_type ret;
1329 char *tmp = cmd_deopt(str);
1330
1331 /* this would be a bug in a command, however handle it gracefully
1332 * as it we only discover it if a user tries to run it
1333 */
1334 if (tmp == NULL)
1335 return no_match;
1336
1337 ret = cmd_match(tmp, command, min, false);
1338
1339 talloc_free(tmp);
1340
1341 return ret;
1342 }
1343 else if (CMD_VARARG(str))
1344 return vararg_match;
1345 else if (CMD_RANGE(str))
1346 {
1347 if (cmd_range_match(str, command))
1348 return range_match;
1349 }
1350#ifdef HAVE_IPV6
1351 else if (CMD_IPV6(str))
1352 {
1353 if (cmd_ipv6_match(command) >= min)
1354 return ipv6_match;
1355 }
1356 else if (CMD_IPV6_PREFIX(str))
1357 {
1358 if (cmd_ipv6_prefix_match(command) >= min)
1359 return ipv6_prefix_match;
1360 }
1361#endif /* HAVE_IPV6 */
1362 else if (CMD_IPV4(str))
1363 {
1364 if (cmd_ipv4_match(command) >= min)
1365 return ipv4_match;
1366 }
1367 else if (CMD_IPV4_PREFIX(str))
1368 {
1369 if (cmd_ipv4_prefix_match(command) >= min)
1370 return ipv4_prefix_match;
1371 }
1372 else if (CMD_VARIABLE(str))
1373 return extend_match;
1374 else if (strncmp(command, str, strlen(command)) == 0)
1375 {
1376 if (strcmp(command, str) == 0)
1377 return exact_match;
1378 else if (partly_match >= min)
1379 return partly_match;
1380 }
1381
1382 return no_match;
1383}
1384
1385/* Filter vector at the specified index and by the given command string, to
1386 * the desired matching level (thus allowing part matches), and return match
1387 * type flag.
1388 */
1389static enum match_type
1390cmd_filter(char *command, vector v, unsigned int index, enum match_type level)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001391{
1392 unsigned int i;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001393 struct cmd_element *cmd_element;
1394 enum match_type match_type;
1395 vector descvec;
1396 struct desc *desc;
1397
1398 match_type = no_match;
1399
1400 /* If command and cmd_element string does not match set NULL to vector */
1401 for (i = 0; i < vector_active(v); i++)
1402 if ((cmd_element = vector_slot(v, i)) != NULL) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001403 if (index >= vector_active(cmd_element->strvec))
1404 vector_slot(v, i) = NULL;
1405 else {
1406 unsigned int j;
1407 int matched = 0;
1408
1409 descvec =
1410 vector_slot(cmd_element->strvec, index);
1411
1412 for (j = 0; j < vector_active(descvec); j++)
1413 if ((desc = vector_slot(descvec, j))) {
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001414 enum match_type ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001415
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001416 ret = cmd_match (desc->cmd, command, level, true);
1417
1418 if (ret != no_match)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001419 matched++;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001420
1421 if (match_type < ret)
1422 match_type = ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001423 }
1424 if (!matched)
1425 vector_slot(v, i) = NULL;
1426 }
1427 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001428
1429 if (match_type == no_match)
1430 return no_match;
1431
1432 /* 2nd pass: We now know the 'strongest' match type for the index, so we
1433 * go again and filter out commands whose argument (at this index) is
1434 * 'weaker'. E.g., if we have 2 commands:
1435 *
1436 * foo bar <1-255>
1437 * foo bar BLAH
1438 *
1439 * and the command string is 'foo bar 10', then we will get here with with
1440 * 'range_match' being the strongest match. However, if 'BLAH' came
1441 * earlier, it won't have been filtered out (as a CMD_VARIABLE allows "10").
1442 *
1443 * If we don't do a 2nd pass and filter it out, the higher-layers will
1444 * consider this to be ambiguous.
1445 */
1446 for (i = 0; i < vector_active(v); i++)
1447 if ((cmd_element = vector_slot(v, i)) != NULL) {
1448 if (index >= vector_active(cmd_element->strvec))
1449 vector_slot(v, i) = NULL;
1450 else {
1451 unsigned int j;
1452 int matched = 0;
1453
1454 descvec =
1455 vector_slot(cmd_element->strvec, index);
1456
1457 for (j = 0; j < vector_active(descvec); j++)
1458 if ((desc = vector_slot(descvec, j))) {
1459 enum match_type ret;
1460
1461 ret = cmd_match(desc->cmd, command, any_match, true);
1462
1463 if (ret >= match_type)
1464 matched++;
1465 }
1466 if (!matched)
1467 vector_slot(v, i) = NULL;
1468 }
1469 }
1470
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001471 return match_type;
1472}
1473
1474/* Check ambiguous match */
1475static int
1476is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
1477{
1478 unsigned int i;
1479 unsigned int j;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001480 struct cmd_element *cmd_element;
1481 const char *matched = NULL;
1482 vector descvec;
1483 struct desc *desc;
1484
1485 for (i = 0; i < vector_active(v); i++)
1486 if ((cmd_element = vector_slot(v, i)) != NULL) {
1487 int match = 0;
1488
1489 descvec = vector_slot(cmd_element->strvec, index);
1490
1491 for (j = 0; j < vector_active(descvec); j++)
1492 if ((desc = vector_slot(descvec, j))) {
1493 enum match_type ret;
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001494 const char *str = desc->cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001495
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001496 if (CMD_OPTION(str))
1497 if ((str = cmd_deopt(str)) == NULL)
1498 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001499
1500 switch (type) {
1501 case exact_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001502 if (!(CMD_VARIABLE (str))
1503 && strcmp(command, str) == 0)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001504 match++;
1505 break;
1506 case partly_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001507 if (!(CMD_VARIABLE (str))
1508 && strncmp(command, str, strlen (command)) == 0)
1509 {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001510 if (matched
1511 && strcmp(matched,
1512 str) != 0)
1513 return 1; /* There is ambiguous match. */
1514 else
1515 matched = str;
1516 match++;
1517 }
1518 break;
1519 case range_match:
1520 if (cmd_range_match
1521 (str, command)) {
1522 if (matched
1523 && strcmp(matched,
1524 str) != 0)
1525 return 1;
1526 else
1527 matched = str;
1528 match++;
1529 }
1530 break;
1531#ifdef HAVE_IPV6
1532 case ipv6_match:
1533 if (CMD_IPV6(str))
1534 match++;
1535 break;
1536 case ipv6_prefix_match:
1537 if ((ret =
1538 cmd_ipv6_prefix_match
1539 (command)) != no_match) {
1540 if (ret == partly_match)
1541 return 2; /* There is incomplete match. */
1542
1543 match++;
1544 }
1545 break;
1546#endif /* HAVE_IPV6 */
1547 case ipv4_match:
1548 if (CMD_IPV4(str))
1549 match++;
1550 break;
1551 case ipv4_prefix_match:
1552 if ((ret =
1553 cmd_ipv4_prefix_match
1554 (command)) != no_match) {
1555 if (ret == partly_match)
1556 return 2; /* There is incomplete match. */
1557
1558 match++;
1559 }
1560 break;
1561 case extend_match:
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001562 if (CMD_VARIABLE (str))
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001563 match++;
1564 break;
1565 case no_match:
1566 default:
1567 break;
1568 }
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001569
1570 if (CMD_OPTION(desc->cmd))
1571 talloc_free((void*)str);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001572 }
1573 if (!match)
1574 vector_slot(v, i) = NULL;
1575 }
1576 return 0;
1577}
1578
1579/* If src matches dst return dst string, otherwise return NULL */
1580static const char *cmd_entry_function(const char *src, const char *dst)
1581{
1582 /* Skip variable arguments. */
1583 if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
1584 CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
1585 return NULL;
1586
1587 /* In case of 'command \t', given src is NULL string. */
1588 if (src == NULL)
1589 return dst;
1590
1591 /* Matched with input string. */
1592 if (strncmp(src, dst, strlen(src)) == 0)
1593 return dst;
1594
1595 return NULL;
1596}
1597
1598/* If src matches dst return dst string, otherwise return NULL */
1599/* This version will return the dst string always if it is
1600 CMD_VARIABLE for '?' key processing */
1601static const char *cmd_entry_function_desc(const char *src, const char *dst)
1602{
1603 if (CMD_VARARG(dst))
1604 return dst;
1605
1606 if (CMD_RANGE(dst)) {
1607 if (cmd_range_match(dst, src))
1608 return dst;
1609 else
1610 return NULL;
1611 }
1612#ifdef HAVE_IPV6
1613 if (CMD_IPV6(dst)) {
1614 if (cmd_ipv6_match(src))
1615 return dst;
1616 else
1617 return NULL;
1618 }
1619
1620 if (CMD_IPV6_PREFIX(dst)) {
1621 if (cmd_ipv6_prefix_match(src))
1622 return dst;
1623 else
1624 return NULL;
1625 }
1626#endif /* HAVE_IPV6 */
1627
1628 if (CMD_IPV4(dst)) {
1629 if (cmd_ipv4_match(src))
1630 return dst;
1631 else
1632 return NULL;
1633 }
1634
1635 if (CMD_IPV4_PREFIX(dst)) {
1636 if (cmd_ipv4_prefix_match(src))
1637 return dst;
1638 else
1639 return NULL;
1640 }
1641
1642 /* Optional or variable commands always match on '?' */
1643 if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
1644 return dst;
1645
1646 /* In case of 'command \t', given src is NULL string. */
1647 if (src == NULL)
1648 return dst;
1649
1650 if (strncmp(src, dst, strlen(src)) == 0)
1651 return dst;
1652 else
1653 return NULL;
1654}
1655
1656/* Check same string element existence. If it isn't there return
1657 1. */
1658static int cmd_unique_string(vector v, const char *str)
1659{
1660 unsigned int i;
1661 char *match;
1662
1663 for (i = 0; i < vector_active(v); i++)
1664 if ((match = vector_slot(v, i)) != NULL)
1665 if (strcmp(match, str) == 0)
1666 return 0;
1667 return 1;
1668}
1669
1670/* Compare string to description vector. If there is same string
1671 return 1 else return 0. */
1672static int desc_unique_string(vector v, const char *str)
1673{
1674 unsigned int i;
1675 struct desc *desc;
1676
1677 for (i = 0; i < vector_active(v); i++)
1678 if ((desc = vector_slot(v, i)) != NULL)
1679 if (strcmp(desc->cmd, str) == 0)
1680 return 1;
1681 return 0;
1682}
1683
1684static int cmd_try_do_shortcut(enum node_type node, char *first_word)
1685{
1686 if (first_word != NULL &&
1687 node != AUTH_NODE &&
1688 node != VIEW_NODE &&
1689 node != AUTH_ENABLE_NODE &&
1690 node != ENABLE_NODE && 0 == strcmp("do", first_word))
1691 return 1;
1692 return 0;
1693}
1694
1695/* '?' describe command support. */
1696static vector
1697cmd_describe_command_real(vector vline, struct vty *vty, int *status)
1698{
1699 unsigned int i;
1700 vector cmd_vector;
1701#define INIT_MATCHVEC_SIZE 10
1702 vector matchvec;
1703 struct cmd_element *cmd_element;
1704 unsigned int index;
1705 int ret;
1706 enum match_type match;
1707 char *command;
1708 static struct desc desc_cr = { "<cr>", "" };
1709
1710 /* Set index. */
1711 if (vector_active(vline) == 0) {
1712 *status = CMD_ERR_NO_MATCH;
1713 return NULL;
1714 } else
1715 index = vector_active(vline) - 1;
1716
1717 /* Make copy vector of current node's command vector. */
1718 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1719
1720 /* Prepare match vector */
1721 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1722
1723 /* Filter commands. */
1724 /* Only words precedes current word will be checked in this loop. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001725 for (i = 0; i < index; i++) {
1726 command = vector_slot(vline, i);
1727 if (!command)
1728 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001729
Harald Welte80d30fe2013-02-12 11:08:57 +01001730 match = cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001731
Harald Welte80d30fe2013-02-12 11:08:57 +01001732 if (match == vararg_match) {
1733 struct cmd_element *cmd_element;
1734 vector descvec;
1735 unsigned int j, k;
1736
1737 for (j = 0; j < vector_active(cmd_vector); j++)
1738 if ((cmd_element =
1739 vector_slot(cmd_vector, j)) != NULL
1740 &&
1741 (vector_active(cmd_element->strvec))) {
1742 descvec =
1743 vector_slot(cmd_element->
1744 strvec,
1745 vector_active
1746 (cmd_element->
1747 strvec) - 1);
1748 for (k = 0;
1749 k < vector_active(descvec);
1750 k++) {
1751 struct desc *desc =
1752 vector_slot(descvec,
1753 k);
1754 vector_set(matchvec,
1755 desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001756 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001757 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001758
Harald Welte80d30fe2013-02-12 11:08:57 +01001759 vector_set(matchvec, &desc_cr);
1760 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001761
Harald Welte80d30fe2013-02-12 11:08:57 +01001762 return matchvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001763 }
1764
Harald Welte80d30fe2013-02-12 11:08:57 +01001765 if ((ret = is_cmd_ambiguous(command, cmd_vector, i,
1766 match)) == 1) {
1767 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001768 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001769 *status = CMD_ERR_AMBIGUOUS;
1770 return NULL;
1771 } else if (ret == 2) {
1772 vector_free(cmd_vector);
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001773 vector_free(matchvec);
Harald Welte80d30fe2013-02-12 11:08:57 +01001774 *status = CMD_ERR_NO_MATCH;
1775 return NULL;
1776 }
1777 }
1778
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001779 /* Prepare match vector */
1780 /* matchvec = vector_init (INIT_MATCHVEC_SIZE); */
1781
1782 /* Make sure that cmd_vector is filtered based on current word */
1783 command = vector_slot(vline, index);
1784 if (command)
Vadim Yanitskiy49a0dec2017-06-12 03:49:38 +07001785 cmd_filter(command, cmd_vector, index, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001786
1787 /* Make description vector. */
Harald Welte80d30fe2013-02-12 11:08:57 +01001788 for (i = 0; i < vector_active(cmd_vector); i++) {
1789 const char *string = NULL;
1790 vector strvec;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001791
Harald Welte80d30fe2013-02-12 11:08:57 +01001792 cmd_element = vector_slot(cmd_vector, i);
1793 if (!cmd_element)
1794 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001795
Harald Welted17aa592013-02-12 11:11:34 +01001796 if (cmd_element->attr & (CMD_ATTR_DEPRECATED|CMD_ATTR_HIDDEN))
1797 continue;
1798
Harald Welte80d30fe2013-02-12 11:08:57 +01001799 strvec = cmd_element->strvec;
1800
1801 /* if command is NULL, index may be equal to vector_active */
1802 if (command && index >= vector_active(strvec))
1803 vector_slot(cmd_vector, i) = NULL;
1804 else {
1805 /* Check if command is completed. */
1806 if (command == NULL
1807 && index == vector_active(strvec)) {
1808 string = "<cr>";
1809 if (!desc_unique_string(matchvec, string))
1810 vector_set(matchvec, &desc_cr);
1811 } else {
1812 unsigned int j;
1813 vector descvec = vector_slot(strvec, index);
1814 struct desc *desc;
1815
1816 for (j = 0; j < vector_active(descvec); j++) {
1817 desc = vector_slot(descvec, j);
1818 if (!desc)
1819 continue;
1820 string = cmd_entry_function_desc
1821 (command, desc->cmd);
1822 if (!string)
1823 continue;
1824 /* Uniqueness check */
1825 if (!desc_unique_string(matchvec, string))
1826 vector_set(matchvec, desc);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001827 }
1828 }
1829 }
Harald Welte80d30fe2013-02-12 11:08:57 +01001830 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001831 vector_free(cmd_vector);
1832
1833 if (vector_slot(matchvec, 0) == NULL) {
1834 vector_free(matchvec);
1835 *status = CMD_ERR_NO_MATCH;
1836 } else
1837 *status = CMD_SUCCESS;
1838
1839 return matchvec;
1840}
1841
1842vector cmd_describe_command(vector vline, struct vty * vty, int *status)
1843{
1844 vector ret;
1845
1846 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
1847 enum node_type onode;
1848 vector shifted_vline;
1849 unsigned int index;
1850
1851 onode = vty->node;
1852 vty->node = ENABLE_NODE;
1853 /* We can try it on enable node, cos' the vty is authenticated */
1854
1855 shifted_vline = vector_init(vector_count(vline));
1856 /* use memcpy? */
1857 for (index = 1; index < vector_active(vline); index++) {
1858 vector_set_index(shifted_vline, index - 1,
1859 vector_lookup(vline, index));
1860 }
1861
1862 ret = cmd_describe_command_real(shifted_vline, vty, status);
1863
1864 vector_free(shifted_vline);
1865 vty->node = onode;
1866 return ret;
1867 }
1868
1869 return cmd_describe_command_real(vline, vty, status);
1870}
1871
1872/* Check LCD of matched command. */
1873static int cmd_lcd(char **matched)
1874{
1875 int i;
1876 int j;
1877 int lcd = -1;
1878 char *s1, *s2;
1879 char c1, c2;
1880
1881 if (matched[0] == NULL || matched[1] == NULL)
1882 return 0;
1883
1884 for (i = 1; matched[i] != NULL; i++) {
1885 s1 = matched[i - 1];
1886 s2 = matched[i];
1887
1888 for (j = 0; (c1 = s1[j]) && (c2 = s2[j]); j++)
1889 if (c1 != c2)
1890 break;
1891
1892 if (lcd < 0)
1893 lcd = j;
1894 else {
1895 if (lcd > j)
1896 lcd = j;
1897 }
1898 }
1899 return lcd;
1900}
1901
1902/* Command line completion support. */
1903static char **cmd_complete_command_real(vector vline, struct vty *vty,
1904 int *status)
1905{
1906 unsigned int i;
1907 vector cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
1908#define INIT_MATCHVEC_SIZE 10
1909 vector matchvec;
1910 struct cmd_element *cmd_element;
1911 unsigned int index;
1912 char **match_str;
1913 struct desc *desc;
1914 vector descvec;
1915 char *command;
1916 int lcd;
1917
1918 if (vector_active(vline) == 0) {
1919 *status = CMD_ERR_NO_MATCH;
Holger Hans Peter Freyther047213b2013-07-03 09:32:37 +02001920 vector_free(cmd_vector);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001921 return NULL;
1922 } else
1923 index = vector_active(vline) - 1;
1924
1925 /* First, filter by preceeding command string */
1926 for (i = 0; i < index; i++)
1927 if ((command = vector_slot(vline, i))) {
1928 enum match_type match;
1929 int ret;
1930
1931 /* First try completion match, if there is exactly match return 1 */
1932 match =
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01001933 cmd_filter(command, cmd_vector, i, any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001934
1935 /* If there is exact match then filter ambiguous match else check
1936 ambiguousness. */
1937 if ((ret =
1938 is_cmd_ambiguous(command, cmd_vector, i,
1939 match)) == 1) {
1940 vector_free(cmd_vector);
1941 *status = CMD_ERR_AMBIGUOUS;
1942 return NULL;
1943 }
1944 /*
1945 else if (ret == 2)
1946 {
1947 vector_free (cmd_vector);
1948 *status = CMD_ERR_NO_MATCH;
1949 return NULL;
1950 }
1951 */
1952 }
1953
1954 /* Prepare match vector. */
1955 matchvec = vector_init(INIT_MATCHVEC_SIZE);
1956
1957 /* Now we got into completion */
1958 for (i = 0; i < vector_active(cmd_vector); i++)
1959 if ((cmd_element = vector_slot(cmd_vector, i))) {
1960 const char *string;
1961 vector strvec = cmd_element->strvec;
1962
1963 /* Check field length */
1964 if (index >= vector_active(strvec))
1965 vector_slot(cmd_vector, i) = NULL;
1966 else {
1967 unsigned int j;
1968
1969 descvec = vector_slot(strvec, index);
1970 for (j = 0; j < vector_active(descvec); j++)
1971 if ((desc = vector_slot(descvec, j))) {
1972 if ((string = cmd_entry_function(vector_slot(vline, index), desc->cmd)))
1973 if (cmd_unique_string (matchvec, string))
1974 vector_set (matchvec, talloc_strdup(tall_vty_cmd_ctx, string));
1975 }
1976 }
1977 }
1978
1979 /* We don't need cmd_vector any more. */
1980 vector_free(cmd_vector);
1981
1982 /* No matched command */
1983 if (vector_slot(matchvec, 0) == NULL) {
1984 vector_free(matchvec);
1985
1986 /* In case of 'command \t' pattern. Do you need '?' command at
1987 the end of the line. */
Pau Espin Pedrol0f7bcb52017-11-02 19:08:14 +01001988 if (vector_slot(vline, index) == NULL)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001989 *status = CMD_ERR_NOTHING_TODO;
1990 else
1991 *status = CMD_ERR_NO_MATCH;
1992 return NULL;
1993 }
1994
1995 /* Only one matched */
1996 if (vector_slot(matchvec, 1) == NULL) {
1997 match_str = (char **)matchvec->index;
1998 vector_only_wrapper_free(matchvec);
1999 *status = CMD_COMPLETE_FULL_MATCH;
2000 return match_str;
2001 }
2002 /* Make it sure last element is NULL. */
2003 vector_set(matchvec, NULL);
2004
2005 /* Check LCD of matched strings. */
2006 if (vector_slot(vline, index) != NULL) {
2007 lcd = cmd_lcd((char **)matchvec->index);
2008
2009 if (lcd) {
2010 int len = strlen(vector_slot(vline, index));
2011
2012 if (len < lcd) {
2013 char *lcdstr;
2014
2015 lcdstr = _talloc_zero(tall_vty_cmd_ctx, lcd + 1,
2016 "complete-lcdstr");
2017 memcpy(lcdstr, matchvec->index[0], lcd);
2018 lcdstr[lcd] = '\0';
2019
2020 /* match_str = (char **) &lcdstr; */
2021
2022 /* Free matchvec. */
2023 for (i = 0; i < vector_active(matchvec); i++) {
2024 if (vector_slot(matchvec, i))
2025 talloc_free(vector_slot(matchvec, i));
2026 }
2027 vector_free(matchvec);
2028
2029 /* Make new matchvec. */
2030 matchvec = vector_init(INIT_MATCHVEC_SIZE);
2031 vector_set(matchvec, lcdstr);
2032 match_str = (char **)matchvec->index;
2033 vector_only_wrapper_free(matchvec);
2034
2035 *status = CMD_COMPLETE_MATCH;
2036 return match_str;
2037 }
2038 }
2039 }
2040
2041 match_str = (char **)matchvec->index;
2042 vector_only_wrapper_free(matchvec);
2043 *status = CMD_COMPLETE_LIST_MATCH;
2044 return match_str;
2045}
2046
2047char **cmd_complete_command(vector vline, struct vty *vty, int *status)
2048{
2049 char **ret;
2050
2051 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2052 enum node_type onode;
2053 vector shifted_vline;
2054 unsigned int index;
2055
2056 onode = vty->node;
2057 vty->node = ENABLE_NODE;
2058 /* We can try it on enable node, cos' the vty is authenticated */
2059
2060 shifted_vline = vector_init(vector_count(vline));
2061 /* use memcpy? */
2062 for (index = 1; index < vector_active(vline); index++) {
2063 vector_set_index(shifted_vline, index - 1,
2064 vector_lookup(vline, index));
2065 }
2066
2067 ret = cmd_complete_command_real(shifted_vline, vty, status);
2068
2069 vector_free(shifted_vline);
2070 vty->node = onode;
2071 return ret;
2072 }
2073
2074 return cmd_complete_command_real(vline, vty, status);
2075}
2076
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002077static struct vty_parent_node *vty_parent(struct vty *vty)
2078{
2079 return llist_first_entry_or_null(&vty->parent_nodes,
2080 struct vty_parent_node,
2081 entry);
2082}
2083
2084static bool vty_pop_parent(struct vty *vty)
2085{
2086 struct vty_parent_node *parent = vty_parent(vty);
2087 if (!parent)
2088 return false;
2089 llist_del(&parent->entry);
2090 vty->node = parent->node;
2091 vty->priv = parent->priv;
2092 if (vty->indent)
2093 talloc_free(vty->indent);
2094 vty->indent = parent->indent;
2095 talloc_free(parent);
2096 return true;
2097}
2098
2099static void vty_clear_parents(struct vty *vty)
2100{
2101 while (vty_pop_parent(vty));
2102}
2103
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002104/* return parent node */
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002105/*
2106 * This function MUST eventually converge on a node when called repeatedly,
2107 * there must not be any cycles.
2108 * All 'config' nodes shall converge on CONFIG_NODE.
2109 * All other 'enable' nodes shall converge on ENABLE_NODE.
2110 * All 'view' only nodes shall converge on VIEW_NODE.
2111 * All other nodes shall converge on themselves or it must be ensured,
2112 * that the user's rights are not extended anyhow by calling this function.
2113 *
2114 * Note that these requirements also apply to all functions that are used
2115 * as go_parent_cb.
2116 * Note also that this function relies on the is_config_child callback to
2117 * recognize non-config nodes if go_parent_cb is not set.
2118 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00002119int vty_go_parent(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002120{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002121 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002122 case AUTH_NODE:
2123 case VIEW_NODE:
2124 case ENABLE_NODE:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002125 case CONFIG_NODE:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002126 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002127 break;
2128
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002129 case AUTH_ENABLE_NODE:
2130 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002131 vty_clear_parents(vty);
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002132 break;
2133
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002134 case CFG_LOG_NODE:
2135 case VTY_NODE:
2136 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002137 vty_clear_parents(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002138 break;
2139
2140 default:
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002141 if (host.app_info->go_parent_cb) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002142 host.app_info->go_parent_cb(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002143 vty_pop_parent(vty);
2144 }
2145 else if (is_config_child(vty)) {
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002146 vty->node = CONFIG_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002147 vty_clear_parents(vty);
2148 }
2149 else {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002150 vty->node = VIEW_NODE;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002151 vty_clear_parents(vty);
2152 }
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002153 break;
2154 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002155
2156 return vty->node;
2157}
2158
2159/* Execute command by argument vline vector. */
2160static int
2161cmd_execute_command_real(vector vline, struct vty *vty,
2162 struct cmd_element **cmd)
2163{
2164 unsigned int i;
2165 unsigned int index;
2166 vector cmd_vector;
2167 struct cmd_element *cmd_element;
2168 struct cmd_element *matched_element;
2169 unsigned int matched_count, incomplete_count;
2170 int argc;
2171 const char *argv[CMD_ARGC_MAX];
2172 enum match_type match = 0;
2173 int varflag;
2174 char *command;
2175
2176 /* Make copy of command elements. */
2177 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2178
2179 for (index = 0; index < vector_active(vline); index++)
2180 if ((command = vector_slot(vline, index))) {
2181 int ret;
2182
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002183 match = cmd_filter(command, cmd_vector, index,
2184 any_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002185
2186 if (match == vararg_match)
2187 break;
2188
2189 ret =
2190 is_cmd_ambiguous(command, cmd_vector, index, match);
2191
2192 if (ret == 1) {
2193 vector_free(cmd_vector);
2194 return CMD_ERR_AMBIGUOUS;
2195 } else if (ret == 2) {
2196 vector_free(cmd_vector);
2197 return CMD_ERR_NO_MATCH;
2198 }
2199 }
2200
2201 /* Check matched count. */
2202 matched_element = NULL;
2203 matched_count = 0;
2204 incomplete_count = 0;
2205
2206 for (i = 0; i < vector_active(cmd_vector); i++)
2207 if ((cmd_element = vector_slot(cmd_vector, i))) {
2208 if (match == vararg_match
2209 || index >= cmd_element->cmdsize) {
2210 matched_element = cmd_element;
2211#if 0
2212 printf("DEBUG: %s\n", cmd_element->string);
2213#endif
2214 matched_count++;
2215 } else {
2216 incomplete_count++;
2217 }
2218 }
2219
2220 /* Finish of using cmd_vector. */
2221 vector_free(cmd_vector);
2222
2223 /* To execute command, matched_count must be 1. */
2224 if (matched_count == 0) {
2225 if (incomplete_count)
2226 return CMD_ERR_INCOMPLETE;
2227 else
2228 return CMD_ERR_NO_MATCH;
2229 }
2230
2231 if (matched_count > 1)
2232 return CMD_ERR_AMBIGUOUS;
2233
2234 /* Argument treatment */
2235 varflag = 0;
2236 argc = 0;
2237
2238 for (i = 0; i < vector_active(vline); i++) {
2239 if (varflag)
2240 argv[argc++] = vector_slot(vline, i);
2241 else {
2242 vector descvec =
2243 vector_slot(matched_element->strvec, i);
2244
2245 if (vector_active(descvec) == 1) {
2246 struct desc *desc = vector_slot(descvec, 0);
2247
2248 if (CMD_VARARG(desc->cmd))
2249 varflag = 1;
2250
2251 if (varflag || CMD_VARIABLE(desc->cmd)
2252 || CMD_OPTION(desc->cmd))
2253 argv[argc++] = vector_slot(vline, i);
2254 } else
2255 argv[argc++] = vector_slot(vline, i);
2256 }
2257
2258 if (argc >= CMD_ARGC_MAX)
2259 return CMD_ERR_EXEED_ARGC_MAX;
2260 }
2261
2262 /* For vtysh execution. */
2263 if (cmd)
2264 *cmd = matched_element;
2265
2266 if (matched_element->daemon)
2267 return CMD_SUCCESS_DAEMON;
2268
2269 /* Execute matched command. */
2270 return (*matched_element->func) (matched_element, vty, argc, argv);
2271}
2272
2273int
2274cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
2275 int vtysh)
2276{
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002277 int ret;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002278 enum node_type onode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002279
2280 onode = vty->node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002281
2282 if (cmd_try_do_shortcut(vty->node, vector_slot(vline, 0))) {
2283 vector shifted_vline;
2284 unsigned int index;
2285
2286 vty->node = ENABLE_NODE;
2287 /* We can try it on enable node, cos' the vty is authenticated */
2288
2289 shifted_vline = vector_init(vector_count(vline));
2290 /* use memcpy? */
2291 for (index = 1; index < vector_active(vline); index++) {
2292 vector_set_index(shifted_vline, index - 1,
2293 vector_lookup(vline, index));
2294 }
2295
2296 ret = cmd_execute_command_real(shifted_vline, vty, cmd);
2297
2298 vector_free(shifted_vline);
2299 vty->node = onode;
2300 return ret;
2301 }
2302
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +02002303 return cmd_execute_command_real(vline, vty, cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002304}
2305
2306/* Execute command by argument readline. */
2307int
2308cmd_execute_command_strict(vector vline, struct vty *vty,
2309 struct cmd_element **cmd)
2310{
2311 unsigned int i;
2312 unsigned int index;
2313 vector cmd_vector;
2314 struct cmd_element *cmd_element;
2315 struct cmd_element *matched_element;
2316 unsigned int matched_count, incomplete_count;
2317 int argc;
2318 const char *argv[CMD_ARGC_MAX];
2319 int varflag;
2320 enum match_type match = 0;
2321 char *command;
2322
2323 /* Make copy of command element */
2324 cmd_vector = vector_copy(cmd_node_vector(cmdvec, vty->node));
2325
2326 for (index = 0; index < vector_active(vline); index++)
2327 if ((command = vector_slot(vline, index))) {
2328 int ret;
2329
Sylvain Munaut4d8eea42012-12-28 11:58:23 +01002330 match = cmd_filter(vector_slot(vline, index),
2331 cmd_vector, index, exact_match);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002332
2333 /* If command meets '.VARARG' then finish matching. */
2334 if (match == vararg_match)
2335 break;
2336
2337 ret =
2338 is_cmd_ambiguous(command, cmd_vector, index, match);
2339 if (ret == 1) {
2340 vector_free(cmd_vector);
2341 return CMD_ERR_AMBIGUOUS;
2342 }
2343 if (ret == 2) {
2344 vector_free(cmd_vector);
2345 return CMD_ERR_NO_MATCH;
2346 }
2347 }
2348
2349 /* Check matched count. */
2350 matched_element = NULL;
2351 matched_count = 0;
2352 incomplete_count = 0;
2353 for (i = 0; i < vector_active(cmd_vector); i++)
2354 if (vector_slot(cmd_vector, i) != NULL) {
2355 cmd_element = vector_slot(cmd_vector, i);
2356
2357 if (match == vararg_match
2358 || index >= cmd_element->cmdsize) {
2359 matched_element = cmd_element;
2360 matched_count++;
2361 } else
2362 incomplete_count++;
2363 }
2364
2365 /* Finish of using cmd_vector. */
2366 vector_free(cmd_vector);
2367
2368 /* To execute command, matched_count must be 1. */
2369 if (matched_count == 0) {
2370 if (incomplete_count)
2371 return CMD_ERR_INCOMPLETE;
2372 else
2373 return CMD_ERR_NO_MATCH;
2374 }
2375
2376 if (matched_count > 1)
2377 return CMD_ERR_AMBIGUOUS;
2378
2379 /* Argument treatment */
2380 varflag = 0;
2381 argc = 0;
2382
2383 for (i = 0; i < vector_active(vline); i++) {
2384 if (varflag)
2385 argv[argc++] = vector_slot(vline, i);
2386 else {
2387 vector descvec =
2388 vector_slot(matched_element->strvec, i);
2389
2390 if (vector_active(descvec) == 1) {
2391 struct desc *desc = vector_slot(descvec, 0);
2392
2393 if (CMD_VARARG(desc->cmd))
2394 varflag = 1;
2395
2396 if (varflag || CMD_VARIABLE(desc->cmd)
2397 || CMD_OPTION(desc->cmd))
2398 argv[argc++] = vector_slot(vline, i);
2399 } else
2400 argv[argc++] = vector_slot(vline, i);
2401 }
2402
2403 if (argc >= CMD_ARGC_MAX)
2404 return CMD_ERR_EXEED_ARGC_MAX;
2405 }
2406
2407 /* For vtysh execution. */
2408 if (cmd)
2409 *cmd = matched_element;
2410
2411 if (matched_element->daemon)
2412 return CMD_SUCCESS_DAEMON;
2413
2414 /* Now execute matched command */
2415 return (*matched_element->func) (matched_element, vty, argc, argv);
2416}
2417
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002418static inline size_t len(const char *str)
2419{
2420 return str? strlen(str) : 0;
2421}
2422
Neels Hofmeyr00b5ed32017-09-20 00:46:03 +02002423/*! Make sure the common length of strings a and b is identical, then compare their lengths. I.e., if a
2424 * is longer than b, a must start with exactly b, and vice versa.
2425 * \returns EINVAL on mismatch, -1 for a < b, 0 for a == b, 1 for a > b.
2426 */
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002427static int indent_cmp(const char *a, const char *b)
2428{
2429 size_t al, bl;
2430 al = len(a);
2431 bl = len(b);
2432 if (al > bl) {
2433 if (bl && strncmp(a, b, bl) != 0)
2434 return EINVAL;
2435 return 1;
2436 }
2437 /* al <= bl */
2438 if (al && strncmp(a, b, al) != 0)
2439 return EINVAL;
2440 return (al < bl)? -1 : 0;
2441}
2442
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002443/* Configration make from file. */
2444int config_from_file(struct vty *vty, FILE * fp)
2445{
2446 int ret;
2447 vector vline;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002448 char *indent;
2449 int cmp;
2450 struct vty_parent_node this_node;
2451 struct vty_parent_node *parent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002452
2453 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002454 indent = NULL;
2455 vline = NULL;
2456 ret = cmd_make_strvec2(vty->buf, &indent, &vline);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002457
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002458 if (ret != CMD_SUCCESS)
2459 goto return_invalid_indent;
2460
2461 /* In case of comment or empty line */
2462 if (vline == NULL) {
2463 if (indent) {
2464 talloc_free(indent);
2465 indent = NULL;
2466 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002467 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002468 }
2469
Neels Hofmeyr43063632017-09-19 23:54:01 +02002470 /* We have a nonempty line. */
2471 if (!vty->indent) {
2472 /* We have just entered a node and expecting the first child to come up; but we
2473 * may also skip right back to a parent or ancestor level. */
2474 parent = vty_parent(vty);
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002475
Neels Hofmeyr43063632017-09-19 23:54:01 +02002476 /* If there is no parent, record any indentation we encounter. */
2477 cmp = parent ? indent_cmp(indent, parent->indent) : 1;
2478
2479 if (cmp == EINVAL)
2480 goto return_invalid_indent;
2481
2482 if (cmp <= 0) {
2483 /* We have gone right back to the parent level or higher, we are skipping
2484 * this child node level entirely. Pop the parent to go back to a node
2485 * that was actually there (to reinstate vty->indent) and re-use below
2486 * go-parent while-loop to find an accurate match of indent in the node
2487 * ancestry. */
2488 vty_go_parent(vty);
2489 } else {
2490 /* The indent is deeper than the just entered parent, record the new
2491 * indentation characters. */
2492 vty->indent = talloc_strdup(vty, indent);
2493 /* This *is* the new indentation. */
2494 cmp = 0;
2495 }
2496 } else {
2497 /* There is a known indentation for this node level, validate and detect node
2498 * exits. */
2499 cmp = indent_cmp(indent, vty->indent);
2500 if (cmp == EINVAL)
2501 goto return_invalid_indent;
2502 }
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002503
2504 /* Less indent: go up the parent nodes to find matching amount of less indent. When this
2505 * loop exits, we want to have found an exact match, i.e. cmp == 0. */
2506 while (cmp < 0) {
2507 vty_go_parent(vty);
2508 cmp = indent_cmp(indent, vty->indent);
2509 if (cmp == EINVAL)
2510 goto return_invalid_indent;
2511 }
2512
2513 /* More indent without having entered a child node level? Either the parent node's indent
2514 * wasn't hit exactly (e.g. there's a space more than the parent level had further above)
2515 * or the indentation increased even though the vty command didn't enter a child. */
2516 if (cmp > 0)
2517 goto return_invalid_indent;
2518
2519 /* Remember the current node before the command possibly changes it. */
2520 this_node = (struct vty_parent_node){
2521 .node = vty->node,
2522 .priv = vty->priv,
2523 .indent = vty->indent,
2524 };
2525
2526 parent = vty_parent(vty);
2527 ret = cmd_execute_command_strict(vline, vty, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002528 cmd_free_strvec(vline);
2529
2530 if (ret != CMD_SUCCESS && ret != CMD_WARNING
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002531 && ret != CMD_ERR_NOTHING_TODO) {
2532 if (indent) {
2533 talloc_free(indent);
2534 indent = NULL;
2535 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002536 return ret;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002537 }
2538
2539 /* If we have stepped down into a child node, push a parent frame.
2540 * The causality is such: we don't expect every single node entry implementation to push
2541 * a parent node entry onto vty->parent_nodes. Instead we expect vty_go_parent() to *pop*
2542 * a parent node. Hence if the node changed without the parent node changing, we must
2543 * have stepped into a child node (and now expect a deeper indent). */
2544 if (vty->node != this_node.node && parent == vty_parent(vty)) {
2545 /* Push the parent node. */
2546 parent = talloc_zero(vty, struct vty_parent_node);
2547 *parent = this_node;
2548 llist_add(&parent->entry, &vty->parent_nodes);
2549
2550 /* The current talloc'ed vty->indent string will now be owned by this parent
2551 * struct. Indicate that we don't know what deeper indent characters the user
2552 * will choose. */
2553 vty->indent = NULL;
2554 }
2555
2556 if (indent) {
2557 talloc_free(indent);
2558 indent = NULL;
2559 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002560 }
2561 return CMD_SUCCESS;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02002562
2563return_invalid_indent:
2564 if (vline)
2565 cmd_free_strvec(vline);
2566 if (indent) {
2567 talloc_free(indent);
2568 indent = NULL;
2569 }
2570 return CMD_ERR_INVALID_INDENT;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002571}
2572
2573/* Configration from terminal */
2574DEFUN(config_terminal,
2575 config_terminal_cmd,
2576 "configure terminal",
2577 "Configuration from vty interface\n" "Configuration terminal\n")
2578{
2579 if (vty_config_lock(vty))
2580 vty->node = CONFIG_NODE;
2581 else {
2582 vty_out(vty, "VTY configuration is locked by other VTY%s",
2583 VTY_NEWLINE);
2584 return CMD_WARNING;
2585 }
2586 return CMD_SUCCESS;
2587}
2588
2589/* Enable command */
2590DEFUN(enable, config_enable_cmd, "enable", "Turn on privileged mode command\n")
2591{
2592 /* If enable password is NULL, change to ENABLE_NODE */
2593 if ((host.enable == NULL && host.enable_encrypt == NULL) ||
2594 vty->type == VTY_SHELL_SERV)
2595 vty->node = ENABLE_NODE;
2596 else
2597 vty->node = AUTH_ENABLE_NODE;
2598
2599 return CMD_SUCCESS;
2600}
2601
2602/* Disable command */
2603DEFUN(disable,
2604 config_disable_cmd, "disable", "Turn off privileged mode command\n")
2605{
2606 if (vty->node == ENABLE_NODE)
2607 vty->node = VIEW_NODE;
2608 return CMD_SUCCESS;
2609}
2610
2611/* Down vty node level. */
2612gDEFUN(config_exit,
2613 config_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
2614{
2615 switch (vty->node) {
Jacob Erlbeckb3657e12013-09-10 14:04:54 +02002616 case AUTH_NODE:
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002617 case VIEW_NODE:
2618 case ENABLE_NODE:
Harald Weltea99d45a2015-11-12 13:48:23 +01002619 vty->status = VTY_CLOSE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002620 break;
2621 case CONFIG_NODE:
2622 vty->node = ENABLE_NODE;
2623 vty_config_unlock(vty);
2624 break;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002625 default:
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002626 if (vty->node > CONFIG_NODE)
2627 vty_go_parent (vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002628 break;
2629 }
2630 return CMD_SUCCESS;
2631}
2632
2633/* End of configuration. */
2634 gDEFUN(config_end,
2635 config_end_cmd, "end", "End current mode and change to enable mode.")
2636{
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002637 if (vty->node > ENABLE_NODE) {
Jacob Erlbeck23497212013-09-10 09:07:31 +02002638 int last_node = CONFIG_NODE;
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002639
2640 /* Repeatedly call go_parent until a top node is reached. */
2641 while (vty->node > CONFIG_NODE) {
2642 if (vty->node == last_node) {
2643 /* Ensure termination, this shouldn't happen. */
2644 break;
2645 }
2646 last_node = vty->node;
2647 vty_go_parent(vty);
2648 }
2649
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002650 vty_config_unlock(vty);
Jacob Erlbeck7eed0532013-09-06 16:51:59 +02002651 if (vty->node > ENABLE_NODE)
2652 vty->node = ENABLE_NODE;
2653 vty->index = NULL;
2654 vty->index_sub = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002655 }
2656 return CMD_SUCCESS;
2657}
2658
2659/* Show version. */
2660DEFUN(show_version,
2661 show_version_cmd, "show version", SHOW_STR "Displays program version\n")
2662{
Harald Welte237f6242010-05-25 23:00:45 +02002663 vty_out(vty, "%s %s (%s).%s", host.app_info->name,
2664 host.app_info->version,
2665 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
2666 vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002667
2668 return CMD_SUCCESS;
2669}
2670
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01002671DEFUN(show_online_help,
2672 show_online_help_cmd, "show online-help", SHOW_STR "Online help\n")
2673{
2674 vty_dump_nodes(vty);
2675 return CMD_SUCCESS;
2676}
2677
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002678/* Help display function for all node. */
2679gDEFUN(config_help,
2680 config_help_cmd, "help", "Description of the interactive help system\n")
2681{
2682 vty_out(vty,
2683 "This VTY provides advanced help features. When you need help,%s\
2684anytime at the command line please press '?'.%s\
2685%s\
2686If nothing matches, the help list will be empty and you must backup%s\
2687 until entering a '?' shows the available options.%s\
2688Two styles of help are provided:%s\
26891. Full help is available when you are ready to enter a%s\
2690command argument (e.g. 'show ?') and describes each possible%s\
2691argument.%s\
26922. Partial help is provided when an abbreviated argument is entered%s\
2693 and you want to know what arguments match the input%s\
2694 (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
2695 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
2696 return CMD_SUCCESS;
2697}
2698
2699/* Help display function for all node. */
2700gDEFUN(config_list, config_list_cmd, "list", "Print command list\n")
2701{
2702 unsigned int i;
2703 struct cmd_node *cnode = vector_slot(cmdvec, vty->node);
2704 struct cmd_element *cmd;
2705
2706 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
2707 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL
2708 && !(cmd->attr == CMD_ATTR_DEPRECATED
2709 || cmd->attr == CMD_ATTR_HIDDEN))
2710 vty_out(vty, " %s%s", cmd->string, VTY_NEWLINE);
2711 return CMD_SUCCESS;
2712}
2713
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002714static int write_config_file(const char *config_file, char **outpath)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002715{
2716 unsigned int i;
2717 int fd;
2718 struct cmd_node *node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002719 char *config_file_tmp = NULL;
2720 char *config_file_sav = NULL;
2721 struct vty *file_vty;
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002722 struct stat st;
2723
2724 *outpath = NULL;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002725
2726 /* Check and see if we are operating under vtysh configuration */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002727 config_file_sav =
2728 _talloc_zero(tall_vty_cmd_ctx,
2729 strlen(config_file) + strlen(CONF_BACKUP_EXT) + 1,
2730 "config_file_sav");
2731 strcpy(config_file_sav, config_file);
2732 strcat(config_file_sav, CONF_BACKUP_EXT);
2733
2734 config_file_tmp = _talloc_zero(tall_vty_cmd_ctx, strlen(config_file) + 8,
2735 "config_file_tmp");
2736 sprintf(config_file_tmp, "%s.XXXXXX", config_file);
2737
2738 /* Open file to configuration write. */
2739 fd = mkstemp(config_file_tmp);
2740 if (fd < 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002741 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_tmp);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002742 talloc_free(config_file_tmp);
2743 talloc_free(config_file_sav);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002744 return -1;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002745 }
2746
2747 /* Make vty for configuration file. */
2748 file_vty = vty_new();
2749 file_vty->fd = fd;
2750 file_vty->type = VTY_FILE;
2751
2752 /* Config file header print. */
2753 vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
Harald Welte237f6242010-05-25 23:00:45 +02002754 host.app_info->name, host.app_info->version);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002755 //vty_time_print (file_vty, 1);
2756 vty_out(file_vty, "!\n");
2757
2758 for (i = 0; i < vector_active(cmdvec); i++)
2759 if ((node = vector_slot(cmdvec, i)) && node->func) {
2760 if ((*node->func) (file_vty))
2761 vty_out(file_vty, "!\n");
2762 }
2763 vty_close(file_vty);
2764
2765 if (unlink(config_file_sav) != 0)
2766 if (errno != ENOENT) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002767 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002768 talloc_free(config_file_sav);
2769 talloc_free(config_file_tmp);
2770 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002771 return -2;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002772 }
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002773
2774 /* Only link the .sav file if the original file exists */
2775 if (stat(config_file, &st) == 0) {
2776 if (link(config_file, config_file_sav) != 0) {
2777 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file_sav);
2778 talloc_free(config_file_sav);
2779 talloc_free(config_file_tmp);
2780 unlink(config_file_tmp);
2781 return -3;
2782 }
2783 sync();
2784 if (unlink(config_file) != 0) {
2785 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2786 talloc_free(config_file_sav);
2787 talloc_free(config_file_tmp);
2788 unlink(config_file_tmp);
2789 return -4;
2790 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002791 }
2792 if (link(config_file_tmp, config_file) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002793 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002794 talloc_free(config_file_sav);
2795 talloc_free(config_file_tmp);
2796 unlink(config_file_tmp);
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002797 return -5;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002798 }
2799 unlink(config_file_tmp);
2800 sync();
2801
2802 talloc_free(config_file_sav);
2803 talloc_free(config_file_tmp);
2804
2805 if (chmod(config_file, 0666 & ~CONFIGFILE_MASK) != 0) {
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002806 *outpath = talloc_strdup(tall_vty_cmd_ctx, config_file);
2807 return -6;
2808 }
2809
2810 return 0;
2811}
2812
2813
2814/* Write current configuration into file. */
2815DEFUN(config_write_file,
2816 config_write_file_cmd,
2817 "write file",
2818 "Write running configuration to memory, network, or terminal\n"
2819 "Write to configuration file\n")
2820{
2821 char *failed_file;
2822 int rc;
2823
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +01002824 if (host.app_info->config_is_consistent) {
2825 rc = host.app_info->config_is_consistent(vty);
2826 if (!rc) {
2827 vty_out(vty, "Configuration is not consistent%s",
2828 VTY_NEWLINE);
2829 return CMD_WARNING;
2830 }
2831 }
2832
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002833 if (host.config == NULL) {
2834 vty_out(vty, "Can't save to configuration file, using vtysh.%s",
2835 VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002836 return CMD_WARNING;
2837 }
2838
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01002839 rc = write_config_file(host.config, &failed_file);
2840 switch (rc) {
2841 case -1:
2842 vty_out(vty, "Can't open configuration file %s.%s",
2843 failed_file, VTY_NEWLINE);
2844 rc = CMD_WARNING;
2845 break;
2846 case -2:
2847 vty_out(vty, "Can't unlink backup configuration file %s.%s",
2848 failed_file, VTY_NEWLINE);
2849 rc = CMD_WARNING;
2850 break;
2851 case -3:
2852 vty_out(vty, "Can't backup old configuration file %s.%s",
2853 failed_file, VTY_NEWLINE);
2854 rc = CMD_WARNING;
2855 break;
2856 case -4:
2857 vty_out(vty, "Can't unlink configuration file %s.%s",
2858 failed_file, VTY_NEWLINE);
2859 rc = CMD_WARNING;
2860 break;
2861 case -5:
2862 vty_out(vty, "Can't save configuration file %s.%s", failed_file,
2863 VTY_NEWLINE);
2864 rc = CMD_WARNING;
2865 break;
2866 case -6:
2867 vty_out(vty, "Can't chmod configuration file %s: %s (%d).%s",
2868 failed_file, strerror(errno), errno, VTY_NEWLINE);
2869 rc = CMD_WARNING;
2870 break;
2871 default:
2872 vty_out(vty, "Configuration saved to %s%s", host.config, VTY_NEWLINE);
2873 rc = CMD_SUCCESS;
2874 break;
2875 }
2876
2877 talloc_free(failed_file);
2878 return rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002879}
2880
2881ALIAS(config_write_file,
2882 config_write_cmd,
2883 "write", "Write running configuration to memory, network, or terminal\n")
2884
2885 ALIAS(config_write_file,
2886 config_write_memory_cmd,
2887 "write memory",
2888 "Write running configuration to memory, network, or terminal\n"
2889 "Write configuration to the file (same as write file)\n")
2890
2891 ALIAS(config_write_file,
2892 copy_runningconfig_startupconfig_cmd,
2893 "copy running-config startup-config",
2894 "Copy configuration\n"
2895 "Copy running config to... \n"
2896 "Copy running config to startup config (same as write file)\n")
2897
2898/* Write current configuration into the terminal. */
2899 DEFUN(config_write_terminal,
2900 config_write_terminal_cmd,
2901 "write terminal",
2902 "Write running configuration to memory, network, or terminal\n"
2903 "Write to terminal\n")
2904{
2905 unsigned int i;
2906 struct cmd_node *node;
2907
2908 if (vty->type == VTY_SHELL_SERV) {
2909 for (i = 0; i < vector_active(cmdvec); i++)
2910 if ((node = vector_slot(cmdvec, i)) && node->func
2911 && node->vtysh) {
2912 if ((*node->func) (vty))
2913 vty_out(vty, "!%s", VTY_NEWLINE);
2914 }
2915 } else {
2916 vty_out(vty, "%sCurrent configuration:%s", VTY_NEWLINE,
2917 VTY_NEWLINE);
2918 vty_out(vty, "!%s", VTY_NEWLINE);
2919
2920 for (i = 0; i < vector_active(cmdvec); i++)
2921 if ((node = vector_slot(cmdvec, i)) && node->func) {
2922 if ((*node->func) (vty))
2923 vty_out(vty, "!%s", VTY_NEWLINE);
2924 }
2925 vty_out(vty, "end%s", VTY_NEWLINE);
2926 }
2927 return CMD_SUCCESS;
2928}
2929
2930/* Write current configuration into the terminal. */
2931ALIAS(config_write_terminal,
2932 show_running_config_cmd,
2933 "show running-config", SHOW_STR "running configuration\n")
2934
2935/* Write startup configuration into the terminal. */
2936 DEFUN(show_startup_config,
2937 show_startup_config_cmd,
2938 "show startup-config", SHOW_STR "Contentes of startup configuration\n")
2939{
2940 char buf[BUFSIZ];
2941 FILE *confp;
2942
2943 confp = fopen(host.config, "r");
2944 if (confp == NULL) {
2945 vty_out(vty, "Can't open configuration file [%s]%s",
2946 host.config, VTY_NEWLINE);
2947 return CMD_WARNING;
2948 }
2949
2950 while (fgets(buf, BUFSIZ, confp)) {
2951 char *cp = buf;
2952
2953 while (*cp != '\r' && *cp != '\n' && *cp != '\0')
2954 cp++;
2955 *cp = '\0';
2956
2957 vty_out(vty, "%s%s", buf, VTY_NEWLINE);
2958 }
2959
2960 fclose(confp);
2961
2962 return CMD_SUCCESS;
2963}
2964
2965/* Hostname configuration */
2966DEFUN(config_hostname,
2967 hostname_cmd,
2968 "hostname WORD",
2969 "Set system's network name\n" "This system's network name\n")
2970{
2971 if (!isalpha((int)*argv[0])) {
2972 vty_out(vty, "Please specify string starting with alphabet%s",
2973 VTY_NEWLINE);
2974 return CMD_WARNING;
2975 }
2976
2977 if (host.name)
2978 talloc_free(host.name);
2979
2980 host.name = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
2981 return CMD_SUCCESS;
2982}
2983
2984DEFUN(config_no_hostname,
2985 no_hostname_cmd,
2986 "no hostname [HOSTNAME]",
2987 NO_STR "Reset system's network name\n" "Host name of this router\n")
2988{
2989 if (host.name)
2990 talloc_free(host.name);
2991 host.name = NULL;
2992 return CMD_SUCCESS;
2993}
2994
2995/* VTY interface password set. */
2996DEFUN(config_password, password_cmd,
2997 "password (8|) WORD",
2998 "Assign the terminal connection password\n"
2999 "Specifies a HIDDEN password will follow\n"
3000 "dummy string \n" "The HIDDEN line password string\n")
3001{
3002 /* Argument check. */
3003 if (argc == 0) {
3004 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3005 return CMD_WARNING;
3006 }
3007
3008 if (argc == 2) {
3009 if (*argv[0] == '8') {
3010 if (host.password)
3011 talloc_free(host.password);
3012 host.password = NULL;
3013 if (host.password_encrypt)
3014 talloc_free(host.password_encrypt);
3015 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3016 return CMD_SUCCESS;
3017 } else {
3018 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3019 return CMD_WARNING;
3020 }
3021 }
3022
3023 if (!isalnum((int)*argv[0])) {
3024 vty_out(vty,
3025 "Please specify string starting with alphanumeric%s",
3026 VTY_NEWLINE);
3027 return CMD_WARNING;
3028 }
3029
3030 if (host.password)
3031 talloc_free(host.password);
3032 host.password = NULL;
3033
3034#ifdef VTY_CRYPT_PW
3035 if (host.encrypt) {
3036 if (host.password_encrypt)
3037 talloc_free(host.password_encrypt);
3038 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3039 } else
3040#endif
3041 host.password = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3042
3043 return CMD_SUCCESS;
3044}
3045
3046ALIAS(config_password, password_text_cmd,
3047 "password LINE",
3048 "Assign the terminal connection password\n"
3049 "The UNENCRYPTED (cleartext) line password\n")
3050
3051/* VTY enable password set. */
3052 DEFUN(config_enable_password, enable_password_cmd,
3053 "enable password (8|) WORD",
3054 "Modify enable password parameters\n"
3055 "Assign the privileged level password\n"
3056 "Specifies a HIDDEN password will follow\n"
3057 "dummy string \n" "The HIDDEN 'enable' password string\n")
3058{
3059 /* Argument check. */
3060 if (argc == 0) {
3061 vty_out(vty, "Please specify password.%s", VTY_NEWLINE);
3062 return CMD_WARNING;
3063 }
3064
3065 /* Crypt type is specified. */
3066 if (argc == 2) {
3067 if (*argv[0] == '8') {
3068 if (host.enable)
3069 talloc_free(host.enable);
3070 host.enable = NULL;
3071
3072 if (host.enable_encrypt)
3073 talloc_free(host.enable_encrypt);
3074 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, argv[1]);
3075
3076 return CMD_SUCCESS;
3077 } else {
3078 vty_out(vty, "Unknown encryption type.%s", VTY_NEWLINE);
3079 return CMD_WARNING;
3080 }
3081 }
3082
3083 if (!isalnum((int)*argv[0])) {
3084 vty_out(vty,
3085 "Please specify string starting with alphanumeric%s",
3086 VTY_NEWLINE);
3087 return CMD_WARNING;
3088 }
3089
3090 if (host.enable)
3091 talloc_free(host.enable);
3092 host.enable = NULL;
3093
3094 /* Plain password input. */
3095#ifdef VTY_CRYPT_PW
3096 if (host.encrypt) {
3097 if (host.enable_encrypt)
3098 talloc_free(host.enable_encrypt);
3099 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(argv[0]));
3100 } else
3101#endif
3102 host.enable = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3103
3104 return CMD_SUCCESS;
3105}
3106
3107ALIAS(config_enable_password,
3108 enable_password_text_cmd,
3109 "enable password LINE",
3110 "Modify enable password parameters\n"
3111 "Assign the privileged level password\n"
3112 "The UNENCRYPTED (cleartext) 'enable' password\n")
3113
3114/* VTY enable password delete. */
3115 DEFUN(no_config_enable_password, no_enable_password_cmd,
3116 "no enable password",
3117 NO_STR
3118 "Modify enable password parameters\n"
3119 "Assign the privileged level password\n")
3120{
3121 if (host.enable)
3122 talloc_free(host.enable);
3123 host.enable = NULL;
3124
3125 if (host.enable_encrypt)
3126 talloc_free(host.enable_encrypt);
3127 host.enable_encrypt = NULL;
3128
3129 return CMD_SUCCESS;
3130}
3131
3132#ifdef VTY_CRYPT_PW
3133DEFUN(service_password_encrypt,
3134 service_password_encrypt_cmd,
3135 "service password-encryption",
3136 "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3137{
3138 if (host.encrypt)
3139 return CMD_SUCCESS;
3140
3141 host.encrypt = 1;
3142
3143 if (host.password) {
3144 if (host.password_encrypt)
3145 talloc_free(host.password_encrypt);
3146 host.password_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.password));
3147 }
3148 if (host.enable) {
3149 if (host.enable_encrypt)
3150 talloc_free(host.enable_encrypt);
3151 host.enable_encrypt = talloc_strdup(tall_vty_cmd_ctx, zencrypt(host.enable));
3152 }
3153
3154 return CMD_SUCCESS;
3155}
3156
3157DEFUN(no_service_password_encrypt,
3158 no_service_password_encrypt_cmd,
3159 "no service password-encryption",
3160 NO_STR "Set up miscellaneous service\n" "Enable encrypted passwords\n")
3161{
3162 if (!host.encrypt)
3163 return CMD_SUCCESS;
3164
3165 host.encrypt = 0;
3166
3167 if (host.password_encrypt)
3168 talloc_free(host.password_encrypt);
3169 host.password_encrypt = NULL;
3170
3171 if (host.enable_encrypt)
3172 talloc_free(host.enable_encrypt);
3173 host.enable_encrypt = NULL;
3174
3175 return CMD_SUCCESS;
3176}
3177#endif
3178
3179DEFUN(config_terminal_length, config_terminal_length_cmd,
3180 "terminal length <0-512>",
3181 "Set terminal line parameters\n"
3182 "Set number of lines on a screen\n"
3183 "Number of lines on screen (0 for no pausing)\n")
3184{
3185 int lines;
3186 char *endptr = NULL;
3187
3188 lines = strtol(argv[0], &endptr, 10);
3189 if (lines < 0 || lines > 512 || *endptr != '\0') {
3190 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3191 return CMD_WARNING;
3192 }
3193 vty->lines = lines;
3194
3195 return CMD_SUCCESS;
3196}
3197
3198DEFUN(config_terminal_no_length, config_terminal_no_length_cmd,
3199 "terminal no length",
3200 "Set terminal line parameters\n"
3201 NO_STR "Set number of lines on a screen\n")
3202{
3203 vty->lines = -1;
3204 return CMD_SUCCESS;
3205}
3206
3207DEFUN(service_terminal_length, service_terminal_length_cmd,
3208 "service terminal-length <0-512>",
3209 "Set up miscellaneous service\n"
3210 "System wide terminal length configuration\n"
3211 "Number of lines of VTY (0 means no line control)\n")
3212{
3213 int lines;
3214 char *endptr = NULL;
3215
3216 lines = strtol(argv[0], &endptr, 10);
3217 if (lines < 0 || lines > 512 || *endptr != '\0') {
3218 vty_out(vty, "length is malformed%s", VTY_NEWLINE);
3219 return CMD_WARNING;
3220 }
3221 host.lines = lines;
3222
3223 return CMD_SUCCESS;
3224}
3225
3226DEFUN(no_service_terminal_length, no_service_terminal_length_cmd,
3227 "no service terminal-length [<0-512>]",
3228 NO_STR
3229 "Set up miscellaneous service\n"
3230 "System wide terminal length configuration\n"
3231 "Number of lines of VTY (0 means no line control)\n")
3232{
3233 host.lines = -1;
3234 return CMD_SUCCESS;
3235}
3236
3237DEFUN_HIDDEN(do_echo,
3238 echo_cmd,
3239 "echo .MESSAGE",
3240 "Echo a message back to the vty\n" "The message to echo\n")
3241{
3242 char *message;
3243
3244 vty_out(vty, "%s%s",
3245 ((message =
3246 argv_concat(argv, argc, 0)) ? message : ""), VTY_NEWLINE);
3247 if (message)
3248 talloc_free(message);
3249 return CMD_SUCCESS;
3250}
3251
3252#if 0
3253DEFUN(config_logmsg,
3254 config_logmsg_cmd,
3255 "logmsg " LOG_LEVELS " .MESSAGE",
3256 "Send a message to enabled logging destinations\n"
3257 LOG_LEVEL_DESC "The message to send\n")
3258{
3259 int level;
3260 char *message;
3261
3262 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3263 return CMD_ERR_NO_MATCH;
3264
3265 zlog(NULL, level,
3266 ((message = argv_concat(argv, argc, 1)) ? message : ""));
3267 if (message)
3268 talloc_free(message);
3269 return CMD_SUCCESS;
3270}
3271
3272DEFUN(show_logging,
3273 show_logging_cmd,
3274 "show logging", SHOW_STR "Show current logging configuration\n")
3275{
3276 struct zlog *zl = zlog_default;
3277
3278 vty_out(vty, "Syslog logging: ");
3279 if (zl->maxlvl[ZLOG_DEST_SYSLOG] == ZLOG_DISABLED)
3280 vty_out(vty, "disabled");
3281 else
3282 vty_out(vty, "level %s, facility %s, ident %s",
3283 zlog_priority[zl->maxlvl[ZLOG_DEST_SYSLOG]],
3284 facility_name(zl->facility), zl->ident);
3285 vty_out(vty, "%s", VTY_NEWLINE);
3286
3287 vty_out(vty, "Stdout logging: ");
3288 if (zl->maxlvl[ZLOG_DEST_STDOUT] == ZLOG_DISABLED)
3289 vty_out(vty, "disabled");
3290 else
3291 vty_out(vty, "level %s",
3292 zlog_priority[zl->maxlvl[ZLOG_DEST_STDOUT]]);
3293 vty_out(vty, "%s", VTY_NEWLINE);
3294
3295 vty_out(vty, "Monitor logging: ");
3296 if (zl->maxlvl[ZLOG_DEST_MONITOR] == ZLOG_DISABLED)
3297 vty_out(vty, "disabled");
3298 else
3299 vty_out(vty, "level %s",
3300 zlog_priority[zl->maxlvl[ZLOG_DEST_MONITOR]]);
3301 vty_out(vty, "%s", VTY_NEWLINE);
3302
3303 vty_out(vty, "File logging: ");
3304 if ((zl->maxlvl[ZLOG_DEST_FILE] == ZLOG_DISABLED) || !zl->fp)
3305 vty_out(vty, "disabled");
3306 else
3307 vty_out(vty, "level %s, filename %s",
3308 zlog_priority[zl->maxlvl[ZLOG_DEST_FILE]],
3309 zl->filename);
3310 vty_out(vty, "%s", VTY_NEWLINE);
3311
3312 vty_out(vty, "Protocol name: %s%s",
3313 zlog_proto_names[zl->protocol], VTY_NEWLINE);
3314 vty_out(vty, "Record priority: %s%s",
3315 (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);
3316
3317 return CMD_SUCCESS;
3318}
3319
3320DEFUN(config_log_stdout,
3321 config_log_stdout_cmd,
3322 "log stdout", "Logging control\n" "Set stdout logging level\n")
3323{
3324 zlog_set_level(NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl);
3325 return CMD_SUCCESS;
3326}
3327
3328DEFUN(config_log_stdout_level,
3329 config_log_stdout_level_cmd,
3330 "log stdout " LOG_LEVELS,
3331 "Logging control\n" "Set stdout logging level\n" LOG_LEVEL_DESC)
3332{
3333 int level;
3334
3335 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3336 return CMD_ERR_NO_MATCH;
3337 zlog_set_level(NULL, ZLOG_DEST_STDOUT, level);
3338 return CMD_SUCCESS;
3339}
3340
3341DEFUN(no_config_log_stdout,
3342 no_config_log_stdout_cmd,
3343 "no log stdout [LEVEL]",
3344 NO_STR "Logging control\n" "Cancel logging to stdout\n" "Logging level\n")
3345{
3346 zlog_set_level(NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED);
3347 return CMD_SUCCESS;
3348}
3349
3350DEFUN(config_log_monitor,
3351 config_log_monitor_cmd,
3352 "log monitor",
3353 "Logging control\n" "Set terminal line (monitor) logging level\n")
3354{
3355 zlog_set_level(NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl);
3356 return CMD_SUCCESS;
3357}
3358
3359DEFUN(config_log_monitor_level,
3360 config_log_monitor_level_cmd,
3361 "log monitor " LOG_LEVELS,
3362 "Logging control\n"
3363 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
3364{
3365 int level;
3366
3367 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3368 return CMD_ERR_NO_MATCH;
3369 zlog_set_level(NULL, ZLOG_DEST_MONITOR, level);
3370 return CMD_SUCCESS;
3371}
3372
3373DEFUN(no_config_log_monitor,
3374 no_config_log_monitor_cmd,
3375 "no log monitor [LEVEL]",
3376 NO_STR
3377 "Logging control\n"
3378 "Disable terminal line (monitor) logging\n" "Logging level\n")
3379{
3380 zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
3381 return CMD_SUCCESS;
3382}
3383
3384static int set_log_file(struct vty *vty, const char *fname, int loglevel)
3385{
3386 int ret;
3387 char *p = NULL;
3388 const char *fullpath;
3389
3390 /* Path detection. */
3391 if (!IS_DIRECTORY_SEP(*fname)) {
3392 char cwd[MAXPATHLEN + 1];
3393 cwd[MAXPATHLEN] = '\0';
3394
3395 if (getcwd(cwd, MAXPATHLEN) == NULL) {
3396 zlog_err("config_log_file: Unable to alloc mem!");
3397 return CMD_WARNING;
3398 }
3399
3400 if ((p = _talloc_zero(tall_vcmd_ctx,
3401 strlen(cwd) + strlen(fname) + 2),
3402 "set_log_file")
3403 == NULL) {
3404 zlog_err("config_log_file: Unable to alloc mem!");
3405 return CMD_WARNING;
3406 }
3407 sprintf(p, "%s/%s", cwd, fname);
3408 fullpath = p;
3409 } else
3410 fullpath = fname;
3411
3412 ret = zlog_set_file(NULL, fullpath, loglevel);
3413
3414 if (p)
3415 talloc_free(p);
3416
3417 if (!ret) {
3418 vty_out(vty, "can't open logfile %s\n", fname);
3419 return CMD_WARNING;
3420 }
3421
3422 if (host.logfile)
3423 talloc_free(host.logfile);
3424
3425 host.logfile = talloc_strdup(tall_vty_cmd_ctx, fname);
3426
3427 return CMD_SUCCESS;
3428}
3429
3430DEFUN(config_log_file,
3431 config_log_file_cmd,
3432 "log file FILENAME",
3433 "Logging control\n" "Logging to file\n" "Logging filename\n")
3434{
3435 return set_log_file(vty, argv[0], zlog_default->default_lvl);
3436}
3437
3438DEFUN(config_log_file_level,
3439 config_log_file_level_cmd,
3440 "log file FILENAME " LOG_LEVELS,
3441 "Logging control\n"
3442 "Logging to file\n" "Logging filename\n" LOG_LEVEL_DESC)
3443{
3444 int level;
3445
3446 if ((level = level_match(argv[1])) == ZLOG_DISABLED)
3447 return CMD_ERR_NO_MATCH;
3448 return set_log_file(vty, argv[0], level);
3449}
3450
3451DEFUN(no_config_log_file,
3452 no_config_log_file_cmd,
3453 "no log file [FILENAME]",
3454 NO_STR
3455 "Logging control\n" "Cancel logging to file\n" "Logging file name\n")
3456{
3457 zlog_reset_file(NULL);
3458
3459 if (host.logfile)
3460 talloc_free(host.logfile);
3461
3462 host.logfile = NULL;
3463
3464 return CMD_SUCCESS;
3465}
3466
3467ALIAS(no_config_log_file,
3468 no_config_log_file_level_cmd,
3469 "no log file FILENAME LEVEL",
3470 NO_STR
3471 "Logging control\n"
3472 "Cancel logging to file\n" "Logging file name\n" "Logging level\n")
3473
3474 DEFUN(config_log_syslog,
3475 config_log_syslog_cmd,
3476 "log syslog", "Logging control\n" "Set syslog logging level\n")
3477{
3478 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3479 return CMD_SUCCESS;
3480}
3481
3482DEFUN(config_log_syslog_level,
3483 config_log_syslog_level_cmd,
3484 "log syslog " LOG_LEVELS,
3485 "Logging control\n" "Set syslog logging level\n" LOG_LEVEL_DESC)
3486{
3487 int level;
3488
3489 if ((level = level_match(argv[0])) == ZLOG_DISABLED)
3490 return CMD_ERR_NO_MATCH;
3491 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, level);
3492 return CMD_SUCCESS;
3493}
3494
3495DEFUN_DEPRECATED(config_log_syslog_facility,
3496 config_log_syslog_facility_cmd,
3497 "log syslog facility " LOG_FACILITIES,
3498 "Logging control\n"
3499 "Logging goes to syslog\n"
3500 "(Deprecated) Facility parameter for syslog messages\n"
3501 LOG_FACILITY_DESC)
3502{
3503 int facility;
3504
3505 if ((facility = facility_match(argv[0])) < 0)
3506 return CMD_ERR_NO_MATCH;
3507
3508 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
3509 zlog_default->facility = facility;
3510 return CMD_SUCCESS;
3511}
3512
3513DEFUN(no_config_log_syslog,
3514 no_config_log_syslog_cmd,
3515 "no log syslog [LEVEL]",
3516 NO_STR "Logging control\n" "Cancel logging to syslog\n" "Logging level\n")
3517{
3518 zlog_set_level(NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
3519 return CMD_SUCCESS;
3520}
3521
3522ALIAS(no_config_log_syslog,
3523 no_config_log_syslog_facility_cmd,
3524 "no log syslog facility " LOG_FACILITIES,
3525 NO_STR
3526 "Logging control\n"
3527 "Logging goes to syslog\n"
3528 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3529
3530 DEFUN(config_log_facility,
3531 config_log_facility_cmd,
3532 "log facility " LOG_FACILITIES,
3533 "Logging control\n"
3534 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
3535{
3536 int facility;
3537
3538 if ((facility = facility_match(argv[0])) < 0)
3539 return CMD_ERR_NO_MATCH;
3540 zlog_default->facility = facility;
3541 return CMD_SUCCESS;
3542}
3543
3544DEFUN(no_config_log_facility,
3545 no_config_log_facility_cmd,
3546 "no log facility [FACILITY]",
3547 NO_STR
3548 "Logging control\n"
3549 "Reset syslog facility to default (daemon)\n" "Syslog facility\n")
3550{
3551 zlog_default->facility = LOG_DAEMON;
3552 return CMD_SUCCESS;
3553}
3554
3555DEFUN_DEPRECATED(config_log_trap,
3556 config_log_trap_cmd,
3557 "log trap " LOG_LEVELS,
3558 "Logging control\n"
3559 "(Deprecated) Set logging level and default for all destinations\n"
3560 LOG_LEVEL_DESC)
3561{
3562 int new_level;
3563 int i;
3564
3565 if ((new_level = level_match(argv[0])) == ZLOG_DISABLED)
3566 return CMD_ERR_NO_MATCH;
3567
3568 zlog_default->default_lvl = new_level;
3569 for (i = 0; i < ZLOG_NUM_DESTS; i++)
3570 if (zlog_default->maxlvl[i] != ZLOG_DISABLED)
3571 zlog_default->maxlvl[i] = new_level;
3572 return CMD_SUCCESS;
3573}
3574
3575DEFUN_DEPRECATED(no_config_log_trap,
3576 no_config_log_trap_cmd,
3577 "no log trap [LEVEL]",
3578 NO_STR
3579 "Logging control\n"
3580 "Permit all logging information\n" "Logging level\n")
3581{
3582 zlog_default->default_lvl = LOG_DEBUG;
3583 return CMD_SUCCESS;
3584}
3585
3586DEFUN(config_log_record_priority,
3587 config_log_record_priority_cmd,
3588 "log record-priority",
3589 "Logging control\n"
3590 "Log the priority of the message within the message\n")
3591{
3592 zlog_default->record_priority = 1;
3593 return CMD_SUCCESS;
3594}
3595
3596DEFUN(no_config_log_record_priority,
3597 no_config_log_record_priority_cmd,
3598 "no log record-priority",
3599 NO_STR
3600 "Logging control\n"
3601 "Do not log the priority of the message within the message\n")
3602{
3603 zlog_default->record_priority = 0;
3604 return CMD_SUCCESS;
3605}
3606#endif
3607
3608DEFUN(banner_motd_file,
3609 banner_motd_file_cmd,
3610 "banner motd file [FILE]",
3611 "Set banner\n" "Banner for motd\n" "Banner from a file\n" "Filename\n")
3612{
3613 if (host.motdfile)
3614 talloc_free(host.motdfile);
3615 host.motdfile = talloc_strdup(tall_vty_cmd_ctx, argv[0]);
3616
3617 return CMD_SUCCESS;
3618}
3619
3620DEFUN(banner_motd_default,
3621 banner_motd_default_cmd,
3622 "banner motd default",
3623 "Set banner string\n" "Strings for motd\n" "Default string\n")
3624{
3625 host.motd = default_motd;
3626 return CMD_SUCCESS;
3627}
3628
3629DEFUN(no_banner_motd,
3630 no_banner_motd_cmd,
3631 "no banner motd", NO_STR "Set banner string\n" "Strings for motd\n")
3632{
3633 host.motd = NULL;
3634 if (host.motdfile)
3635 talloc_free(host.motdfile);
3636 host.motdfile = NULL;
3637 return CMD_SUCCESS;
3638}
3639
3640/* Set config filename. Called from vty.c */
3641void host_config_set(const char *filename)
3642{
3643 host.config = talloc_strdup(tall_vty_cmd_ctx, filename);
3644}
3645
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003646/*! Deprecated, now happens implicitly when calling install_node().
3647 * Users of the API may still attempt to call this function, hence
3648 * leave it here as a no-op. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +00003649void install_default(int node)
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003650{
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003651}
3652
3653/*! Deprecated, now happens implicitly when calling install_node().
3654 * Users of the API may still attempt to call this function, hence
3655 * leave it here as a no-op. */
3656void vty_install_default(int node)
3657{
3658}
3659
3660/*! Install common commands like 'exit' and 'list'. */
3661static void install_basic_node_commands(int node)
3662{
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003663 install_element(node, &config_help_cmd);
3664 install_element(node, &config_list_cmd);
3665
3666 install_element(node, &config_write_terminal_cmd);
3667 install_element(node, &config_write_file_cmd);
3668 install_element(node, &config_write_memory_cmd);
3669 install_element(node, &config_write_cmd);
3670 install_element(node, &show_running_config_cmd);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +02003671
3672 install_element(node, &config_exit_cmd);
3673
3674 if (node >= CONFIG_NODE) {
3675 /* It's not a top node. */
3676 install_element(node, &config_end_cmd);
3677 }
3678}
3679
Neels Hofmeyr69054e22017-10-19 02:44:57 +02003680/*! Return true if a node is installed by install_basic_node_commands(), so
3681 * that we can avoid repeating them for each and every node during 'show
3682 * running-config' */
3683static bool vty_command_is_common(struct cmd_element *cmd)
3684{
3685 if (cmd == &config_help_cmd
3686 || cmd == &config_list_cmd
3687 || cmd == &config_write_terminal_cmd
3688 || cmd == &config_write_file_cmd
3689 || cmd == &config_write_memory_cmd
3690 || cmd == &config_write_cmd
3691 || cmd == &show_running_config_cmd
3692 || cmd == &config_exit_cmd
3693 || cmd == &config_end_cmd)
3694 return true;
3695 return false;
3696}
3697
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003698/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003699 * Write the current running config to a given file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003700 * \param[in] vty the vty of the code
3701 * \param[in] filename where to store the file
3702 * \return 0 in case of success.
3703 *
3704 * If the filename already exists create a filename.sav
3705 * version with the current code.
3706 *
3707 */
3708int osmo_vty_write_config_file(const char *filename)
3709{
3710 char *failed_file;
3711 int rc;
3712
3713 rc = write_config_file(filename, &failed_file);
3714 talloc_free(failed_file);
3715 return rc;
3716}
3717
3718/**
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003719 * Save the current state to the config file
Holger Hans Peter Freyther738f1332012-03-24 18:26:24 +01003720 * \return 0 in case of success.
3721 *
3722 * If the filename already exists create a filename.sav
3723 * version with the current code.
3724 *
3725 */
3726int osmo_vty_save_config_file(void)
3727{
3728 char *failed_file;
3729 int rc;
3730
3731 if (host.config == NULL)
3732 return -7;
3733
3734 rc = write_config_file(host.config, &failed_file);
3735 talloc_free(failed_file);
3736 return rc;
3737}
3738
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003739/* Initialize command interface. Install basic nodes and commands. */
3740void cmd_init(int terminal)
3741{
3742 /* Allocate initial top vector of commands. */
3743 cmdvec = vector_init(VECTOR_MIN_SIZE);
3744
3745 /* Default host value settings. */
3746 host.name = NULL;
3747 host.password = NULL;
3748 host.enable = NULL;
3749 host.logfile = NULL;
3750 host.config = NULL;
3751 host.lines = -1;
3752 host.motd = default_motd;
3753 host.motdfile = NULL;
3754
3755 /* Install top nodes. */
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003756 install_node_bare(&view_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003757 install_node(&enable_node, NULL);
Neels Hofmeyrf4f23bd2017-09-20 15:39:37 +02003758 install_node_bare(&auth_node, NULL);
3759 install_node_bare(&auth_enable_node, NULL);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003760 install_node(&config_node, config_write_host);
3761
3762 /* Each node's basic commands. */
3763 install_element(VIEW_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003764 install_element(VIEW_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003765 if (terminal) {
3766 install_element(VIEW_NODE, &config_list_cmd);
3767 install_element(VIEW_NODE, &config_exit_cmd);
3768 install_element(VIEW_NODE, &config_help_cmd);
3769 install_element(VIEW_NODE, &config_enable_cmd);
3770 install_element(VIEW_NODE, &config_terminal_length_cmd);
3771 install_element(VIEW_NODE, &config_terminal_no_length_cmd);
3772 install_element(VIEW_NODE, &echo_cmd);
3773 }
3774
3775 if (terminal) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003776 install_element(ENABLE_NODE, &config_disable_cmd);
3777 install_element(ENABLE_NODE, &config_terminal_cmd);
3778 install_element (ENABLE_NODE, &copy_runningconfig_startupconfig_cmd);
3779 }
3780 install_element (ENABLE_NODE, &show_startup_config_cmd);
3781 install_element(ENABLE_NODE, &show_version_cmd);
Holger Hans Peter Freyther8297c812011-11-18 23:14:24 +01003782 install_element(ENABLE_NODE, &show_online_help_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003783
3784 if (terminal) {
3785 install_element(ENABLE_NODE, &config_terminal_length_cmd);
3786 install_element(ENABLE_NODE, &config_terminal_no_length_cmd);
3787 install_element(ENABLE_NODE, &echo_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003788 }
3789
3790 install_element(CONFIG_NODE, &hostname_cmd);
3791 install_element(CONFIG_NODE, &no_hostname_cmd);
3792
3793 if (terminal) {
3794 install_element(CONFIG_NODE, &password_cmd);
3795 install_element(CONFIG_NODE, &password_text_cmd);
3796 install_element(CONFIG_NODE, &enable_password_cmd);
3797 install_element(CONFIG_NODE, &enable_password_text_cmd);
3798 install_element(CONFIG_NODE, &no_enable_password_cmd);
3799
3800#ifdef VTY_CRYPT_PW
3801 install_element(CONFIG_NODE, &service_password_encrypt_cmd);
3802 install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
3803#endif
3804 install_element(CONFIG_NODE, &banner_motd_default_cmd);
3805 install_element(CONFIG_NODE, &banner_motd_file_cmd);
3806 install_element(CONFIG_NODE, &no_banner_motd_cmd);
3807 install_element(CONFIG_NODE, &service_terminal_length_cmd);
3808 install_element(CONFIG_NODE, &no_service_terminal_length_cmd);
3809
3810 }
3811 srand(time(NULL));
3812}
Harald Welte7acb30c2011-08-17 17:13:48 +02003813
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02003814/*! @} */