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