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